Help it works but its to SLOW


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help it works but its to SLOW
# 1  
Old 03-26-2008
Help it works but its to SLOW

I am a novice at shell scripting. I have managed to cobble together a script that does exactly what I need it to do. However I am gathering information from 700+ devices. This script takes hours to complete the task. Is there a better way of doing it than what I have listed here? This script processes line by line from the file. Might there be a way to process multiple lines at the same time? Any help would be great.

I would also like to hear feed back about the script itself. How did I do for my first unix script, what would make it better.
I have posted the working script below. (community strings changed of course)

NOTE: Has to be shell script, Perl is on the box but I don't have access to it.

Code:
#

#======================================================================#
#This section tests for the file device.log, and removes if it exists  #
#======================================================================#
if [ device.log ]
then
rm device.log
fi
cat adoa.txt | while read device              
do
#======================================================================#
#This section will pull the ip address from DNS and assign the value   #
#To the variable "ip".  It will then test to see if the variable "ip"  #
#Has a Null (value of no length) or if it contains data. If a value is #
#Found it is stored in the variable "dns" If no data is found in the   #
#variable "ip" it will assign the string value of "Not in DNS"  to the #
#variable "dns"                                                        #
#======================================================================#
#
	ip=`(host $device | awk ' /has/ {print $4}')` 
	if [ "$ip" ]
	then 
		dns="$ip"
#
#		
#
#
#======================================================================#
#This section will Ping the variable "device" and record a yes or no   #
#Answer in the variable "routable". Error messages such as unknown     #
#Host are sent to /dev/null to prevent them from displaying            #
#======================================================================#
#
	answer=`(ping -c 1 -q -w 2 -n  $device 2>/dev/null | grep received | awk '{print $4}')`
	if
        [ $answer = 0 ];
        then answer="NO"
        else answer="YES"
        fi
#
#======================================================================#
#This section will test the read and read/write snmp community strings #
#Yes or No answer is stored in the variable "snmpread" and "snmpwrite" #
#This section also collects the device system name and stores it in    #
#the variable "routername"
#======================================================================#
#
	if
	[ $answer = "YES" ];
        then

	routername=`(snmpget -t 2 -r 0 -Oqv -Os -v 2c -c (Read only community string) $device 2>/dev/null  SNMPv2-MIB::sysName.0)`
        if [ "$routername" = "" ];
        then snmpread="NO"
	routername="N/A"
        else snmpread="YES"
        fi
        snmpwrite=`(snmpget -t 2 -r 0 -Oqv -Os -v 2c -c (Read Write community string) $device 2>/dev/null  SNMPv2-MIB::sysName.0)`
        if [ "$snmpwrite" = "" ];
        then snmpwrite="NO"
        else snmpwrite="YES"
        fi

if [ "$snmpwrite" = "YES" ];
then
devicenumber=`(snmpwalk -v 2c -OQs -c  (Read Write community string) $device  1.3.6.1.2.1.4.20.1.2 | grep $ip | awk ' /=/ {print $3}')`
interface=`(snmpwalk -v 2c -OQs -c  (Read Write community string) $device  1.3.6.1.2.1.2.2.1.2 | grep -w ifDescr.$devicenumber | awk '/=/ {print $3}')`
else
interface="N/A"
fi
#======================================================================#
#this section takes the values of the variables and outputs them to    #
#the file device.log and echos the values on the screen as well        #
#this section is for the ouput of completed script                     #
#======================================================================#
echo "$device,$routername,$dns,$answer,$snmpread,$snmpwrite,$interface" >> device.log 
echo "$device,$routername,$dns,$answer,$snmpread,$snmpwrite,$interface"

	

#
#======================================================================#
#when device is not reachable by  ping this section assigns values to  #
#variables not tested and exits script                                 #
#======================================================================#
#
        
          else
                snmpread="NO"
                snmpwrite="NO"
		interface="N/A"
		routername="N/A"
echo "$device,$routername,$dns,$answer,$snmpread,$snmpwrite,$interface" >> device.log
echo "$device,$routername,$dns,$answer,$snmpread,$snmpwrite,$interface"

fi

#
#=====================================================================#
# If device is not is DNS this section assigns values to variables    #
# Not tested and exits script                                         #
#=====================================================================#
#

 else
                dns="Not in DNS"
                answer="NO"
                snmpread="NO"
                snmpwrite="NO"
		interface="N/A"
		routername="N/A"
echo "$device,$routername,$dns,$answer,$snmpread,$snmpwrite,$interface" >> device.log
echo "$device,$routername,$dns,$answer,$snmpread,$snmpwrite,$interface"
  
fi


done

# 2  
Old 03-26-2008
Quote:
Originally Posted by Garlandxj
Code:
if [ device.log ]
then
rm device.log
fi

I don't think the condition is useful, you probably mean if [ -e device.log ] or some such but you might as well just unconditionally rm -f device.log

Quote:
Code:
cat adoa.txt | while read device

Well, here's our old friend the Useless Use of Cat. This is a fairly benign case, but as a stylistic issue, you might prefer while read device; do ... done <adoa.txt


Quote:
Code:
	answer=`(ping -c 1 -q -w 2 -n  $device 2>/dev/null | grep received | awk '{print $4}')`

Again as a stylistic issue, grep | awk is a bit of an abomination, because awk has a built-in grep. Try awk '/received/ { print $4 }'

As for the SNMP stuff, would it be more efficient to collect all the data and then at the end commit the result to SNMP in one big transaction? I don't have much experience with that so this is pure speculation.

You could run the pings asynchronously, that's probably the big bottleneck here. There was a very brief thread about that here within the last week or so; https://www.unix.com/shell-programmin...sing-bash.html

Hope this helps.
# 3  
Old 03-26-2008
Quote:
Originally Posted by Garlandxj
Code:
echo "$device,$routername,$dns,$answer,$snmpread,$snmpwrite,$interface" >> device.log
echo "$device,$routername,$dns,$answer,$snmpread,$snmpwrite,$interface"

Oh, and to reduce code duplication, you could use "tee -a device.log" here. Also, since this recurs a number of times, maybe you could break it out into its own little function. That would be my main stylistic suggestion actually -- see which parts you could modularize into functions.
# 4  
Old 03-27-2008
Thanks for the response

I will look into your suggestions. I am sure my "style" Issues will improve with use. SNMP is a large monster in itself. The ping is one of the bottle necks but the information being requested via snmp is another. Each device in the list takes 15 to 30 seconds to process. Taking the larger number that is about 350 minutes to process all devices. I was hoping there is a way to process say 10 devices at once. This may be too advanced for a shell script. My only experience at programming is from Turbo Pascal 15 years ago in high school, so I am a bit out of date.

Thank you again for all your help. So over all its not a completely horrific example of a script? LOL
# 5  
Old 03-27-2008
It's been a long time since I worked with SNMP so can't really help there. Being able to batch all the commands would probably speed it up a little bit, as you spend a significant amount of time just handshaking when you do them one by one.

Another thing I noticed just now: the parentheses inside the backticks are redundant, they force a subshell to be spawned within the backticks for no good reason.

No, it's a pretty decent script. Beginner scripts are often ways too overcomplicated but this one is straightforward and you don't seem too confused about how to do things. If you can avoid the backticks then that's probably a design improvement, but that probably requires you to find a different SNMP command.
# 6  
Old 03-27-2008
Garlandxj, your script is pretty decent, besides the minor issues noted by "era".
The duration of the whole operation is probably, as you said, because you are collecting data from 700+ devices.
You also said that you don't have access to perl on the box - if you are granted with such, in the future, you might consider Perl:SNMP module, this could speed up the things a little bit, but not too much, since you are dependеnt on SNMP binary itself.
# 7  
Old 04-01-2008
Well its fast

A few minor changes
I wrote a small script that calls the other as a back ground process.

cat devices.txt | while read device
do

export device
jobs
( devicev2 &)
done


This actually works wonderful. I Get the data from all 700 devices in under 1 minute. Closer to 35 seconds. There is a down side however. Both processors spike to 90 % utilization. Not to bad since it processes all the data so quickly. I have purchased a book on shell scripting and hopefully my "Style" will improve quickly.

One quick question: To limit this to 100 devices per batch (to keep from killing the processors) how would i go about doing that. I have been playing all night with different coding and I seem to get it wrong every time. A nudge in the right direction would be great.

Thanks again for all your help
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

How this works?

I have a program............ #include<stdio.h> #include<unistd.h> main() { if(fork == 0) { printf("Hi every body:p!!!!!!!!!!"); } } This program works with out any error. here fork is not a system call. It just act as a variable.But how it works without declaring it? What data type it... (19 Replies)
Discussion started by: carolsanjeevi
19 Replies

2. UNIX for Dummies Questions & Answers

>./a.pl works, >a.pl - does not

When I try to execute script, I get message: >aa.pl zsh: command not found: aa.pl but >./aa.pl works OK. What to change in environment to force the former way to work? Thank you, Alex Z (4 Replies)
Discussion started by: zzol
4 Replies

3. Programming

how this works?

pls explain me how this works.... DECODE (SUBSTR (field, 1, 1),'''', '''''' || field || '''','''' || field || '''') here field is a column in an oracle table.... (7 Replies)
Discussion started by: vijay_0209
7 Replies

4. UNIX for Dummies Questions & Answers

How ls | wc -l works?

ls displays files in tabbed output. Say a directory contains 3 files. ls will list all 3 in one line. So, I expect ls | wc -l to give 1, but it counts the nr of files and gives 3. Can someone explain how this works? (3 Replies)
Discussion started by: krishmaths
3 Replies

5. UNIX for Dummies Questions & Answers

scp is slow

All of the sudden scp got really slow ... from 2-3 seconds to 30 seconds. This happened for 5 hours, and then it went back to running fast. Why? If I use the -q qualifier which "Disables the progress meter" could this have any adverse effect? Thanks (1 Reply)
Discussion started by: tomstone_98
1 Replies

6. SCO

Slow cd response

Hi All We have one SCO Server here and it never gives us any trouble. Until Now!! Well its not earth shattering but we have one user who is complaining of a very slow response time when changing to his Home Directory. Other users who have similar profiles are OK. I have su'd to this user and I can... (0 Replies)
Discussion started by: JohnOB
0 Replies

7. Post Here to Contact Site Administrators and Moderators

Slow

The site has gone slow for quite some time... Can you do somethin abt it (2 Replies)
Discussion started by: DPAI
2 Replies

8. UNIX for Dummies Questions & Answers

slow ftp

On one of of solaris 7 boxes whenever i telnet or ftp it takes sometime before i get the prompt...any quick ideas where I should look...Thanks (3 Replies)
Discussion started by: suntan
3 Replies
Login or Register to Ask a Question