About Me

My photo
I know the last digit of PI

Friday, April 11, 2014

UPS under Linux

The following steps are tested on Fedora 20, but generally should work on every other system with few modifications.

Step1. Install NUT


yum install nut*

Step2. Configure UPS

Execute
nut-scanner
It should output something like this:
[nutdev1]
driver = "blazer_usb"
port = "auto"
vendorid = "0001"
productid = "0000"
product = "STD UPS MON V1.0"
bus = "006"

Add the nut-scanner output to the end of /etc/ups/ups.conf file. You can change the [nutdev1] to something more meaningful e.g. [myUps1] or [InformGuardUPS]. In the examples bellow we will use the default name [nutdev1].

Change /etc/ups/upsd.conf and add "LISTEN 127.0.0.1 3493" (without quotes)

Change /etc/ups/upsd.users and add a new admin user

[admin]
password = mypassword
actions = SET
instcmds = ALL

Change /etc/ups/upsmon.conf and add following lines:

RUN_AS_USER root
MONITOR nutdev1@localhost 1 admin mypassword master


Step3. Manually test everything

upsdrvctl start
The command should output something similar to:
Network UPS Tools - Generic HID driver 0.34 (2.4.1)
USB communication driver 0.31
Using subdriver: MGE HID 1.12
Detected EATON - Ellipse MAX 1100 [ADKK22008]
If you face a problem like "libusb couldn't open usb device /dev/usb/XXXXXX: permission denied", then use google to find a way how to solve it nicely. I used very brutal method:
chmod -R 777 /dev/bus/usb/
Then try to start UPS driver again Start the upsd:
upsd
And the result should be similar to:
Network UPS Tools upsd 2.4.1
listening on 127.0.0.1 port 3493
listening on ::1 port 3493
Connected to UPS [eaton]: usbhid-ups-eaton
List your ups names with
upsc -L
Try to query the UPS status with.
upsc nutdev1@localhost
or just
upsc nutdev1
Depending on your UPS capabilities it should return various variables and their values. See http://www.networkupstools.org/docs/user-manual.chunked/apcs01.html for more details. If you want to check if is it working on battery right now execute
upsc nutdev1 ups.status
and the output should be OL (online) or OB (on battery), LB (low battery), etc. You can also play around with some of the UPS commands/settings
upscmd nutdev1 beeper.toggle

After that reboot the system (or stop all daemons)



Step4. Starts UPS daemons automatically

Execute following commands:
systemctl enable nut-server
systemctl start nut-server
systemctl enable nut-monitor.service
systemctl start nut-monitor.service
Restart the system, and if check if everything is ok, by executing the UPS status with upsc command. Now the UPS monitoring system is configured and in case of power loss, the computer will shutdown, when the UPS battery is low. If you don't want to shutdown the computer before the 'battery low' signal follow proceed with the step

Step5. Fine tuning

This step is optional, but gives you more control over the UPS events handling. If you want to trunoff the computer before the low battery signal, then use following steps:
1. Edit /etc/ups/upsmon.conf and add following lines:
NOTIFYCMD /usr/sbin/upssched
NOTIFYFLAG ONLINE     EXEC
NOTIFYFLAG ONBATT     EXEC
NOTIFYFLAG LOWBATT    EXEC
NOTIFYFLAG FSD        EXEC
NOTIFYFLAG COMMOK     EXEC
NOTIFYFLAG COMMBAD    EXEC
NOTIFYFLAG SHUTDOWN   EXEC
NOTIFYFLAG REPLBATT   EXEC
NOTIFYFLAG NOCOMM     EXEC
NOTIFYFLAG NOPARENT   EXEC
2. Edit /etc/ups/upssched.conf and add following lines (it will allow you shutdown the computer after 15 seconds, after working on battery)
PIPEFN /var/run/nut/upssched.pipe
LOCKFN /var/run/nut/upssched.pipe
AT ONBATT * START-TIMER executeShutdown 15
AT ONLINE * CANCEL-TIMER executeShutdown
AT ONBATT * EXECUTE onBattery
AT ONLINE * EXECUTE onLine
AT NOCOMM * EXECUTE noComm
AT COMMBAD * EXECUTE commBad
AT COMMOK * EXECUTE commOk
3. Edit /usr/bin/upssched-cmd (see the exact file name from /usr/bin/upssched-cmd variable CMDSCRIPT) and add executeShutdown section similar to:
case $1 in
        upsgone)
                logger -t upssched-cmd "The UPS has been gone for awhile"
                ;;
        executeShutdown)
                shutdown -h now
                ;;
        onBattery)
                logger -t upssched-cmd "UPS is on battery!"
                ;;
        onLine)
                logger -t upssched-cmd "UPS is back online!"
                ;;
        noComm)
                logger -t upssched-cmd "No communication with the UPS device"
                ;;
        commBad)
                logger -t upssched-cmd "Communication with UPS device lost"
                ;;
        commOk)
                logger -t upssched-cmd "Communication with UPS device restored"
                ;;
        *)
                logger -t upssched-cmd "Unrecognized command: $1"
                ;;
esac

Tuesday, April 08, 2014

PermGen space leaks

The causes of PermGen space leaks:
  1. Thread left running after web-app undeployment - the context class loader of the thread is usually the class loader of the web application, so it contains all webapp classes.
  2. Using ThreadLocal with thread created by the web server - web app class is assigned to web server thread (e.g. HTTP worker thread) - the web app class holds a reference to the classloader, even if the web app is undeployed.
  3. Database driver leak - every database driver should register in  java.sql.DriverManager
    the web-app must deregister it from the there, otherwise it hold a reference to the web-app class loader, if the web app is undeploye