Skip to content

Commit

Permalink
First version
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel authored and daniel committed Apr 26, 2017
1 parent f185815 commit eb61ba1
Show file tree
Hide file tree
Showing 13 changed files with 1,611 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/nbproject/private/
186 changes: 186 additions & 0 deletions ServerMonitor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
<?php

class ServerMonitor {

static function getCpu() {
$obj = new stdClass();
$cmd = "cat /proc/cpuinfo";
exec($cmd . " 2>&1", $output, $return_val);
if ($return_val !== 0) {
$obj->error = "Get CPU ERROR** " . print_r($output, true);
$obj->command = $cmd;
} else {
$obj->title = "";
$obj->success = 1;
$obj->output = $output;
$obj->command = $cmd;
$obj->percent = 0;
$obj->percent = self::getServerLoad();
// find model name
foreach ($output as $value) {
if (preg_match("/model name.+:(.*)/i", $value, $match)) {
$obj->title = $match[1];
break;
}
}
}
return $obj;
}

static function getMemory() {
$obj = new stdClass();
$cmd = "cat /proc/meminfo";
exec($cmd . " 2>&1", $output, $return_val);
if ($return_val !== 0) {
$obj->error = "Get Memmory ERROR** " . print_r($output, true);
$obj->command = $cmd;
} else {
$obj->title = "";
$obj->success = 1;
$obj->output = $output;
$obj->command = $cmd;
$obj->memTotal = 0;
$obj->memFree = 0;
$obj->memAvailable = 0;
foreach ($output as $value) {
if (preg_match("/MemTotal: *([0-9]+) *([a-z]+) */i", $value, $match)) {
$obj->memTotalBytes = $match[1] * 1024;
$obj->memTotal = self::humanFileSize($obj->memTotalBytes);
continue;
}
if (preg_match("/MemFree: *([0-9]+) *([a-z]+) */i", $value, $match)) {
$obj->memFreeBytes = $match[1] * 1024;
$obj->memFree = self::humanFileSize($obj->memFreeBytes);
continue;
}
if (preg_match("/MemAvailable: *([0-9]+) *([a-z]+) */i", $value, $match)) {
$obj->memAvailableBytes = $match[1] * 1024;
$obj->memAvailable = self::humanFileSize($obj->memAvailableBytes);
continue;
}
if (!empty($obj->memTotal) && !empty($obj->memFree) && !empty($obj->memAvailable)) {
$onePc = $obj->memTotalBytes / 100;
$obj->usedBytes = $obj->memTotalBytes - ($obj->memFreeBytes + $obj->memAvailableBytes);
$obj->used = self::humanFileSize($obj->usedBytes);
$obj->percent = $obj->usedBytes / $onePc;
$obj->title = "Total: {$obj->memTotal} | Free: {$obj->memFree} | Available: {$obj->memAvailable} | Used: {$obj->used}";
}
}
}
return $obj;
}

static function getDisk() {
$obj = new stdClass();
$cmd = "df -h";
exec($cmd . " 2>&1", $output, $return_val);
if ($return_val !== 0) {
$obj->error = "Get Disk ERROR** " . print_r($output, true);
$obj->command = $cmd;
} else {
if (preg_match("/([0-9]+)%/i", end($output), $match)) {
$obj->percent = $match[1];
}
$obj->title = "{$obj->percent}%";
$obj->success = 1;
$obj->output = $output;
$obj->command = $cmd;
}
return $obj;
}

static function humanFileSize($size, $unit = "") {
if ((!$unit && $size >= 1 << 30) || $unit == "GB")
return number_format($size / (1 << 30), 2) . "GB";
if ((!$unit && $size >= 1 << 20) || $unit == "MB")
return number_format($size / (1 << 20), 2) . "MB";
if ((!$unit && $size >= 1 << 10) || $unit == "KB")
return number_format($size / (1 << 10), 2) . "KB";
return number_format($size) . " bytes";
}

static private function _getServerLoadLinuxData() {
if (is_readable("/proc/stat")) {
$stats = @file_get_contents("/proc/stat");

if ($stats !== false) {
// Remove double spaces to make it easier to extract values with explode()
$stats = preg_replace("/[[:blank:]]+/", " ", $stats);

// Separate lines
$stats = str_replace(array("\r\n", "\n\r", "\r"), "\n", $stats);
$stats = explode("\n", $stats);

// Separate values and find line for main CPU load
foreach ($stats as $statLine) {
$statLineData = explode(" ", trim($statLine));

// Found!
if
(
(count($statLineData) >= 5) &&
($statLineData[0] == "cpu")
) {
return array(
$statLineData[1],
$statLineData[2],
$statLineData[3],
$statLineData[4],
);
}
}
}
}

return null;
}

// Returns server load in percent (just number, without percent sign)
static function getServerLoad() {
$load = null;

if (stristr(PHP_OS, "win")) {
$cmd = "wmic cpu get loadpercentage /all";
@exec($cmd, $output);

if ($output) {
foreach ($output as $line) {
if ($line && preg_match("/^[0-9]+\$/", $line)) {
$load = $line;
break;
}
}
}
} else {
if (is_readable("/proc/stat")) {
// Collect 2 samples - each with 1 second period
// See: https://de.wikipedia.org/wiki/Load#Der_Load_Average_auf_Unix-Systemen
$statData1 = self::_getServerLoadLinuxData();
sleep(1);
$statData2 = self::_getServerLoadLinuxData();

if
(
(!is_null($statData1)) &&
(!is_null($statData2))
) {
// Get difference
$statData2[0] -= $statData1[0];
$statData2[1] -= $statData1[1];
$statData2[2] -= $statData1[2];
$statData2[3] -= $statData1[3];

// Sum up the 4 values for User, Nice, System and Idle and calculate
// the percentage of idle time (which is part of the 4 values!)
$cpuTime = $statData2[0] + $statData2[1] + $statData2[2] + $statData2[3];

// Invert percentage to get CPU time, not idle time
$load = 100 - ($statData2[3] * 100 / $cpuTime);
}
}
}

return $load;
}

}
4 changes: 4 additions & 0 deletions cpu.json.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php
require_once 'ServerMonitor.php';
header('Content-Type: application/json');
echo json_encode(ServerMonitor::getCpu());
4 changes: 4 additions & 0 deletions disk.json.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php
require_once 'ServerMonitor.php';
header('Content-Type: application/json');
echo json_encode(ServerMonitor::getDisk());
50 changes: 50 additions & 0 deletions gauge/css/asPieProgress.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* jQuery asPieProgress v0.4.6
* https://github.com/amazingSurge/jquery-asPieProgress
*
* Copyright (c) amazingSurge
* Released under the LGPL-3.0 license
*/
.pie_progress {
position: relative;
text-align: center;

-webkit-transform-style: preserve-3d;

transform-style: preserve-3d;
}
.pie_progress__svg {
position: relative;
display: inline-block;
width: 100%;
padding-bottom: 100%;
overflow: hidden;
vertical-align: middle;
}
.pie_progress__svg svg {
position: absolute;
top: 0;
left: 0;
display: inline-block;
width: 100%;
height: 100%;
margin: 0 auto;
}
.pie_progress__content, .pie_progress__number, .pie_progress__label, .pie_progress__icon {
position: absolute;
top: 50%;
left: 0;
width: 100%;
-webkit-transform: translateY(-50%);
-ms-transform: translateY(-50%);
transform: translateY(-50%);
}

.pie_progress__number {
font-size: 42px;
}

.pie_progress__label {
margin-top: 32px;
font-size: 12px;
}
8 changes: 8 additions & 0 deletions gauge/css/asPieProgress.min.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* jQuery asPieProgress v0.4.6
* https://github.com/amazingSurge/jquery-asPieProgress
*
* Copyright (c) amazingSurge
* Released under the LGPL-3.0 license
*/
.pie_progress{position:relative;text-align:center;-webkit-transform-style:preserve-3d;transform-style:preserve-3d}.pie_progress__svg{position:relative;display:inline-block;width:100%;padding-bottom:100%;overflow:hidden;vertical-align:middle}.pie_progress__svg svg{position:absolute;top:0;left:0;display:inline-block;width:100%;height:100%;margin:0 auto}.pie_progress__content,.pie_progress__icon,.pie_progress__label,.pie_progress__number{position:absolute;top:50%;left:0;width:100%;-webkit-transform:translateY(-50%);-ms-transform:translateY(-50%);transform:translateY(-50%)}.pie_progress__number{font-size:42px}.pie_progress__label{margin-top:32px;font-size:12px}
Loading

0 comments on commit eb61ba1

Please sign in to comment.