Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update uptime.php #12

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
175 changes: 121 additions & 54 deletions uptime.php
Original file line number Diff line number Diff line change
@@ -1,68 +1,135 @@
<?php
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;
}
// ========================================================================================================================================
// 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'] = '<div class="progress progress-striped active">
<div class="bar bar-'.$memlevel.'" style="width: '.$memory.';">'.$memory.'</div>
</div>';
// 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'] = '<div class="progress progress-striped active">
<div class="bar bar-'.$hddlevel.'" style="width: '.$hdd.';">'.$hdd.'</div>
</div>';
// load
$post['load'] = $internal['load'][$loadtime]; // posting load avg.
// load done

// Are we online?
$post['online'] = '<div class="progress"><div class="bar bar-success" style="width: 100%;"><small>Up</small></div></div>';
// 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 = '<div class="progress progress-striped active"><div class="bar bar-success" style="width: ' . $width . '%;">' . $perc . '%</div</div>';
}
elseif($perc < $wl) {
$return = '<div class="progress progress-striped active"><div class="bar bar-warning" style="width: ' . $width . '%;">' . $perc . '%</div></div>';
}
elseif($perc < $dl) {
$return = '<div class="progress progress-striped active"><div class="bar bar-danger" style="width: ' . $width . '%;">' . $perc . '%</div></div>';
}
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'] = '<div class="progress">
<div class="bar bar-success" style="width: 100%;"><small>Up</small></div>
</div>';

echo json_encode($array);
?>