The scheme consists of 3 parts. First is on the client site (odroid), it handles keepalive sending. Other two are on the remote site (server), handling keepalive reception and processing. Following figure demonstrates component cooperation.
keepalive-send.sh:
#!/bin/bash # # krisko 2014 # Simple keepalive send, enable and disable script enable(){ curl -k "https://server.com/keepalive.php?keepalive=enable" } disable(){ curl -k "https://server.com/keepalive.php?keepalive=disable" } alive(){ curl -k "https://server.com/keepalive.php?keepalive=alive" &>/dev/null } rmnotified(){ curl -k "https://server.com/keepalive.php?keepalive=rmnotified" } case "$1" in enable | disable | alive | rmnotified ) $1;; * ) echo "ERROR: unknown option"; exit 1;; esac exit 0 ################## # ChangeLog/TODO # ################## # 1.0.0 2014-06-14 Genesis ##################This script simply calls server.com url, where keepalive.php is listening for keepalive signals and handles to it a parameter (keepalive=alive). It is triggered with crontab (/usr/local/bin/keepalive-send.sh alive) each minute.
keepalive.php:
<?php $keepalive=htmlspecialchars($_GET["keepalive"]); if($keepalive=="help") { echo "Not allowed!\n"; exit(1); } $output = shell_exec("/home/krisko/bin/keepalive.sh $keepalive"); echo "$output"; ?>This script simply calls keepalive.sh handling it the received parameter.
NOTE: you need to enable shell_exec in your php.ini
keepalive.sh:
#!/bin/bash # # krisko 2014 # simple keepalive checking script alivefile="/tmp/krisko-keepalive" notified="/tmp/krisko-notified" MAXTIME=2 NOTIFYTIME=5 SUBJECT="KEEPALIVE Alert" EMAILID="myemail@gmail.com" URL="https://server.com/keepalive.php?keepalive=" help(){ cat << EOF krisko 2014 Simple keepalive script USAGE: $0 (OPTION) OPTIONS: enable enable keepalive checking disable disable keepalive checking alive receive alive signal check check keepalive timers rmnotified remove notified file (cannot be called directly from shell) refalive refresh alive file state (cannot be called directly from shell) EOF } callurl(){ curl -k "$URL$1" } notify(){ echo $@ echo $@ | mail -s "$SUBJECT" $EMAILID } # wee need to call url because od apache permissions rmnotified(){ [ -f $notified ] && rm $notified } mknotified(){ > $notified } refalive(){ > $alivefile } enable(){ > $alivefile callurl rmnotified notify "INFO: keeaplive check enabled" } disable(){ echo ":" > $alivefile notify "INFO: keeaplive check disabled" } checknotify(){ CHKNOTIFY=$(find $notified -cmin +$NOTIFYTIME 2>/dev/null) if [ ! -z $CHKNOTIFY ]; then callurl mknotified notify "ERROR: keepalive timeout"; elif [ ! -f $notified ]; then callurl mknotified notify "ERROR: keepalive timeout"; fi } alive(){ [ $(cat $alivefile) ] && echo "DISABLED" || callurl refalive } check(){ [ $(cat $alivefile) ] && echo "DISABLED" || { CHKFILE=$(find $alivefile -cmin +$MAXTIME 2>/dev/null) [ ! -z $CHKFILE ] && checknotify || { callurl rmnotified; echo OK; } } } case "$1" in "enable" | "disable" | "alive" | "check" | "rmnotified" | "refalive" | "mknotified" ) "$1";; "help" ) help;; * ) echo "INFO: unknown parameter, use help for help"; exit 2;; esac exit 0 ################## # ChangeLog/TODO # ################## # 1.0.0 2014-06-11 Genesis # 1.0.1 2014-06-15 Added url handling functions ##################This script will handle all necessary operation for keepalive signal handling. Notice the refalive function called from within the script. This is due to web server permissions when creating and removing temporary files.
keepalive.sh check:
*/2 * * * * /home/krisko/bin/keepalive.sh check &>/dev/null
This check is triggered with crontab each 2 minutes. It check whether the alivefile="/tmp/krisko-keepalive" was refreshed. If not, this means that the keepalive did not arive and an email notification is sent. Also another temporary file is created (notified="/tmp/krisko-notified") which will assure, that the next notification is sent after NOTIFYTIME period.
Practically, first notification is sent, when the keepalive did not arive after 2 minutes. Then each 6 minutes, an additional notification is sent. This can be adjusted with MAXTIME and NOTIFYTIME variables.
No comments:
Post a Comment