* 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
==============================================================================

Network UPS Tools is a collection of programs which provide a common
interface for monitoring and administering UPS hardware.  It uses a
layered approach to connect all of the parts.

Drivers are provided for a wide assortment of equipment.  They
understand the specific language of each UPS and map it back to a
compatibility layer.  This means both an expensive "smart" protocol UPS
and a simple "power strip" model can be handled transparently.

This information is cached by the network server upsd, which then
answers queries from the clients.  upsd contains a number of access
control features to limit the abilities of the clients.  Only authorized
hosts may monitor or control your UPS hardware if you wish.  Since the
notion of monitoring over the network is built into the software, you
can hang many systems off one large UPS and they will all shut down
together.

Clients such as upsmon check on the status of the hardware and do things
when necessary.  The most important task is shutting down the operating
system cleanly before the UPS runs out of power.  Other programs are
also provided to log UPS status regularly, monitor status through your
web browser, and more.

==============================================================================
 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 3493 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 - These programs talk directly to your UPS hardware. 

 - server  - upsd serves data from the 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/<driver> /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
 ----------------------

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

	bcmxcp           - Powerware UPS 9120 'Binary Computer Mode'

	belkin           - Belkin Regulator Pro

	bestferrups801-807

			 - Best FerrUPS 8.01-8.07 models

	bestuferrups	 - Best Power Micro-Ferrups 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 700AVR.

	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 Ellipse / Ellipse Premium units

	mge-utalk	 - MGE Pulsar ESV/EX, ES+, Evolution units

	microdowell      - Microdowell BBox 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
                         - Socomec Sicon Egys 420 VA 

	sec		 - SEC protocol units (various sources)

                         - Clary ST-800

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

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

	powernet	 - APC SNMP devices (e.g. SNMP management cards)
				Requires Net-SNMP and OpenSSL libraries.
				Experimental driver, not built by default.

	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

This list may seem smaller when compared to previous versions (circa
0.45.5) since old style drivers were removed.  See the FAQ if this
affects you.

 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 drivers 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

 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 which provides the essential feature
you expect to find in UPS monitoring software - safe shutdowns
when the power fails.  It is a "client", but has a separate section in
the documentation since 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 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
typically powers off the UPS at the very end of the scripts.

	MONITOR someups@somehost 1 mypassword master

This example tells upsmon to:

	- Monitor a UPS called someups ("[someups]" section in ups.conf)

	- ... on a system called "somehost"

	- ... that provides power to 1 power supply on this system

	- ... using the password "mypassword" when talking to upsd
	  (as set in your upsd.conf with ACCESS definitions)

	- ... in master mode.

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

The difference here is that upsmon will run the local shutdown command
immediately when the power situation becomes critical.  This can happen
one of two ways:

	- The UPS is running on battery (OB) and it is low (LB).

	- The "forced shutdown" (FSD) flag has been set by the
	  master upsmon process.

The goal is to quickly shut down the system before the master upsmon,
since the master system will tell the UPS to 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.

 PRIVILEGES
 ----------

upsmon requires root privileges on most systems since it needs to
execute the shutdown command.  Typically this involves sending a signal
to init, and that can only be done by the owner of the process.  This
one command is the only part of the entire package that needs root. 
Therefore, upsmon makes every effort to minimize the use of privileges
during runtime.

You should start upsmon as root.  It will then fork off a child process
which drops privileges and starts monitoring your UPS hardware.  This
child process does all of the work - talking to the network, calling
your external notify commands, and so on.  It keeps a pipe open to the
privileged parent, which sits around waiting for a single command.

In the event that a shutdown is necessary, the unprivileged upsmon child
will send a command over that pipe to the privileged parent.  The parent
will then call your SHUTDOWNCMD, and the process will commence.  This
makes it very difficult to gain root powers through upsmon, as the
privileged part can only do two things - call the shutdown command, or
exit.

This means you will see two upsmon processes in your process listing.
This is expected, and it means things are working properly.  If you
really want to run the whole thing as root, call it with the -p flag.

Some lucky systems may be able to grant upsmon the capability to send
the shutdown command without requiring full blown root.  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.

For more details on specific programs, refer to their man pages.

 UPSC
 ----

upsc is a lightweight client that sends queries over UDP to upsd.  It
will give you a listing of every status variable for a UPS by default,
or just one if you specify an additional argument.  This can be useful
in shell scripts for monitoring something without writing your own
network code.

upsc is a quick way to find out if your driver(s) and upsd are working
together properly.  Just run upsc <ups> to see what's going on, i.e.:

	morbo:~$ upsc localhost
	host: localhost
	MFR: APC
	MODEL: SMART-UPS 700

	[ and so on ]

If you are interested in writing a simple client that monitors upsd,
the source code for upsc is a good way to learn about using the 
upsfetch functions.

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

 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
 ------

upslog will write status information from upsd to a file at set
intervals.  You can use this to generate graphs or reports with other
programs such as gnuplot.

 UPSCT2
 ------

upsct2 allows you to display and change the read/write variables in your
UPS hardware.  Not all devices or drivers implement this, so this may
not have any effect on your system.  

A driver that supports read/write variables will give results like this:

	$ upsct2 localhost
	host: localhost
	[SLFTSTINT] "Selftest intervals" (ENUM:4)
	Option: "336"
	Option: "168" SELECTED
	Option: "ON "
	Option: "OFF"

	[ snip ]

On the other hand, one that doesn't support them will print this:

	$ upsct2 fenton@gearbox
	host: gearbox
	No R/W variables available on this UPS.

upsct2 requires administrator powers to change settings in the hardware.
Refer to upsd.users(5) for information on defining users in upsd.

 UPSCMD
 ------

Some UPS hardware and drivers support the notion of an instant command -
a feature such as starting a battery test, or powering off the load.
You can use upscmd to list or invoke instant commands if your
hardware/drivers support them.

Use the -l command to list them, like this:

	$ upscmd -l localhost
	Instant commands supported on UPS [localhost]:

	ON - Turn on load
	FPTEST - Front panel test

	[ snip ]

upsmon also requires administrator powers to start instant commands.
To define users and passwords in upsd, see upsd.users(5).

==============================================================================
 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,
first run 'configure --with-cgi', then 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/

In the event that you need libpng or zlib in order to compile gd, 
they can be found at these URLs:

	http://www.libpng.org/pub/png/pngcode.html

	http://www.gzip.org/zlib/

 ACCESS RESTRICTIONS
 ===================

The CGI programs use hosts.conf to see if they are allowed to talk to a
host.  This keeps malicious visitors from creating queries from your web
server to random hosts on the Internet.

If you get error messages that say "Access to that host is not
authorized", you're probably missing an entry in your hosts.conf.

 MULTIMON
 ========

multimon.cgi provides an overview of every system in the hosts.conf.
It also generates links to the upsstats pages for each entry.  This
allows you to monitor many systems from a single page.

You can add "&refresh=nn" to your multimon.cgi URL if you want it to
do some meta tag magic to auto-reload the page every nn seconds.

 UPSSTATS
 ========

upsstats attempts to resemble APC's old Powerchute interface.  It
generates a simple HTML table that contains basic information about the
hardware and some status bars courtesy of upsimage.

 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.

 - "-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.

Post 1.0, the plan is to create two trees so that development can 
continue without affecting the stable line.  Once in awhile, the
development tree will be frozen and fixed up and then it will become the
new stable tree.  This is expected to follow the "odd/even middle
number" scheme of the Linux kernel.

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

Additional documentation can be found in the docs subdirectory.

If you want to create a new driver, be sure to look at skel.c and
main.c, as all drivers are now built around a common core.
new-drivers.txt in the docs directory also provides details on some of
the inner workings.

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.
