| 1 |
#!/bin/bash |
| 2 |
# avr-evtd Event script |
| 3 |
# |
| 4 |
# Sample Event Script written by Bob Perry |
| 5 |
# NOTE: No longer syncronous with avr-evtd process: maybe more than |
| 6 |
# one instance of this script running |
| 7 |
# |
| 8 |
# Enters via the avr-evtd daemon with the following command options: |
| 9 |
# 0 - Special event following double press of the 'red' reset button |
| 10 |
# 1 - AVR device has requested a halt due to error |
| 11 |
# 2 - Shutdown request from timed shutdown logic |
| 12 |
# 3 - User has released the power button |
| 13 |
# 4 - User has pressed the power button |
| 14 |
# 5 - User has released the 'red' reset button |
| 15 |
# 6 - User has pressed the reset button |
| 16 |
# 7 - User has held the power button > 3 seconds |
| 17 |
# 8 - User has held the reset button > 3 seconds |
| 18 |
# 9 - Disk used beyond DISKCHECK% |
| 19 |
# F - Fan failure event, fan stopped for > FANSTOP seconds |
| 20 |
# E - User selected EM-Mode |
| 21 |
# S - Five minute shutdown warning event |
| 22 |
# D - Error message handler |
| 23 |
|
| 24 |
DEVICE=$2 |
| 25 |
|
| 26 |
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin |
| 27 |
tag=avr-daemon |
| 28 |
facility=user.info |
| 29 |
|
| 30 |
# |
| 31 |
# Populate the configured settings |
| 32 |
# |
| 33 |
if [ -f /etc/default/avr-evtd ]; then |
| 34 |
. /etc/default/avr-evtd |
| 35 |
fi |
| 36 |
|
| 37 |
if [ "$DEBUG" = "ON" ] && [ -d "$LOG" ]; then |
| 38 |
echo "`date` command is $1[$3] for $DEVICE" >> $LOG/avr-evtd.log |
| 39 |
fi |
| 40 |
|
| 41 |
case "$1" in |
| 42 |
0) |
| 43 |
case "$3" in |
| 44 |
1) # Indicate this mode by flashing the DISK FULL LED |
| 45 |
echo -n "YYYY" > $DEVICE |
| 46 |
# Add an EM IP address or if no ethernet, start it up |
| 47 |
FAIL=1 |
| 48 |
route -n | grep -q eth0 && FAIL=0 |
| 49 |
if [ "$FAIL" -eq 0 ]; then |
| 50 |
FAIL=1 |
| 51 |
route -n | grep -q 192.168.11.0 && FAIL=0 |
| 52 |
if [ "$FAIL" -eq 1 ]; then |
| 53 |
ifconfig eth0:EM 192.168.11.150 netmask 255.255.255.0 up |
| 54 |
fi |
| 55 |
else |
| 56 |
/sbin/ifup --force -a -i /etc/avr-evtd/emergency-eth0 |
| 57 |
sleep 1 |
| 58 |
route add -net 192.168.11.0 netmask 255.255.255.255.0 eth0:EM |
| 59 |
fi |
| 60 |
if [ -e /sbin/utelnetd ]; then |
| 61 |
utelnetd -p 1234 -l /bin/bash & |
| 62 |
if [ -e /etc/init.d/apservd ]; then |
| 63 |
chmod +x /etc/init.d/apservd |
| 64 |
/etc/init.d/apservd start |
| 65 |
fi |
| 66 |
fi ;; |
| 67 |
3) # Flash more lights to indicate we are desperate |
| 68 |
echo -n "SSSS" > $DEVICE |
| 69 |
if [ ! -e /etc/passwd.em ]; then cp -f /etc/passwd /etc/passwd.em ; fi |
| 70 |
if [ ! -e /etc/shadow.em ]; then cp -f /etc/shadow /etc/shadow.em ; fi |
| 71 |
if [ ! -e /etc/group.em ]; then cp -f /etc/group /etc/group.em ; fi |
| 72 |
if [ ! -e /etc/gshadow.em ]; then cp -f /etc/gshadow /etc/gshadow.em ; fi |
| 73 |
if [ ! -e /etc/issue.net.em ]; then cp -f /etc/issue.net /etc/issue.net.em ; fi |
| 74 |
tar xvf /etc/avr-evtd/recovery.tar |
| 75 |
;; |
| 76 |
*) |
| 77 |
exit -2 |
| 78 |
;; |
| 79 |
esac |
| 80 |
;; |
| 81 |
3) |
| 82 |
echo -n "[avr-evtd]: Power Button Up" |
| 83 |
;; |
| 84 |
4) |
| 85 |
echo -n "[avr-evtd]: Power Button Down" |
| 86 |
;; |
| 87 |
5) |
| 88 |
echo -n "[avr-evtd]: Reset Button Up" |
| 89 |
;; |
| 90 |
6) |
| 91 |
echo -n "[avr-evtd]: Reset Button Down" |
| 92 |
;; |
| 93 |
1|2|7) |
| 94 |
echo -n "[avr-evtd]: Shutdown" |
| 95 |
echo -n "]]]]EEEE" > $DEVICE |
| 96 |
# Perform shutdown request |
| 97 |
shutdown -h now |
| 98 |
;; |
| 99 |
8) |
| 100 |
echo -n "[avr-evtd]: User demanded reset" |
| 101 |
echo -n "]]]]CCCC" > $DEVICE |
| 102 |
# Perform reboot |
| 103 |
reboot |
| 104 |
;; |
| 105 |
9) |
| 106 |
if [ "$3" -eq 0 ]; then |
| 107 |
echo -n "[avr-evtd]: Disk usage now safe" |
| 108 |
else |
| 109 |
echo -n "[avr-evtd]: Disk used $3% > Monitored $DISKCHECK%" |
| 110 |
fi |
| 111 |
;; |
| 112 |
E) |
| 113 |
echo -n "[avr-evtd]: EM mode selected" |
| 114 |
if [ "$EMMODE" = "YES" ]; then |
| 115 |
# Determine flash block to use, default to 2 |
| 116 |
if [ -f /proc/mtd ]; then |
| 117 |
MTD=`cat /proc/mtd | grep mtd_status` |
| 118 |
BLOCK_ID=${MTD:3:1} |
| 119 |
if [ -z "$BLOCK_ID" ]; then BLOCK_ID=2 ; fi |
| 120 |
FLASH=/dev/mtdblock$BLOCK_ID |
| 121 |
else |
| 122 |
ls -al /dev/fl3 | grep -q "250,[ ]*3" && FLASH=/dev/fl3 |
| 123 |
fi |
| 124 |
|
| 125 |
# Flash device available? |
| 126 |
if [ -n "$FLASH" ]; then |
| 127 |
# Check we are dealing with a buffered device? |
| 128 |
if [ ! -b $FLASH ]; then |
| 129 |
echo "$FLASH device does not exist, no EM-Mode available" >&2 |
| 130 |
else |
| 131 |
DUMP=`cat $FLASH` |
| 132 |
STATE=${DUMP:2:4} |
| 133 |
# Check state is currently NORMAL |
| 134 |
if [ "$STATE" = "OKOK" ]; then |
| 135 |
echo -n "NGNGNG" > $FLASH |
| 136 |
fi |
| 137 |
echo -n "]]]]CCCC" > $DEVICE |
| 138 |
/sbin/reboot |
| 139 |
fi |
| 140 |
fi |
| 141 |
fi |
| 142 |
;; |
| 143 |
F) |
| 144 |
echo -n "[avr-evtd]: Fan failure detected" |
| 145 |
if [ "$3" -eq 0 ]; then |
| 146 |
logger -p user.emerg -i 'AVR Detected fan fault' |
| 147 |
fi |
| 148 |
if [ "$3" -eq 4 ]; then |
| 149 |
# Illuminate relevant LED and wait for AVR halt message |
| 150 |
echo -n "iiii" > $DEVICE |
| 151 |
fi |
| 152 |
;; |
| 153 |
S) |
| 154 |
if [ "$3" -gt 100 ]; then |
| 155 |
MESSAGE="System shutdown in less than 5 minutes" |
| 156 |
else |
| 157 |
MESSAGE="Shutdown delayed by $3 minutes" |
| 158 |
fi |
| 159 |
# Produce relevant message |
| 160 |
logger -p user.emerg -i $MESSAGE |
| 161 |
;; |
| 162 |
D) |
| 163 |
MESSAGE="[$3] Error with configuration file" |
| 164 |
logger -t $tag -p $facility -i $MESSAGE |
| 165 |
;; |
| 166 |
*) |
| 167 |
exit 1 |
| 168 |
;; |
| 169 |
esac |
| 170 |
|
| 171 |
exit 0 |