* Network UPS Tools Documentation
*
* Russell Kroll <rkroll@exploits.org> and others, see CREDITS file.
*
* Released under the GNU GPL - see COPYING for details.
*
* Program support page: http://www.exploits.org/nut/
* Mailing list details: http://lists.exploits.org/

==============================================================================
 DESCRIPTION
==============================================================================

This is a developing project to monitor a large assortment of UPS hardware.
Many models have ports on the back to allow other devices to check on
their status.  If it gives basic information about the power and battery
status, it can probably be supported without too much difficulty.  More
advanced features on the higher end models are also supported to allow
tracking of values over time such as temperature and voltage.

Network communications are used so that multiple systems can monitor a
single physical UPS and shut down together if necessary without any
special "sharing hardware" on the UPS itself.

==============================================================================
 INSTALLING
==============================================================================

If you are installing these programs for the first time, go read the
INSTALL file to find out how to do that.  This document contains more
information on what all of this stuff does.

==============================================================================
 NETWORK INFORMATION
==============================================================================

These programs are designed to share information over the network.  In
the examples below, "localhost" is used as the hostname.  This can also be
an IP address, fully qualified domain name, and so on.  You can also
specify a port number as part of the hostname in case your upsd process
runs on another port.

In the case of the program upsc, to contact the upsd server running on the
default port 3305 on the local machine, you'd do this:

	/usr/local/ups/bin/upsc localhost

You can compile in a different port number with "configure --with-port".  
That will change the default ports that the programs use.  

To make the client talk to localhost on a specific host (overriding any
compiled-in value), add it after the hostname with a colon, like this:

	/usr/local/ups/bin/upsc localhost:1234

This is handy when you have a mixed environment and some of the systems
are on different ports.  Finally, UPSes have names, and sometimes you have
more than one on a given system.  Here's how you tell them apart.

	/usr/local/ups/bin/upsc bigups@mysystem

	/usr/local/ups/bin/upsc sparky@mysystem

The UPS name goes in front of the @.  If you don't specify a UPS name, it
picks the first one in the configuration files on the server.  The general
form for UPS identifiers is simply:

	[<upsname>@]hostname[:port]

Keep this in mind when viewing the examples below.

==============================================================================
 MANIFEST
==============================================================================

This package is broken down into several categories:

 - drivers - (aka "models") These programs talk directly to your UPS hardware. 

 - server  - upsd serves data from the models (drivers) to the network.

 - clients - They talk to upsd and do things with the status data.

 - cgi-bin - Special class of clients that you can use with your web server.

==============================================================================
 DRIVERS
==============================================================================

These programs provide support for specific UPS models.

You can run them directly, and the basic form of that looks like this:

	/installpath/bin/<model> /dev/port

So, to run the apcsmart driver on port ttyS1 given an install path of
/usr/local/ups, you'd do:

	/usr/local/ups/bin/apcsmart /dev/ttyS1

You should only use this form when testing or debugging the drivers, as
it's messy and there are better ways to start them.  For normal
operations, you should configure them by using the ups.conf file.
Let's define an example UPS that we'll call "sparky".  It uses the
apcsmart driver, and is connected to /dev/ttyS1 - the second serial
port on most Linux-based boxes.  The configuration in ups.conf then looks 
like this:

	[sparky]
		driver = apcsmart
		port = /dev/ttyS1

Easy.  Now you can use the driver controller to start it by name.

	/usr/local/ups/bin/upsdrvctl start sparky

 EXTRA ARGUMENTS
 ---------------

Some drivers require additional arguments to properly communicate with
your hardware.  If it doesn't detect your UPS with no extra arguments,
then check the driver's help (-h) to see what options are available.

When available, these additional options may be specified on the command
line with -x or in the ups.conf.

For example, the apcsmart driver allows setting "cable" to "940-0095B".
To do this on the command line, it would look like this:

	/usr/local/ups/bin/apcsmart -x cable=940-0095B /dev/ttyS1

ups.conf is the right way to handle driver configuration, so you should
store the value in there.  Here's how you put that cable definition in
ups.conf:

	[sparky]
		driver = apcsmart
		port = /dev/ttyS1
		cable = 940-0095B

After that, every time you start the driver through upsdrvctl, it will
know to use that cable setting.

 HARDWARE SUPPORT TABLE
 ----------------------

Check this table to see which driver or drivers support your hardware.

	apcsmart         - APC Smart-UPS, Back-UPS Pro, Matrix-UPS models

        belkin           - Belkin Regulator Pro

	bestferrups801-807

			 - Best FerrUPS 8.01-8.07 models

	bestups          - Best Power models: (newer, usually beige)

                           - Fortress (FOR)
                           - Fortress Telecom (FTC)
                           - Patriot Pro (PRO)
			   - Patriot Pro II (PR2)
                         
                           The SOLA 520 and 620 are also recognized.

			   This driver will probably also work with other
			   PhoenixTec protocol hardware.

	cyberpower       - Cyber Power Systems units (experimental)

			   This one is still in the very early stages.
			   It will probably only give useful numbers on
			   the AVR700.

	everups          - EVER Sp. z o.o models

                           - NET *-DPC
                           - AP *-PRO 

	fentonups        - Fenton Technologies models:

	                   - PowerPal
	                   - PowerOn
	                   - PowerPure

                         - Effekta MI/MT/MH models (2502 cable)

                         - PowerGuard PG-600

			 - PowerCom SMK-800A

			   This driver implements the Megatec protocol, so
			   other Megatec hardware will probably work with it.

	genericups       - Many contact closure models -
                           see "Generic UPS driver" below 

        hidups           - Experimental driver for USB HID UPSes on Linux

                           Some units that have been used with this driver:

                           - APC Back-UPS Pro 350 USB
                           - APC Back-UPS 500 USB
                           - MGE Ellipse USB

                           NOTE: not built by default.  Read the FAQ
                           to find out how to build/install/use this one.

	hp		 - HP Powertrust models

	masterguard	 - Masterguard UPS hardware

	mge-ellipse	 - MGE Pulsar units

	newapc           - APC smart protocol units

			   - Back-UPS Pro
			   - Smart-UPS
			   - Matrix-UPS

			   Note: older Matrix-UPS units may have better
			   results with apcsmart instead

	powercom	 - Trust 425/625
			 - Powercom units
			 - Advice Partner/King PR750

	sec		 - SEC protocol units (various sources)

	snmp-ups         - SNMP UPS equipment (RFC 1628)
                           Experimental driver - not built by default.

                           See docs/drivers/snmp-ups.txt for more
                           information.

	tripplite	 - Tripp-Lite SmartUPS units

	victronups	 - IMV/Victron hardware

For another take on this list, try the web page: 

	http://www.exploits.org/nut/library/compat.html

If this list seems smaller than usual, it's due to unmaintained drivers
being removed recently.  See the FAQ.

 GENERIC UPS DRIVER
 ------------------

The "genericups" driver will support many models that use the same basic
principle to communicate with the computer.  This is known as "contact
closure", and basically involves raising or lowering signals to indicate
power status.

This type of UPS tends to be cheaper, and only provides the very simplest
data about power and battery status.  Advanced features like battery
charge readings and such require a "smart" UPS and a driver which
supports it.

See the genericups(8) man page for more information.

There is also a document called generic-ups.txt included with the
source distribution that contains information on adding additional
types to the genericups driver.

 DRIVER CONTROL
 --------------

UPS drivers should be controlled with upsdrvctl.  It uses the ups.conf to 
start and stop the programs that support the UPS hardware.

To start all UPS drivers in the ups.conf, just use "start":

	/usr/local/ups/bin/upsdrvctl start

To stop all drivers, use "stop":

	/usr/local/ups/bin/upsdrvctl stop

To start or stop just one, list the UPS name after the command:

	/usr/local/ups/bin/upsdrvctl start sparky
	/usr/local/ups/bin/upsdrvctl stop sparky

With upsdrvctl, you can install a bunch of different UPSes on different
machines and just put "upsdrvctl start" in their startup scripts.  It
will figure out the right drivers from the ups.conf on each system.

In the old days, you had to change the startup scripts to reflect the
configuration of each system, and it made a mess for those of us with
several systems that would otherwise have identical startup files.

You can also use upsdrvctl to shut down (power down) all of your UPS 
hardware with the shutdown command.  Use this command with caution!

WARNING - if you play around with this command, expect your filesystems
to die.  Don't power off your computers unless they're ready for it.

	/usr/local/ups/bin/upsdrvctl shutdown
	/usr/local/ups/bin/upsdrvctl shutdown sparky

You should read the shutdown.txt file in the docs subdirectory to
learn more about when to use this feature.  If called at the wrong time,
you may cause data loss by turning off a system with a filesystem
mounted read-write.

==============================================================================
 NETWORK SERVER
==============================================================================

upsd is responsible for passing data from the model modules to the client
programs via the network.  You should start it after whatever driver is
appropriate for the UPS you have.  Continuing with the example where
the package was installed in /usr/local/ups, the startup line would
look like this:

	/usr/local/ups/sbin/upsd

 ARGUMENTS
 ---------

upsd supports several arguments to change its default behavior.  Start it
with -h to see more information on what they are.

 MULTIPLE UPS MONITORING
 -----------------------

There is no hard limit to the number of local UPSes that upsd can monitor
aside from your system's resources.  Simply define a section for each UPS,
start the appropriate driver, and upsd will serve data for it.  Once this
is done, you can use programs like upsc to read the status.

To check the first/only UPS on your system:

	upsc localhost

To check another UPS on your system, assuming you have more than one in
the ups.conf:

	upsc upsname@localhost

"upsname" corresponds to the name of the section in the ups.conf.  To
configure a UPS called "zoidberg", it would look something like this:

	[zoidberg]
		driver = futureups
		port = /dev/lobster

Multiple local UPS monitoring works best on systems with many serial
ports.


==============================================================================
 UPSMON
==============================================================================

upsmon is a very important daemon that provides the basic functionality
you expect from UPS monitoring software - system shutdowns when the power
fails.  Technically, it is a "client", but it has its own section in
the documentation because it is so important.

You configure it by telling it about UPSes that you want to monitor in
upsmon.conf.  Each UPS can be defined as one of three possible types:

 - Master - This UPS supplies power to the system running upsmon, and
   this system is also responsible for shutting it down when the battery
   is depleted.  This occurs after any slave systems have disconnected
   safely.

   If your UPS is plugged directly into a system's serial port, the
   upsmon on that system should define that UPS as a Master.

 - Slave - This UPS supplies power to the system running upsmon, but 
   this system can't shut it down directly.  This system will shut down
   the operating system before the master turns off the power.

   Use this mode when you run multiple computers on the same UPS.
   Obviously, only one can be connected to the serial port on the UPS,
   and that system is the master.  Everything else is a slave.
   
 - Monitor-only.  This UPS will still generate notifications about
   status changes (on battery, on line, etc.) but no shutdowns of the
   local system result from critical situations on that UPS.

For a typical home user, there's one computer connected to one UPS.
That means you run a model driver, upsd, and upsmon in master mode.

Here's how you configure upsmon to suit your environment.

 MASTER MODE
 -----------

In this mode, upsmon keeps the system running until all slaves disconnect
safely.  This allows them extra time to bring down the operating
system.  The master system then runs its own shutdown sequence, which
usually includes a call to the model driver to kill the power after the
filesystems have been remounted read-only.

	MONITOR someups@somehost 1 mypassword master

Note that the password must match the corresponding ACCESS line in the
upsd.conf.  This example assumes that "someups@somehost" runs just 1
of this system's power supplies.  If you have more than one power supply,
be sure to set this appropriately.

Note that upsmon will shut down *all* UPSes that are listed as "master"
in the config file when the situation gets bad enough to warrant a 
shutdown.  

Example:

 - You have two power supplies.  Each one has its own UPS.
 - This particular computer needs both power supplies to keep running.
 - Someone trips over the cord to one of the UPSes.

Eventually, that UPS will drain and reach a critical state.  upsmon will
notice this situation and command a shutdown for *both* UPSes since it
is master for both of them.  Any slaves attached to these units will
see the "forced shutdown" flag and shut themselves down too, so they
will be OK.

 SLAVE MODE
 ----------

This mode is configured much like master mode, except for the last keyword.

	MONITOR someups@somehost 1 mypassword slave

If upsmon is running in slave mode for a given UPS, it will run the
local shutdown command immediately when a UPS goes critical.  This can
happen one of two ways:

 - The UPS reaches a low battery state while running on battery power.
 - The UPS has the "forced shutdown" flag set by a master upsmon process.

The idea is to shut down quickly and safely before the master upsmon 
comes along and turns the power off.

 MONITOR-ONLY MODE
 -----------------

You can configure a UPS strictly for monitoring by declaring the number
of power supplies serviced by it to be 0.  You do that with a config
entry like this:

	MONITOR someupsname@somehost 0 nopass slave

The password and "slave" don't actually do anything here, but are included
to keep the number of fields constant.

 SHUTDOWN CONFIGURATION
 ----------------------

The stock command to shut down the system is "/sbin/shutdown -h +0".
That works in most places.  If your system needs to do something else
when it's time for upsmon to shut it down, edit the upsmon.conf and 
change SHUTDOWNCMD to that new value.

 PRIVILEGES
 ----------

upsmon must start as root on most systems as it needs to exec a shutdown 
command.  By default, upsmon splits into a pair of processes after 
entering the background.  One retains root powers, while the other drops 
to an unprivileged user ID.

The unprivileged process does most of the heavy lifting - contacting 
upsd, keeping track of UPS status data, and so on.

Meanwhile, the root process just waits for the shutdown signal.  When that 
arrives, it will write out the power down flag and call your SHUTDOWNCMD.

If you need to run upsmon in the old style where the whole thing always
runs as root, call it with the -p flag.  To change the user that the
unprivileged process becomes, specify it with -u.

If your system has capabilities and fine grained permissions, then full
root access is not necessary.  That style of configuration is beyond the
scope of this document.

 ADDITIONAL INFORMATION
 ----------------------

More information on configuring upsmon can be found in these places:

 - The man page - upsmon(8)

 - big-servers.txt in the docs subdirectory

 - shutdown.txt in the docs subdirectory

 - The stock upsmon.conf that comes with the package

==============================================================================
 CLIENTS
==============================================================================

Clients talk to upsd over the network and do useful things with the data
that's being collected.  Some of them are designed for basic command line
usage while a special subset can be run as CGI programs from within your
web server.

 UPSC
 ----

upsc provides a quick way to test the functionality of your setup.  To
run it, just do "upsc localhost" and see what comes back.  The results
vary greatly based on the system and the hardware being monitored.  A
typical run on the example configuration once looked like this:

	rkpent:~$ upsc localhost
	host: localhost
	MFR: APC
	MODEL: SMART-UPS 700
	SERIAL: WS9643050926
	STATUS: OL
	UTILITY: 113.7
	BATTPCT: 100.0
	ACFREQ: 60.00
	LOADPCT: 023.9
	UPSTEMP: 038.2
	UPSIDENT: RKPENT
	LOWXFER: 103
	HIGHXFER: 132
	WAKEDELAY: 060
	LINESENS: H
	AMBTEMP: 27.68
	CONTACTS: F0
	AMBHUMID: 041.6

Every value known to upsd about your ups is returned, so this list has
grown quite large as more features have been added to the software.  You
can use upsc along with other Unix tools like cut, grep, sed, and awk to
monitor UPS equipment easily in shell scripts.

The meaning of these values is documented.  See protocol.txt in the
docs subdirectory for more information.

upsc is also a good place to start if you're interested in writing your
own lightweight client to talk to upsd.  The source is relatively simple,
and it's easy to start fetching variables with this code base.

 UPSCT
 -----

Like upsc, but this version uses TCP connections to get things done.  This
will probably perform a lot better over links where UDP packets get
corrupted by noisy links or are dropped by firewalls.

 UPSLOG
 ------

To keep an eye on your UPS hardware, use upslog.  It will poll upsd at
set intervals and log the data to a file.  You can then use this to
generate interesting looking graphs or reports with other programs.

See the upslog(8) man page for more information.

 UPSCT2
 ------

This is a simple command line administrationc tool for displaying and
changing the read/write settings in your UPS hardware.  Not all devices
support the notion of changing things, so this may not have any effect
on your system.

See the upsct2(8) man page for more.

 UPSCMD
 ------

Some models support "instant commands".  upscmd provides an interface to
access those commands that may be supported in your hardware.  To see
what's available, use the -l command:

	/usr/local/ups/bin/upscmd -l <ups>

Continuing with the localhost example, that's:

	/usr/local/ups/bin/upscmd -l localhost

You should get a list of a few interesting things on most models.  To
actually invoke one of them, specify the ups and command to run:

	/usr/local/ups/bin/upscmd localhost fptest

At this point, upscmd will ask for a user name and password.  You'll need
to use a valid combination as defined in your upsd.users file.  Once 
that's entered correctly, the UPS should perform a front panel test
(fptest), assuming it's capable of such a task.

This will only work if the user/password combo are valid and if that user
has been granted permission to actually do whatever command you specify.
See the example upsd.users file for more information on this.

==============================================================================
 CGI PROGRAMS
==============================================================================

These are a special subset of the clients that provide UPS information
through a web interface.  This requires a web server with a sane CGI
implementation.  Apache is the most common server for this sort of thing
but others should be able to cope too.

These programs are not installed or compiled by default.  To compile them,
do "make cgi".  To install, "make install-cgi".  If you receive errors
about "gd" during configure, go get it and install it before continuing.

You can get the source here:

	 http://www.boutell.com/gd/

What happens next depends on your compiler's configuration.  If the 
install target for gd puts everything in a sane place, your compiler and
linker should pick it all up and be happy with it.  If those directories
are not searched on your system, consider changing things so that they are
*or* install gd somewhere else.

Once the CGI programs are installed, it's now a matter of making your web
server find them.  I usually point a symlink from my cgi-bin directory to
/usr/local/ups/cgi-bin and enable symlinks for that part of the tree.
Apache seems to like this just fine.  

Assuming a fairly stock configuration of both Apache and these programs,
setting this symlink should be sufficient.

	cd /usr/local/apache/cgi-bin ; ln -s /usr/local/ups/cgi-bin ups

Given recent versions of Apache, you will probably have to use 
"Options FollowSymLinks" to make that work.  If this is an unacceptable
parameter on your system for security reasons or otherwise, point the link
the other way and force the programs to install directly under the cgi-bin
directory.  They don't care where they are as long as the config files are
readable.

 HOSTS.CONF
 ==========

The CGI programs use this configuration file to limit the hosts that
they may speak to.  If you tell one of them to talk to a host that isn't
exactly matched by an entry in this file, they will refuse.  This keeps
random people from using your web server's programs to open connections
to other hosts.

So, if your CGI programs say "Access to that host is not authorized",
check the hosts.conf first.

 MULTIMON
 ========

This program lets you watch many different systems from one web page.
It also provides links to other pages with more information on each.
It figures out which systems to monitor by reading the hosts.conf file.
Be sure to set this up with each system that you'd like to monitor.

This is probably the best one to bookmark if multiple systems are
routinely monitored in your configuration.

You can add "&refresh=nn" to your multimon.cgi URL if you want it to
instruct your web browser to reload the page every nn seconds.

 UPSSTATS
 ========

upsstats provides a page that attempts to look like Powerchute with the
help of upsimage.  It shows some basic information about the system being
monitored and then IMG SRCs a few images that draw bars showing current
values.  They are drawn by upsimage, which is the next section:

 UPSIMAGE
 ========

This is usually called by upsstats via IMG SRC tags to draw either the
utility voltage, battery charge percent, or load percent.  It may be
useful to call from other pages, but usually isn't.

 UPSSET
 ======

upsset provides several useful administration functions through a web
interface.  You can use upsset to kick off instant commands on your UPS
hardware like running a battery test.  You can also use it to change
variables in your UPS that accept user-specified values.

Essentially, upsset provides the functions of upsct2 and upscmd, but
with a happy pointy-clicky interface.

upsset will not run until you convince it that you have secured your
system.  You *must* secure your CGI path so that random interlopers
can't run this program remotely.  See the upsset.conf file.  Once you
have secured the directory, you can enable this program in that
configuration file.  It is not active by default.

==============================================================================
 SUPPORT / HELP / ETC.
==============================================================================

The main URL:

	http://www.exploits.org/nut/

There is also a mailing list for general queries and discussion about
this software.  It typically moves around 50-100 messages per month at
the time of this writing.  To join, send "subscribe ups" to  
majordomo@lists.exploits.org.  

If you just want to receive an automatic message when a new version
(release or testing) is posted, subscribe to upsdevannounce instead.  That
list is moderated, and will only be used for these notifications.

Finally, there are developer lists called upsdev and hidups.  upsdev is
for any development, and hidups is just for that one driver.  These are
not install help lists, and any such mails probably will be ignored.

The submission address is just the list name @ lists.exploits.org.

The mailing lists are archived on the web:

	http://lists.exploits.org/

Try running some searches against the archives.  Many times, problems have
already been answered by someone else.

There is more documentation in the docs/ directory within the source 
tree.  Be sure to read through the files in there (especially the
FAQ) before mailing the list for help.  Many times the questions have
already been answered in the files which are right in front of you.

==============================================================================
 MAKING YOUR OWN CLIENTS (UPSFETCH)
==============================================================================

The upsfetch.o library can be linked into other programs to let them grab
data from a UPS running this software.  The clients upsc and upsct are
provided as lightweight examples of how to retrieve data using those
functions.  Other programs may need this library for linking.  In this
case, do "make install-misc" and it will put that and the header file in a
directory called misc under your install path.

==============================================================================
 VERSION NUMBERING
==============================================================================

Here's how the version numbering has been proceeding:

 - Everything is called 0.x since planned features are lacking.  Once
   the package includes all of the items marked for 1.0, then it will
   become 1.0.

 - The middle number is currently incremented with each new project.
   For example, the 0.44.x releases focused on upsmon.  Before that,
   0.43.x introduced instant commands and clients to use them.

   This middle-number scheme is not expected to continue after 1.0.

 - "-pre" versions are developmental snapshots of the tree.  They are
   potentially broken as code is reworked between releases.  Typically
   the last -pre version will be nearly identical to the release that
   follows, except for any bug fixes and documentation updates.

==============================================================================
 HACKING / DEVELOPMENT INFO
==============================================================================

Additional documentation can be found in the docs subdirectory.  Of
particular interest to those looking to create a new driver are 
model-arguments.txt and new-modules.txt.  Also see skel.c and the
existing new-style drivers that use main.c to see how it's done.

Information on the architecture and how it all fits together is in the
design.txt file.  In short, there's a lot more documentation out there.

==============================================================================
 ACKNOWLEDGEMENTS / CONTRIBUTIONS
==============================================================================

Fenton Technologies contributed a PowerPal 660 to the project.  Their open 
stance and quick responses to technical inquiries are appreciated for 
making the development of the fentonups driver possible.

Bo Kersey of VirCIO (http://www.vircio.com) provided a Best Power 
Fortress 750 to facilitate the bestups driver.  

Invensys Energy Systems provided the SOLA/Best "Phoenixtec" protocol
document currently residing at the following URL:

	http://www.exploits.org/nut/library/protocols/sola.html

PowerKinetics technical support provided documentation on their MiniCOL
protocol, which is archived in the NUT protocol library online:

	http://www.exploits.org/nut/library/protocols/minicol/

Cyber Power Systems contributed a 700AVR model for testing and driver
development.

MGE UPS Systems provided a Pulsar Ellipse premium 500 for development
and expansion of the new hidups driver, along with extensive information
on their implementation of the HID power class and other documents.
That documentation is online at this URL:

	http://www.exploits.org/nut/library/protocols/mge/

All donated equipment can usually be seen on the big multimon page:

	http://www.exploits.org/cgi-bin/ups/multimon.cgi

Finally, a special thanks to CPS (http://www.citypublicservice.com/)
for providing authentic extended power failures on a regular basis.
Their innovative service provides plenty of opportunities to test
this software and defrost my refrigerator every time there's a
thunderstorm.
