diff --git a/uptime.php b/uptime.php index 6b4c5f8..0ec88ef 100644 --- a/uptime.php +++ b/uptime.php @@ -1,68 +1,135 @@ 0 ? $days . ' day'.($days > 1 ? 's' : '') : $hours.':'.$mins.':'.$seconds; -} +// ======================================================================================================================================== +// Uptime (free resources) +// ======================================================================================================================================== +// +// By Cameron Munroe ~ Mun +// Website: https://www.qwdsa.com/converse/threads/serverstatus-rebuild.43/ +// Version 0.1a +// +// +// should be cross compatiable with ServerStatus2 by @mojeda +// rebuild off the original uptime.php file, which can be found here: +// https://raw.githubusercontent.com/Munroenet/ServerStatus/master/uptime.php +// ======================================================================================================================================== -$array = array(); -$fh = fopen('/proc/uptime', 'r'); -$uptime = fgets($fh); -fclose($fh); -$uptime = explode('.', $uptime, 2); -$array['uptime'] = sec2human($uptime[0]); +// ======================================================================================================================================== +// Settings! +// ======================================================================================================================================== -$fh = fopen('/proc/meminfo', 'r'); - $mem = 0; - while ($line = fgets($fh)) { - $pieces = array(); - if (preg_match('/^MemTotal:\s+(\d+)\skB$/', $line, $pieces)) { - $memtotal = $pieces[1]; - } - if (preg_match('/^MemFree:\s+(\d+)\skB$/', $line, $pieces)) { - $memfree = $pieces[1]; - } - if (preg_match('/^Cached:\s+(\d+)\skB$/', $line, $pieces)) { - $memcache = $pieces[1]; - break; - } - } -fclose($fh); +$dl = 10; // At this percent of usage we will show it as RED! +$wl = 25; // At this percent of usage we will show it as Yellow! + +$memloc = '/proc/meminfo'; // This is where we get our info for meminfo. +// Debian / Ubuntu it is /proc/meminfo and should be the default for Linux! + +$fileloc = '/'; // This is the drive we are monitoring! +// You can change this to what ever folder you like, but root i.e. '/' is +// most important on your system! + +$uptimeloc = '/proc/uptime'; // this is where we get our info for uptime! + +$loadtime = 0; // the settings are: +// 0 for 1 minute average +// 1 for 5 minute average +// 2 for 15 minute average + +// ======================================================================================================================================== +// Getting Data! +// ======================================================================================================================================== +// You shouldn't edit anything below here, unless you know +// what you are doing! + +$post = array(); // this is the info that will be posted to the page, +// be careful what you put under this handle! + +$internal = array(); // internal array! + +$internal['uptime'] = explode('.', file_get_contents($uptimeloc), 2); +$internal['memraw'] = file_get_contents($memloc); +$internal['hddtotal'] = disk_total_space($fileloc); +$internal['hddfree'] = disk_free_space($fileloc); +$internal['load'] = sys_getloadavg(); + +// ======================================================================================================================================== +// Process The Data! +// ======================================================================================================================================== -$memmath = $memcache + $memfree; -$memmath2 = $memmath / $memtotal * 100; -$memory = round($memmath2) . '%'; +// uptime +$post['uptime'] = sec2human($internal['uptime'][0]); // Processing uptime and putting in post field! +// uptime done! -if ($memory >= "51%") { $memlevel = "success"; } -elseif ($memory <= "50%") { $memlevel = "warning"; } -elseif ($memory <= "35%") { $memlevel = "danger"; } -$array['memory'] = '
-
'.$memory.'
-
'; +// memory +preg_match_all('/MemTotal:(.*)kB/', $internal['memraw'], $internal['memtotal']); // Get Total Memory! +$internal['memtotal'] = trim($internal['memtotal'][1][0], " "); // Make nice. +preg_match_all('/MemFree:(.*)kB/', $internal['memraw'], $internal['memfree']); // Get Free Memory! +$internal['memfree'] = trim($internal['memfree'][1][0], " "); // Make nice. +preg_match_all('/Cached:(.*)kB/', $internal['memraw'], $internal['memcache']); // Get Cached Memory! +$internal['memfree'] = trim($internal['memcache'][1][0], " ") + $internal['memfree']; // Making cache seen as Free Memory! -$hddtotal = disk_total_space("/"); -$hddfree = disk_free_space("/"); -$hddmath = $hddfree / $hddtotal * 100; -$hdd = round($hddmath) . '%'; -if ($hdd >= "51%") { $hddlevel = "success"; } -elseif ($hdd <= "50%") { $hddlevel = "warning"; } -elseif ($hdd <= "35%") { $hddlevel = "danger"; } +$internal['memperc'] = floor(($internal['memfree'] / $internal['memtotal']) * 100); // calculations +$post['memory'] = levels($internal['memperc'], $dl, $wl); // adding to the post field! +// memory done! +// HDD +$internal['hddperc'] = floor(($internal['hddfree'] / $internal['hddtotal']) * 100); // calculations! +$post['hdd'] = levels($internal['hddperc'], $dl, $wl); // adding hdd to the post field! +// HDD done! -$array['hdd'] = '
-
'.$hdd.'
-
'; +// load +$post['load'] = $internal['load'][$loadtime]; // posting load avg. +// load done + +// Are we online? +$post['online'] = '
Up
'; +// YES WE ARE! + +// ======================================================================================================================================== +// Post Data +// ======================================================================================================================================== + +echo json_encode($post); // Time to show the world what we are made of! +// ======================================================================================================================================== +// Functions +// ======================================================================================================================================== + + +function levels($perc, $dl, $wl){ + // make nice green bars + if($perc < 30) { + $width = 30; + } else { + $width = $perc; + } + if($perc > $wl) { + $return = '
' . $perc . '%'; + } + elseif($perc < $wl) { + $return = '
' . $perc . '%
'; + } + elseif($perc < $dl) { + $return = '
' . $perc . '%
'; + } + return $return; + +} + + +// Sec2Human is from the original Script +function sec2human($time) { + $seconds = $time%60; + $mins = floor($time/60)%60; + $hours = floor($time/60/60)%24; + $days = floor($time/60/60/24); + return $days > 0 ? $days . ' day'.($days > 1 ? 's' : '') : $hours.':'.$mins.':'.$seconds; +} -$load = sys_getloadavg(); -$array['load'] = $load[0]; +// ======================================================================================================================================== +// Done +// ======================================================================================================================================== -$array['online'] = '
-
Up
-
'; -echo json_encode($array); +?>