summaryrefslogtreecommitdiff
path: root/bin/check.sh
blob: 5c9b938bfc54ff5ca81b1be952516f6c7ee5a7e8 (plain)
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#!/bin/bash

# enter a mail adress for the report
NOTICETO=""

# set to 1 to NOT send a message on failure
NONOTICE=

# add process names to check. escape white spaces
CheckList=(
	nginx 
	mysqld
	php5-fpm
)

# set to 1 to suppress output (cronjob)
QUIET=

# do not edit under this line
# --------------------------

usage(){
	echo "Usage: $1"
	echo "-c --check 		add a process name to check"
	echo "-p --print		print list of process we check for"
	echo "-q --quiet		surpress output"
	echo "-v --verbose		be verbose"
	echo "-m --mailto ADRESS	send report to ADRESS"
	echo "-n --nonotice		don't send mail"
	echo "-h --help		prints this help"
	echo "Have a nice day."
	exit 0
}

while true; do
	case "$1" in
		-c|--check)	if [ "x$2" != "x" ]; then	
					CheckList+=("$2")	
				else
					echo "$1 requieres a process name as an argument" 1>&2
					exit 1
				fi
				shift
				shift
				;;
		-p|--print)	for i in ${CheckList[@]}; do
					echo $i
				done
				exit 0
				;;
		-q|--quiet)	QUIET=1
				shift
				;;
		-v|--verbose)	QUIET=0
				shift
				;;
		-m|--mailto)
				if [ "x$2" != "x" ]; then
					NOTICETO="$2"
				else
					echo "$1 requieres a mail adress as an argument" 1>&2
					exit 1
				fi
				shift
				shift
				;;
		-n|--nonotice|--nomail)	NONOTICE=1
					shift
					;;
		-h|--help)	usage $0
				shift
				;;
		--)		shift
				break
				;;
		-*)		echo "Unknown argument '$1'" 1>&2
				echo "Try -h for help"
				exit 1
				;;
		*)		break
				;;
	esac
done

# hostname
HOST=$(hostname)

# exit code
EXIT=0

if [ "x$NOTICETO" == "x" ]; then
	NOTICETO="empty"
fi

# check if NONOTICE was set
if [ "x$NONOTICE" == "x" ]; then
	NONOTICE=0
fi

if [ $NOTICETO == "empty" ]; then
	if [ $NONOTICE -eq 0 ]; then
		echo "For status reports we need a mail adress." 1>&2
		exit 1
	fi
fi

# check if QUIET was set
if [ "x$QUIET" == "x" ]; then
	QUIET=0
fi

function do_check() {
	pgrep "$1" > /dev/null 2>&1
	if [ $? -ne 0 ]; then
		if [ $NONOTICE -eq 0 ]; then
			echo "This is a automatic message." | mail -s "$HOST: $1 failure" "$NOTICETO"
		fi

		return 1
	else
		return 0
	fi
}

for i in ${CheckList[@]}; do
	if [ $QUIET -eq 0 ];  then
		echo Checking: $i...
	fi

	do_check "$i" 
	RET=$?
	if [ $RET -ne 0 ]; then
		EXIT=1
	fi

	if [ $QUIET -eq 0 ];  then
		if [ $RET -ne 0 ]; then
			echo -n "$(tput setaf 1)Test for $(tput bold)$i$(tput sgr0) $(tput setaf 1)failed.$(tput sgr0)."
			if [ $NONOTICE -eq 0 ]; then
				echo " We have sent a notice."
			else
				echo ""
			fi
		else
			echo "$(tput bold)$i$(tput sgr0) $(tput setaf 3)is up!$(tput sgr0)"
		fi
	fi
done

exit $EXIT