Commit 683fb184 authored by Ad Schellevis's avatar Ad Schellevis

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

parent 5ef34e44
...@@ -5,331 +5,353 @@ require_once("filter.inc"); ...@@ -5,331 +5,353 @@ 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 = ""; {
$matches = ""; $boottime = "";
$boottime = get_single_sysctl("kern.boottime"); $matches = "";
preg_match("/sec = (\d+)/", $boottime, $matches); $boottime = get_single_sysctl("kern.boottime");
$boottime = $matches[1]; preg_match("/sec = (\d+)/", $boottime, $matches);
if(intval($boottime) == 0) $boottime = $matches[1];
return 0; if (intval($boottime) == 0) {
return 0;
$uptime = time() - $boottime; }
return $uptime; $uptime = time() - $boottime;
return $uptime;
} }
function get_stats() { function get_stats()
$stats['cpu'] = cpu_usage(); {
$stats['mem'] = mem_usage(); $stats['cpu'] = cpu_usage();
$stats['uptime'] = get_uptime(); $stats['mem'] = mem_usage();
$stats['states'] = get_pfstate(); $stats['uptime'] = get_uptime();
$stats['temp'] = get_temp(); $stats['states'] = get_pfstate();
$stats['datetime'] = update_date_time(); $stats['temp'] = get_temp();
$stats['interfacestatistics'] = get_interfacestats(); $stats['datetime'] = update_date_time();
$stats['interfacestatus'] = get_interfacestatus(); $stats['interfacestatistics'] = get_interfacestats();
$stats['gateways'] = get_gatewaystats(); $stats['interfacestatus'] = get_interfacestatus();
$stats['cpufreq'] = get_cpufreq(); $stats['gateways'] = get_gatewaystats();
$stats['load_average'] = get_load_average(); $stats['cpufreq'] = get_cpufreq();
$stats['mbuf'] = get_mbuf(); $stats['load_average'] = get_load_average();
$stats['mbufpercent'] = get_mbuf(true); $stats['mbuf'] = get_mbuf();
$stats['statepercent'] = get_pfstate(true); $stats['mbufpercent'] = get_mbuf(true);
$stats = join("|", $stats); $stats['statepercent'] = get_pfstate(true);
return $stats; $stats = join("|", $stats);
return $stats;
} }
function get_gatewaystats() { function get_gatewaystats()
$a_gateways = return_gateways_array(); {
$gateways_status = array(); $a_gateways = return_gateways_array();
$gateways_status = return_gateways_status(true); $gateways_status = array();
$data = ""; $gateways_status = return_gateways_status(true);
$isfirst = true; $data = "";
foreach($a_gateways as $gname => $gw) { $isfirst = true;
if(!$isfirst) foreach($a_gateways as $gname => $gw) {
$data .= ","; if (!$isfirst) {
$isfirst = false; $data .= ",";
$data .= $gw['name'] . ","; }
if ($gateways_status[$gname]) { $isfirst = false;
$data .= lookup_gateway_ip_by_name($gname) . ","; $data .= $gw['name'] . ",";
$gws = $gateways_status[$gname]; if ($gateways_status[$gname]) {
switch(strtolower($gws['status'])) { $data .= lookup_gateway_ip_by_name($gname) . ",";
case "none": $gws = $gateways_status[$gname];
$online = "Online"; switch(strtolower($gws['status'])) {
$bgcolor = "#90EE90"; // lightgreen case "none":
break; $online = "Online";
case "down": $bgcolor = "#90EE90"; // lightgreen
$online = "Offline"; break;
$bgcolor = "#F08080"; // lightcoral case "down":
break; $online = "Offline";
case "delay": $bgcolor = "#F08080"; // lightcoral
$online = "Latency"; break;
$bgcolor = "#F0E68C"; // khaki case "delay":
break; $online = "Latency";
case "loss": $bgcolor = "#F0E68C"; // khaki
$online = "Packetloss"; break;
$bgcolor = "#F0E68C"; // khaki case "loss":
break; $online = "Packetloss";
default: $bgcolor = "#F0E68C"; // khaki
$online = "Pending"; break;
break; default:
} $online = "Pending";
} else { break;
$data .= "~,"; }
$gws['delay'] = "~"; } else {
$gws['loss'] = "~"; $data .= "~,";
$online = "Unknown"; $gws['delay'] = "~";
$bgcolor = "#ADD8E6"; // lightblue $gws['loss'] = "~";
} $online = "Unknown";
$data .= ($online == "Pending") ? "{$online},{$online}," : "{$gws['delay']},{$gws['loss']},"; $bgcolor = "#ADD8E6"; // lightblue
$data .= "<table border=\"0\" cellpadding=\"0\" cellspacing=\"2\" style=\"table-layout: fixed;\" summary=\"status\"><tr><td bgcolor=\"$bgcolor\">&nbsp;$online&nbsp;</td></tr></table>"; }
} $data .= ($online == "Pending") ? "{$online},{$online}," : "{$gws['delay']},{$gws['loss']},";
return $data; $data .= "<table border=\"0\" cellpadding=\"0\" cellspacing=\"2\" style=\"table-layout: fixed;\" summary=\"status\"><tr><td bgcolor=\"$bgcolor\">&nbsp;$online&nbsp;</td></tr></table>";
}
return $data;
} }
function get_uptime() { function get_uptime()
$uptime = get_uptime_sec(); {
$uptime = get_uptime_sec();
if(intval($uptime) == 0)
return; if (intval($uptime) == 0) {
return;
$updays = (int)($uptime / 86400); }
$uptime %= 86400;
$uphours = (int)($uptime / 3600); $updays = (int)($uptime / 86400);
$uptime %= 3600; $uptime %= 86400;
$upmins = (int)($uptime / 60); $uphours = (int)($uptime / 3600);
$uptime %= 60; $uptime %= 3600;
$upsecs = (int)($uptime); $upmins = (int)($uptime / 60);
$uptime %= 60;
$uptimestr = ""; $upsecs = (int)($uptime);
if ($updays > 1)
$uptimestr .= "$updays Days "; $uptimestr = "";
else if ($updays > 0) if ($updays > 1) {
$uptimestr .= "1 Day "; $uptimestr .= "$updays Days ";
} elseif ($updays > 0) {
if ($uphours > 1) { $uptimestr .= "1 Day ";
$hours = "s"; }
} else {
$hours = ""; if ($uphours > 1) {
} $hours = "s";
} else {
if ($upmins > 1) { $hours = "";
$minutes = "s"; }
} else {
$minutes = "" ; if ($upmins > 1) {
} $minutes = "s";
} else {
if ($upmins > 1) { $minutes = "" ;
$seconds = "s"; }
} else {
$seconds = ""; if ($upmins > 1) {
} $seconds = "s";
} else {
$uptimestr .= sprintf("%02d Hour$hours %02d Minute$minutes %02d Second$seconds", $uphours, $upmins, $upsecs); $seconds = "";
return $uptimestr; }
$uptimestr .= sprintf("%02d Hour$hours %02d Minute$minutes %02d Second$seconds", $uphours, $upmins, $upsecs);
return $uptimestr;
} }
/* 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; {
$diff = array('user', 'nice', 'sys', 'intr', 'idle'); $duration = 1;
$cpuTicks = array_combine($diff, explode(" ", get_single_sysctl('kern.cp_time'))); $diff = array('user', 'nice', 'sys', 'intr', 'idle');
sleep($duration); $cpuTicks = array_combine($diff, explode(" ", get_single_sysctl('kern.cp_time')));
$cpuTicks2 = array_combine($diff, explode(" ", get_single_sysctl('kern.cp_time'))); sleep($duration);
$cpuTicks2 = array_combine($diff, explode(" ", get_single_sysctl('kern.cp_time')));
$totalStart = array_sum($cpuTicks); $totalStart = array_sum($cpuTicks);
$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']);
// Calculate the percentage used // Calculate the percentage used
$cpuUsage = floor(100 * ($totalUsed / ($totalEnd - $totalStart))); $cpuUsage = floor(100 * ($totalUsed / ($totalEnd - $totalStart)));
return $cpuUsage; return $cpuUsage;
} }
function get_pfstate($percent=false) { function get_pfstate($percent=false)
global $config; {
$matches = ""; global $config;
if (isset($config['system']['maximumstates']) and $config['system']['maximumstates'] > 0) $matches = "";
$maxstates="{$config['system']['maximumstates']}"; if (isset($config['system']['maximumstates']) and $config['system']['maximumstates'] > 0) {
else $maxstates="{$config['system']['maximumstates']}";
$maxstates=default_state_size(); } else {
$curentries = `/sbin/pfctl -si |grep current`; $maxstates=default_state_size();
if (preg_match("/([0-9]+)/", $curentries, $matches)) { }
$curentries = $matches[1]; $curentries = `/sbin/pfctl -si |grep current`;
} if (preg_match("/([0-9]+)/", $curentries, $matches)) {
if (!is_numeric($curentries)) $curentries = $matches[1];
$curentries = 0; }
if ($percent) if (!is_numeric($curentries)) {
if (intval($maxstates) > 0) $curentries = 0;
return round(($curentries / $maxstates) * 100, 0); }
else if ($percent) {
return "NA"; if (intval($maxstates) > 0) {
else return round(($curentries / $maxstates) * 100, 0);
return $curentries . "/" . $maxstates; } else {
return "NA";
}
} else {
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 }'`); {
list( $mbufs_current, $mbufs_cache, $mbufs_total, $mbufs_max ) = explode( "/", $mbufs_output); $mbufs_output=trim(`/usr/bin/netstat -mb | /usr/bin/grep "mbuf clusters in use" | /usr/bin/awk '{ print $1 }'`);
if ($percent) list( $mbufs_current, $mbufs_cache, $mbufs_total, $mbufs_max ) = explode( "/", $mbufs_output);
if ($mbufs_max > 0) if ($percent) {
return round(($mbufs_total / $mbufs_max) * 100, 0); if ($mbufs_max > 0) {
else return round(($mbufs_total / $mbufs_max) * 100, 0);
return "NA"; } else {
else return "NA";
return "{$mbufs_total}/{$mbufs_max}"; }
} else {
return "{$mbufs_total}/{$mbufs_max}";
}
} }
function get_temp() { function get_temp()
$temp_out = get_single_sysctl("dev.cpu.0.temperature"); {
if ($temp_out == "") $temp_out = get_single_sysctl("dev.cpu.0.temperature");
$temp_out = get_single_sysctl("hw.acpi.thermal.tz0.temperature"); if ($temp_out == "") {
$temp_out = get_single_sysctl("hw.acpi.thermal.tz0.temperature");
// Remove 'C' from the end }
return rtrim($temp_out, 'C'); // Remove 'C' from the end
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 = ""; {
$filesystems = array(); $mout = "";
exec("/bin/df -Tht ufs,tmpfs,zfs,cd9660 | /usr/bin/awk '{print $1, $2, $3, $4, $6, $7;}'", $mout); $filesystems = array();
exec("/bin/df -Tht ufs,tmpfs,zfs,cd9660 | /usr/bin/awk '{print $1, $2, $3, $4, $6, $7;}'", $mout);
/* Get rid of the header */
array_shift($mout); /* Get rid of the header */
foreach ($mout as $fs) { array_shift($mout);
$f = array(); foreach ($mout as $fs) {
list($f['device'], $f['type'], $f['total_size'], $f['used_size'], $f['percent_used'], $f['mountpoint']) = explode(' ', $fs); $f = array();
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); {
$swap_used = ""; exec("/usr/sbin/swapinfo", $swap_info);
foreach ($swap_info as $line) $swap_used = "";
if (preg_match('/(\d+)%$/', $line, $matches)) { foreach ($swap_info as $line) {
$swap_used = $matches[1]; if (preg_match('/(\d+)%$/', $line, $matches)) {
break; $swap_used = $matches[1];
} break;
}
return $swap_used; }
return $swap_used;
} }
function mem_usage() { function mem_usage()
$totalMem = get_single_sysctl("vm.stats.vm.v_page_count"); {
if ($totalMem > 0) { $totalMem = get_single_sysctl("vm.stats.vm.v_page_count");
$inactiveMem = get_single_sysctl("vm.stats.vm.v_inactive_count"); if ($totalMem > 0) {
$cachedMem = get_single_sysctl("vm.stats.vm.v_cache_count"); $inactiveMem = get_single_sysctl("vm.stats.vm.v_inactive_count");
$freeMem = get_single_sysctl("vm.stats.vm.v_free_count"); $cachedMem = get_single_sysctl("vm.stats.vm.v_cache_count");
$usedMem = $totalMem - ($inactiveMem + $cachedMem + $freeMem); $freeMem = get_single_sysctl("vm.stats.vm.v_free_count");
$memUsage = round(($usedMem * 100) / $totalMem, 0); $usedMem = $totalMem - ($inactiveMem + $cachedMem + $freeMem);
} else $memUsage = round(($usedMem * 100) / $totalMem, 0);
$memUsage = "NA"; } else {
$memUsage = "NA";
return $memUsage; }
return $memUsage;
} }
function update_date_time() { function update_date_time()
$datetime = date("D M j G:i:s T Y"); {
return $datetime; $datetime = date("D M j G:i:s T Y");
return $datetime;
} }
function get_cpufreq() { function get_cpufreq()
$cpufreqs = ""; {
$out = ""; $cpufreqs = "";
$cpufreqs = explode(" ", get_single_sysctl('dev.cpu.0.freq_levels')); $out = "";
$maxfreq = explode("/", $cpufreqs[0]); $cpufreqs = explode(" ", get_single_sysctl('dev.cpu.0.freq_levels'));
$maxfreq = $maxfreq[0]; $maxfreq = explode("/", $cpufreqs[0]);
$curfreq = ""; $maxfreq = $maxfreq[0];
$curfreq = get_single_sysctl('dev.cpu.0.freq'); $curfreq = "";
if (($curfreq > 0) && ($curfreq != $maxfreq)) $curfreq = get_single_sysctl('dev.cpu.0.freq');
$out = "Current: {$curfreq} MHz, Max: {$maxfreq} MHz"; if (($curfreq > 0) && ($curfreq != $maxfreq)) {
return $out; $out = "Current: {$curfreq} MHz, Max: {$maxfreq} MHz";
}
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);
$cpucount = $cpudetail[0]; $cpucount = $cpudetail[0];
} }
return $cpucount; return $cpucount;
} }
function get_load_average() { function get_load_average()
$load_average = ""; {
exec("/usr/bin/uptime | /usr/bin/sed 's/^.*: //'", $load_average); $load_average = "";
return $load_average[0]; exec("/usr/bin/uptime | /usr/bin/sed 's/^.*: //'", $load_average);
return $load_average[0];
} }
function get_interfacestats() function get_interfacestats()
{ {
$ifdescrs = get_configured_interface_list(); $ifdescrs = get_configured_interface_list();
$array_collisions = array(); $array_collisions = array();
$new_data = ''; $new_data = '';
foreach ($ifdescrs as $ifdescr => $ifname) { foreach ($ifdescrs as $ifdescr => $ifname) {
$ifinfo = get_interface_info($ifdescr); $ifinfo = get_interface_info($ifdescr);
$new_data .= "{$ifinfo['inpkts']},"; $new_data .= "{$ifinfo['inpkts']},";
$new_data .= "{$ifinfo['outpkts']},"; $new_data .= "{$ifinfo['outpkts']},";
$new_data .= format_bytes($ifinfo['inbytes']) . ","; $new_data .= format_bytes($ifinfo['inbytes']) . ",";
$new_data .= format_bytes($ifinfo['outbytes']) . ","; $new_data .= format_bytes($ifinfo['outbytes']) . ",";
if (isset($ifinfo['inerrs'])){ if (isset($ifinfo['inerrs'])){
$new_data .= "{$ifinfo['inerrs']},"; $new_data .= "{$ifinfo['inerrs']},";
$new_data .= "{$ifinfo['outerrs']},"; $new_data .= "{$ifinfo['outerrs']},";
} else { } else {
$new_data .= "0,"; $new_data .= "0,";
$new_data .= "0,"; $new_data .= "0,";
} }
if (isset($ifinfo['collisions'])) { if (isset($ifinfo['collisions'])) {
$new_data .= htmlspecialchars($ifinfo['collisions']) . ","; $new_data .= htmlspecialchars($ifinfo['collisions']) . ",";
} else { } else {
$new_data .= "0,"; $new_data .= "0,";
} }
} }
return $new_data;
return $new_data;
} }
function get_interfacestatus() function get_interfacestatus()
{ {
$ifdescrs = get_configured_interface_with_descr(); $ifdescrs = get_configured_interface_with_descr();
$data = ''; $data = '';
foreach ($ifdescrs as $ifdescr => $ifname) { foreach ($ifdescrs as $ifdescr => $ifname) {
$ifinfo = get_interface_info($ifdescr); $ifinfo = get_interface_info($ifdescr);
$data .= $ifname . ","; $data .= $ifname . ",";
if ($ifinfo['status'] == "up" || $ifinfo['status'] == "associated") { if ($ifinfo['status'] == "up" || $ifinfo['status'] == "associated") {
$data .= "up"; $data .= "up";
} elseif ($ifinfo['status'] == "no carrier") { } elseif ($ifinfo['status'] == "no carrier") {
$data .= "down"; $data .= "down";
} elseif ($ifinfo['status'] == "down") { } elseif ($ifinfo['status'] == "down") {
$data .= "block"; $data .= "block";
} }
$data .= ","; $data .= ",";
if ($ifinfo['ipaddr']) { if ($ifinfo['ipaddr']) {
$data .= htmlspecialchars($ifinfo['ipaddr']); $data .= htmlspecialchars($ifinfo['ipaddr']);
} }
$data .= ","; $data .= ",";
if ($ifinfo['status'] != "down") { if ($ifinfo['status'] != "down") {
$data .= htmlspecialchars($ifinfo['media']); $data .= htmlspecialchars($ifinfo['media']);
} }
$data .= "~"; $data .= "~";
} }
return $data; return $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