Created functions for most graphs (memory isn't done yet) to remove some code duplication

Temp graph, mem graph, and uptime are now dynamic (inline with cpu usage)
swap still needs to be done (maybe config change also)
This commit is contained in:
Bill Marquette 2005-03-13 06:45:30 +00:00
parent 75616b0016
commit 71bceee778

View File

@ -52,6 +52,52 @@ if ($fd) {
fclose($fd);
}
function get_uptime() {
exec("/sbin/sysctl -n kern.boottime", $boottime);
preg_match("/sec = (\d+)/", $boottime[0], $matches);
$boottime = $matches[1];
$uptime = time() - $boottime;
if ($uptime > 60)
$uptime += 30;
$updays = (int)($uptime / 86400);
$uptime %= 86400;
$uphours = (int)($uptime / 3600);
$uptime %= 3600;
$upmins = (int)($uptime / 60);
$uptimestr = "";
if ($updays > 1)
$uptimestr .= "$updays days, ";
else if ($updays > 0)
$uptimestr .= "1 day, ";
$uptimestr .= sprintf("%02d:%02d", $uphours, $upmins);
return $uptimestr;
}
function get_cputicks() {
$cputicks = explode(" ", `/sbin/sysctl -n kern.cp_time`);
return $cputicks;
}
function get_cpuusage($cpuTicks, $cpuTicks2) {
$diff = array();
$diff['user'] = ($cpuTicks2[0] - $cpuTicks[0])+1;
$diff['nice'] = ($cpuTicks2[1] - $cpuTicks[1])+1;
$diff['sys'] = ($cpuTicks2[2] - $cpuTicks[2])+1;
$diff['intr'] = ($cpuTicks2[3] - $cpuTicks[3])+1;
$diff['idle'] = ($cpuTicks2[4] - $cpuTicks[4])+1;
//echo "<!-- user: {$diff['user']} nice {$diff['nice']} sys {$diff['sys']} intr {$diff['intr']} idle {$diff['idle']} -->";
$totalDiff = $diff['user'] + $diff['nice'] + $diff['sys'] + $diff['intr'] + $diff['idle'];
$cpuUsage = round(100 * (1 - $diff['idle'] / $totalDiff), 0);
return $cpuUsage;
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
@ -107,26 +153,7 @@ if ($fd) {
<td width="25%" class="vncellt">Uptime</td>
<td width="75%" class="listr">
<?php
exec("/sbin/sysctl -n kern.boottime", $boottime);
preg_match("/sec = (\d+)/", $boottime[0], $matches);
$boottime = $matches[1];
$uptime = time() - $boottime;
if ($uptime > 60)
$uptime += 30;
$updays = (int)($uptime / 86400);
$uptime %= 86400;
$uphours = (int)($uptime / 3600);
$uptime %= 3600;
$upmins = (int)($uptime / 60);
$uptimestr = "";
if ($updays > 1)
$uptimestr .= "$updays days, ";
else if ($updays > 0)
$uptimestr .= "1 day, ";
$uptimestr .= sprintf("%02d:%02d", $uphours, $upmins);
echo htmlspecialchars($uptimestr);
echo "<input style='border: 0px solid white;' size='30' name='uptime' id='uptime' value='" .htmlspecialchars(get_uptime()) . "'>";
?>
</td>
</tr><?php if ($config['lastchange']): ?>
@ -140,20 +167,7 @@ if ($fd) {
<td width="25%" class="vncellt">CPU usage</td>
<td width="75%" class="listr">
<?php
$cpuTicks = explode(" ", `/sbin/sysctl -n kern.cp_time`);
$cpuTicks2 = explode(" ", `/sbin/sysctl -n kern.cp_time`);
$diff = array();
$diff['user'] = ($cpuTicks2[0] - $cpuTicks[0])+1;
$diff['nice'] = ($cpuTicks2[1] - $cpuTicks[1])+1;
$diff['sys'] = ($cpuTicks2[2] - $cpuTicks[2])+1;
$diff['intr'] = ($cpuTicks2[3] - $cpuTicks[3])+1;
$diff['idle'] = ($cpuTicks2[4] - $cpuTicks[4])+1;
echo "<!-- user: {$diff['user']} nice {$diff['nice']} sys {$diff['sys']} intr {$diff['intr']} idle {$diff['idle']} -->";
$totalDiff = $diff['user'] + $diff['nice'] + $diff['sys'] + $diff['intr'] + $diff['idle'];
$cpuUsage = round(100 * (1 - $diff['idle'] / $totalDiff), 0);
$cpuUsage = get_cpuusage(get_cputicks(), get_cputicks());
echo "<img src='bar_left.gif' height='15' width='4' border='0' align='absmiddle'>";
echo "<img src='bar_blue.gif' height='15' name='cpuwidtha' id='cpuwidtha' width='" . $cpuUsage . "' border='0' align='absmiddle'>";
@ -178,8 +192,8 @@ $usedMem = $totalMem - $freeMem;
$memUsage = round(($usedMem * 100) / $totalMem, 0);
echo " <img src='bar_left.gif' height='15' width='4' border='0' align='absmiddle'>";
echo "<img src='bar_blue.gif' height='15' width='" . $memUsage . "' border='0' align='absmiddle'>";
echo "<img src='bar_gray.gif' height='15' width='" . (100 - $memUsage) . "' border='0' align='absmiddle'>";
echo "<img src='bar_blue.gif' height='15' name='memwidtha' id='memwidtha' width='" . $memUsage . "' border='0' align='absmiddle'>";
echo "<img src='bar_gray.gif' height='15' name='memwidthb' id='memwidthb' width='" . (100 - $memUsage) . "' border='0' align='absmiddle'>";
echo "<img src='bar_right.gif' height='15' width='5' border='0' align='absmiddle'> ";
echo "<input style='border: 0px solid white;' size='30' name='memusagemeter' id='memusagemeter' value='{$memUsage}%'>";
//echo $memUsage . "%";
@ -195,6 +209,7 @@ echo "<input style='border: 0px solid white;' size='30' name='memusagemeter' id=
$swapUsage = `/usr/sbin/swapinfo | cut -c45-55 | grep "%"`;
$swapUsage = ereg_replace('%', "", $swapUsage);
$swapUsage = ereg_replace(' ', "", $swapUsage);
$swapUsage = rtrim($swapUsage);
echo "<img src='bar_left.gif' height='15' width='4' border='0' align='absmiddle'>";
echo "<img src='bar_blue.gif' height='15' width='" . $swapUsage . "' border='0' align='absmiddle'>";
@ -207,17 +222,17 @@ echo "<input style='border: 0px solid white;' size='30' name='swapusagemeter' id
</td>
</tr>
<?php
/* XXX - Stub in the HW monito for net4801 - needs to use platform var's once we start using them */
/* XXX - Stub in the HW monitor for net4801 - needs to use platform var's once we start using them */
if (file_exists("/etc/48xx")) {
echo " <tr>";
echo " <td width='25%' class='vncellt'>Temperature: </td>";
echo " <td width='25%' class='vncellt'>Temperature </td>";
echo " <td width='75%' class='listr'>";
// Initialize hw monitor
exec("/usr/local/sbin/env4801 -i");
$Temp = `/usr/local/sbin/env4801 | grep Temp |cut -c24-25`;
$Temp = rtrim(`/usr/local/sbin/env4801 | grep Temp |cut -c24-25`);
echo "<img src='bar_left.gif' height='15' width='4' border='0' align='absmiddle'>";
echo "<img src='bar_blue.gif' height='15' width='" . $Temp . "' border='0' align='absmiddle'>";
echo "<img src='bar_gray.gif' height='15' width='" . (100 - $Temp) . "' border='0' align='absmiddle'>";
echo "<img src='bar_blue.gif' height='15' name='Tempwidtha' id='tempwidtha' width='" . $Temp . "' border='0' align='absmiddle'>";
echo "<img src='bar_gray.gif' height='15' name='Tempwidthb' id='tempwidthb' width='" . (100 - $Temp) . "' border='0' align='absmiddle'>";
echo "<img src='bar_right.gif' height='15' width='5' border='0' align='absmiddle'> ";
echo "<input style='border: 0px solid white;' size='30' name='Tempmeter' id='Tempmeter' value='{$Temp}C'>";
echo " </td>";
@ -236,24 +251,39 @@ $counter = 0;
While(!Connection_Aborted()) {
sleep(1);
$cpuTicks = explode(" ", `/sbin/sysctl -n kern.cp_time`);
sleep(2);
$cpuTicks2 = explode(" ", `/sbin/sysctl -n kern.cp_time`);
$diff = array();
$diff['user'] = ($cpuTicks2[0] - $cpuTicks[0])+1;
$diff['nice'] = ($cpuTicks2[1] - $cpuTicks[1])+1;
$diff['sys'] = ($cpuTicks2[2] - $cpuTicks[2])+1;
$diff['intr'] = ($cpuTicks2[3] - $cpuTicks[3])+1;
$diff['idle'] = ($cpuTicks2[4] - $cpuTicks[4])+1;
$totalDiff = $diff['user'] + $diff['nice'] + $diff['sys'] + $diff['intr'] + $diff['idle'];
$cpuUsage = round(100 * (1 - $diff['idle'] / $totalDiff), 0);
echo "<script language='javascript'>\n";
/* Update uptime */
echo "document.forms[0].uptime.value = '" . get_uptime() . "';\n";
/* Update CPU meter */
sleep(1);
$cpuTicks = get_cputicks();
sleep(2);
$cpuTicks2 = get_cputicks();
$cpuUsage = get_cpuusage($cpuTicks, $cpuTicks2);
echo "document.cpuwidtha.style.width='" . $cpuUsage . "';\n";
echo "document.cpuwidthb.style.width='" . (100 - $cpuUsage) . "';\n";
echo "document.forms[0].cpumeter.value = '" . $cpuUsage . "%';\n";
/* Update memory usage */
exec("/sbin/sysctl -n vm.stats.vm.v_active_count vm.stats.vm.v_inactive_count " .
"vm.stats.vm.v_wire_count vm.stats.vm.v_cache_count vm.stats.vm.v_free_count", $memory);
$totalMem = $memory[0] + $memory[1] + $memory[2] + $memory[3] + $memory[4];
$freeMem = $memory[4];
$usedMem = $totalMem - $freeMem;
$memUsage = round(($usedMem * 100) / $totalMem, 0);
echo "document.memwidtha.style.width='" . $memUsage . "';\n";
echo "document.memwidthb.style.width='" . (100 - $memUsage) . "';\n";
echo "document.forms[0].memmeter.value = '" . $memUsage . "%';\n";
/* Update temp. meter */
$Temp = rtrim(`/usr/local/sbin/env4801 | grep Temp |cut -c24-25`);
echo "document.Tempwidtha.style.width='" . $Temp . "';\n";
echo "document.Tempwidthb.style.width='" . (100 - $Temp) . "';\n";
echo "document.forms[0].Tempmeter.value = '" . $Temp . "C';\n";
echo "</script>\n";
/*