September 2013

Monitoring a Shell Command for Differences in Output

A few weeks ago, we experienced some fluctuations in the electric power coming in to the house. Early in the morning on weekends, the line voltage dropped and recovered repeatedly, causing my two UPSs to freak out. They switched to battery and back over and over until the battery ran dead. The utility company has since corrected the problem, but they called to ask if I’ve seen any trouble since. I can see in logs on the NAS when the NAS’s UPS switches to battery and back, but the NAS doesn’t show the UPS’s voltage. The UPSs can report line voltage, but it doesn’t store that information. Momentary status is available by running apcaccess which sends status to standard output in a format containing one variable per line like this (non-relevant sections omitted):

APC : 001,035,0863
DATE : Sat Sep 28 08:06:36 EDT 2013

[…]
STATUS : ONLINE
LINEV : 120.0 Volts

[…]
END APC : Sat Sep 28 08:06:59 EDT 2013

This morning, I wanted to watch the voltage to see how much it’s changing. I can run watch, which will run a command repeatedly and show what values have changed since watch has been running, but it doesn’t show the history of those changes. A little Internet searching came up empty for a utility that will get this job done. So, I wrote this Bash script called watchdiff:

#!/bin/bash

COMMAND="$*"
LAST_COMMAND_OUTPUT="<none>"
THIS_COMMAND_OUTPUT=""
DASHES="=========="
while true; do
	THIS_COMMAND_OUTPUT="`eval ${COMMAND}`"
	if [ "${THIS_COMMAND_OUTPUT}" != "${LAST_COMMAND_OUTPUT}" ]; then
	    echo ${DASHES} $(date) ${DASHES}
	    echo "${THIS_COMMAND_OUTPUT}"
	    LAST_COMMAND_OUTPUT="${THIS_COMMAND_OUTPUT}"
	fi
	sleep 1
done

Since I’m looking for changes in the line voltage, I run it like this:
watchdiff 'apcaccess | grep LINEV'
The parameters need to be enclosed in quotes here because otherwise the shell will interpret that as (watchdiff apcaccess) | grep LINEV, which will run watchdiff over the entire output of apcaccess, then grep for LINEV. Since apcaccess includes time in its report, the output is always different.

Lastly, to record this data, I run the command thusly:
watchdiff 'apcaccess | grep LINEV' | tee nas-voltage-report.txt

Uncategorized

Comments (0)

Permalink

APC Back-UPS ES 500 with Synology NAS

The NAS is a Synology DS411+II. The UPS is an APC Back-UPS ES 500 connected to the NAS with the supplied USB cable.
With the UPS connected to the NAS, go to the NAS’ web page (port 5000). From the top left menu button, select “System Information”. On the General tab, under External Devices, you should see the UPS. From there, go to Control Panel, select Power, select the UPS tab and select Enable UPS support.

The following instructions are based on the Synology forum post “Success with DS-209 and APCupsd UPS Monitoring“. Although the NAS can see the UPS and gets status from it, I was unable to get apcupsd to work.

To install the monitoring deamon, log in to the NAS as root:
ipkg install apcupsd
Edit the file /opt/etc/apcupsd/apcupsd.conf.
If you have more than one UPS, set the UPSNAME to keep them straight. This is optional. Change UPSCABLE to usb. Change UPSTYPE to usb and set DEVICE to blank.
mkdir -p /opt/var/lock
mkdir -p /opt/var/log
touch /opt/var/log/apcupsd.events
touch /opt/var/log/apcupsd.status

To test: apcupsd -b -d5 -T -f /opt/etc/apcupsd/apcupsd.conf. Open another shell to the NAS ans root and run apctest.

Uncategorized

Comments (0)

Permalink

Using ipkg with Synology DSM 4

ipkg is available with version 4 of Synology DSM, it’s just in a different place. To use it, log in as root and edit /root/.profile. Add the following line at the end of the file:
PATH=/opt/bin:/opt/sbin:$PATH
You don’t need to export the path as that’s done earlier in the script.

Uncategorized

Comments (0)

Permalink