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 );
}
@haincha
Copy link

haincha commented Apr 16, 2013

Any chance you could provide any assistance? Plugging in my server's info and loading the script, it doesn't load. I run server from home, so it has a local IP, then I have my domain.com, running the echo | curl from my server doesn't load anything. No errors are produced, just keeps a blank page.

@yankcrime
Copy link

@haincha Is stats.json getting created and populated OK?

@mojeska
Copy link

mojeska commented Apr 24, 2013

I'm probably having the same problem @haincha is having, however I'm guessing it's a configuration issue with apache. When I fire off the ...echo/curl... command I do not get any errors, however the stats.json file is not being generated (I assume this file is generated in the same directory as the php script. I'm sure you're busy, but if you have a minute to suggest what I should look at, I would greatly appreciate it.

[UPDATE] Looks like it was a permissions issue on the directory containing the script. Now I'm getting an error in the Status Board app on iOS. Invalid JSON "graph" must be a JSON object.

@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