summaryrefslogtreecommitdiff
path: root/libs
diff options
context:
space:
mode:
authorShevAbam <shevabam@gmail.com>2014-06-18 10:11:11 +0200
committerShevAbam <shevabam@gmail.com>2014-06-18 10:11:11 +0200
commit20705a550df5b13a544dc0865c6aef16e64c99d9 (patch)
treee396dface41e30d39a68d5fe66097b1f8f641a81 /libs
+ First commit (version 2.0)v2.0
Diffstat (limited to 'libs')
-rw-r--r--libs/Utils/Config.class.php94
-rw-r--r--libs/Utils/Misc.class.php57
-rw-r--r--libs/cpu.php65
-rw-r--r--libs/disk.php49
-rw-r--r--libs/last_login.php32
-rw-r--r--libs/load_average.php25
-rw-r--r--libs/memory.php30
-rw-r--r--libs/network.php32
-rw-r--r--libs/ping.php25
-rw-r--r--libs/services.php37
-rw-r--r--libs/swap.php31
-rw-r--r--libs/system.php83
12 files changed, 560 insertions, 0 deletions
diff --git a/libs/Utils/Config.class.php b/libs/Utils/Config.class.php
new file mode 100644
index 0000000..8111b32
--- /dev/null
+++ b/libs/Utils/Config.class.php
@@ -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'];
+ }
+ }
+ }
+} \ No newline at end of file
diff --git a/libs/Utils/Misc.class.php b/libs/Utils/Misc.class.php
new file mode 100644
index 0000000..3fcfedb
--- /dev/null
+++ b/libs/Utils/Misc.class.php
@@ -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;
+ }
+} \ No newline at end of file
diff --git a/libs/cpu.php b/libs/cpu.php
new file mode 100644
index 0000000..e2743f9
--- /dev/null
+++ b/libs/cpu.php
@@ -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); \ No newline at end of file
diff --git a/libs/disk.php b/libs/disk.php
new file mode 100644
index 0000000..d3d3199
--- /dev/null
+++ b/libs/disk.php
@@ -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); \ No newline at end of file
diff --git a/libs/last_login.php b/libs/last_login.php
new file mode 100644
index 0000000..5a8887e
--- /dev/null
+++ b/libs/last_login.php
@@ -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); \ No newline at end of file
diff --git a/libs/load_average.php b/libs/load_average.php
new file mode 100644
index 0000000..8eea8f9
--- /dev/null
+++ b/libs/load_average.php
@@ -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); \ No newline at end of file
diff --git a/libs/memory.php b/libs/memory.php
new file mode 100644
index 0000000..27a783e
--- /dev/null
+++ b/libs/memory.php
@@ -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); \ No newline at end of file
diff --git a/libs/network.php b/libs/network.php
new file mode 100644
index 0000000..12521bf
--- /dev/null
+++ b/libs/network.php
@@ -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); \ No newline at end of file
diff --git a/libs/ping.php b/libs/ping.php
new file mode 100644
index 0000000..1cadb70
--- /dev/null
+++ b/libs/ping.php
@@ -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); \ No newline at end of file
diff --git a/libs/services.php b/libs/services.php
new file mode 100644
index 0000000..eaedf3a
--- /dev/null
+++ b/libs/services.php
@@ -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); \ No newline at end of file
diff --git a/libs/swap.php b/libs/swap.php
new file mode 100644
index 0000000..93bd323
--- /dev/null
+++ b/libs/swap.php
@@ -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); \ No newline at end of file
diff --git a/libs/system.php b/libs/system.php
new file mode 100644
index 0000000..3e4ba1a
--- /dev/null
+++ b/libs/system.php
@@ -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); \ No newline at end of file