texto = 'SELECT apikey FROM usuarios WHERE idusuario=1'; $rs = new Recordset; $rs->Comando = &$cmd; if (!$rs->Abrir()) return (null); $rs->Primero(); if ($rs->EOF) { return (null); } $k = $rs->campos['apikey']; $rs->Cerrar(); return $k; } /** * db_fetchCals() * @brief Retrieves calendars from the database * @return array calendars */ function db_fetchCals() { global $cnx; $tbl = array(); $cmd = CreaComando ($cnx); if (!$cmd) { die ('ACCESS_ERROR'); } $cmd->texto = 'SELECT idcalendario, description, json_text FROM calendarios'; $rs = new Recordset; $rs->Comando = &$cmd; if (!$rs->Abrir()) return (false); $rs->Primero(); if ($rs->EOF) { return ($tbl); } $id = $rs->campos['idcalendario']; $desc = $rs->campos['description']; $txt = $rs->campos['json_text']; $tbl[$id] = array ( 'id' => $id, 'desc' => $desc, 'json' => json_decode($txt), ); while (1) { $rs->Siguiente(); if ($rs->EOF) { break; } $id = $rs->campos['idcalendario']; $desc = $rs->campos['description']; $txt = $rs->campos['json_text']; $tbl[$id] = array ( 'id' => $id, 'desc' => $desc, 'json' => json_decode($txt), ); } $rs->Cerrar(); return $tbl; } /** * uds_login ($auth, $username, $password) * @brief Log into UDS * @param string UDS authenticator * @param string user * @param string password * @return object response from the server, contains result, token, version and scrambler */ function uds_login ($auth, $username, $password) { global $UDS_REST_URL; $parameters = [ 'auth' => $auth, 'username' => $username, 'password' => $password ]; $payload = json_encode ($parameters); $ch = curl_init(); curl_setopt ($ch, CURLOPT_URL, $UDS_REST_URL."auth/login"); curl_setopt ($ch, CURLOPT_POST, 1); curl_setopt ($ch, CURLOPT_POSTFIELDS, $payload); curl_setopt ($ch, CURLOPT_HTTPHEADER, array ('Content-Type:application/json')); curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true); // In real life you should use something like: // curl_setopt ($ch, CURLOPT_POSTFIELDS, http_build_query (array ('postvar1' => 'value1'))); $server_output = json_decode (curl_exec ($ch)); curl_close ($ch); return $server_output; } /** * uds_getServiceInfo ($headers, $providerId, $serviceId) * @brief Get service information from UDS * @param array UDS headers * @param string provider id * @param string service id * @return object response from the server */ function uds_getServiceInfo ($headers, $providerId, $serviceId) { global $UDS_REST_URL; $ch = curl_init(); curl_setopt ($ch, CURLOPT_URL, $UDS_REST_URL."providers/".$providerId."/services/".$serviceId); curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt ($ch, CURLOPT_HTTPHEADER, $headers); $server_output = json_decode (curl_exec ($ch)); print_r (curl_error ($ch)); curl_close ($ch); return $server_output; } /** * uds_getAllServicePools ($headers) * @brief Get all service pools from UDS * @param array UDS headers * @return object response from the server */ function uds_getAllServicePools ($headers) { global $UDS_REST_URL; $ch = curl_init(); curl_setopt ($ch, CURLOPT_URL, $UDS_REST_URL."servicespools/overview"); curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt ($ch, CURLOPT_HTTPHEADER, $headers); $server_output = json_decode (curl_exec ($ch)); curl_close ($ch); return $server_output; } /** * uds_setServicePool ($headers, $servicePool) * @brief Writes service pool information to UDS * @param array UDS headers * @param object service pool details * @return object response from the server */ function uds_setServicePool ($headers, $servicePool) { global $UDS_REST_URL; $payload = json_encode ($servicePool); $ch = curl_init(); curl_setopt ($ch, CURLOPT_URL, $UDS_REST_URL."servicespools/".$servicePool->id); curl_setopt ($ch, CURLOPT_POST, 1); curl_setopt ($ch, CURLOPT_CUSTOMREQUEST, "PUT"); curl_setopt ($ch, CURLOPT_POSTFIELDS, $payload); curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt ($ch, CURLOPT_HTTPHEADER, $headers); $server_output = curl_exec ($ch); curl_close ($ch); return json_decode ($server_output); } /** * og_getAula ($idCentro, $idAula) * @brief Gets lab information from OG * @param int building id * @param int lab id * @return object response from the server */ function og_getAula ($idCentro, $idAula) { global $OG_REST_URL; global $OG_REST_AUTH; $result = null; $url = $OG_REST_URL."ous/".$idCentro."/labs/".$idAula; $ch = curl_init(); curl_setopt ($ch, CURLOPT_URL, $url); curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0); curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt ($ch, CURLOPT_HTTPHEADER, [ 'Accept: application/json', 'Authorization: '.$OG_REST_AUTH ]); $server_output = json_decode (curl_exec ($ch)); $curl_errno = curl_errno ($ch); $curl_error = curl_error ($ch); if ($curl_errno) { printf ("error (%s) errno (%d)\n", $curl_error, $curl_errno); return null; } $code = curl_getinfo ($ch, CURLINFO_RESPONSE_CODE); curl_close ($ch); if ($code <= 201) { $result = $server_output; } return $result; } /** * og_getClientDiskConfig ($idCentro, $idAula, $idCliente) * @brief Gets information about disks of a client from OG * @param int building id * @param int lab id * @param int client id * @return object response from the server */ function og_getClientDiskConfig ($idCentro, $idAula, $idCliente) { global $OG_REST_URL; global $OG_REST_AUTH; $result = null; $url = $OG_REST_URL."ous/".$idCentro."/labs/".$idAula."/clients/".$idCliente."/diskcfg"; $ch = curl_init(); curl_setopt ($ch, CURLOPT_URL, $url); curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0); curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt ($ch, CURLOPT_HTTPHEADER, [ 'Accept: application/json', 'Authorization: '.$OG_REST_AUTH ]); $server_output = json_decode (curl_exec ($ch)); $curl_errno = curl_errno ($ch); $curl_error = curl_error ($ch); if ($curl_errno) { printf ("error (%s) errno (%d)\n", $curl_error, $curl_errno); return null; } $code = curl_getinfo ($ch, CURLINFO_RESPONSE_CODE); curl_close ($ch); if ($code <= 201) { $result = $server_output; } return $result; } /** * og_aula_is_remote ($cal_id) * @brief Checks whether lab is remote right now * @param int calendar id * @return bool whether lab is remote (true) or not (false) */ function og_aula_is_remote ($cal_id) { global $cals, $tz; $ruleset = $cals[$cal_id]['json']->remotepc_calendar->ruleset; //print_r($ruleset); $dt = new DateTime ('now', new DateTimeZone ($tz)); $dow = strtolower ($dt->format ('D')); // textual representation of a day, three letters, 'Mon' through 'Sun' $hour = $dt->format ('G'); // 24-hour without leading zeros $ts = $dt->format ('U'); // unix epoch // primero recorremos todas las reglas mirando solo las que tienen remote=false (ie. presencial) // si estamos fuera de todos estos rangos, es que estamos en remoto: return 1 // si estamos dentro de alguno de ellos, es que estamos en presencial: continuamos $presencial = 0; foreach ($ruleset as $r) { if (array_key_exists ('remote', $r) and $r->remote) { continue; } if (!array_key_exists ($dow, $r) or !$r->$dow) { continue; } if ($hour < $r->from_hr or $hour > $r->to_hr) { continue; } $presencial = 1; break; } if (0 == $presencial) { return 1; } // si llegamos aqui, es que estamos en uno de los rangos de presencial, pero puede haber excepciones // recorremos todas las reglas mirando las que tienen remote=true // si estamos en alguno de esos rangos, return 1 foreach ($ruleset as $r) { if (!array_key_exists ('remote', $r) or !$r->remote) { continue; } if ($ts >= $r->from_ts and $ts <= $r->to_ts) { return 1; } } // si llegamos aqui es que estamos dentro de presencial y fuera de las excepciones: return 0 return 0; } // {"remotepc_calendar":{"timezone":"Europe/Madrid","ruleset":[ // {"remote":false,"mon":true,"tue":true,"wed":true,"thu":true,"fri":true,"from_hr":"8","to_hr":"17"}, // {"remote":true,"reason":"exc","from_ts":1709074800,"to_ts":1706569199} // ]}} function og_ordenador_cumple_criterios ($client) { if ($client->status == 'oglive' || $client->status == 'linux' || $client->status == 'windows') { return 1; } return 0; } /** * og_sondeoAula ($idCentro, $idAula, $idImagen) * @brief Gets how many clients are available for remotepc from OG * @param int building id * @param int lab id * @param int image id * @return int number of clients available for remotepc */ function og_sondeoAula ($idCentro, $idAula, $idImagen) { global $OG_REST_URL; global $OG_REST_AUTH; $aula = og_getAula ($idCentro, $idAula); if (null === $aula || $aula->inremotepc != 1) { return 0; } if (!og_aula_is_remote ($aula->idcalendario)) { return 0; } $url = $OG_REST_URL."ous/".$idCentro."/labs/".$idAula."/clients/status"; $ch = curl_init(); curl_setopt ($ch, CURLOPT_URL, $url); curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0); curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt ($ch, CURLOPT_HTTPHEADER, [ 'Accept: application/json', 'Authorization: '.$OG_REST_AUTH ]); $server_output = json_decode (curl_exec ($ch)); $curl_errno = curl_errno ($ch); $curl_error = curl_error ($ch); if ($curl_errno) { printf ("error (%s) errno (%d)\n", $curl_error, $curl_errno); return null; } $code = curl_getinfo ($ch, CURLINFO_RESPONSE_CODE); curl_close ($ch); if ($code > 201) { return 0; } $clientsOn = 0; foreach ($server_output as $client) { if (!og_ordenador_cumple_criterios ($client)) { continue; } $clientDisk = og_getClientDiskConfig ($idCentro, $idAula, $client->id); if (null === $clientDisk) { continue; } for ($p = 0; $p < count ($clientDisk->diskcfg); $p++) { $part = $clientDisk->diskcfg[$p]; if (isset ($part->image) && $part->image->id == $idImagen) { $clientsOn++; break; } } } return $clientsOn; } $OG_REST_AUTH = db_fetch_apikey(); if (null == $OG_REST_AUTH) { print ("failed to fetch OG API key from the database\n"); exit (1); } $cals = db_fetchCals(); //print_r($cals); $server_output = uds_login ($UDS_AUTHENTICATOR, $UDS_USER, $UDS_PASS); if ($server_output->result != "ok") { print_r (curl_error ($ch)); exit (1); } $headers = uds_getHeaders ($server_output->token, $server_output->scrambler); $servicePools = uds_getAllServicePools ($headers); foreach ($servicePools as $servicePool) { $providerId = $servicePool->provider_id; $serviceId = $servicePool->service_id; $service = uds_getServiceInfo ($headers, $providerId, $serviceId); if ($service === null) { print ("service with providerId ($providerId) serviceId ($serviceId) is null\n"); continue; } // Conectar con opengnsys para ver cuantos pcs hay disponibles y enviar dicho parametro $max_srvs = og_sondeoAula ($service->ou, $service->lab, $service->image); if (null === $max_srvs) { print ("og_sondeoAula failed\n"); continue; } $servicePool->osmanager_id = null; $servicePool->max_srvs = $max_srvs; $servicePool->initial_srvs = $servicePool->max_srvs; $servicePool->cache_l1_srvs = 0; $servicePool->visible = ($servicePool->max_srvs > 0); /* if ($servicePool->initial_srvs > $servicePool->max_srvs){ $servicePool->initial_srvs = $servicePool->max_srvs; } /**/ $sp = uds_setServicePool ($headers, $servicePool); if (null === $sp) { print ("uds_setServicePool failed\n"); continue; } printf ("%s: ou:%d lab:%d imagen:%d max_srvs:%d visible:%d\n", $sp->name, $service->ou, $service->lab, $service->image, $sp->max_srvs, $sp->visible); }