|
2 | 2 |
|
3 | 3 | namespace App\SupportedApps\Monit;
|
4 | 4 |
|
5 |
| -class Monit extends \App\SupportedApps implements \App\EnhancedApps |
| 5 | +class Monit extends \App\SupportedApps |
6 | 6 | {
|
7 |
| - public $config; |
8 |
| - |
9 |
| - //protected $login_first = true; // Uncomment if api requests need to be authed first |
10 |
| - //protected $method = 'POST'; // Uncomment if requests to the API should be set by POST |
11 |
| - |
12 |
| - public function __construct() |
| 7 | + public static function getAvailableStats() |
13 | 8 | {
|
14 |
| - //$this->jar = new \GuzzleHttp\Cookie\CookieJar; // Uncomment if cookies need to be set |
| 9 | + return [ |
| 10 | + 'running_services' => 'Running', |
| 11 | + 'failed_services' => 'Failed', |
| 12 | + 'load' => 'Load', |
| 13 | + 'cpu' => 'CPU', |
| 14 | + 'memory' => 'Memory', |
| 15 | + 'swap' => 'Swap' |
| 16 | + ]; |
15 | 17 | }
|
16 | 18 |
|
17 | 19 | public function test()
|
18 | 20 | {
|
19 |
| - $test = parent::appTest($this->url("status")); |
20 |
| - echo $test->status; |
| 21 | + $response = $this->executeCurl($this->url('/_status?format=xml')); |
| 22 | + if ($response['httpcode'] == 200) { |
| 23 | + echo 'Successfully communicated with the API'; |
| 24 | + } else { |
| 25 | + echo 'Failed to connect to Monit. HTTP Status: ' . $response['httpcode']; |
| 26 | + } |
21 | 27 | }
|
22 | 28 |
|
23 | 29 | public function livestats()
|
24 | 30 | {
|
25 |
| - $status = "inactive"; |
26 |
| - $res = parent::execute($this->url("status")); |
27 |
| - $details = json_decode($res->getBody()); |
28 |
| - |
| 31 | + $status = 'inactive'; |
29 | 32 | $data = [];
|
30 |
| - return parent::getLiveStats($status, $data); |
| 33 | + $response = $this->executeCurl($this->url('/_status?format=xml')); |
| 34 | + |
| 35 | + if ($response['httpcode'] == 200) { |
| 36 | + $xml = simplexml_load_string($response['response']); |
| 37 | + $json = json_encode($xml); |
| 38 | + $data = json_decode($json, true); |
| 39 | + |
| 40 | + $running_services = 0; |
| 41 | + $failed_services = 0; |
| 42 | + $load = 'N/A'; |
| 43 | + $cpu = 'N/A'; |
| 44 | + $memory = 'N/A'; |
| 45 | + $swap = 'N/A'; |
| 46 | + |
| 47 | + if (isset($data['service'])) { |
| 48 | + if (isset($data['service'][0])) { |
| 49 | + foreach ($data['service'] as $service) { |
| 50 | + if (isset($service['status']) && $service['status'] == 0) { |
| 51 | + $running_services++; |
| 52 | + } else { |
| 53 | + $failed_services++; |
| 54 | + } |
| 55 | + } |
| 56 | + } else { |
| 57 | + if (isset($data['service']['status']) && $data['service']['status'] == 0) { |
| 58 | + $running_services++; |
| 59 | + } else { |
| 60 | + $failed_services++; |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + foreach ($data['service'] as $service) { |
| 66 | + if (isset($service['system'])) { |
| 67 | + $load = $service['system']['load']['avg05'] ?? 'N/A'; |
| 68 | + $cpu = isset($service['system']['cpu']['user']) |
| 69 | + ? $service['system']['cpu']['user'] . '%' |
| 70 | + : 'N/A'; |
| 71 | + $memory = isset($service['system']['memory']['percent']) |
| 72 | + ? $service['system']['memory']['percent'] . '%' |
| 73 | + : 'N/A'; |
| 74 | + $swap = isset($service['system']['swap']['percent']) |
| 75 | + ? $service['system']['swap']['percent'] . '%' |
| 76 | + : 'N/A'; |
| 77 | + break; |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + $status = 'active'; |
| 82 | + $data = [ |
| 83 | + 'running_services' => $running_services, |
| 84 | + 'failed_services' => $failed_services, |
| 85 | + 'load' => $load, |
| 86 | + 'cpu' => $cpu, |
| 87 | + 'memory' => $memory, |
| 88 | + 'swap' => $swap |
| 89 | + ]; |
| 90 | + } else { |
| 91 | + $data = [ |
| 92 | + 'error' => 'Failed to connect to Monit. HTTP Status: ' . $response['httpcode'] |
| 93 | + ]; |
| 94 | + } |
| 95 | + |
| 96 | + $visiblestats = []; |
| 97 | + if (isset($this->config->availablestats)) { |
| 98 | + foreach ($this->config->availablestats as $stat) { |
| 99 | + $visiblestats[] = [ |
| 100 | + 'title' => self::getAvailableStats()[$stat], |
| 101 | + 'value' => $data[$stat] ?? 'N/A' |
| 102 | + ]; |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + return parent::getLiveStats($status, ['visiblestats' => $visiblestats]); |
| 107 | + } |
| 108 | + |
| 109 | + private function url($endpoint) |
| 110 | + { |
| 111 | + $config = $this->config; |
| 112 | + $url = rtrim($config->url, '/'); |
| 113 | + return $url . $endpoint; |
31 | 114 | }
|
32 |
| - public function url($endpoint) |
| 115 | + |
| 116 | + private function executeCurl($url) |
33 | 117 | {
|
34 |
| - $api_url = parent::normaliseurl($this->config->url) . $endpoint; |
35 |
| - return $api_url; |
| 118 | + $username = $this->config->username; |
| 119 | + $password = $this->config->password; |
| 120 | + |
| 121 | + $ch = curl_init(); |
| 122 | + curl_setopt($ch, CURLOPT_URL, $url); |
| 123 | + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
| 124 | + curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); |
| 125 | + curl_setopt($ch, CURLOPT_USERPWD, "$username:$password"); |
| 126 | + $response = curl_exec($ch); |
| 127 | + $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
| 128 | + curl_close($ch); |
| 129 | + |
| 130 | + return [ |
| 131 | + 'response' => $response, |
| 132 | + 'httpcode' => $httpcode |
| 133 | + ]; |
36 | 134 | }
|
37 | 135 | }
|
0 commit comments