+ First commit (version 2.0)

This commit is contained in:
ShevAbam
2014-06-18 10:11:11 +02:00
commit 20705a550d
33 changed files with 3672 additions and 0 deletions
+94
View File
@@ -0,0 +1,94 @@
<?php
class Config
{
public $file = null;
public $config = null;
public function __construct()
{
$this->file = $_SERVER['DOCUMENT_ROOT'].'/esm.config.json';
if (file_exists($this->file))
$this->_readFile();
}
private function _readFile()
{
$content = file_get_contents($this->file);
$this->config = json_decode($content, true);
}
/**
* Returns a specific config variable
* Ex : get('ping:hosts')
*/
public function get($var)
{
$tab = $this->config;
$explode = explode(':', $var);
foreach ($explode as $vartmp)
{
if (isset($tab[$vartmp]))
{
$tab = $tab[$vartmp];
}
}
return $tab == $this->config ? null : $tab;
}
/**
* Returns all config variables
*/
public function getAll()
{
return $this->config;
}
/**
* Checks if there is an eSM`Web update available
*/
public function checkUpdate()
{
$response = null;
$this_version = $this->get('esm:version');
$update_url = $this->get('esm:website').'/esm-web/update/'.$this_version;
if (!function_exists('curl_version'))
{
$tmp = @file_get_contents($update_url);
$response = json_decode($tmp, true);
}
else
{
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_CONNECTTIMEOUT => 10,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_TIMEOUT => 60,
CURLOPT_USERAGENT => 'eZ Server Monitor `Web',
CURLOPT_URL => $update_url,
));
$response = json_decode(curl_exec($curl), true);
curl_close($curl);
}
if (!is_null($response) && !empty($response))
{
if (is_null($response['error']))
{
return $response['datas'];
}
}
}
}
+57
View File
@@ -0,0 +1,57 @@
<?php
class Misc
{
/**
* Returns human size
*/
public static function getSize($filesize, $precision = 2)
{
$units = array('', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y');
foreach ($units as $idUnit => $unit)
{
if ($filesize > 1024)
$filesize /= 1024;
else
break;
}
return round($filesize, $precision).' '.$units[$idUnit].'o';
}
/**
* Returns hostname
*/
public static function getHostname()
{
return php_uname('n');
}
/**
* Returns server IP
*/
public static function getLanIp()
{
return $_SERVER['SERVER_ADDR'];
}
/**
* Allows to pluralize a word based on a number
* Ex : echo 'mot'.Misc::pluralize(5); ==> prints mots
* Ex : echo 'cheva'.Misc::pluralize(5, 'ux', 'l'); ==> prints chevaux
* Ex : echo 'cheva'.Misc::pluralize(1, 'ux', 'l'); ==> prints cheval
*
* @param int $nb
* @param string $plural
* @param string $singular
*
* @return string
*/
public static function pluralize($nb, $plural = 's', $singular = '')
{
return $nb > 1 ? $plural : $singular;
}
}
+65
View File
@@ -0,0 +1,65 @@
<?php
// Number of cores
if (!($num_cores = shell_exec('/bin/grep -c ^processor /proc/cpuinfo')))
{
$num_cores = 'N.A';
}
// CPU info
if (!($cpuinfo = shell_exec('cat /proc/cpuinfo')))
{
$model = 'N.A';
$frequency = 'N.A';
$cache = 'N.A';
$bogomips = 'N.A';
}
else
{
$processors = preg_split('/\s?\n\s?\n/', trim($cpuinfo));
foreach ($processors as $processor)
{
$details = preg_split('/\n/', $processor, -1, PREG_SPLIT_NO_EMPTY);
foreach ($details as $detail)
{
list($key, $value) = preg_split('/\s*:\s*/', trim($detail));
switch ($key)
{
case 'model name':
case 'cpu model':
case 'cpu':
$model = $value;
break;
case 'cpu MHz':
case 'clock':
$frequency = $value.' MHz';
break;
case 'cache size':
case 'l2 cache':
$cache = $value;
break;
case 'bogomips':
$bogomips = $value;
break;
}
}
}
}
$datas = array(
'model' => $model,
'num_cores' => $num_cores,
'frequency' => $frequency,
'cache' => $cache,
'bogomips' => $bogomips,
);
echo json_encode($datas);
+49
View File
@@ -0,0 +1,49 @@
<?php
require 'Utils/Misc.class.php';
$datas = array();
if (!(exec('/bin/df | awk \'{print $2","$3","$4","$5","$6}\'', $df)))
{
$datas[] = array(
'total' => 'N.A',
'used' => 'N.A',
'free' => 'N.A',
'percent_used' => 0,
'mount' => 'N.A',
);
}
else
{
$first_line = false;
$mounted_points = array();
foreach ($df as $mounted)
{
if ($first_line === false)
{
$first_line = true;
continue;
}
list($total, $used, $free, $percent, $mount) = explode(',', $mounted);
if (!in_array($mount, $mounted_points))
{
$mounted_points[] = trim($mount);
$datas[] = array(
'total' => Misc::getSize($total * 1024),
'used' => Misc::getSize($used * 1024),
'free' => Misc::getSize($free * 1024),
'percent_used' => trim($percent, '%'),
'mount' => $mount,
);
}
}
}
echo json_encode($datas);
+32
View File
@@ -0,0 +1,32 @@
<?php
require 'Utils/Config.class.php';
$Config = new Config();
$datas = array();
if (!(exec('/usr/bin/lastlog --time 365 | /usr/bin/awk \'{print $1","$3","$4" "$5" "$6" "$7" "$8}\'', $users)))
{
$datas[] = array(
'user' => 'N.A',
'host' => 'N.A',
'date' => 'N.A',
);
}
else
{
$max = $Config->get('last_login:max');
for ($i = 1; $i < count($users) && $i <= $max; $i++)
{
list($user, $host, $date) = explode(',', $users[$i]);
$datas[] = array(
'user' => $user,
'host' => $host,
'date' => $date,
);
}
}
echo json_encode($datas);
+25
View File
@@ -0,0 +1,25 @@
<?php
if (!($load_tmp = shell_exec('/bin/cat /proc/loadavg | /usr/bin/awk \'{print $1","$2","$3}\'')))
{
$load = array(0, 0, 0);
}
else
{
$load_exp = explode(',', $load_tmp);
$load = array_map(
function ($value) {
$v = (int)($value * 100);
if ($v > 100)
$v = 100;
return $v;
},
$load_exp
);
}
$datas = $load;
echo json_encode($datas);
+30
View File
@@ -0,0 +1,30 @@
<?php
require 'Utils/Misc.class.php';
// Free
if (!($free = shell_exec('grep MemFree /proc/meminfo | awk \'{print $2}\'')))
{
$free = 0;
}
// Total
if (!($total = shell_exec('grep MemTotal /proc/meminfo | awk \'{print $2}\'')))
{
$total = 0;
}
// Used
$used = $total - $free;
// Percent used
$percent_used = 100 - (round($free / $total * 100));
$datas = array(
'used' => Misc::getSize($used * 1024),
'free' => Misc::getSize($free * 1024),
'total' => Misc::getSize($total * 1024),
'percent_used' => $percent_used,
);
echo json_encode($datas);
+32
View File
@@ -0,0 +1,32 @@
<?php
require 'Utils/Misc.class.php';
$datas = array();
if (!(exec('/sbin/ifconfig | awk -F " " \'{print $1}\' | sed -e \'/^$/d\'', $getInterfaces)))
{
$datas[] = array('interface' => 'N.A', 'ip' => 'N.A');
}
else
{
exec('/sbin/ifconfig | awk \'/inet / {print $2}\' | cut -d \':\' -f2', $getIps);
foreach ($getInterfaces as $key => $interface)
{
// Get transmit and receive datas by interface
exec('cat /sys/class/net/'.$interface.'/statistics/tx_bytes', $getBandwidth_tx);
exec('cat /sys/class/net/'.$interface.'/statistics/rx_bytes', $getBandwidth_rx);
$datas[] = array(
'interface' => $interface,
'ip' => $getIps[$key],
'transmit' => Misc::getSize($getBandwidth_tx[0]),
'receive' => Misc::getSize($getBandwidth_rx[0]),
);
unset($getBandwidth_tx, $getBandwidth_rx);
}
}
echo json_encode($datas);
+25
View File
@@ -0,0 +1,25 @@
<?php
require 'Utils/Config.class.php';
$Config = new Config();
$datas = array();
if (count($Config->get('ping:hosts')) > 0)
$hosts = $Config->get('ping:hosts');
else
$hosts = array('google.com', 'wikipedia.org');
foreach ($hosts as $host)
{
exec('/bin/ping -qc 1 '.$host.' | awk -F/ \'/^rtt/ { print $5 }\'', $result);
$datas[] = array(
'host' => $host,
'ping' => $result[0],
);
unset($result);
}
echo json_encode($datas);
+37
View File
@@ -0,0 +1,37 @@
<?php
require 'Utils/Config.class.php';
$Config = new Config();
$datas = array();
if (count($Config->get('services')) > 0)
{
foreach ($Config->get('services') as $service)
{
$ip = 'localhost';
$sock = @fsockopen($ip, $service['port'], $num, $error, 5);
if ($sock)
{
$datas[] = array(
'port' => $service['port'],
'name' => $service['name'],
'status' => 1,
);
fclose($sock);
}
else
{
$datas[] = array(
'port' => $service['port'],
'name' => $service['name'],
'status' => 0,
);
}
}
}
echo json_encode($datas);
+31
View File
@@ -0,0 +1,31 @@
<?php
require 'Utils/Misc.class.php';
// Free
if (!($free = shell_exec('grep SwapFree /proc/meminfo | awk \'{print $2}\'')))
{
$free = 0;
}
// Total
if (!($total = shell_exec('grep SwapTotal /proc/meminfo | awk \'{print $2}\'')))
{
$total = 0;
}
// Used
$used = $total - $free;
// Percent used
$percent_used = 100 - (round($free / $total * 100));
$datas = array(
'used' => Misc::getSize($used * 1024),
'free' => Misc::getSize($free * 1024),
'total' => Misc::getSize($total * 1024),
'percent_used' => $percent_used,
);
echo json_encode($datas);
+83
View File
@@ -0,0 +1,83 @@
<?php
require 'Utils/Misc.class.php';
// Hostname
$hostname = php_uname('n');
// OS
if (!($os = shell_exec('/usr/bin/lsb_release -ds')))
{
$os = 'N.A';
}
// Kernel
if (!($kernel = shell_exec('/bin/uname -r')))
{
$kernel = 'N.A';
}
// Uptime
if (!($totalSeconds = shell_exec('/usr/bin/cut -d. -f1 /proc/uptime')))
{
$uptime = 'N.A';
}
else
{
$totalMin = $totalSeconds / 60;
$totalHours = $totalMin / 60;
$days = floor($totalHours / 24);
$hours = floor($totalHours - ($days * 24));
$min = floor($totalMin - ($days * 60 * 24) - ($hours * 60));
$uptime = '';
if ($days != 0)
$uptime .= $days.' day'.Misc::pluralize($days).' ';
if ($hours != 0)
$uptime .= $hours.' hour'.Misc::pluralize($hours).' ';
if ($min != 0)
$uptime .= $min.' minute'.Misc::pluralize($min);
}
// Last boot
if (!($upt_tmp = shell_exec('cat /proc/uptime')))
{
$last_boot = 'N.A';
}
else
{
$upt = explode(' ', $upt_tmp);
$last_boot = date('Y-m-d H:i:s', time() - intval($upt[0]));
}
// Current users
if (!($current_users_cmd = shell_exec('who -q')))
{
$current_users = 'N.A';
}
else
{
$arr_user = explode('=', $current_users_cmd);
$current_users = $arr_user[1];
}
// Server datetime
if (!($server_date = shell_exec('/bin/date')))
{
$server_date = date('Y-m-d H:i:s');
}
$datas = array(
'hostname' => $hostname,
'os' => $os,
'kernel' => $kernel,
'uptime' => $uptime,
'last_boot' => $last_boot,
'current_users' => $current_users,
'server_date' => $server_date,
);
echo json_encode($datas);