Solved

Perl script help

Posted on 2009-05-13
4
338 Views
Last Modified: 2013-12-26
Hello,
I have a couple of requirements using Perl or Bash scripts. Please look at this command output given in the Code text box:

I want a script which will send me a mail, when anyone of the following incident occur:

1. when the command "netstat -an | grep 1530" fails to get the LISTEN value in its output.
2. In the output of /proc/net/bonding/bond0 file, when the value of "MII Status" is showing as "down".

An example at which the mail has to be sent:

[root@orasmb ~]# netstat -an | grep 1530
[root@orasmb ~]#                                                          <-- No output
[root@orasmb ~]# cat /proc/net/bonding/bond0
Ethernet Channel Bonding Driver: v3.2.4 (January 28, 2008)

Bonding Mode: fault-tolerance (active-backup)
Primary Slave: eth4
Currently Active Slave: eth4
MII Status: up                                            
MII Polling Interval (ms): 100
Up Delay (ms): 0
Down Delay (ms): 0

Slave Interface: eth0
MII Status: down                <-- Showing down, mail has to be sent now.                                
Link Failure Count: 1
Permanent HW addr: 00:15:17:97:b1:d9

Slave Interface: eth4
MII Status: up                                                    
Link Failure Count: 1
Permanent HW addr: 00:1a:64:db:34:58
[root@orasmb ~]#

In addition, in the Subject of the mail, it should contain the hostname of the server.

Thanks,
AR
[root@orasmb ~]# netstat -an | grep 1530
tcp        0      0 10.20.40.50:1530            0.0.0.0:*                           LISTEN                    <-----
tcp        0      0 10.20.40.50:1530            10.20.40.50:32964           TIME_WAIT
tcp        0      0 10.20.40.50:1530            10.20.40.50:32963           TIME_WAIT
tcp        0      0 10.20.40.50:1530            10.20.40.50:32969           TIME_WAIT
tcp        0      0 10.20.40.50:1530            10.20.40.50:32970           TIME_WAIT
 
[root@orasmb ~]# cat /proc/net/bonding/bond0
Ethernet Channel Bonding Driver: v3.2.4 (January 28, 2008)
 
Bonding Mode: fault-tolerance (active-backup)
Primary Slave: eth4
Currently Active Slave: eth4
MII Status: up                                            <-------------
MII Polling Interval (ms): 100
Up Delay (ms): 0
Down Delay (ms): 0
 
Slave Interface: eth0
MII Status: up                                                <-------------
Link Failure Count: 1
Permanent HW addr: 00:15:17:97:b1:d9
 
Slave Interface: eth4
MII Status: up                                                    <-------------
Link Failure Count: 1
Permanent HW addr: 00:1a:64:db:34:58
[root@orasmb ~]#

Open in new window

0
Comment
Question by:rdashokraj
4 Comments
 
LVL 1

Accepted Solution

by:
rchi54 earned 200 total points
ID: 24381184
Save the following in a file, chmod to allow execute privileges, and put in cron to run every minute.

#! /bin/bash

if [[ `netstat -an | grep -c 1530` -eq 0 ]]
then
      echo "Subject: `hostname` no LISTEN on 1530" | /usr/lib/sendmail -F yourname@someplace.com yourname@someplace.com
fi


if [[ `cat /proc/net/bonding/bond0 | grep -c down` -ne 0 ]]
then
      echo "Subject: `hostname` has a DOWN interface" | /usr/lib/sendmail -F yourname@someplace.com yourname@someplace.com
fi
0
 
LVL 40

Assisted Solution

by:omarfarid
omarfarid earned 150 total points
ID: 24381396
try this

netstat -an | grep -w 1530 | grep LISTEN
if [ $? -ne 0 ]
then
      mailx -s "Port 1530 is down " username@domain
fi
grep  "MII Status" /proc/net/bonding/bond0 | tail -1 | grep down
if [ $? -ne 0 ]
then
      mailx -s " MII Status is down " username@domain
fi
0
 
LVL 48

Assisted Solution

by:Tintin
Tintin earned 150 total points
ID: 24381860

#!/bin/bash
netstat -an | grep 1530 | grep -q LISTEN || echo "Port 1530 is not listening" | mail -s "Port 1530 down" some@user
 
grep "MII Status" /proc/net/bonding/bond0 | grep -q down /proc/net/bonding/bond0 && echo "Port down" | mail -s "Port down" some@user

Open in new window

0
 

Author Closing Comment

by:rdashokraj
ID: 31581247
Thankyou all !
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Vb script to unzip a files and rename the files 5 97
Linux Scripting 3 145
AWK: Pytthagoras bp script Part deux 22 33
cscript to activate Windows and Office? 2 23
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question