Skip to content

Commit

Permalink
Fix for monit (#768)
Browse files Browse the repository at this point in the history
* Fix Monit App

replace queue and speed by number of running /failed service

* Fix-for-Monit

* php-cs-fix
  • Loading branch information
GOUKI9999 committed Aug 17, 2024
1 parent d163378 commit 800b12a
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 27 deletions.
134 changes: 116 additions & 18 deletions Monit/Monit.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,134 @@

namespace App\SupportedApps\Monit;

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

//protected $login_first = true; // Uncomment if api requests need to be authed first
//protected $method = 'POST'; // Uncomment if requests to the API should be set by POST

public function __construct()
public static function getAvailableStats()
{
//$this->jar = new \GuzzleHttp\Cookie\CookieJar; // Uncomment if cookies need to be set
return [
'running_services' => 'Running',
'failed_services' => 'Failed',
'load' => 'Load',
'cpu' => 'CPU',
'memory' => 'Memory',
'swap' => 'Swap'
];
}

public function test()
{
$test = parent::appTest($this->url("status"));
echo $test->status;
$response = $this->executeCurl($this->url('/_status?format=xml'));
if ($response['httpcode'] == 200) {
echo 'Successfully communicated with the API';
} else {
echo 'Failed to connect to Monit. HTTP Status: ' . $response['httpcode'];
}
}

public function livestats()
{
$status = "inactive";
$res = parent::execute($this->url("status"));
$details = json_decode($res->getBody());

$status = 'inactive';
$data = [];
return parent::getLiveStats($status, $data);
$response = $this->executeCurl($this->url('/_status?format=xml'));

if ($response['httpcode'] == 200) {
$xml = simplexml_load_string($response['response']);
$json = json_encode($xml);
$data = json_decode($json, true);

$running_services = 0;
$failed_services = 0;
$load = 'N/A';
$cpu = 'N/A';
$memory = 'N/A';
$swap = 'N/A';

if (isset($data['service'])) {
if (isset($data['service'][0])) {
foreach ($data['service'] as $service) {
if (isset($service['status']) && $service['status'] == 0) {
$running_services++;
} else {
$failed_services++;
}
}
} else {
if (isset($data['service']['status']) && $data['service']['status'] == 0) {
$running_services++;
} else {
$failed_services++;
}
}
}

foreach ($data['service'] as $service) {
if (isset($service['system'])) {
$load = $service['system']['load']['avg05'] ?? 'N/A';
$cpu = isset($service['system']['cpu']['user'])
? $service['system']['cpu']['user'] . '%'
: 'N/A';
$memory = isset($service['system']['memory']['percent'])
? $service['system']['memory']['percent'] . '%'
: 'N/A';
$swap = isset($service['system']['swap']['percent'])
? $service['system']['swap']['percent'] . '%'
: 'N/A';
break;
}
}

$status = 'active';
$data = [
'running_services' => $running_services,
'failed_services' => $failed_services,
'load' => $load,
'cpu' => $cpu,
'memory' => $memory,
'swap' => $swap
];
} else {
$data = [
'error' => 'Failed to connect to Monit. HTTP Status: ' . $response['httpcode']
];
}

$visiblestats = [];
if (isset($this->config->availablestats)) {
foreach ($this->config->availablestats as $stat) {
$visiblestats[] = [
'title' => self::getAvailableStats()[$stat],
'value' => $data[$stat] ?? 'N/A'
];
}
}

return parent::getLiveStats($status, ['visiblestats' => $visiblestats]);
}

private function url($endpoint)
{
$config = $this->config;
$url = rtrim($config->url, '/');
return $url . $endpoint;
}
public function url($endpoint)

private function executeCurl($url)
{
$api_url = parent::normaliseurl($this->config->url) . $endpoint;
return $api_url;
$username = $this->config->username;
$password = $this->config->password;

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
$response = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

return [
'response' => $response,
'httpcode' => $httpcode
];
}
}
7 changes: 6 additions & 1 deletion Monit/config.blade.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<h2>{{ __('app.apps.config') }} ({{ __('app.optional') }}) @include('items.enable')</h2>

<div class="items">
<div class="input">
<label>{{ strtoupper(__('app.url')) }}</label>
Expand All @@ -12,7 +13,11 @@
<label>{{ __('app.apps.password') }}</label>
{!! Form::input('password', 'config[password]', '', ['placeholder' => __('app.apps.password'), 'data-config' => 'password', 'class' => 'form-control config-item']) !!}
</div>
<div class="input">
<label>Stats to show</label>
{!! 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']) !!}
</div>
<div class="input">
<button style="margin-top: 32px;" class="btn test" id="test_config">Test</button>
</div>
</div>
</div>
14 changes: 6 additions & 8 deletions Monit/livestats.blade.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
<ul class="livestats">
<li>
<span class="title">Queue</span>
<strong>{!! $queue_size !!}</strong>
</li>
<li>
<span class="title">Speed</span>
<strong>{!! $current_speed !!}</strong>
</li>
@foreach ($visiblestats as $stat)
<li>
<span class="title">{{ $stat['title'] }}</span>
<strong>{{ $stat['value'] }}</strong>
</li>
@endforeach
</ul>

0 comments on commit 800b12a

Please sign in to comment.