LinHES Forums
http://forums.linhes.org/

Automated APC UPS setup script
http://forums.linhes.org/viewtopic.php?f=3&t=16292
Page 1 of 2

Author:  tjc [ Sat Sep 01, 2007 12:32 pm ]
Post subject:  Automated APC UPS setup script

1) Save the following code in a file (trim it so that the first line is the one that says "#!/bin/bash") called "setup_apcupsd.sh".
2) Make sure there are no trailing blanks after the \ characters at the end of lines 25 through 30.
3) Make sure you've connected the USB cable from your UPS to your computer.
4) Make the script executable by running the command: "chmod +x setup_apcupsd.sh"
5) Run your new script.

Code:
root@black2:~/setup# cat setup_apcupsd.sh
#!/bin/bash

if [ $(id -u) -ne 0 ] ; then
    echo "You must run this script as root!"
    exit 1
fi
echo
echo "Checking for an APC UPS (only recognizes USB connected units)..."
echo
if ! dmesg | egrep -i 'USB .*Device \[(APC|American Power Conversion)' >/dev/null 2>&1 ; then
    echo "No USB connected APC UPS detected."
    exit 1
fi
if [ "$1" != "no_apt_get_update" ] ; then
    echo
    echo "Updating apt-get info..."
    echo
    apt-get update
fi
echo
echo "Making sure the apcupsd software is up to date..."
echo
apt-get install apcupsd libsnmp-base libsnmp9
echo "Fixing up the config files..."
sed -si.orig -e '/^\(UPSCABLE\) .*/s//\1 usb/' \
             -e '/^\(UPSTYPE\) .*/s//\1 usb/' \
             -e '/^\(DEVICE\) .*/s//\1 /' \
             -e '/^\(STATTIME\) .*/s//\1 300/' \
             /etc/apcupsd/apcupsd.conf
sed -si.orig -e '/^\(ISCONFIGURED=\).*/s//\1yes/' \
             /etc/default/apcupsd
echo
echo "Restarting the apcupsd daemon process..."
echo
/etc/init.d/apcupsd restart
echo
echo "Making sure the apcupsd daemon is started at boot..."
echo
update-rc.d apcupsd defaults
echo
echo "APC UPS setup complete!"
echo


Edit #1 - Corrected the problem Mike spotted.
Edit #2 - Incorporated Jack's fix from below.
Edit #3 - Fixed an editing fumble.

Author:  Too Many Secrets [ Sun Sep 02, 2007 1:09 pm ]
Post subject: 

This came just in time for me. Just purchased an UPS (on sale of course) and was going to monkey with getting the usb setup working. Saw this, worked like a charm! Thanks a bunch! But now I'm afraid to test it...

Author:  mihanson [ Sun Sep 02, 2007 3:04 pm ]
Post subject:  Re: Automated APC UPS setup script

tjc wrote:
Code:
echo
update-rc.d webmin defaults
echo
echo "APC UPS setup complete!"
echo


Shouldn't this be

Quote:
echo
update-rc.d apcupsd defaults
echo
echo "APC UPS setup complete!"
echo


:?:

Author:  tjc [ Sun Sep 02, 2007 3:55 pm ]
Post subject: 

First test, check the information in the /var/log/apcupsd.status file to make sure you're getting status. After that you can wait for the automatic self test to fire (this may take several days). You should see a little (5%) dip in the battery charge level which comes back up to 100% within a few minutes and a note about self test in the /var/log/apcupsd.events log. Here's the report from the last one my UPS did.
Code:
Sat Sep 01 13:20:16 EDT 2007  UPS Self Test switch to battery.
Sat Sep 01 13:20:25 EDT 2007  UPS Self Test completed: Not supported

Author:  tjc [ Sun Sep 02, 2007 4:00 pm ]
Post subject:  Re: Automated APC UPS setup script

mihanson wrote:
Shouldn't this be

Quote:
echo
update-rc.d apcupsd defaults
echo
echo "APC UPS setup complete!"
echo


:?:

Yes it should. :oops: Fixed now. Sigh... :oops:

Author:  Too Many Secrets [ Sun Sep 02, 2007 5:51 pm ]
Post subject: 

Thanks again tjc, I ran the "update-rc.d apcupsd defaults" as root, so I'm thinking I should be good for the next reboot. I'm getting info in my log file. I'm assuming that the software will safely shut down the box when power goes and stay down. Just wondering how long it waits? I see MINTIMEL as 3 minutes and MBATTCHG as 5%, are these the times it waits to start shutdown?

Author:  tjc [ Sun Sep 02, 2007 6:24 pm ]
Post subject: 

Yeah. If it gets below either 5% battery charge or 3 minutes of runtime left it should shut down.

Author:  khrusher [ Mon Sep 03, 2007 6:28 am ]
Post subject: 

you can test by putting a fake shutdown script inplace....maybe w/ some log output.

Author:  tjc [ Mon Sep 03, 2007 7:12 am ]
Post subject: 

The apcupsd site has an online manual with a whole chapter on ways of testing it. http://www.apcupsd.com/manual/Testing_Apcupsd.html

I never opted for anything that elaborate. I installed it, made sure the system was talking to the UPS (the status log is enough proof on that count), made sure nothing else was running to generate disk activity, pulled the plug, and let it shutdown the box.

Author:  RobTheGob [ Mon Sep 03, 2007 5:52 pm ]
Post subject: 

One very cool thing about apcupsd is that it additional linux systems can point to the controlling apcupsd system and run like "slaves".

I have my linux firewall controlling the APC UPS - then my MythTV box (which is also connected to the APC) runs its own instance of apcupsd and points to the firewall IP address rather than directly to the APC.

When the firewall starts shutting down - the MythTV box is also told to shutdown...

Author:  Too Many Secrets [ Fri Sep 14, 2007 10:27 pm ]
Post subject: 

Did this script make it into F27? I can't locate, will recreate according to above.

Author:  tjc [ Fri Sep 14, 2007 10:46 pm ]
Post subject: 

It's there on my machine, and I even used it before the restore: /usr/local/bin/setup_apcupsd.sh

Author:  Too Many Secrets [ Fri Sep 14, 2007 10:52 pm ]
Post subject: 

tjc wrote:
It's there on my machine, and I even used it before the restore: /usr/local/bin/setup_apcupsd.sh


OK found it, right where it should have been. but it didn't show up with a locate. I must need some sleep.

Author:  jmckeown2 [ Sat Sep 15, 2007 2:53 pm ]
Post subject: 

tjc,
APC apparently made some changes that fool the detection, so the script fails for me. On My system:
Code:
root@joesmyth:/usr/local/bin#  dmesg | egrep -i 'UPS'
hiddev96: USB HID v1.10 Device [American Power Conversion Back-UPS XS  900 FW:830.E6 .D USB FW:E6 ] on usb-0000:00:02.0-2

It looks like "APC" is changing to "American Power Conversion"
I changed the script to:
Code:
if ! dmesg | egrep -i 'USB .*Device \[(APC|American Power Conversion)' >/dev/null 2>&1 ; then

Notice, besides the obvious, the change from grep to egrep. it works for me now.

Now, can someone verify that this does not break "APC" UPS's? I hope this can be of help.

Author:  tjc [ Sat Sep 15, 2007 3:04 pm ]
Post subject: 

Looks like a good fix on inspection and works for me when tested.

Thanks!

BTW - Does the RRD script to monitor a UPS work with the status output of your unit?

Page 1 of 2 All times are UTC - 6 hours
Powered by phpBB® Forum Software © phpBB Group
http://www.phpbb.com/