diff --git a/UptimeKuma/UptimeKuma.php b/UptimeKuma/UptimeKuma.php index 2ee8aae6ef..6e3552419a 100644 --- a/UptimeKuma/UptimeKuma.php +++ b/UptimeKuma/UptimeKuma.php @@ -2,6 +2,94 @@ namespace App\SupportedApps\UptimeKuma; -class UptimeKuma extends \App\SupportedApps +class UptimeKuma extends \App\SupportedApps implements \App\EnhancedApps { + public $config; + + private $mapping = [ + 0 => "down", + 1 => "up", + 2 => "pending", + 3 => "maintenance", + ]; + + public function __construct() + { + } + + private function getAttrs() + { + if (empty($this->config->apikey)) { + return []; + } + + $basicAuthValue = base64_encode(":" . $this->config->apikey); + + return [ + "headers" => [ + "Authorization" => "Basic " . $basicAuthValue, + ] + ]; + } + + public function test() + { + + $test = parent::appTest($this->url("metrics"), $this->getAttrs()); + echo $test->status; + } + + public function livestats() + { + $status = "inactive"; + + $response = parent::execute($this->url("metrics"), $this->getAttrs()); + $body = $response->getBody(); + + $lines = explode("\n", $body); + + $data = [ + "up" => 0, + "down" => 0, + "pending" => 0, + "maintenance" => 0, + "unknown" => 0, + ]; + + foreach ($lines as $line) { + if (strlen($line) === 0 || strpos($line, '#') === 0) { + // If the line is empty or is a comment we can skip it + continue; + } + + if (strpos($line, 'monitor_status') !== 0) { + // If the line is a metric but not a monitor we can ignore it + continue; + } + + // We only really care about the state, which is the integer at the end + // of the line. + // + // This can be 0 (Down), 1 (Up), 2 (Pending), 3 (Maintenance) + // + // We only care about the down or up but let's translate all of them. + $state = intval(substr($line, strrpos($line, ' '))); + + $data[$this->mapping[$state] ?? 'unknown']++; + } + + if ($data["down"] > 0) { + $status = "active"; + } + + return parent::getLiveStats($status, $data); + } + + public function url($endpoint) + { + $api_url = parent::normaliseurl($this->config->url) . + $endpoint; + + return $api_url; + } } diff --git a/UptimeKuma/app.json b/UptimeKuma/app.json index 7e1807b271..2ebffba5fb 100644 --- a/UptimeKuma/app.json +++ b/UptimeKuma/app.json @@ -4,7 +4,7 @@ "website": "https://uptime.kuma.pet", "license": "MIT License", "description": "It is a self-hosted monitoring tool like \"Uptime Robot\".", - "enhanced": false, + "enhanced": true, "tile_background": "dark", "icon": "uptimekuma.svg" } diff --git a/UptimeKuma/config.blade.php b/UptimeKuma/config.blade.php new file mode 100644 index 0000000000..aa383f03de --- /dev/null +++ b/UptimeKuma/config.blade.php @@ -0,0 +1,32 @@ +