Tuesday, April 22, 2008

Monitoring network using scripts

I've been working on some script for this few days to monitor the network connectivity for one of my servers. Technically the server have 2 network interfaces connected to network which is "eth0" & "eth2".

Each of this interface will have a different vlan and pointing to a different gateway:
eth0 -> 192.168.10.254
eth2 -> 192.168.11.254

The main interface used will be "eth0" and there are services which will point through this vlan, in case that the interface or the switch connected to this interface fail, it will need to switch to the other interface which is "eth2".

I've created the script as below:

#!/bin/bash
# Script for system monitoring with ping command
# Editor : Mohammad Al Ghazali Ismail

# Gateway
GW1="192.168.10.254"
GW2="192.168.11.254"

# Email
EMAILADD="someone@somedomain.com"

# Mail Subject
SUBJECT="Network Server Fail"

# Number of ping request
COUNT=5

count=$(ping -c $COUNT $GW1 | grep 'received' | awk -F ',' '{print $2}' | awk '{print $1}')
# If 100% failed
if [ $count -eq 0 ]; then
# 100% failed, check for GW2
count=$(ping -c $COUNT $GW2 | grep 'received' | awk -F ',' '{print $2}' | awk '{print $1}')
if [ $count -eq 0 ]; then
# send notification to user
echo "Unable to resolve ping from gateway $GW1 and $GW2 on $(date)" | mail -s "$SUBJECT" $EMAILADD
else
# start the service on 2nd vlan
start servicename on eth2
fi
else
# check the service on 2nd vlan
check servicename on eth2
if servicename is up
stop servicename on eth2
start servicename on eth0
fi
fi

I save the script on some directory and added it into the crontab which will execute it every 2 minutes:

# crontab -e
*/2 * * * * sh /directory/script.sh

Notes : The italic word need to be replace with the right method of checking, starting and stopping the services.

1 comment:

Normala said...

dah buat script...pulakkks... belajar rerajin ya...