CPU : changes on retrieving CPU information

Load Average and CPU : fix on recovery the number of CPU cores
Last login : the datas are retrieved differently
This commit is contained in:
ShevAbam 2015-01-16 18:14:07 +01:00
parent c3ec82dd6f
commit 3ed4c1d633
6 changed files with 42 additions and 26 deletions

View File

@ -145,7 +145,6 @@ esm.getLast_login = function() {
var html = ''; var html = '';
html += '<tr>'; html += '<tr>';
html += '<td>'+data[line].user+'</td>'; html += '<td>'+data[line].user+'</td>';
html += '<td>'+data[line].host+'</td>';
html += '<td class="w50p">'+data[line].date+'</td>'; html += '<td class="w50p">'+data[line].date+'</td>';
html += '</tr>'; html += '</tr>';

View File

@ -2,24 +2,24 @@
class Misc class Misc
{ {
/** /**
* Returns human size * Returns human size
*/ */
public static function getSize($filesize, $precision = 2) public static function getSize($filesize, $precision = 2)
{ {
$units = array('', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y'); $units = array('', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y');
foreach ($units as $idUnit => $unit) foreach ($units as $idUnit => $unit)
{ {
if ($filesize > 1024) if ($filesize > 1024)
$filesize /= 1024; $filesize /= 1024;
else else
break; break;
} }
return round($filesize, $precision).' '.$units[$idUnit].'B'; return round($filesize, $precision).' '.$units[$idUnit].'B';
} }
/** /**
* Returns hostname * Returns hostname
@ -30,6 +30,26 @@ class Misc
} }
/**
* 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 * Returns server IP
*/ */

View File

@ -1,21 +1,17 @@
<?php <?php
require 'Utils/Misc.class.php';
// Number of cores // Number of cores
if (!($num_cores = shell_exec('/bin/grep -c ^processor /proc/cpuinfo'))) $num_cores = Misc::getCpuCoresNumber();
{
$num_cores = 'N.A';
}
// CPU info // CPU info
if (!($cpuinfo = shell_exec('cat /proc/cpuinfo'))) $model = 'N.A';
{ $frequency = 'N.A';
$model = 'N.A'; $cache = 'N.A';
$frequency = 'N.A'; $bogomips = 'N.A';
$cache = 'N.A';
$bogomips = 'N.A'; if ($cpuinfo = shell_exec('cat /proc/cpuinfo'))
}
else
{ {
$processors = preg_split('/\s?\n\s?\n/', trim($cpuinfo)); $processors = preg_split('/\s?\n\s?\n/', trim($cpuinfo));
@ -27,15 +23,16 @@ else
{ {
list($key, $value) = preg_split('/\s*:\s*/', trim($detail)); list($key, $value) = preg_split('/\s*:\s*/', trim($detail));
switch ($key) switch (strtolower($key))
{ {
case 'model name': case 'model name':
case 'cpu model': case 'cpu model':
case 'cpu': case 'cpu':
case 'processor':
$model = $value; $model = $value;
break; break;
case 'cpu MHz': case 'cpu mhz':
case 'clock': case 'clock':
$frequency = $value.' MHz'; $frequency = $value.' MHz';
break; break;

View File

@ -5,11 +5,10 @@ $Config = new Config();
$datas = array(); $datas = array();
if (!(exec('/usr/bin/lastlog --time 365 | /usr/bin/awk \'{print $1","$3","$4" "$5" "$6" "$7" "$8}\'', $users))) if (!(exec('/usr/bin/lastlog --time 365 | /usr/bin/awk -F\' \' \'{ print $1";"$5, $4, $8, $6}\'', $users)))
{ {
$datas[] = array( $datas[] = array(
'user' => 'N.A', 'user' => 'N.A',
'host' => 'N.A',
'date' => 'N.A', 'date' => 'N.A',
); );
} }
@ -19,11 +18,10 @@ else
for ($i = 1; $i < count($users) && $i <= $max; $i++) for ($i = 1; $i < count($users) && $i <= $max; $i++)
{ {
list($user, $host, $date) = explode(',', $users[$i]); list($user, $date) = explode(';', $users[$i]);
$datas[] = array( $datas[] = array(
'user' => $user, 'user' => $user,
'host' => $host,
'date' => $date, 'date' => $date,
); );
} }

View File

@ -1,4 +1,5 @@
<?php <?php
require 'Utils/Misc.class.php';
if (!($load_tmp = shell_exec('cat /proc/loadavg | awk \'{print $1","$2","$3}\''))) if (!($load_tmp = shell_exec('cat /proc/loadavg | awk \'{print $1","$2","$3}\'')))
{ {
@ -7,7 +8,7 @@ if (!($load_tmp = shell_exec('cat /proc/loadavg | awk \'{print $1","$2","$3}\'')
else else
{ {
// Number of cores // Number of cores
$cores = (int)shell_exec('grep -c ^processor /proc/cpuinfo'); $cores = Misc::getCpuCoresNumber();
$load_exp = explode(',', $load_tmp); $load_exp = explode(',', $load_tmp);

View File

@ -15,6 +15,7 @@ if (!($os = shell_exec('/usr/bin/lsb_release -ds')))
} }
} }
} }
$os = trim($os, '"');
// Kernel // Kernel
if (!($kernel = shell_exec('/bin/uname -r'))) if (!($kernel = shell_exec('/bin/uname -r')))