Commit 11f6f7d6 authored by Ad Schellevis's avatar Ad Schellevis Committed by Franco Fichtner

(legacy) another spaces and curly braces, stats.inc

(cherry picked from commit 683fb184)
parent 44da7ebd
...@@ -5,20 +5,22 @@ require_once("filter.inc"); ...@@ -5,20 +5,22 @@ require_once("filter.inc");
require_once("pfsense-utils.inc"); require_once("pfsense-utils.inc");
require_once("interfaces.inc"); require_once("interfaces.inc");
function get_uptime_sec() { function get_uptime_sec()
{
$boottime = ""; $boottime = "";
$matches = ""; $matches = "";
$boottime = get_single_sysctl("kern.boottime"); $boottime = get_single_sysctl("kern.boottime");
preg_match("/sec = (\d+)/", $boottime, $matches); preg_match("/sec = (\d+)/", $boottime, $matches);
$boottime = $matches[1]; $boottime = $matches[1];
if(intval($boottime) == 0) if (intval($boottime) == 0) {
return 0; return 0;
}
$uptime = time() - $boottime; $uptime = time() - $boottime;
return $uptime; return $uptime;
} }
function get_stats() { function get_stats()
{
$stats['cpu'] = cpu_usage(); $stats['cpu'] = cpu_usage();
$stats['mem'] = mem_usage(); $stats['mem'] = mem_usage();
$stats['uptime'] = get_uptime(); $stats['uptime'] = get_uptime();
...@@ -37,15 +39,17 @@ function get_stats() { ...@@ -37,15 +39,17 @@ function get_stats() {
return $stats; return $stats;
} }
function get_gatewaystats() { function get_gatewaystats()
{
$a_gateways = return_gateways_array(); $a_gateways = return_gateways_array();
$gateways_status = array(); $gateways_status = array();
$gateways_status = return_gateways_status(true); $gateways_status = return_gateways_status(true);
$data = ""; $data = "";
$isfirst = true; $isfirst = true;
foreach($a_gateways as $gname => $gw) { foreach($a_gateways as $gname => $gw) {
if(!$isfirst) if (!$isfirst) {
$data .= ","; $data .= ",";
}
$isfirst = false; $isfirst = false;
$data .= $gw['name'] . ","; $data .= $gw['name'] . ",";
if ($gateways_status[$gname]) { if ($gateways_status[$gname]) {
...@@ -85,11 +89,13 @@ function get_gatewaystats() { ...@@ -85,11 +89,13 @@ function get_gatewaystats() {
return $data; return $data;
} }
function get_uptime() { function get_uptime()
{
$uptime = get_uptime_sec(); $uptime = get_uptime_sec();
if(intval($uptime) == 0) if (intval($uptime) == 0) {
return; return;
}
$updays = (int)($uptime / 86400); $updays = (int)($uptime / 86400);
$uptime %= 86400; $uptime %= 86400;
...@@ -100,10 +106,11 @@ function get_uptime() { ...@@ -100,10 +106,11 @@ function get_uptime() {
$upsecs = (int)($uptime); $upsecs = (int)($uptime);
$uptimestr = ""; $uptimestr = "";
if ($updays > 1) if ($updays > 1) {
$uptimestr .= "$updays Days "; $uptimestr .= "$updays Days ";
else if ($updays > 0) } elseif ($updays > 0) {
$uptimestr .= "1 Day "; $uptimestr .= "1 Day ";
}
if ($uphours > 1) { if ($uphours > 1) {
$hours = "s"; $hours = "s";
...@@ -128,7 +135,8 @@ function get_uptime() { ...@@ -128,7 +135,8 @@ function get_uptime() {
} }
/* Calculates non-idle CPU time and returns as a percentage */ /* Calculates non-idle CPU time and returns as a percentage */
function cpu_usage() { function cpu_usage()
{
$duration = 1; $duration = 1;
$diff = array('user', 'nice', 'sys', 'intr', 'idle'); $diff = array('user', 'nice', 'sys', 'intr', 'idle');
$cpuTicks = array_combine($diff, explode(" ", get_single_sysctl('kern.cp_time'))); $cpuTicks = array_combine($diff, explode(" ", get_single_sysctl('kern.cp_time')));
...@@ -139,8 +147,9 @@ function cpu_usage() { ...@@ -139,8 +147,9 @@ function cpu_usage() {
$totalEnd = array_sum($cpuTicks2); $totalEnd = array_sum($cpuTicks2);
// Something wrapped ?!?! // Something wrapped ?!?!
if ($totalEnd <= $totalStart) if ($totalEnd <= $totalStart) {
return 0; return 0;
}
// Calculate total cycles used // Calculate total cycles used
$totalUsed = ($totalEnd - $totalStart) - ($cpuTicks2['idle'] - $cpuTicks['idle']); $totalUsed = ($totalEnd - $totalStart) - ($cpuTicks2['idle'] - $cpuTicks['idle']);
...@@ -151,51 +160,61 @@ function cpu_usage() { ...@@ -151,51 +160,61 @@ function cpu_usage() {
return $cpuUsage; return $cpuUsage;
} }
function get_pfstate($percent=false) { function get_pfstate($percent=false)
{
global $config; global $config;
$matches = ""; $matches = "";
if (isset($config['system']['maximumstates']) and $config['system']['maximumstates'] > 0) if (isset($config['system']['maximumstates']) and $config['system']['maximumstates'] > 0) {
$maxstates="{$config['system']['maximumstates']}"; $maxstates="{$config['system']['maximumstates']}";
else } else {
$maxstates=default_state_size(); $maxstates=default_state_size();
}
$curentries = `/sbin/pfctl -si |grep current`; $curentries = `/sbin/pfctl -si |grep current`;
if (preg_match("/([0-9]+)/", $curentries, $matches)) { if (preg_match("/([0-9]+)/", $curentries, $matches)) {
$curentries = $matches[1]; $curentries = $matches[1];
} }
if (!is_numeric($curentries)) if (!is_numeric($curentries)) {
$curentries = 0; $curentries = 0;
if ($percent) }
if (intval($maxstates) > 0) if ($percent) {
if (intval($maxstates) > 0) {
return round(($curentries / $maxstates) * 100, 0); return round(($curentries / $maxstates) * 100, 0);
else } else {
return "NA"; return "NA";
else }
} else {
return $curentries . "/" . $maxstates; return $curentries . "/" . $maxstates;
}
} }
function get_mbuf($percent=false) { function get_mbuf($percent=false)
{
$mbufs_output=trim(`/usr/bin/netstat -mb | /usr/bin/grep "mbuf clusters in use" | /usr/bin/awk '{ print $1 }'`); $mbufs_output=trim(`/usr/bin/netstat -mb | /usr/bin/grep "mbuf clusters in use" | /usr/bin/awk '{ print $1 }'`);
list( $mbufs_current, $mbufs_cache, $mbufs_total, $mbufs_max ) = explode( "/", $mbufs_output); list( $mbufs_current, $mbufs_cache, $mbufs_total, $mbufs_max ) = explode( "/", $mbufs_output);
if ($percent) if ($percent) {
if ($mbufs_max > 0) if ($mbufs_max > 0) {
return round(($mbufs_total / $mbufs_max) * 100, 0); return round(($mbufs_total / $mbufs_max) * 100, 0);
else } else {
return "NA"; return "NA";
else }
} else {
return "{$mbufs_total}/{$mbufs_max}"; return "{$mbufs_total}/{$mbufs_max}";
}
} }
function get_temp() { function get_temp()
{
$temp_out = get_single_sysctl("dev.cpu.0.temperature"); $temp_out = get_single_sysctl("dev.cpu.0.temperature");
if ($temp_out == "") if ($temp_out == "") {
$temp_out = get_single_sysctl("hw.acpi.thermal.tz0.temperature"); $temp_out = get_single_sysctl("hw.acpi.thermal.tz0.temperature");
}
// Remove 'C' from the end // Remove 'C' from the end
return rtrim($temp_out, 'C'); return rtrim($temp_out, 'C');
} }
/* Get mounted filesystems and usage. Do not display entries for virtual filesystems (e.g. devfs, nullfs, unionfs) */ /* Get mounted filesystems and usage. Do not display entries for virtual filesystems (e.g. devfs, nullfs, unionfs) */
function get_mounted_filesystems() { function get_mounted_filesystems()
{
$mout = ""; $mout = "";
$filesystems = array(); $filesystems = array();
exec("/bin/df -Tht ufs,tmpfs,zfs,cd9660 | /usr/bin/awk '{print $1, $2, $3, $4, $6, $7;}'", $mout); exec("/bin/df -Tht ufs,tmpfs,zfs,cd9660 | /usr/bin/awk '{print $1, $2, $3, $4, $6, $7;}'", $mout);
...@@ -205,29 +224,29 @@ function get_mounted_filesystems() { ...@@ -205,29 +224,29 @@ function get_mounted_filesystems() {
foreach ($mout as $fs) { foreach ($mout as $fs) {
$f = array(); $f = array();
list($f['device'], $f['type'], $f['total_size'], $f['used_size'], $f['percent_used'], $f['mountpoint']) = explode(' ', $fs); list($f['device'], $f['type'], $f['total_size'], $f['used_size'], $f['percent_used'], $f['mountpoint']) = explode(' ', $fs);
/* We dont' want the trailing % sign. */ /* We dont' want the trailing % sign. */
$f['percent_used'] = trim($f['percent_used'], '%'); $f['percent_used'] = trim($f['percent_used'], '%');
$filesystems[] = $f; $filesystems[] = $f;
} }
return $filesystems; return $filesystems;
} }
function swap_usage() { function swap_usage()
{
exec("/usr/sbin/swapinfo", $swap_info); exec("/usr/sbin/swapinfo", $swap_info);
$swap_used = ""; $swap_used = "";
foreach ($swap_info as $line) foreach ($swap_info as $line) {
if (preg_match('/(\d+)%$/', $line, $matches)) { if (preg_match('/(\d+)%$/', $line, $matches)) {
$swap_used = $matches[1]; $swap_used = $matches[1];
break; break;
} }
}
return $swap_used; return $swap_used;
} }
function mem_usage() { function mem_usage()
{
$totalMem = get_single_sysctl("vm.stats.vm.v_page_count"); $totalMem = get_single_sysctl("vm.stats.vm.v_page_count");
if ($totalMem > 0) { if ($totalMem > 0) {
$inactiveMem = get_single_sysctl("vm.stats.vm.v_inactive_count"); $inactiveMem = get_single_sysctl("vm.stats.vm.v_inactive_count");
...@@ -235,19 +254,21 @@ function mem_usage() { ...@@ -235,19 +254,21 @@ function mem_usage() {
$freeMem = get_single_sysctl("vm.stats.vm.v_free_count"); $freeMem = get_single_sysctl("vm.stats.vm.v_free_count");
$usedMem = $totalMem - ($inactiveMem + $cachedMem + $freeMem); $usedMem = $totalMem - ($inactiveMem + $cachedMem + $freeMem);
$memUsage = round(($usedMem * 100) / $totalMem, 0); $memUsage = round(($usedMem * 100) / $totalMem, 0);
} else } else {
$memUsage = "NA"; $memUsage = "NA";
}
return $memUsage; return $memUsage;
} }
function update_date_time() { function update_date_time()
{
$datetime = date("D M j G:i:s T Y"); $datetime = date("D M j G:i:s T Y");
return $datetime; return $datetime;
} }
function get_cpufreq() { function get_cpufreq()
{
$cpufreqs = ""; $cpufreqs = "";
$out = ""; $out = "";
$cpufreqs = explode(" ", get_single_sysctl('dev.cpu.0.freq_levels')); $cpufreqs = explode(" ", get_single_sysctl('dev.cpu.0.freq_levels'));
...@@ -255,14 +276,15 @@ function get_cpufreq() { ...@@ -255,14 +276,15 @@ function get_cpufreq() {
$maxfreq = $maxfreq[0]; $maxfreq = $maxfreq[0];
$curfreq = ""; $curfreq = "";
$curfreq = get_single_sysctl('dev.cpu.0.freq'); $curfreq = get_single_sysctl('dev.cpu.0.freq');
if (($curfreq > 0) && ($curfreq != $maxfreq)) if (($curfreq > 0) && ($curfreq != $maxfreq)) {
$out = "Current: {$curfreq} MHz, Max: {$maxfreq} MHz"; $out = "Current: {$curfreq} MHz, Max: {$maxfreq} MHz";
}
return $out; return $out;
} }
function get_cpu_count($show_detail = false) { function get_cpu_count($show_detail = false)
{
$cpucount = get_single_sysctl('kern.smp.cpus'); $cpucount = get_single_sysctl('kern.smp.cpus');
if ($show_detail) { if ($show_detail) {
$cpudetail = ""; $cpudetail = "";
exec("/usr/bin/grep 'SMP.*package.*core' /var/run/dmesg.boot | /usr/bin/cut -f2- -d' '", $cpudetail); exec("/usr/bin/grep 'SMP.*package.*core' /var/run/dmesg.boot | /usr/bin/cut -f2- -d' '", $cpudetail);
...@@ -271,7 +293,8 @@ function get_cpu_count($show_detail = false) { ...@@ -271,7 +293,8 @@ function get_cpu_count($show_detail = false) {
return $cpucount; return $cpucount;
} }
function get_load_average() { function get_load_average()
{
$load_average = ""; $load_average = "";
exec("/usr/bin/uptime | /usr/bin/sed 's/^.*: //'", $load_average); exec("/usr/bin/uptime | /usr/bin/sed 's/^.*: //'", $load_average);
return $load_average[0]; return $load_average[0];
...@@ -302,7 +325,6 @@ function get_interfacestats() ...@@ -302,7 +325,6 @@ function get_interfacestats()
$new_data .= "0,"; $new_data .= "0,";
} }
} }
return $new_data; return $new_data;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment