Netstat -i notification


 
Thread Tools Search this Thread
Operating Systems Linux Netstat -i notification
# 1  
Old 09-20-2012
Netstat -i notification

I'm a bit new to scripting, but have been working on some scripts. I create a script to monitor the network interface for netstat -i, but not sure if I am getting what I need...I'd like to grab the entire column for RX-DRP and inform me of which interface is giving the RX-DRP errors...here is a sample of my script.

Code:
#!/bin/bash
#----------------------------------------------------------------------------
# Check previous netstat output against current netstat output to determine
# if there are packet receive errors.  We have to do this because the counters
# are only cleared when the server is rebooted.
#----------------------------------------------------------------------------

NOW=`date`
echo "$NOW"
emailaddress=emailadr@yoursite.com
logfile=/tmp/1.out


ARGCOUNT=1

if [ $# -ne "$ARGCOUNT" ]
then
  echo "Usage: `basename $0` <$email-address>"
  exit 1
fi


#Check if previous Netstat -i output file exist

if [ -s $logfile ]
then

    OLDNETSTAT=`netstat -i | awk "{print $6}"  | cut -f 15,19,20 -d ' '`
    netstat -i |grep "RX-DRP" | awk "{print $6}" > $logfile  #take a new netstat snapshot
    NOWNETSTAT=`netstat -i | awk "{print $6}"  | cut -f 15,19,20 -d ' '`

    echo "OLDNETSTAT=$OLDNETSTAT"
    echo "NOWNETSTAT=$NOWNETSTAT"

    if [ $OLDNETSTAT -lt $NOWNETSTAT ]
    then
        NUMDROPS=$(( NOWNETSTAT - OLDNETSTAT ))
        servername=`hostname -s`
        MYMESSAGE="WARNING: netstat -i: $NUMDROPS new "RX-DRP" error(s) on $servername"
        mail -s "$MYMESSAGE" $1 < /dev/null
    fi

else
   echo "Running command: netstat -i | awk "{print $6}"  | cut -f 15,19,20 -d ' ' > $logfile"

   netstat -i | awk "{print $6}"  | cut -f 15,19,20 -d ' ' > $logfile
fi

any help would be great...

Thanks,
Moderator's Comments:
Mod Comment Please view this code tag video for how to use code tags when posting code and data.

Last edited by Corona688; 09-20-2012 at 12:15 PM..
# 2  
Old 09-20-2012
You're not sure? What does it do right now?

What does your netstat -i output look like?
# 3  
Old 09-20-2012
Corona,

This is what my output looks like for it.

Code:
Kernel Interface table
Iface       MTU Met    RX-OK RX-ERR RX-DRP RX-OVR    TX-OK TX-ERR TX-DRP TX-OVR Flg
eth0       1500   0 4812332037      0 74613075      0    70140      0      0      0 BMRU
eth0.382   1500   0    15759      0      0      0     2268      0      0      0 BMRU
eth0.383   1500   0 51656441      0      0      0     1757      0      0      0 BMRU
eth0.384   1500   0 173975608      0      0      0    33158      0      0      0 BMRU
eth0.385   1500   0 1305446752      0      0      0    32367      0      0      0 BMRU
eth1       1500   0 4812334815      0 56291912      0    41865      0      0      0 BMRU
eth1.382   1500   0 565902390      0      0      0     2284      0      0      0 BMRU
eth1.383   1500   0 146491897      0      0      0    17160      0      0      0 BMRU
eth1.384   1500   0 66451563      0      0      0     1453      0      0      0 BMRU
eth1.385   1500   0 473344908      0      0      0    20428      0      0      0 BMRU
eth2       1500   0 4631003384      0 63203535      0    47655      0      0      0 BMRU
eth2.382   1500   0    51035      0      0      0     1372      0      0      0 BMRU
eth2.383   1500   0 584990978      0      0      0    24590      0      0      0 BMRU
eth2.384   1500   0 143301795      0      0      0    15555      0      0      0 BMRU
eth2.385   1500   0 363999708      0      0      0     5816      0      0      0 BMRU
eth3       1500   0 4796688112      0 56788578      0 16095025      0      0      0 BMRU
eth3.380   1500   0      137      0      0      0       66      0      0      0 BMRU
eth3.381   1500   0    74326      0      0      0 10328387      0      0      0 BMRU
eth4       1500   0   849542      0      0      0  3199486      0      0      0 BMRU
lo        16436   0   274266      0      0      0   274266      0      0      0 LRU

---------- Post updated at 11:33 AM ---------- Previous update was at 11:31 AM ----------

I'm trying to get the RX-DRP instance for drops and send me an email. I created one for netstat -su and it works fine and this is my script for the netstat -su.

Code:
#!/bin/bash
#----------------------------------------------------------------------------
# Check previous netstat output against current netstat output to determine
# if there are packet receive errors.  We have to do this because the counters
# are only cleared when the server is rebooted.
#----------------------------------------------------------------------------

NOW=`date`
echo "$NOW"
emailaddress=emailaddr@yoursite.com
logfile=/tmp/netstat-su.out


ARGCOUNT=1

if [ $# -ne "$ARGCOUNT" ]
then
  echo "Usage: `basename $0` <$email-address>"
  exit 1
fi


#Check if previous netstat output file exist

if [ -s $logfile ]
then

    OLDNETSTAT=`grep "packet receive errors" $logfile | cut -f 5 -d ' '`
    netstat -us > $logfile  #take a new netstat snapshot
    NOWNETSTAT=`cat $logfile | grep "packet receive errors"       | cut -f 5 -d ' '`

    echo "OLDNETSTAT=$OLDNETSTAT"
    echo "NOWNETSTAT=$NOWNETSTAT"

    if [ $OLDNETSTAT -lt $NOWNETSTAT ]
    then
        NUMDROPS=$(( NOWNETSTAT - OLDNETSTAT ))
        servername=`hostname -s`
        MYMESSAGE="WARNING: netstat -su: $NUMDROPS new packet receive error(s) on $servername"
        mail -s "$MYMESSAGE" $1 < /dev/null
    fi

else
   echo "Running command: netstat -us > $logfile"

   netstat -us > $logfile
fi

Which also gives me the number of drop counts as well.

Last edited by Corona688; 09-20-2012 at 01:19 PM..
# 4  
Old 09-20-2012
You've got some useless use of cat in there.

I wouldn't bother processing the logfile except when you use it. You can do so in one step.

I might do this:

Code:
netstat -us > newlogfile

[ -f oldlogfile ] && awk '(NR==FNR) && $6 { A[$1]=$6; next } # Store old log file errors in array A
        $1 in A { print $1, $6-A[$1] } # If the interface is found in A, print interface-name, difference of errors
        ' oldlogfile newlogfile

mv newlogfile oldlogfile # Replace logfile


Last edited by Corona688; 09-20-2012 at 01:36 PM..
# 5  
Old 09-20-2012
Where would I place that?
# 6  
Old 09-20-2012
It'd replace that whole big if-else.

But try it in your shell first, see what it does.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

netstat output

Hi Team, Below is the output of netstat -an | grep 1533 tcp 0 0 17.18.18.12:583 10.3.2.0:1533 ESTABLISHED tcp 0 0 17.18.18.12:370 10.3.2.0:1533 ESTABLISHED Below is the o/p of netstat -a | grep server_name tcp 0 ... (4 Replies)
Discussion started by: Girish19
4 Replies

2. OS X (Apple)

netstat

When running netstat -i from the Command Terminal, It returns with 21 different connections.. The addresses all look like this: ::1 fe80:1::1 10:dd:b1:a5:c4:ba with Network names like Linke#2 fe80::8e2d How can I delve deeper into this to clarify what is going on with my network?... (0 Replies)
Discussion started by: dwfiedler
0 Replies

3. Red Hat

netstat

Hi Can any body tell me about TIME_WAIT status meaning in the following command output. # netstat -anp|grep 5000 tcp 0 0 127.0.0.1:50006 0.0.0.0:* LISTEN 5058/ccsd tcp 0 0 0.0.0.0:50008 0.0.0.0:* ... (3 Replies)
Discussion started by: mastansaheb
3 Replies

4. UNIX for Dummies Questions & Answers

Need help with a netstat command

Do I have this command correct to show all current connections/sessions my Solaris box has? It does not seem to do anything. netstat -an | grep EST (6 Replies)
Discussion started by: SIFT3R
6 Replies

5. UNIX for Advanced & Expert Users

Help with netstat

Hi, I want to list the time for how long a secure connections last to my server/blade. i am using netstat command to get the same, but not sure how to get the time for how long connections is being ESTABLISHED. netstat -na | grep 'ESTABLISHED' | grep :443 |awk '{print $4}' | cut -d: -f1 |... (1 Reply)
Discussion started by: Siddheshk
1 Replies

6. Solaris

netstat -- what am i looking at?

Greetings to all, Here is a line of output from my netstat command cbp031.904 wdcprodhome.nfsd 98304 0 49640 0 ESTABLISHED The only thing i recognize is the unix machine "cbp031" but what is .904 and all the other data telling me? Thanks in advance. (3 Replies)
Discussion started by: Harleyrci
3 Replies

7. AIX

netstat -anp

Hi Which options can I use to show program with its open ports on AIX?. Just like netstat on linux does: netstat -anp|grep 25 tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 2112/sendmail: acce Thanks Time to start using code tags. Check your... (2 Replies)
Discussion started by: iga3725
2 Replies

8. UNIX for Dummies Questions & Answers

netstat not working

Hello, One of the solaris machine in our network is giving an error when the netstat command is executed. soloman:/home/db2admin->netstat ip open: Permission denied can't open mib stream: Bad file number But it works with root. I couldnt' get any useful info during my search in google. ... (2 Replies)
Discussion started by: k_pranava
2 Replies

9. IP Networking

netstat

Hi what is the command to see the process name/application name along with the port number, connection status ... netstat is not giving process/application name Is there any way to know which application is holding which port? Thanks in advance (3 Replies)
Discussion started by: axes
3 Replies

10. IP Networking

Netstat

Giving netstat command on the prompt gives commands such as localhost.43592 localhost.35237 32768 0 32768 0 TIME_WAIT localhost.43594 localhost.43595 32768 0 32768 0 TIME_WAIT localhost.43598 localhost.35237 32768 0 32768 0 TIME_WAIT... (6 Replies)
Discussion started by: DPAI
6 Replies
Login or Register to Ask a Question