Last active
August 29, 2015 13:56
-
-
Save cinsk/9048297 to your computer and use it in GitHub Desktop.
force soft reboot Scalr-managed server if abnormal reboot detected
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
# reboot-abnormal Force soft-reboot when Scalr event was not received | |
# | |
# chkconfig: 2345 80 10 | |
# description: Force soft reboot when Scalr event was not received | |
# | |
# pidfile: /var/run/reboot-abnormal.pid | |
. /etc/rc.d/init.d/functions | |
function uptime() { | |
# print uptime in seconds from epoch | |
cat /proc/uptime | \ | |
(read a b; now=$(date +%s); up=${a/.*/}; echo $((now - up)); ) | |
} | |
TMUP=${UPTIME:-$(uptime)} | |
LOGFILE=${LOGFILE:-/var/log/scalarizr_debug.log} | |
#echo $TMUP | |
#exit 0 | |
function abnormal() { | |
local ret | |
awk -F " - " -f <(cat - <<-EOF | |
BEGIN { | |
uptime=$1 | |
isnormal=0 | |
} | |
\$3 == "scalarizr.messaging.p2p.producer" { | |
gsub(/,[0-9]+\$/, "", \$1); | |
gsub(/[-:]/, " ", \$1); | |
tm = mktime(\$1); | |
#print uptime, tm | |
if (tm >= uptime && \$4 ~ /^Message/) { | |
if (\$4 ~ /'HostInit'/ || \$4 ~ /'RebootFinish'/) | |
isnormal=1 | |
} | |
} | |
END { | |
printf "isnormal=%d\n", isnormal | |
exit isnormal | |
} | |
EOF | |
) "$LOGFILE" | |
ret=$? | |
return $ret; | |
} | |
function start() { | |
(echo $$ > /var/run/reboot-abnormal.pid; \ | |
sleep 300; \ | |
if abnormal $TMUP; then reboot; fi;) & | |
} | |
function stop() { | |
kill $(cat /var/run/reboot-abnormal.pid) | |
} | |
function restart() { | |
stop | |
start | |
} | |
case "$1" in | |
start) | |
$1 | |
;; | |
stop) | |
$1 | |
;; | |
restart) | |
$1 | |
;; | |
abnormal) | |
$1 $TMUP | |
;; | |
*) | |
echo "Usage: $0 {start|stop|restart}" | |
exit 1 | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment