From 20705a550df5b13a544dc0865c6aef16e64c99d9 Mon Sep 17 00:00:00 2001 From: ShevAbam Date: Wed, 18 Jun 2014 10:11:11 +0200 Subject: + First commit (version 2.0) --- libs/Utils/Config.class.php | 94 +++++++++++++++++++++++++++++++++++++++++++++ libs/Utils/Misc.class.php | 57 +++++++++++++++++++++++++++ libs/cpu.php | 65 +++++++++++++++++++++++++++++++ libs/disk.php | 49 +++++++++++++++++++++++ libs/last_login.php | 32 +++++++++++++++ libs/load_average.php | 25 ++++++++++++ libs/memory.php | 30 +++++++++++++++ libs/network.php | 32 +++++++++++++++ libs/ping.php | 25 ++++++++++++ libs/services.php | 37 ++++++++++++++++++ libs/swap.php | 31 +++++++++++++++ libs/system.php | 83 +++++++++++++++++++++++++++++++++++++++ 12 files changed, 560 insertions(+) create mode 100644 libs/Utils/Config.class.php create mode 100644 libs/Utils/Misc.class.php create mode 100644 libs/cpu.php create mode 100644 libs/disk.php create mode 100644 libs/last_login.php create mode 100644 libs/load_average.php create mode 100644 libs/memory.php create mode 100644 libs/network.php create mode 100644 libs/ping.php create mode 100644 libs/services.php create mode 100644 libs/swap.php create mode 100644 libs/system.php (limited to 'libs') 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 @@ +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 @@ + $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 @@ + $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 @@ + '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 @@ + '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 @@ + 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 @@ + 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 @@ + '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 @@ +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 @@ +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 @@ + 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 @@ + $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 -- cgit v1.3