From f5bd5d9dd4c8829bfb865b05a111f0ba533c9d8c Mon Sep 17 00:00:00 2001 From: Ryan Wagoner Date: Thu, 15 Feb 2007 19:53:17 +0000 Subject: -move network ups tools into its own directory -added status page to display ups status --- packages/nut/nut.inc | 354 ++++++++++++++++++++++++++++++++ packages/nut/nut.xml | 479 ++++++++++++++++++++++++++++++++++++++++++++ packages/nut/status_nut.php | 94 +++++++++ 3 files changed, 927 insertions(+) create mode 100644 packages/nut/nut.inc create mode 100644 packages/nut/nut.xml create mode 100644 packages/nut/status_nut.php (limited to 'packages') diff --git a/packages/nut/nut.inc b/packages/nut/nut.inc new file mode 100644 index 00000000..defc8807 --- /dev/null +++ b/packages/nut/nut.inc @@ -0,0 +1,354 @@ +. + 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_once("config.inc"); + require_once("functions.inc"); + + /* Nut */ + + define('NUT_RCFILE', '/usr/local/etc/rc.d/nut.sh'); + define('NUT_DIR','/usr/local/etc/nut'); + + function nut_notice ($msg) { syslog(LOG_NOTICE, "nut: {$msg}"); return; } + function nut_warn ($msg) { syslog(LOG_WARNING, "nut: {$msg}"); return; } + + function nut_action ($action) { + if (file_exists(NUT_RCFILE)) + mwexec(NUT_RCFILE.' '.$action); + } + + function nut_config ($name) { + global $config; + if($config['installedpackages']['nut']['config'][0]["{$name}"]) + return $config['installedpackages']['nut']['config'][0]["{$name}"]; + return null; + } + + function nut_config_sub ($name,$len) { + global $config; + if(nut_config($name)) + return substr(nut_config($name),0,strlen(nut_config($name))-$len); + return null; + } + + function nut_write_config($file, $text, $mask = null, $uid = null) { + $conf = fopen($file, "w"); + if(!$conf) { + nut_warn("Could not open {$file} for writing."); + exit; + } + fwrite($conf, $text); + fclose($conf); + if($mask) chmod($file, $mask); + if($uid) chown($file, $uid); + } + + function nut_validate_ip($ip) { + if(!eregi("^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$", $ip)) + return false; + foreach(explode('.', $ip) as $sub) + if($sub < 0 || $sub > 256) + return false; + return true; + } + + function before_form_nut($pkg) { + /* return available serial ports */ + $handle = popen('dmesg | grep \'^sio[0-9]: type\'','r'); + $read = fread($handle, 2096); + pclose($handle); + + /* explode at the newlines */ + $read = explode("\n",$read); + + /* parse resulting text */ + foreach($read as $line) { + if($line!= '') { + $names[] = 'ttyd'.$line{3}.' (COM'.($line{3}+1).')'; + $values[] = '/dev/ttyd'.$line{3}; + } + } + + /* find correct field */ + $i = 0; + foreach ($pkg['fields']['field'] as $field) { + if ($field['fieldname'] == 'port') + break; + $i++; + } + $field = &$pkg['fields']['field'][$i]; + + /* add available serial ports */ + for ($i = 0; $i < count($values) ; $i++) + $field['options']['option'][] = array('name' => $names[$i], 'value' => $values[$i]); + } + + function validate_form_nut($post, $input_errors) { + global $config; + + /* monitor remote validation */ + if($post['monitor'] == 'remote') { + if(!$post['remotename']) + $input_errors[] = 'You must specify a name in the \'Remote Name\' field'; + if(!$post['remoteaddr'] || !nut_validate_ip($post['remoteaddr'])) + $input_errors[] = 'You must specify a valid address \'Remote Address\' field'; + if(!$post['remoteuser']) + $input_errors[] = 'You must specify a name in the \'Remote User\' field'; + if(!$post['remotepass']) + $input_errors[] = 'You must specify a name in the \'Remote Password\' field'; + } + + /* monitor local validation */ + elseif($post['monitor'] == 'local') { + if(!$post['name']) + $input_errors[] = 'You must specify a name in the \'Name\' field'; + if(!$post['driver']) + $input_errors[] = 'You must select a driver in the \'Driver\' field'; + if(!$post['port']) + $input_errors[] = 'You must select a port in the \'Port\' field'; + } + + /* this is run before the config file changed */ + /* so we can unassign the existing port */ + if(nut_config('monitor') == 'local' && count($input_errors) == 0) { + $port = nut_config('port'); + $driver = nut_config_sub('driver', 2); + + if($port && $driver) { + nut_action('stop'); + + if($port != 'auto') + chown($port, 'root'); + } + } + } + + function deinstall_package_nut() { + nut_action('stop'); + + @unlink(NUT_RCFILE); + @unlink(NUT_DIR.'/upsmon.conf'); + @unlink(NUT_DIR.'/ups.conf'); + @unlink(NUT_DIR.'/upsd.conf'); + @unlink(NUT_DIR.'/upsd.users'); + + exec('rm -rf /var/db/nut'); + + /*exec("pkg_delete nut-usb-2.0.5"); + exec("pkg_delete nut-2.0.5");*/ + } + + function sync_package_nut_remote() { + $remotename = nut_config('remotename'); + $remoteaddr = nut_config('remoteaddr'); + $remoteuser = nut_config('remoteuser'); + $remotepass = nut_config('remotepass'); + + if(!($remotename && $remoteaddr && $remoteuser && $remotepass)) + return false; + + /* upsmon.conf */ + $upsmon_conf = << 'nut.sh', + 'start' => $start, + 'stop' => $stop + ) + ); + conf_mount_ro(); + + return true; + } + + function sync_package_nut_local() { + $name = nut_config('name'); + $driver = nut_config_sub('driver', 2); + $port = nut_config('port'); + $upstype = nut_config_sub('upstype', 3); + $cable = nut_config_sub('cable', 3); + $allowaddr = nut_config('allowaddr'); + $allowuser = nut_config('allowuser'); + $allowpass = nut_config('allowpass'); + + if(!($name && $driver && $port)) + return false; + + if($port != 'auto') + chown($port, 'uucp'); + + /* ups.conf */ + if($port == 'auto') + $ups_conf = "user=root\n"; + $ups_conf .= "[{$name}]\n"; + $ups_conf .= "driver={$driver}\n"; + $ups_conf .= "port={$port}\n"; + if($cable) + $ups_conf .= "cable={$cable}\n"; + if($upstype) + $ups_conf .= "upstype={$upstype}\n"; + + /* upsd.conf */ + $upsd_conf = "ACL all 0.0.0.0/0\n"; + $upsd_conf .= "ACL localhost 127.0.0.1/32\n"; + if($allowaddr && $allowuser) { + $upsd_conf .= "ACL remote {$allowaddr}\n"; + $upsd_conf .= "ACCEPT remote\n"; + } + $upsd_conf .= "ACCEPT localhost\n"; + $upsd_conf .= "REJECT all\n"; + + /* upsd.users */ + $upsd_users = "[monuser]\n"; + $upsd_users .= "password = mypass\n"; + $upsd_users .= "allowfrom = localhost\n"; + $upsd_users .= "upsmon master\n"; + if($allowaddr && $allowuser) { + $upsd_users .= "\n[$allowuser]\n"; + $upsd_users .= "password = $allowpass\n"; + $upsd_users .= "allowfrom = remote\n"; + $upsd_users .= "upsmon master\n"; + } + + /* upsmon.conf */ + $upsmon_conf = << 'nut.sh', + 'start' => $start, + 'stop' => $stop + ) + ); + conf_mount_ro(); + + return true; + } + + function sync_package_nut() { + global $config; + global $input_errors; + + config_lock(); + + nut_action('stop'); + + /* create state path, might already exist */ + @mkdir('/var/db/nut'); + chmod('/var/db/nut', 0700); + chown('/var/db/nut', 'uucp'); + + if(nut_config('monitor') == 'remote') + $return = sync_package_nut_remote(); + elseif(nut_config('monitor') == 'local') + $return = sync_package_nut_local(); + + if($return && $_POST['monitor']) { + /* only start if changing settings as we have a startup script for system boot */ + /* this prevents service from starting / stopping / starting on boot */ + + nut_notice('Starting service'); + nut_action('start'); + if((int)exec('pgrep upsmon | wc -l') == 0) + nut_notice('Service failed to start: check configuration'); + + } elseif(!$return && file_exists(NUT_RCFILE)) { + /* no parameters user does not want nut running */ + /* lets stop the service and remove the rc file */ + + if(!nut_config('monitor')) + nut_notice('Service stopped: nut disabled'); + else + nut_notice('Service stopped: check configuration'); + + conf_mount_rw(); + unlink(NUT_RCFILE); + unlink(NUT_DIR.'/upsmon.conf'); + /* might not always exist */ + @unlink(NUT_DIR.'/ups.conf'); + @unlink(NUT_DIR.'/upsd.conf'); + @unlink(NUT_DIR.'/upsd.users'); + exec('rm -rf /var/db/nut'); + conf_mount_ro(); + } + + config_unlock(); + } +?> diff --git a/packages/nut/nut.xml b/packages/nut/nut.xml new file mode 100644 index 00000000..92d5ec09 --- /dev/null +++ b/packages/nut/nut.xml @@ -0,0 +1,479 @@ + + + Services: NUT + nut + 2.0.5 + Change + /usr/local/pkg/nut.inc + /status_nut.php + + NUT + Set Network UPS Tools settings. +
Services
+ /status_nut.php +
+ + http://www.pfsense.com/packages/config/nut/nut.inc + /usr/local/pkg/ + 0755 + + + http://www.pfsense.com/packages/config/nut/status_nut.php + /usr/local/www/ + 0755 + + + nut + nut.sh + upsmon + + + + NUT Status + /status_nut.php + + + NUT Settings + /pkg_edit.php?xml=nut.xml&id=0 + + + + + + UPS Monitoring + monitor + select + + + + + + + + + Local UPS Name + name + input + + + Local UPS Model + driver + select + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Local UPS Port + port + select + + + + + + Local UPS Generic Type (optional) + upstype + select + + + + + + + + + + + + + Local UPS Cable Type (optional) + cable + select + + + + + + + + + + + + + + + + + + Local Remote Access Address <br>(ex: 192.168.1.0/24) (optional) + allowaddr + input + + + Local Remote Access User (optional) + allowuser + input + + + Local Remote Acces Password (optional) + allowpass + password + + + Remote UPS Name + remotename + input + + + Remote UPS Address <br>(ex: 192.168.1.10) + remoteaddr + input + + + Remote UPS User + remoteuser + input + + + Remote UPS Password + remotepass + password + + + + before_form_nut(&$pkg); + + + validate_form_nut($_POST, &$input_errors); + + + sync_package_nut(); + + + deinstall_package_nut(); + +
diff --git a/packages/nut/status_nut.php b/packages/nut/status_nut.php new file mode 100644 index 00000000..2a7c800d --- /dev/null +++ b/packages/nut/status_nut.php @@ -0,0 +1,94 @@ +. + 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]; + +/* Defaults to this page but if no settings are present, redirect to setup page */ +if(!$nut_config['monitor']) + Header("Location: /pkg_edit.php?xml=nut.xml&id=0"); + +$pgtitle = "Status: NUT Status"; +include("head.inc"); +/* put your custom HTML head content here */ +/* using some of the $pfSenseHead function calls */ +//$pfSenseHead->addMeta(""); +//echo $pfSenseHead->getHTML(); + +?> + + +

+ + +
+ + +
+ + + + +
+"); + print("Battery Charge: {$ups['battery.charge']}
"); + print("Battery Voltage: {$ups['battery.voltage']}
"); + print("Input Voltage: {$ups['input.voltage']}
"); + print("Output Voltage: {$ups['output.voltage']}
"); + + /*print('
'); print_r($ups); print('
');*/ + } +?> +
+
+ + + -- cgit v1.2.3