summaryrefslogtreecommitdiff
path: root/libs
diff options
context:
space:
mode:
authorShevAbam <shevabam@gmail.com>2015-07-07 15:57:41 +0200
committerShevAbam <shevabam@gmail.com>2015-07-07 15:57:41 +0200
commit9970343fe5c8226f25234addc6b80836c092fe1f (patch)
treece8f029f3ce9251bd4c207a6427c356ad9042525 /libs
parentdd982cee0716e38e68bc42ffb24952b5675a5b19 (diff)
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)
Diffstat (limited to 'libs')
-rw-r--r--libs/Utils/Config.php (renamed from libs/Utils/Config.class.php)5
-rw-r--r--libs/Utils/Misc.php (renamed from libs/Utils/Misc.class.php)72
-rw-r--r--libs/cpu.php31
-rw-r--r--libs/disk.php3
-rw-r--r--libs/last_login.php2
-rw-r--r--libs/load_average.php2
-rw-r--r--libs/memory.php6
-rw-r--r--libs/network.php3
-rw-r--r--libs/ping.php2
-rw-r--r--libs/services.php44
-rw-r--r--libs/swap.php6
-rw-r--r--libs/system.php4
12 files changed, 136 insertions, 44 deletions
diff --git a/libs/Utils/Config.class.php b/libs/Utils/Config.php
index c9140c6..212a227 100644
--- a/libs/Utils/Config.class.php
+++ b/libs/Utils/Config.php
@@ -9,7 +9,7 @@ class Config
{
$this->_checkPHPVersion(5.3);
- $this->file = __DIR__.'/../../esm.config.json';
+ $this->file = __DIR__.'/../../conf/esm.config.json';
if (!file_exists($this->file))
throw new \Exception('Config file '.basename($this->file).' not found');
@@ -47,7 +47,8 @@ class Config
}
}
- return $tab == $this->config ? null : $tab;
+ // return $tab == $this->config ? null : $tab;
+ return $tab;
}
diff --git a/libs/Utils/Misc.class.php b/libs/Utils/Misc.php
index e2fe7cb..a86312b 100644
--- a/libs/Utils/Misc.class.php
+++ b/libs/Utils/Misc.php
@@ -4,6 +4,10 @@ class Misc
{
/**
* Returns human size
+ *
+ * @param float $filesize File size
+ * @param int $precision Number of decimals
+ * @return string Human size
*/
public static function getSize($filesize, $precision = 2)
{
@@ -23,6 +27,8 @@ class Misc
/**
* Returns hostname
+ *
+ * @return string Hostname
*/
public static function getHostname()
{
@@ -32,6 +38,8 @@ class Misc
/**
* Returns CPU cores number
+ *
+ * @return int Number of cores
*/
public static function getCpuCoresNumber()
{
@@ -52,6 +60,8 @@ class Misc
/**
* Returns server IP
+ *
+ * @return string Server local IP
*/
public static function getLanIp()
{
@@ -61,6 +71,11 @@ class Misc
/**
* 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)
{
@@ -89,14 +104,61 @@ class Misc
* 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
+ * @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 @@
<?php
-require 'Utils/Misc.class.php';
+require '../autoload.php';
+$Config = new Config();
// Number of cores
$num_cores = Misc::getCpuCoresNumber();
@@ -10,6 +11,7 @@ $model = 'N.A';
$frequency = 'N.A';
$cache = 'N.A';
$bogomips = 'N.A';
+$temp = 'N.A';
if ($cpuinfo = shell_exec('cat /proc/cpuinfo'))
{
@@ -50,6 +52,32 @@ if ($cpuinfo = shell_exec('cat /proc/cpuinfo'))
}
}
+if ($frequency == 'N.A')
+{
+ if ($f = shell_exec('cat /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq'))
+ {
+ $f = $f / 1000;
+ $frequency = $f.' MHz';
+ }
+}
+
+// CPU Temp
+if ($Config->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 @@
<?php
-require 'Utils/Misc.class.php';
-require 'Utils/Config.class.php';
+require '../autoload.php';
$Config = new Config();
$datas = array();
diff --git a/libs/last_login.php b/libs/last_login.php
index e93bbf8..2063731 100644
--- a/libs/last_login.php
+++ b/libs/last_login.php
@@ -1,5 +1,5 @@
<?php
-require 'Utils/Config.class.php';
+require '../autoload.php';
$Config = new Config();
diff --git a/libs/load_average.php b/libs/load_average.php
index 2e1947d..09a2f3c 100644
--- a/libs/load_average.php
+++ b/libs/load_average.php
@@ -1,5 +1,5 @@
<?php
-require 'Utils/Misc.class.php';
+require '../autoload.php';
if (!($load_tmp = shell_exec('cat /proc/loadavg | awk \'{print $1","$2","$3}\'')))
{
diff --git a/libs/memory.php b/libs/memory.php
index 81d9ac9..a4eda7b 100644
--- a/libs/memory.php
+++ b/libs/memory.php
@@ -1,5 +1,5 @@
<?php
-require 'Utils/Misc.class.php';
+require '../autoload.php';
$free = 0;
@@ -22,7 +22,9 @@ if (!($total = shell_exec('grep MemTotal /proc/meminfo | awk \'{print $2}\'')))
$used = $total - $free;
// Percent used
-$percent_used = 100 - (round($free / $total * 100));
+$percent_used = 0;
+if ($total > 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 @@
<?php
-require 'Utils/Misc.class.php';
+require '../autoload.php';
$datas = array();
$network = array();
-
// Possible commands for ifconfig and ip
$commands = array(
'ifconfig' => 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 @@
<?php
-require 'Utils/Config.class.php';
+require '../autoload.php';
$Config = new Config();
diff --git a/libs/services.php b/libs/services.php
index 9b83acb..61411e4 100644
--- a/libs/services.php
+++ b/libs/services.php
@@ -1,35 +1,33 @@
<?php
-require 'Utils/Config.class.php';
+require '../autoload.php';
$Config = new Config();
$datas = array();
-if (count($Config->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 @@
<?php
-require 'Utils/Misc.class.php';
+require '../autoload.php';
// Free
if (!($free = shell_exec('grep SwapFree /proc/meminfo | awk \'{print $2}\'')))
@@ -17,7 +17,9 @@ if (!($total = shell_exec('grep SwapTotal /proc/meminfo | awk \'{print $2}\'')))
$used = $total - $free;
// Percent used
-$percent_used = 100 - (round($free / $total * 100));
+$percent_used = 0;
+if ($total > 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 @@
<?php
-require 'Utils/Misc.class.php';
+require '../autoload.php';
// Hostname
$hostname = php_uname('n');
@@ -9,7 +9,7 @@ if (!($os = shell_exec('/usr/bin/lsb_release -ds | cut -d= -f2 | tr -d \'"\'')))
{
if(!($os = shell_exec('cat /etc/system-release | cut -d= -f2 | tr -d \'"\'')))
{
- if (!($os = shell_exec('find /etc/*-release -type f -exec cat {} \; | grep NAME | tail -n 1 | cut -d= -f2 | tr -d \'"\'')))
+ if (!($os = shell_exec('find /etc/*-release -type f -exec cat {} \; | grep PRETTY_NAME | tail -n 1 | cut -d= -f2 | tr -d \'"\'')))
{
$os = 'N.A';
}