UPS Sandbox

From SME Server
Revision as of 23:52, 22 February 2015 by Phil101 (talk | contribs)
Jump to navigation Jump to search


Is this article helpful to you?
Please consider donating or volunteering
Thank you!

Uninterruptable Power Supply

Introduction

The primary goal of the Network UPS Tools (NUT) project is to provide reliable monitoring of UPS hardware and ensure safe shutdowns of the systems which are connected.

The default configuration of NUT, will keep your connected systems operational until a critical battery state is reached (ie battery is nearing exhaustion) and then power down your server/equipment in a controlled fashion. See http://www.networkupstools.org/

If you have an APC UPS, see also Uninterruptable_Power_Supply:APC for an alternative to the standard SME Nut implementation

If you have Dell UPS, this might Help Uninterruptable_Power_Supply:LatestGeekery

Default Configuration (USB)

The default configuration in SME Server for 'NUT' is set by the configuration database properties

Model  = usbhid-ups
status = disabled
type   = service
Important.png Note:
Absence of a 'Master' property setting defaults the configuration to being a Master setup. That is a UPS connected directly to the server via USB or serial cable is assumed. See later for setting as a Slave.

The default is for NUT to be disabled, that is no UPS connected


Most USB connected UPS's will work with these default settings. If using a USB connection just enable NUT as follows:

config setprop nut status enabled
signal-event post-upgrade
signal-event reboot

If your USB UPS does not work properly OR you have a serial device then follow the Configuration Options below as required.

Configuration Options

Not all UPS's are supported by USB or the usbhid-ups driver. However NUT supports many UPS's and can be configured under SME Server easily.

Serial Connection

  1. Find the configuration details for your model of UPS. Refer to: http://www.networkupstools.org/stable-hcl.html and make note of the driver name and upstype number (if any) in the third column.
    Warning.png Warning:
    Always use the serial cable supplied with the UPS. Standard serial cables won't work with a serial UPS and have been known to cause damage to the UPS. Pay particular attention to any references to cable in in the UPS Model column.

  2. From the consol issue the following commands: config setprop nut Model <model> config setprop nut Device <device> config setprop nut Type <type> config setprop nut status enabled Where:
    <model> and <type> are the driver name and type number found above.
    <device> is the serial port that the UPS is connected to eg. /dev/ttyS0. It also possible to use a more readable symlink. See HowTo on udev - symlinks for details. Note: The case of Model, Device and Type.
  3. Check: config show nut
  4. Apply changes and restart server: signal-event post-upgrade signal-event reboot Alternatively, without NUT running or requiring a server reboot: signal-event console-save service nut start
  5. Confirm server is communicating with UPS: upsc UPS@localhost Whenever a UPS event occurs Emails are sent to the admin account.

Configuring as a slave

Set configuration values:

config setprop nut SlaveUPS UPS@192.168.33.11
config setprop nut Master no

Where 192.168.33.11 is your UPS master, that is the computer that is in direct communication with the UPS. The hostname of that computer may also work.

Apply changes and restart server:

signal-event post-upgrade
signal-event reboot 

Confirm server is communicating with master:

upsc UPS@192.168.33.11

Connecting multiple UPS's

To be added http://bugs.contribs.org/show_bug.cgi?id=629

[edit]

UPS Variables and Commands

In some cases you may wish to modify variables on the actual UPS such as the Low Charge/LOWBATTERY setting. This requires the use of the upsrw command and UPS administrative privileges.

You may also want to control the UPS directly from the command line by issuing UPS commands. This requires use of the upscmd command and UPS administrative privileges.

Warning.png Warning:
In general, the UPS data should be left protected and changes to it or issuing of commands well thought out. If you wish to make data changes or issue commands then the administrative privileges can be enabled as below and should then be disabled.


UPS Administrative Privileges

In order to be able to use upsrw and upscmd it is necessary to have a suitable additional user defined in the upsd.users configuration file.

In order to create a suitable user we will use SME Servers templating system and configuration database. This is based on an original forum thread[1].

First we need to create a suitable custom template directory

mkdir -p /etc/e-smith/templates-custom/etc/ups/upsd.users
cd /etc/e-smith/templates-custom/etc/ups/upsd.users

Create and edit a new file called 'admin' with the following content:

{
    # create admin user for upsd to allow setting of
    # UPS parameters via upsrw

    $OUT .= "";
    return unless (($nut{AdminUser} || 'disabled') eq 'enabled');
    return unless (($nut{AdminPass} || '') ne '');

    $OUT .= "\n";
    $OUT .= "       [admin]\n";
    $OUT .= "               password  = $nut{AdminPass}\n";
    if ( ($nut{Master} || 'yes') ne 'no') {
       $OUT .= "               allowfrom = localhost\n";
    } else {
       $OUT .= "               allowfrom = localhost localnet\n";
    }
    $OUT .= "               actions   = set\n";
    $OUT .= "               instcmds  = all\n";
}

Create two new database properties for nut

config setprop nut AdminUser enabled                (This enables the creation of the user in the template above)
config setprop nut AdminPass admin                  (This sets a password for the admin user. Set to whatever you want)

Now, to get upsd to recognise the new user with the required administrative privileges we expand the template and reload the upsd configuration

/sbin/e-smith/expand-template /etc/ups/upsd.users
/usr/sbin/upsd -c reload


Important.png Note:
To disabled the admin user once you have changed the UPS parameters or issued commands as required, issue the commands
config setprop nut AdminUser disabled
/sbin/e-smith/expand-template /etc/ups/upsd.users
/usr/sbin/upsd -c reload


Setting UPS Variables

In order to set UPS variables it is necessary to have enabled a user with administrative privileges as above first.

In the examples below, it is assumed your UPS name is UPS, that it is local, that the administrative user is admin and password admin. You can verify your UPS name via:

upsc -l

To view a complete list of the UPS variables, both informational and modifiable

upsc UPS

To determine the modifiable variables for your UPS, their current settings and their available setting values execute the command:

upsrw UPS

You can now modify the variables you wish using a command similar to the following (Note the order of the arguments is important, and you may need quotes around the value being set, "20"):

upsrw -s battery.charge.low=20 -u admin -p admin UPS

Where the value after -s should be one of the parameters identified by the upsrw ups command. You can of course verify your changes using

upsrw UPS

or

upsc UPS

After you are done, clean up by disabling the upsd administrative user admin:

Warning.png Warning:
Make sure you understand the meaning or the UPS variables and their available setting options. Verify that your changes meet your intended behaviour!


More information on upsrw can be found at:

- Manual page: man upsrw

Issuing UPS Commands

In order to issue UPS commands it is necessary to have enabled a user with administrative privileges as above first.

In the examples below, it is assumed your UPS name is UPS, that it is local, that the administrative user is admin and password admin. You can verify your UPS name via:

upsc -l

To view a complete list of available commands for your UPS:

upscmd -l UPS

You can now issue a command to the UPS with similar to the following:

upscmd -u admin -p admin UPS test.battery.start

Where the command test.battery.start is a valid command for your UPS as previously determined by upscmd -l UPS. Depending upon the command issued you may get broadcast messages and emails relating to and confirming what the UPS is doing.

After you are done, clean up by disabling the upsd administrative user admin:

Warning.png Warning:
Before issuing any commands verify what they do for your particular UPS via the relevant documentation and ensure that the command meets your intended behavioural requirement!

Issuing commands could shutdown your server unexpectedly!


Warning.png Warning:
Be sure to have e-smith-nutUPS-2.4.0-9.el6.sme.noarch or higher to carry on with these instructions. If you get a lower version, just follow SME8 instruction.


rpm -qa e-smith-nutUPS

If you get NUT running with administrative privileges modification from sme8 instruction, you need to remove the custom template created for this. These variables are taken in charge by the new package.

First you need to delete the custom template file.( This command delete the directory, be sure you don't have personal file present. If the case, delete manualy the file created for administrative privileges )

rm -rf /etc/e-smith/templates-custom/etc/ups/upsd.users

Now, to get upsd to recognise the modification of user, we need to expand the template and reload the upsd configuration

/sbin/e-smith/expand-template /etc/ups/upsd.users
/usr/sbin/upsd -c reload

UPS Variables and Commands

In some cases you may wish to modify variables on the actual UPS such as the Low Charge/LOWBATTERY setting. This requires the use of the upsrw command and UPS administrative privileges.

You may also want to control the UPS directly from the command line by issuing UPS commands. This requires use of the upscmd command and UPS administrative privileges.

Warning.png Warning:
In general, the UPS data should be left protected and changes to it or issuing of commands well thought out. If you wish to make data changes or issue commands then the administrative privileges can be enabled as below and should then be disabled.


UPS Administrative Privileges

You should check your new password ( AdminPass ) to run upserw & upscmd. Of course, you could change your password for a easier one to use.

config show nut

To set new admin password in database. The new password would be admin ( change it to suit your need )

config setprop nut AdminPass admin 

To enabled administrative privileges and run command to ups.

config setprop nut AdminUser enabled

Now, to get upsd to recognise admin modification for administrative privileges we expand the template and reload the upsd configuration

/sbin/e-smith/expand-template /etc/ups/upsd.users
/usr/sbin/upsd -c reload


Important.png Note:
To disabled the admin user once you have changed the UPS parameters or issued commands as required, issue the commands
config setprop nut AdminUser disabled
/sbin/e-smith/expand-template /etc/ups/upsd.users
/usr/sbin/upsd -c reload


UPS access

The access of the ups is controled by database properties. The default propertie is set to localhost and give permission to run upsrw & upscmd from localhost only if administrative privileges is set to enabled as above. No slave ups could be connected in this mode. Three choices is available to set access.


  • localhost: the ups access is only from the local machine ( UPS master ).
  • private: the ups access is from your local machine and local network as per define in server-manager panel.
  • public: the ups access is similar to localhost.


To set access properties in the database ( example: localhost )

config setprop nut access localhost  
/sbin/e-smith/expand-template /etc/ups/upsd.conf
signal-event console-save

In localhost or public mode ( no remote access ), access to your ups is ( UPS name is UPS )

UPS@localhost

In private mode, access to your ups is ( UPS name is UPS )

UPS@localhost or UPS@192.168.1.1 ( ups master IP )
slave ups get access with UPS@192.168.1.1 ( ups master IP )

Setting UPS Variables

In order to set UPS variables it is necessary to have enabled a user with administrative privileges as above first and you get the possibility to run command from slave ups if access is set to private as above.

In the examples below, it is assumed your UPS name is UPS, that it is local, that the administrative user is admin and password admin. You can verify your UPS name via:

upsc -l

To view a complete list of the UPS variables, both informational and modifiable

upsc UPS

To determine the modifiable variables for your UPS, their current settings and their available setting values execute the command:

upsrw UPS

You can now modify the variables you wish using a command similar to the following (Note the order of the arguments is important, and you may need quotes around the value being set, "20"):

upsrw -s battery.charge.low=20 -u admin -p admin UPS

Where the value after -s should be one of the parameters identified by the upsrw ups command. You can of course verify your changes using

upsrw UPS

or

upsc UPS

After you are done, clean up by disabling the upsd administrative user admin:

Warning.png Warning:
Make sure you understand the meaning or the UPS variables and their available setting options. Verify that your changes meet your intended behaviour!


More information on upsrw can be found at:

- Manual page: man upsrw

Issuing UPS Commands

In order to issue UPS commands it is necessary to have enabled a user with administrative privileges as above first.

In the examples below, it is assumed your UPS name is UPS, that it is local, that the administrative user is admin and password admin. You can verify your UPS name via:

upsc -l

To view a complete list of available commands for your UPS:

upscmd -l UPS

You can now issue a command to the UPS with similar to the following:

upscmd -u admin -p admin UPS test.battery.start

Where the command test.battery.start is a valid command for your UPS as previously determined by upscmd -l UPS. Depending upon the command issued you may get broadcast messages and emails relating to and confirming what the UPS is doing.

After you are done, clean up by disabling the upsd administrative user admin:

Warning.png Warning:
Before issuing any commands verify what they do for your particular UPS via the relevant documentation and ensure that the command meets your intended behavioural requirement!

Issuing commands could shutdown your server unexpectedly!


Scheduling Events

Shutdown Time Delay Example

By default NUT will issue a shutdown command as soon as it receives a low battery event from the UPS. There may be instances and installation configurations that require a shutdown sooner, or other events with timed or schedules outcomes. See the man pages etc for further info and example situations.

In essence the upsmon program monitors the relevant UPS and for each NOTIFYFLAG event in upsmon.conf takes immediate action as defined. In order to delay or schedule any actions, the events need to be passed to upssched which can set timers and schedule events.

The following changes to standard SME Server NUT configuration will shut down the server a specified time after receiving the "on battery" signal (the example given is for 2 minutes). It assumes you already have an enabled and working NUT configuration and UPS

To create a timed shutdown before the BATTLOW signal is received, it is necessary to configure upssched and have a script handle the UPS events (upsmon cannot do this).

First we need to create a new custom template directory:

mkdir -p /etc/e-smith/templates-custom/etc/ups/upsmon.conf
cd /etc/e-smith/templates-custom/etc/ups/upsmon.conf

Create and edit a new file called 'NOTIFYCMD' with the following content:

NOTIFYCMD /usr/sbin/upssched

Now create another a custom template directory

mkdir -p /etc/e-smith/templates-custom/etc/ups/upssched.conf
cd /etc/e-smith/templates-custom/etc/ups/upssched.conf

Create and edit a new file called '01CONFIG' with the following content:

CMDSCRIPT /sbin/e-smith/nutUPS.cmd
PIPEFN /tmp/upspipe
LOCKFN /tmp/upslock
AT COMMBAD * EXECUTE commbad
AT COMMOK * EXECUTE commok
AT NOCOMM * EXECUTE nocomm
AT ONBATT * EXECUTE powerout
AT ONBATT * START-TIMER shutdownnow 120     
AT LOWBATT * EXECUTE shutdowncritical
AT ONLINE * CANCEL-TIMER shutdownnow
AT ONLINE * EXECUTE powerup

In the above set the line AT ONBATT * START-TIMER shutdownnow 120 to how many seconds after ONBATT signal you want to shut down

Create and edit a new script file at:

/sbin/e-smith/nutUPS.cmd

Add the following content:

#! /bin/sh
       case $1 in
               commbad)
                       /bin/echo "UPS communications failure on `date`." | /bin/mail -s"UPS communications LOST" admin
                       /usr/bin/wall "UPS communications failure."
                       ;;
               commok)
                       /bin/echo "UPS communications restored on `date`." | /bin/mail -s"UPS communications restored" admin
                       /usr/bin/wall "UPS communications restored."
                       ;;
               nocomm)
                       /bin/echo "UPS communications cannot be established on `date`." | /bin/mail -s"UPS uncontactable" admin
                       /usr/bin/wall "UPS communications cannot be established."
                       ;;
               powerout)
                       /bin/echo "Power failure on `date`." | /bin/mail -s"UPS on battery" admin
                       /usr/bin/wall "UPS on battery. Shutdown in 60 seconds...."
                       ;;
               shutdownnow)
                       /bin/echo "UPS has been on battery for 60 seconds. Starting orderly shutdown on `date`." | /bin/mail -s"UPS on battery for 60 seconds" admin
                       /usr/bin/wall "UPS has been on battery for 60 seconds. Shutting down NOW!!!!"
                       /usr/bin/sudo /sbin/e-smith/signal-event halt
                       ;;
               shutdowncritical)
                       /bin/echo "UPS battery level CRITICAL. Starting EMERGENCY shutdown on `date`." | /bin/mail -s"UPS battery CRITICAL" admin
                       /usr/bin/wall "UPS battery level CRITICAL. Shutting down NOW!!!!"
                       /usr/bin/sudo /sbin/e-smith/signal-event halt
                       ;;
               powerup)
                       /bin/echo "Power restored on `date`." | /bin/mail -s"UPS on line" admin
                       /usr/bin/wall "UPS on line. Shutdown aborted."
                       ;;
               *)
                       /bin/echo "Unrecognized command: $1"
                       ;;
       esac

Nut requires to use sudo for this process to work, so sudo needs configuring to enable the user nut. By default the /etc/sudoers file is not part of the SME Server template system. To workaround this create a custom template directory:

mkdir -p /etc/e-smith/templates-custom/etc/sudoers
cd /etc/e-smith/templates-custom/etc/sudoers

To preserve the content of the orginal /etc/sudoers file copy that into the custom template directory:

cp /etc/sudoers 10sudoers 

Create and edit a new file called '30nut' with the following content:

nut   ALL=NOPASSWD: ALL

Then run:

expand-template /etc/sudoers

Finally to complete the process:

signal-event post-upgrade
signal-event reboot

Configure Multimon etc Scripts

The apcupsd-multimon rpm contains four cgi scripts that can be run via the webserver to monitor the UPS(s). The rpm does not install them correctly for SME however so the following modifications are needed.

mkdir -p /opt/apcupsd
chown root:www /opt/apcupsd
mv /var/www/cgi-bin/multimon.cgi /opt/apcupsd
mv /var/www/cgi-bin/upsstats.cgi /opt/apcupsd
mv /var/www/cgi-bin/upsfstats.cgi /opt/apcupsd
mv /var/www/cgi-bin/upsimage.cgi /opt/apcupsd
chown root:www /opt/apcupsd/*
chmod 750 /opt/apcupsd/*
mkdir -p /etc/e-smith/templates-custom/etc/httpd/conf/httpd.conf
cd /etc/e-smith/templates-custom/etc/httpd/conf/httpd.conf

Now edit and create a new file 92apcupsdmon with the following content

{
    $OUT = "";
    my $allow = 'all';
    my $pass = '0';
    my $satisfy = 'all';
    my $name = $apcupsd{'Name'} || 'APC UPS Daemon Monitoring';

    for ('exit-if-none')
    {
      if ($apcupsd{'PublicAccess'})
      {
          if ($apcupsd{'PublicAccess'} eq 'none')
          {
           next;
          }
          elsif ($apcupsd{'PublicAccess'} eq 'local')
          {
            $allow   = $localAccess;
            $pass    = 0;
            $satisfy = 'all';
          }
          elsif ($apcupsd{'PublicAccess'} eq 'local-pw')
          {
            $allow   = $localAccess;
            $pass    = 1;
            $satisfy = 'all';
          }
          elsif ($apcupsd{'PublicAccess'} eq 'global')
          {
            $allow   = 'all';
            $pass    = 0;
            $satisfy = 'all';
          }
          elsif ($apcupsd{'PublicAccess'} eq 'global-pw')
          {
            $allow   = 'all';
            $pass    = 1;
            $satisfy = 'all';
          }
          elsif ($apcupsd{'PublicAccess'} eq 'global-pw-remote')
          {
            $allow   = $localAccess;
            $pass    = 1;
            $satisfy = 'any';
          }
      } 

      $OUT .= "#------------------------------------------------------------\n";
      $OUT .= "# apcupsd multimon - $name\n";
      $OUT .= "#------------------------------------------------------------\n";

      {
        if ((exists $apcupsd{'URL'}) && ($apcupsd{'URL'} ne '')) {
          $OUT .= "Alias  /$apcupsd{'URL'}  /opt/apcupsd\n"; 
        }  
      }

      $OUT .= "Alias  /apcupsd  /opt/apcupsd\n";

      $OUT .= "\n";
      $OUT .= "<Directory /opt/apcupsd>\n";
      $OUT .= "    DirectoryIndex upsstats.cgi\n";
      $OUT .= "    Options +ExecCGI\n";
      $OUT .= "    order deny,allow\n";
      $OUT .= "    deny from all\n";
      $OUT .= "    allow from $allow\n";
      if ($pass)
      {
          $OUT .= "    AuthName $name\n";
          $OUT .= "    AuthType Basic\n";
          $OUT .= "    AuthExternal pwauth\n";
          $OUT .= "    require valid-user\n";
          $OUT .= "    Satisfy $satisfy\n";
      }
      $OUT .= "</Directory>\n";
    }
}

Configure databases and expand the template

db accounts set apcupsd reserved
config setprop apcupsd PublicAccess local
/sbin/e-smith/expand-template /etc/httpd/conf/httpd.conf
  Note:
The above sets access to the scripts to local ip addresses only. See the Web_Application_RPM#New_DB_settings for further info and settings


Restart the web server

sv t httpd-e-smith

Usage of Multimon etc Scripts

Now go to http://yourdomain.tld/apcupsd to see the statistics for the UPS at localhost, or https://yourdomain.tld/apcupsd/multimon.cgi for an overview of the UPS(s).

By editing /etc/apcupsd/hosts.conf and adding additional network UPS details, multimon.cgi can be used to monitor more than one UPS. By default only the localhost is monitored.


Aditional Information

There are template fragments in /etc/e-smith/templates/etc/ups that control the config files located in /etc/ups. The default settings should be OK for most situations.

By default, NUT is configured for a USB connected UPS in Master mode, but is disabled. When enabled, NUT will monitor the UPS and take various actions when certain notifications are received. This is controlled by the /etc/ups/upsmon.conf file which among other things lists the notifications and the actions to be taken for each. For example an On Battery event is captured by the NOTIFYFLAG ONBATT entry and the following SYSLOG+WALL+EXEC command string. This string tells upsmon to write the event to the System Log, broadcast a message to all users via Wall, and execute the command denoted by the NOTIFYCMD entry.

SME Server sets the NOTIFYCMD to /sbin/e-smith/nutUPS.notify, and this executable file simply sends an email to the SME admin user with a notification of the event.

Apart from the various events that the UPS and upsmon may notify via the NOTIFYFLAGS a Low Battery event will automatically and immediately cause upsmon to issue the SHUTDOWNCMD as defined in upsmon.conf (signal-event halt) and set a flag POWERDOWNFLAG so it knows on future restart that it is a UPS recovery.

For information on configuration parameters:

man ups.conf
man upsd.conf
man upsd.users
man upsmon.conf
man upssched.conf

For general information:

man upsd
man nutupsdrv

The NUT website is here: NUT

From the above references you can glean which configuration setting does what function, etc.

If you want to modify the operation of NUT from the standard configuration, then you should generally modify the NUT config files by creating custom templates, expanding the templates and restarting service. This will ensure modifications survive a future reboot or reconfiguration.

An example of doing this can be found in the forum [2] and in the section above for UPS Administrative Privileges

See also Known Problem - Restarting Nut


Documentation

Nut is a Software well documented, you can find the TOC here and with an overview

Ne pas oublier de rajouter les categories