|
2 | 2 |
|
3 | 3 | namespace App\SupportedApps\Glances;
|
4 | 4 |
|
5 |
| -class Glances extends \App\SupportedApps |
| 5 | +class Glances extends \App\SupportedApps implements \App\EnhancedApps |
6 | 6 | {
|
| 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 | + } |
7 | 106 | }
|
0 commit comments