Skip to content

Instantly share code, notes, and snippets.

@mattsmallman
Forked from thomasmb/statusboard-serverstats.php
Last active December 16, 2015 05:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattsmallman/5383260 to your computer and use it in GitHub Desktop.
Save mattsmallman/5383260 to your computer and use it in GitHub Desktop.
<?php
//add your server aliases here
$servers = array(
"185.14.184.234" => "mcfly.bensmann.no",
"185.14.184.xxx" => "another.server.com",
);
//change this line to compensate for servers updating less frequenct (65 chosen for servers running curl every 1 min)
$timeinsecondstoalert = 65
//this script is triggered by this command from the terminal or cron:
//echo "time=`uptime`&df=`df -h`" | curl -s -d @- http://domain.com/path/to/script.php
//update
//record data
if(isset( $_POST['time'], $_POST['df'] )){
//getting the server load
preg_match('/\d+\.\d{2}/', $_POST['time'],$load);
//get available disk space
preg_match('/\d+%/', $_POST['df'],$df);
if(!count($load) || !count($df))
return false;
$stats = array(
"df" => $df[0],
"load" => $load[0],
"ip" => $_SERVER["REMOTE_ADDR"],
"time" => (string) time()
);
save_to_stats($stats);
}else{
output_stats_table();
}
function save_to_stats($stats){
$data = json_decode( file_get_contents("stats.json"), true );
$data[ $stats['ip'] ] = $stats;
file_put_contents("stats.json", json_encode($data), LOCK_EX);
}
function output_stats_table(){
global $servers;
//display data
$data = json_decode( file_get_contents("stats.json"), true );
?>
<table id="projects">
<?php foreach($data as $server => $stats): ?>
<tr>
<td class="server-status" style="width:48px;" ><?php if (time() - (int) $stats['time'] > $timeinsecondstoalert )
{
echo '<img src="Red_Light_Icon.png">'; ;
}
else
{
echo '<img src="Green_Light_Icon.png">';
} ?></td>
<td class="server-name" style="width:350px; text-transform:lowercase;"><?php echo $servers[$stats['ip']] ; ?></td>
<td class="server-load" style="width:72px;"><?php echo $stats['load']; ?></td>
<td class="projectsBars">
<?php for( $i = 1, $j = number_of_bars($stats['df']); $i <= $j; $i++ ): ?>
<div class="barSegment value<?php echo $i; ?>"></div>
<?php endfor; ?>
</td>
</tr>
<?php endforeach; ?>
</table>
<?php
};
function number_of_bars($df){
$value = (int) str_replace('%', '', $df) / 10;
return round( ($value > 8 ? 8 : $value) * .8 );
}
@mattsmallman
Copy link
Author

Update original to include time logging of each update and check to see they have happened as well as red and green light icons to indicate status. Some amendments to table layout.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment