 
 
#!/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 /opt/APC/PowerChuteBusinessEdition/Agent

#date setup
MONTH=`date +%m`
DAY=`date +%d`
YES=$(($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 DataLog | \
  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
 
 


