A bit of PHP/awk magic

Since WE Energies drops the mains power so often around here, I’ve got everything connected to UPSes; the server in particular.  This morning, the power dropped at 0930 and was out until 1047.  My UPS has ~62 minutes of power, leaving a 10 minute gap.  I decided that I wanted to make a power outage as painless as possible for my users, so I built a Drupal block that I put into my sites that checks to see if the system is on mains power.  If it isn’t, it shows a warning in red letting the visitor know that the server is on battery and may go down at any time.

<?php 
 $batt_time = shell_exec('/usr/sbin/apcaccess | \
     awk -F":" \'/TIMELEFT/ { print $2 }\'');
?>
<h1 style="color: #FF0000;">WARNING:</h1>
<p style="bold; color: #FF0000;">The server that hosts this website is
running on emergency battery power and could shut down at any time.  The 
UPS has approximately <?=$batt_time?> remaining.</p>

This bit of code tells Drupal whether to display the block or not. If it returns true, then display. If false, then don’t:

<?php
$batt_status = trim(shell_exec('/usr/sbin/apcaccess | \
     awk -F":" \'/STATUS/ { print $2 }\''));
return $batt_status != "ONLINE";
?>
Advertisements
  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: