Next Previous Contents

36. UPS: Complete UPS Backup & Graphing support for APC UPSes

36.1 The state of the software

Today, APC UPSes are fully supported by both OpenSource and APC proprietary software for Linux. Overall, both versions do their job well but they don't completely overlap in features and flexibility. The APC version is short, sweet, and does 90% of everything you could ever want. On the flip side, the OpenSource versions allow for remote shutdown of internal LAN-based PCs, etc. Here is a breakdown of the PROs/CONs of both pacakges:

OpenSource APCUPSd:

APC Powerchute Plus (NOT the Business Edition - free but proprietary):

This TrinityOS chapter covers:

One difference that should be mentioned again is that the official APC Powerchute software for Linux is NOT compatible with MS Windows UPS clients written by APC. This means that you cannot use your internal LAN to shutdown other MS Windows machines in addition to your Linux machine.

Currently, these docs only cover the installation of the OpenSouce "apcupsd" tool from both RPM and tar.gz form. If there is enough interest, I can also describe the setup of APC Powerchute software too. I still recommend the OpenSource version (it DOES shutdown other machines running OSes like Windows, etc.). Think modular. :-)

36.2 Installing and Using APC's Powerchute

If you still want to run Powerchute software over the APCUPSd program, I recommend that you:

36.3 Installing APCUPSd

Ok..

- Download the newest APCUPSd found in Section 5

- Next, fix its permissions:


                chmod 750 /sbin/apcupsd

36.4 Configuring APCUPSd for logging and paging

Redhat:

Next, edit /etc/apcupsd/apcupsd.conf and make the following changes. Please note that you need to alter the example to better match your environment.

/etc/apcupsd/apcupsd.conf


UPSCABLE smart
UPSTYPE smartups
DEVICE /dev/ttyS0
LOCKFILE /var/lock

BATTERYLEVEL 10
MINUTES 0
TIMEOUT 0
ANNOY 300
PROCFS 5
ANNOYDELAY 60
NOLOGIN disable
KILLDELAY 0 

#Set only to on if you plan to shutdown other machines via a TCP/IP network
NETSERVER off

EVENTSFILES /var/log/apcupsd.events
STATTIME 0 
STATFILE /var/log/apcupsd.status 
LOGSTATS off

#Log UPS stats once a second
DATATIME 1

#Newer APCUPSd programs no longer log directly to a data file.
#  The newer versions now log ONLY to SYSLOG
FACILITY local0 

SENSITIVITY H
WAKEUP 180
BEEPSTATE L
SELFTEST 336

UPSCLASS standalone
UPSMODE disable
NETACCESS false
--              

The next step is to configure SYSLOG to support the new APCUPSd logging system (APCUPSd no longer logs directly to a specified file). Edit the /etc/syslog.conf file and add the following line:

/etc/syslog.conf


local0.*                        /var/log/apcupsd.data 

Ok, so this is nice and all but the common SYSLOG setup in Linux will also send ALL log messages to other files as well. There is no need to mess up these other files with the intentionally chatty UPS log stats so I recommend to modify other "*.*" lines to exclude these once-a-second UPS stats info. Please edit all the syslog lines that apply but this example should cover it:

/etc/syslog.conf


*.*;local0.!info                               /var/log/syslog 

*.info;mail.none;authpriv.none;local0.!info    /var/log/messages  

Once this is all setup, you should activate both the new log file and the new SYSLOG system:

Redhat:

Slackware:

Optional stuff: Paging users when power events occur:

/etc/apcupsd/apccontrol


emergency)
   wall "Emergency Shutdown. Possible battery failure on UPS ${2}."
   echo "Emergency! Batteries have failed on UPS ${2}. Change them \
NOW" | /bin/mail 1234567@skytel.com
   ${SHUTDOWN} -h now "apcupsd emergency shutdown"
;;

onbattery)
   wall "Power failure on UPS ${2}. Running on batteries."
   /usr/bin/logger "Power failure on UPS ${2}. Running on batteries."
   echo "Power failure on UPS ${2}. Running on batteries." \
| /bin/mail 1234567@skytel.com
;;

Now, fix the permissions on the files:

Finally, you need to TEST the new UPS setup:

36.5 Testing your new UPS setup

36.6 Graphing the UPS stats results each day

As mentioned above, I once had a UPS that lost control of the charging circuit and and nearly burned down my house. Ever since then, I felt that I needed to always monitor the envirtonmentals of my UPS. Hopefully this will help prevent this catastrophe from ever happening to me again.

The following script will take the previous day's APCUPSd or APC Powerchute logs and create a high quality multicolor graph in PDF format. Not only that but the PDF is emailed to you via CRON every night. Check out http://www.ecst.csuchico.edu/~dranch/LINUX/TrinityOS-security/var/log/ups-log-jun24.pdf to see an example PDF of my terrible day. Specifically look at the temperature line and imagine the worst sulfur smell you could imagine! Overall, I got lucky!

Please also notice that this script has a BUNCH of pre-installed software requirements but most machines should have this already installed. Please see the comments in the script below for full details. Like any shell script, you can change things around to better fit your needs.

Download the script directly: Within the http://www.ecst.csuchico.edu/~dranch/LINUX/TrinityOS-security/TrinityOS-security.tar.gz archive

or

Just the file:

Powerchute: http://www.ecst.csuchico.edu/~dranch/LINUX/TrinityOS-security/usr/lib/powerchute/powerchute-generate-ups-graph.sh

APCUPSd: http://www.ecst.csuchico.edu/~dranch/LINUX/TrinityOS-security/usr/local/sbin/apcupsd-generate-ups-graph.sh

Here is the script for Powerchute:

<TrinityOS powerchute-generate-ups-graph.sh START>


#!/bin/sh

# TrinityOS - powerchute-generate-ups-graph.sh
# written by David Ranch
# v1.50
#
#   Changes
#   -------
#     1.5 - Fixed a long standing OCTAL conversion error
#     1.2 - Added some additional debugging options
#     1.1 - Updated to reflect support for both APCUPSd and Powerchute
#           and noted possibly Mutt attachment issues
#     1.0 - Original version
#
#
# This script takes the output from APC's Powerchute for Linux and
# both graphs it and emails it to the administrator.  
#
# If you are running the OpenSource APCUPSd tool, please use the 
#    apcupsd-generate-ups-graph.sh script available in TrinityOS.
#
# NOTE: This script requires:
#        - Powerchute for Linux installed and running properly 
#        - bash
#        - awk
#        - gnuplot
#        - ps2pdf (ghostscript)
#        - mutt
#
# NOTE#2:  APC Powerchute v4.5.2 has a log file size limitation of
#          750k per the powerchute.ini file but APCUPSd doesn't have
#          this limitation.  Because of this Powerchute limit, 
#          I've found that you CANNOT sample anything faster than 
#          say 7 seconds.  Obviously, this isn't very granular.
#          If 7 seconds is just enough, you MUST run this script
#          around midnight or the script will fail due to missing
#          data.


#Local vars
#
#Machine running the UPS software
HOST="roadrunner"
#Who the resulting email should goto
ADMIN="johndoe@acme123.com"

# =================================================================

clear
cd /usr/lib/powerchute

#date setup
MONTH=`date +%m`
DAY=`date +%d`
YES=$((10#$DAY-1))
YEAR=`date +%y`
YESTERDAY="$MONTH/$YES/$YEAR"

#DEBUG - enable and change the DAY line to graph a specific day
#        and make sure you
#DAY=20
#YES=$(($DAY-1))
#echo -e "\n\nDEBUG: Graphing $YESTERDAY\n\n"


#Need to remove the commas and such
#  This is setup to manipulate Powerchutes logs.  You must make slight
#  changes to this to handle APCUPSds logs (it has a few more fields)
#  Feel free to email me if you need a hand.
#

echo -e "Beginning process to create graph for: $YESTERDAYi\n"
echo "Filtering original powerchute.dat file.."
cat powerchute.dat | \
  awk -F , '{print $1" "$2" "$3" "$4" "$5" "$6" "$7" "$8" "$9}' \
  > filtered-powerchute.dat

#Ok, now create the gnuplot command file
echo "set title \"$HOST $YESTERDAY APC Powerchute Log\"" > generate-apc-graph-$MONTH$YES$YEAR.gnuplot
echo "set xlabel \"Date\"" >> generate-apc-graph-$MONTH$YES$YEAR.gnuplot
echo "set ylabel \"Absolute number\"" >> generate-apc-graph-$MONTH$YES$YEAR.gnuplot
echo "set timefmt \"%m/%d/%y %H:%M:%S"\" >> generate-apc-graph-$MONTH$YES$YEAR.gnuplot
echo "set xdata time" >> generate-apc-graph-$MONTH$YES$YEAR.gnuplot
echo "set xrange [ \"$MONTH/$YES/$YEAR\":\"$MONTH/$DAY/$YEAR\" ]" >> generate-apc-graph-$MONTH$YES$YEAR.gnuplot
echo "set terminal postscript" >> generate-apc-graph-$MONTH$YES$YEAR.gnuplot
echo "set terminal postscript color" >> generate-apc-graph-$MONTH$YES$YEAR.gnuplot
echo "set terminal postscript solid" >> generate-apc-graph-$MONTH$YES$YEAR.gnuplot
echo "set output \"/tmp/ups-log-$MONTH$YES$YEAR.ps\"" >> generate-apc-graph-$MONTH$YES$YEAR.gnuplot

#This is for Powerchutes logs.  If you are using APCUPSd, you will need
#to make slight changes here as the order is a little different and APCUPSd
#also has a few extra files too.
echo "plot \"filtered-powerchute.dat\" using 1:3 title 'LineMIN' with lines, \\" >> generate-apc-graph-$MONTH$YES$YEAR.gnuplot  
echo "  \"filtered-powerchute.dat\" using 1:4 title 'LineMAX' with lines, \\" >> generate-apc-graph-$MONTH$YES$YEAR.gnuplot  
echo "  \"filtered-powerchute.dat\" using 1:5 title 'OutV' with lines, \\" >> generate-apc-graph-$MONTH$YES$YEAR.gnuplot  
echo "  \"filtered-powerchute.dat\" using 1:6 title 'BattV' with lines, \\" >> generate-apc-graph-$MONTH$YES$YEAR.gnuplot  
echo "  \"filtered-powerchute.dat\" using 1:7 title 'LineFREQ' with lines, \\" >> generate-apc-graph-$MONTH$YES$YEAR.gnuplot  
echo "  \"filtered-powerchute.dat\" using 1:8 title 'UPSload' with lines, \\" >> generate-apc-graph-$MONTH$YES$YEAR.gnuplot  
echo "  \"filtered-powerchute.dat\" using 1:9 title 'UPStemp' with lines" >> generate-apc-graph-$MONTH$YES$YEAR.gnuplot  

echo "Deleteing old ps and pdf files.."
#rm -f /tmp/ups-log*.ps /tmp/ups-log*.pdf

echo "Creating files.."
gnuplot generate-apc-graph-$MONTH$YES$YEAR.gnuplot  
echo " - done creating files"

echo "Creating /tmp/ups-log-$MONTH$YES$YEAR.ps.."
ps2pdf /tmp/ups-log-$MONTH$YES$YEAR.ps
rm -f /tmp/ups-log-$MONTH$YES$YEAR.ps
mv -f ups-log-$MONTH$YES$YEAR.pdf /tmp

echo "Cleaning up.."
#rm -f filtered-powerchute.dat
rm -f generate-apc-graph-$MONTH$YES$YEAR.gnuplot  

# NOTE: If the emailed PDF seems to be corrupt, make sure that you
#       have the /etc/mailcap file installed
#
echo "Emailing graph.."
echo "Results for $MONTH$YES$YEAR" | \
  mutt -a /tmp/ups-log-$MONTH$YES$YEAR.pdf \
  -s "$HOST UPS graph for $MONTH$YES$YEAR" $ADMIN

#Uncomment this out once you are SURE things are working.  If things 
#are NOT working, make sure this file exists if not check that you
#have all the required tools installed, etc.
#
#rm -f /tmp/ups-log-$MONTH$YES$YEAR.pdf

<TrinityOS powerchute-generate-ups-graph.sh STOP>

Here is the script for APCUPSd:

<TrinityOS apcupsd-generate-ups-graph.sh START>


#!/bin/sh

# TrinityOS - apcupsd-generate-ups-graph.sh
# written by David Ranch
# v1.50
#
#   Changes
#   -------
#     1.5 - Fixed a long standing OCTAL conversion error
#     1.2 - Added some additional debugging options
#     1.1 - Updated to reflect support for both APCUPSd and Powerchute
#           and noted possibly Mutt attachment issues
#     1.0 - Original version
#
# This script takes the output from APCUPSd for Linux and
# both graphs it and emails it to the administrator.  
#
# If you are running APC"s Powerchute for Linux,  please use the 
#    powerchute-generate-ups-graph.sh script available in TrinityOS.
#
# NOTE: This script requires:
#        - APCUPSd for Linux running properly (doc'ed in TrinityOS)
#        - bash
#        - awk
#        - gnuplot
#        - ps2pdf (ghostscript)
#        - mutt
#


#Local vars
#
#Machine running the UPS software
HOST="Roadrunner"
#Who the resulting email should goto
ADMIN="johndoe@acme123.com"

# =================================================================

clear

#Enable this line if you run APCUPSd
cd /var/log

#date setup
MONTH=`date +%b`
DAY=`date +%d`
YES=$((10#$DAY-1))
TOM=$((10#$DAY+1))
YEAR=`date +%y`
YESTERDAY="$MONTH/$YES/$YEAR"

#DEBUG - enable and change the DAY line to graph a specific day
#        and make sure you
#DAY=20
#YES=$(($DAY-1))
#echo -e "\n\nDEBUG: Graphing $YESTERDAY\n\n"


# Need to remove the commas and such
#
#  This script manipulates APCUPSd logs.  If you are running Powerchute, 
#  please use the Powerchute script shown above instead
#

echo -e "Beginning process to create graph for: $YESTERDAY\n"
echo "Filtering original apcupsd.data file.."
cat apcupsd.data | grep -v "succeeded" | grep -v "repeated" | \
  awk '{print $1" "$2" "$3" "$6}' | \
  awk -F , '{print $1" "$2" "$3" "$4" "$5" "$6" "$7" "$8" "$9" "$10}' \
  > filtered-apcupsd.data


#Ok, now create the gnuplot command file
echo "set title \"$HOST $YESTERDAY APC APCUPSd Log\"" > generate-apc-graph-$MONTH$YES$YEAR.gnuplot
echo "set xlabel \"Date\"" >> generate-apc-graph-$MONTH$YES$YEAR.gnuplot
echo "set ylabel \"Absolute number\"" >> generate-apc-graph-$MONTH$YES$YEAR.gnuplot
echo "set timefmt \"%b %d %H:%M:%S"\" >> generate-apc-graph-$MONTH$YES$YEAR.gnuplot
echo "set xdata time" >> generate-apc-graph-$MONTH$YES$YEAR.gnuplot

#debug
#echo "set xrange [ \"$MONTH $DAY\":\"$MONTH $TOM\" ]" >> generate-apc-graph-$MONTH$YES$YEAR.gnuplot
echo "set xrange [ \"$MONTH $YES\":\"$MONTH $DAY\" ]" >> generate-apc-graph-$MONTH$YES$YEAR.gnuplot

#Disable the following FOUR lines to display the graph in a Xwindow
echo "set terminal postscript" >> generate-apc-graph-$MONTH$YES$YEAR.gnuplot
echo "set terminal postscript color" >> generate-apc-graph-$MONTH$YES$YEAR.gnuplot
echo "set terminal postscript solid" >> generate-apc-graph-$MONTH$YES$YEAR.gnuplot
echo "set output \"/tmp/ups-log-$MONTH$YES$YEAR.ps\"" >> generate-apc-graph-$MONTH$YES$YEAR.gnuplot

#This is for APCUPSd logs.  
echo "plot \"filtered-apcupsd.data\" using 1:4 title 'LineMIN' with lines, \\" >> generate-apc-graph-$MONTH$YES$YEAR.gnuplot  
echo "  \"filtered-apcupsd.data\" using 1:5 title 'LineMAX' with lines, \\" >> generate-apc-graph-$MONTH$YES$YEAR.gnuplot  
echo "  \"filtered-apcupsd.data\" using 1:6 title 'OutV' with lines, \\" >> generate-apc-graph-$MONTH$YES$YEAR.gnuplot  
echo "  \"filtered-apcupsd.data\" using 1:7 title 'BattV' with lines, \\" >> generate-apc-graph-$MONTH$YES$YEAR.gnuplot  
echo "  \"filtered-apcupsd.data\" using 1:8 title 'LineFREQ' with lines, \\" >> generate-apc-graph-$MONTH$YES$YEAR.gnuplot  
echo "  \"filtered-apcupsd.data\" using 1:9 title 'UPSload' with lines, \\" >> generate-apc-graph-$MONTH$YES$YEAR.gnuplot  
echo "  \"filtered-apcupsd.data\" using 1:10 title 'UPStemp' with lines" >> generate-apc-graph-$MONTH$YES$YEAR.gnuplot  

echo "Deleteing old ps and pdf files.."
rm -f /tmp/ups-log*.ps /tmp/ups-log*.pdf

echo "Creating files.."
gnuplot generate-apc-graph-$MONTH$YES$YEAR.gnuplot  
echo " - done creating files"

echo "Creating /tmp/ups-log-$MONTH$YES$YEAR.ps.."
ps2pdf /tmp/ups-log-$MONTH$YES$YEAR.ps
rm -f /tmp/ups-log-$MONTH$YES$YEAR.ps
mv -f ups-log-$MONTH$YES$YEAR.pdf /tmp

echo "Cleaning up.."
rm -f filtered-apcupsd.data
rm -f generate-apc-graph-$MONTH$YES$YEAR.gnuplot  


# NOTE: If the emailed PDF seems to be corrupt, make sure that you
#       have the /etc/mailcap file installed
#
echo "Emailing graph.."
echo "Results for $MONTH$YES$YEAR" | \
 mutt -a /tmp/ups-log-$MONTH$YES$YEAR.pdf \
 -s "$HOST UPS graph for $MONTH$YES$YEAR" $ADMIN

#Uncomment this out once you are SURE things are working.  If things 
#are NOT working, make sure this file exists if not check that you
#have all the required tools installed, etc.
#
#rm -f /tmp/ups-log-$MONTH$YES$YEAR.pdf

<TrinityOS apcupsd-generate-ups-graph.sh STOP>

Next, make the script executable:


chmod 700 /usr/lib/powerchute/powerchute-generate-ups-graph.sh

OR


chmod 700 /usr/local/sbin/apcupsd-generate-ups-graph.sh

Ok.. to get things running once a night, we need to use CRON:

Ok.. one last thing: With such an agreesive logging schedule, APCUPSd can create VERY large files ( 805k per day). Powerchute doesn't have this issue since it automatically rotates the logs once the file hits 750k. This limit is both nice but also VERY limiting. With APCUPSd, I recommend to rotate the logs at LEASE every week. To do this, APPEND the following lines to the end of the /etc/logrotate.d/syslog file (Redhat only):

/etc/logrotate.d/syslog


/var/log/apcupsd.data {
        rotate 5
        weekly
        postrotate
        /usr/bin/killall -HUP syslogd
        endscript
}  

That's it. Enjoy!


Next Previous Contents