Search This Blog

Wednesday, August 21, 2013

Linux scripting to save blackouts hours

In my house sometimes I got a blackout, don't know why yet, but it happens.
Now, I want to know for how long blackout lasted.
I made up some simple bash scripting for this; possible next actions, link this info to a nagios or ninjablock application to receive notifications:

timer.sh:
root@beaglebone:~/Desktop# cat timer.sh

#!/bin/bash
TARGET=/home/root/Desktop/log.txt
echo $(date -d "today" +"%Y-%m-%d %H:%M:%S") > $TARGET

timerdown.sh: 
root@beaglebone:~/Desktop# cat timerdown.sh

#!/bin/bash
SOURCE=/home/root/Desktop/log.txt
TARGET=/home/root/Desktop/log_$(date -r $SOURCE +"%Y-%m-%d_%H-%M-%S").txt
touch $TARGET
mv $SOURCE $TARGET

crontab:
root@beaglebone:~/Desktop# crontab -l

30 * * * * /usr/bin/ntpdate-sync silent
*/5 * * * * /home/root/Desktop/timer.sh
@reboot /home/root/Desktop/timerdown.sh