Skip to content

Commit

Permalink
Merge pull request #35 from solusio/task-lbinde-graphs
Browse files Browse the repository at this point in the history
Graphs
  • Loading branch information
laurisb authored Nov 12, 2020
2 parents 045c7c7 + 9653045 commit a65fe90
Show file tree
Hide file tree
Showing 6 changed files with 539 additions and 3 deletions.
14 changes: 14 additions & 0 deletions lang/english.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,17 @@
$_LANG['solusiovps_exception_page_pending_message'] = 'Your service is being provisioned. If you have any questions, please contact technical support.';
$_LANG['solusiovps_exception_page_cancelled_title'] = 'The service was terminated';
$_LANG['solusiovps_exception_page_cancelled_message'] = 'This service was already terminated. If you have any questions, please contact technical support.';

$_LANG['solusiovps_chart_cpu_title'] = 'CPU usage';
$_LANG['solusiovps_chart_cpu_label_load'] = 'Load average (%)';

$_LANG['solusiovps_chart_network_title'] = 'Network usage';
$_LANG['solusiovps_chart_network_label_read'] = 'Read KiB';
$_LANG['solusiovps_chart_network_label_write'] = 'Write KiB';

$_LANG['solusiovps_chart_disk_title'] = 'Disk usage';
$_LANG['solusiovps_chart_disk_label_read'] = 'Read KiB';
$_LANG['solusiovps_chart_disk_label_write'] = 'Write KiB';

$_LANG['solusiovps_chart_memory_title'] = 'Memory usage';
$_LANG['solusiovps_chart_memory_label_usage'] = 'Usage MiB';
28 changes: 28 additions & 0 deletions lib/SolusAPI/Resources/UsageResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

// Copyright 2020. Plesk International GmbH.

namespace WHMCS\Module\Server\SolusIoVps\SolusAPI\Resources;

class UsageResource extends ApiResource
{
public function cpu(string $uuid): array
{
return $this->processResponse($this->connector->get("usage/cpu/{$uuid}"));
}

public function network(string $uuid): array
{
return $this->processResponse($this->connector->get("usage/network/{$uuid}"));
}

public function disks(string $uuid): array
{
return $this->processResponse($this->connector->get("usage/disks/{$uuid}"));
}

public function memory(string $uuid): array
{
return $this->processResponse($this->connector->get("usage/memory/{$uuid}"));
}
}
119 changes: 118 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"description": "SolusIO WHMCS VPS Provisioning module",
"license": "Apache-2.0",
"dependencies": {
"@novnc/novnc": "^1.2.0"
"@novnc/novnc": "^1.2.0",
"chart.js": "^2.9.4"
}
}
74 changes: 74 additions & 0 deletions pages/usage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

// Copyright 2020. Plesk International GmbH.

use WHMCS\Module\Server\SolusIoVps\Database\Models\Hosting;
use WHMCS\Module\Server\SolusIoVps\Database\Models\Server;
use WHMCS\Module\Server\SolusIoVps\Database\Models\SolusServer;
use WHMCS\Module\Server\SolusIoVps\SolusAPI\Connector;
use WHMCS\Module\Server\SolusIoVps\SolusAPI\Resources\UsageResource;
use WHMCS\Module\Server\SolusIoVps\WhmcsAPI\ClientArea;

define('CLIENTAREA', true);

require dirname(__DIR__, 4) . '/init.php';
require dirname(__DIR__) . '/vendor/autoload.php';

$serviceId = (int) $_GET['serviceId'];
$ca = new ClientArea();
$hosting = Hosting::getByServiceId($serviceId);

if (!$ca->hasAccessToHosting($hosting)) {
die('Access denied');
}

$server = SolusServer::getByServiceId($serviceId);
$serverId = (int) $hosting->server;
$serverParams = Server::getParams($serverId);
$payload = json_decode($server->payload, true);
$uuid = $payload['uuid'];

$usageResource = new UsageResource(Connector::create($serverParams));
$cpuUsage = $usageResource->cpu($uuid);
$networkUsage = $usageResource->network($uuid);
$diskUsage = $usageResource->disks($uuid);
$memoryUssage = $usageResource->memory($uuid);

$usage = [
'cpu' => [],
'network' => [],
'disk' => [],
'memory' => [],
];

foreach ($cpuUsage['data']['items'] as $item) {
$usage['cpu'][] = [
'second' => date('H:i:s', strtotime($item['time'])),
'load_average' => $item['load_average'],
];
}

foreach ($networkUsage['data']['items'] as $item) {
$usage['network'][] = [
'second' => date('H:i:s', strtotime($item['time'])),
'read_kb' => $item['read_kb'],
'write_kb' => $item['write_kb'],
];
}

foreach ($diskUsage['data']['items'] as $item) {
$usage['disk'][] = [
'second' => date('H:i:s', strtotime($item['time'])),
'read_kb' => $item['read_kb'],
'write_kb' => $item['write_kb'],
];
}

foreach ($memoryUssage['data']['items'] as $item) {
$usage['memory'][] = [
'second' => date('H:i:s', strtotime($item['time'])),
'memory' => $item['memory'],
];
}

echo json_encode($usage);
Loading

0 comments on commit a65fe90

Please sign in to comment.