I've created a simple script that runs nslookup for the configured IPs. It will run 10x (per requirement) to validate if it's pointed to the active IP and then email an alert if it returns inactive.
Script is working perfectly, however, when it alerts, it does it 10x due to the loop. How do I get just the last line if it does not satisfy the condition so that it will only send one email per IP?
Code below:
CDC="456.22."
PDC="123.11"
ACTIVE_DC=$CDC
#ACTIVE_DC=$PDC
#ADD/REMOVE VIRTUAL IPs HERE
vIP="app1.app.com app2.app.com"
[email protected]
subject="[TEST - - CRITICAL WARNING] NETWORK TRAFFIC POINTING TO INACTIVE DATA CENTER"
DATE=$(date ' %Y-%m-%d %H:%M:%S')
echo ""
echo "########################################################################"
echo "#################################START##################################"
for IP in $vIP; do
echo""
app_name=$(nslookup $IP | awk 'NR==4 {print NR,$0}' | cut -c 9-50) #Prints each VIP
echo "$app_name"
for i in {1..10}; do #Runs nslookup 10x for each VIP
current=$(nslookup $IP | awk 'NR==5 {print NR,$0}' | cut -c 12-30)
current2=$(echo "$current" | cut -c 1-6)
echo "$current"
if [[ "$current2" == "$ACTIVE_DC" ]]; then
echo "Traffic is pointing to active DC"
else
echo "ERROR Traffic is pointing to INACTIVE DC"
echo "$DATE - [TEST] CRITICAL - NETWORK TRAFFIC POINTING TO INACTIVE DATA CENTER
CHECK $app_name
TRAFFIC POINTING TO $current
TRAFFIC SHOULD BE POINTING to $ACTIVE_DC ONLY!
" | mailx -s "$subject" -r $MAIL_FROM [email protected]
fi
done
done
CodePudding user response:
Modified script:
#!/bin/bash
CDC="456.22."
PDC="123.11"
ACTIVE_DC=$CDC
#ACTIVE_DC=$PDC
#ADD/REMOVE VIRTUAL IPs HERE
vIP="app1.app.com app2.app.com"
[email protected]
subject="[TEST - - CRITICAL WARNING] NETWORK TRAFFIC POINTING TO INACTIVE DATA CENTER"
DATE=$(date ' %Y-%m-%d %H:%M:%S')
echo ""
echo "########################################################################"
echo "#################################START##################################"
for IP in $vIP; do
sendemail='false'
echo ""
app_name=$(nslookup "$IP" | awk 'NR==4 {print NR,$0}' | cut -c 9-50) #Prints each VIP
echo "$app_name"
for i in {1..10}; do #Runs nslookup 10x for each VIP
current=$(nslookup "$IP" | awk 'NR==5 {print NR,$0}' | cut -c 12-30)
current2=$(echo "$current" | cut -c 1-6)
echo "$current"
if [[ "$current2" == "$ACTIVE_DC" ]]; then
echo "Traffic is pointing to active DC"
else
sendemail='true'
echo "ERROR Traffic is pointing to INACTIVE DC"
break
fi
done
if [[ "$sendemail" == 'true' ]]
then
echo "$DATE - [TEST] CRITICAL - NETWORK TRAFFIC POINTING TO INACTIVE DATA CENTER
CHECK $app_name
TRAFFIC POINTING TO $current
TRAFFIC SHOULD BE POINTING to $ACTIVE_DC ONLY!
" | mailx -s "$subject" -r $MAIL_FROM [email protected]
fi
done
What I added is:
- variable
sendemail
is set to false at the beginning of everyfor
loop on IP addresses. - If there is an error, the email must be sent, so set the
sendemail
variable to true andbreak
out of thefor i ...
loop. - If the
sendemail
is true, send the email. It will now happen only once per IP, since the email sending code is outside the nslookup 10X loop.