Skip to content

Commit

Permalink
Merge pull request #706 from imnotjames/feat/uptime-kuma-enhanced
Browse files Browse the repository at this point in the history
feat: enhance uptime kuma app
  • Loading branch information
mvdkleijn authored Feb 28, 2024
2 parents 57a74e5 + e3b0181 commit fafc559
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 5 deletions.
90 changes: 89 additions & 1 deletion UptimeKuma/UptimeKuma.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
2 changes: 1 addition & 1 deletion UptimeKuma/app.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
32 changes: 32 additions & 0 deletions UptimeKuma/config.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<h2>{{ __('app.apps.config') }} ({{ __('app.optional') }}) @include('items.enable')</h2>
<div class="items">
<input type="hidden" data-config="dataonly" class="config-item" name="config[dataonly]" value="1" />
<div class="input">
<label>{{ strtoupper(__('app.url')) }}</label>
{!! Form::text('config[override_url]', isset($item) ? $item->getconfig()->override_url : null, ['placeholder' => __('app.apps.override'), 'id' => 'override_url', 'class' => 'form-control']) !!}
</div>
<div class="input">
<label>{{ __('app.apps.apikey') }}</label>
{!! Form::text('config[apikey]', isset($item) ? ($item->getconfig()->apikey ?? null) : null, ['placeholder' => __('app.apps.apikey'), 'data-config' => 'apikey', 'class' => 'form-control config-item']) !!}
</div>
<div class="input">
<label>Skip TLS verification</label>
<div class="toggleinput" style="margin-top: 26px; padding-left: 15px;">
{!! Form::hidden('config[ignore_tls]', 0, ['class' => 'config-item', 'data-config' => 'ignore_tls']) !!}
<label class="switch">
<?php
$checked = false;
if (isset($item) && !empty($item) && isset($item->getconfig()->ignore_tls)) {
$checked = $item->getconfig()->ignore_tls;
}
$set_checked = $checked ? ' checked="checked"' : '';
?>
<input type="checkbox" class="config-item" data-config="ignore_tls" name="config[ignore_tls]" value="1" <?php echo $set_checked; ?> />
<span class="slider round"></span>
</label>
</div>
</div>
<div class="input">
<button style="margin-top: 32px;" class="btn test" id="test_config">Test</button>
</div>
</div>
10 changes: 10 additions & 0 deletions UptimeKuma/livestats.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<ul class="livestats">
<li>
<span class="title">Up</span>
<strong>{!! $up ?? 0 !!}</strong>
</li>
<li>
<span class="title">Down</span>
<strong>{!! $down ?? 0 !!}</strong>
</li>
</ul>
3 changes: 0 additions & 3 deletions UptimeKuma/uptime-kuma.svg

This file was deleted.

0 comments on commit fafc559

Please sign in to comment.