home *** CD-ROM | disk | FTP | other *** search
/ Freelog Special Edition 1: Linux / CD1.iso / doc / HOWTO / UPS-HOWTO < prev    next >
Text File  |  1998-10-14  |  141KB  |  3,697 lines

  1.   The UPS Howto
  2.   Harvey J. Stein, hjstein@bfr.co.il, Berger Financial Research, Ltd.
  3.   v2.42, 18 November 1997
  4.  
  5.   This document will help you connect an uninterruptable power supply to
  6.   a Linux box... if you're lucky...  Copyright (c) 1994, 1995, 1996,
  7.   1997 by Harvey J. Stein.  You may use this document as you see fit, as
  8.   long as it remains intact.  In particular, this notice (along with the
  9.   contributions below) must remain untouched.
  10.  
  11.   1.  Introduction
  12.  
  13.   This HOWTO covers connecting a UPS to a computer running Linux.  The
  14.   idea is to connect the two in such a way that Linux can shutdown
  15.   cleanly when the power goes out, and before the UPS's battery gives
  16.   out.
  17.  
  18.   This includes pointing out the existence of software packages which
  19.   aid in establishing such communications, and detailing exactly how
  20.   such communications are carried out.  The latter often is unnecessary
  21.   if you can find a software package that's already been configured for
  22.   your UPS.  Otherwise, you'll have to read on.
  23.  
  24.   To a large extent this document is even more redundant than when I
  25.   originally wrote it three years ago.  All the basic information has
  26.   always been contained in the powerd man page that comes with the
  27.   SysVinit package.  Whereas three years ago one could commonly find
  28.   Linux distributions which didn't even include this man page, I don't
  29.   believe this is the case any longer.
  30.  
  31.   Furthermore, when I first wrote this Howto, there was no software
  32.   other than powerd.c for Linux/UPS communications and control.  Today
  33.   there are quite afew UPS control packages available in Sunsite's UPS
  34.   directory <http://sunsite.unc.edu:/pub/Linux/system/ups>.
  35.  
  36.   None the less, I'm continuing to maintain the UPS Howto.  Why bother?
  37.   Well,
  38.  
  39.   ╖  An additional general overview might help to understand how to
  40.      connect a Linux system to a UPS, even if it's just the same
  41.      information written differently.
  42.  
  43.   ╖  The HOWTO is serving as a repository for UPS specific data - there
  44.      are many UPSs that haven't yet been incorporated into the general
  45.      packages.
  46.  
  47.   ╖  The HOWTO contains additional details that aren't available in
  48.      other documents.
  49.  
  50.   ╖  Some of the UPS software packages available in Sunsite's UPS
  51.      directory <http://sunsite.unc.edu:/pub/Linux/system/ups> seem to be
  52.      quite sparsely documented.  You might need to read this before you
  53.      can understand how to use them.
  54.  
  55.   ╖  This thing seems to have a life of it's own now.  It's clear when a
  56.      Howto should be born.  It's less clear when it should be put to
  57.      sleep.
  58.  
  59.   1.1.  Contributors
  60.  
  61.   I am forever indebted to those from whom I've received help,
  62.   suggestions, and UPS specific data.  The list includes:
  63.   ╖  Hennus Bergman (hennus@sky.owl.nl)
  64.  
  65.   ╖  Charli (mefistos@impsat1.com.ar)
  66.  
  67.   ╖  Ciro Cattuto (Ciro Cattuto)
  68.  
  69.   ╖  Nick Christenson (npc@minotaur.jpl.nasa.gov)
  70.  
  71.   ╖  Lam Dang (angit@netcom.com)
  72.  
  73.   ╖  Markus Eiden (Markus@eiden.de)
  74.  
  75.   ╖  Dan Fandrich (dan@fch.wimsey.bc.ca)
  76.  
  77.   ╖  Ben Galliart (bgallia@orion.it.luc.edu)
  78.  
  79.   ╖  Danny ter Haar (dth@cistron.nl)
  80.  
  81.   ╖  Christian G. Holtje (docwhat@uiuc.edu)
  82.  
  83.   ╖  Raymond A. Ingles (inglesra@frc.com)
  84.  
  85.   ╖  Peter Kammer (pkammer@ics.uci.edu)
  86.  
  87.   ╖  Marek Michalkiewicz (ind43@sun1000.ci.pwr.wroc.pl)
  88.  
  89.   ╖  Jim Ockers (ockers@umr.edu)
  90.  
  91.   ╖  Evgeny Stambulchik (fnevgeny@plasma-gate.weizmann.ac.il)
  92.  
  93.   ╖  Clive A. Stubbings (cas@vjet.demon.co.uk)
  94.  
  95.   ╖  Miquel van Smoorenburg (miquels@cistron.nl)
  96.  
  97.   ╖  Slavik Terletsky (ts@polynet.lviv.ua)
  98.  
  99.   ╖  Tom Webster (webster@kaiwan.com)
  100.  
  101.   Note that email addresses appearing below as excerpts from email
  102.   messages can be out of date.  The above is probably out of date too,
  103.   but some of it's more recent than what's below.
  104.  
  105.   Also, many apologies to anyone whom I've failed to note in this list.
  106.   Please email me and I'll add you.
  107.  
  108.   1.2.  Important disclaimer
  109.  
  110.   I really can't guarantee that any of this will work for you.
  111.   Connecting a UPS to a computer can be a tricky business.  One or the
  112.   other or both might burn out, blow up, catch fire, or start World War
  113.   Three.  Furthermore, I only have direct experience with the Advice
  114.   1200 A UPS, and a 5kva Best Ferrups, and I didn't have to make a
  115.   cable.  So, BE CAREFUL. GATHER ALL INFORMATION YOU CAN ON YOUR UPS.
  116.   THINK FIRST.  DON'T IMPLICITLY TRUST ANYTHING YOU READ HERE OR
  117.   ANYWHERE ELSE.
  118.  
  119.   On the other hand, I managed to get everything working with my UPSs,
  120.   without much information from the manufacturer, and without blowing
  121.   anything up, so it is possible.
  122.  
  123.   1.3.  Other documents
  124.  
  125.   This document does not cover the general features and capabilities of
  126.   UPSs.  For that type of information, you might turn to The UPS FAQ
  127.   <ftp://navigator.jpl.nasa.gov/pub/doc/faq/UPS.faq>.  It can also be
  128.   found at ftp://rtfm.mit.edu/pub/usenet-by-hierarchy/comp/answers/UPS-
  129.   faq.  It is maintained by Nick Christenson
  130.   (npc@minotaur.jpl.nasa.gov), but seems to have last been updated in
  131.   1995.  In email to him, he'd like that you put UPS or UPS FAQ or
  132.   something along these lines in the Subject line of the message.
  133.  
  134.   There're also more and more UPS manufactures sprouting up on the net.
  135.   Some of them actually supply useful information on their web sites.  A
  136.   convenient list of UPS manufacturers' web sites is available at The
  137.   UPS Directory <http://www.upssystems.uk.com/upsdir.html>.  Said site
  138.   also has a UPS FAQ <http://www.upssystems.uk.com/upsfaqs.html>.
  139.  
  140.   2.  Important note on obsolete information
  141.  
  142.   I've just discovered that some of the documentation below is obsolete.
  143.   In particular, the init daemon that comes with the latest sysvinit
  144.   package
  145.   <http://sunsite.unc.edu/pub/Linux/system/daemons/init/sysvinit-2.64.tar.gz>
  146.   is more sophisticated than I've portrayed it to be.  Although it seems
  147.   that the current version is backward compatible with what's written
  148.   here, it looks like it has some undocumented features which are very
  149.   important for UPS support.
  150.  
  151.   The control mechanism outlined below only allows powerd to give init
  152.   one of two messages, namely powerfail or powerok.  init runs one
  153.   command when it receives powerfail, and another when it receives
  154.   powerok.  This leads to complicated powerd logic for dealing with low
  155.   battery signals and other sorts of special situations.
  156.  
  157.   Newer versions of init (as of version 2.58, it seems) are more
  158.   sophisticated.  These versions can be signaled to run one of three
  159.   scripts.  Thus, init can have a powerfail script for announcing a
  160.   power outage, a powerfailnow script for doing an immediate shutdown,
  161.   and a powerok script for halting any pending shutdowns.  This is much
  162.   cleaner than the gyrations one would have to go through with the
  163.   mechanisms detailed below.
  164.  
  165.   Although most of the discussion here assumes the old init
  166.   communication method, I just added two new sections where the authors
  167.   uses the new communcation method.  These are sections ``Trust Energy
  168.   Protector 400/650'' and ``APC Smart-UPS 700''.  The former is
  169.   especially detailed.  Both include a powerd.c which signals init to do
  170.   an immediate shutdown when a low battery signal is received, as well
  171.   as the relevant /etc/inittab lines to make this work.  Other than
  172.   this, all I can tell you is to look at the source code for init.
  173.  
  174.   Also, for all I know, many of the software packages listed below also
  175.   use this newer communication method.
  176.  
  177.   3.  Smart and dumb UPSs.
  178.  
  179.   UPSs fall into two categories, which I'll call ``smart'' and ``dumb''.
  180.   The difference between the two is the amount of information one can
  181.   get from the UPS and the amount of control one can exert over the UPS.
  182.  
  183.      Dumb UPS
  184.  
  185.      ╖  Connects to the computer via serial port.
  186.  
  187.      ╖  Uses modem control lines to communicate with the computer.
  188.  
  189.      ╖  Can signal whether or not the power is out.
  190.  
  191.      ╖  Typically can signal whether or not the battery is low.
  192.  
  193.      ╖  The computer can usually signal the UPS to turn itself off.
  194.  
  195.      Smart UPS
  196.  
  197.      ╖  Connects to the computer via serial port.
  198.  
  199.      ╖  Communicates with the computer via normal data transfer through
  200.         the serial port.
  201.  
  202.      ╖  Typically has some sort of command language that the computer
  203.         can use to get various pieces of information from the UPS, to
  204.         set various operating parameters for the UPS, and to control the
  205.         UPS (such as turning it off).
  206.  
  207.   Usually smart UPSs can be operated in dumb mode.  This is useful
  208.   because as far as I know, the company which manufactures the most
  209.   popular smart UPS (namely APC) will only disclose the communication
  210.   protocol for their UPSs to people who sign a non-disclosure agreement.
  211.  
  212.   As far as I know, the only smart UPS available which is easy to
  213.   communicate with under Linux are those made by Best.  Furthermore,
  214.   BEST fully documents the smart mode (and the dumb mode) of their UPSs.
  215.   BEST also supplies source code for programs which can communicate with
  216.   their UPSs.
  217.  
  218.   All the packages listed in section ``Software'' will communicate with
  219.   a UPS in dumb mode.  This is all you really need.  The ones
  220.   specifically for the APC UPSs make various claims as to being usable
  221.   in smart mode, but I don't know exactly what they permit.  A full
  222.   implementation would give you a pop-up window with all sorts of fun
  223.   gauges displaying various statistics for the UPS, such as load,
  224.   internal temperature, fault history, input voltage, output voltage,
  225.   etc.  It seems like the smupsd-0.9-1.i386.rpm package (section
  226.   ``Software'') approaches this.  I'm not sure about the others.
  227.  
  228.   The rest of this document is pretty much confined to configuring your
  229.   system to work with a dumb UPS.  The general idea is about the same
  230.   with a smart UPS, but the details of how powerd would need to work and
  231.   what kind of cable you need are different for a smart UPS.
  232.  
  233.   4.  Software
  234.  
  235.   Basically, all you need is a working powerd binary, usually found in
  236.   /sbin/powerd.  This is usually part of the SysVinit package.  As far
  237.   as I know, all current Linux distributions include a recent version of
  238.   SysVinit.  Very old versions didn't include powerd.
  239.  
  240.   The only problem you might have is that your cable might not match how
  241.   powerd is set up, in which case you'll have to either rewire your
  242.   cable, or pick up a copy of powerd.c and modify it to work with your
  243.   cable.  Or, for that matter, you can always pick up one of the
  244.   following packages, most of which allow you to configure them to match
  245.   your cable.
  246.   As mentioned, an alternative to using the powerd that comes with the
  247.   SysVinit package would be to use one of the UPS packages now
  248.   available.  There are many packages currently available to aid in
  249.   setting up computer/ups communications.  None of this was available
  250.   when I first wrote this Howto, which is why I had to write it.  In
  251.   fact, there's a good chance that you might be able to use one of these
  252.   software packages, and avoid this Howto entirely!
  253.  
  254.   As of 15 March 1997 or so, Sunsite's UPS directory
  255.   <http://sunsite.unc.edu:/pub/Linux/system/ups> had quite a few
  256.   packages available.  Other sites seem to have UPS control packages
  257.   available too.  Here's what I've found to date (all but two from
  258.   sunsite):
  259.  
  260.      Enhanced_APC_BackUPS.tar.gz
  261.         <http://sunsite.unc.edu:/pub/Linux/system/ups/Enhanced_APC_BackUPS.tar.gz>
  262.         A package for controlling APC Smart UPSs.  Seems to basically
  263.         follow the BUPS Howto (included here), but also seems to have
  264.         some low battery warning support.
  265.  
  266.      Enhanced_APC_UPSD-v1.4.tar.gz
  267.         <http://sunsite.unc.edu:/pub/Linux/system/ups/Enhanced_APC_UPSD-
  268.         v1.4.tar.gz>
  269.         The .lsm file says that it's formerly the above package, but it
  270.         actually includes the above package as a .tar.gz file inside of
  271.         this tar.gz file!  The documentation is spotty.  It seems to
  272.         support APC UPSs in both smart mode and dumb mode, but I can't
  273.         be sure.
  274.  
  275.      apcd-0.5.tar.gz
  276.         <http://sunsite.unc.edu:/pub/Linux/system/ups/apcd-0.5.tar.gz>
  277.         Another package for controlling APC Smart UPSs.  Seems to
  278.         include some sort of master/slave support (i.e. - one machine
  279.         signals others to shut down when the power goes out).  Seems to
  280.         use the UPS in smart mode, as opposed to via modem signal line
  281.         toggling.
  282.  
  283.      smupsd-0.9-1.i386.rpm
  284.         <ftp://ftp.redhat.com/pub/contrib/i386/smupsd-0.9-1.i386.rpm>
  285.  
  286.      smupsd-0.9-1.src.rpm
  287.         <ftp://ftp.redhat.com/pub/contrib/i386/smupsd-0.9-1.src.rpm>
  288.         The author (David E. Myers, dem@netsco.com) writes:
  289.  
  290.         smupsd monitors an APC    Smart-UPS[TM] under Red Hat[TM] Linux.
  291.         Should power fail, smupsd will power down the system and the UPS
  292.         in an orderly fashion.
  293.  
  294.         smupsd has the following features:
  295.  
  296.      ╖  Shuts down the system and the UPS based on either remaining UPS
  297.         battery charge or elapsed time since power failure.
  298.  
  299.      ╖  UPS parameters can be monitored live from any host with the
  300.         graphical monitor program upsmon, written in JavaTM.
  301.  
  302.      ╖  UPS parameters can be logged to a file for analysis and
  303.         reporting.
  304.  
  305.      ╖  When additional systems share the same UPS, instances of smupsd
  306.         running on these systems can read UPS parameters from the one
  307.         running on the system serially connected to the UPS
  308.         (master/slave).
  309.  
  310.      ╖  Network access from remote hosts can be controlled via the
  311.         /etc/hosts.allow file.
  312.  
  313.      genpower-1.0.1.tgz
  314.         <http://sunsite.unc.edu:/pub/Linux/system/ups/genpower-1.0.1.tgz>
  315.         A general UPS handling package.  Includes configurations for
  316.         many UPSs - two TrippLite configurations, and three APC
  317.         configurations.  Includes good documentation.  A best buy.
  318.  
  319.      powerd-2.0.tar.gz
  320.         <http://sunsite.unc.edu:/pub/Linux/system/ups/powerd-2.0.tar.gz>
  321.         A replacement for the powerd that comes with the SysVinit
  322.         package.  As opposed to comments included in the documentation
  323.         it doesn't seem to have been merged into the SysVinit package as
  324.         of version 2.62.  Its advantages are that it can act as a server
  325.         for other powerds running on other machines (for when you have a
  326.         network of machines hanging off a single UPS), and it can be
  327.         configured by config file - the source code doesn't have to be
  328.         edited and recompiled.
  329.  
  330.      upsd-1.0.tgz
  331.         <http://sunsite.unc.edu:/pub/Linux/system/ups/upsd-1.0.tgz>
  332.         Another replacement for powerd.  Seems to be quite comparable in
  333.         features to powerd-2.0.tar.gz.
  334.  
  335.      checkups.tar
  336.         <http://www.bestpower.com/section/software/checkups.tar>
  337.         This package is for controlling Best UPSs.  It's direct from
  338.         Best's web site.  Includes binaries for lots of unix flavors,
  339.         but more importantly, it includes source code, so you can try it
  340.         out under Linux, and if it doesn't work, you can try to fix it.
  341.         The source code includes both ``basic checkups'' which controls
  342.         the UPS in dumb mode, and ``advanced checkups'' which is a
  343.         little more sophisticated - it will signal a shutdown when the
  344.         UPS says it has X minutes of power remaining instead of just
  345.         shutting down X minutes after the power goes out.  The advanced
  346.         checkups program also will shut down when the UPS registers
  347.         various alarms such as High Ambient Temperature, Near Low
  348.         Battery, Low AC Out, or User Test Alarm.
  349.  
  350.      bestups-0.9.tar.gz
  351.         <http://sunsite.unc.edu:/pub/Linux/system/ups/bestups-0.9.tar.gz>
  352.         A package that might very well be on sunsite by the time you
  353.         read this.  It's a pair of communications module which works
  354.         with Best Ferrups UPSs.  It operates the UPS in smart mode.  It
  355.         inter-operates well with powerd-2.0 - useful if you have a big
  356.         Best Ferrups UPS keeping up all the machines on a network.
  357.  
  358.         NOTE - This package has yet to be uploaded to Sunsite.  I keep
  359.         begging the author to finish and upload it, but he has yet to
  360.         find the time.
  361.  
  362.      LanSafe III <http://www.deltecpower.com/soft.html>
  363.         Deltec Electronics (and Exide) sell a software package called
  364.         LanSafe III.  They have a Linux version.  It comes with their
  365.         UPSs.  They also say that it works with other UPSs (on the dumb
  366.         level).
  367.  
  368.      apcupsd-2.8.tar.gz
  369.         <http://sunsite.unc.edu:/pub/Linux/system/ups/apcupsd-2.8.tar.gz>
  370.         The author (Andre Hedrick, hedrick@astro.dyer.vanderbilt.edu)
  371.         writes:
  372.  
  373.         apcupsd-2.1.tar.gz replaces Enhanced-APC-UPSD.tar.gz
  374.  
  375.         It is a very complete package for APC UPSs.  There is support
  376.         for the entire range of UPSs in their product line.  I have now
  377.         added smart mode signaling to the package and support with APC's
  378.         own cables or a custom cable if you don't have an APC cable that
  379.         is supported to date.
  380.  
  381.      smartups-1.1.tgz
  382.         <http://sunsite.unc.edu:/pub/Linux/system/ups/smartups-1.1.tgz>
  383.         From the LSM:
  384.  
  385.         A powerd and an X11 graphing utility which shows you the
  386.         voltages, frequencies, load percentage and battery level in
  387.         realtime. The protocol that the "Safeware" software uses, and
  388.         "Tripplite" UPSs are supported. Source + ELF binaries.
  389.  
  390.      ups.tar.gz
  391.         <http://sunsite.unc.edu:/pub/Linux/system/ups/ups.tar.gz>
  392.         From the LSM:
  393.  
  394.         Program to interact with battery backups (Powerbox UPS).
  395.  
  396.      usvd-2.0.0.tgz
  397.         <http://sunsite.unc.edu:/pub/Linux/system/usvd-2.0.0/usvd-2.0.0.tgz>
  398.         From the LSM:
  399.  
  400.         usvd is a daemon that monitors the state of an uninterrupted
  401.         power supply and reacts upon state changes (line fail, line
  402.         back, battery low situations). You can write your own scripts
  403.         that are called in these cases. It does *not* require SYSVINIT.
  404.  
  405.   Note that I've only glanced at these packages.  I haven't used them.
  406.   We were just about to start using bestups-0.9.tar.gz
  407.   <http://sunsite.unc.edu:/pub/Linux/system/ups/bestups-0.9.tar.gz> in
  408.   conjunction with powerd-2.0.tar.gz
  409.   <http://sunsite.unc.edu:/pub/Linux/system/ups/powerd-2.0.tar.gz>, but
  410.   we never quite got around to it.
  411.  
  412.   5.  Do it yourself guide
  413.  
  414.   This discussion is specifically tailored for dumb UPS control.
  415.   However, most of the process is about the same for dumb UPSs and smart
  416.   UPSs.  The biggest difference is in the details of how the UPS
  417.   monitoring daemon (typically powerd) communicates with the UPS.
  418.  
  419.   Before doing anything, I suggest the following algorithm:
  420.  
  421.   ╖  Skim this document.
  422.  
  423.   ╖  Download and investigate all packages which seem specifically
  424.      tailored to your UPS.
  425.  
  426.   ╖  Download and investigate the more generic packages.  Note that some
  427.      of the more generic packages are actually more powerful, better
  428.      documented, and easier to use than their more specific
  429.      counterparts.
  430.  
  431.   ╖  If you still can't get things working, or if points are still
  432.      unclear, read this document more carefully, and hack away...
  433.  
  434.   5.1.  What you need to do (summary)
  435.  
  436.   ╖  Plug the computer into the UPS.
  437.  
  438.   ╖  Connect the computer's serial port to the UPS with a special cable.
  439.  
  440.   ╖  Run powerd (or some sort of equivalent) on the computer.
  441.  
  442.   ╖  Setup your init to do something reasonable on powerfail and powerok
  443.      events (like start a shutdown and kill any currently running
  444.      shutdowns, respectively, for example).
  445.  
  446.   5.2.  How it's supposed to work
  447.  
  448.      UPS's job
  449.         When the power goes out, the UPS continues to power the computer
  450.         and signals that the power went out by throwing a relay or
  451.         turning on an opticoupler on it's control port.
  452.  
  453.      Cable's job
  454.         The cable is designed so that when the UPS throws said relay,
  455.         this causes a particular serial port control line (typically
  456.         DCD) to go high.
  457.  
  458.      Powerd's job
  459.         The powerd daemon monitors the serial port.  Keeps
  460.         raised/lowered whatever serial port control lines the UPS needs
  461.         to have raised/lowered (typically, DTR must be kept high and
  462.         whatever line shuts off the UPS must be kept low).  When powerd
  463.         sees the UPS control line go high, it writes FAIL to
  464.         /etc/powerstatus and sends the init process a SIGPWR signal.
  465.         (Older versions of powerd and initd wrote to /etc/powerfail.)
  466.         When the control line goes low again, it writes OK to
  467.         /etc/powerstatus and sends init a SIGPWR signal.
  468.  
  469.      Init's job (aside from everything else it does)
  470.         When it receives a SIGPWR, it looks at /etc/powerstatus.  If it
  471.         contains FAIL it runs the powerfail entry from /etc/inittab.  If
  472.         it contains OK it runs the powerokwait entry from inittab.
  473.  
  474.   5.3.  How to set things up
  475.  
  476.   The following presupposes that you have a cable that works properly
  477.   with powerd.  If you're not sure that your cable works (or how it
  478.   works), see section ``Reverse-engineering cables and hacking
  479.   powerd.c'' for information on dealing with poorly described cables and
  480.   reconfiguring powerd.c.  Sections ``Serial port pin assignments'' and
  481.   ``Ioctl to RS232 correspondence'' will also be useful.
  482.  
  483.   If you need to make a cable, see section ``How to make a cable'' for
  484.   the overall details, and the subsection of section ``Info on selected
  485.   UPSs'' that refers to your UPS.  The latter might also include
  486.   information on manufacturer supplied cables.  You may want to at least
  487.   skim all of section ``Info on selected UPSs'' because each section has
  488.   a few additional generally helpful details.
  489.  
  490.   ╖  Edit /etc/inittab.  Put in something like this:
  491.  
  492.      # What to do when power fails (Halt system & drain battery :):
  493.      pf::powerfail:/etc/powerfailscript +5
  494.  
  495.      # If power is back before shutdown, cancel the running shutdown.
  496.      pg:0123456:powerokwait:/etc/powerokscript
  497.  
  498.   ╖  Write scripts /etc/powerfailscript and /etc/powerokscript to
  499.      shutdown in 5 minutes (or whatever's appropriate) and kill any
  500.      existing shutdown, respectively.  Depending on the version of
  501.      shutdown that you're using, this will be either so trivial that
  502.      you'll dispense with the scripts, or be a 1 line bash script,
  503.      something along the lines of:
  504.  
  505.          kill `ps -aux | grep "shutdown" | grep -v grep | awk '{print $2}'`
  506.  
  507.   and you'll keep the scripts.  (In case it doesn't come out right, the
  508.   first single quote on the above line is a backquote, the second and
  509.   third are single quotes, and the last is also a backquote.)
  510.  
  511.   ╖  Tell init to re-process the inittab file with the command:
  512.  
  513.          telinit q
  514.  
  515.   ╖  Edit rc.local so that powerd gets run upon startup.  The syntax is:
  516.  
  517.          powerd <line>
  518.  
  519.   Replace <line> with the serial port that the UPS is connected, such as
  520.   /dev/cua1.
  521.  
  522.   ╖  Connect computer's serial port to UPS's serial port.  DO NOT PLUG
  523.      THE COMPUTER INTO UPS YET.
  524.  
  525.   ╖  Plug a light into the UPS.
  526.  
  527.   ╖  Turn on the UPS and the light.
  528.  
  529.   ╖  Run powerd.
  530.  
  531.   ╖  Test the setup:
  532.  
  533.   ╖  Yank the UPS's plug.
  534.  
  535.   ╖  Check that the light stays on.
  536.  
  537.   ╖  Check that /etc/powerfailscript runs.
  538.  
  539.   ╖  Check that shutdown is running.
  540.  
  541.   ╖  Plug the UPS back in.
  542.  
  543.   ╖  Check that the light stays on.
  544.  
  545.   ╖  Check that /etc/powerokscript runs.
  546.  
  547.   ╖  Check that /etc/powerfailscript is not running.
  548.  
  549.   ╖  Check that shutdown is no longer running.
  550.  
  551.   ╖  Yank the UPS's plug again.  Leave it out and make sure that the
  552.      computer shuts down properly in the proper amount of time.
  553.  
  554.   ╖  The Dangerous Part. After everything seems to be proper, power down
  555.      the computer and plug it into the UPS.  Run a script that sync's
  556.      the hard disk every second or so.  Simultaneously run a second
  557.      script that keeps doing a find over your entire hard disk.  The
  558.      first is to make this a little safer and the second is to help draw
  559.      lots of power.  Now, pull the plug on the UPS, check again that
  560.      shutdown is running and wait.  Make sure that the computer shuts
  561.      down cleanly before the battery on the UPS gives out.  This is
  562.      dangerous because if the power goes out before the computer shuts
  563.      down, you can end up with a corrupt file system, and maybe even
  564.      lose all your files.  You'll probably want to do a full backup
  565.      before this test, and set the shutdown time extremely short to
  566.      begin with.
  567.  
  568.   Congratulations!  You now have a Linux computer that's protected by a
  569.   UPS and will shutdown cleanly when the power goes out!
  570.  
  571.   5.4.  User Enhancements
  572.  
  573.   ╖  Hack powerd.c to monitor the line indicating that the batteries are
  574.      low.  When the batteries get low, do an immediate shutdown.
  575.  
  576.   ╖  Modify the shutdown procedure so that if it's shutting down in a
  577.      powerfail situation, then it turns off the UPS after doing
  578.      everything necessary.
  579.  
  580.   6.  Hardware notes
  581.  
  582.   6.1.  How to make a cable
  583.  
  584.   This section is just from messages I've seen on the net.  I haven't
  585.   done it so I can't write from experience.  If anyone has, please write
  586.   this section for me :).  See also the message about the GPS1000
  587.   contained in section ``GPS1000 from ACCODATA'', not to mention all the
  588.   UPS specific data in section ``Info on selected UPSs''.
  589.  
  590.      >From miquels@caution.cistron.nl.mugnet.org Wed Jul 21 14:26:33 1993
  591.      Newsgroups: comp.os.linux
  592.      Subject: Re: UPS interface for Linux?
  593.      From: miquels@caution.cistron.nl.mugnet.org (Miquel van Smoorenburg)
  594.      Date: Sat, 17 Jul 93 18:03:37
  595.      Distribution: world
  596.      Organization: Cistron Electronics.
  597.  
  598.      In article <1993Jul15.184450.5193@excaliber.uucp>
  599.      joel@rac1.wam.umd.edu (Joel M. Hoffman) writes:
  600.      >I'm in the process of buying a UPS (Uninteruptable Power Supply), and
  601.      >notice that some of them have interfaces for LAN's to signal the LAN
  602.      >when the power fails.
  603.      >
  604.      >Is there such an interface for Linux?
  605.      >
  606.      >Thanks.
  607.      >
  608.      >-Joel
  609.      >(joel@wam.umd.edu)
  610.      >
  611.  
  612.      When I worked on the last versioon of SysVinit (Now version 2.4),
  613.      I temporarily had a UPS on my computer, so I added support for it.
  614.      You might have seen that in the latest <signal.h> header files there
  615.      is a #define SIGPWR 30 now :-). Anyway, I did not have such a special
  616.      interface but the output of most UPS's is just a relais that makes or breaks
  617.      on power interrupt. I thought up a simple way to connect this to the
  618.      DCD line of the serial port. In the SysVinit package there is a daemon
  619.      called 'powerd' that keeps an eye on that serial line and sends SIGPWR
  620.      to init when the status changes, so that init can do something (such as
  621.      bringing the system down within 5 minutes). How to connect the UPS to
  622.      the serial line is described in the source "powerd.c", but I will
  623.      draw it here for explanation:
  624.  
  625.                           +------------------------o  DTR
  626.                           |
  627.                         +---+
  628.                         |   | resistor
  629.                         |   | 10 kilo-Ohm
  630.                         |   |
  631.                         +---+                                To serial port.
  632.                           |
  633.             +-----o-------+------------------------o  DCD
  634.             |             |
  635.             o  UPS        |
  636.           \    relais     |
  637.            \              |
  638.             |             |
  639.             +-----o-------+------------------------o  GND
  640.  
  641.      Nice drawing eh?
  642.  
  643.      Hope this helps.
  644.      SysVinit can be found on sunsite (and tsx-11 probably) as
  645.      SysVinit2.4.tar.z
  646.  
  647.      Mike.
  648.  
  649.      --
  650.  
  651.      Miquel van Smoorenburg, <miquels@cistron.nl.mugnet.org>
  652.      Ibmio.com: cannot open CONFIG.SYS: file handle broke off.
  653.  
  654.      >From danny@caution.cistron.nl.mugnet.org Wed Jul 21 14:27:04 1993
  655.      Newsgroups: comp.os.linux
  656.      Subject: Re: UPS interface for Linux?
  657.      From: danny@caution.cistron.nl.mugnet.org (Danny ter Haar)
  658.      Date: Mon, 19 Jul 93 11:02:14
  659.      Distribution: world
  660.      Organization: Cistron Electronics.
  661.  
  662.      In article <9307174330@caution.cistron.nl.mugnet.org>
  663.      miquels@caution.cistron.nl.mugnet.org (Miquel van Smoorenburg) writes:
  664.      >How to connect the UPS to the serial line is described in the source
  665.      >"powerd.c", but I will draw it here for explanation:
  666.  
  667.      The drawing wasn't really clear, please use this one in stead !
  668.      >
  669.      >                     +------------------------o  DTR
  670.      >                     |
  671.      >                   +---+
  672.      >                   |   | resistor
  673.      >                   |   | 10 kilo-Ohm
  674.      >                   |   |
  675.      >                   +---+                                To serial port.
  676.      >                     |
  677.      >       +-----o-------+------------------------o  DCD
  678.      >       |
  679.      >       o  UPS
  680.      >     \    relais
  681.      >      \
  682.      >       |
  683.      >       +-----o--------------------------------o  GND
  684.      >
  685.  
  686.      The DTR is kept high, when the UPS's power input is gone it
  687.      will close the relais . The computer is monitoring
  688.      the DCD input port to go LOW . When this happens it will start a
  689.      shutdown sequence...
  690.  
  691.      _____
  692.      Danny
  693.  
  694.      --
  695.      <=====================================================================>
  696.      Danny ter Haar  <dannyth@hacktic.nl> or <danny@cistron.nl.mugnet.org>
  697.      Robins law #103: 'a couple of lightyears can't part good friends'
  698.  
  699.   6.2.  Reverse-engineering cables and hacking powerd.c
  700.  
  701.   Try to get documentation for the cables that your UPS seller supplies.
  702.   In particular find out:
  703.  
  704.   ╖  What lines need to be kept high.
  705.  
  706.   ╖  What line(s) turn off the UPS.
  707.  
  708.   ╖  What lines the UPS toggles to indicate that:
  709.  
  710.   ╖  Power is out.
  711.  
  712.   ╖  Battery is low.
  713.  
  714.   You then need to either hack powerd.c appropriately, or use one of the
  715.   above configurable packages (see the packages genpower-1.0.1.tgz,
  716.   powerd-2.0.tar.gz, or upsd-1.0.tgz described in section ``Software'').
  717.   If you use one of the packages, follow the instructions there.  If you
  718.   want to hack powerd.c, keep reading.
  719.  
  720.   If you have trouble getting the above information, or just want to
  721.   check it (a good idea) the following program  might help.  It's a
  722.   hacked version of powerd.c.  It allows you to set the necessary port
  723.   flags from the command line and then monitors the port, displaying the
  724.   control lines every second.  I used it as ``upscheck /dev/cua1 2''
  725.   (for example) to set the 2nd bit (DTR) and to clear the other bits.
  726.   The number base 2 indicates which bits to set, so for example to set
  727.   bits 1, 2 and 3, (and clear the others) use 7.  See the code for
  728.   details.
  729.  
  730.   Here's the (untested) upscheck.c program.  It's untested because I
  731.   edited the version I originally used to make it clearer, and can't
  732.   test the new version at the moment.
  733.  
  734.   /*
  735.    * upscheck     Check how UPS & computer communicate.
  736.    *
  737.    * Usage:       upscheck <device> <bits to set>
  738.    *              For example, upscheck /dev/cua4 4 to set bit 3 &
  739.    *              monitor /dev/cua4.
  740.    *
  741.    * Author:      Harvey J. Stein <hjstein@math.huji.ac.il>
  742.    *              (but really just a minor modification of Miquel van
  743.    *              Smoorenburg's <miquels@drinkel.nl.mugnet.org> powerd.c
  744.    *
  745.    * Version:     1.0 19940802
  746.    *
  747.    */
  748.   #include <sys/types.h>
  749.   #include <sys/ioctl.h>
  750.   #include <fcntl.h>
  751.   #include <errno.h>
  752.   #include <stdlib.h>
  753.   #include <unistd.h>
  754.   #include <stdio.h>
  755.   #include <signal.h>
  756.  
  757.   /* Main program. */
  758.   int main(int argc, char **argv)
  759.   {
  760.     int fd;
  761.  
  762.   /*  These TIOCM_* parameters are defined in <linux/termios.h>, which  */
  763.   /*  is indirectly included here.                                      */
  764.     int dtr_bit = TIOCM_DTR;
  765.     int rts_bit = TIOCM_RTS;
  766.     int set_bits;
  767.     int flags;
  768.     int status, oldstat = -1;
  769.     int count = 0;
  770.     int pc;
  771.  
  772.     if (argc < 2) {
  773.           fprintf(stderr, "Usage: upscheck <device> <bits-to-set>\n");
  774.           exit(1);
  775.     }
  776.  
  777.     /* Open monitor device. */
  778.     if ((fd = open(argv[1], O_RDWR | O_NDELAY)) < 0) {
  779.       fprintf(stderr, "upscheck: %s: %s\n", argv[1], sys_errlist[errno]);
  780.       exit(1);}
  781.  
  782.     /* Get the bits to set from the command line. */
  783.     sscanf(argv[2], "%d", &set_bits);
  784.  
  785.     while (1) {
  786.       /* Set the command line specified bits (& only the command line */
  787.       /* specified bits).                                             */
  788.       ioctl(fd, TIOCMSET, &set_bits);
  789.       fprintf(stderr, "Setting %o.\n", set_bits);
  790.  
  791.       sleep(1);
  792.  
  793.       /* Get the current line bits */
  794.       ioctl(fd, TIOCMGET, &flags);
  795.       fprintf(stderr, "Flags are %o.\n", flags);
  796.  
  797.   /*  Fiddle here by changing TIOCM_CTS to some other TIOCM until    */
  798.   /*  this program detects that the power goes out when you yank     */
  799.   /*  the plug on the UPS.  Then you'll know how to modify powerd.c. */
  800.       if (flags & TIOCM_CTS)
  801.         {
  802.           pc = 0 ;
  803.           fprintf(stderr, "power is up.\n");
  804.         }
  805.       else
  806.         {
  807.           pc = pc + 1 ;
  808.           fprintf(stderr, "power is down.\n");
  809.         }
  810.       }
  811.  
  812.     close(fd);
  813.   }
  814.  
  815.   6.3.  Serial port pin assignments
  816.  
  817.   The previous section presupposes knowledge of the correspondence
  818.   between terminal signals and serial port pins.  Here's a reference for
  819.   that correspondence, taken from David Tal's ``Frequently Used Cables
  820.   and Connectors'' document.  I'm including a diagram illustrating the
  821.   connectors, and a table listing the correspondence between pin numbers
  822.   and terminal line signals.
  823.  
  824.   If you need a general reference for cable wiring, connectors, etc,
  825.   then David Tal's would be a good one, but I can't seem to locate this
  826.   document on the net any more.  But I've found a good replacement.
  827.   It's The Hardware Book <http://www.blackdown.org/~hwb/hwb.html>.
  828.  
  829.   Other useful sites:
  830.  
  831.   ╖  Yost Serial Device Wiring Standard
  832.      <http://star.sols.pt/docs/yost.html> which contains interesting
  833.      information on how to use RJ-45 jacks and eight wire cables for all
  834.      serial port connections.
  835.  
  836.   ╖  Stokely Consulting <http://www.stokely.com/stokely> for general
  837.      Unix info, and in particular their Unix Serial Port Resources.
  838.  
  839.   ╖  Unix Workstation System Administration Education Certification
  840.      <http://www.uwsg.indiana.edu/usail/edcert/> which contains RS-232:
  841.      Connectors and Cabling
  842.      <http://www.uwsg.indiana.edu/usail/peripherals/serial/rs232/>
  843.  
  844.   Incidentally, it seems that the Linuxdoc-sgml package still doesn't
  845.   format tables very well in the html output.  If you want to be able to
  846.   read the following table, you're probably going to have to look at
  847.   either the DVI version or the plain text version of this document.
  848.  
  849.   |       |        |        |       |         |          |                                |
  850.   |DB-25  |  DB-9  |  Name  |  EIA  |  CCITT  |  DTE-DCE |  Description                   |
  851.   |Pin #  |  Pin # |        |       |         |          |                                |
  852.   |________|_________|_________|________|__________|___________|_______________                  |
  853.   |1      |        |  FG    |  AA   |  101    |  ---     | Frame Ground/Chassis GND       |
  854.   |2      |  3     |  TD    |  BA   |  103    |  --->    | Transmitted Data, TxD          |
  855.   |3      |  2     |  RD    |  BB   |  104    |  <---    |  Received Data, RxD            |
  856.   |4      |  7     |  RTS   |  CA   |  105    |  --->    | Request To Send                |
  857.   |5      |  8     |  CTS   |  CB   |  106    |  <---    |  Clear To Send                 |
  858.   |6      |  6     |  DSR   |  CC   |  107    |  <---    |  Data Set Ready                |
  859.   |7      |  5     |  SG    |  AB   |  102    |  ----    | Signal Ground, GND             |
  860.   |8      |  1     |  DCD   |  CF   |  109    |  <---    |  Data Carrier Detect           |
  861.   |9      |        |  --    |  --   |  -      |  -       | Positive DC test voltage       |
  862.   |10     |        |  --    |  --   |  -      |  -       | Negative DC test voltage       |
  863.   |11     |        |  QM    |  --   |  -      |  <---    |  Equalizer mode                |
  864.   |12     |        |  SDCD  |  SCF  |  122    |  <---    |  Secondary Data Carrier Detect |
  865.   |13     |        |  SCTS  |  SCB  |  121    |  <---    |  Secondary Clear To Send       |
  866.   |14     |        |  STD   |  SBA  |  118    |  --->    | Secondary Transmitted Data     |
  867.   |15     |        |  TC    |  DB   |  114    |  <---    |  Transmitter (signal) Clock    |
  868.   |16     |        |  SRD   |  SBB  |  119    |  <---    |  Secondary Receiver Clock      |
  869.   |17     |        |  RC    |  DD   |  115    |  --->    | Receiver (signal) Clock        |
  870.   |18     |        |  DCR   |  --   |  -      |  <---    |  Divided Clock Receiver        |
  871.   |19     |        |  SRTS  |  SCA  |  120    |  --->    | Secondary Request To Send      |
  872.   |20     |  4     |  DTR   |  CD   |  108.2  |  --->    | Data Terminal Ready            |
  873.   |21     |        |  SQ    |  CG   |  110    |  <---    |  Signal Quality Detect         |
  874.   |22     |  9     |  RI    |  CE   |  125    |  <---    |  Ring Indicator                |
  875.   |23     |        |  --    |  CH   |  111    |  --->    | Data rate selector             |
  876.   |24     |        |  --    |  CI   |  112    |  <---    |  Data rate selector            |
  877.   |25     |        |  TC    |  DA   |  113    |  <---    |  Transmitted Clock             |
  878.  
  879.       Pin Assignment for the Serial Port (RS-232C), 25-pin and 9-pin
  880.           1                         13         1         5
  881.         _______________________________      _______________
  882.         \  . . . . . . . . . . . . .  /      \  . . . . .  /    RS232-connectors
  883.          \  . . . . . . . . . . . .  /        \  . . . .  /     seen from outside
  884.           ---------------------------          -----------      of computer.
  885.           14                      25            6       9
  886.  
  887.      DTE : Data Terminal Equipment (i.e. computer)
  888.      DCE : Data Communications Equipment (i.e. modem)
  889.      RxD : Data received; 1 is transmitted "low", 0 as "high"
  890.      TxD : Data sent; 1 is transmitted "low", 0 as "high"
  891.      DTR : DTE announces that it is powered up and ready to communicate
  892.      DSR : DCE announces that it is ready to communicate; low=modem hangup
  893.      RTS : DTE asks DCE for permission to send data
  894.      CTS : DCE agrees on RTS
  895.      RI  : DCE signals the DTE that an establishment of a connection is attempted
  896.      DCD : DCE announces that a connection is established
  897.  
  898.   6.4.  Ioctl to RS232 correspondence
  899.  
  900.   Since you also might need to modify powerd.c to raise and lower the
  901.   correct lines, you might also need the numeric values of different
  902.   terminal signals.  The can be found in /usr/include/linux/termios.h,
  903.   but are reproduced here for reference.  Since they could change,
  904.   you're best off confirming these values against said file.
  905.  
  906.   /* modem lines */
  907.   #define TIOCM_LE        0x001
  908.   #define TIOCM_DTR       0x002
  909.   #define TIOCM_RTS       0x004
  910.   #define TIOCM_ST        0x008
  911.   #define TIOCM_SR        0x010
  912.   #define TIOCM_CTS       0x020
  913.   #define TIOCM_CAR       0x040
  914.   #define TIOCM_RNG       0x080
  915.   #define TIOCM_DSR       0x100
  916.   #define TIOCM_CD        TIOCM_CAR
  917.   #define TIOCM_RI        TIOCM_RNG
  918.  
  919.   Note that the 3rd column is in Hex.
  920.  
  921.   7.  What to do when you're really stuck
  922.  
  923.   Here's a novel solution to UPS control for when the UPS and the
  924.   computer just aren't on speaking terms.  I must say that every time I
  925.   read this, I'm struck by how clever a solution it is.
  926.  
  927.   From: " Raymond A. Ingles" <inglesra@frc.com>
  928.   To: hjstein@math.huji.ac.il
  929.   Subject: UPS HOWTO tip
  930.   Date: Mon, 24 Feb 1997 11:48:32 -0500 (EST)
  931.  
  932.    I don't know if others would find this useful, but I thought I might
  933.   pass this along for possible inclusion in the HOWTO. Thanks for
  934.   maintaining a HOWTO that I found very useful!
  935.  
  936.   -----------------
  937.  
  938.    My fiancee bought me a UPS as a present, a Tripp-Lite 400, I believe. It
  939.   was very welcome and seems to operate as expected, but unfortunately
  940.   doesn't have a serial interface to let the computer know the line power
  941.   has failed. It's apparently intended for home or office use where the
  942.   computer will not be left unattended.
  943.  
  944.    This, of course, was unacceptable and I began working on a line monitor,
  945.   planning on opening up the case and figuring out how to add the hardware
  946.   that the manufacturer had left out. Then I realized that there was a
  947.   quicker and simpler and cheaper (if somewhat less functional) way.
  948.  
  949.    I had an old 2400 baud modem that I wasn't using, and hooked it up to an
  950.   unused serial port on my computer. I then plugged the modem into a surge
  951.   supressor plugged into the wall power. I set up powerd with the options
  952.   as follows:
  953.  
  954.   -----
  955.   serialline    /dev/ttyS1
  956.   monitor     DCD
  957.   failwhen    low
  958.   -----
  959.  
  960.    Now, when the wall power fails (or, since that hasn't happened lately,
  961.   when I pull the surge supressor from the wall to test this setup) the modem
  962.   fails but the UPS starts supplying power to the computer. When powerd
  963.   notices the modem has dropped DCD, it triggers the powerfail sequence.
  964.  
  965.    Obviously, this has some limitations. You can't tell from the modem when
  966.   the battery is low and so on. You can only tell that the wall power has
  967.   failed. Still, it's certainly cheap and I hate to see functioning
  968.   computer equipment lie unused. These days you should be able to get a
  969.   2400 baud modem for very nearly free.
  970.  
  971.    I'd still suggest getting a real UPS with full communication capability.
  972.   But if you're stuck with a less-functional one, this may at least make it
  973.   useful.
  974.  
  975.    Sincerely,
  976.  
  977.    Ray Ingles               (810) 377-7735                inglesra@frc.com
  978.  
  979.    "Anybody who has ever seen a photograph showing the kind of damage that
  980.   a trout traveling that fast can inflict on the human skull knows that
  981.   such photographs are very valuable. I paid $20 for mine." - Dave Barry
  982.  
  983.   8.  Info on selected UPSs
  984.  
  985.   This section contains UPS specific information.  What I'd like is to
  986.   have the UPS control port information (what each pin does and needs to
  987.   have done), information on the manufacturer supplied cable (what it
  988.   connects where), and a hacked version of powerd.c which works with the
  989.   UPS.  What I currently have is fairly complete descriptions of setting
  990.   up each UPS.  I'd try to distill out the relevant information, but
  991.   since I can't test each UPS, it's hard to decide exactly what's
  992.   relevant.  Furthermore, each UPS seems to have some additional quirks
  993.   that are nicely described by the authors of each section.  So for now
  994.   I'm leaving everything in.  Makes for a hefty Howto.
  995.  
  996.   Please send me your experiences for inclusion here.
  997.  
  998.   8.1.  General Experiences.
  999.  
  1000.   I've been saving peoples comments, but haven't gotten permission yet
  1001.   to include them here.  Here's a general summary of what I've heard
  1002.   from people.
  1003.  
  1004.   APC: Won't release info on their smart mode without your signature on
  1005.   a non-disclosure agreement.  Thus, people are forced to run their
  1006.   smart UPSs in the dumb mode as outlined above.  Various people have
  1007.   had varying amounts of success reverse engineering
  1008.  
  1009.   Best: Helpful and friendly.  Supply source code and documentation both
  1010.   for dumb modes and smart modes.
  1011.  
  1012.   TrippLite: One person reported that TrippLite won't release info
  1013.   either.
  1014.  
  1015.   Upsonic: One person reported that Upsonic has discussed technical
  1016.   details over the phone, answered questions via fax and are generally
  1017.   helpful.
  1018.  
  1019.   8.2.  Advice 1200 A
  1020.  
  1021.   UPS from Advice Electronics, Tel Aviv Israel (they stick their own
  1022.   name on the things).
  1023.  
  1024.   I don't recommend them.  Our experiences with them have been very bad.
  1025.   We've twice had a 17" monitor fry when the power failed.  We've had
  1026.   computers spontaneously reboot when the power failed.
  1027.  
  1028.   None the less, for completeness, here's he UPS Control Port's pin
  1029.   specifications.
  1030.  
  1031.   ╖  2 - Power Fail.
  1032.  
  1033.   ╖  5 - Battery Low.
  1034.  
  1035.   ╖  6 - Shut Down UPS.
  1036.  
  1037.   ╖  4 - Common ground for pin 2, 5, 6.
  1038.  
  1039.   They also gave me the following picture which didn't help me, but may
  1040.   help you if you want to build a cable yourself:
  1041.  
  1042.            2 ----------+
  1043.                        |
  1044.                        \
  1045.                         \|
  1046.                          |--------------
  1047.                         /|
  1048.                       \/      <--- The "\/" here indicates the type of
  1049.                       |            this transister.  I forget what
  1050.                       |            denotes what, but this one points
  1051.                    +-----+         away from the center line.
  1052.                   /  /  /
  1053.  
  1054.            5 ----------+
  1055.                        |
  1056.                        \
  1057.                         \|
  1058.                          |--------------
  1059.                         /|
  1060.                       \/
  1061.                       |
  1062.                       |
  1063.                    +-----+
  1064.                   /  /  /
  1065.  
  1066.                          +-------------
  1067.                          |
  1068.                          /
  1069.                 10K    |/
  1070.            6 --\/\/\/--|
  1071.                        |\
  1072.                          \/
  1073.                          |
  1074.                          |
  1075.                       +-----+
  1076.                      /  /  /
  1077.  
  1078.            4 ----------+
  1079.                        |
  1080.                        |
  1081.                     +-----+
  1082.                    /  /  /
  1083.  
  1084.   Cable supplied
  1085.  
  1086.   They first gave me a cable that was part of a DOS UPS control package
  1087.   called RUPS.  I used this for testing.  When I was satisfied, they
  1088.   gave me a cable they use for Netware servers connected to UPSs.  It
  1089.   functioned identically.  Here are the details:
  1090.  
  1091.   ╖  DTR - Powers cable (make powerd.c keep it high).
  1092.  
  1093.   ╖  CTS - Power out (stays high and goes low when power goes out).
  1094.  
  1095.   ╖  DSR - Battery low (stays high.  Goes low when battery does).
  1096.  
  1097.   ╖  RTS - Turns off UPS (keep it low.  Set it high to turn off UPS).
  1098.  
  1099.   (The powerd.c that comes with SysVinit set or left RTS high, causing
  1100.   the UPS to shut off immediately when powerd was started up!)
  1101.   8.3.  Trust Energy Protector 400/650
  1102.  
  1103.   This section is good for more than just the Trust Energy Protector.
  1104.   It illustrates how to work with the new features of init.
  1105.  
  1106.   How to use a Trust Energy Protector 400/650 under Linux
  1107.  
  1108.   by Ciro Cattuto <mailto:ciro@stud.unipg.it>
  1109.  
  1110.   Version 1.0 - 31 March 1997
  1111.  
  1112.   8.3.1.  The computer to UPS connection
  1113.  
  1114.   The Trust Energy Protector 400/650 is equipped with a remote signal
  1115.   port.  Using a properly designed cable, it is possible to connect the
  1116.   UPS port to the serial port of a computer, thus making it aware of
  1117.   power failure events.
  1118.  
  1119.   8.3.1.1.  The UPS signal port
  1120.  
  1121.   These are the pin assignments for the DB-9 signal port of the Trust
  1122.   Energy Protector 400/650, as described in the user's manual:
  1123.  
  1124.      pin 2
  1125.         The relay will close when input power fails.
  1126.  
  1127.      pin 4
  1128.         Common for pins 2 and 5.
  1129.  
  1130.      pin 5
  1131.         The relay will close when the battery inside the Trust Energy
  1132.         Protector 400/650 has less than 1.5 minutes of backup time left.
  1133.  
  1134.      pin 6
  1135.         The user may send a high level signal (+5V - +12V) for over 1ms
  1136.         to turn off the Trust Energy Protector 400/650. However this
  1137.         option can only be activated when the input power fails.
  1138.  
  1139.      pin 7
  1140.         Common for pin 6.
  1141.  
  1142.   8.3.1.2.  The Cable
  1143.  
  1144.   This is the cable I used to connect the UPS to the serial port of my
  1145.   computer:
  1146.  
  1147.   computer side (DB-25)               UPS side (DB-9)
  1148.   ===================================================
  1149.  
  1150.    6 DSR --+             [R] = 10 kilo-Ohm resistor
  1151.            |
  1152.   20 DTR --+----+
  1153.            |    |
  1154.           [R]  [R]                           +--- 7
  1155.            |    |                            |
  1156.    8 DCD --+----|-----------        ---------|--- 2
  1157.                 |                            |
  1158.    7 GND -------|-----------        ---------+--- 4
  1159.                 |             ....
  1160.    5 CTS -------+-----------        ------------- 5
  1161.  
  1162.    2 TX  -------------------        ------------- 6
  1163.  
  1164.   ===================================================
  1165.  
  1166.   In the case of a DB-9 serial port, the pins 6,20,8,7,5,2 are mapped to
  1167.   pins 6,4,1,5,8,3.
  1168.  
  1169.   8.3.1.3.  How the cable works
  1170.  
  1171.   The computer raises DTR and checks whether DSR is high, to ensure that
  1172.   the cable is connected to the computer. While the power is good, DCD
  1173.   and CTS are both high (because of the pull-up resistors).
  1174.  
  1175.   When the power fails, the relay between pins 2 and 4 of the UPS port
  1176.   closes, and DCD becomes low, signalling the failure condition.
  1177.  
  1178.   Similarly, when the UPS batteries are getting low, the relay between
  1179.   pins 5 and 4 closes, thus lowering CTS.
  1180.  
  1181.   During a power failure the computer is able to turn off the UPS by
  1182.   raising TX for over 1ms. This can be easily accomplished sending a
  1183.   0xFF byte to the serial port, at a low baud rate.
  1184.  
  1185.   8.3.2.  The powerd daemon
  1186.  
  1187.   To make use of the information available at the serial port we need to
  1188.   run a program which monitors the port, decodes the signals and sends
  1189.   the appropriate messages to the operating system, i.e. to the init
  1190.   process.  The init process can execute scripts and programs designed
  1191.   to handle (gracefully!) the power failure event.
  1192.  
  1193.   8.3.2.1.  Compiling powerd
  1194.  
  1195.   In Appendix A you'll find the source code of powerd, the daemon I use
  1196.   to monitor the Trust Energy Protector 400/650.  To compile it you will
  1197.   need the source code of the sysvinit package (I used the code from
  1198.   sysvinit-2.60). Just overwrite the original powerd.c and compile it.
  1199.  
  1200.   8.3.2.2.  How powerd works
  1201.  
  1202.   As soon as powerd starts it opens the serial device connected to the
  1203.   UPS and forces DTR high. It then forks a daemon and exits, leaving the
  1204.   daemon running. The powerd daemon can be in one of three states:
  1205.  
  1206.      State 0 - POWER IS GOOD
  1207.         In this state powerd reads the serial port every T0_SLEEP
  1208.         seconds (see the #define lines at the beginning of the code).
  1209.         If DCD drops, powerd switches to state 1. If CTS drops powerd
  1210.         switches to state 2 (this shouldn't happen without DCD dropping
  1211.         before, but I decided to stay on the safe side).
  1212.  
  1213.      State 1 - POWER FAILURE
  1214.         A power failure was detected. DCD is low and powerd reads the
  1215.         UPS port every T1_SLEEP seconds. If DCD becomes high, it
  1216.         switches to state 0. If CTS drops, it switches to state 2.
  1217.  
  1218.      State 2 - POWER CRITICAL
  1219.         UPS batteries are low. The powerd daemon will remain in this
  1220.         state.
  1221.  
  1222.   Each time powerd changes state, it notifies the init process, so that
  1223.   the appropriate action can be taken. These events are logged using the
  1224.   system logging facility.
  1225.  
  1226.   If DSR is low there must be something wrong with the cable.  Powerd
  1227.   keeps monitoring the DSR line, and every two minutes sends a warning
  1228.   message to the system logging facility.
  1229.  
  1230.   8.3.2.3.  Running powerd
  1231.  
  1232.   The powerd daemon should be launched from the system initialization
  1233.   scripts, during system startup. I added the following lines to my
  1234.   /etc/rc.d/rc.local script:
  1235.  
  1236.        # Add support for the UPS
  1237.        echo "Starting powerd daemon..."
  1238.        rm -f /etc/turnUPSoff
  1239.        stty -crtscts speed 75 < /dev/cua3 > /dev/null
  1240.        if [ -x /usr/sbin/powerd ]; then
  1241.                /usr/sbin/powerd /dev/cua3
  1242.        fi
  1243.  
  1244.   First we remove (if present) the file /etc/turnUPSoff.  This file is
  1245.   used by the system shutdown script (/etc/rc.d/rc.0, in my case) to
  1246.   decide whether we want to turn the UPS off.  See later in this
  1247.   document for more information.
  1248.  
  1249.   Then we disable hardware flow control on the serial device connected
  1250.   to the UPS, and set its baud rate to 75.  Now we're confident that the
  1251.   TX signal will stay high for a time long enough to turn the UPS off,
  1252.   if we send a character to the serial port (again, see later).
  1253.  
  1254.   Finally we launch the powerd daemon, specifying the serial port to
  1255.   monitor. Notice that we're not going to read characters from the
  1256.   serial device, so don't worry if you have interrupt conflicts -
  1257.   they'll do no harm.
  1258.  
  1259.   8.3.3.  The inittab file and the shutdown scripts
  1260.  
  1261.   The powerd process is now running, and it will send signals to init
  1262.   whenever a power failure occurs. Now we have to configure the system
  1263.   so that it can react in a useful way when those signals are received.
  1264.  
  1265.   8.3.3.1.  Modifying inittab
  1266.  
  1267.   Add the following lines near the beginning of your /etc/inittab file:
  1268.  
  1269.        # What to do when power fails (delayed shutdown).
  1270.        pf::powerfail:/etc/powerfail_script
  1271.  
  1272.        # If power is back before shutdown, cancel the running shutdown.
  1273.        pg::powerokwait:/etc/powerokay_script
  1274.  
  1275.        # If UPS batteries are getting low, do an immediate shutdown.
  1276.        pc::powerfailnow:/etc/powerfailnow_script
  1277.  
  1278.   8.3.3.2.  The scripts
  1279.  
  1280.   The scripts powerfail_script, powerokay_script and powerfailnow_script
  1281.   are executed when init receives the corresponding signal. They have
  1282.   the responsibility of shutting down the system in a clean way or
  1283.   cancelling a running shutdown in case power comes back.  These are the
  1284.   scripts I'm currently using:
  1285.  
  1286.   /etc/powerfail_script:
  1287.  
  1288.        #!/bin/sh
  1289.        /bin/sync
  1290.        /usr/bin/sleep 10m
  1291.        kill -9 `ps auxw | grep "shutdown" | grep -v grep | awk '{print $2}'`
  1292.        > /etc/turnUPSoff
  1293.        /sbin/shutdown -t30 -h +3 "POWER FAILURE"
  1294.  
  1295.   My Trust Energy Protector 400 powers only the computer, so I have
  1296.   quite a long backup time. Since power failures only last for some
  1297.   minutes in my zone, the system responds to a blackout in the following
  1298.   way: it waits for 10 minutes (usually the power comes back before) and
  1299.   then halts the system, allowing the users to close their applications
  1300.   and leave the machine. Before issuing the shutdown command, I make
  1301.   sure that there are no running shutdowns. I also create the file
  1302.   /etc/turnUPSoff, so that the system will turn off the UPS.
  1303.  
  1304.   /etc/powerokay_script:
  1305.  
  1306.        #!/bin/sh
  1307.        kill `ps auxw | grep "powerfail_script" | grep -v grep | awk '{print $2}'`
  1308.        kill -9 `ps auxw | grep "shutdown" | grep -v grep | awk '{print $2}'`
  1309.        rm -f /etc/turnUPSoff
  1310.  
  1311.   If power comes back, we kills the running powerfail_script and any
  1312.   running shutdown. We also remove /etc/turnUPSoff.
  1313.  
  1314.   /etc/powerfailnow_script:
  1315.  
  1316.        #!/bin/sh
  1317.        kill -9 `ps auxw | grep "shutdown" | grep -v grep | awk '{print $2}'`
  1318.        > /etc/turnUPSoff
  1319.        /sbin/shutdown -h now "UPS batteries low. IMMEDIATE SHUTDOWN."
  1320.  
  1321.   If batteries are getting low, we make sure that there are no running
  1322.   shutdowns, create the /etc/turnUPSoff file and then shutdown the
  1323.   system immediately.
  1324.  
  1325.   8.3.3.3.  The system shutdown script
  1326.  
  1327.   When system shutdown is complete, we can turn off the UPS raising the
  1328.   TX signal of the serial port for over 1ms.  The serial device is
  1329.   already properly configured (see the stty command in the rc.local
  1330.   script). If the file /etc/turnUPSoff is present, we send the byte 0xff
  1331.   (all '1' bits) to the serial port.
  1332.  
  1333.   To do this, add the following lines near the bottom of your system
  1334.   shutdown script (/etc/rc.d/rc.0, in my case).  The proper place
  1335.   depends on the way your system is configured, but it should be okay to
  1336.   insert the lines before the echo command which prints the "System is
  1337.   halted" message.
  1338.  
  1339.        # Is this a powerfail situation?
  1340.        if [ -f /etc/turnUPSoff ]; then
  1341.          echo "Turning off UPS. Bye."
  1342.          sleep 5
  1343.          echo -e "\377" > /dev/cua3
  1344.          exit 1
  1345.        fi
  1346.  
  1347.   8.3.4.  General remarks
  1348.  
  1349.   This document contains things I learned while trying to configure *my*
  1350.   Linux system to use the Trust Energy Protector 400.  Some informations
  1351.   (the path of the system inizialization scripts, for example) may be
  1352.   specific to my system, and you probably will need some customization.
  1353.   However, I hope this document will be a useful trace for those trying
  1354.   to use a Trust Energy Protector 400/650 under Linux. If you experience
  1355.   difficulties, look for general information in the rest of this UPS-
  1356.   Howto.  Good luck!
  1357.  
  1358.   8.3.4.1.  Feedback
  1359.  
  1360.   I would greatly appreciate receiving feedback about this document, so
  1361.   that I can polish it and correct possible mistakes (I know the English
  1362.   is not very good, but I'm Italian after all!).  Direct any
  1363.   comments/suggestions/critics to the following e-mail address:
  1364.  
  1365.   ciro@stud.unipg.it
  1366.  
  1367.   If you have problems using Trust Energy Protector 400/650 under Linux,
  1368.   feel free to contact me.  I'll try to help you.
  1369.  
  1370.   8.3.4.2.  Legal Issues
  1371.  
  1372.   I have no relation at all with Trust Networking Products.
  1373.  
  1374.   The information contained in this document comes "as is".  Use it at
  1375.   your own risk. I can't be held responsible for any damage or loss of
  1376.   data resulting from the use of the code and information given here.
  1377.  
  1378.   Ciro Cattuto
  1379.  
  1380.   -----
  1381.  
  1382.   8.3.5.  Appendix A  -  Source code for the powerd daemon
  1383.  
  1384.   powerd.c:
  1385.  
  1386.   /*
  1387.    * powerd       Catch power failure signals from
  1388.    *              a Trust Energy Protector 400/650
  1389.    *              and notify init
  1390.    *
  1391.    * Usage:       powerd /dev/cua3 (or any other serial device)
  1392.    *
  1393.    * Author:      Ciro Cattuto <ciro@stud.unipg.it>
  1394.    *
  1395.    * Version 1.0 - 31 March 1997
  1396.    *
  1397.    * This code is heavily based on the original powerd.c code
  1398.    * by Miquel van Smoorenburg <miquels@drinkel.ow.org>.
  1399.    *
  1400.    * This program is free software; you can redistribute it and/or
  1401.    * modify it under the terms of the GNU General Public License
  1402.    * as published by the Free Software Foundation; either version
  1403.    * 2 of the License, or (at your option) any later version.
  1404.    *
  1405.    */
  1406.  
  1407.   /* state 0 - power is good */
  1408.   #define T0_SLEEP        10      /* interval between port reads, in seconds */
  1409.   #define T0_DCD          3       /* number of seconds DCD has to be high
  1410.                                      to cause an action                      */
  1411.   #define T0_CTS          3       /* number of seconds CTS has to be high
  1412.                                      to cause an action                      */
  1413.   /* state 1 - power is failing */
  1414.   #define T1_SLEEP        2       /* interval between ports reads            */
  1415.   #define T1_DCD          3       /* same as T0_DCD                          */
  1416.   #define T1_CTS          3       /* same as T0_CTS                          */
  1417.  
  1418.   #define DSR_SLEEP       2
  1419.   #define DSR_TRIES       60
  1420.  
  1421.   /* Use the new way of communicating with init. */
  1422.   #define NEWINIT
  1423.  
  1424.   #include <sys/types.h>
  1425.   #include <sys/stat.h>
  1426.   #include <sys/ioctl.h>
  1427.   #include <fcntl.h>
  1428.   #include <errno.h>
  1429.   #include <stdlib.h>
  1430.   #include <unistd.h>
  1431.   #include <stdio.h>
  1432.   #include <signal.h>
  1433.   #include <syslog.h>
  1434.   #include <string.h>
  1435.   #include "paths.h"
  1436.   #ifdef NEWINIT
  1437.   #include "initreq.h"
  1438.   #endif
  1439.  
  1440.   #ifndef SIGPWR
  1441.   #  define SIGPWR SIGUSR1
  1442.   #endif
  1443.  
  1444.   #ifdef NEWINIT
  1445.   void alrm_handler()
  1446.   {
  1447.   }
  1448.   #endif
  1449.  
  1450.   /* Tell init that the power has gone (1), is back (0),
  1451.      or the UPS batteries are low (2). */
  1452.   void powerfail(int event)
  1453.   {
  1454.     int fd;
  1455.   #ifdef NEWINIT
  1456.     struct init_request req;
  1457.  
  1458.     /* Fill out the request struct. */
  1459.     memset(&req, 0, sizeof(req));
  1460.     req.magic = INIT_MAGIC;
  1461.     switch (event)
  1462.           {
  1463.           case 0:
  1464.                   req.cmd = INIT_CMD_POWEROK;
  1465.                   break;
  1466.           case 1:
  1467.                   req.cmd = INIT_CMD_POWERFAIL;
  1468.                   break;
  1469.           case 2:
  1470.           default:
  1471.                   req.cmd = INIT_CMD_POWERFAILNOW;
  1472.           }
  1473.  
  1474.     /* Open the fifo (with timeout) */
  1475.     signal(SIGALRM, alrm_handler);
  1476.     alarm(3);
  1477.     if ((fd = open(INIT_FIFO, O_WRONLY)) >= 0
  1478.                   && write(fd, &req, sizeof(req)) == sizeof(req)) {
  1479.           close(fd);
  1480.           return;
  1481.     }
  1482.     /* Fall through to the old method.. */
  1483.   #endif
  1484.  
  1485.     /* Create an info file for init. */
  1486.     unlink(PWRSTAT);
  1487.     if ((fd = open(PWRSTAT, O_CREAT|O_WRONLY, 0644)) >= 0) {
  1488.     switch (event)
  1489.           {
  1490.           case 0:
  1491.                   write(fd, "OK\n", 3);
  1492.                   break;
  1493.  
  1494.           case 1:
  1495.                   write(fd, "FAIL\n", 5);
  1496.                   break;
  1497.  
  1498.           case 2:
  1499.           default:
  1500.                   write(fd, "LOW\n", 4);
  1501.                   break;
  1502.           }
  1503.     close(fd);
  1504.     }
  1505.  
  1506.     kill(1, SIGPWR);
  1507.   }
  1508.  
  1509.   /* Main program. */
  1510.   int main(int argc, char *argv[])
  1511.   {
  1512.     int fd;
  1513.     int dtr_bit = TIOCM_DTR;
  1514.     int flags;
  1515.     int DCD, CTS;
  1516.     int status = -1;
  1517.     int DCD_count = 0, CTS_count = 0;
  1518.     int tries;
  1519.  
  1520.     if (argc < 2) {
  1521.           fprintf(stderr, "Usage: powerd <device>\n");
  1522.           exit(1);
  1523.     }
  1524.  
  1525.     /* Start syslog. */
  1526.     openlog("powerd", LOG_CONS|LOG_PERROR, LOG_DAEMON);
  1527.  
  1528.     /* Open monitor device. */
  1529.     if ((fd = open(argv[1], O_RDWR | O_NDELAY)) < 0) {
  1530.           syslog(LOG_ERR, "%s: %s", argv[1], sys_errlist[errno]);
  1531.           closelog();
  1532.           exit(1);
  1533.     }
  1534.  
  1535.     /* Line is opened, so DTR is high. Force it anyway to be sure. */
  1536.     ioctl(fd, TIOCMBIS, &dtr_bit);
  1537.  
  1538.     /* Daemonize. */
  1539.     switch(fork()) {
  1540.           case 0: /* Child */
  1541.                   closelog();
  1542.                   setsid();
  1543.                   break;
  1544.           case -1: /* Error */
  1545.                   syslog(LOG_ERR, "can't fork.");
  1546.                   closelog();
  1547.                   exit(1);
  1548.           default: /* Parent */
  1549.                   closelog();
  1550.                   exit(0);
  1551.     }
  1552.  
  1553.     /* Restart syslog. */
  1554.     openlog("powerd", LOG_CONS, LOG_DAEMON);
  1555.  
  1556.     /* Now sample the DCD line. */
  1557.     while(1) {
  1558.           /* Get the status. */
  1559.           ioctl(fd, TIOCMGET, &flags);
  1560.  
  1561.           /* Check the connection: DSR should be high. */
  1562.           tries = 0;
  1563.           while((flags & TIOCM_DSR) == 0) {
  1564.                   /* Keep on trying, and warn every two minutes. */
  1565.                   if ((tries % DSR_TRIES) == 0)
  1566.                       syslog(LOG_ALERT, "UPS connection error");
  1567.                   sleep(DSR_SLEEP);
  1568.                   tries++;
  1569.                   ioctl(fd, TIOCMGET, &flags);
  1570.           }
  1571.           if (tries > 0)
  1572.                   syslog(LOG_ALERT, "UPS connection OK");
  1573.  
  1574.           /* Calculate present status. */
  1575.           DCD = flags & TIOCM_CAR;
  1576.           CTS = flags & TIOCM_CTS;
  1577.  
  1578.           if (status == -1)
  1579.                   {
  1580.                   status = (DCD != 0) ? 0 : 1;
  1581.                   if (DCD == 0)
  1582.                           {
  1583.                           syslog(LOG_ALERT, "Power Failure. UPS active.");
  1584.                           powerfail(1);
  1585.                           }
  1586.                   }
  1587.  
  1588.           switch (status)
  1589.                   {
  1590.                   case 0:
  1591.                           if ((DCD != 0) && (CTS != 0))
  1592.                                   {
  1593.                                   DCD_count = 0;
  1594.                                   CTS_count = 0;
  1595.                                   sleep(T0_SLEEP);
  1596.                                   continue;
  1597.                                   }
  1598.                           if (DCD == 0)
  1599.                                   DCD_count++;
  1600.                           if (CTS == 0)
  1601.                                   CTS_count++;
  1602.                           if ((DCD_count < T0_DCD) && (CTS_count < T0_CTS))
  1603.                                   {
  1604.                                   sleep(1);
  1605.                                   continue;
  1606.                                   }
  1607.                           if (CTS_count == T0_CTS)
  1608.                                   {
  1609.                                   status = 2;
  1610.                                   syslog(LOG_ALERT, "UPS batteries low!");
  1611.                                   break;
  1612.                                   }
  1613.                           status = 1;
  1614.                           DCD_count = 0;
  1615.                           syslog(LOG_ALERT, "Power Failure. UPS active.");
  1616.                           break;
  1617.  
  1618.                   case 1:
  1619.                           if ((DCD == 0) && (CTS != 0))
  1620.                                   {
  1621.                                   DCD_count = 0;
  1622.                                   CTS_count = 0;
  1623.                                   sleep(T1_SLEEP);
  1624.                                   continue;
  1625.                                   }
  1626.                           if (DCD != 0)
  1627.                                   DCD_count++;
  1628.                           if (CTS == 0)
  1629.                                   CTS_count++;
  1630.                           if ((DCD_count < T1_DCD) && (CTS_count < T1_CTS))
  1631.                                   {
  1632.                                   sleep(1);
  1633.                                   continue;
  1634.                                   }
  1635.                           if (CTS_count == T1_CTS)
  1636.                                   {
  1637.                                   status = 2;
  1638.                                   syslog(LOG_ALERT, "UPS batteries low!");
  1639.                                   break;
  1640.                                   }
  1641.                           status = 0;
  1642.                           DCD_count = 0;
  1643.                           CTS_count = 0;
  1644.                           syslog(LOG_ALERT, "Power okay.");
  1645.                           break;
  1646.  
  1647.                   case 2:
  1648.                           sleep(1);
  1649.                           continue;
  1650.                   default:
  1651.                           break;
  1652.                   }
  1653.  
  1654.           powerfail(status);
  1655.     }
  1656.     /* Never happens */
  1657.     return(0);
  1658.   }
  1659.  
  1660.   8.4.  Trust UPS 400-A
  1661.  
  1662.   I received a submission about the Trust UPS 400-A.  I don't know if
  1663.   it's the same as the Trust Energy Protector 400, so I'm including the
  1664.   submission.
  1665.  
  1666.   From: "Marcel Ammerlaan" <marcel@ch.twi.tudelft.nl>
  1667.   To: hjstein@math.huji.ac.il
  1668.   Subject: UPS addition
  1669.   Date: Wed, 16 Jul 1997 01:17:11 +100
  1670.  
  1671.   Hello Harvey,
  1672.  
  1673.   I've got an addition to your UPS Howto. I've got a
  1674.   "Trust UPS 400-A" which isn't listed. This product doesn't seem
  1675.   to be manufactured anymore by it's producer (www.trust.box.nl).
  1676.   But that doesn't mean it's not available anymore, I've got mine
  1677.   really cheap just a month ago. Also this company just relabels
  1678.   products so maybe there are others that have got the same UPS.
  1679.  
  1680.   I have included a picture of the UPS in case anybody got such a beast
  1681.   under another label.
  1682.  
  1683.   The cable was easily constructed based on the original powerd cable
  1684.   and the documentation from trust.
  1685.   It clearly describes which pins of the D-shell connector of the UPS
  1686.   carry which signal.
  1687.  
  1688.   It extends the original design with 2 extra functions:
  1689.   1) Battery low indication
  1690.   2) Power down UPS
  1691.  
  1692.   The cable I created looks like (see the other attachement).
  1693.  
  1694.   This cable has been tested with powergend by Tom Webster and did work
  1695.   completely (although your milage may vary).
  1696.  
  1697.   Type:               "pleur"
  1698.   Cable Power:        {TIOCM_DTR,0}
  1699.   Inverter Kill:      {TIOCM_RTS,1}
  1700.   Inverter Kill Time: 5
  1701.   Power Check:        {TIOCM_CTS,0}
  1702.   Battery Check:      {TIOCM_CAR,0}
  1703.   Cable Check:        {TIOCM_RI,0}
  1704.  
  1705.   Although (just as the powerd cable) the cable check function isn't
  1706.   used because the UPS doesn't seem to support it.
  1707.  
  1708.   Well that's about it I guess. If you need more information about the
  1709.   UPS the cable or the software feel free to contact me.
  1710.  
  1711.   And remember, everything described here works for me but I don't
  1712.   guarantee it will for you.
  1713.  
  1714.   Marcel Ammerlaan
  1715.   CEO Pleursoft (explains the cablename doesn't it :-)
  1716.   The Netherlands
  1717.  
  1718.   <RSA implemented in 3 lines of perl deleted by the editor ;)>
  1719.  
  1720.   Marcel Ammerlaan   | <m.j.ammerlaan@twi.tudelft.nl>
  1721.   Paardenmarkt 78    | Just another nerd on the loose
  1722.   2611 PD Delft      |
  1723.   The Netherlands    |
  1724.  
  1725.   8.5.  Sustainer S-40a
  1726.  
  1727.   Information on the Sustainer S-40a.
  1728.  
  1729.   From: fnevgeny@plasma-gate.weizmann.ac.il (Evgeny Stambulchik)
  1730.   To: hjstein@math.huji.ac.il, hjstein@math.huji.ac.il, hjstein@math.huji.ac.il,
  1731.           hjstein@math.huji.ac.il
  1732.   Subject: UPS-HowTo add-ons
  1733.   Date: Sun, 10 Sep 1995 13:09:50 +0300 (IST)
  1734.  
  1735.   Hi Harvey,
  1736.  
  1737.   This is an addition to your UPS-HowTo. I'm using Sustainer S-40a UPS for a few
  1738.   months with unipower package (now it's called genpower) and home-made cable
  1739.   constructed as follows (I've sent all this stuff to Tom Webster, author of the
  1740.   package, too, and it should appear in the next version):
  1741.  
  1742.            UPS SIDE                                   LINUX SIDE
  1743.  
  1744.                  2 POWER FAIL                             1(8)
  1745.      +-----------o-------------------------+----------------o  DCD
  1746.      |                                     |
  1747.      o                                     |
  1748.       /                                    |
  1749.      /                                     |
  1750.      |           4 COMMON                  |              5(7)
  1751.      +-----------o------+------------------|----------------o  GND
  1752.      |                  |                  |
  1753.      \                  |                  |
  1754.       \                 |                  |
  1755.      o                  |                  |
  1756.      |           5 BATT | LOW              |              8(5)
  1757.      +-----------o------|------------------|--------+-------o  CTS
  1758.                         |                  |        |
  1759.                         |                +-+-+    +-+-+
  1760.                         |                |   |    |   |
  1761.                         |    3 resistors |   |    |   |
  1762.                         |                |   |    |   |
  1763.                         |    by 10 kOhm  |   |    |   |
  1764.                         |                +-+-+    +-+-+
  1765.                         |                  |        |     4(20)
  1766.                         |                  +--------+-------o  DTR
  1767.                         |
  1768.   |              6 SHUT | DOWN              +-------+     7(4)
  1769.   +-+       +----o------|-------------------+       +-------o  RTS
  1770.     \       |           |                   +-------+
  1771.      \|    -+-          |
  1772.       | <- \ /          |
  1773.      /|    -+-          |
  1774.     /       |    7      |
  1775.     |       +----o------+
  1776.   --+--
  1777.    ---
  1778.     -
  1779.  
  1780.   NOTE!!!: Shutdown pins in the tech info supplied with UPS (4 and 6) are given
  1781.   incorrectly! The valid ones are 6 and 7, as shown above.
  1782.   Note2: Pin numbers on the PC side in the brackets are for 25-pin connector,
  1783.   outside - for 9-pin one.
  1784.  
  1785.   Here's the unipowerd.h file I used:
  1786.  
  1787.   /************************************************************************/
  1788.   /* File Name            : unipowerd.h                                   */
  1789.   /* Program Name         : unipowerd                   Version: 1.0.0    */
  1790.   /* Author               : Tom Webster <webster@kaiwan.com>              */
  1791.   /* Created              : 1994/04/20                                    */
  1792.   /* Last Modified By     : Tom Webster                 Date: 1995/04/09  */
  1793.   /* Last Modified By     : Evgeny Stambulchik (for Sustainer UPS)        */
  1794.   /*                                                                      */
  1795.   /* Compiler (created)   : GCC 2.5.8                                     */
  1796.   /* Compiler (env)       : Linux 1.0.9                                   */
  1797.   /* ANSI C Compatable    : No                                            */
  1798.   /* POSIX Compatable     : Yes?                                          */
  1799.   /*                                                                      */
  1800.   /* Purpose              : Header file for unipowerd.                    */
  1801.   /*                      : Contains the configuration information for    */
  1802.   /*                      : unipowerd.  Edit this file as indicated       */
  1803.   /*                      : below to activate features and to customize   */
  1804.   /*                      : unipowerd for your UPS.                       */
  1805.   /*                                                                      */
  1806.   /* Copyright            : GNU Copyleft                                  */
  1807.   /************************************************************************/
  1808.  
  1809.   /* The following are the RS232 control lines      */
  1810.   /*                                                */
  1811.   /*                                            D D */
  1812.   /*                                            T C */
  1813.   /* Macro           English                    E E */
  1814.   /* ---------------------------------------------- */
  1815.   /* TIOCM_DTR       DTR - Data Terminal Ready  --> */
  1816.   /* TIOCM_RTS       RTS - Ready to send        --> */
  1817.   /* TIOCM_CTS       CTS - Clear To Send        <-- */
  1818.   /* TIOCM_CAR       DCD - Data Carrier Detect  <-- */
  1819.   /* TIOCM_RNG       RI  - Ring Indicator       <-- */
  1820.   /* TIOCM_DSR       DSR - Data Signal Ready    <-- */
  1821.  
  1822.   #define HIGH            (1)
  1823.   #define LOW             0
  1824.   #define PWRSTAT         "/etc/powerstatus"
  1825.   #define UPSSTAT         "/etc/upsstatus"
  1826.  
  1827.   /* CABLEPOWER is the line which provides power to */
  1828.   /* the cable for normal monitoring activities.    */
  1829.   #define CABLEPOWER      TIOCM_DTR
  1830.  
  1831.   #define POWERBIT        TIOCM_CAR
  1832.   #define POWEROK         HIGH
  1833.  
  1834.   /* Define CABLECHECK as 1 to check for low battery */
  1835.   /* Define CABLECHECK as 0 value to skip            */
  1836.   #define CABLECHECK      0
  1837.   #define CABLEBIT        TIOCM_RNG
  1838.   #define CABLEOK         HIGH
  1839.  
  1840.   /* Define BATTCHECK as 1 to check for low battery  */
  1841.   /* Define BATTCHECK as 0 value to skip.            */
  1842.   #define BATTCHECK       1
  1843.   #define BATTBIT         TIOCM_CTS
  1844.   #define BATTOK          HIGH
  1845.  
  1846.   /* Define INVERTERKILL as 1 to hndle killing the inverter */
  1847.   /* Define INVERTERKILL as 0 value to skip.                */
  1848.   /* INVERTERBIT is the line which will kill the inverter   */
  1849.   /*    while the UPS is in powerfail mode.                 */
  1850.   /* INVERTERTIME is the time in seconds to hold the line   */
  1851.   /*    defiined by INVERTERBIT high to kill the inverter.  */
  1852.   #define INVERTERKILL    1
  1853.   #define INVERTERBIT     TIOCM_RTS
  1854.   #define INVERTERTIME    5
  1855.  
  1856.   /************************************************************************/
  1857.   /* End of File unipowerd.h                                              */
  1858.   /************************************************************************/
  1859.  
  1860.   I'm aware that current name of the package is genpower. I haven't try it yet as
  1861.   see no reason to switch to the new version meantime; the former seems to work
  1862.   very stable. Nevertheless, here is the add-on for genpower-1.0.1's genpowerd.h
  1863.   file (hopefully, I "translated" unipowerd.h correctly):
  1864.   Add-on for genpower-1.0.1's genpowerd.h file:
  1865.  
  1866.   /* Evgeny's Sustainer S-40A */
  1867.    {"sustainer",   {TIOCM_DTR,0}, {TIOCM_RTS,1},  5, {TIOCM_CAR,0}, {TIOCM_CTS,0},
  1868.   {0,0}}
  1869.  
  1870.   Evgeny
  1871.  
  1872.   8.6.  Systel
  1873.  
  1874.   Another Israeli company.  I never ended up purchasing a UPS from them,
  1875.   but they were very good about getting me detailed documentation on
  1876.   their communication port.  It should be easy enough to control their
  1877.   UPS.  Their phone number is 972-8-409-019 (972-8-407-216 for fax).
  1878.  
  1879.   8.7.  Deltec Power, Fiskars Power Systems and Exide.
  1880.  
  1881.   Fiskars <http://www.fiskars.fi/> is a Finnish holding company.  They
  1882.   used to own Deltec Power <http://www.deltecpower.com>.  In March of
  1883.   1996 Fiskars sold Deltec Power to Exide Electronics Group
  1884.   <http://www.exide.com/exide>.  At that time, Deltec Power was one of
  1885.   the world's largest makers of UPSs.
  1886.  
  1887.   Under Fiskars, Deltec used to make the PowerServers 10, 20, 30, and
  1888.   40.  The Deltec Power home page mentions other UPSs.
  1889.  
  1890.   Exide now bundles UPS control software with their UPSs that works
  1891.   under Linux.  They also sell the software separately.  They say that
  1892.   their software works with other UPSs too.
  1893.  
  1894.   I'd like to hear from people using their software.
  1895.  
  1896.   Here's the advertisement they emailed me:
  1897.  
  1898.   Exide Electronics announces LanSafe III UPS Power Management Software
  1899.   for Linux.
  1900.  
  1901.   LanSafe III is a UPS Power Management application. It provides
  1902.   automatic orderly shutdown functionality incase of an extended power
  1903.   failure that should outlast the UPS battery run time.
  1904.  
  1905.   LanSafe III enables broadcast messages and e-mail to be sent according
  1906.   to user defined power condition changes. The shutdown procedure can
  1907.   also be customized.
  1908.  
  1909.   LanSafe III works together with the vast majority of all Exide
  1910.   Electronics UPS models. It goes even one step further by supporting
  1911.   basic shutdown functionality also with other manufacturers UPSs.
  1912.  
  1913.   LanSafe III for Linux runs on Intel based Linux systems. Both
  1914.   character based and X11/Motif based user interfaces are provided.
  1915.  
  1916.   LanSafe III supports all the major OS platforms: Linux, IBM AIX, HP
  1917.   UX, Digital UNIX, SCO UNIX, Solaris, SunOS, AT&T UNIX, all Windows
  1918.   platforms, OS/2, Novell and Macintosh among others.
  1919.  
  1920.   LanSafe III is bundled with the following Exide Electronics UPSs:
  1921.   OneUPS Plus, NetUPS, PowerWare Prestige, PowerWare Profile, PowerWare
  1922.   Plus 5xx.
  1923.  
  1924.   It also ships with FPS Power Systems UPSs: PowerRite Plus, PowerRite
  1925.   Max, PowerWorks A30, PowerWorks A40, Series 9000 and Series 10000.
  1926.  
  1927.   It is also possible to purchase a separate software license to use
  1928.   with a previous UPS model or an other manufactures UPS. Regular
  1929.   licenses are S$149, with site licenses also available.
  1930.  
  1931.   For details please visit our Web sites at www.exide.com,
  1932.   www.fiskarsUPS.com and www.deltecpower.com.
  1933.  
  1934.   Incidentally, when I tried to connect to www.fiskarsUPS.com, it
  1935.   prompted me for a username and password.
  1936.  
  1937.   8.8.  Beaver model UB500 UPS
  1938.  
  1939.   dan@fch.wimsey.bc.ca (Dan Fandrich) writes:
  1940.  
  1941.   I seem to have gotten my old Beaver model UB500 UPS working with
  1942.   genpower.  The interface uses RS-232 compatible voltage levels, so
  1943.   installing it is a snap.  There is a DE-9 female connector on the back
  1944.   which plugs directly into a 9-pin PC serial port using a plain 9-pin
  1945.   video monitor extension cable.
  1946.  
  1947.   The DIP switches allow quite versatile pinouts.  To emulate genpower's
  1948.   apc1-nt type of UPS, they must be set as follows:
  1949.  
  1950.            |   |       |                       |   |   |   |   |
  1951.            |1  |  on   |  (CTS = power fail)   |   |   |   |   |
  1952.            |2  |  off  |  (CTS = low battery)  |   |   |   |   |
  1953.            |3  |  off  |  (DSR = power fail)   |   |   |   |   |
  1954.            |4  |  off  |  (DSR = low battery)  |   |   |   |   |
  1955.            |5  |  off  |  (CD = power fail)    |   |   |   |   |
  1956.            |6  |  on   |  (CD = low battery)   |   |   |   |   |
  1957.            |7  |  off  |  (RI = power fail)    |   |   |   |   |
  1958.            |8  |  off  |  (RI = low battery)   |   |   |   |   |
  1959.            |9  |  on   |  (DTR = inverter off) |   |   |   |   |
  1960.            |10 |  off  |  (RTS = inverter off) |   |   |   |   |
  1961.  
  1962.                DIP switch SW601 for Beaver model UB500 UPS.
  1963.  
  1964.   The switches form groups of adjacent pairs for each output pin.  They
  1965.   are mutually exclusive--don't try to turn on both switch 5 and 6
  1966.   simultaneously, for example, or you'll be shorting the low battery and
  1967.   power fail signals.
  1968.  
  1969.   That's all there is to it.  Feel free to add this do your
  1970.   documentation.
  1971.  
  1972.   8.9.  Sendom
  1973.  
  1974.   Documentation on using the Sendom UPS.
  1975.  
  1976.        From: charli <mefistos@impsat1.com.ar>
  1977.        To: hjstein@math.huji.ac.il
  1978.        Subject: ups howto contribution
  1979.        Date: Wed, 13 Nov 1996 19:07:41 -0200
  1980.  
  1981.        hjstein@math.huji.ac.il
  1982.  
  1983.        sir:
  1984.  
  1985.        i connected a sendom ups with the help of your UPS-howto and man powerd
  1986.        and discovered something useful. perhaps this thing extends to some
  1987.        other ups.
  1988.        im using slackware 3.0 distribution. i has the soft configuration in
  1989.        /etc/inittab already done. its only needed to add the /rc.local powerd
  1990.        /cuaX
  1991.  
  1992.        i used the man powerd diagram:
  1993.                9pin    25pin
  1994.  
  1995.        DTR     4       20      ---------
  1996.                                 |      >
  1997.        DSR     6       6       --      < 10k
  1998.                                        >
  1999.        DCD     1       8       -------------------------
  2000.                                                        relais
  2001.        GND     5       7       -------------------------
  2002.  
  2003.        the fact is that the sendom ups dont use relais but some electronic
  2004.        solid state device, and it works one way BUT NO THE OTHER. so if you
  2005.        make the cable and doesnt work, first try inverting the cable in the
  2006.        ups "relais"
  2007.  
  2008.        i hope this can be useful, if you want to include this somewhere, feel
  2009.        free to correct my english. please aknowledge this mail even with an
  2010.        empty
  2011.        mail so i know it arrived
  2012.        end
  2013.  
  2014.   8.10.  Best
  2015.  
  2016.   Information on Best UPSs is available on at the Best Power
  2017.   <http://www.bestpower.com/index.html> website.  Their website includes
  2018.   the checkups.tar (section ``Software'') package for communicating with
  2019.   Best UPSs, both in smart mode and in dumb mode, and it includes source
  2020.   code, so you can compile it under Linux.
  2021.  
  2022.   8.10.1.  Best Fortress - Using Best's software
  2023.  
  2024.   Linux Best Power UPS Mini-HOWTO by Michael Stutz (stutz@dsl.org, and
  2025.   http://dsl.org/m/) v1.0, 14 Aug 97
  2026.  
  2027.   0.0 Disclaimer
  2028.  
  2029.   Copyright 1997 by Michael Stutz; this information is free; it may be
  2030.   redistributed and/or modified under the terms of the GNU General
  2031.   Public License, either Version 2 of the License, or (at your
  2032.   preference) any later version, and as long as this sentence remains;
  2033.   this information comes WITHOUT ANY WARRANTY; without even the implied
  2034.   warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE; see
  2035.   the GNU General Public License for more details.
  2036.  
  2037.   1.0 Introduction
  2038.  
  2039.   Best Power <http://www.bestpower.com> are the makers of high quality
  2040.   UPS products, with their Fortress line in particular being well-suited
  2041.   for typical Linux users. Although their products are not currently
  2042.   priced as low as some (such as APC), Best Power provide source code
  2043.   for their UPS software and have been very respondent to queries from
  2044.   Linux users.  Furthermore, their hardware seems to be highly regarded,
  2045.   making Best Power a winning choice for Linux users.
  2046.  
  2047.   This document describes the installation of a Best Power Fortress UPS
  2048.   (model used was a 1996-model 660a with accompanying Best Power CD-ROM)
  2049.   to a Linux box.
  2050.  
  2051.   2.0 Installation
  2052.  
  2053.   2.1 Hardware
  2054.  
  2055.   Install the hardware as indicated in the instructions. The Best Power
  2056.   ``Fortress'' series comes with an RS-232 cable that should attach to a
  2057.   spare serial port on the back of the computer.
  2058.  
  2059.   2.2 Software
  2060.  
  2061.   This is where it differs from the manual, which does not currently
  2062.   have Linux-specific instructions. The accompanying Fortress CD-ROM,
  2063.   however, does come with source code for the UPS software, so getting
  2064.   it up and running on a Linux system is a trivial task.
  2065.  
  2066.   To do this, follow these steps, and use the manual as a reference to
  2067.   get an overall feel for how the software works. I took the liberty of
  2068.   making a few changes in this HOWTO from the way the Fortress software
  2069.   is set up on other UNIX systems that I feel are better suited for a
  2070.   Linux system.  For example, I eliminated the need for an /etc/best
  2071.   directory and put the executables in /usr/local/sbin, which I feel is
  2072.   a more appropriate place.
  2073.  
  2074.   ╖  First, create the "upsdown" script that is executed during a power
  2075.      outage. This one will halt the system:
  2076.  
  2077.   cat > /etc/upsdown << EOF
  2078.   #!/bin/sh
  2079.   shutdown -h now < /dev/console &
  2080.   EOF
  2081.  
  2082.   ╖  Now, make directories for the documentation and the source code:
  2083.  
  2084.        mkdir /usr/doc/best
  2085.        mkdir /usr/local/src/best
  2086.  
  2087.   ╖  Mount the CD-ROM, and untar the unix/checkups.tar file into the
  2088.      /tmp directory or somewhere similar:
  2089.  
  2090.        cd /tmp
  2091.        tar /cdrom/unix/checkups.tar
  2092.  
  2093.   ╖  Change into the etc/best/advanced directory that should have been
  2094.      extracted from the checkups tarball.
  2095.  
  2096.   ╖  Copy the documentation and UPS script files to their proper place
  2097.      in the sytem:
  2098.  
  2099.        cp README /usr/doc/best
  2100.        cp manual.txt /usr/doc/best
  2101.        cp bestsend /etc
  2102.        cp source/*.c /usr/local/src/best
  2103.  
  2104.   ╖  Clean up the /tmp mess and compile the software:
  2105.  
  2106.        cd /usr/local/src/best
  2107.        rm -R /tmp/etc
  2108.        gcc -o checkups checkups.c
  2109.        gcc -o mftalk mftalk.c
  2110.        mv checkups /usr/local/sbin
  2111.        mv mftalk /usr/local/sbin
  2112.  
  2113.   ╖  Test the UPS. Replace ttySx with the serial port of your choice. If
  2114.      you have a good connection, you should see a row of characters
  2115.      print across the screen:
  2116.  
  2117.        mftalk /dev/ttySx
  2118.  
  2119.   ╖  Make the checkups program run at startup for testing. This can be
  2120.      done in several different ways (described in the manual).  The way
  2121.      I did it is by adding this line to /etc/inittab:
  2122.  
  2123.        ups:234:once:/usr/local/sbin/checkups -c500 /dev/ttyS1
  2124.  
  2125.   ╖  Test it. Do this by taking out power to UPS by pulling out the fuse
  2126.      connected to the UPS, and wait a couple of minutes. It print a
  2127.      warning messages and then halt the system after a few mintues.
  2128.  
  2129.   ╖  If that works, take out the "-c500" from the line in your inittab
  2130.      (which basically means shut down the system right away instead of
  2131.      when the UPS power runs out), and you're good to go!
  2132.  
  2133.   3.0 Conclusions
  2134.  
  2135.   I welcome suggestions for improving this document or the techniques
  2136.   described herein. As of this writing, Best Power seemed interested in
  2137.   including this or other information in their documentation to help
  2138.   Linux users with their product, so this is definitely a company to
  2139.   support. Let them know how you feel at sales@bestpower.com and
  2140.   support@bestpower.com.
  2141.  
  2142.   8.10.2.  Best Fortress LI-950
  2143.  
  2144.   Some comments on the Best Fortress.
  2145.  
  2146.   From lnz@dandelion.com Wed May 31 19:53:09 1995
  2147.   Newsgroups: comp.os.linux.hardware
  2148.   Subject: Re: UPS for use with Linux?
  2149.   From: Leonard N. Zubkoff <lnz@dandelion.com>
  2150.   Date: 25 May 1995 16:27:55 -0700
  2151.   Organization: Dandelion Digital
  2152.   NNTP-Posting-Host: dandelion.com
  2153.   NNTP-Posting-User: root
  2154.   In-reply-to: nautix@community.net's message of 23 May 1995 09:41:40 -0700
  2155.  
  2156.   In article <3pt384$sic@odin.community.net> nautix@community.net writes:
  2157.  
  2158.     Ditto what Craig says.  APC was very uncooperative, but I have only
  2159.     good things to say about Best.  I use their Fortress LI 660 model;
  2160.     660 VA, lots of status features on the front, etc.  The CheckUPS
  2161.     software costs extra and needs some hacking to fit into my
  2162.     FSSTND-ish file system (the directories and file names are hard-coded
  2163.     to fit into SunOS 4.1.x).  I'd be happy to send you my diffs, if
  2164.     you want them.  (I love it when a vendor ships the source as
  2165.     a normal business practice!)
  2166.  
  2167.     The CheckUPS software is limited to doing automagic shutdowns, though.
  2168.     The UPS can give lots of status information; CheckUPS only asks for
  2169.     ``If the power has failed, how much battery time remains?''
  2170.  
  2171.     Best follows up on their customer satisfaction cards, too.
  2172.     I indicated that I was dissappointed that CheckUPS didn't do more
  2173.     status reporting (like input voltage, output voltage, percent load,
  2174.     etc.), which is available from the UPS.  I asked for a the
  2175.     spec on the interface lingo; they said ``sure'' and had it to me in
  2176.     2 days, free.  A full-featured UPS status checker is on my back burner.
  2177.     Does anyone see a demand for such a utility?
  2178.  
  2179.   Let me add yet another recommendation for Best Power.  I just purchased a
  2180.   Fortress LI-950, though I declined on the CheckUPS software.  Unlike some
  2181.   other brands, a simple three wire cable is all that's needed to connect the
  2182.   Fortress to a serial port -- no need for pull-up circuitry in the cabling.
  2183.   A few minutes hacking and I had program to act as both a shutdown monitor
  2184.   daemon, and to kill the inverter output when the system is shutdown while
  2185.   on battery power.
  2186.  
  2187.   I may eventually want to use the smarter serial communication mode rather
  2188.   than the simple contact mode, so I asked Best technical support for the
  2189.   documentation, and it arrived today, a week after I called them.  Once I
  2190.   peruse the documentation I'll decide if a smarter interface is really
  2191.   worthwhile, especially since at some point I'll need to shut down two
  2192.   networked machines sharing the UPS.
  2193.  
  2194.                   Leonard
  2195.  
  2196.   8.10.3.  Best Ferrups
  2197.  
  2198.   In addition to the doumentation and softare on Best's web site, you
  2199.   could also use the bestups-0.9.tar.gz (section ``Software'') package.
  2200.   We've just started testing it with our 5kva FERRUPS.
  2201.  
  2202.   The basic idea is that there are two modules.  One which fields
  2203.   information requests on a network port, relays those requests to the
  2204.   UPS, and returns the results.  The second module talks to the first,
  2205.   interprets the results, and responds with either OK or FAIL.
  2206.  
  2207.   This is sufficient to allow the powerd-2.0.tar.gz package (section
  2208.   ``Software'') to do the rest of the work.
  2209.  
  2210.   The details can be gotten from the bestups-0.9.tar.gz package (section
  2211.   ``Software'').
  2212.  
  2213.   Incidentally, our 5kva Ferrups has performed flawlessly in keeping our
  2214.   10 computers and 30 screens humming.
  2215.  
  2216.   8.11.  GPS1000 from ACCODATA
  2217.  
  2218.      >From hennus@sky.nl.mugnet.org Thu Mar 10 15:10:22 1994
  2219.      Newsgroups: comp.os.linux.help
  2220.      Subject: Re: auto-shutdown with UPS
  2221.      From: hennus@sky.nl.mugnet.org (Hennus Bergman)
  2222.      Date: Tue, 1 Mar 1994 22:17:45 GMT
  2223.      Distribution: world
  2224.      Organization: The Organization For Removal Of On-Screen Logos
  2225.  
  2226.      In article <CRAFFERT.94Feb28125452@nostril.lehman.com>,
  2227.      Colin Owen Rafferty <craffert@nostril.lehman.com> wrote:
  2228.      >I am about to buy an Uninterruptable Power Supply for my machine, and
  2229.      >I would like to get one that has the "auto-shutdown" feature.
  2230.      >
  2231.      I just got one of those real cheap :-)
  2232.      It's a GPS1000 by ACCODATA. Anybody know how good the output
  2233.      signal of these things is? [Don't have a scope myself :-(]
  2234.  
  2235.      >I assume that these each have some kind of serial connection that
  2236.      >tells the system information about it.
  2237.      >
  2238.      I took it apart to find out how it worked. There were three optocouplers
  2239.      (two output, one input) connected to a 9 pin connector at the back.
  2240.      One turns on when the power fails, and goes off again when the power
  2241.      returns. While the power is off, you can use the `input' to shut the
  2242.      battery off. [It releases the power-relay.] The third one is some kind
  2243.      of feedback to tell that it did accepted the `shut-down command'.
  2244.      I think the interface for my UPS was designed to be connected to TTL-level
  2245.      signals, but with some resistors it could be connected to serial port.
  2246.      It's wired in such a way that using a RS-232 port you cannot use both
  2247.      output optocouplers; but the shutdown feedback is not necessary anyway,
  2248.      just use the important one. ;-)
  2249.      [Note that it is possible to blow the transistor part in optocouplers
  2250.      with RS-232 levels if you wire it the wrong way round ;-)]
  2251.  
  2252.      I was hoping I would be able to connect it to my unused game port,
  2253.      but that doesn't have an output, does it?
  2254.      I'll probably end up getting an extra printer port for this.
  2255.  
  2256.      Not all UPS' use optocouplers, some use simple relays, which are
  2257.      less critical to connect, but of course not as `nice'.
  2258.  
  2259.      >Has anyone written a package that watches the UPS and does a shutdown
  2260.      >(or something) when the power is off?
  2261.      SysVinit-2.4 (and probably 2.5 as well) has a `powerd' daemon that
  2262.      continually watches a serial port for presence of the CD (Carrier
  2263.      Detect) line and signals init when it drops. Init then activates
  2264.      shutdown with a time delay. If the power returns within a few minutes
  2265.      the shutdown is cancelled. Very Nice.
  2266.      The only problem I had with it is that it doesn't actually tell the
  2267.      UPS to turn off when the shutdown is complete. It just sits there with
  2268.      a root prompt. I'll probably write a small program to shut it down
  2269.      >from /etc/brc. RSN.
  2270.  
  2271.      >    Colin Rafferty, Lehman Brothers <craffert@lehman.com>
  2272.  
  2273.      Hennus Bergman
  2274.  
  2275.   8.12.  TrippLite BC750LAN (Standby UPS)
  2276.  
  2277.   Tom Webster (webster@kaiwan.com, the author of the genpower package)
  2278.   sent me information on the TrippLite BC750LAN.  If you have one of
  2279.   these, your probably best off using his package.
  2280.   But, for completeness, here's his cable wiring diagram (done by trial
  2281.   and error, and without documentation):
  2282.  
  2283.                  UPS                System
  2284.                 DB-25               DB-25
  2285.                   1 <-------------->  1       Ground
  2286.  
  2287.                   2 <-------------->  4       Power Fail
  2288.                   8 <-------------->  8       Sensing Circuit
  2289.  
  2290.                   3 <-------------->  2       Inverter Shutdown
  2291.                  20 <--------------> 22       Circuit
  2292.  
  2293.   8.13.  APC
  2294.  
  2295.   If the above plethora of APC packages doesn't get you running, maybe
  2296.   the following sections will help.
  2297.  
  2298.   8.13.1.  APC Back-UPS
  2299.  
  2300.   There seems to be some controversy as to the accuracy of the
  2301.   information here on APC Back-UPSs.  So, please be careful.  I'm
  2302.   prefacing this section with one message of caution I received.  It
  2303.   might not make a lot of sense before the rest of this section is read,
  2304.   but this way, at least you're more likely to see it.  And again, since
  2305.   I don't have any APC UPS units, I can't verify the accuracy of either
  2306.   of these messages.
  2307.  
  2308.   8.13.1.1.  A message of caution
  2309.  
  2310.   From ind43@sun1000.ci.pwr.wroc.pl Sun Oct  9 11:00:42 1994
  2311.   Newsgroups: comp.os.linux.admin
  2312.   Subject: BUPS-HOWTO warning
  2313.   From: ind43@sun1000.ci.pwr.wroc.pl (Marek Michalkiewicz)
  2314.   Date: 6 Oct 1994 18:38:15 GMT
  2315.   Organization: Technical Univeristy of Wroclaw
  2316.   NNTP-Posting-Host: ci3ux.ci.pwr.wroc.pl
  2317.   X-Newsreader: TIN [version 1.2 PL2]
  2318.  
  2319.   If you want to connect the APC Back-UPS to your Linux box, this might
  2320.   be of interest to you.
  2321.  
  2322.   There is a good BUPS-HOWTO which describes how to do this. But it has
  2323.   one "bug".
  2324.  
  2325.   The RTS serial port signal is used to shut down the UPS. The UPS will
  2326.   shut down only if it operates from its battery. The manual says that
  2327.   the shutdown signal must be high for at least 0.5s. But few milliseconds
  2328.   is enough, at least for my APC Back-UPS 600.
  2329.  
  2330.   Using RTS to shut down the UPS can be dangerous, because the RTS goes
  2331.   high when the serial device is opened. The backupsd program then turns
  2332.   RTS off, but it is on (high) for a moment. This kills the power when
  2333.   backupsd is first started and there is a power failure at this time.
  2334.   This can happen for example when the UPS is shut down, unattended,
  2335.   and the power comes back for a while.
  2336.  
  2337.   Either start backupsd before mounting any filesystems for read-write,
  2338.   or (better) use TX (pin 3) instead of RTS (pin 7) to shut down the
  2339.   UPS (pin numbers are for 9-pin plug). Use ioctl(fd, TCSBRKP, 10);
  2340.   to make TX high for one second, for example. Using TX should be safe.
  2341.   Maybe I will post the diffs if time permits...
  2342.  
  2343.   -- Marek Michalkiewicz
  2344.   ind43@ci3ux.ci.pwr.wroc.pl
  2345.  
  2346.   8.13.1.2.  BUPS-HOWTO
  2347.  
  2348.   Luminated Software Group Presents
  2349.  
  2350.   HOWTO use Back-UPS (by APC) (to keep your linux box from frying)
  2351.  
  2352.   Version: 1.01  BETA
  2353.  
  2354.   Document by: Christian G. Holtje <docwhat@uiuc.edu> Cabling info and
  2355.   help: Ben Galliart <bgallia@orion.it.luc.edu>
  2356.  
  2357.   This document, under one condition, is placed in Public Domain. The
  2358.   one condition is that credit is given where credit is due.  Modify
  2359.   this as much as you want, just give some credit to us who worked.
  2360.  
  2361.   *******************************************************************************
  2362.   Warning!  I, nor any of us who have written or helped with this
  2363.   document, make and guarantees or claims for this text/source/hints.
  2364.   If anything is damaged, we take NO RESPONSIBILITY!  This works to the
  2365.   BEST OF OUR KNOWLEDGE, but we may have made mistakes.  So be careful!
  2366.   *******************************************************************************
  2367.  
  2368.   Al right, you just bought (or are going to buy) a Back-UPS from APC.
  2369.   (Other brands might be able to use this info, with little or no
  2370.   modification, but we don't know)  You've looked at the price of the
  2371.   Power-Chute software/cabling, and just are not sure it's worth the
  2372.   price.  Well, I made my own cable, and my own software and am using it
  2373.   to automatically shut off the power to my linux box when a power
  2374.   failure hits.  Guess what?  You can too!
  2375.  
  2376.   *** The Cable ***
  2377.  
  2378.   This was the hardest part to figure out (I know little about hardware,
  2379.   so Ben did the most work for this).  To build one, you need to buy
  2380.   from your local radio shack (or other part supplier) this stuff:
  2381.  
  2382.        1 9-Position Male D-Subminature Connector (solder-type)
  2383.                [Radio Shack cat. no. 276-1537c]
  2384.        1 9-Position Female D-Subminature Connector (solder-type)
  2385.                [Radio Shack cat. no. 276-1538c]
  2386.        2 casings for the above plugs (usually sold separately)
  2387.        Some stranded wire (wire made of strands, not solid wire)
  2388.  
  2389.   You also need, but may be able to borrow:
  2390.  
  2391.        1 soldering iron
  2392.        solder
  2393.  
  2394.   Okay...this is how you connect it up!
  2395.  
  2396.   These diagrams are looking into the REVERSE SIDE (the side where you
  2397.   solder the wire onto the plugs)  The letters G, R, and B represent the
  2398.   colors of the wires I used, and help to distinguish one line from the
  2399.   next.  (NOTE:  I'm use standard rs-232 (as near as we can tell)
  2400.   numbering.  The APC book uses different numbers.  Ignore them!  Use
  2401.   ours...I already changed the numbers for you!)
  2402.  
  2403.           ---------------------     Male Side! (This goes into the UPS)
  2404.            \  B   R  *  *  * /
  2405.              \  *  *  *  G  /
  2406.                ------------
  2407.  
  2408.           ---------------------     Female Side! (This goes into your COM port)
  2409.            \  R   *  *  *  G /
  2410.              \  *  B  *  *  /
  2411.                ------------
  2412.  
  2413.   For those who like the numbers better:
  2414.  
  2415.                Male            Female
  2416.        ---------------------------------------
  2417.                1               7               Black
  2418.                2               1               Red
  2419.                9               5               Green
  2420.  
  2421.   ---------Aside:  What the rs-232 pins are for!----------- Since we had
  2422.   to dig this info up anyway:
  2423.  
  2424.   >From the REAR (the soldering side) the pins are numbered so:
  2425.  
  2426.           ---------------------
  2427.            \  1   2  3  4  5 /
  2428.              \  6  7  8  9  /
  2429.                ------------
  2430.  
  2431.   The pins mean:
  2432.  
  2433.                Number  Name                    Abbr. (Sometimes written with D prefix)
  2434.                1       Carrier Detect          CD
  2435.                2       Receive Data            RD
  2436.                3       Transmit Data           TD(?)
  2437.                4       Data Terminal Ready     DTR
  2438.                5       Signal Ground           Gnd
  2439.                6       Data Set Ready          DSR
  2440.                7       Request to Send         RTS(?)
  2441.                8       Clear to Send           CS
  2442.                9       Ring Indicator          RI
  2443.  
  2444.   What we did is connect the UPS's RS-232 Line Fail Output to the CD,
  2445.   the UPS's chassis to Gnd, and the UPS's RS-232 Shut Down Input to RTS.
  2446.   Easy now that we told you, no?
  2447.  
  2448.   I have no idea if the software below will work, if you purchase the
  2449.   cable from APC.  It might, and it might not.
  2450.  
  2451.   *** The Software ***
  2452.  
  2453.   Okay, I use the SysVInit package by Miquel van Smoorenburg for Linux.
  2454.   (see end for file locations, credits, email addresses, etc.)  I don't
  2455.   know what would have to be changed to use someone elses init, but I
  2456.   know this code (following) will work with Miquel's stuff.  Just so I
  2457.   give credit where credit's due.  I looked at Miquel's code to figure
  2458.   out how ioctl()'s worked.  If I didn't have that example, I'd have
  2459.   been in trouble.  I also used the powerfail() routine (verbatim, I
  2460.   think), since it must interact with his init, I thought that he should
  2461.   know best.  The .c file is at the end of this document, and just needs
  2462.   to be clipped off.  To clip the file, edit away and extra '.sigs' and
  2463.   junk.  This document should end on the line /* End of File */.....cut
  2464.   the rest.
  2465.  
  2466.   This program can either be run as a daemon to check the status of the
  2467.   UPS and report it to init, or it can be run to send the kill-power
  2468.   command to the UPS.  The power will only be killed if there is a power
  2469.   problem, and the UPS is running off the battery.  Once the power is
  2470.   restored, it turns back on.
  2471.  
  2472.   To run as a daemon, just type: backupsd /dev/backups
  2473.  
  2474.   /dev/backups is a link to /dev/cua0 at the moment (COM 1, for you
  2475.   DOSers).  The niceness of the link is that I can just re-link the
  2476.   device if I change to com 2 or 3.
  2477.  
  2478.   Then, if the power dies init will run the commands for the powerwait.
  2479.   An example (This is from my /etc/inittab):
  2480.  
  2481.        #Here are the actions for powerfailure.
  2482.        pf::powerwait:/etc/rc.d/rc.power start
  2483.        po::powerokwait:/etc/rc.d/rc.power stop
  2484.  
  2485.   The powerwait will run, if the power goes down, and powerokwait will
  2486.   run if the power comes back up.
  2487.  
  2488.   Here is my entire rc.power:
  2489.  
  2490.        #! /bin/sh
  2491.        #
  2492.        # rc.power      This file is executed by init when there is a powerfailure.
  2493.        #
  2494.        # Version:      @(#)/etc/rc.d/rc.power   1.50    1994-08-10
  2495.        #
  2496.        # Author:       Christian Holtje, <docwhat@uiuc.edu>
  2497.        #
  2498.  
  2499.          # Set the path.
  2500.          PATH=/sbin:/etc:/bin:/usr/bin:/sbin/dangerous
  2501.  
  2502.          # Find out how we were called.
  2503.          case "$1" in
  2504.                start)
  2505.                        echo "Warning there is Power problems."  | wall
  2506.                        # Save current Run Level
  2507.                        ps | gawk '{ if (($5 == "init") && ($1 == "1")) print $6 }' \
  2508.                                 | cut -f2 -d[ | cut -f1 -d] \
  2509.                                 > /tmp/run.level.power
  2510.                        /sbin/shutdown -h +1m
  2511.                        ;;
  2512.                stop)
  2513.                        echo "Power is back up.  Attempting to halt shutdown." | wall
  2514.                        shutdown -c
  2515.                        ;;
  2516.                *)
  2517.                        echo "Usage:  $0 [start|stop]"
  2518.                        exit 1
  2519.                        ;;
  2520.          esac
  2521.  
  2522.   Pretty nifty, no?  Actually, there is a problem here...I haven't had
  2523.   time to figure it out...If there is a 'sh' wizard out there....
  2524.  
  2525.   There is one little detail left, that is having the UPS turn off the
  2526.   power if it was halted with the power out.  This is accomplished by
  2527.   adding this line into the end of your halt script:
  2528.  
  2529.          /sbin/backupsd /dev/backups killpower
  2530.  
  2531.   This will only kill the power if there is no power being supplied to
  2532.   your UPS.
  2533.  
  2534.   *** Testing the stuff ***
  2535.  
  2536.   This is just a short section saying this:
  2537.  
  2538.   BE CAREFUL!
  2539.  
  2540.   I recommend backing up your linux partitions, syncing several times
  2541.   before testing and just being careful in general.  Of course, I'm just
  2542.   recommending this.  I wasn't careful at all, and had to clean my
  2543.   partition several times testing my config.  But it works.  :)
  2544.  
  2545.   *** Where to Get It ***
  2546.  
  2547.   Miquel van Smoorenburg's SysVInit can be gotten at:
  2548.  
  2549.   sunsite.unc.edu:/pub/Linux/system/Daemons/SysVinit-2.50.tgz
  2550.  
  2551.   and a fix for some bash shells is right next-door as:
  2552.  
  2553.   sunsite.unc.edu:/pub/Linux/system/Daemons/SysVinit-2.50.patch1
  2554.  
  2555.   As to getting this HOWTO, you can email me.  docwhat@uiuc.edu  with
  2556.   the subject saying 'request' and the keyword 'backups' in body of the
  2557.   letter.  (I may automate this, and other stuff)
  2558.  
  2559.   *** Credit Where Credit's Due Dept. ***
  2560.  
  2561.   Thanks to Miquel van Smoorenburg <miquels@drinkel.nl.mugnet.org> for
  2562.   his wonderful SysVInit package and his powerd.c which helped me very
  2563.   much.
  2564.  
  2565.   Christian Holtje <docwhat@uiuc.edu> Documentation backupsd.c (what
  2566.   wasn't Miquel's) rc.power
  2567.  
  2568.   Ben Galliart <bgallia@orion.it.luc.edu> The cable Information for the
  2569.   RS-232 standard Lousy Jokes (none quoted here)
  2570.  
  2571.   /*  backupsd.c -- Simple Daemon to catch power failure signals from a
  2572.    *                Back-UPS (from APC).
  2573.    *
  2574.    *  Parts of the code are from Miquel van Smoorenburg's powerd.c
  2575.    *  Other parts are original from Christian Holtje <docwhat@uiuc.edu>
  2576.    *  I believe that it is okay to say that this is Public Domain, just
  2577.    *  give credit, where credit is due.
  2578.    *
  2579.    *  Disclaimer:  We make NO claims to this software, and take no
  2580.    *               resposibility for it's use/misuse.
  2581.    */
  2582.  
  2583.   #include <sys/types.h>
  2584.   #include <sys/ioctl.h>
  2585.   #include <fcntl.h>
  2586.   #include <errno.h>
  2587.   #include <stdlib.h>
  2588.   #include <unistd.h>
  2589.   #include <stdio.h>
  2590.   #include <signal.h>
  2591.  
  2592.   /* This is the file needed by SysVInit */
  2593.   #define PWRSTAT         "/etc/powerstatus"
  2594.  
  2595.   void powerfail(int fail);
  2596.  
  2597.   /* Main program. */
  2598.   int main(int argc, char **argv)
  2599.   {
  2600.     int fd;
  2601.     int killpwr_bit = TIOCM_RTS;
  2602.     int flags;
  2603.     int status, oldstat = -1;
  2604.     int count = 0;
  2605.  
  2606.     if (argc < 2) {
  2607.           fprintf(stderr, "Usage: %s <device> [killpower]\n", argv[0]);
  2608.           exit(1);
  2609.     }
  2610.  
  2611.     /* Open the the device */
  2612.     if ((fd = open(argv[1], O_RDWR | O_NDELAY)) < 0) {
  2613.           fprintf(stderr, "%s: %s: %s\n", argv[0], argv[1], sys_errlist[errno]);
  2614.           exit(1);
  2615.     }
  2616.  
  2617.     if ( argc >= 3  && (strcmp(argv[2], "killpower")==0) )
  2618.         {
  2619.             /* Let's kill the power! */
  2620.             fprintf(stderr, "%s: Attempting to kill the power!\n",argv[0] );
  2621.             ioctl(fd, TIOCMBIS, &killpwr_bit);
  2622.             /* Hmmm..... If you have a power outtage, you won't make it! */
  2623.             exit(0);
  2624.         }
  2625.     else
  2626.         /* Since we don't want to kill the power, clear the RTS. (killpwr_bit) */
  2627.         ioctl(fd, TIOCMBIC, &killpwr_bit);
  2628.  
  2629.   /* Become a daemon. */
  2630.     switch(fork()) {
  2631.     case 0: /* I am the child. */
  2632.                   setsid();
  2633.                   break;
  2634.     case -1: /* Failed to become daemon. */
  2635.                   fprintf(stderr, "%s: can't fork.\n", argv[0]);
  2636.                   exit(1);
  2637.     default: /* I am the parent. */
  2638.                   exit(0);
  2639.     }
  2640.  
  2641.     /* Now sample the DCD line. */
  2642.     while(1) {
  2643.         ioctl(fd, TIOCMGET, &flags);
  2644.         status = (flags & TIOCM_CD);
  2645.         /* Did DCD jumps to high? Then the power has failed. */
  2646.         if (oldstat == 0 && status != 0) {
  2647.             count++;
  2648.             if (count > 3) powerfail(0);
  2649.             else { sleep(1); continue; }
  2650.         }
  2651.         /* Did DCD go down again? Then the power is back. */
  2652.         if (oldstat > 0 && status == 0) {
  2653.             count++;
  2654.             if (count > 3) powerfail(1);
  2655.             else { sleep(1); continue; }
  2656.         }
  2657.         /* Reset count, remember status and sleep 2 seconds. */
  2658.         count = 0;
  2659.         oldstat = status;
  2660.         sleep(2);
  2661.     }
  2662.     /* Error! (shouldn't happen) */
  2663.     return(1);
  2664.   }
  2665.  
  2666.   /* Tell init the power has either gone or is back. */
  2667.   void powerfail(ok)
  2668.   int ok;
  2669.   {
  2670.     int fd;
  2671.  
  2672.     /* Create an info file needed by init to shutdown/cancel shutdown */
  2673.     unlink(PWRSTAT);
  2674.     if ((fd = open(PWRSTAT, O_CREAT|O_WRONLY, 0644)) >= 0) {
  2675.           if (ok)
  2676.                   write(fd, "OK\n", 3);
  2677.           else
  2678.                   write(fd, "FAIL\n", 5);
  2679.           close(fd);
  2680.     }
  2681.     kill(1, SIGPWR);
  2682.   }
  2683.  
  2684.   /* End of File */
  2685.  
  2686.   8.13.1.3.  More notes
  2687.  
  2688.   From ockers@carnot02.maem.umr.edu Mon Jan 16 15:27:29 1995
  2689.   Newsgroups: comp.os.linux.hardware
  2690.   Subject: Back-UPS, backupsd, and low battery signal
  2691.   From: ockers@carnot02.maem.umr.edu (Jim Ockers)
  2692.   Date: 12 Jan 1995 04:22:44 GMT
  2693.   Reply-To: ockers@umr.edu
  2694.   Organization: the all-male wasteland of Rolla, MO
  2695.   NNTP-Posting-Host: carnot02.maem.umr.edu
  2696.   X-Newsreader: TIN [version 1.2 PL2]
  2697.  
  2698.   Hello all,
  2699.  
  2700.   I use the backupsd on my linux system and I like it a lot.  I also
  2701.   run Windows NT when I have to and it has a UPS daemon too.  The pinouts
  2702.   required by Windows NT are different from the ones you specify in the
  2703.   program but that is easily changed since I have the source for your
  2704.   program..
  2705.  
  2706.   Anyways I was browsing through the Windows NT knowledge base (KB) and
  2707.   found something interesting.  If you look in the documentation for your
  2708.   Back-UPS under "computer interface port" you will see that this UPS will
  2709.   send a Low Battery signal at least two minutes before the battery fails.
  2710.  
  2711.   At least the manual for my Back-UPS 400 says that...
  2712.  
  2713.   However they also speak some Electrical Engineering gibberish ("Outputs ...
  2714.   are actually open collector outputs which must be pulled up to a common
  2715.   referenced supply no greater than +40 Vdc.  The transistors are capable
  2716.   of a maximum non-inductive load of 25mAdc.)
  2717.  
  2718.   Well that means nothing to me, but what I discovered in the NT KB was
  2719.   that it is possible to use the low battery signal in the same manner that
  2720.   the other signals are used.  The output from pin 5 on the UPS should go
  2721.   to the pin on which you are reading the LowBatt signal into the computer.
  2722.   When that line goes high, the battery is running out of charge.  When
  2723.   the situation is normal, that line will be low.  (Hi/Lo in a standard
  2724.   RS-232 signal, just like the other lines.)
  2725.  
  2726.   What they don't tell you in the APC manual, and they should, is that
  2727.   you need to buy a 10 KOhm resistor (50 cents at Radio Shack) and connect
  2728.   pins 5 and 8 on the UPS side using the resistor.  Pin 8 provides the
  2729.   "common referenced supply no greater than 40vdc".  Here's how you would
  2730.   make the cable (the 1st three lines are the same as the HOWTO):
  2731.  
  2732.          PC side                         UPS side
  2733.   pin      7 <------------------------------> 1                 ShutDownUPS
  2734.            1 <------------------------------> 2                 LineFail
  2735.            5 <------------------------------> 4 (same as 9 )    GND
  2736.            ? your choice  <-----------------> 5                 LowBatt
  2737.                                               |
  2738.                                                > 10
  2739.                                               <  KOhm
  2740.                                               |
  2741.                                               8
  2742.  
  2743.   So then when the LowBatt line is HIGH then the computer has 2 minutes
  2744.   to shut down before the battery runs out.
  2745.  
  2746.   This is not mentioned in the Back-UPS HOWTO nor is it addressed in the
  2747.   backupsd source.  However I would think that it would be a Good Thing
  2748.   to have in there; especially since a power failure would not require
  2749.   a shutdown unless the UPS batteries were low.  In most cases it would
  2750.   mean that the backupsd could send a warning to everyone if the LineFails,
  2751.   and give everyone a one (or two) minute warning when the batteries start
  2752.   running down.
  2753.  
  2754.   As far as I know this applies to all the APC Back-UPS and Smart-UPS
  2755.   products.  These instructions were for a Smart-UPS 900,1250, and 2000
  2756.   according to the NT KB.  However they have been tested with a Back-UPS
  2757.   400 running Windows NT and everything works properly...
  2758.  
  2759.   I'd sure like to have a backupsd that handled the LowBatt situation too.
  2760.   Does anyone feel like modifying the backupsd.c source so that it will do
  2761.   this too?  (I can't program in C yet...)
  2762.  
  2763.   P.S. The APC manual says to use only pin 4 as the common and even though
  2764.   in the diagram it says that pin 9 is connected to pin 4 you might want to
  2765.   be sure and use pin 4 .  This differs from the instructions in the HOWTO.
  2766.  
  2767.   P.P.S. I mailed this to the Back-UPS HOWTO authors.
  2768.  
  2769.   --
  2770.   Jim  (ockers@umr.edu)                   Ask me about Linux!
  2771.   http://www.umr.edu/~ockers/ - home page
  2772.  
  2773.   From: Peter Kammer <pkammer@liege.ICS.UCI.EDU>
  2774.   To: "Harvey J. Stein" <hjstein@math.huji.ac.il>
  2775.   cc: "Christian G. Holtje" <docwhat@uiuc.edu>
  2776.   Subject: UPS-Howto--minor correction
  2777.   Date: Mon, 07 Oct 1996 12:00:16 -0700
  2778.  
  2779.   Mr. Stein,
  2780.  
  2781.           Let me first thank you for putting together and maintaining the
  2782.   Linux UPS-HowTo document.  I recently attached a APC Back-UPS 400 to a
  2783.   a Linux box and the document turned out to be very helpful.
  2784.           I would like to suggest a correction to the the text diagrams which
  2785.   accompany the description in section 11.5.2.  The diagrams are presented as
  2786.   being the rear of the plug.  This in mind, the diagram of the male is
  2787.   backwards:
  2788.  
  2789.            ---------------------     Male Side! (This goes into the UPS)
  2790.             \  B   R  *  *  * /
  2791.               \  *  *  *  G  /
  2792.                 ------------
  2793.  
  2794.   _From the rear_, the pins on the male connector are numbered
  2795.   right-to-left.  The correct diagram should be:
  2796.  
  2797.            ---------------------     Male Side! (This goes into the UPS)
  2798.             \  *   *  *  R  B /
  2799.               \  G  *  *  *  /
  2800.                 ------------
  2801.  
  2802.   Similarly, the numbered diagram should be labeled as for the rear of the
  2803.   female plug.
  2804.  
  2805.            ---------------------
  2806.             \  1   2  3  4  5 /
  2807.               \  6  7  8  9  /
  2808.                 ------------
  2809.  
  2810.   The rear of the male is numbered the reverse:
  2811.  
  2812.            ---------------------
  2813.             \  5   4  3  2  1 /
  2814.               \  9  8  7  6  /
  2815.                 ------------
  2816.  
  2817.   This caused us some confusion until we realized our mistake.  With four
  2818.   different configurations to be aware of (front, rear) x (male, female) it
  2819.   is easy to get confused.  Even now, reference in hand, I keep reexamining
  2820.   my diagrams.
  2821.  
  2822.   It might also be helpful to add a reference to the APC technical document
  2823.   for the Back-UPS line which is available on-line at:
  2824.  
  2825.      http://www.apcc.com/english/techs/techref4/224e.htm
  2826.  
  2827.   Once we corrected our wiring, setting up the software was relatively simple
  2828.   thanks to your documentation.  We used the alternative (using TD to kill
  2829.   the UPS power rather than RTS) wiring scheme and ran into few problems.
  2830.   Your efforts in maintaining this information are much appreciated.
  2831.  
  2832.   ------------
  2833.   Peter Kammer                       Dept. of Information and Computer Science
  2834.   pkammer@ics.uci.edu                University of California
  2835.   http://www.ics.uci.edu/~pkammer/   Irvine, CA 92697-3425
  2836.  
  2837.   8.13.1.4.  APC Back-UPS Pro 650
  2838.  
  2839.        From: Troy Muller <tmuller@agora.rdrop.com>
  2840.        Sender: tmuller@napalm.it.wsu.edu
  2841.        To: abel@netvision.net.il
  2842.        Subject: APC Back-UPS Pro 650
  2843.        Date: Sun, 06 Apr 1997 12:50:40 -0700
  2844.  
  2845.        Dear Mr. Stein,
  2846.  
  2847.        I have a Back-UPS Pro 650 from APC and finally got it working with a
  2848.        standard APC cable.
  2849.  
  2850.        I used cable number 940-0023A and Enhanced_APC_BackUPS software.  My
  2851.        only grudge is the software broadcasts every 2 seconds, but hacking the
  2852.        dowall.c code to sleep 10 sec before broadcasting seems to limit it to
  2853.        every 10 seconds with a 2-3 message queued to be printed (ie. much more
  2854.        acceptable).
  2855.  
  2856.   8.13.2.  APC Smart-UPS
  2857.  
  2858.   Many people have APC Smart UPSs.  There seem to be packages for using
  2859.   them in smart modes (see the afore mentioned packages
  2860.   Enhanced_APC_UPSD-v1.4.tar.gz, apcd-0.5.tar.gz, and
  2861.   smupsd-0.7-1.i386.rpm described in section ``Software'').  I don't
  2862.   know how the support in each package is.  It seems that APC still
  2863.   refuses to release the protocol for the ``smart'' mode without a non-
  2864.   disclosure agreement, so everyone's left reverse engineering it.
  2865.  
  2866.   The general consensus is to buy from a brand which does release the
  2867.   information, such as Best.
  2868.  
  2869.   Another possibility is to run the SCO Unix version of APC's Powerchute
  2870.   UPS control software under Linux via the iBCS compatibility package.
  2871.   I'm told by Clive A. Stubbings (cas@vjet.demon.co.uk) that this works
  2872.   nicely after some install script adjustments.  He says that the only
  2873.   problem is "the GUI stuff seems to have difficulty controlling non-
  2874.   local UPSs across the net".
  2875.  
  2876.   If you have an APC Smart-UPS, and you can't get any of the above
  2877.   software to work in smart mode, you can still use it in dumb mode.
  2878.   The following sections detail how to do that.  In particular, I've
  2879.   received messages from people regarding the Model 600, the Model 700,
  2880.   and the model 1400.  You'll probably have to hack powerd.c as outlined
  2881.   in section ``Reverse-engineering cables and hacking powerd.c''.
  2882.  
  2883.   8.13.2.1.  APC Smart-UPS, Model 600
  2884.  
  2885.   From dangit@netcom.com Mon Aug 22 10:16:23 1994
  2886.   Newsgroups: comp.os.linux.misc
  2887.   Subject: UPS Monitoring Cable For APC
  2888.   From: dangit@netcom.com (Lam Dang)
  2889.   Date: Fri, 19 Aug 1994 11:56:28 GMT
  2890.   Organization: NETCOM On-line Communication Services (408 261-4700 guest)
  2891.   X-Newsreader: TIN [version 1.2 PL1]
  2892.  
  2893.   [Didn't make it the first time.]
  2894.  
  2895.   A few netters have asked about UPS monitoring cables.  This is what I
  2896.   found when I made one for my APC Smart-UPS, Model 600.  A disclaimer is in
  2897.   order.  This is just an experimenter's report; use it at your own risks.
  2898.   Please read the User's Manual first, especially Section 6.4, Computer
  2899.   Interface Port.
  2900.  
  2901.   The cable is to run between a 9-pin female connector on the UPS and a
  2902.   25-pin male connector on the PC.  Since I cut off one end of a 9-pin
  2903.   cable and replaced it with a 25-pin connector, I had to be VERY
  2904.   CAREFUL ABOUT PIN NUMBERS.  The 25-pin hood is big enough to contain a
  2905.   voltage regulator and two resistors.  I got all the materials (listed
  2906.   below) from Radio Shack for less than 10 bucks.  As required by Windows NT
  2907.   Advanced Server 3.5 (Beta 2), the "interface" between the UPS connector
  2908.   and the PC connector is as follows:
  2909.  
  2910.           UPS (9-pin)              PC (25-pin)
  2911.  
  2912.           1 (Shutdown)             20 (DTR)
  2913.           3 (Line Fail)             5 (CTS)
  2914.           4 (Common)                7 (GND)
  2915.           5 (Low Battery)           8 (DCD)
  2916.           9 (Chassis Ground)        1 (Chassis Ground)
  2917.  
  2918.   This is pretty straightforward.  You can use UPS pin 6 instead of 3
  2919.   (they're the inverse of each other).  The complication is in pulling up
  2920.   UPS open collector pins 3 (or 6) and 5.
  2921.  
  2922.   This APC model provides an unregulated output of 24 Vdc at UPS pin 8. The
  2923.   output voltage is available all the time (at least until some time after
  2924.   Low Battery has been signalled).  The supply is limited to 40 mA.  To
  2925.   pull up, UPS pin 8 is input to a +5 Vdc voltage regulator.  The output of
  2926.   the regulator goes into two 4.7K resistors.  The other end of one
  2927.   resistor connects both UPS pin 3 (Line Fail) and PC pin 5 (CTS).  That
  2928.   of the other resistor connects both UPS pin 5 (Low Battery) and PC pin 8
  2929.   (DCD).  The two resistors draw about 2 mA when closed.
  2930.  
  2931.   Test your cable without connecting it to the PC.  When the UPS is on
  2932.   line, pins 5 (CTS) and 8 (DCD) at the PC end of the cable should be very
  2933.   close to 5 Vdc, and applying a high to pin 20 (DTR) for 5 seconds should
  2934.   have no effect.  Now pull the power plug to put the UPS on battery.  Pin
  2935.   5 (CTS) should go down to zero Vdc, pin 8 (DCD) should stay the same at 5
  2936.   Vdc, and applying a high to pin 20 (DTR), e.g., by shorting pins 8 and 20,
  2937.   should shut down the UPS after about 15 seconds.
  2938.  
  2939.   Keep the UPS on battery until Low Battery is lighted on its front panel.
  2940.   Now pin 8 (DCD) should go down to zero Vdc too.  Wait until the UPS
  2941.   battery is recharged.  Then connect your cable to the PC, disable the UPS
  2942.   option switches by turning all of them ON, and run your favorite UPS
  2943.   monitoring software.
  2944.  
  2945.   For those who want to run it with Windows NT Advanced Server, the UPS
  2946.   interface voltages are NEGATIVE for both power failure (using UPS pin 3)
  2947.   and low battery conditions, and POSITIVE for remote shutdown.  Serial
  2948.   line parameters such as baud rate don't matter.
  2949.  
  2950.   I haven't tested my cable with Linux powerd.  When you do, please let us
  2951.   know.  I run NT as often as Linux on the same PC.  I must conform to NT's
  2952.   UPS scheme.  Perhaps somebody can modify powerd to work with it and post
  2953.   the source code here.
  2954.  
  2955.   List of materials:
  2956.  
  2957.           1 shielded D-sub connector hood (Radio Shack 276-1510)
  2958.           1 25-pin female D-sub crimp-type connector (276-1430)
  2959.           1 7805 +5Vdc voltage regulator (276-1770)
  2960.           2 4.7K resistors
  2961.           1 component perfboard (276-148)
  2962.           1 cable with at least one 9-pin male connector.
  2963.  
  2964.   You'll need a multimeter, a soldering iron, and a couple of hours.
  2965.  
  2966.   Hope this helps.
  2967.  
  2968.   Regards,
  2969.  
  2970.   --
  2971.   Lam Dang
  2972.   dangit@netcom.com
  2973.  
  2974.   8.13.2.2.  APC Smart-UPS 700
  2975.  
  2976.   Here're some details for running an APC Smart-UPS 700 in dumb mode.
  2977.  
  2978.   It has a clever usage of a transistor in the cable so that the UPS
  2979.   will turn off when the computer is turned off.  And it includes a
  2980.   powerd.c which also does a fast low battery shutdown.
  2981.  
  2982.   Also, note that Markus' is also using init's new capabilities.  So we
  2983.   have here another illustration of how to use the new init to your
  2984.   advantage.
  2985.  
  2986.   From: Markus Eiden <Markus@eiden.de>
  2987.   Sender: eiden@eiden.de
  2988.   To: "Harvey J. Stein" <abel@netvision.net.il>
  2989.   Subject: Re: APC Smart-UPS
  2990.   Date: Sun, 30 Mar 1997 16:21:05 +0200
  2991.  
  2992.   I'm using an APC Smart-UPS 700 for my Linux box, running 2.0.21 on an
  2993.   ASUS-Board.
  2994.  
  2995.   To use some features of the UPS you need four things:
  2996.  
  2997.   1) You have to build a RS232-cable with a small interface.
  2998.   2) You need the powerd-source from the sysvinit-package (I use version 2.65
  2999.       from Miquel van Smoorenburg). Then you have to patch his powerd.
  3000.   3) You have to change your /etc/inittab
  3001.   4) You need a script to run some commands if the power is down or battery
  3002.      is low.
  3003.  
  3004.   Some features:
  3005.  
  3006.   When the power goes down, a script will start and a syslog-entry is done.
  3007.  
  3008.   If the battery is low, an other  script will start
  3009.   (which shutdown your computer of course) and a syslog-entry is done.
  3010.  
  3011.   If you shutdown your computer and the power is down, the UPS will be shut
  3012.   down too.
  3013.  
  3014.    1)Let's start with the cable:
  3015.   ================================
  3016.  
  3017.   If you have a look at the back side of you UPS you will see a female
  3018.   connector like this:
  3019.  
  3020.                8             1: Shutdown the UPS when the power is down and
  3021.                                  pin 1 is high.
  3022.        X   X   X   X         3: Goes low on "Linefail"
  3023.      X   X   X   X   X       4: GND
  3024.                              5: Goes low on  "Low battery"
  3025.      1       3   4   5       8: +24V
  3026.  
  3027.   On the other hand at the back side of your PC there exist a male
  3028.   connector like this:
  3029.  
  3030.            8       6         1: DCD
  3031.        X   X   X   X         4: DTR
  3032.      X   X   X   X   X       5: GND
  3033.      5   4           1       6: DSR
  3034.                              8: CTS
  3035.  
  3036.   You have to build the following interface between these connectors:
  3037.  
  3038.    PC                                                           UPS
  3039.  
  3040.                                            #------------------  (8)
  3041.                                            |
  3042.                                           470 Ohm
  3043.                                            |
  3044.              #-----#-----#-----#-----#-----#----- ca. 9-12V
  3045.              |     |     |     |     |     |
  3046.              47    3.3   3.3   3.3   1     470
  3047.              kOhm  kOhm  kOhm  kOhm  kOhm  Ohm
  3048.              |     |     |     |     |     |
  3049.    (8) ------------------------#     |     |
  3050.              |     |     |           |     |
  3051.    (6) ------------#------------------------------------------- (5)
  3052.              |           |           |     |
  3053.    (1) ------------------#------------------------------------- (3)
  3054.              |                       |     |
  3055.              |                      C#------------------------- (1)
  3056.              |                      -|     |
  3057.              |                    B/       |
  3058.    (4) ------#-----12kOhm---------|        |
  3059.                                    \>E     |
  3060.                                    |       |
  3061.    (5)-----------------------------#-------#------------------- (4)
  3062.  
  3063.     -I use a "BC140" - transistor, but nearly any simple NPN-transistor
  3064.      should work  ;-)
  3065.  
  3066.     -The transistor works as an "inverter". If you shutdown your PC,
  3067.      AND the power ist down, then pin 4(PC) goes low and 1(UPS) goes
  3068.      high. This shuts down the UPS  for saving batteries power.
  3069.  
  3070.    2) The powerd-Source:
  3071.   =======================
  3072.  
  3073.   I patched the powerd-source very little (so it is really the source
  3074.   of Miquel).
  3075.  
  3076.   (a) Give an "alert" to the syslogd if 8(PC, CTS) is down (Cause
  3077.        the cable is not connected)
  3078.  
  3079.   (b) DCD droped to zero => power has failed => call powerfail(0)
  3080.       => Give INIT_CMD_POWERFAIL to the init-process
  3081.  
  3082.   (c) DCD comes up again => power is back => call powerfail(1)
  3083.       => Give INIT_CMD_POWEROK to the init-process
  3084.  
  3085.   (d) DSR and DCD are dropped to zero => power has failed and
  3086.       battery is low => call powerfail(2) => Give
  3087.       INIT_CMD_POWERFAILNOW to the init-process
  3088.  
  3089.    Thats it.
  3090.  
  3091.   ------------------------------>8---- Schnipp ----------------------------
  3092.  
  3093.   /*
  3094.    * powerd       Monitor the DCD line of a serial port connected to
  3095.    *              an UPS. If the power goes down, notify init.
  3096.    *              If the power comes up again, notify init again.
  3097.    *              As long as the power is OK, the DCD line should be
  3098.    *              "HIGH". When the power fails, DCD should go "LOW".
  3099.    *              Powerd keeps DTR high so that you can connect
  3100.    *              DCD and DTR with a resistor of 10 Kilo Ohm and let the
  3101.    *              UPS or some relais pull the DCD line to ground.
  3102.    *              You also need to connect DTR and DSR together. This
  3103.    *              way, powerd can check now and then if DSR is high
  3104.    *              so it knows the UPS is connected!!
  3105.    *
  3106.    * Usage:       powerd /dev/cua4 (or any other serial device).
  3107.    *
  3108.    * Author:      Miquel van Smoorenburg, <miquels@drinkel.cistron.nl>.
  3109.    *              Some minor changes by Markus Eiden, <Markus@Eiden.de>
  3110.    *              for the APC-Smart-UPS-powerd.
  3111.    *
  3112.    * Version:     1.31,  29-Feb-1996.
  3113.    *
  3114.    *              This program was originally written for my employer,
  3115.    *                      ** Cistron Electronics **
  3116.    *              who has given kind permission to release this program
  3117.    *              for general puppose.
  3118.    *
  3119.    *              Copyright 1991-1996 Cistron Electronics.
  3120.    *
  3121.    *              This program is free software; you can redistribute it and/or
  3122.    *              modify it under the terms of the GNU General Public License
  3123.    *              as published by the Free Software Foundation; either version
  3124.    *              2 of the License, or (at your option) any later version.
  3125.    *
  3126.    *              Some minor changes for the APC-powerd by Markus Eiden
  3127.    *              Markus@Eiden.de
  3128.    */
  3129.  
  3130.   /* Use the new way of communicating with init. */
  3131.   #define NEWINIT
  3132.  
  3133.   #include <sys/types.h>
  3134.   #include <sys/stat.h>
  3135.   #include <sys/ioctl.h>
  3136.   #include <fcntl.h>
  3137.   #include <errno.h>
  3138.   #include <stdlib.h>
  3139.   #include <unistd.h>
  3140.   #include <stdio.h>
  3141.   #include <signal.h>
  3142.   #include <syslog.h>
  3143.   #include <string.h>
  3144.   #include "paths.h"
  3145.   #ifdef NEWINIT
  3146.   #include "initreq.h"
  3147.   #endif
  3148.  
  3149.   #ifndef SIGPWR
  3150.   #  define SIGPWR SIGUSR1
  3151.   #endif
  3152.  
  3153.   #ifdef NEWINIT
  3154.   void alrm_handler()
  3155.   {
  3156.   }
  3157.   #endif
  3158.  
  3159.   /* Tell init the power has either gone or is back. */
  3160.   void powerfail(ok)
  3161.   int ok;
  3162.   {
  3163.     int fd;
  3164.   #ifdef NEWINIT
  3165.     struct init_request req;
  3166.  
  3167.     /* Fill out the request struct. */
  3168.     memset(&req, 0, sizeof(req));
  3169.     req.magic = INIT_MAGIC;
  3170.  
  3171.     /* INIT_CMD_* are definied in initreq.h                   *
  3172.      * Have a look at  init.c and /etc/inittab                *
  3173.      *                                                        *
  3174.      * ok=0 -> INIT_CMD_POWERFAIL      -> powerwait           *
  3175.      * ok=1 -> INIT_CMD_POWEROK        -> powerokwait         *
  3176.      * ok=2 -> INIT_CMD_POWERFAILNOW   -> powerfailnow        */
  3177.  
  3178.     switch (ok) {
  3179.       case 0 : req.cmd = INIT_CMD_POWERFAIL;
  3180.                /* Linefail -> warning */
  3181.                break;
  3182.       case 1 : req.cmd = INIT_CMD_POWEROK;
  3183.                /* Power is back -> cancel warning */
  3184.                break;
  3185.       case 2 : req.cmd = INIT_CMD_POWERFAILNOW;
  3186.                /* Linefail and LowBatt -> reboot */
  3187.                break;
  3188.                }
  3189.  
  3190.     /* Open the fifo (with timeout) */
  3191.     signal(SIGALRM, alrm_handler);
  3192.     alarm(3);
  3193.     if ((fd = open(INIT_FIFO, O_WRONLY)) >= 0
  3194.                   && write(fd, &req, sizeof(req)) == sizeof(req)) {
  3195.           close(fd);
  3196.           return;
  3197.     }
  3198.     /* Fall through to the old method.. */
  3199.   #endif
  3200.  
  3201.     /* Create an info file for init. */
  3202.     unlink(PWRSTAT);
  3203.     if ((fd = open(PWRSTAT, O_CREAT|O_WRONLY, 0644)) >= 0) {
  3204.           if (ok)
  3205.                   write(fd, "OK\n", 3);
  3206.           else
  3207.                   write(fd, "FAIL\n", 5);
  3208.           close(fd);
  3209.     }
  3210.     kill(1, SIGPWR);
  3211.   }
  3212.  
  3213.   /* Main program. */
  3214.   int main(int argc, char **argv)
  3215.   {
  3216.     int fd;
  3217.     int dtr_bit = TIOCM_DTR;
  3218.     int flags;
  3219.     int status, oldstat = -1;
  3220.     int count = 0;
  3221.     int tries = 0;
  3222.     int powerfailed = 0;
  3223.     int rebootnow   = 0;
  3224.     if (argc < 2) {
  3225.           fprintf(stderr, "Usage: powerd <device>\n");
  3226.           exit(1);
  3227.     }
  3228.  
  3229.     /* Start syslog. */
  3230.     openlog("powerd", LOG_CONS|LOG_PERROR, LOG_DAEMON);
  3231.  
  3232.     /* Open monitor device. */
  3233.     if ((fd = open(argv[1], O_RDWR | O_NDELAY)) < 0) {
  3234.           syslog(LOG_ERR, "%s: %s", argv[1], sys_errlist[errno]);
  3235.           closelog();
  3236.           exit(1);
  3237.     }
  3238.  
  3239.     /* Line is opened, so DTR is high. Force it anyway to be sure. */
  3240.  
  3241.     /* USE: low Batt -> Reboot -> DTR goes low                     *
  3242.      * transistor is open -> shutdown-pin  of the UPS goes         *
  3243.      * high -> UPS goes down after about 20s. If there is a        *
  3244.      * linefail and the computer is off, the                       *
  3245.      * UPS goes down. If the power is back, the                    *
  3246.      * UPS goes on, the computer boots, and powerd                 *
  3247.      * is startet.                                                 */
  3248.  
  3249.    /* Verwendung: Die UPS meldet low Batt -> Reboot -> DTR geht   *
  3250.      * auf Low -> Transistor oeffnet -> Shutdown der UPS geht auf  *
  3251.      * High -> UPS schaltet sich nach circa 20s aus. Bei jedem     *
  3252.      * Linefail und ausgeschaltetem Computer, schaltet sich die    *
  3253.      * UPS aus. Kommt der Strom zurueck, dann schaltet sich die    *
  3254.      * UPS selbstaendig ein, der Computer bootet, und der powerd   *
  3255.      * wird gestartet.                                             */
  3256.  
  3257.     ioctl(fd, TIOCMBIS, &dtr_bit);
  3258.  
  3259.     /* Daemonize. */
  3260.     switch(fork()) {
  3261.           case 0: /* Child */
  3262.                   closelog();
  3263.                   setsid();
  3264.                   break;
  3265.           case -1: /* Error */
  3266.                   syslog(LOG_ERR, "can't fork.");
  3267.                   closelog();
  3268.                   exit(1);
  3269.           default: /* Parent */
  3270.                   closelog();
  3271.                   exit(0);
  3272.     }
  3273.  
  3274.     /* Restart syslog. */
  3275.     openlog("powerd", LOG_CONS, LOG_DAEMON);
  3276.  
  3277.      syslog(LOG_INFO, "APCpowerd started...");
  3278.  
  3279.     /* Now sample the DCD line. */
  3280.     while(1) {
  3281.           /* Get the status. */
  3282.           ioctl(fd, TIOCMGET, &flags);
  3283.  
  3284.           /* Check the connection: CTS should be high. */
  3285.           tries = 0;
  3286.           /* TIOCM_*- have a look at  .../ams/termios.h */
  3287.           while((flags & TIOCM_CTS) == 0) {
  3288.                   /* Keep on trying, and warn every two minutes. */
  3289.                   if ((tries % 60) == 0)
  3290.                       syslog(LOG_ALERT, "UPS connection error");
  3291.                   sleep(2);
  3292.                   tries++;
  3293.                   ioctl(fd, TIOCMGET, &flags);
  3294.           }
  3295.           if (tries > 0)
  3296.                   syslog(LOG_ALERT, "UPS connection OK");
  3297.  
  3298.           /* Calculate present status. */
  3299.           status = (flags & TIOCM_CAR);
  3300.  
  3301.           /* Did DCD drop to zero? Then the power has failed. */
  3302.           if (oldstat != 0 && status == 0) {
  3303.                   count++;
  3304.                   if (count > 3) {
  3305.                           powerfailed = 1;
  3306.                           powerfail(0);
  3307.                           }
  3308.                   else {
  3309.                           sleep(1);
  3310.                           continue;
  3311.                   }
  3312.           }
  3313.           /* Did DCD come up again? Then the power is back. */
  3314.           if (oldstat == 0 && status > 0) {
  3315.                   count++;
  3316.                   if (count > 3) {
  3317.                           powerfailed = 0;
  3318.  
  3319.                           /* eigentlich unnoetig: */
  3320.                           rebootnow = 0;
  3321.  
  3322.                           powerfail(1);
  3323.                           }
  3324.                   else {
  3325.                           sleep(1);
  3326.                           continue;
  3327.                   }
  3328.           }
  3329.  
  3330.           /* Low battery and Linefail ? */
  3331.           if (rebootnow==0)
  3332.           if (powerfailed==1)
  3333.           if ((flags & TIOCM_DSR) == 0)
  3334.           {
  3335.              rebootnow=1;
  3336.              powerfail(2);
  3337.  
  3338.           }
  3339.  
  3340.           /* Reset count, remember status and sleep 2 seconds. */
  3341.           count = 0;
  3342.           oldstat = status;
  3343.           sleep(2);
  3344.     }
  3345.     /* Never happens */
  3346.     return(0);
  3347.   }
  3348.  
  3349.   ------------------- schnap ------------ 8<-----------------------------
  3350.  
  3351.    3) Change your inittab:
  3352.   =========================
  3353.  
  3354.   Init gets the INIT_CMDs and will start a script:
  3355.  
  3356.   pf::powerwait:/sbin/init.d/powerfail    start
  3357.   pn::powerfailnow:/sbin/init.d/powerfail now
  3358.   po::powerokwait:/sbin/init.d/powerfail  stop
  3359.  
  3360.   (Which means for example: if the power has failed (powerwait) start the
  3361.   script /sbin/init.d/powerfail with the parameter "start".)
  3362.  
  3363.   4) The powerfail-Script
  3364.   ========================
  3365.  
  3366.   ------------------ 8< ------- schnipp ---------------
  3367.  
  3368.   #! /bin/sh
  3369.   # Copyright (c) 1997 Markus Eiden, Markus@Eiden.de
  3370.   #
  3371.  
  3372.   case "$1" in
  3373.       start)
  3374.           echo "THE POWER IS DOWN!" | wall
  3375.           logger "Powerfail"
  3376.           ;;
  3377.       now)
  3378.           echo "BATTERY IS LOW! Shutdown in 1 minute" | wall
  3379.           logger "Battery is low, shutdown in 1 minute"
  3380.           sync
  3381.           /sbin/shutdown -r -t 5 +1
  3382.           ;;
  3383.       stop)
  3384.           echo "THE POWER IS BACK!!" | wall
  3385.           logger "Power is back"
  3386.  
  3387.           /sbin/shutdown -c >/dev/null 2>/dev/null
  3388.  
  3389.           ;;
  3390.     *)
  3391.           echo "Usage: $0 {start|now|stop}"
  3392.           exit 1
  3393.           ;;
  3394.   esac
  3395.  
  3396.   exit 0
  3397.  
  3398.   --------------------- >8 ----------- schnapp -------------
  3399.  
  3400.   Well, that should be easy ;-)
  3401.  
  3402.   You are ready now, but be careful: It works for me, but I really can't
  3403.   guarantee that any of this will work for you.
  3404.  
  3405.   Some advice at the end:  If /sbin/init.d/powerfail shuts down your
  3406.   PC  then DTR goes down, so the shutdown pin (UPS) goes high.
  3407.   >From that time it takes about 20 or 30 seconds for the UPS to shut down.
  3408.   It is your job to prevent your Linux-box from booting within these 20
  3409.   seconds (in particular to mount the filesystem). On my system it was no
  3410.   problem. There are four easy ways to prevent the PC from the fast booting:
  3411.    1) The BIOS should do some routines (Like searching the number of
  3412.        tracks of your floppydisk if you have one)
  3413.  
  3414.    2) If you have LILO installed, tell him to wait.
  3415.  
  3416.    3) You do nothing (like I did)
  3417.  
  3418.    4) You buy some more memory so that counting the memory takes 30 seconds.
  3419.       That should be about 1024 Megabytes ;-).
  3420.  
  3421.   Markus Eiden
  3422.  
  3423.   Markus@Eiden.de
  3424.  
  3425.   --
  3426.   -----------------------------------------------------------------------
  3427.   StR Dipl.-Ing. Markus Eiden \\://                       Markus@eiden.de
  3428.   Am alten Sportplatz 3       (o -)        http://www.rp.schule.de/eiden/
  3429.   D-67599 Gundheim      ---ooO-(_)-Ooo---             NIC-HDL: ME256-RIPE
  3430.  
  3431.   8.13.2.3.  APC Smart-UPS 1400
  3432.  
  3433.   Another day, another APC.  This is for the Smart-UPS 1400, in dumb
  3434.   mode.
  3435.  
  3436.   From: "Slavik Terletsky" <ts@polynet.lviv.ua>
  3437.   To: hjstein@math.huji.ac.il
  3438.   Subject: my contribution to UPS HOWTO
  3439.   Date: Mon, 27 Jan 1997 21:10:16 +0000
  3440.  
  3441.   Hello
  3442.   I just hacked ups daemon, if you want, you may enclose it
  3443.   in your UPS HOWTO document (i used some info from).
  3444.   Please replay.
  3445.   --
  3446.  
  3447.   UPS daemon for FreeBSD (2.1.5 - tested).
  3448.   Interacts with APC Smart-UPS 1400.
  3449.  
  3450.   Connection scheme:
  3451.  
  3452.   UPS (pin, signal name)          PC (pin, signal name)
  3453.   ----------------------          ---------------------
  3454.   1 Shutdown              >----------->   4 Data Terminal Ready
  3455.   2 Line Failed           >----------->   8 Clear To Send
  3456.   4 Common                >----------->   5 Ground
  3457.   5 Battery Low           >--------+-->   1 Data Carrier Detector
  3458.   8 Battery (+24V)        >--|10K|-+
  3459.  
  3460.   UPSD DESCRIPTION
  3461.  
  3462.   usage: upsd <device> [wait [script]]
  3463.  
  3464.   device  - device name upsd interacts thru (e.g. /dev/cuaa1)
  3465.   wait    - time (secs) to wait before running script, (default value 0 sec).
  3466.   script  - system shutdown script (default /etc/rc.shutdown).
  3467.  
  3468.   Actions:
  3469.   upsd logs all the changes of UPS status (power {up,down}, battery {low,ok}).
  3470.   When "power down" and "battery low" upsd activates UPS SHUTDOWN signal,
  3471.   waits for a <wait> seconds, and then runs system shutdown script - <script>.
  3472.  
  3473.   Script sample:
  3474.  
  3475.   #!/bin/sh
  3476.   # Script is executed when system is going down.
  3477.  
  3478.   PATH=/sbin:/bin:/usr/sbin:/usr/bin
  3479.  
  3480.   echo "System is going DOWN right NOW" | wall
  3481.  
  3482.   reboot
  3483.  
  3484.   Upsd source:
  3485.   /* UPS daemon
  3486.    * Copyright 1997 Slavik Terletsky. All rights reserved.
  3487.    * Author: Slavik Terletsky <ts@polynet.lviv.ua>
  3488.    * System: FreeBSD
  3489.    */
  3490.   #include <stdio.h>
  3491.   #include <stdlib.h>
  3492.   #include <signal.h>
  3493.   #include <syslog.h>
  3494.   #include <unistd.h>
  3495.   #include <varargs.h>
  3496.   #include <fcntl.h>
  3497.   #include <errno.h>
  3498.   #include <sys/uio.h>
  3499.   #include <sys/types.h>
  3500.   #include <sys/ioctl.h>
  3501.   #include <sys/ttycom.h>
  3502.  
  3503.   int status;
  3504.   int wait = 0;
  3505.   FILE *fd;
  3506.   char *scr = "/etc/rc.shutdown";
  3507.   char *idf = "/var/run/upsd.pid";
  3508.  
  3509.   void upsterm();
  3510.   void upsdown(int);
  3511.  
  3512.   int main(int argc, char *argv[]) {
  3513.    int pd;
  3514.    int zero = 0;
  3515.    char d5, d6, d7;
  3516.    char low = 0;
  3517.    char pow = 1;
  3518.  
  3519.    /* check arguments */
  3520.    switch(argc) {
  3521.    case  4:
  3522.    scr = argv[3];
  3523.    case  3:
  3524.    wait = atoi(argv[2]);
  3525.    case  2:
  3526.    break;
  3527.    default:
  3528.    fprintf(stderr, "usage: %s <device> [wait [script]]\n", argv[0]);
  3529.    exit(1);
  3530.    }
  3531.  
  3532.    /* check if script exists */
  3533.    if(!(fd = fopen(scr, "r"))) {
  3534.    fprintf(stderr, "fopen: %s: %s\n", scr, sys_errlist[errno]);
  3535.    exit(1);
  3536.    }
  3537.    fclose(fd);
  3538.  
  3539.    /* check if upsd is already running */
  3540.    if(fd = fopen(idf, "r")) {
  3541.    fprintf(stderr, "fopen: %s: File already exists\n", idf);
  3542.    exit(1);
  3543.    }
  3544.  
  3545.    /* become a daemon */
  3546.    switch(fork()) {
  3547.    case -1:       /* error */
  3548.    fprintf(stderr, "fork: %s\n", sys_errlist[errno]);
  3549.    exit(1);
  3550.    case  0:       /* child */
  3551.    break;
  3552.    default:       /* parent */
  3553.    exit(0);
  3554.    }
  3555.  
  3556.    /* save the pid */
  3557.    if(!(fd = fopen(idf, "w"))) {
  3558.    fprintf(stderr, "fopen: %s: %s\n", idf, sys_errlist[errno]);
  3559.    exit(1);
  3560.    }
  3561.    fprintf(fd, "%d\n", (int)getpid());
  3562.    fclose(fd);
  3563.  
  3564.    /* open monitor device */
  3565.    if((pd = open(argv[1], O_RDWR | O_NDELAY)) < 0) {
  3566.    fprintf(stderr, "open: %s: %s\n", argv[1], sys_errlist[errno]);
  3567.    exit(1);
  3568.    }
  3569.  
  3570.    /* daemon is alive */
  3571.    openlog("upsd", LOG_PID, LOG_DAEMON);
  3572.    syslog(LOG_INFO, "daemon started");
  3573.  
  3574.    /* signal reaction */
  3575.    (void)signal(SIGTERM, upsterm);
  3576.  
  3577.    /* monitor device */
  3578.    while(1) {
  3579.    /* clear bits */
  3580.    if(ioctl(pd, TIOCMSET, &zero) < 0) {
  3581.     fprintf(stderr, "ioctl: %s\n", sys_errlist[errno]);
  3582.     exit(1);
  3583.    }
  3584.  
  3585.    /* get device status */
  3586.    if(ioctl(pd, TIOCMGET, &status) < 0) {
  3587.     fprintf(stderr, "ioctl: %s\n", sys_errlist[errno]);
  3588.     exit(1);
  3589.    }
  3590.  
  3591.    /* determin status */
  3592.    d5 = status & 0x20;
  3593.    d6 = status & 0x40;
  3594.    d7 = status & 0x80;
  3595.  
  3596.    /* power up */
  3597.    if(!(d7 + d5)) {
  3598.     if(!pow) {
  3599.      syslog(LOG_CRIT, "power up");
  3600.      pow = 1;
  3601.     }
  3602.    /* power down */
  3603.    } else {
  3604.     if(pow) {
  3605.      syslog(LOG_CRIT, "power down");
  3606.      pow = 0;
  3607.     }
  3608.    }
  3609.  
  3610.    /* battery low */
  3611.    if(!d6 && !low) {
  3612.     syslog(LOG_ALERT, "battery low");
  3613.     low = 1;
  3614.  
  3615.     /* down ups */
  3616.     if(!pow) {
  3617.      upsdown(pd);
  3618.     }
  3619.    }
  3620.  
  3621.    /* battery ok */
  3622.    if(d6 && low) {
  3623.     syslog(LOG_CRIT, "battery ok");
  3624.     low = 0;
  3625.    }
  3626.  
  3627.    sleep(1);
  3628.    }
  3629.  
  3630.    /* not reached */
  3631.    return 0;
  3632.   }
  3633.  
  3634.   void upsterm() {
  3635.    /* log termination message */
  3636.    syslog(LOG_INFO, "daemon terminated");
  3637.  
  3638.    /* remove pid file */
  3639.    unlink(idf);
  3640.  
  3641.    exit(0);
  3642.   }
  3643.  
  3644.   void upsdown(int pd) {
  3645.    /* log shutdown message */
  3646.    syslog(LOG_ALERT, "system is going down");
  3647.  
  3648.    /* remove pid file */
  3649.    unlink(idf);
  3650.  
  3651.    /* save our filesystem */
  3652.    system("/bin/sync");
  3653.    system("/bin/sync");
  3654.    system("/bin/sync");
  3655.  
  3656.    /* shutdown ups */
  3657.    status = TIOCM_DTR;
  3658.    if(ioctl(pd, TIOCMSET, &status) < 0) {
  3659.    fprintf(stderr, "ioctl: %s\n", sys_errlist[errno]);
  3660.    exit(1);
  3661.    }
  3662.  
  3663.    /* wait and then run script */
  3664.    sleep(wait);
  3665.    system(scr);
  3666.   }
  3667.   # Slavik Terletsky      # University "Lvivska Poytechnika" #
  3668.   # Network Administrator # mailto:ts@polynet.lviv.ua        #
  3669.  
  3670.   9.  How to shutdown other machines on the same UPS
  3671.  
  3672.   Some people (myself included) have several computers running Linux
  3673.   connected to one UPS.  One computer monitors the UPS and needs to get
  3674.   the other computers to shut down when the power goes out.
  3675.  
  3676.   We assume the computers can communicate over a network.  Call the
  3677.   computer that monitors the UPS the master and the other computers the
  3678.   slaves.
  3679.  
  3680.   In the old days this required some fancy programming.
  3681.  
  3682.   These days, the best thing to do is to pick up either the
  3683.   powerd-2.0.tar.gz package or the upsd-1.0.tgz package (see section
  3684.   ``Software''), and follow the instructions.  Both are able to run on
  3685.   the slaves in a mode where they connect over the network to a powerd
  3686.   or upsd process running on the master to query the status of the UPS.
  3687.   Some of the APC specific packages seem to have this ability too.
  3688.  
  3689.   Note, though, that if your network is insecure, you might want to add
  3690.   a little security to this, lest someone spoof the slave powerd
  3691.   processes into thinking that the power is out.
  3692.  
  3693.   Another possibility is to go for SNMP (Simple Network Management
  3694.   Protocol).  Detailing the use of SNMP is beyond the scope of this
  3695.   document, not to mention currently being beyond me.
  3696.  
  3697.