Advertisement
vady

APC PowerChute Personal Edition EventLog File Parser

Mar 27th, 2013
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.70 KB | None | 0 0
  1. <?php
  2. /* ----------------------------------------------------
  3. PowerChute Personal Edition 3.0.2 EventLog File Parser
  4. ©2013 by (v) Vadym Gulyi | http://vady.kiev.ua/
  5. -- credits:
  6.    http://johnwbartlett.com/PowerChute.html
  7.    http://www.seltron.net/apc/powerchute.php
  8.    http://vady.kiev.ua/tweets/1106
  9. -- license:
  10.    GPL v2 - http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  11. ---------------------------------------------------- */
  12. $events = array(
  13.     '172' => "Battery backup transferred to battery power due to overvoltage",
  14.     '173' => "Battery backup transferred to battery power due to undervoltage",
  15.     '174' => "Battery backup transferred to battery power due to blackout",
  16.     '177' => "PowerChute causing PC to hibernate",
  17.     '61451' => "Self-test Failed",
  18.     '61452' => "Self-test Passed",
  19.     '61453' => "Battery backup transferred to battery power due to electrical noise",
  20.     '61455' => "Battery backup transferred to AC utility power",
  21.     '61456' => "PowerChute not communicating with battery backup",
  22.     '61459' => "Battery connected in the battery backup",
  23.     '61460' => "UPS has been disconnected",
  24.     '61465' => "PowerChute recommenced communicating with the battery backup",
  25.     '61483' => "PowerChute not communicating with the battery backup while on battery power",
  26.     '61501' => "PowerChute regained communication with the battery backup while on battery power"
  27. );
  28. $onbattery = array(
  29.     '172' => true,
  30.     '173' => true,
  31.     '174' => true,
  32.     '177' => false,
  33.     '61453' => true,
  34.     '61455' => false,
  35.     '61456' => false,
  36. //  '61459' => "Battery connected in the battery backup",
  37.     '61460' => false,
  38.     '61465' => false,
  39.     '61483' => true,
  40.     '61501' => true
  41. );
  42. function getmicrotime() {
  43.     list($usec, $sec) = explode(" ",microtime());
  44.     return ((float)$usec + (float)$sec);
  45. }
  46. $file = (!empty($_FILES['eventlog'])) ? $_FILES['eventlog']['tmp_name'] : 'C:\Program Files\PowerChute Personal Edition\eventlog.dat';
  47. $html = '<html><head><title>PowerChute Personal Edition EventLog File Parser</title></head><body>';
  48. if(file_exists($file)) {
  49.     $timer = getmicrotime();
  50.     setlocale(LC_ALL, 'ru_RU.CP1251', 'rus_RUS.CP1251', 'Russian_Russia.1251');
  51.     $html .= '<table border="1" cellpadding="5" cellspacing="0"><tr><th scope="col">#</th><th scope="col">Date</th><th scope="col">&nbsp;</th><th scope="col">&nbsp;</th><th scope="col">On Battery?</th><th scope="col">Event</th></tr>';
  52.     $nn = 1;
  53.     foreach(array_chunk(array_slice(unpack("v*", "\0\0".file_get_contents($file)), 1),10) as $data) {
  54.         $date = mktime($data[4], $data[5], $data[6], $data[1], $data[3], $data[0]);
  55.         $timestamp = sprintf('<span title="%s">%s</span>', strftime("%A, %d %B %Y", $date)." @ ".date("H:i:s", $date), date("Y-m-d H:i:s", $date));
  56.         $event = (isset($events[$data[8]])) ? $events[$data[8]] : 'Event #'.$data[8];
  57.         $event = sprintf('<span title="EventID #%d">%s</span>', $data[8], $event);
  58.         $unknown = sprintf('<span title="Duration: %s">%s</span>', date('i:s',$data[7]), $data[7]);
  59.         $accuse = array_key_exists($data[8], $onbattery) ? ($onbattery[$data[8]]===true) ? 'Yes' : 'No' : 'N/A';
  60.         $html .= sprintf('<tr valign="top"><td align="right">%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>', $nn++, $timestamp, $data[2], $unknown, $accuse, $event);
  61.     }
  62.     $html .= '</table>';
  63.     echo $html;
  64.     if($nn>1) printf("<p>%d total records parsed in %s seconds!</p>", $nn-1, (number_format(getmicrotime()-$timer,2)));
  65.     else echo '<p>No data found in eventlog.dat.</p>';
  66. } else {
  67.     echo $html.'<form enctype="multipart/form-data" method="post" action="'.$_SERVER['PHP_SELF'].'"><label for="eventlog">Provide <strong>eventlog.dat</strong> file:</label><input type="file" name="eventlog" id="eventlog" /><input type="submit" value="Parse" /></form>';
  68. }
  69. echo '</body></html>';
  70. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement