Bidule0hm
Server Electronics Sorcerer
- Joined
- Aug 5, 2013
- Messages
- 3,710
Sorry to post here but I don't have the permission to post in the Hacking section, maybe a moderator can move this topic if he determines that it's worth it.
Disclaimer: Although there is very little chance that something goes wrong you'll do this at your own risk.
So the goal is to add some of the UPS attributes provided by the upsc command, like the battery charge for example, to the email that is send when the UPS goes on battery, etc.
1) Open the shell in the GUI or SSH to the server (you must be root or be able to sudo)
2) If you are on FreeNAS 9.2.* or less mount the filesystem writable (type mount -uw /)
3) Type cd /usr/local/bin/
4) Type nano custom-upssched-cmd (or use any other editor that you like)
5) You should see something like this:
6) Comment (or delete) the line 34 by adding a # at the start, like this:
7) Just after this line add this:
8) You can personalize the attributes depending on your particular UPS, use upsc yourUpsName@localhost to see all the attributes and pick the ones you want.
9) The file should now look like this:
10) Save the changes and quit the editor (in nano do Ctrl + O, Enter, Ctrl + X)
11) If you are on FreeNAS 9.2.* or less remount the filesystem readonly (type mount -ur /), this will take some time, let it terminate.
12) The server must be able to send emails and the email notifier should be enabled and correctly configured in the UPS service settings (Web GUI --> Services --> UPS --> the concerned settings are the 3 last).
13) Test it by unplugging the UPS for a few seconds, give enough time to the UPS deamon to read the UPS data (by default the polling interval is 2 seconds), then replug it.
14) You should receive an email like this one:
14) Followed by another email like this one:
That's all :D I hope you found this useful ;)
Disclaimer: Although there is very little chance that something goes wrong you'll do this at your own risk.
So the goal is to add some of the UPS attributes provided by the upsc command, like the battery charge for example, to the email that is send when the UPS goes on battery, etc.
1) Open the shell in the GUI or SSH to the server (you must be root or be able to sudo)
2) If you are on FreeNAS 9.2.* or less mount the filesystem writable (type mount -uw /)
3) Type cd /usr/local/bin/
4) Type nano custom-upssched-cmd (or use any other editor that you like)
5) You should see something like this:
Code:
#! /bin/sh # # This script should be called by upssched via the CMDSCRIPT directive. # # Here is a quick example to show how to handle a bunch of possible # timer names with the help of the case structure. # # This script may be replaced with another program without harm. # # The first argument passed to your CMDSCRIPT is the name of the timer # from your AT lines. . /etc/rc.freenas PATH=/sbin:/usr/sbin:/bin:/usr/bin:/usr/local/sbin:/usr/local/bin IFS=\| f="ups_emailnotify ups_toemail ups_subject ups_shutdown" sf=$(echo $f | sed -e 's/ /, /g') ${FREENAS_SQLITE_CMD} ${FREENAS_CONFIG} \ "SELECT $sf FROM services_ups ORDER BY -id LIMIT 1" | \ while eval read $f; do case $1 in "ONBATT") if [ "${ups_shutdown}" = "batt" -a "$1" = "ONBATT" ]; then logger -t upssched-cmd "issuing shutdown" /usr/local/sbin/upsmon -c fsd fi ;; "EMAIL"|"COMMBAD"|"COMMOK") if [ "${ups_emailnotify}" -eq 1 ]; then echo "$NOTIFYTYPE - $UPSNAME" | mail -s "$(echo "${ups_subject}"|sed "s/%d/$(date)/"|sed "s/%h/$(hostname)/")" "${ups_toemail}" fi ;; "ONLINE") #Missed timer, but already emailed. ;; *) logger -t upssched-cmd "Unrecognized command: $1" ;; esac done
6) Comment (or delete) the line 34 by adding a # at the start, like this:
Code:
# echo "$NOTIFYTYPE - $UPSNAME" | mail -s "$(echo "${ups_subject}"|sed "s/%d/$(date)/"|sed "s/%h/$(hostname)/")" "${ups_toemail}"
7) Just after this line add this:
Code:
logfile="/tmp/ups_notify.tmp" ( echo "To: ${ups_toemail}" echo "Subject: ${ups_subject}" echo "Content-Type: text/html" echo "MIME-Version: 1.0" echo -e "\r\n" echo "<pre style=\"font-size:14px\">" echo "" echo "<b>$NOTIFYTYPE</b>" echo "" date "+Time: %Y-%m-%d %H:%M:%S" echo "UPS Status: `upsc ups@localhost ups.status`" echo "Output Load: `upsc ups@localhost ups.load` %" echo "Output Voltage: `upsc ups@localhost output.voltage` V" echo "Battery Runtime: `upsc ups@localhost battery.runtime` s" echo "Battery Charge: `upsc ups@localhost battery.charge` %" echo "" echo "</pre>" ) > ${logfile} sendmail -t < ${logfile} rm ${logfile}
8) You can personalize the attributes depending on your particular UPS, use upsc yourUpsName@localhost to see all the attributes and pick the ones you want.
9) The file should now look like this:
Code:
#! /bin/sh # # This script should be called by upssched via the CMDSCRIPT directive. # # Here is a quick example to show how to handle a bunch of possible # timer names with the help of the case structure. # # This script may be replaced with another program without harm. # # The first argument passed to your CMDSCRIPT is the name of the timer # from your AT lines. . /etc/rc.freenas PATH=/sbin:/usr/sbin:/bin:/usr/bin:/usr/local/sbin:/usr/local/bin IFS=\| f="ups_emailnotify ups_toemail ups_subject ups_shutdown" sf=$(echo $f | sed -e 's/ /, /g') ${FREENAS_SQLITE_CMD} ${FREENAS_CONFIG} \ "SELECT $sf FROM services_ups ORDER BY -id LIMIT 1" | \ while eval read $f; do case $1 in "ONBATT") if [ "${ups_shutdown}" = "batt" -a "$1" = "ONBATT" ]; then logger -t upssched-cmd "issuing shutdown" /usr/local/sbin/upsmon -c fsd fi ;; "EMAIL"|"COMMBAD"|"COMMOK") if [ "${ups_emailnotify}" -eq 1 ]; then # echo "$NOTIFYTYPE - $UPSNAME" | mail -s "$(echo "${ups_subject}"|sed "s/%d/$(date)/"|sed "s/%h/$(hostname)/")" "${ups_toemail}" logfile="/tmp/ups_notify.tmp" ( echo "To: ${ups_toemail}" echo "Subject: ${ups_subject}" echo "Content-Type: text/html" echo "MIME-Version: 1.0" echo -e "\r\n" echo "<pre style=\"font-size:14px\">" echo "" echo "<b>$NOTIFYTYPE</b>" echo "" date "+Time: %Y-%m-%d %H:%M:%S" echo "UPS Status: `upsc ups@localhost ups.status`" echo "Output Load: `upsc ups@localhost ups.load` %" echo "Output Voltage: `upsc ups@localhost output.voltage` V" echo "Battery Runtime: `upsc ups@localhost battery.runtime` s" echo "Battery Charge: `upsc ups@localhost battery.charge` %" echo "" echo "</pre>" ) > ${logfile} sendmail -t < ${logfile} rm ${logfile} fi ;; "ONLINE") #Missed timer, but already emailed. ;; *) logger -t upssched-cmd "Unrecognized command: $1" ;; esac done
10) Save the changes and quit the editor (in nano do Ctrl + O, Enter, Ctrl + X)
11) If you are on FreeNAS 9.2.* or less remount the filesystem readonly (type mount -ur /), this will take some time, let it terminate.
12) The server must be able to send emails and the email notifier should be enabled and correctly configured in the UPS service settings (Web GUI --> Services --> UPS --> the concerned settings are the 3 last).
13) Test it by unplugging the UPS for a few seconds, give enough time to the UPS deamon to read the UPS data (by default the polling interval is 2 seconds), then replug it.
14) You should receive an email like this one:
ONBATT
Time: 2015-02-13 01:32:09
UPS Status: OB DISCHRG
Output Load: 4 %
Output Voltage: 230.0 V
Battery Runtime: 1493 s
Battery Charge: 100 %
14) Followed by another email like this one:
ONLINE
Time: 2015-02-13 01:32:18
UPS Status: OL CHRG
Output Load: 4 %
Output Voltage: 230.0 V
Battery Runtime: 1175 s
Battery Charge: 100 %
That's all :D I hope you found this useful ;)