Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?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

This comment has been minimized.

Copy link
Owner Author

@mattsmallman mattsmallman commented Apr 14, 2013

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
You can’t perform that action at this time.