Skip to content

Commit

Permalink
Enhance-For-Netdata (#767)
Browse files Browse the repository at this point in the history
* Enhance-For-Netdata

* Some Value Error fix

* php-cs-fix
  • Loading branch information
GOUKI9999 authored Aug 17, 2024
1 parent 800b12a commit 6a952ea
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 27 deletions.
127 changes: 108 additions & 19 deletions Netdata/Netdata.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,127 @@

class Netdata extends \App\SupportedApps implements \App\EnhancedApps
{
private const ENDPOINT = "api/v1/info";

public $config;

public function __construct()
public static function getAvailableStats()
{
return [
'cpu' => 'CPU',
'memory_free' => 'Mem Free',
'memory_used' => 'Mem Used',
'load1' => 'Load 1',
'load5' => 'Load 5',
'load15' => 'Load 15',
'disk_in' => 'Disk In',
'disk_out' => 'Disk Out',
'network_in' => 'Net In',
'network_out' => 'Net Out'
];
}

public function test()
{
$test = parent::appTest($this->url(self::ENDPOINT));
echo $test->status;
$response = $this->executeCurl($this->url('/api/v1/allmetrics?format=json'));
if ($response['httpcode'] == 200) {
echo 'Successfully communicated with the API';
} else {
echo 'Failed to connect to Netdata. HTTP Status: ' . $response['httpcode'];
}
}

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

$data = [
"count_warning" => $details->alarms->warning,
"count_critical" => $details->alarms->critical,
];
$status = 'inactive';
$data = [];
$response = $this->executeCurl($this->url('/api/v1/allmetrics?format=json'));

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

$cpu = isset($json['system.cpu']['dimensions']['idle']['value'])
? number_format(100 - $json['system.cpu']['dimensions']['idle']['value'], 1) . '%'
: 'N/A';

$memoryFree = isset($json['system.ram']['dimensions']['free']['value'])
? number_format($json['system.ram']['dimensions']['free']['value'], 1) . 'MB'
: 'N/A';
$memoryUsed = isset($json['system.ram']['dimensions']['used']['value'])
? number_format($json['system.ram']['dimensions']['used']['value'], 1) . 'MB'
: 'N/A';

$load1 = isset($json['system.load']['dimensions']['load1']['value'])
? $json['system.load']['dimensions']['load1']['value']
: 'N/A';
$load5 = isset($json['system.load']['dimensions']['load5']['value'])
? $json['system.load']['dimensions']['load5']['value']
: 'N/A';
$load15 = isset($json['system.load']['dimensions']['load15']['value'])
? $json['system.load']['dimensions']['load15']['value']
: 'N/A';

$diskIn = isset($json['system.io']['dimensions']['in']['value'])
? number_format($json['system.io']['dimensions']['in']['value'], 1) . 'KB/s'
: 'N/A';
$diskOut = isset($json['system.io']['dimensions']['out']['value'])
? number_format($json['system.io']['dimensions']['out']['value'], 1) . 'KB/s'
: 'N/A';

return parent::getLiveStats($status, $data);
$networkIn = isset($json['system.net']['dimensions']['InOctets']['value'])
? number_format($json['system.net']['dimensions']['InOctets']['value'], 1) . 'KB/s'
: 'N/A';
$networkOut = isset($json['system.net']['dimensions']['OutOctets']['value'])
? number_format($json['system.net']['dimensions']['OutOctets']['value'], 1) . 'KB/s'
: 'N/A';

$status = 'active';
$data = [
'cpu' => $cpu,
'memory_free' => $memoryFree,
'memory_used' => $memoryUsed,
'load1' => $load1,
'load5' => $load5,
'load15' => $load15,
'disk_in' => $diskIn,
'disk_out' => $diskOut,
'network_in' => $networkIn,
'network_out' => $networkOut
];
} else {
$data = [
'error' => 'Failed to connect to Netdata. 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]);
}

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

public function executeCurl($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

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

<div class="items">
<div class="input">
<label>{{ strtoupper(__('app.url')) }}</label>
{!! Form::text('config[override_url]', isset($item) ? $item->getconfig()->override_url : null, ['placeholder' => __('app.apps.override'), 'id' => 'override_url', 'class' => 'form-control']) !!}
</div>
<div class="input">
<label>Stats to show</label>
{!! Form::select('config[availablestats][]', App\SupportedApps\Netdata\Netdata::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>
Expand Down
14 changes: 6 additions & 8 deletions Netdata/livestats.blade.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
<ul class="livestats">
<li>
<span class="title">Warning</span>
<strong>{!! $count_warning !!}</strong>
</li>
<li>
<span class="title">Critical</span>
<strong>{!! $count_critical !!}</strong>
</li>
@foreach ($visiblestats as $stat)
<li>
<span class="title">{{ $stat['title'] }}</span>
<strong>{{ $stat['value'] }}</strong>
</li>
@endforeach
</ul>

0 comments on commit 6a952ea

Please sign in to comment.