LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 12-03-2010, 01:34 PM   #1
raggmopp1961
LQ Newbie
 
Registered: Sep 2006
Distribution: RehHat, Fedora Core
Posts: 12

Rep: Reputation: 0
need help to modify output of shell script


Hi all:

I'm running RHEL5.5 and nagios
3.2.0. The real question deals with how to change the printed output so nagios will work with it.

I have made a script that will calculate network
throughput on interfaces. The script is going through and finding all interfaces (eth, bond, lan) and doing the math to calculate throughput.

The script;
#!/bin/bash

PATH=/bin:/usr/bin:/sbin:/usr/sbin:/etc
export PATH

#set -xv

declare -a int
i=0
j=0

/bin/netstat -i | awk '/eth/ || /bond/ || /lan/' | awk '{print $1, $4, $5, $6, $8, $9, $10}' > /tmp/traf_cnt1
sleep 5
/bin/netstat -i | awk '/eth/ || /bond/ || /lan/' | awk '{print $1, $4, $5, $6, $8, $9, $10}' > /tmp/traf_cnt2

for INT in `cat /tmp/traf_cnt1| awk '/^eth/ || /^bond/ || /lan/' | awk '{print $1}'`
do
int[i]=$INT
i=$((i+1))
done

while [ $j -lt $i ]
do
set `cat /tmp/traf_cnt1| grep ${int[$j]}`
INT=$1
RXOK1=$2
RXERR1=$3
RXDRP1=$4
TXOK1=$5
TXERR1=$6
TXDRP1=$7

set `cat /tmp/traf_cnt2 | grep ${int[$j]}`
INT=$1
RXOK2=$2
RXERR2=$3
RXDRP2=$4
TXOK2=$5
TXERR2=$6 #echo "${int[$j]}"

RXOK3=`expr ${RXOK2} - ${RXOK1}`
RXOK=`expr ${RXOK3} \* 12`
TXOK3=`expr ${TXOK2} - ${TXOK1}`
TXOK=`expr ${TXOK3} \* 12`

echo "${int[$j]} Packets receive=$RXOK, transmit=$TXOK: Error receive=$RXERR2, transmit=$TXERR2: Dropped receive=$RXDRP2, transmit=$TXDRP2 | $RXOK;;;; $TXOK;;;; $RXERR2;;;; $TXERR2;;;; $RXDRP2;;;; $TXDRP2;;;; "

j=$((j+1))

done


If I run from command line, all looks good. See below
bond0 Packets receive=276, transmit=144: Error receive=0, transmit=0: Dropped receive=0, transmit=0 | 276;;;; 144;;;; 0;;;; 0;;;; 0;;;; 0;;;;
eth0 Packets receive=252, transmit=144: Error receive=0, transmit=0: Dropped receive=0, transmit=0 | 252;;;; 144;;;; 0;;;; 0;;;; 0;;;; 0;;;;
eth1 Packets receive=24, transmit=0: Error receive=0, transmit=0: Dropped receive=0, transmit=0 | 24;;;; 0;;;; 0;;;; 0;;;; 0;;;; 0;;;;

But I need this to work within nagios.

I need the print output to look like;
bond0 Packets receive=276, transmit=144: Error receive=0, transmit=0: Dropped receive=0, transmit=0 eth0 Packets receive=252, transmit=144: Error receive=0, transmit=0: Dropped receive=0, transmit=0 eth1 Packets receive=24, transmit=0: Error receive=0, transmit=0: Dropped receive=0, transmit=0 | bond0=276;;;; 144;;;; 0;;;; 0;;;; 0;;;; eth0=252;;;; 144;;;; 0;;;; 0;;;; 0;;;; eth1=24;;;; 0;;;; 0;;;; 0;;;; 0;;;; 0;;;;

The output is mainly for nagios to report and trend the values. As nagios wants to see nothing but perf data after the '|' character, I somehow need to have only one '|' character for all of the output.

Many thanks!
 
Old 12-03-2010, 02:07 PM   #2
thesnow
Member
 
Registered: Nov 2010
Location: Minneapolis, MN
Distribution: Ubuntu, Red Hat, Mint
Posts: 126

Rep: Reputation: 28
You can modify the last few lines to something like this:

Code:
echo "${int[$j]} Packets receive=$RXOK, transmit=$TXOK: Error receive=$RXERR2, transmit=$TXERR2: Dropped receive=$RXDRP2, transmit=$TXDRP2"
var1="$var1 ${int[$j]}=$RXOK;;;; $TXOK;;;; $RXERR2;;;; $TXERR2;;;; $RXDRP2;;;; $TXDRP2;;;; "

j=$((j+1))

done

echo "| $var1"
If you need it all on one line, then it would need to be a bit different.
 
1 members found this post helpful.
Old 12-04-2010, 03:26 AM   #3
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,689

Rep: Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987
I am not quite sure I see the issue?? You have all the data you need to be displayed you just need to change how you deliver it.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Using shell command output as input in shell script - how to do? EnderX Linux - Newbie 2 06-30-2010 01:46 PM
how to modify a line of a file using shell script? shayori Linux - Newbie 9 04-16-2010 03:44 AM
bash Shell script to modify DNS zone file KinnowGrower Programming 4 12-12-2009 09:07 PM
shell script to batch process-modify ip numbers in files. SupermanInNY Programming 5 08-13-2009 04:03 AM
modify content in a file using shell script fjkum Linux - Newbie 2 08-03-2006 05:46 AM


All times are GMT -5. The time now is 06:01 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration