Syed Jahanzaib Personal Blog to Share Knowledge !

August 24, 2016

Radius Manager Connection Tracking System for Mikrotik

Filed under: Mikrotik Related, Radius Manager — Tags: , , — Syed Jahanzaib / Pinochio~:) @ 10:35 AM

trackme

As requested by few OP, following is a short technical reference guide on how you can enable TCP/UDP connections in Mikrotik to be stored in Radius manager connection tracking database so that you can view user base connection tracking report. In my personal view, it’s not much useful and at low end hardware it creates bottleneck, but if you have good resources with some fast storage (like SSD or RAID-10) it is better to set it up.

CTS is best described in the manual by dmasoftlab manual as mentioned below

Radius Manager has a special feature: the Connection Tracking System. It is available only in Radius Manager CTS version or higher. With the help of it the system can track and log all the TCP and UDP connections for all registered (online) users.

By default when You install the CTS enabled version of Radius Manager, it will use the default CTS database (CONNTRACK). It is strongly recommended to use a separate database host for the CONNTRACK database, due to the enormous amount of data stored daily. It can be even a 100-500 MegaBytes (and in my personal experience it can grow as much as 3-5 GB on busy network, ZAIB) per day. Fast disks (like SSD in RAID 10 mode,  zaib) are also recommended to be able to seek and store the data in real time. Radius Manager periodically stores the traffic data to CONNTRACK database (typically in
every 5–60 seconds).


Mikrotik (6.x) Configuration to enable Firewall Logging to remote server (RM)

If you have already configured the radius manager, then the conntrack database also get configured via the installation script. Next step is to enable the firewall logging in the mikrotik router so that mikrotik can send the categorized TCP/UDP data to radius manager conntrack database.

In mikrotik, open terminal and issue following commands …

In below example, we have following IP scheme.

PPPoE users ip pool = 172.16.0.1-172.16.0.255
Radius Manager IP  = 101.11.11.254

/ip firewall filter add chain=forward src-address=172.16.0.1-172.16.0.255 protocol=tcp connection-state=new action=log

/ip firewall filter add chain=forward src-address=172.16.0.1-172.16.0.255 protocol=udp connection-state=new action=log

/system logging action add name=rmctszaib remote=101.11.11.254 target=remote remote-port=4950

/system logging add topics=firewall action=rmctszaib

If you don’t see any errors , you are good to Go.


RADIUS MANAGER SECTION:

1. Radius Manager should be licensed with CTS level. Basic license wont gonna work ..

2. Make sure that  RMCONNTRACK service is UP and running.To make sure it’s running , get its process by

ps aux |grep rmconntrack

and you should get result something like following

root@radius:/# ps aux |grep rmconntrack
root xxxx 0.0 0.0 xxxxxx xxx ? Ssl 13:22 0:00 /usr/local/bin/rmconntrack

if you see its running, proceed further , otherwise you may start it manually by

rmconntrack –x

Now login to RM ACP  >  Reports  >  Connection Report

Here you can get report for all or individual user.

As showed in the image below …

c1

 

c2

 


Regard’s
Syed Jahanzaib

 

 

1

August 8, 2016

UPS Monitor BASH Script with Multi Matching ‘IF Statements’

Filed under: Linux Related — Tags: , , — Syed Jahanzaib / Pinochio~:) @ 1:26 PM

Following script is just For my personal reference Purpose only!!!

 

script ups output

 

Emerson UPS Info via DUDE

kesc

 


Scenario:

In our datacenter , we have Emerson 10kva UPS x 2 in failover + load balancing mode. Yesterday we got some electric failure resulting in UPS shutdown after its batteries banks got drained. This UPS doesn’t have advance alerting system other than simple traditional alerts of light failure and restore via email. Therefore I made some customized scripts which are now monitoring the UPS status with more deeper digging.

Example: If the Electricity failure occurs (or low voltage under xxx value) AND the remaining backup time goes below our defined threshold, then it should send email + sms alert to the admin. So basically we will match at least two conditions.



#!/bin/bash
# Function: DATA CENTER UPS KE INPUT and VOLTAGE ALERT SCRIPT
# Scheduled Script to check data center KE INPUt + UPS Backup remain time via SNMP after every 1 mnt.
# If it found lower than our defined limit, send sms or email Alerts, but do not repeat it until next status change.
# Script Designed by Syed Jahanzaib
# aacable at hotmail dot com
# http://aacable . wordpress . com
# Created : 8th-Aug-2016  @ AGP
# set -x

# Colors Config . . . [[ JZ . . . ]]
ESC_SEQ="\x1b["
COL_RESET=$ESC_SEQ"39;49;00m"
COL_RED=$ESC_SEQ"31;01m"
COL_GREEN=$ESC_SEQ"32;01m"
DATE=`date`

# COMPANY NAME
COMPANY="ZABBO Pvt Ltd."
FOOTER="Powered by Syed Jahanzaib / AGP Pvt. Ltd."

# Hostname
HOSTNAME=`hostname`

# KANNEL SMS Gateway Info
KANNELURL="KANNEL-URL:13013"
KANNELID="kannel"
KANNELPASS="KANNEL-PASS"
CELL1="03333021909"

# GMAIL DETAILS
GMAILID="YOURMGAILID@gmail.com"
GMAILPASS="GMAILPASS"
ADMINMAIL1="aacable@hotmail.com"
SENDMAILAPP="/temp/sendEmail-v1.56/sendEmail"
STATUS_HOLDER="/tmp/upsmon.txt"

# UPS IP / SNMP RELATED INFO
UPSIP="$1"
SNMPSTR="public"
# OID For Emerson rack base UPS 10kvs with snmp web card
UPSINPUTOID="1.3.6.1.4.1.13400.2.16.2.2.1.0"
UPSTIMEOID="1.3.6.1.4.1.13400.2.16.2.5.2.0"

# Minimum Backup time threshold limit, below this action can be triggered if matched with second trigger
UPSTIMELIMIT="30"
# Minimum KE INput electric voltages limit, below this , both condition can be matched.
KELIMIT="190"

# Check if UPS IP is accessible or not, if not then EXIT immediately with error / zaib
if [[ $(ping -q -c 1 $UPSIP) == @(*100% packet loss*) ]]; then
echo "ALERT ..... UPS IP $UPSIP is DOWN ..."
exit 1
fi

# Check if SNMP query is accessible or not, if not then EXIT immediately
SNMPRESULT="/tmp/snmpoutput.txt"
touch $SNMPRESULT
snmpwalk -v1 -c agp $1 $UPSINPUTOID > $SNMPRESULT
if [ ! -s $SNMPRESULT ]; then
echo "ALERT ALERT: UPS IP $UPSIP - SNMP NOT Responding"
exit 1
fi

# If temporary status holder is not present , then create it,
# forumla is being applied to prevent repeated attempt of file creation / zaib
if [ ! -f $STATUS_HOLDER ]; then
echo -e "Creating Status Holder for first time usage"
touch $STATUS_HOLDER
fi

# Check for KE INPUT via SNMP query, make sure to chhange it accordingly
# currently i am using emerson UPS with snmp web card, as example
TIMEQ=`snmpwalk -v2c -Oqv -c $SNMPSTR $UPSIP $UPSTIMEOID`
KEINPUT=`snmpwalk -v2c -Oqv -c $SNMPSTR $UPSIP $UPSINPUTOID`
# divide time formula which comes like 2100 , so divide with /100 so we get 21 actually, just an example
KE=$(($KEINPUT / 100))

# Message template for up n down alerts
LOWMSG="$COMPANY Data Center UPS K.E Input have failed.
UPS IP = $UPSIP
Current K.E Input = $KE Volts
Remaining backup time = $TIMEQ mnts
Please check urgent"

OKMSG="$COMPANY Data Center UPS KESC Input have restored.
UPS IP = $UPSIP
Current K.E Input = $KE Volts
Remaining backup time = $TIMEQ mnts"

NORMSG="All Seems OK !"
UPMSG="/tmp/upmsg.sms"
DOWNMSG="/tmp/downmsg.sms"

# SMS and email msg fromat for up n down
MSG_UP="$COMPANY Alert:

$OKMSG @ $DATE
$FOOTER"

LOWMSG="$COMPANY Alert:

$LOWMSG @ $DATE
$FOOTER"

# Print Current fetched Values
echo -e "
UPS IP / PING response = $UPSIP / Ping Responding OK
UPS SNMP Response = OK / SNMP query Accessible
Minimum Time Threshold = $COL_GREEN $UPSTIMELIMIT mnts $COL_RESET
Minimum KE Input Threshold = $COL_GREEN $KELIMIT volts $COL_RESET
Current Backup Time = $COL_RED $TIMEQ mnts $COL_RESET
Current K.E Input = $COL_RED $KE volts $COL_RESET"

# Check if ke failure alert have already been sent, if yes then inform accordingly.
if [[ "$TIMEQ" -lt "$UPSTIMELIMIT" ]] && [[ "$KE" -lt "KELIMIT" ]]; then
echo -e "$COL_RED $LOWMSG $COL_RESET"
if [ $(grep -c "TEMP" "$STATUS_HOLDER") -eq 1 ]; then
echo -e "$COL_RED SMS/Email for DOWN have already been sent $COL_RESET"
fi
fi

# Matching Formula starts here .. zaib
# IF KE INPUT result is less than our defined limit and remaining time is low as well, , 
# then send sms and email, IF NOT ALREADY SENT
if [[ "$TIMEQ" -lt "$UPSTIMELIMIT" ]] && [[ "$KE" -lt "KELIMIT" ]]; then
if [ $(grep -c "TEMP" "$STATUS_HOLDER") -eq 0 ]; then
echo -e "$COL_RED ALERT: $LOWMS $(date) / SENDING SMS/Email .... $COL_RESET"
echo "$LOWMSG" > $DOWNMSG

# Sending DOWN SMS via KANNEL
cat $DOWNMSG | curl "http://$KANNELURL/cgi-bin/sendsms?username=$KANNELID&password=$KANNELPASS&to=$CELL1" -G --data-urlencode text@-

# Sending Email via sendEmail tool app using GMAIL
$SENDMAILAPP -u "$LOWMSG @ $DATE" -o tls=yes -s smtp.gmail.com:587 -t $ADMINMAIL1 -xu $GMAILID -xp $GMAILPASS -f $GMAILID -o message-file=$DOWNMSG -o message-content-type=text

echo "TEMP" > $STATUS_HOLDER
fi
else
echo -e "$COL_GREEN $NORMSG ... $COL_RESET"
if [ $(grep -c "TEMP" "$STATUS_HOLDER") -eq 1 ]; then
echo -e "$COL_GREEN $COMPANY ALERT : $OKMSG $(date) / SENDING OK SMS/Email .... $COL_RESET"
echo "$OKMSG" > $UPMSG

# Sending UP SMS via KANNEL
cat $UPMSG | curl "http://$KANNELURL/cgi-bin/sendsms?username=$KANNELID&password=$KANNELPASS&to=$CELL1" -G --data-urlencode text@-

# Sending UP / OK Email via sendEmail tool app using GMAIL
$SENDMAILAPP -u "$OKMSG @ $DATE" -o tls=yes -s smtp.gmail.com:587 -t $ADMINMAIL1 -xu $GMAILID -xp $GMAILPASS -f $GMAILID -o message-file=$UPMSG -o message-content-type=text
sed -i "/TEMP/d" "$STATUS_HOLDER"
fi
fi

# UPS Monitor Script Ends Here
# Syed Jahanzaib / aacable @ hotmail . com
# http:// aacable . wordpress . com


 

failure

 


Second Version / uPDATED 16-8-2016  12:05pm


#!/bin/bash
#set -x

# Function: DATA CENTER UPS KE INPUT and VOLTAGE ALERT SCRIPT
# Scheduled Script to check data center KE INPUt + UPS Bacnkup remain time via SNMP after every 1 mnt.
# If it found high or above our defined limit, send sms or email Alerts, but donot repeat it untill next status change.
# Script Designed by Syed Jahanzaib
# aacable at hotmail dot com
# http://aacable . wordpress . com
# Colors Config . . . [[ JZ . . . ]]

ESC_SEQ="\x1b["
COL_RESET=$ESC_SEQ"39;49;00m"
COL_RED=$ESC_SEQ"31;01m"
COL_GREEN=$ESC_SEQ"32;01m"
DATE=`date`

# COMPANY NAME
COMPANY="*AGP*"
FOOTER="Powered by Syed Jahanzaib / AGP Pvt. Ltd."
TEMPWORD="UPSMON"

# Hostname
HOSTNAME=`hostname`

# KANNEL SMS Gateway Info
KANNELURL="127.0.0.1:13013"
KANNELID="KANNEL-USER"
KANNELPASS="KANNEL-PASS"
CELL1="03333XXXXXX"

# GMAIL DETAILS
GMAILID="YOURGMAIL@gmail.com"
GMAILPASS="GMAILPASS"
ADMINMAIL1="TO-1@hotmail.com"
SENDMAILAPP="/temp/sendEmail-v1.56/sendEmail"
STATUS_HOLDER="/tmp/upsmon.txt"

# UPS RELATED INFO
UPSIP="$1"
SNMPSTR="public"
UPSINPUTOID="1.3.6.1.4.1.13400.2.16.2.2.1.0"
UPSTIMEOID="1.3.6.1.4.1.13400.2.16.2.5.2.0"
UPSTIMELIMIT="10"
KELIMIT="180"

# Check if UPS is accessibel or not, if not then EXIT immediately with error / zaib
if [[ $(ping -q -c 1 $UPSIP) == @(*100% packet loss*) ]]; then
echo "ALERT ..... UPS IP $UPSIP is DOWN ..."
exit 1
fi
SNMPRESULT="/tmp/snmpoutput.txt"
touch $SNMPRESULT
snmpwalk -v1 -c agp $1 $UPSINPUTOID > $SNMPRESULT
if [ ! -s $SNMPRESULT ]; then
echo "ALERT ALERT: UPS IP $UPSIP - SNMP NOT Repsonding"
exit 1
fi

# If temporary status holder is not present , then create it,
# forumla is being applied to prevent repeated attempt of file creation / zaib
if [ ! -f $STATUS_HOLDER ]; then
echo -e "Creating Status Holder for first time usage"
touch $STATUS_HOLDER
fi

# Check for KE INPUT via SNMP query, make sure to chhange it accordingly
# currently i am using emerson UPS with snmp web card, as example
TIMEQ=`snmpwalk -v2c -Oqv -c $SNMPSTR $UPSIP $UPSTIMEOID`
KEINPUT=`snmpwalk -v2c -Oqv -c $SNMPSTR $UPSIP $UPSINPUTOID`
KE=$(($KEINPUT / 100))

# divide time formula which comes like 2100 , so divide with /100 so we get 21 actuall, just an example
LOWMSG="UPSMON - $COMPANY UPS CRITICAL ALERT: Data Center UPS working on batteries @$DATE & remainging timings are $TIMEQ mnts.

UPS IP / PING response = $UPSIP / Ping Responding OK
UPS SNMP Response = OK / SNMP query Accessible
Minimum Time Threshold = $UPSTIMELIMIT mnts
Minimum KE Input Threshold = $KELIMIT volts
Current Backup Time = $TIMEQ mnts
Current K.E Input = $KE volts
###############################################################
FINAL RESULT = ALERT / K.E failed. UPS working on Batteries. #
###############################################################

$FOOTER"

OKMSG="UPSMON - $COMPANY Data Center UPS K.E Input have restored @ $DATE

UPS IP / PING response = $UPSIP / Ping Responding OK
UPS SNMP Response = OK / SNMP query Accessible
Minimum Time Threshold = $UPSTIMELIMIT mnts
Minimum KE Input Threshold = $KELIMIT volts
Current Backup Time = $TIMEQ mnts
Current K.E Input = $KE volts

#################################################################
FINAL RESULT = OK now / K.E restored. UPS working on K.E Input #
#################################################################

$FOOTER"

NORMSG="All Seems OK @ $DATE !"
UPMSG="/tmp/upmsg.msg"
DOWNMSG="/tmp/downmsg.msg"

UPMSG_SMS="/tmp/upmsg.sms"
DOWNMSG_SMS="/tmp/downmsg.sms"
# SMS and email msg fromat for up n down
MSG_UP_SUB="UPSMON - $COMPANY UPS Info: Data Center UPS K.E restored @ $DATE"
MSG_DOWN_SUB="UPSMON - $COMPANY UPS CRITICAL Alert: UPS working on batteries @$DATE & remainging timings are $TIMEQ mnts."
# Print Ok or ALERT Values according to the following formula
if [[ "$TIMEQ" -lt "$UPSTIMELIMIT" ]] && [[ "$KE" -lt "$KELIMIT" ]]; then
# PRINT FAILURE MESSAGE BY ZAIB ---------
echo "
######################################################################
UPSMON - Script to test KE. Input and battery remaining time @$DATE. #
######################################################################

UPS IP / PING response = $UPSIP / Ping Responding OK
UPS SNMP Response = OK / SNMP query Accessible
Minimum Time Threshold = $UPSTIMELIMIT mnts
Minimum KE Input Threshold = $KELIMIT volts
Current Backup Time = $TIMEQ mnts
Current K.E Input = $KE volts

###############################################################
FINAL RESULT = ALERT / K.E failed. UPS working on Batteries. #
###############################################################

$FOOTER"
else
echo "
######################################################################
UPSMON - Script to test KE. Input and battery remaining time @$DATE. #
######################################################################

UPS IP / PING response = $UPSIP / Ping Responding OK
UPS SNMP Response = OK / SNMP query Accessible
Minimum Time Threshold = $UPSTIMELIMIT mnts
Minimum KE Input Threshold = $KELIMIT volts
Current Backup Time = $TIMEQ mnts
Current K.E Input = $KE volts

#########################
FINAL RESULT = ALL OK. #
#########################

$FOOTER"
fi

# Matching Formula starts here .. zaib
# IF KE INPUT result is greater the our defined limit, then give alert
if [[ "$TIMEQ" -lt "$UPSTIMELIMIT" ]] && [[ "$KE" -lt "$KELIMIT" ]]; then
echo ""
if [ $(grep -c "$TEMPWORD" "$STATUS_HOLDER") -eq 1 ]; then
echo "SMS/Email for DOWN have already been sent"
fi
fi

# IF KE INPUT result is less then our defined limit, then send sms and email, IF NOT ALREAY SENT
if [[ "$TIMEQ" -lt "$UPSTIMELIMIT" ]] && [[ "$KE" -lt "$KELIMIT" ]]; then
if [ $(grep -c "$TEMPWORD" "$STATUS_HOLDER") -eq 0 ]; then
echo "
$MSG_DOWN_SUB / SENDING SMS/Email ....
"
echo "$LOWMSG" > $DOWNMSG
echo "$MSG_DOWN_SUB" > $DOWNMSG_SMS

# Sending DOWN SMS via KANNEL
cat $DOWNMSG_SMS | curl "http://$KANNELURL/cgi-bin/sendsms?username=$KANNELID&password=$KANNELPASS&to=$CELL1" -G --data-urlencode text@-

# Sending Email via sendEmail tool app using GMAIL
$SENDMAILAPP -u "$MSG_DOWN_SUB" -o tls=yes -s smtp.gmail.com:587 -t $ADMINMAIL1 -xu $GMAILID -xp $GMAILPASS -f $GMAILID -o message-file=$DOWNMSG -o message-content-type=text

echo "$TEMPWORD" > $STATUS_HOLDER
fi
else
# echo -e "$NORMSG ..."
echo -e ""
if [ $(grep -c "$TEMPWORD" "$STATUS_HOLDER") -eq 1 ]; then
echo -e "
$MSG_UP_SUB / SENDING OK SMS/Email ....
"
echo "$OKMSG" > $UPMSG
echo "$MSG_UP_SUB" > $UPMSG_SMS
# Sending UP SMS via KANNEL
cat $UPMSG_SMS | curl "http://$KANNELURL/cgi-bin/sendsms?username=$KANNELID&password=$KANNELPASS&to=$CELL1" -G --data-urlencode text@-

# Sending Email via sendEmail tool app using GMAIL
$SENDMAILAPP -u "$MSG_UP_SUB" -o tls=yes -s smtp.gmail.com:587 -t $ADMINMAIL1 -xu $GMAILID -xp $GMAILPASS -f $GMAILID -o message-file=$UPMSG -o message-content-type=text
fi
fi

# UPSMON.SH Script Ends Here
# Syed Jahanzaib / aacable @ hotmail . com
# http:// aacable . wordpress . com


 

Regard’s
Syed Jahanzaib

 

August 5, 2016

BASH script to monitor Remote Host Port with Ping

Filed under: Linux Related — Tags: , , , , , — Syed Jahanzaib / Pinochio~:) @ 10:16 AM

portmonitor

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 …

1- ping

 

If port not respond …

2- port

 

Email Alerts:

3- mail alert

 

SMS Alerts:

 

port alert1 (1)

 

port alert1 (2)

 


 

%d bloggers like this: