Skip to content

Commit

Permalink
Enhanced glances (#758)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
PedroBuffon committed Jul 13, 2024
1 parent c170849 commit 666f8ab
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 2 deletions.
101 changes: 100 additions & 1 deletion Glances/Glances.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,105 @@

namespace App\SupportedApps\Glances;

class Glances extends \App\SupportedApps
class Glances extends \App\SupportedApps implements \App\EnhancedApps
{
public $config;

public function __construct()
{
}

public function test()
{
$test = parent::appTest($this->url("status"));
echo $test->status;
}

public function livestats()
{
$status = "inactive";
$details = [];

if (isset($this->config->availablestats) && is_array($this->config->availablestats)) {
$details = ["visiblestats" => []];
foreach ($this->config->availablestats as $stat) {
$newstat = new \stdClass();
$availableStats = self::getAvailableStats();

if (isset($availableStats[$stat])) {
$newstat->title = $availableStats[$stat];

// Fetch CpuTotal
if ($stat === "CpuTotal") {
$Response = parent::execute($this->url("cpu/total"));
$result = json_decode($Response->getBody());
if (isset($result->total)) {
$newstat->value = $result->total;
} else {
$newstat->value = null; // or some default value
}
}

// Fetch MemTotal
if ($stat === "MemTotal") {
$Response = parent::execute($this->url("mem/total"));
$result = json_decode($Response->getBody());
if (isset($result->total)) {
$newstat->value = $this->convertBytesToGigabytes($result->total);
} else {
$newstat->value = null; // or some default value
}
}

// Fetch MemAvail
if ($stat === "MemAvail") {
$Response = parent::execute($this->url("mem/available"));
$result = json_decode($Response->getBody());
if (isset($result->available)) {
$newstat->value = $this->convertBytesToGigabytes($result->available);
} else {
$newstat->value = null; // or some default value
}
}

// Fetch MemAvail
if ($stat === "MemUsage") {
$Response = parent::execute($this->url("mem/used"));
$result = json_decode($Response->getBody());
if (isset($result->used)) {
$newstat->value = $this->convertBytesToGigabytes($result->used);
} else {
$newstat->value = null; // or some default value
}
}

$details["visiblestats"][] = $newstat;
}
}
}

return parent::getLiveStats($status, $details);
}

public function url($endpoint)
{
$api_url = parent::normaliseurl($this->config->url) . "api/4/" . $endpoint;
return $api_url;
}

public static function getAvailableStats()
{
return [
"CpuTotal" => "CpuTotal",
"MemTotal" => "MemTotal",
"MemAvail" => "MemAvail",
"MemUsage" => "MemUsage",
];
}

private function convertBytesToGigabytes($bytes)
{
$gigabytes = $bytes / (1024 ** 3); // Converts bytes to gigabytes
return round($gigabytes, 2) . ' GB'; // Rounds to 4 significant digits
}
}
2 changes: 1 addition & 1 deletion Glances/app.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"website": "https://nicolargo.github.io/glances",
"license": "GNU Lesser General Public License v3.0 only",
"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.",
"enhanced": false,
"enhanced": true,
"tile_background": "dark",
"icon": "glances.png"
}
14 changes: 14 additions & 0 deletions Glances/config.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<h2>{{ __('app.apps.config') }} ({{ __('app.optional') }}) @include('items.enable')</h2>
<div class="items">
<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>Stats to show</label>
{!! Form::select('config[availablestats][]', App\SupportedApps\Glances\Glances::getAvailableStats(), isset($item) ? $item->getConfig()->availablestats ?? null : null, ['multiple' => 'multiple']) !!}
</div>
<div class="input">
<button style="margin-top: 32px;" class="btn test" id="test_config">Test</button>
</div>
</div>
8 changes: 8 additions & 0 deletions Glances/livestats.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<ul class="livestats">
@foreach ($visiblestats as $stat)
<li>
<span class="title">{!! $stat->title !!}</span>
<strong>{!! $stat->value !!}</strong>
</li>
@endforeach
</ul>

0 comments on commit 666f8ab

Please sign in to comment.