From 9970343fe5c8226f25234addc6b80836c092fe1f Mon Sep 17 00:00:00 2001 From: ShevAbam Date: Tue, 7 Jul 2015 15:57:41 +0200 Subject: General : cleaning and optimizing CSS General : responsive design General : reload button now spins when you reload block General : update jQuery plugin Knob to 1.2.11 General : optimizing security (config file esm.config.json is now in the conf/ folder with an htaccess) CPU : retrieves correctly CPU frequency for Raspberry Pi CPU : add CPU temperature (+ option to enable/disable) System : little correction for getting distro name Swap : fix if swap is disabled Services status : adds protocol TCP or UDP for checking service status Services status : new option to hide port number (see show_port in services section) --- libs/Utils/Config.class.php | 137 ------------------------------------ libs/Utils/Config.php | 138 +++++++++++++++++++++++++++++++++++++ libs/Utils/Misc.class.php | 102 --------------------------- libs/Utils/Misc.php | 164 ++++++++++++++++++++++++++++++++++++++++++++ libs/cpu.php | 31 ++++++++- libs/disk.php | 3 +- libs/last_login.php | 2 +- libs/load_average.php | 2 +- libs/memory.php | 6 +- libs/network.php | 3 +- libs/ping.php | 2 +- libs/services.php | 44 ++++++------ libs/swap.php | 6 +- libs/system.php | 4 +- 14 files changed, 368 insertions(+), 276 deletions(-) delete mode 100644 libs/Utils/Config.class.php create mode 100644 libs/Utils/Config.php delete mode 100644 libs/Utils/Misc.class.php create mode 100644 libs/Utils/Misc.php (limited to 'libs') diff --git a/libs/Utils/Config.class.php b/libs/Utils/Config.class.php deleted file mode 100644 index c9140c6..0000000 --- a/libs/Utils/Config.class.php +++ /dev/null @@ -1,137 +0,0 @@ -_checkPHPVersion(5.3); - - $this->file = __DIR__.'/../../esm.config.json'; - - if (!file_exists($this->file)) - throw new \Exception('Config file '.basename($this->file).' not found'); - - $this->_readFile(); - } - - private function _readFile() - { - $content = file_get_contents($this->file); - $this->config = json_decode(utf8_encode($content), true); - - if ($this->config == null && json_last_error() != JSON_ERROR_NONE) - { - throw new \LogicException(sprintf("Failed to parse config file '%s'. Error: '%s'", basename($this->file) , json_last_error_msg())); - } - } - - - /** - * 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 the PHP version compared to the required version - */ - private function _checkPHPVersion($min) - { - if (!version_compare(phpversion(), $min, '>=')) - throw new \Exception('Your PHP version is too old ! PHP '.$min.' is required.'); - - return true; - } - - - /** - * Checks if there is an eSM`Web update available - */ - public function checkUpdate() - { - if ($this->get('esm:check_updates') === false) - return null; - - $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']; - } - } - } -} - - -// PHP 5.5.0 -if (!function_exists('json_last_error_msg')) -{ - function json_last_error_msg() - { - static $errors = array( - JSON_ERROR_NONE => null, - JSON_ERROR_DEPTH => 'Maximum stack depth exceeded', - JSON_ERROR_STATE_MISMATCH => 'Underflow or the modes mismatch', - JSON_ERROR_CTRL_CHAR => 'Unexpected control character found', - JSON_ERROR_SYNTAX => 'Syntax error, malformed JSON', - JSON_ERROR_UTF8 => 'Malformed UTF-8 characters, possibly incorrectly encoded' - ); - $error = json_last_error(); - return array_key_exists($error, $errors) ? $errors[$error] : "Unknown error ({$error})"; - } -} \ No newline at end of file diff --git a/libs/Utils/Config.php b/libs/Utils/Config.php new file mode 100644 index 0000000..212a227 --- /dev/null +++ b/libs/Utils/Config.php @@ -0,0 +1,138 @@ +_checkPHPVersion(5.3); + + $this->file = __DIR__.'/../../conf/esm.config.json'; + + if (!file_exists($this->file)) + throw new \Exception('Config file '.basename($this->file).' not found'); + + $this->_readFile(); + } + + private function _readFile() + { + $content = file_get_contents($this->file); + $this->config = json_decode(utf8_encode($content), true); + + if ($this->config == null && json_last_error() != JSON_ERROR_NONE) + { + throw new \LogicException(sprintf("Failed to parse config file '%s'. Error: '%s'", basename($this->file) , json_last_error_msg())); + } + } + + + /** + * 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; + return $tab; + } + + + /** + * Returns all config variables + */ + public function getAll() + { + return $this->config; + } + + + /** + * Checks the PHP version compared to the required version + */ + private function _checkPHPVersion($min) + { + if (!version_compare(phpversion(), $min, '>=')) + throw new \Exception('Your PHP version is too old ! PHP '.$min.' is required.'); + + return true; + } + + + /** + * Checks if there is an eSM`Web update available + */ + public function checkUpdate() + { + if ($this->get('esm:check_updates') === false) + return null; + + $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']; + } + } + } +} + + +// PHP 5.5.0 +if (!function_exists('json_last_error_msg')) +{ + function json_last_error_msg() + { + static $errors = array( + JSON_ERROR_NONE => null, + JSON_ERROR_DEPTH => 'Maximum stack depth exceeded', + JSON_ERROR_STATE_MISMATCH => 'Underflow or the modes mismatch', + JSON_ERROR_CTRL_CHAR => 'Unexpected control character found', + JSON_ERROR_SYNTAX => 'Syntax error, malformed JSON', + JSON_ERROR_UTF8 => 'Malformed UTF-8 characters, possibly incorrectly encoded' + ); + $error = json_last_error(); + return array_key_exists($error, $errors) ? $errors[$error] : "Unknown error ({$error})"; + } +} \ No newline at end of file diff --git a/libs/Utils/Misc.class.php b/libs/Utils/Misc.class.php deleted file mode 100644 index e2fe7cb..0000000 --- a/libs/Utils/Misc.class.php +++ /dev/null @@ -1,102 +0,0 @@ - $unit) - { - if ($filesize > 1024) - $filesize /= 1024; - else - break; - } - - return round($filesize, $precision).' '.$units[$idUnit].'B'; - } - - - /** - * Returns hostname - */ - public static function getHostname() - { - return php_uname('n'); - } - - - /** - * Returns CPU cores number - */ - public static function getCpuCoresNumber() - { - if (!($num_cores = shell_exec('/bin/grep -c ^processor /proc/cpuinfo'))) - { - if (!($num_cores = trim(shell_exec('/usr/bin/nproc')))) - { - $num_cores = 1; - } - } - - if ((int)$num_cores <= 0) - $num_cores = 1; - - return (int)$num_cores; - } - - - /** - * Returns server IP - */ - public static function getLanIp() - { - return $_SERVER['SERVER_ADDR']; - } - - - /** - * Returns a command that exists in the system among $cmds - */ - public static function whichCommand($cmds, $args = '', $returnWithArgs = true) - { - $return = ''; - - foreach ($cmds as $cmd) - { - if (trim(shell_exec($cmd.$args)) != '') - { - $return = $cmd; - - if ($returnWithArgs) - $return .= $args; - - break; - } - } - - return $return; - } - - - /** - * 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/Utils/Misc.php b/libs/Utils/Misc.php new file mode 100644 index 0000000..a86312b --- /dev/null +++ b/libs/Utils/Misc.php @@ -0,0 +1,164 @@ + $unit) + { + if ($filesize > 1024) + $filesize /= 1024; + else + break; + } + + return round($filesize, $precision).' '.$units[$idUnit].'B'; + } + + + /** + * Returns hostname + * + * @return string Hostname + */ + public static function getHostname() + { + return php_uname('n'); + } + + + /** + * Returns CPU cores number + * + * @return int Number of cores + */ + public static function getCpuCoresNumber() + { + if (!($num_cores = shell_exec('/bin/grep -c ^processor /proc/cpuinfo'))) + { + if (!($num_cores = trim(shell_exec('/usr/bin/nproc')))) + { + $num_cores = 1; + } + } + + if ((int)$num_cores <= 0) + $num_cores = 1; + + return (int)$num_cores; + } + + + /** + * Returns server IP + * + * @return string Server local IP + */ + public static function getLanIp() + { + return $_SERVER['SERVER_ADDR']; + } + + + /** + * Returns a command that exists in the system among $cmds + * + * @param array $cmds List of commands + * @param string $args List of arguments (optional) + * @param bool $returnWithArgs If true, returns command with the arguments + * @return string Command + */ + public static function whichCommand($cmds, $args = '', $returnWithArgs = true) + { + $return = ''; + + foreach ($cmds as $cmd) + { + if (trim(shell_exec($cmd.$args)) != '') + { + $return = $cmd; + + if ($returnWithArgs) + $return .= $args; + + break; + } + } + + return $return; + } + + + /** + * 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 Number + * @param string $plural String for plural word + * @param string $singular String for singular word + * @return string String pluralized + */ + public static function pluralize($nb, $plural = 's', $singular = '') + { + return $nb > 1 ? $plural : $singular; + } + + + /** + * Checks if a port is open (TCP or UPD) + * + * @param string $host Host to check + * @param int $port Port number + * @param string $protocol tcp or udp + * @param integer $timeout Timeout + * @return bool True if the port is open else false + */ + public static function scanPort($host, $port, $protocol = 'tcp', $timeout = 3) + { + if ($protocol == 'tcp') + { + $handle = @fsockopen($host, $port, $errno, $errstr, $timeout); + + if ($handle) + return true; + else + return false; + } + elseif ($protocol == 'udp') + { + $handle = @fsockopen('udp://'.$host, $port, $errno, $errstr, $timeout); + + socket_set_timeout($handle, $timeout); + + $write = fwrite($handle, 'x00'); + + $startTime = time(); + + $header = fread($handle, 1); + + $endTime = time(); + + $timeDiff = $endTime - $startTime; + + fclose($handle); + + if ($timeDiff >= $timeout) + return true; + else + return false; + } + + return false; + } +} \ No newline at end of file diff --git a/libs/cpu.php b/libs/cpu.php index 73a5409..9665c87 100644 --- a/libs/cpu.php +++ b/libs/cpu.php @@ -1,5 +1,6 @@ get('cpu:enable_temperature')) +{ + if (exec('/usr/bin/sensors | grep -E "^(CPU Temp|Core 0)" | cut -d \'+\' -f2 | cut -d \'.\' -f1', $t)) + { + if (isset($t[0])) + $temp = $t[0].' °C'; + } + else + { + if (exec('cat /sys/class/thermal/thermal_zone0/temp', $t)) + { + $temp = round($t[0] / 1000).' °C'; + } + } +} + $datas = array( 'model' => $model, @@ -57,6 +85,7 @@ $datas = array( 'frequency' => $frequency, 'cache' => $cache, 'bogomips' => $bogomips, + 'temp' => $temp, ); echo json_encode($datas); \ No newline at end of file diff --git a/libs/disk.php b/libs/disk.php index e897818..f3da378 100644 --- a/libs/disk.php +++ b/libs/disk.php @@ -1,6 +1,5 @@ 0) + $percent_used = 100 - (round($free / $total * 100)); $datas = array( diff --git a/libs/network.php b/libs/network.php index b63d990..c24fcd4 100644 --- a/libs/network.php +++ b/libs/network.php @@ -1,10 +1,9 @@ array('ifconfig', '/sbin/ifconfig', '/usr/bin/ifconfig', '/usr/sbin/ifconfig'), diff --git a/libs/ping.php b/libs/ping.php index 9ea0ec8..dc8121d 100644 --- a/libs/ping.php +++ b/libs/ping.php @@ -1,5 +1,5 @@ get('services')) > 0) +$available_protocols = array('tcp', 'udp'); + +$show_port = $Config->get('services:show_port'); + +if (count($Config->get('services:list')) > 0) { - foreach ($Config->get('services') as $service) + foreach ($Config->get('services:list') as $service) { - $host = $service['host']; - $sock = @fsockopen($host, $service['port'], $num, $error, 5); - - if ($sock) - { - $datas[] = array( - 'port' => $service['port'], - 'name' => $service['name'], - 'status' => 1, - ); - - fclose($sock); - } + $host = $service['host']; + $port = $service['port']; + $name = $service['name']; + $protocol = isset($service['protocol']) && in_array($service['protocol'], $available_protocols) ? $service['protocol'] : 'tcp'; + + if (Misc::scanPort($host, $port, $protocol)) + $status = 1; else - { - $datas[] = array( - 'port' => $service['port'], - 'name' => $service['name'], - 'status' => 0, - ); - } + $status = 0; + + $datas[] = array( + 'port' => $show_port === true ? $port : '', + 'name' => $name, + 'status' => $status, + ); } } diff --git a/libs/swap.php b/libs/swap.php index 522660b..e3cc980 100644 --- a/libs/swap.php +++ b/libs/swap.php @@ -1,5 +1,5 @@ 0) + $percent_used = 100 - (round($free / $total * 100)); $datas = array( diff --git a/libs/system.php b/libs/system.php index f569d30..015b1dd 100644 --- a/libs/system.php +++ b/libs/system.php @@ -1,5 +1,5 @@