133 lines
3.5 KiB
Bash
133 lines
3.5 KiB
Bash
#!/bin/bash
|
|
SPATH=/scripts
|
|
SERVER=$1
|
|
SERVERKEY=$2
|
|
SERVERHASH=$3
|
|
SERVERURL=$4
|
|
|
|
if [ "$4" == "" ]; then
|
|
echo Aufruf:
|
|
echo " $0 <SERVER> <KEY> <HASH> <URL>"
|
|
echo "Beispiele"
|
|
echo " $0 DAGOBERT \"H4565-O0VPE-A3KTI\" \"cca255c16db3b2ab1429e00416568d67513c75ff\" http://groupoffice.agadmin.de"
|
|
echo " $0 DAISY \"KTV9G-5XAFA-CKQNB\" \"1a6bede9c48d9e0e188acf59c7b01b33d17e1813\" https://mscp.agadmin.de:4443"
|
|
echo ""
|
|
exit
|
|
fi
|
|
|
|
Pushover () {
|
|
HOST=$1
|
|
MSG=$2
|
|
echo "Send Pushover Message"
|
|
curl -s --form-string "token=aBxDT9DHgzq1CboHVMJdRAtZdi8XbB" --form-string "user=uMwPrXwN7hLbFe6GgoQTctdwzso3TP" --form-string "message=$MSG" --form-string "title=$HOST" --form-string "url=https://vpanel.colorhost.de" --form-string "url_title=colorhost vPanel" https://api.pushover.net/1/messages.json
|
|
}
|
|
|
|
ServerStatus () {
|
|
KEY=$1
|
|
HASH=$2
|
|
STAT=`curl --connect-timeout 3 --max-time 5 -s "https://vpanel.colorhost.de:5656/api/client/command.php?action=status&key=$KEY&hash=$HASH" | perl -pe's/.*<statusmsg>(.*)<\/statusmsg>.*/$1/'`
|
|
if [ "$STAT" == "online" ]; then
|
|
return 1
|
|
fi
|
|
return 0
|
|
}
|
|
|
|
ServerAction () {
|
|
KEY=$1
|
|
HASH=$2
|
|
ACTION=$3
|
|
STAT=`curl --connect-timeout 3 --max-time 5 -s "https://vpanel.colorhost.de:5656/api/client/command.php?action=$ACTION&key=$KEY&hash=$HASH" | perl -pe's/.*<statusmsg>(.*)<\/statusmsg>.*/$1/'`
|
|
}
|
|
|
|
GetHTMLHeader () {
|
|
URL=$1
|
|
curl --connect-timeout 3 --max-time 5 -sI "$URL" > /dev/null
|
|
if [ "$?" == "0" ]; then
|
|
return 1
|
|
fi
|
|
return 0
|
|
}
|
|
|
|
WE=0
|
|
SE=0
|
|
OWE=0
|
|
OSE=0
|
|
while [ 1 ]; do
|
|
echo `date`
|
|
echo "Check Webpage $SERVERURL"
|
|
GetHTMLHeader $SERVERURL
|
|
RESPONSE=$?
|
|
if [ "$RESPONSE" == "1" ]; then
|
|
WE=0
|
|
SE=0
|
|
if [ "$OWE" == "1" ]; then
|
|
echo "Webpage $SERVERURL recovered"
|
|
Pushover $SERVER "Webpage $SERVERURL recovered"
|
|
OWE=0
|
|
else
|
|
echo "Webpage $SERVERURL up"
|
|
fi
|
|
else
|
|
(( WE++ ))
|
|
if [ "$OWE" == "0" ]; then
|
|
OWE=1
|
|
echo "Webpage $SERVERURL went down"
|
|
Pushover $SERVER "Webpage $SERVERURL went down"
|
|
else
|
|
echo "Webpage $SERVERURL down"
|
|
fi
|
|
fi
|
|
|
|
echo "Check Server $SERVER"
|
|
ServerStatus $SERVERKEY $SERVERHASH
|
|
STATUS=$?
|
|
if [ "$STATUS" == "1" ]; then
|
|
SE=0
|
|
if [ "$OSE" == "1" ]; then
|
|
echo "Server $SERVER recovered"
|
|
Pushover $SERVER "Server recovered"
|
|
OSE=0
|
|
else
|
|
echo "Server $SERVER up"
|
|
fi
|
|
else
|
|
(( SE++ ))
|
|
if [ "$OSE" == "0" ]; then
|
|
OSE=1
|
|
echo "Server $SERVER went down"
|
|
Pushover $SERVER "Server went down"
|
|
else
|
|
echo "Server $SERVER down"
|
|
fi
|
|
fi
|
|
|
|
if [ "$SE" -gt "3" ]; then
|
|
echo "Server reboot initiated for $SERVER"
|
|
Pushover $SERVER "Server reboot initiated"
|
|
ServerAction $SERVERKEY $SERVERHASH "reboot"
|
|
ServerAction $SERVERKEY $SERVERHASH "boot"
|
|
SE=0
|
|
sleep 10
|
|
else
|
|
if [ "$WE" -gt "3" ] && [ "$SE" == "0" ]; then
|
|
echo "Apache restart initiated on $SERVER"
|
|
Pushover $SERVER "Apache restart initiated"
|
|
ssh -i /home/andre/.ssh/id_rsa andre@$SERVER.agadmin.de 'sudo /etc/init.d/apache2 restart' &
|
|
sleep 5
|
|
fi
|
|
|
|
if [ "$WE" -gt "6" ] && [ "$SE" == "0" ]; then
|
|
echo "Server reboot initiated for $SERVER"
|
|
Pushover $SERVER "Server reboot initiated"
|
|
ServerAction $SERVERKEY $SERVERHASH "reboot"
|
|
ServerAction $SERVERKEY $SERVERHASH "boot"
|
|
SE=0
|
|
sleep 10
|
|
fi
|
|
fi
|
|
sleep 60
|
|
echo ""
|
|
|
|
done
|
|
|