Skip to content

Commit 666f8ab

Browse files
authored
Enhanced glances (#758)
* Enhancing Gotify App(Need Testing) * Enhancing Gotify tested and working * Reset gotify on main branch * Enhanced Glaces * Added MemUsage to livestats * Fixed Class Name * Fixed whitespaces and added new line at the end
1 parent c170849 commit 666f8ab

File tree

4 files changed

+123
-2
lines changed

4 files changed

+123
-2
lines changed

Glances/Glances.php

+100-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,105 @@
22

33
namespace App\SupportedApps\Glances;
44

5-
class Glances extends \App\SupportedApps
5+
class Glances extends \App\SupportedApps implements \App\EnhancedApps
66
{
7+
public $config;
8+
9+
public function __construct()
10+
{
11+
}
12+
13+
public function test()
14+
{
15+
$test = parent::appTest($this->url("status"));
16+
echo $test->status;
17+
}
18+
19+
public function livestats()
20+
{
21+
$status = "inactive";
22+
$details = [];
23+
24+
if (isset($this->config->availablestats) && is_array($this->config->availablestats)) {
25+
$details = ["visiblestats" => []];
26+
foreach ($this->config->availablestats as $stat) {
27+
$newstat = new \stdClass();
28+
$availableStats = self::getAvailableStats();
29+
30+
if (isset($availableStats[$stat])) {
31+
$newstat->title = $availableStats[$stat];
32+
33+
// Fetch CpuTotal
34+
if ($stat === "CpuTotal") {
35+
$Response = parent::execute($this->url("cpu/total"));
36+
$result = json_decode($Response->getBody());
37+
if (isset($result->total)) {
38+
$newstat->value = $result->total;
39+
} else {
40+
$newstat->value = null; // or some default value
41+
}
42+
}
43+
44+
// Fetch MemTotal
45+
if ($stat === "MemTotal") {
46+
$Response = parent::execute($this->url("mem/total"));
47+
$result = json_decode($Response->getBody());
48+
if (isset($result->total)) {
49+
$newstat->value = $this->convertBytesToGigabytes($result->total);
50+
} else {
51+
$newstat->value = null; // or some default value
52+
}
53+
}
54+
55+
// Fetch MemAvail
56+
if ($stat === "MemAvail") {
57+
$Response = parent::execute($this->url("mem/available"));
58+
$result = json_decode($Response->getBody());
59+
if (isset($result->available)) {
60+
$newstat->value = $this->convertBytesToGigabytes($result->available);
61+
} else {
62+
$newstat->value = null; // or some default value
63+
}
64+
}
65+
66+
// Fetch MemAvail
67+
if ($stat === "MemUsage") {
68+
$Response = parent::execute($this->url("mem/used"));
69+
$result = json_decode($Response->getBody());
70+
if (isset($result->used)) {
71+
$newstat->value = $this->convertBytesToGigabytes($result->used);
72+
} else {
73+
$newstat->value = null; // or some default value
74+
}
75+
}
76+
77+
$details["visiblestats"][] = $newstat;
78+
}
79+
}
80+
}
81+
82+
return parent::getLiveStats($status, $details);
83+
}
84+
85+
public function url($endpoint)
86+
{
87+
$api_url = parent::normaliseurl($this->config->url) . "api/4/" . $endpoint;
88+
return $api_url;
89+
}
90+
91+
public static function getAvailableStats()
92+
{
93+
return [
94+
"CpuTotal" => "CpuTotal",
95+
"MemTotal" => "MemTotal",
96+
"MemAvail" => "MemAvail",
97+
"MemUsage" => "MemUsage",
98+
];
99+
}
100+
101+
private function convertBytesToGigabytes($bytes)
102+
{
103+
$gigabytes = $bytes / (1024 ** 3); // Converts bytes to gigabytes
104+
return round($gigabytes, 2) . ' GB'; // Rounds to 4 significant digits
105+
}
7106
}

Glances/app.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"website": "https://nicolargo.github.io/glances",
55
"license": "GNU Lesser General Public License v3.0 only",
66
"description": "Glances is a cross-platform monitoring tool which aims to present a large amount of monitoring information through a curses or Web based interface.",
7-
"enhanced": false,
7+
"enhanced": true,
88
"tile_background": "dark",
99
"icon": "glances.png"
1010
}

Glances/config.blade.php

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<h2>{{ __('app.apps.config') }} ({{ __('app.optional') }}) @include('items.enable')</h2>
2+
<div class="items">
3+
<div class="input">
4+
<label>{{ strtoupper(__('app.url')) }}</label>
5+
{!! Form::text('config[override_url]', isset($item) ? $item->getconfig()->override_url : null, ['placeholder' => __('app.apps.override'), 'id' => 'override_url', 'class' => 'form-control']) !!}
6+
</div>
7+
<div class="input">
8+
<label>Stats to show</label>
9+
{!! Form::select('config[availablestats][]', App\SupportedApps\Glances\Glances::getAvailableStats(), isset($item) ? $item->getConfig()->availablestats ?? null : null, ['multiple' => 'multiple']) !!}
10+
</div>
11+
<div class="input">
12+
<button style="margin-top: 32px;" class="btn test" id="test_config">Test</button>
13+
</div>
14+
</div>

Glances/livestats.blade.php

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<ul class="livestats">
2+
@foreach ($visiblestats as $stat)
3+
<li>
4+
<span class="title">{!! $stat->title !!}</span>
5+
<strong>{!! $stat->value !!}</strong>
6+
</li>
7+
@endforeach
8+
</ul>

0 commit comments

Comments
 (0)