Skip to content

Commit

Permalink
Fix some typo and Error
Browse files Browse the repository at this point in the history
Just Rnning/Failed is correct when active
  • Loading branch information
GOUKI9999 committed Aug 7, 2024
1 parent dabcd8a commit ea813fd
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 37 deletions.
59 changes: 31 additions & 28 deletions Monit/Monit.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,18 @@

class Monit extends \App\SupportedApps
{
public static function getAvailableStats()
{
return [
'running_services' => 'Running',
'failed_services' => 'Failed',
'load' => 'Load',
'cpu' => 'CPU',
'memory' => 'Memory',
'swap' => 'Swap'
];
}

public function test()
{
$response = $this->executeCurl($this->url('/_status?format=xml'));
Expand All @@ -17,7 +29,14 @@ public function test()
public function livestats()
{
$status = 'inactive';
$data = [];
$data = [
'running_services' => 'N/A',
'failed_services' => 'N/A',
'load' => 'N/A',
'cpu' => 'N/A',
'memory' => 'N/A',
'swap' => 'N/A'
];

$response = $this->executeCurl($this->url('/_status?format=xml'));

Expand All @@ -26,13 +45,11 @@ public function livestats()
$json = json_encode($xml);
$data = json_decode($json, true);

// 计算运行的服务数量和失败的服务数量
$running_services = 0;
$failed_services = 0;

if (isset($data['service'])) {
if (isset($data['service'][0])) {
// 如果是多个服务的情况
foreach ($data['service'] as $service) {
if (isset($service['status']) && $service['status'] == 0) {
$running_services++;
Expand All @@ -41,7 +58,6 @@ public function livestats()
}
}
} else {
// 如果是单个服务的情况
if (isset($data['service']['status']) && $data['service']['status'] == 0) {
$running_services++;
} else {
Expand All @@ -51,40 +67,27 @@ public function livestats()
}

$status = 'active';
$metrics = [];

if (isset($this->config->availablestats) && in_array('running_services', $this->config->availablestats)) {
$metrics['running_services'] = $running_services;
}
$data['running_services'] = $running_services;
$data['failed_services'] = $failed_services;
}

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

$data = $metrics;
} else {
$data = [
'error' => 'Failed to connect to Monit. HTTP Status: ' . $response['httpcode']
];
}

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

public static function getAvailableStats()
{
return [
'running_services' => 'Running Services',
'failed_services' => 'Failed Services'
];
return parent::getLiveStats($status, ['visiblestats' => $visiblestats]);
}

private function url($endpoint)
{
$config = $this->config;

$url = rtrim($config->url, '/');

return $url . $endpoint;
}

Expand Down
38 changes: 37 additions & 1 deletion Monit/config.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,42 @@
{!! 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; display: block;" class="btn test" id="test_config">Test</button>
<button style="margin-top: 32px;" class="btn test" id="test_config">Test</button>
</div>
</div>

<script>
document.getElementById('test_config').addEventListener('click', function() {
var username = document.querySelector('[data-config="username"]').value;
var password = document.querySelector('[data-config="password"]').value;
var url = document.getElementById('override_url').value;
console.log('Username:', username);
console.log('Password:', password);
console.log('URL:', url);
if (!username || !password || !url) {
alert('URL, Username, and Password are required');
return;
}
fetch(url, {
method: 'GET',
headers: {
'Authorization': 'Basic ' + btoa(username + ':' + password),
'Content-Type': 'application/json',
},
})
.then(response => {
if (response.status === 200) {
alert('Successfully communicated with the API');
} else {
alert('Failed: Invalid credentials');
}
})
.catch(error => {
alert('Error testing Monit configuration');
console.error('Error:', error);
});
});
</script>
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">Running</span>
<strong>{!! $running_services !!}</strong>
</li>
<li>
<span class="title">Failed</span>
<strong>{!! $failed_services !!}</strong>
</li>
@foreach ($visiblestats as $stat)
<li>
<span class="title">{{ $stat['title'] }}</span>
<strong>{{ $stat['value'] }}</strong>
</li>
@endforeach
</ul>

0 comments on commit ea813fd

Please sign in to comment.