Skip to content

Instantly share code, notes, and snippets.

@thomasmb
Created April 10, 2013 14:05
Show Gist options
  • Star 29 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save thomasmb/5354937 to your computer and use it in GitHub Desktop.
Save thomasmb/5354937 to your computer and use it in GitHub Desktop.
A simple script for getting and displaying server stats in StatusBoard
<?php
//add your server aliases here
$servers = array(
"185.14.184.234" => "mcfly.bensmann.no",
"185.14.184.xxx" => "another.server.com",
);
//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
//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"]
);
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-name" style="width:400px; text-transform:lowercase;"><?php echo $servers[$stats['ip']] ; ?></td>
<td class="server-disk-space"><?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 );
}
@davidlano
Copy link

I'm also struggling with this script. I can't seem to get my stats.json file populated. I don't experience any errors when I run the command from terminal to echo the server status...but when I land on the script page all I see is a blank white screen. Any suggestions? I'm super excited to get this up and running! Thank you!

@awestvik
Copy link

Not working here either, Debian squeeze. Even chmod 777 dir+files. Can only see the load.
stats.json gives me this:

{"98.22.33.11":{"df":"0%","load":"0.05","ip":"98.22.33.11"}

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