{"id":1189,"date":"2013-09-28T07:28:02","date_gmt":"2013-09-28T12:28:02","guid":{"rendered":"http:\/\/www.familytidings.com\/blog\/?p=1189"},"modified":"2013-09-28T07:29:58","modified_gmt":"2013-09-28T12:29:58","slug":"monitoring-a-shell-command-for-differences-in-output","status":"publish","type":"post","link":"https:\/\/www.familytidings.com\/blog\/?p=1189","title":{"rendered":"Monitoring a Shell Command for Differences in Output"},"content":{"rendered":"<p>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&#8217;ve seen any trouble since. I can see in logs on the NAS when the NAS&#8217;s UPS switches to battery and back, but the NAS doesn&#8217;t show the UPS&#8217;s voltage. The UPSs can report line voltage, but it doesn&#8217;t store that information. Momentary status is available by running <code>apcaccess<\/code> which sends status to standard output in a format containing one variable per line like this (non-relevant sections omitted):<\/p>\n<blockquote><p><code>APC : 001,035,0863<br \/>\nDATE : Sat Sep 28 08:06:36 EDT 2013<\/code><br \/>\n[&#8230;]<br \/>\n<code>STATUS : ONLINE<br \/>\nLINEV : 120.0 Volts<\/code><br \/>\n[&#8230;]<br \/>\n<code>END APC : Sat Sep 28 08:06:59 EDT 2013<\/code><\/p><\/blockquote>\n<p>This morning, I wanted to watch the voltage to see how much it&#8217;s changing. I can run <code>watch<\/code>, which will run a command repeatedly and show what values have changed since <code>watch<\/code> has been running, but it doesn&#8217;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:<\/p>\n<pre>#!\/bin\/bash\r\n\r\nCOMMAND=\"$*\"\r\nLAST_COMMAND_OUTPUT=\"&lt;none&gt;\"\r\nTHIS_COMMAND_OUTPUT=\"\"\r\nDASHES=\"==========\"\r\nwhile true; do\r\n\tTHIS_COMMAND_OUTPUT=\"`eval ${COMMAND}`\"\r\n\tif [ \"${THIS_COMMAND_OUTPUT}\" != \"${LAST_COMMAND_OUTPUT}\" ]; then\r\n\t    echo ${DASHES} $(date) ${DASHES}\r\n\t    echo \"${THIS_COMMAND_OUTPUT}\"\r\n\t    LAST_COMMAND_OUTPUT=\"${THIS_COMMAND_OUTPUT}\"\r\n\tfi\r\n\tsleep 1\r\ndone<\/pre>\n<p>Since I&#8217;m looking for changes in the line voltage, I run it like this:<br \/>\n<code>watchdiff 'apcaccess | grep LINEV'<\/code><br \/>\nThe parameters need to be enclosed in quotes here because otherwise the shell will interpret that as <code>(watchdiff apcaccess) | grep LINEV<\/code>, which will run watchdiff over the entire output of apcaccess, <em>then<\/em> grep for LINEV. Since apcaccess includes time in its report, the output is always different.<\/p>\n<p>Lastly, to record this data, I run the command thusly:<br \/>\n<code>watchdiff 'apcaccess | grep LINEV' | tee nas-voltage-report.txt<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1189","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/www.familytidings.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1189","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.familytidings.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.familytidings.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.familytidings.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.familytidings.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1189"}],"version-history":[{"count":16,"href":"https:\/\/www.familytidings.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1189\/revisions"}],"predecessor-version":[{"id":1205,"href":"https:\/\/www.familytidings.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1189\/revisions\/1205"}],"wp:attachment":[{"href":"https:\/\/www.familytidings.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1189"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.familytidings.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1189"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.familytidings.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1189"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}