Skip to content

Commit 800b12a

Browse files
authored
Fix for monit (#768)
* Fix Monit App replace queue and speed by number of running /failed service * Fix-for-Monit * php-cs-fix
1 parent d163378 commit 800b12a

File tree

3 files changed

+128
-27
lines changed

3 files changed

+128
-27
lines changed

Monit/Monit.php

+116-18
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,134 @@
22

33
namespace App\SupportedApps\Monit;
44

5-
class Monit extends \App\SupportedApps implements \App\EnhancedApps
5+
class Monit extends \App\SupportedApps
66
{
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()
138
{
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+
];
1517
}
1618

1719
public function test()
1820
{
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+
}
2127
}
2228

2329
public function livestats()
2430
{
25-
$status = "inactive";
26-
$res = parent::execute($this->url("status"));
27-
$details = json_decode($res->getBody());
28-
31+
$status = 'inactive';
2932
$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;
31114
}
32-
public function url($endpoint)
115+
116+
private function executeCurl($url)
33117
{
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+
];
36134
}
37135
}

Monit/config.blade.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<h2>{{ __('app.apps.config') }} ({{ __('app.optional') }}) @include('items.enable')</h2>
2+
23
<div class="items">
34
<div class="input">
45
<label>{{ strtoupper(__('app.url')) }}</label>
@@ -12,7 +13,11 @@
1213
<label>{{ __('app.apps.password') }}</label>
1314
{!! Form::input('password', 'config[password]', '', ['placeholder' => __('app.apps.password'), 'data-config' => 'password', 'class' => 'form-control config-item']) !!}
1415
</div>
16+
<div class="input">
17+
<label>Stats to show</label>
18+
{!! Form::select('config[availablestats][]', App\SupportedApps\Monit\Monit::getAvailableStats(), isset($item) && isset($item->getconfig()->availablestats) ? $item->getconfig()->availablestats : null, ['multiple' => 'multiple', 'class' => 'form-control config-item']) !!}
19+
</div>
1520
<div class="input">
1621
<button style="margin-top: 32px;" class="btn test" id="test_config">Test</button>
1722
</div>
18-
</div>
23+
</div>

Monit/livestats.blade.php

+6-8
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
<ul class="livestats">
2-
<li>
3-
<span class="title">Queue</span>
4-
<strong>{!! $queue_size !!}</strong>
5-
</li>
6-
<li>
7-
<span class="title">Speed</span>
8-
<strong>{!! $current_speed !!}</strong>
9-
</li>
2+
@foreach ($visiblestats as $stat)
3+
<li>
4+
<span class="title">{{ $stat['title'] }}</span>
5+
<strong>{{ $stat['value'] }}</strong>
6+
</li>
7+
@endforeach
108
</ul>

0 commit comments

Comments
 (0)