diff --git a/source/packages/plg_system_mokowaas_monitor/src/Extension/Monitor.php b/source/packages/plg_system_mokowaas_monitor/src/Extension/Monitor.php index 750da3a7..addcb36a 100644 --- a/source/packages/plg_system_mokowaas_monitor/src/Extension/Monitor.php +++ b/source/packages/plg_system_mokowaas_monitor/src/Extension/Monitor.php @@ -109,16 +109,30 @@ class Monitor extends CMSPlugin implements SubscriberInterface $app = $this->getApplication(); + $config = Factory::getConfig(); + $payload = [ - 'token' => $healthToken, - 'domain' => $domain, - 'site_name' => Factory::getConfig()->get('sitename', 'Joomla'), - 'site_url' => $siteUrl, - 'joomla_version' => (new Version())->getShortVersion(), - 'php_version' => PHP_VERSION, + 'token' => $healthToken, + 'domain' => $domain, + 'site_name' => $config->get('sitename', 'Joomla'), + 'site_url' => $siteUrl, + 'joomla_version' => (new Version())->getShortVersion(), + 'php_version' => PHP_VERSION, 'mokowaas_version' => $this->getMokoWaaSVersion(), + 'client_info' => [ + 'company' => $config->get('sitename', ''), + 'email' => $config->get('mailfrom', ''), + ], ]; + // Include live health data by calling the local health endpoint + $healthData = $this->fetchLocalHealth($siteUrl, $healthToken); + + if ($healthData !== null) + { + $payload['health'] = $healthData; + } + $endpoint = $baseUrl . '/api/index.php/v1/mokowaasbase/heartbeat'; $json = json_encode($payload, JSON_UNESCAPED_SLASHES); @@ -165,6 +179,42 @@ class Monitor extends CMSPlugin implements SubscriberInterface } } + /** + * Fetch health data from the local site's health endpoint. + * + * @param string $siteUrl Local site URL. + * @param string $healthToken Health API token. + * + * @return array|null Parsed health data or null on failure. + */ + private function fetchLocalHealth(string $siteUrl, string $healthToken): ?array + { + $url = $siteUrl . '/?mokowaas=health'; + + $ch = curl_init($url); + curl_setopt_array($ch, [ + CURLOPT_RETURNTRANSFER => true, + CURLOPT_TIMEOUT => 10, + CURLOPT_FOLLOWLOCATION => true, + CURLOPT_SSL_VERIFYPEER => false, + CURLOPT_HTTPHEADER => [ + 'Authorization: Bearer ' . $healthToken, + 'Accept: application/json', + ], + ]); + + $response = curl_exec($ch); + $code = (int) curl_getinfo($ch, CURLINFO_HTTP_CODE); + curl_close($ch); + + if ($code !== 200 || empty($response)) + { + return null; + } + + return json_decode($response, true) ?: null; + } + /** * Get the installed MokoWaaS package version. */