Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- status_nut.php
- part of pfSense (http://www.pfsense.com/)
- Copyright (C) 2007 Ryan Wagoner <rswagoner@gmail.com>.
- All rights reserved.
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions are met:
- 1. Redistributions of source code must retain the above copyright notice,
- this list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above copyright
- notice, this list of conditions and the following disclaimer in the
- documentation and/or other materials provided with the distribution.
- THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
- INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
- AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
- AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
- OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
- SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
- INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
- CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
- POSSIBILITY OF SUCH DAMAGE.
- */
- require("guiconfig.inc");
- $nut_config = $config['installedpackages']['nut']['config'][0];
- /* functions */
- function tblopen () {
- print('<table width="100%" class="tabcont" cellspacing="0" cellpadding="6">'."\n");
- }
- function tblclose () {
- print("</table>\n");
- }
- function tblrow ($name, $value, $symbol = null) {
- $display = false;
- $disp_text = '';
- if($value)
- foreach($value as $i=>$r) {
- if($symbol == '°') $value = sprintf("%.1f", $value);
- if($symbol == 'Hz') $value = sprintf("%d", $value);
- if($r) {
- $display = true;
- $text = "{$r}{$symbol}";
- }
- $disp_text .= "<td class='vtable'>$text</td>\n";
- }
- if($display)
- echo "<tr><td class='vncellreq' width='100px'>{$name}</td>\n$disp_text<tr>\n";
- }
- function tblrowbar ($name, $values, $symbol, $red, $yellow, $green) {
- if(!$values) return;
- echo "<tr><td class='vncellreq' width='100px'>{$name}</td>\n";
- foreach($values as $i=>$value) {
- $value = sprintf("%.1f", $value);
- $red = explode('-', $red);
- $yellow = explode('-', $yellow);
- $green = explode('-', $green);
- sort($red);
- sort($yellow);
- sort($green);
- if($value >= $red[0] && $value <= ($red[0]+9)) {
- $color = 'black';
- // $bgcolor = 'red';
- $bgcolor = '#FF6666';
- }
- if($value >= ($red[0]+10) && $value <= $red[1]) {
- $color = 'black'; //was white
- // $bgcolor = 'red';
- $bgcolor = '#FF6666';
- }
- if($value >= $yellow[0] && $value <= $yellow[1]) {
- $color = 'black';
- $bgcolor = 'yellow';
- }
- if($value >= $green[0] && $value <= ($green[0]+9)) {
- $color = 'black';
- // $bgcolor = 'lightgreen';
- $bgcolor = '#66FF66';
- }
- if($value >= ($green[0]+10) && $value <= $green[1]) {
- $color = 'black'; //was white
- // $bgcolor = 'lightgreen';
- $bgcolor = '#66FF66';
- }
- echo "
- <td class='vtable'>
- <div style='width: 125px; height: 12px; border-top: thin solid gray; border-bottom: thin solid gray; background-color: white;'>
- <div style='width: {$value}{$symbol}; height: 12px; background-color: {$bgcolor}; color: {$color}; text-align: center;'>
- {$value}{$symbol}
- </div>
- </div>
- </td>\n";
- }
- echo "<tr>\n";
- }
- /* defaults to this page but if no settings are present, redirect to setup page */
- if(!$nut_config['monitor'])
- Header("Location: /pkg.php?xml=nut.xml");
- $pgtitle = "Status: NUT Status";
- include("head.inc");
- /* put your custom HTML head content here */
- /* using some of the $pfSenseHead function calls */
- //$pfSenseHead->addMeta("<meta http-equiv=\"refresh\" content=\"120;url={$_SERVER['SCRIPT_NAME']}\" />");
- //echo $pfSenseHead->getHTML();
- if($_REQUEST['reload']) {
- require_once("../pkg/nut.inc");
- sync_package_nut();
- }
- ?>
- <body link="#0000CC" vlink="#0000CC" alink="#0000CC">
- <?php include("fbegin.inc"); ?>
- <p class="pgtitle"><?=$pgtitle?></font></p>
- <?php if ($savemsg) print_info_box($savemsg); ?>
- <div id="mainlevel">
- <table width="100%" border="0" cellpadding="0" cellspacing="0">
- <?php
- $tab_array = array();
- $tab_array[] = array(gettext("NUT Status "), true, "/status_nut.php");
- $tab_array[] = array(gettext("NUT Settings "), false, "/pkg.php?xml=nut.xml");
- $tab_array[] = array(gettext("NUT Reload "), false, "/status_nut.php?reload=yes");
- display_top_tabs($tab_array);
- ?>
- </table>
- <?php
- $row=0;
- $j=0;
- foreach($config['installedpackages']['nut']['config'] as $i=>$nut_config) {
- $running = ((int)exec('pgrep upsmon | wc -l') > 0) ? true : false;
- if($nut_config['monitor'] == 'local') {
- $upsData[$row]['monitoring'][$i] = 'Local UPS ('.$nut_config['port'].')';
- $upsData[$row]['name'][$i] = $nut_config['name'];
- $cmd = "upsc {$nut_config['name']}@localhost";
- } elseif($nut_config['monitor'] == 'remote') {
- $upsData[$row]['monitoring'][$i] = "Remote UPS ({$nut_config['remoteaddr']})";
- $upsData[$row]['name'][$i] = $nut_config['remotename'];
- $cmd = "upsc {$nut_config['remotename']}@{$nut_config['remoteaddr']}";
- } else continue;
- if($running)
- $handle = popen($cmd, 'r');
- else
- $upsData[$row]['ERROR:'] = 'NUT is enabled, however the service is not running!';
- if($handle) {
- $read = fread($handle, 4096);
- pclose($handle);
- $lines = explode("\n", $read);
- $ups = array();
- foreach($lines as $line) {
- $line = explode(':', $line);
- $ups[$line[0]] = trim($line[1]);
- }
- if(count($lines) == 1)
- $ups['ERROR:'] = 'Data stale!';
- $ups['model'] = $ups['ups.model'];
- $disp_status = '';
- $status = explode(' ', $ups['ups.status']);
- foreach($status as $condition) {
- if($disp_status) $disp_status .= ', ';
- switch ($condition) {
- case 'WAIT':
- $disp_status .= 'Waiting';
- break;
- case 'OFF':
- $disp_status .= 'Off Line';
- break;
- case 'OL':
- $disp_status .= 'On Line';
- break;
- case 'OB':
- $disp_status .= 'On Battery';
- break;
- case 'TRIM':
- $disp_status .= 'SmartTrim';
- break;
- case 'BOOST':
- $disp_status .= 'SmartBoost';
- break;
- case 'OVER':
- $disp_status .= 'Overload';
- break;
- case 'LB':
- $disp_status .= 'Battery Low';
- break;
- case 'RB':
- $disp_status .= 'Replace Battery';
- break;
- case 'CAL':
- $disp_status .= 'Calibration';
- break;
- default:
- $disp_status .= $condition;
- break;
- }
- }
- $upsData[$row]['disabled'][$i] = ($nut_config['disabled'])?'disabled':'enabled';
- $upsData[$row]['status'][$i] = $disp_status;
- if($upsData[$row]['disabled'][$i]!='disabled') {
- $upsData[$row]['load'][$i] = $ups['ups.load'];
- $upsData[$row]['batteryCharge'][$i] = $ups['battery.charge'];
- $upsData[$row]['runtimeRemaining'][$i] = $ups['battery.runtime'];
- $upsData[$row]['batteryVoltage'][$i] = $ups['battery.voltage'];
- $upsData[$row]['inputVoltage'][$i] = $ups['input.voltage'];
- $upsData[$row]['inputFrequency'][$i] = $ups['input.frequency'];
- $upsData[$row]['outputVoltage'][$i] = $ups['output.voltage'];
- $upsData[$row]['serial'][$i] = $ups['ups.serial'];
- $upsData[$row]['temperature'][$i] = $ups['ups.temperature'];
- }
- else $upsData[$row]['status'][$i]='Disabled';
- $upsData[$row]['model'][$i] = $ups['model'];
- }
- if($j++%4==3) $row++;
- unset($ups);
- } ?>
- <table width="100%" border="0" cellpadding="0" cellspacing="0">
- <?foreach($upsData as $row=>$data){?>
- <tr>
- <td>
- <? tblopen();
- tblrow('Name:', $data['name']);
- tblrow('Monitoring:', $data['monitoring']);
- tblrow('Model:', $data['model']);
- tblrow('Status:', $data['status']);
- tblrowbar('Load:', $data['load'], '%', '100-80', '79-60', '59-0');
- tblrowbar('Battery Charge:', $data['batteryCharge'], '%', '0-29' ,'30-79', '80-100');
- tblrow('Runtime Remaining:', $data['runtimeRemaining'], ' seconds');
- tblrow('Battery Voltage:', $data['batteryVoltage'], 'V');
- tblrow('Input Voltage:', $data['inputVoltage'], 'V');
- tblrow('Input Frequency:', $data['inputFrequency'], 'Hz');
- tblrow('Output Voltage:', $data['outputVoltage'], 'V');
- tblrow('Serial:', $data['serial'], 'V');
- tblrow('Temperature:', $data['temperature'], '°');
- tblclose(); ?>
- </td>
- </tr>
- <?}?>
- </table>
- <?php
- // print('<pre>'); print_r($config['installedpackages']['nut']); print('</pre>');
- // print('<pre>'); print_r($upsData[$row]); print('</pre>');
- ?>
- </div>
- <?php include("fend.inc"); ?>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement