Following script was designed for an OP who wanted to monitor his FTP Media sharing server network connectivity along with application running status. Previously the OP was using simple ping script to monitor the FTP server connectivity status, but later realized that most of time the FTP server network connectivity remains OK, but the FTP application (in this particular case its HFS) get crashes after prolonged use. So it was must to monitor the application PORT along with the network connectivity as well.
Requirements:
- Check remote host network connectivity by ping, if found not responding, do not check further to query port status, send alert ping connectivity down alert 1 time until next status change,
- If Ping responds ok, then check the port status, if found not responding, then send alert 1 time until next status change.
Solution:
I made following script which checks for network connectivity by PING and then query port running status using ‘nc’. This is just an example. You can use your own techniques to acquire the same result. This is fully tested and working script. There are many other ways to do the same like using mikrotik netwatch tool which is way too simple, or use any NMS app like Nagios, or DUDE which have good GUI control so no need to do coding in the dark : )
Surely this contains too much junk or some unwanted sections, so you may want to trim it according to your taste and requirements.
Regard’s
Syed Jahanzaib
the Script!
- mkdir /temp
- cd /temp
- touch portmon.sh
- chmod +x portmon.sh
- nano portmon.sh
and paste following, make sure to edit all info accordingly…
#!/bin/sh #set -x # Script to check HOST ping and specific port. / zaib # If found Down, then send one time sms/email until the next status change. # which prevent repeatedly sms for same status. # useful to monitor any application. # Querying port is done by using 'nc' tool. you can use your own customized methods to query about any thing. # For SMS we used local KANNEL as sms gateway # For Email, we have used SENDEMAIL to send email via GMAIL. its a very simple tool and you can use your gmail account. # This script took around 6 hours to complete because of multiple checks for up n down with prevention of repeated sms/email.. # You are free to use ,modify or distribute it. just keep the header intact. # Regard's / # Syed Jahanzaib # Email: aacable at hotmail dot com / http : // aacable . wordpress . com # 5th August, 2016 / Jummah-tul-mubarak, 1st - yakum zilqad, 1437 Hijri HOST="$1" PORT="$2" APPNAME="MIKROTIK" HOSTNAME="hostname" TEMP="temp" # Check if folder exists, if not create one and continue, if already exists, ignore and process further ... if [ ! -d "/$TEMP" ]; then echo echo echo "/$TEMP folder not found, Creating it so all ping results should be saved there . . ." mkdir /$TEMP fi COMPANY="ZABBO (Pvt) Ltd." DATE=`date` # GMAIL DETAILS GMAILID="YOURGMAILID@gmail.com" GMAILPASS="GMAILPASS" ADMINMAIL1="YOURMAIL@hotmail.com" SENDMAIL="/temp/sendEmail-v1.56/sendEmail" # SMS RELATED and KANNEL INFO # KANNEL SMS Gateway Info KANNELURL="127.0.0.1:13013" KANNELID="kannel" KANNELPASS="KANNEL-PASS" CELL1="03333021909" ######################################### # SMS/EMAIL Messages for PING UP / DOWN # ######################################### MSG_DOWN_PING="ALERT: $DATE $HOST not responding to ping request. Check connectivity. $COMPANY" MSG_UP_PING="INFO: $DATE $HOST is reachable now. OK! $COMPANY" # Temporary file holder for storing sms/email PING related msgs which will be sent to admin. MSGDOWNHOLDER_PING="/$TEMP/$HOST.down.msg" MSGUPHOLDER_PING="/$TEMP/$HOST.up.msg" touch $MSGDOWNHOLDER_PING touch $MSGUPHOLDER_PING echo "$MSG_DOWN_PING" > $MSGDOWNHOLDER_PING echo "$MSG_UP_PING" > $MSGUPHOLDER_PING ######################################### # SMS/EMAIL Messages for PORT UP / DOWN # ######################################### # SMS/EMAIL Messages for PORT UP / DOWN MSG_DOWN_PORT="ALERT: $DATE $HOST not responding to $port port request. Check $APPNAME. $COMPANY" MSG_UP_PORT="INFO: $DATE $HOST $PORT port is reachable/responding now. OK! $COMPANY" # Temporary file holder for storing sms/email PORT related msgs which will be sent to admin. MSGDOWNHOLDER_PORT="/$TEMP/$HOST.port.down.msg" MSGUPHOLDER_PORT="/$TEMP/$HOST.port.up.msg" touch $MSGDOWNHOLDER_PORT touch $MSGUPHOLDER_PORT echo "$MSG_DOWN_PORT" > $MSGDOWNHOLDER_PORT echo "$MSG_UP_PORT" > $MSGUPHOLDER_PORT HOST_DOWN_ALERTONSCREEN="ALERT ..... HOST $HOST PING is DOWN @ $DATE ..." HOST_UP_ALERTONSCREEN="INFO ..... HOST $HOST PING is OK @ $DATE ..." HOST_PORT_DOWN_ALERTONSCREEN="ALERT .... System ping is responding OK, BUT PORT number $PORT on $HOST is down @ $DATE" HOST_PORT_UP_ALERTONSCREEN="INFO .... $APPNAME server port nummber $PORT on $HOST is responding OK @ $DATE" # How many PING attempts PING_ATTEMPTS=1 # Temporary file holder for host ping status HOST_PING_STATUS="/$TEMP/$HOST.ping" # Temporary file holder for host port status HOST_PORT_STATUS="/$TEMP/$HOST.port" # Create temp file if not already present, usually for 1st time execution touch $HOST_PING_STATUS touch $HOST_PORT_STATUS ### START ACTION ################################# # CHECK PING SECTION USING PING # ################################# ################################## ### CHECK PING STATUS - for DOWN # ################################## # Check if HOST is accessibel or not, if not then EXIT immediately with error / zaib if [[ $(ping -q -c $PING_ATTEMPTS $HOST) == @(*100% packet loss*) ]]; then echo "$HOST_DOWN_ALERTONSCREEN" if [ $(grep -c "$HOST" "$HOST_PING_STATUS") -eq 1 ]; then echo "Host DOWN SMS have already been sent to $CELL1 ... " fi if [ $(grep -c "$HOST" "$HOST_PING_STATUS") -eq 0 ]; then echo "SENDING HOST PING DOWN SMS/EMAIL ..." echo "$HOST" > $HOST_PING_STATUS # Sending PING DOWN ALERT via EMAIL $SENDMAIL -u "$HOST_DOWN_ALERTONSCREEN" -o tls=yes -s smtp.gmail.com:587 -t $ADMINMAIL1 -xu $GMAILID -xp $GMAILPASS -f $GMAILID -o message-file=$MSGDOWNHOLDER_PING -o message-content-type=text # Sending PING DOWN ALERT via SMS cat $MSGDOWNHOLDER_PING | curl "http://$KANNELURL/cgi-bin/sendsms?username=$KANNELID&password=$KANNELPASS&to=$CELL1" -G --data-urlencode text@- echo "SMS/EMAIL Sent DONE only 1 time until next status change ..." fi # If host is DOWN, then send sms/email alert & exit. exit 1 ################################ ### CHECK PING STATUS - for UP # ################################ else echo "$HOST_UP_ALERTONSCREEN" if [ $(grep -c "$HOST" "$HOST_PING_STATUS") -eq 1 ]; then echo "$HOST is responding OK now, and SMS/EMAIL Sent for UP sent DONE only 1 time until next status change ..." sed -i "/$HOST/d" "$HOST_PING_STATUS" # Sending PING UP ALERT via EMAIL $SENDMAIL -u "$HOST_UP_ALERTONSCREEN" -o tls=yes -s smtp.gmail.com:587 -t $ADMINMAIL1 -xu $GMAILID -xp $GMAILPASS -f $GMAILID -o message-file=$MSGUPHOLDER_PING -o message-content-type=text # Sending PING DOWN ALERT via SMS cat $MSGUPHOLDER_PING | curl "http://$KANNELURL/cgi-bin/sendsms?username=$KANNELID&password=$KANNELPASS&to=$CELL1" -G --data-urlencode text@- fi fi #################################### # CHECK PORT SECTION USING NC TOOL # #################################### ################################ ### CHECK PORT STATUS - for UP # ################################ CHKPORT=`nc -z -w 1 $HOST $PORT; echo $?` if [ $CHKPORT -eq 0 ]; then echo -e "$HOST_PORT_UP_ALERTONSCREEN" if [ $(grep -c "$HOST" "$HOST_PORT_STATUS") -eq 1 ]; then echo "Sending UP SMS 1 time only" # Sending PORT DOWN ALERT via EMAIL $SENDMAIL -u "$HOST_PORT_UP_ALERTONSCREEN" -o tls=yes -s smtp.gmail.com:587 -t $ADMINMAIL1 -xu $GMAILID -xp $GMAILPASS -f $GMAILID -o message-file=$MSGUPHOLDER_PORT -o message-content-type=text # Sending PORT DOWN ALERT via SMS cat $MSGUPHOLDER_PORT | curl "http://$KANNELURL/cgi-bin/sendsms?username=$KANNELID&password=$KANNELPASS&to=$CELL1" -G --data-urlencode text@- sed -i "/$HOST/d" "$HOST_PORT_STATUS" fi fi ################################## ### CHECK PORT STATUS - for DOWN # ################################## if [ $CHKPORT -eq 1 ]; then echo "$HOST_PORT_DOWN_ALERTONSCREEN" if [ $(grep -c "$HOST" "$HOST_PORT_STATUS") -eq 1 ]; then echo "Host PORT DOWN SMS have already been sent to $CELL1 ... " fi if [ $(grep -c "$HOST" "$HOST_PORT_STATUS") -eq 0 ]; then echo "SENDING HOST PORT DOWN SMS ..." echo "$HOST" > $HOST_PORT_STATUS echo "SMS Sent FOR PORT DOWN DONE only 1 time until next status change ..." # Sending PORT DOWN ALERT via EMAIL $SENDMAIL -u "$HOST_PORT_DOWN_ALERTONSCREEN" -o tls=yes -s smtp.gmail.com:587 -t $ADMINMAIL1 -xu $GMAILID -xp $GMAILPASS -f $GMAILID -o message-file=$MSGDOWNHOLDER_PORT -o message-content-type=text # Sending PORT UP ALERT via SMS cat $MSGDOWNHOLDER_PORT | curl "http://$KANNELURL/cgi-bin/sendsms?username=$KANNELID&password=$KANNELPASS&to=$CELL1" -G --data-urlencode text@- fi fi #################### # SCRIPT ENDS HERE # # SYED JAHANZAIB # ####################
Usage:
change the IP and port number.
- /temp/portmon.sh 192.168.20.1 1235
You can add entry in cron like this
# Check for Service remote host port status */5 * * * * /temp/portmon.sh 192.168.20.1 1235
RESULT:
If ping not respond …
If port not respond …
Email Alerts:
SMS Alerts:
Filed under: Linux Related