|
|
@@ -1,7 +1,12 @@
|
|
|
<?php
|
|
|
|
|
|
+use GuzzleHttp\Client;
|
|
|
+use GuzzleHttp\Exception\GuzzleException;
|
|
|
+
|
|
|
trait UptimeKumaHomepageItem
|
|
|
{
|
|
|
+ private static Client $kumaClient;
|
|
|
+
|
|
|
public function uptimeKumaSettingsArray($infoOnly = false)
|
|
|
{
|
|
|
$homepageInformation = [
|
|
|
@@ -46,7 +51,8 @@ trait UptimeKumaHomepageItem
|
|
|
'homepageUptimeKumaAuth'
|
|
|
],
|
|
|
'not_empty' => [
|
|
|
- 'uptimeKumaURL'
|
|
|
+ 'uptimeKumaURL',
|
|
|
+ 'uptimeKumaToken',
|
|
|
]
|
|
|
]
|
|
|
];
|
|
|
@@ -76,12 +82,24 @@ trait UptimeKumaHomepageItem
|
|
|
}
|
|
|
$api = [];
|
|
|
$url = $this->qualifyURL($this->config['uptimeKumaURL']);
|
|
|
- $dataUrl = $url . '/assets/php/loop.php';
|
|
|
try {
|
|
|
- $options = $this->requestOptions($url, $this->config['homepageUptimeKumaRefresh']);
|
|
|
- $response = Requests::get($dataUrl, ['Token' => $this->config['organizrAPI']], $options);
|
|
|
-
|
|
|
- } catch (Requests_Exception $e) {
|
|
|
+ $response = $this->getKumaClient($url, $this->config['uptimeKumaToken'])->get('/metrics');
|
|
|
+
|
|
|
+ $body = $response->getBody()->getContents();
|
|
|
+
|
|
|
+ $body = explode(PHP_EOL, $body);
|
|
|
+ $body = array_filter($body, function (string $item) {
|
|
|
+ return str_starts_with($item, 'monitor_status');
|
|
|
+ });
|
|
|
+ $body = array_map(function (string $item) {
|
|
|
+ try {
|
|
|
+ return $this->parseUptimeKumaStatus($item);
|
|
|
+ } catch (Exception $e) {
|
|
|
+ // do nothing when monitor is disabled
|
|
|
+ }
|
|
|
+ }, $body);
|
|
|
+ $api = array_values(array_filter($body));
|
|
|
+ } catch (GuzzleException $e) {
|
|
|
$this->setLoggerChannel('UptimeKuma')->error($e);
|
|
|
$this->setAPIResponse('error', $e->getMessage(), 401);
|
|
|
return false;
|
|
|
@@ -90,4 +108,46 @@ trait UptimeKumaHomepageItem
|
|
|
$this->setAPIResponse('success', null, 200, $api);
|
|
|
return $api;
|
|
|
}
|
|
|
+
|
|
|
+ private function getKumaClient(string $url, string $token): Client
|
|
|
+ {
|
|
|
+ if (!isset(static::$kumaClient)) {
|
|
|
+ static::$kumaClient = new Client([
|
|
|
+ 'base_uri' => $url,
|
|
|
+ 'headers' => [
|
|
|
+ 'Authorization' => sprintf("Basic %s", base64_encode(':'.$token)),
|
|
|
+ ],
|
|
|
+ ]);
|
|
|
+ }
|
|
|
+
|
|
|
+ return static::$kumaClient;
|
|
|
+ }
|
|
|
+
|
|
|
+ private function parseUptimeKumaStatus(string $status): array
|
|
|
+ {
|
|
|
+ if (substr($status, -1) === '2') {
|
|
|
+ throw new Exception("monitor diasbled");
|
|
|
+ }
|
|
|
+
|
|
|
+ $up = (substr($status, -1)) == '0' ? false : true;
|
|
|
+ $status = substr($status, 15);
|
|
|
+ $status = substr($status, 0, -4);
|
|
|
+ $status = explode(',', $status);
|
|
|
+ $data = [
|
|
|
+ 'name' => $this->getStringBetweenQuotes($status[0]),
|
|
|
+ 'url' => $this->getStringBetweenQuotes($status[2]),
|
|
|
+ 'type' => $this->getStringBetweenQuotes($status[1]),
|
|
|
+ 'status' => $up,
|
|
|
+ ];
|
|
|
+
|
|
|
+ return $data;
|
|
|
+ }
|
|
|
+
|
|
|
+ private function getStringBetweenQuotes(string $input): string
|
|
|
+ {
|
|
|
+ if (preg_match('/"(.*?)"/', $input, $match) == 1) {
|
|
|
+ return $match[1];
|
|
|
+ }
|
|
|
+ return '';
|
|
|
+ }
|
|
|
}
|