Simple bash script to monitor your webserver remotely on different ports
Simple bash script to monitor a webserver on different ports (here smtp, dns, http & https but it can be customized) I'm sure there is over 100 available programs doing this but I wanted something with small memory usage. Also, I only wanted to be notified once, notifications are receive by SMS on my cell. With the software I was using before, I was getting notified every minute until I could reach a computer and fix the problem or stop monitoring which was quite annoying.
A BETTER VERSION OF THE SCRIPT USING NMAP INSTEAD OF TELNET IS AVAILABLE HERE
Software installation
You need mail, dig and telnet installed.
The script
NOTE : The script will not continue until you do :
./whatever_you_called_this_script fix
This is done on purpose to receive only ONE notification...
#!/bin/bash # Script to check important ports on remote webserver # Copyright (c) 2009 blogama.org # This script is licensed under GNU GPL version 2.0 or above # --------------------------------------------------------------------- ### This script does a verification on port 25, 53, 80 and 443 ### ### After 2 failed check it will send a mail notification ### ######To be modified###### WORKDIR="/root" ###HTTP### HTTPSERVERIP="192.168.1.106" HTTPSERVERPORT="80" ########## ###HTTPS### HTTPSSERVERIP="192.168.1.106" HTTPSSERVERPORT="443" ########## ###MAIL### SMTPSERVERIP="192.168.1.106" SMTPSERVERPORT="25" ########## ###DNS### DNSSERVERIP="192.168.1.106" DOMAINTOCHECKDNS="example.com" ANSWERIP="192.168.1.106" ######### ###NOTIFICATIONS### EMAIL="admin@example.com" ########## ######End to be modified###### ######Do not make modifications below###### ### Binaries ### MAIL=$(which mail) TELNET=$(which telnet) DIG=$(which dig) ###Change dir### cd $WORKDIR ###Restore when problem fix### if [ $1 ]; then if [ $1=="fix" ]; then rm server_problem*.txt exit 1; fi fi ###Check if already notified### if [ -f server_problem.txt ]; then exit 1; fi ###Test SMTP### ( echo "quit" ) | $TELNET $SMTPSERVERIP $SMTPSERVERPORT | grep Connected > /dev/null 2>&1 if [ "$?" -ne "1" ]; then #Ok echo "PORT CONNECTED" if [ -f server_problem_first_time_smtp.txt ]; then #remove file if problem fixed rm -rf server_problem_first_time_smtp.txt fi else #Connection failure if [ -f server_problem_first_time_smtp.txt ]; then #Second time, send notification below echo "SMTP PORT NOT CONNECTING" >> server_problem.txt rm -rf server_problem_first_time_smtp.txt else #First notification > server_problem_first_time_smtp.txt fi fi ###Test HTTP### ( echo "quit" ) | $TELNET $HTTPSERVERIP $HTTPSERVERPORT | grep Connected > /dev/null 2>&1 if [ "$?" -ne "1" ]; then #Ok echo "PORT CONNECTED" if [ -f server_problem_first_time_http.txt ]; then #remove file if problem fixed rm -rf server_problem_first_time_http.txt fi else #Connection failure if [ -f server_problem_first_time_http.txt ]; then #Second time, send notification below echo "HTTP PORT NOT CONNECTING" >> server_problem.txt rm -rf server_problem_first_time_http.txt else #First notification > server_problem_first_time_http.txt fi fi ###Test HTTPS### ( echo "quit" ) | $TELNET $HTTPSSERVERIP $HTTPSSERVERPORT | grep Connected > /dev/null 2>&1 if [ "$?" -ne "1" ]; then #Ok echo "PORT CONNECTED" if [ -f server_problem_first_time_https.txt ]; then #remove file if problem fixed rm -rf server_problem_first_time_https.txt fi else #Connection failure if [ -f server_problem_first_time_https.txt ]; then #Second time, send notification below echo "HTTPS PORT NOT CONNECTING" >> server_problem.txt rm -rf server_problem_first_time_https.txt else #First notification > server_problem_first_time_https.txt fi fi ###Test DNS### $DIG $DOMAINTOCHECKDNS @$DNSSERVERIP | grep $ANSWERIP if [ "$?" -ne "1" ]; then #Ok echo "PORT CONNECTED" if [ -f server_problem_first_time_dns.txt ]; then #remove file if problem fixed rm -rf server_problem_first_time_dns.txt fi else #Connection failure if [ -f server_problem_first_time_dns.txt ]; then #Second time, send notification below echo "DNS PORT NOT CONNECTING" >> server_problem.txt rm -rf server_problem_first_time_dns.txt else #First notification > server_problem_first_time_dns.txt fi fi ###Send mail notification after 2 failed check### if [ -f server_problem.txt ]; then $MAIL -s "Server problem" $EMAIL < /root/server_problem.txt fi
make it executable :
chmod +x whatever_you_called_this_script
Add the script to your crontab :
* * * * * /root/check >/dev/null 2>&1
- subjects:


Comments
hint: subroutine to bypass firewall
Nice script, but if you have a firewall between you and the web server to check, the telnet command may hang; imagine a minimal situation where even nmap or wget commands are unavailable, but telnet is available. pls. pay attention of the format.
This is my suggestion:
#!/bin/bash
#copyleft michele dot mase at gmail dot com
HOST=www.google.it
PORT=80
OK=KO
TIMEOUT=1
#which is the pid of telnet
telnetpid () {
ps -ef|grep [t]elnet\ $HOST|awk '{print $2}'
}
#check if there is a hanged telnet session
sleep $TIMEOUT && [ -n "$(telnetpid)" ] && kill
$(telnetpid) && exit 1 &
#check telnet result
if telnet $HOST $PORT < /dev/null 2>&1 | grep -q Connected
then
OK=OK
else
OK=$OK
fi
echo Connection $HOST:$PORT=$OK
exit 0
Script results
Hi, This is the output when I run the script. Is this the output I want to see? Do I use the actual static IP of the server or use an internal localhost IP?
root@mojave:~# ./srvwatch.sh
Connection closed by foreign host.
PORT CONNECTED
Connection closed by foreign host.
PORT CONNECTED
Connection closed by foreign host.
PORT CONNECTED
; <<>> DiG 9.4.2-P2 <<>> mydomain.tld @11.111.111.111
PORT CONNECTED
output
Hi, you can remove all echo "PORT CONNECTED" if you dont want any output.
Do I use the actual static IP of the server or use an internal localhost IP?
I did this to monitor from a remote location. For example :
###DNS###
DNSSERVERIP="64.15.75.221"
DOMAINTOCHECKDNS="alivetesting.com"
ANSWERIP="64.15.75.200"
#########
would do
good
it's indeed usefull and yes very "lightwight" i'm running it from two boxes so i can monitor the services from a inet persepctive :) thanks!
pgn.ro