From 9b45d5df9e4d665ec52229de09395d0ce6f0e310 Mon Sep 17 00:00:00 2001 From: Yasen Pramatarov Date: Wed, 23 Oct 2024 13:06:59 +0300 Subject: [PATCH] Adds status checks for the agents in status page --- README.md | 1 + app/classes/agent.php | 58 ++++++++++++++++++++++++++++--- app/pages/status.php | 33 ++++++++++++++++++ app/templates/status-agent.php | 7 +++- app/templates/status-platform.php | 2 +- public_html/phpinfo.php | 3 ++ public_html/static/css/main.css | 1 + 7 files changed, 98 insertions(+), 7 deletions(-) create mode 100644 public_html/phpinfo.php diff --git a/README.md b/README.md index 60edafe..90bb930 100644 --- a/README.md +++ b/README.md @@ -43,6 +43,7 @@ Chart.js is used in this project and is licensed under the MIT License. See lice - web server (deb: apache | nginx) - php support in the web server (deb: php-fpm | libapache2-mod-php) - pdo and pdo_sqlite support in php (deb: php-db, php-sqlite3) uncomment in php.ini: ;extension=pdo_sqlite +- php-curl module ## installation diff --git a/app/classes/agent.php b/app/classes/agent.php index 4a6dfb9..d8bf270 100644 --- a/app/classes/agent.php +++ b/app/classes/agent.php @@ -40,6 +40,30 @@ class Agent { return $query->fetchAll(PDO::FETCH_ASSOC); } + // get details of a specified agent ID + public function getAgentIDDetails($agent_id) { + $sql = 'SELECT + ja.id, + ja.platform_id, + ja.agent_type_id, + ja.url, + ja.secret_key, + jat.description AS agent_description, + jat.endpoint AS agent_endpoint + FROM + jilo_agents ja + JOIN + jilo_agent_types jat ON ja.agent_type_id = jat.id + WHERE + ja.id = :agent_id'; + + $query = $this->db->prepare($sql); + $query->bindParam(':agent_id', $agent_id); + $query->execute(); + + return $query->fetchAll(PDO::FETCH_ASSOC); + } + // get agent types public function getAgentTypes() { $sql = 'SELECT * @@ -165,32 +189,56 @@ class Agent { public function fetchAgent($agent_id, $force = false) { // we need agent details for URL and JWT token - $agent = $this->getAgentDetails($agent_id); + $agentDetails = $this->getAgentIDDetails($agent_id); + + // Safe exit in case the agent is not found + if (empty($agentDetails)) { + return json_encode(['error' => 'Agent not found']); + } + + $agent = $agentDetails[0]; $agent_cache_name = 'agent' . $agent_id . '_cache'; $agent_cache_time = 'agent' . $agent_id . '_time'; // check if the cache is still valid, unless force-refresh is requested - if (!$force && this->checkAgentCache($agent_id)) { + if (!$force && $this->checkAgentCache($agent_id)) { return $_SESSION[$agent_cache_name]; } + // generate the JWT token + $payload = [ + 'agent_id' => $agent_id, + 'timestamp' => time() + ]; + $jwt = $this->generateAgentToken($payload, $agent['secret_key']); + // Make the API request $ch = curl_init(); - curl_setopt($ch, CURLOPT_URL, $agent[0]['url']); + curl_setopt($ch, CURLOPT_URL, $agent['url'] . $agent['agent_endpoint']); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_TIMEOUT, 10); // timeout 10 seconds + curl_setopt($ch, CURLOPT_HTTPHEADER, [ + 'Authorization: Bearer ' . $jwt, + 'Content-Type: application/json' + ]); $response = curl_exec($ch); $curl_error = curl_error($ch); $curl_errno = curl_errno($ch); + $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); - // general curl error - if ($curl_error) { + // curl error + if ($curl_errno) { return json_encode(['error' => 'curl error: ' . $curl_error]); } + // response is not 200 OK + if ($http_code !== 200) { + return json_encode(['error' => 'HTTP error: ' . $http_code]); + } + // other custom error(s) if (strpos($response, 'Auth header not received') !== false) { return json_encode(['error' => 'Auth header not received']); diff --git a/app/pages/status.php b/app/pages/status.php index 8a9202a..5e88044 100644 --- a/app/pages/status.php +++ b/app/pages/status.php @@ -9,9 +9,42 @@ $agentObject = new Agent($dbWeb); include '../app/templates/status-server.php'; foreach ($platformsAll as $platform) { + include '../app/templates/status-platform.php'; + $agentDetails = $agentObject->getAgentDetails($platform['id']); foreach ($agentDetails as $agent) { + $agent_url = parse_url($agent['url']); + $agent_protocol = isset($agent_url['scheme']) ? $agent_url['scheme']: ''; + $agent_host = isset($agent_url['host']) ? $agent_url['host']: ''; + $agent_port = isset($agent_url['port']) ? $agent_url['port']: ''; + + // we get agent data to check availability + $agent_response = $agentObject->fetchAgent($agent['id'], true); + $agent_data = json_decode($agent_response); + + if (json_last_error() === JSON_ERROR_NONE) { + $agent_availability = 'unknown'; + foreach ($agent_data as $key => $value) { + if ($key === 'error') { + $agent_availability = '' . htmlspecialchars($value) . ''; + break; + } + if (preg_match('/_state$/', $key)) { + if ($value === 'error') { + $agent_availability = 'not running'; + break; + } + if ($value === 'running') { + $agent_availability = 'running'; + break; + } + } + } + } else { + $agent_availability = 'json error'; + } + include '../app/templates/status-agent.php'; } } diff --git a/app/templates/status-agent.php b/app/templates/status-agent.php index 94745bb..e85c4c8 100644 --- a/app/templates/status-agent.php +++ b/app/templates/status-agent.php @@ -3,7 +3,12 @@

- Jilo Agent: + Jilo Agent : + +
+ host: , + port: , + endpoint:

diff --git a/app/templates/status-platform.php b/app/templates/status-platform.php index 38c4206..463f154 100644 --- a/app/templates/status-platform.php +++ b/app/templates/status-platform.php @@ -4,7 +4,7 @@

- Jitsi Meet platform: + Jitsi Meet platform:

diff --git a/public_html/phpinfo.php b/public_html/phpinfo.php new file mode 100644 index 0000000..968c8df --- /dev/null +++ b/public_html/phpinfo.php @@ -0,0 +1,3 @@ + \ No newline at end of file diff --git a/public_html/static/css/main.css b/public_html/static/css/main.css index 15651b1..859f366 100644 --- a/public_html/static/css/main.css +++ b/public_html/static/css/main.css @@ -140,6 +140,7 @@ html, body { .main-content { flex-grow: 1; transition: width 0.5s ease; + margin-bottom: 50px; /* width: 80%;*/ } .main-content.expanded {