Attempting to write NUT UPS Status widget for Dashboard
-
I'm trying to write a Dashboard widget for the NUT package. I've got it to display the data I want but the data is static and won't update the changing data using that AJAX magic. Most of the data I want to see update (Status, Battery Charge, Runtime Remaining, Battery Voltage) is getting pulled using a popen call and parsed into an array from which I grab the individual data items. Can I get these data items within the widget to refresh automatically?
If someone is interested in helping me out I can post the code, most of which is just lightly modified code from the NUT package's status_nut.php file.
-
take a look on any packages widgets file to see how it updates.
here are some examples:
https://github.com/bsdperimeter/pfsense-packages/blob/master/config/pf-blocker/pfBlocker.widget.php
https://github.com/bsdperimeter/pfsense-packages/blob/master/config/postfix/postfix.widget.php
https://github.com/bsdperimeter/pfsense-packages/blob/master/config/varnish3/varnish.widget.php
-
Thanks for pointing me to some examples, Marcello. I'll make another pass at my code and see if I can get the data fields to update. If successful I might try coding some more dashboard widgets. I've been meaning to get more familiar with AJAX coding.
-
Tweek,
Were you successful in creating this? It would be an excellent addition to the dashboard.
I would definitely test it for ya. :-) -
Tweek,
Were you successful in creating this? It would be an excellent addition to the dashboard.
I would definitely test it for ya. :-)I paused work on it in December. I used the examples marcelloc provided to add some refresh code to the widget but it wouldn't refresh properly–instead of the data items in the widget getting refreshed multiple widgets would appear on the dashboard. After an hour or two of trying to figure out what was happening I put the project on hold, then I ran out of free vacation time. I didn't want to burden other developers asking for help and I am now busy working on other projects. I'd happily post the code for debugging if you feel up to the task. Otherwise I will try to pick up where I left off in a week or two.
-
Sounds good. no biggie. I'll be looking forward to it. Thanks!
-
I received an email from user "LostInIgnorance" asking me to post the code I have. I am posting the version I wrote before attempting to add the automatic AJAX widget refresh code. It is as follows:
/* $Id$ Copyright 2008 Seth Mos Part of pfSense widgets (www.pfsense.com) originally based on m0n0wall (http://m0n0.ch/wall) 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. */ $nocsrf = true; require_once("guiconfig.inc"); require_once("pfsense-utils.inc"); require_once("functions.inc"); $nut_config = $config['installedpackages']['nut']['config'][0]; /* functions */ function secs2hms($secs) { if ($secs<0) return false; $m = (int)($secs / 60); $s = $secs % 60; $h = (int)($m / 60); $m = $m % 60; return array($h, "h ", $m, "m ", $s, "s "); } function tblrowbar ($value, $symbol, $red, $yellow, $green) { if(!$value) return; $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'; } if($value >= ($red[0]+10) && $value <= $red[1]) { $color = 'white'; $bgcolor = 'red'; } if($value >= $yellow[0] && $value <= $yellow[1]) { $color = 'black'; $bgcolor = 'yellow'; } if($value >= $green[0] && $value <= ($green[0]+9)) { $color = 'black'; $bgcolor = 'green'; } if($value >= ($green[0]+10) && $value <= $green[1]) { $color = 'white'; $bgcolor = 'green'; } print(<< <eod<br>{$value}{$symbol} EOD ."\n"); } ?> | Monitoring | Model | Status | | $running = ((int)exec('pgrep upsmon | wc -l') > 0) ? true : false; if($nut_config['monitor'] == 'local') { echo "Local UPS"; $cmd = "upsc {$nut_config['name']}@localhost"; } elseif($nut_config['monitor'] == 'remote') { echo "Remote UPS"; $cmd = "upsc {$nut_config['remotename']}@{$nut_config['remoteaddr']}"; } elseif($nut_config['monitor'] == 'snmp') { echo "SNMP UPS"; $cmd = "upsc {$nut_config['snmpname']}@localhost"; } ?> | if($running) $handle = popen($cmd, 'r'); elseif($nut_config['monitor'] == 'snmp') $condition = "NUT enabled but service not running!\nSNMP UPS may be unreachable."; else $condition = "NUT enabled but service 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) $condition = "ERROR:Data stale!"; echo $ups['ups.model']; print(<< | EOD ."\n"); $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; } } echo $disp_status; print(<< | Battery Charge | Runtime Remaining | Battery Voltage | | EOD ."\n"); /* echo $ups['battery.charge'] . "%"; */ tblrowbar($ups['battery.charge'], '%', '0-29' ,'30-79', '80-100'); print(<< | EOD ."\n"); echo implode(secs2hms($ups['battery.runtime'])); print(<< | EOD ."\n"); echo $ups['battery.voltage'] . "V"; } ?> |</eod<br>
It ain't pretty but it does the job.