Bash Monitoring Alert Script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash Monitoring Alert Script
# 1  
Old 06-22-2009
Bash Monitoring Alert Script

Done some google search on this, and I can find a ton that just search for a domain, like yahoo.com, google.com, etc...

But I need to take one I found, and modify it to include port number. I have a bunch of apache instances that I manage and it's a pretty consolidated environment, so lots of apache servers using lots of different ports. One domain, like abc.com will point towards 2-4 apache instances on different servers/port number combinations.

Here is the code I found that worked well, but I need to be able to check them by servernameSmilieort rather than domain.com.

Code:
#!/bin/bash
# Simple SHELL script for Linux and UNIX system monitoring with
# ping command
# -------------------------------------------------------------------------
# Copyright (c) 2006 nixCraft project <http://www.cyberciti.biz/fb/>
# This script is licensed under GNU GPL version 2.0 or above
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# -------------------------------------------------------------------------
# Setup email ID below
# See URL for more info:
# http://www.cyberciti.biz/tips/simple-linux-and-unix-system-monitoring-with-ping-command-and-scripts.html
# -------------------------------------------------------------------------

# add ip / hostname separated by while space
HOSTS='cat server_list.txt'

# no ping request
COUNT=1

# email report when
SUBJECT="Ping failed"
EMAILID="test@domain.com"
for myHost in $HOSTS
do
  count=$(ping -c $COUNT $myHost | grep 'received' | awk -F',' '{ print $2 }' | awk '{ print $1 }')
  if [ $count -eq 0 ]; then
    # 100% failed
    echo "Host : $myHost is down (ping failed) at $(date)" | mail -s "$SUBJECT" $EMAILID
  fi
done

Edit: I know you can't use ping to specify a port number, but is there another way to send a packet to a serverSmilieort for the monitoring script?

Last edited by cbo0485; 06-22-2009 at 05:07 PM..
# 2  
Old 06-22-2009
You dont just ping ports. You would have to add a lot of code so it knows what to look for. If it didn't get a certain response then it would mark it as down and email you. Im sure there are scripts out there for monitoring ports.
# 3  
Old 06-22-2009
You may add "-o ConnectTimeout=3" to "ping", 3 is in seconds.
# 4  
Old 06-22-2009
nmap seems to somewhat give me what I need, unfortunately it doesn't just give me a 1 or a 0.
Code:
nmap -p 15000 test1.domain.com

Starting nmap 3.50 ( http://www.insecure.org/nmap/ ) at 2009-06-22 16:05 EDT
Interesting ports on test.domain.com (11.11.11.11):
PORT      STATE  SERVICE
15000/tcp closed unknown

Nmap run completed -- 1 IP address (1 host up) scanned in 0.324 seconds
nmap -p 15000 test.domain.com

Starting nmap 3.50 ( http://www.insecure.org/nmap/ ) at 2009-06-22 16:06 EDT
Interesting ports on test.domain.com (11.11.101.11:
PORT      STATE SERVICE
15000/tcp open  unknown

Nmap run completed -- 1 IP address (1 host up) scanned in 0.310 seconds

Is there any way to get only the open or closed output and then I could use an if statement to say, if closed send e-mail, if open exit.

But I'd need it ran through a loop because we have 10-20 apache instances on one server.
# 5  
Old 06-22-2009
Here is another way to do it...

Code:
TIMEOUT=2
URL="http://somedomain.com:80/index.html"
CHECK=$(wget --timeout=$TIMEOUT --tries=1 -qO - $URL )
if [[ ${#CHECK} > 0 ]]; then
        echo "Server up!"

else
        echo "Server Down!"
fi

change index.html to a file that exists on the webserver.

change 80 to what ever port.

somedomain.com can also be an ipaddress..
# 6  
Old 06-22-2009
I got it to work using netcat. Unfortunately I wasn't thinking ahead, so now I have a script that sets a variable inside itself to about 30 different 5 digit port numbers. I copy/pasted that into a file so I have a file:

cat port_numbers.txt
NNNNN NNNNN NNNNN NNNNN NNNNN NNNNN NNNNN

etc..

I need to use sed/awk to make it like this:

cat port_numbers.txt
NNNNN
NNNNN
NNNNN
etc
.
.
.
# 7  
Old 06-22-2009
Code:
tr ' ' '\n' < port_numbers.txt > port_numbersNEW.txt

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to block first bash script until second bash script script launches web server/site?

I'm new to utilities like socat and netcat and I'm not clear if they will do what I need. I have a "compileDeployStartWebServer.sh" script and a "StartBrowser.sh" script that are started by emacs/elisp at the same time in two different processes. I'm using Cygwin bash on Windows 10. My... (3 Replies)
Discussion started by: siegfried
3 Replies

2. What is on Your Mind?

Script to alert with email after a job got successful in monitoring tool

Hi guyz, Please send me a script which is to be sending an automatic email to particular mail id's when a job get complete with status successful. Please send me your script to my mail id : rehan.csjmu@gmail.com Thanks in Advance. ---------- Post updated at 11:33 AM ----------... (1 Reply)
Discussion started by: Rehan Ahmad
1 Replies

3. Shell Programming and Scripting

Alert for monitoring

Dear All, I am new to unix scripting, i need to develop a alert system which get trigger when my scripts don't find any *.log file to copy from one directory to other directory. In detail my scripts run every day and it create a dummy file and check all those *.log file 1 day old and copy them... (3 Replies)
Discussion started by: guddu_12
3 Replies

4. UNIX for Advanced & Expert Users

ldapsearch in monitoring script without bind password written in script

Hi I do a very simple monitoring of our OpenLDAP (runs in cronjob and generate alerts if unsuccessfull) $ ldapsearch -h hostname.domain -D "cn=monitor_user,ou=People,dc=organisation" -w "password" -b "dc=organisation" -x "(&(cn=monitor_user)(ou=People))" dn | grep -v version dn:... (4 Replies)
Discussion started by: slashdotweenie
4 Replies

5. Infrastructure Monitoring

Alert / Monitoring / Historial Data Open Source

hi guys my boss asked me for a tool to monitor and alert my Linux boxes... and some Win boxes... but not only monitor for high CPU Usage, hign memory usage and so on....he also wants a tool for historical data I mean something like I want to know the CPU Usage and memory usage for the last... (1 Reply)
Discussion started by: karlochacon
1 Replies

6. Linux

Memory monitoring and sending alert mail to users in network using shell script

i m workiing on a shell script which may monitors network memory and send alert to user if it increase a threshold (1 Reply)
Discussion started by: navdeep5673
1 Replies

7. Shell Programming and Scripting

need script bash alert email

Hello I have a server it is running more than 5 people Scripts I want to send a warning to the administrator for the main server All is written in the ssh strain For example, when typing the following command "ls" " rm " and other email sends the administrator for the main server About it... (2 Replies)
Discussion started by: x-zer0
2 Replies

8. Shell Programming and Scripting

Help with Monitoring script

Hi Gurus, Currently I am learning UNIX through online forums and unix blogs. I have the below requirement. I need to write a script to monitor server processes. For example, there are 3 processes currently running on the server.(java, pmrepagent, pmserver). If any of the process goes down,... (2 Replies)
Discussion started by: svajhala
2 Replies

9. Infrastructure Monitoring

Monitoring and Alert System

hi, i serarch monitoring and alert system. when HDD and services are down. Send email and sms alert to me and help desk. but i don't find any program. Can you help me ? Thanks. (1 Reply)
Discussion started by: oulutas
1 Replies

10. Shell Programming and Scripting

Monitoring a file - Basic Bash Question

*This is not homework I am new to UNIX and want to try this Monitoring a file demo* *If this is the wrong forum please move it - im new to the forums* $1 = the file to be monitored $2 = the time for the file to sleep If the file gets changed (using -nt) it will send my username mail saying... (2 Replies)
Discussion started by: Nolan-
2 Replies
Login or Register to Ask a Question