Advertisement
Guest User

status_nut.php

a guest
Jan 14th, 2011
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.63 KB | None | 0 0
  1. <?php
  2. /*
  3.     status_nut.php
  4.     part of pfSense (http://www.pfsense.com/)
  5.  
  6.     Copyright (C) 2007 Ryan Wagoner <rswagoner@gmail.com>.
  7.     All rights reserved.
  8.  
  9.     Redistribution and use in source and binary forms, with or without
  10.     modification, are permitted provided that the following conditions are met:
  11.  
  12.     1. Redistributions of source code must retain the above copyright notice,
  13.        this list of conditions and the following disclaimer.
  14.  
  15.     2. Redistributions in binary form must reproduce the above copyright
  16.        notice, this list of conditions and the following disclaimer in the
  17.        documentation and/or other materials provided with the distribution.
  18.  
  19.     THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
  20.     INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  21.     AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  22.     AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
  23.     OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  24.     SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  25.     INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  26.     CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  27.     ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  28.     POSSIBILITY OF SUCH DAMAGE.
  29. */
  30.  
  31. require("guiconfig.inc");
  32.  
  33. $nut_config = $config['installedpackages']['nut']['config'][0];
  34.  
  35. /* functions */
  36.  
  37. function tblopen () {
  38.     print('<table width="100%" class="tabcont" cellspacing="0" cellpadding="6">'."\n");
  39. }
  40.  
  41. function tblclose () {
  42.     print("</table>\n");
  43. }
  44.  
  45. function tblrow ($name, $value, $symbol = null) {
  46.     $display = false;
  47.     $disp_text = '';
  48.     if($value)
  49.     foreach($value as $i=>$r) {
  50.         if($symbol == '&deg;') $value = sprintf("%.1f", $value);
  51.         if($symbol == 'Hz') $value = sprintf("%d", $value);
  52.         if($r) {
  53.             $display = true;
  54.             $text = "{$r}{$symbol}";
  55.         }
  56.         $disp_text .= "<td class='vtable'>$text</td>\n";
  57.     }
  58.     if($display)
  59.         echo "<tr><td class='vncellreq' width='100px'>{$name}</td>\n$disp_text<tr>\n";
  60. }
  61.  
  62. function tblrowbar ($name, $values, $symbol, $red, $yellow, $green) {
  63.     if(!$values) return;
  64.     echo "<tr><td class='vncellreq' width='100px'>{$name}</td>\n";
  65.     foreach($values as $i=>$value) {
  66.         $value = sprintf("%.1f", $value);
  67.  
  68.         $red = explode('-', $red);
  69.         $yellow = explode('-', $yellow);
  70.         $green = explode('-', $green);
  71.  
  72.         sort($red);
  73.         sort($yellow);
  74.         sort($green);
  75.  
  76.         if($value >= $red[0] && $value <= ($red[0]+9)) {
  77.             $color = 'black';
  78. //          $bgcolor = 'red';
  79.             $bgcolor = '#FF6666';
  80.         }
  81.         if($value >= ($red[0]+10) && $value <= $red[1]) {
  82.             $color = 'black'; //was white
  83. //          $bgcolor = 'red';
  84.             $bgcolor = '#FF6666';
  85.         }
  86.         if($value >= $yellow[0] && $value <= $yellow[1]) {
  87.             $color = 'black';
  88.             $bgcolor = 'yellow';
  89.         }
  90.         if($value >= $green[0] && $value <= ($green[0]+9)) {
  91.             $color = 'black';
  92. //          $bgcolor = 'lightgreen';
  93.             $bgcolor = '#66FF66';
  94.         }  
  95.         if($value >= ($green[0]+10) && $value <= $green[1]) {
  96.             $color = 'black'; //was white
  97. //          $bgcolor = 'lightgreen';
  98.             $bgcolor = '#66FF66';
  99.         }
  100.  
  101.         echo "
  102.  <td class='vtable'>
  103.    <div style='width: 125px; height: 12px; border-top: thin solid gray; border-bottom: thin solid gray; background-color: white;'>
  104.      <div style='width: {$value}{$symbol}; height: 12px; background-color: {$bgcolor}; color: {$color}; text-align: center;'>
  105.     {$value}{$symbol}
  106.      </div>
  107.    </div>
  108.  </td>\n";
  109.     }
  110.     echo "<tr>\n";
  111. }
  112.  
  113. /* defaults to this page but if no settings are present, redirect to setup page */
  114. if(!$nut_config['monitor'])
  115.     Header("Location: /pkg.php?xml=nut.xml");
  116.  
  117. $pgtitle = "Status: NUT Status";
  118. include("head.inc");
  119. /* put your custom HTML head content here        */
  120. /* using some of the $pfSenseHead function calls */
  121. //$pfSenseHead->addMeta("<meta http-equiv=\"refresh\" content=\"120;url={$_SERVER['SCRIPT_NAME']}\" />");
  122. //echo $pfSenseHead->getHTML();
  123. if($_REQUEST['reload']) {
  124.     require_once("../pkg/nut.inc");
  125.     sync_package_nut();
  126. }
  127. ?>
  128. <body link="#0000CC" vlink="#0000CC" alink="#0000CC">
  129. <?php include("fbegin.inc"); ?>
  130. <p class="pgtitle"><?=$pgtitle?></font></p>
  131. <?php if ($savemsg) print_info_box($savemsg); ?>
  132.  
  133. <div id="mainlevel">
  134. <table width="100%" border="0" cellpadding="0" cellspacing="0">
  135. <?php
  136.     $tab_array = array();
  137.     $tab_array[] = array(gettext("NUT Status "), true, "/status_nut.php");
  138.     $tab_array[] = array(gettext("NUT Settings "), false, "/pkg.php?xml=nut.xml");
  139.     $tab_array[] = array(gettext("NUT Reload "), false, "/status_nut.php?reload=yes");
  140.     display_top_tabs($tab_array);
  141. ?>
  142. </table>
  143. <?php
  144.    
  145.  
  146. $row=0;
  147. $j=0;
  148. foreach($config['installedpackages']['nut']['config'] as $i=>$nut_config) {
  149.     $running = ((int)exec('pgrep upsmon | wc -l') > 0) ? true : false;
  150.  
  151.     if($nut_config['monitor'] == 'local') {
  152.         $upsData[$row]['monitoring'][$i] = 'Local UPS ('.$nut_config['port'].')';
  153.         $upsData[$row]['name'][$i] = $nut_config['name'];
  154.         $cmd = "upsc {$nut_config['name']}@localhost";
  155.     } elseif($nut_config['monitor'] == 'remote') {
  156.         $upsData[$row]['monitoring'][$i] = "Remote UPS ({$nut_config['remoteaddr']})";
  157.         $upsData[$row]['name'][$i] = $nut_config['remotename'];
  158.         $cmd = "upsc {$nut_config['remotename']}@{$nut_config['remoteaddr']}";
  159.     } else continue;
  160.  
  161.  
  162.     if($running)
  163.         $handle = popen($cmd, 'r');
  164.     else
  165.         $upsData[$row]['ERROR:'] = 'NUT is enabled, however the service is not running!';
  166.    
  167.     if($handle) {
  168.         $read = fread($handle, 4096);
  169.         pclose($handle);
  170.  
  171.         $lines = explode("\n", $read);
  172.         $ups = array();
  173.         foreach($lines as $line) {
  174.             $line = explode(':', $line);
  175.             $ups[$line[0]] = trim($line[1]);
  176.         }
  177.  
  178.         if(count($lines) == 1)
  179.             $ups['ERROR:'] = 'Data stale!';
  180.  
  181.         $ups['model'] = $ups['ups.model'];
  182.         $disp_status = '';
  183.         $status = explode(' ', $ups['ups.status']);
  184.         foreach($status as $condition) {
  185.             if($disp_status) $disp_status .= ', ';
  186.             switch ($condition) {
  187.                 case 'WAIT':
  188.                     $disp_status .= 'Waiting';
  189.                     break;
  190.                 case 'OFF':
  191.                     $disp_status .= 'Off Line';
  192.                     break;
  193.                 case 'OL':
  194.                     $disp_status .= 'On Line';
  195.                     break;
  196.                 case 'OB':
  197.                     $disp_status .= 'On Battery';
  198.                     break;
  199.                 case 'TRIM':
  200.                     $disp_status .= 'SmartTrim';
  201.                     break;
  202.                 case 'BOOST':
  203.                     $disp_status .= 'SmartBoost';
  204.                     break;
  205.                 case 'OVER':
  206.                     $disp_status .= 'Overload';
  207.                     break;
  208.                 case 'LB':
  209.                     $disp_status .= 'Battery Low';
  210.                     break;
  211.                 case 'RB':
  212.                     $disp_status .= 'Replace Battery';
  213.                     break;
  214.                 case 'CAL':
  215.                     $disp_status .= 'Calibration';
  216.                     break;
  217.                 default:
  218.                     $disp_status .= $condition;
  219.                     break;
  220.             }
  221.         }
  222.         $upsData[$row]['disabled'][$i] = ($nut_config['disabled'])?'disabled':'enabled';
  223.  
  224.  
  225.         $upsData[$row]['status'][$i] = $disp_status;
  226.         if($upsData[$row]['disabled'][$i]!='disabled') {
  227.             $upsData[$row]['load'][$i] = $ups['ups.load'];
  228.             $upsData[$row]['batteryCharge'][$i] = $ups['battery.charge'];
  229.             $upsData[$row]['runtimeRemaining'][$i] = $ups['battery.runtime'];
  230.             $upsData[$row]['batteryVoltage'][$i] = $ups['battery.voltage'];
  231.             $upsData[$row]['inputVoltage'][$i] = $ups['input.voltage'];
  232.             $upsData[$row]['inputFrequency'][$i] = $ups['input.frequency'];
  233.             $upsData[$row]['outputVoltage'][$i] = $ups['output.voltage'];
  234.             $upsData[$row]['serial'][$i] = $ups['ups.serial'];
  235.             $upsData[$row]['temperature'][$i] = $ups['ups.temperature'];
  236.         }
  237.         else $upsData[$row]['status'][$i]='Disabled';
  238.         $upsData[$row]['model'][$i] = $ups['model'];
  239.  
  240.     }
  241.     if($j++%4==3) $row++;
  242.     unset($ups);
  243. } ?>
  244.  
  245. <table width="100%" border="0" cellpadding="0" cellspacing="0">
  246. <?foreach($upsData as $row=>$data){?>
  247.     <tr>
  248.         <td>
  249.             <? tblopen();
  250.             tblrow('Name:', $data['name']);
  251.             tblrow('Monitoring:', $data['monitoring']);
  252.             tblrow('Model:', $data['model']);
  253.             tblrow('Status:', $data['status']);
  254.             tblrowbar('Load:', $data['load'], '%', '100-80', '79-60', '59-0');
  255.             tblrowbar('Battery Charge:', $data['batteryCharge'], '%', '0-29' ,'30-79', '80-100');
  256.             tblrow('Runtime Remaining:', $data['runtimeRemaining'], ' seconds');
  257.             tblrow('Battery Voltage:', $data['batteryVoltage'], 'V');
  258.             tblrow('Input Voltage:', $data['inputVoltage'], 'V');
  259.             tblrow('Input Frequency:', $data['inputFrequency'], 'Hz');
  260.             tblrow('Output Voltage:', $data['outputVoltage'], 'V');
  261.             tblrow('Serial:', $data['serial'], 'V');
  262.             tblrow('Temperature:', $data['temperature'], '&deg;');
  263.             tblclose(); ?>
  264.         </td>
  265.     </tr>
  266. <?}?>
  267. </table>
  268.  
  269. <?php
  270. //  print('<pre>'); print_r($config['installedpackages']['nut']); print('</pre>');
  271. //  print('<pre>'); print_r($upsData[$row]); print('</pre>');
  272. ?>
  273. </div>
  274. <?php include("fend.inc"); ?>
  275. </body>
  276. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement