~skiqqy/bin

bin/serverstat -rwxr-xr-x 1.6 KiB
c0954778Stephen Cochrane Prog on deploy 2 days ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/bin/bash
# Auther: Skiqqy
# This script checks on my web server and sends an email if it is down.

SCRIPT_PATH=$(dirname $0)
. "$SCRIPT_PATH/import/alert.sh" > /dev/null 2>&1
[ ! $(command -v error) ] && echo "[WARNING] Missing 'error.sh' import."
[ ! $(command -v mail) ] && error "Missing \'mail\' command."
[ ! $(command -v mailx) ] && error "Missing \'mailx\' command."
[ ! $(command -v sendmail) ] && error "Missing \'sendmail\' command."

# Vars
domain="skiqqy.xyz"
subd=( api git irc proj blog wiki files social music dev )
MAIL="skippycochrane@gmail.com" # Who we will me mailing.
SLEEP=60
DOWNC=5 # What we consider to be an unacceptable amount of down domains warrenting an email.

http_code () {
	echo $(curl -s -o /dev/null -w "%{http_code}" $1)
}

# Sends a mail to described person
# $1 message
# $2 subject
send_mail () {
	if [ -z $reset ]
	then
		warning "Server down, sending mail"
		reset=0
		echo -e "$1" | mail -s $2 $MAIL
	fi
}

for ((;;))
do
	code=$(http_code https://$domain)
	down=()
	downc=0
	if [ "$code" -eq 200 ]
	then
		for sub in ${subd[@]}
		do
			code=$(http_code "https://$sub.$domain")
			if [ ! "$code" -eq 200 ]
			then
				# We take note that this domain is down.
				downc=$(( downc + 1 ))
				down+=( "$sub.domain" )
			fi
		done

		# Check to see if the # of downed domains warrents an email.
		if [ "$downc" -ge $DOWNC ]
		then
			send_mail "The following domains are down\n${down[@]}" "Server Warning: Catastrophic"
		else
			reset=
		fi
	else
		warning "$domain is down"
		send_mail "$domain is down." "Server Warning"
	fi
	sleep $SLEEP
done