1827324 Members
4526 Online
109962 Solutions
New Discussion

snmp script question

 
SOLVED
Go to solution
Rob Johnson_3
Regular Advisor

snmp script question

I would like to have the 4 returned values from snmptest2 ending with a comma. How can I do this. What I have below is something "Muthukumar" asked me to do but it still doesn't appear to be quiet right. In the nodes file, there is currently only one hostname that snmptest2 is reading.


johnsonr@nmscrme01 > cat snmptest2
index=0
for device in `cat /opt/home/johnsonr/scripts/nodes`
do
snmpwalk $device public system.sysName.0
ret1[$index]=$?","
snmpwalk $device public interfaces.ifTable.ifEntry.ifDescr.1
ret2[$index]=$?","
snmpwalk $device public interfaces.ifTable.ifEntry.ifAdminStatus.1
ret3[$index]=$?","
snmpwalk $device public interfaces.ifTable.ifEntry.ifOperStatus.1
ret4[$index]=$?","
let index=index+1
done
johnsonr@nmscrme01 > ./snmptest2
system.sysName.0 = hostname.domain.com
interfaces.ifTable.ifEntry.ifDescr.1 = FastEthernet0/1
interfaces.ifTable.ifEntry.ifAdminStatus.1 = up(1)
interfaces.ifTable.ifEntry.ifOperStatus.1 = down(2)
johnsonr@nmscrme01 >
9 REPLIES 9
Hein van den Heuvel
Honored Contributor
Solution

Re: snmp script question


This noe topic appears to be a continuation of topic:
http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=958181


If you could attach a txt file with sample input and perhaps a few lines of sample output then more people will be able to try to help you.

Hein.

Rob Johnson_3
Regular Advisor

Re: snmp script question

The input is this:

A file called nodes. See the second line in the snmptest2 script above.

In the nodes file is one line of text that is this:
lca-sfa-rtr1.somewhere.com

Additional device names can be added to this nodes file once I get it working.

The output of the script is:

johnsonr@nmscrme01 > ./snmptest2
system.sysName.0=lca-sfa-rtr1.somewhere.com interfaces.ifTable.ifEntry.ifDescr.1=Fa0/1
interfaces.ifTable.ifEntry.ifAdminStatus.1=up
interfaces.ifTable.ifEntry.ifOperStatus.1=down
johnsonr@nmscrme01 >

I would like to have a comma at the end of each of these lines so I can easily open the file as a comma seperated value file using MS excel.
Hein van den Heuvel
Honored Contributor

Re: snmp script question



Ok, I have something to get you going, based on a similar problem I recently solved for myself.

The PERL script test.pl code is below, and in the attachment.

It is completely dynamic.
It reads the list of nodes from a file you can optionally specify defaulting to nodes.txt
It readd the list of columns from a variable you can readily edit.
It stores output in a file you can optionally specify defaulting to test.csv.

Hope this helps,
Enjoy,

Hein.


I can not do that 'snmpwalk' function on my system, so i replaced it with input data from a file. The test inputfile looked like:

------------------------- sample snmpdata.txt ------------

system.sysName.0=lca-sfa-rtr1.somewhere.com
interfaces.ifTable.ifEntry.ifDescr.1=Fa0/1
interfaces.ifTable.ifEntry.ifAdminStatus.1=up
interfaces.ifTable.ifEntry.ifOperStatus.1=down
system.sysName.0=lca-sfa-rtr1.elsewhere.com
interfaces.ifTable.ifEntry.ifDescr.1=Fa0/test
interfaces.ifTable.ifEntry.ifAdminStatus.1=left
interfaces.ifTable.ifEntry.ifOperStatus.1=right


-------------------------- sample nodes.txt --------------------------

lca-sfa-rtr1.somewhere.com
lca-sfa-rtr1.elsewhere.com


----- test result -----------------

C:\sap>perl test.pl < snmpdata.txt

C:\sap>type test.csv
system.sysName.0,interfaces.ifTable.ifEntry.ifDescr.1,interfaces.ifTable.ifEntr
.ifAdminStatus.1,interfaces.ifTable.ifEntry.ifOperStatus.1
lca-sfa-rtr1.somewhere.com,Fa0/1,up,down
lca-sfa-rtr1.elsewhere.com,Fa0/test,left,right



my @columns = qw/system.sysName.0 interfaces.ifTable.ifEntry.ifDescr.1 interfaces.ifTable.ifEntry.ifAdminStatus.1 interfaces.ifTable.ifEntry.ifOperStatus.1/;
my $CSV = "test.csv";
my $NODES = "nodes.txt";
#
use strict 'vars';
use Getopt::Std;
use vars qw/ %opt /;
my (%snmpwalk);
my ($csv, $nodes, $tmp, $i, $x, $nam, $val, $node);
my ($opt_d, $opt_c, $opt_n, $debug, $column, $node, $line);

getopts ('dc:n:',\%opt) or &usage;
$debug = $opt{d} ? 1 : 0;
$csv = $opt{c} ? $opt{c} : $CSV;
$nodes = $opt{n} ? $opt{n} : $NODES;
sub usage()
{
print STDERR << "EOF";

This program read a nodes files and creates a csv file with snmp values for each node.

usage: $0 [-d] [-c:csv_file] [-n:nodes_file] [test-input-file-just-for-demo]

-d : print debugging messages to stderr
-c : csv output file name. Default $CSV
-n : nodes input file name. Default $NODES

EOF
exit;
}


#
# temporary code to replace 'snmpwalk' which I do not have.
# just fakes it with an array read from STDIN
#
while (<>) {
chop;
($nam,$val)=split /=/;
$node = $val if (/sysName/);
$snmpwalk{$node.$nam}=$val;
print "$node|$nam|$val\n" if $debug;
}

open (NODES, "<$nodes") or die "could not open nodes file $nodes";

open (CSV, ">$csv") or die "could not open scv file $csv";
$tmp = join(",",@columns);
print CSV "$tmp\n";

while () {
chop;
$node = $_;
$line = "";
print "- $node\n" if $debug;
foreach $column (@columns) {
$val = $snmpwalk{$node.$column}; # replace with snmpwalk code
$line .= $val . ",";
print "-- $column, $val\n" if $debug;
}
chop $line;
print CSV $line."\n";
}

Muthukumar_5
Honored Contributor

Re: snmp script question

Use this code:

index=0
for file in `ls`
do
ls -l $file >/dev/null
arr[$index]=$?","
let index=index+1
done
echo ${arr[*]}


index=0
for device in `cat /opt/home/johnsonr/scripts/nodes`
do
ret1[$index]=$(snmpwalk $device public system.sysName.0)
ret2[$index]=$(snmpwalk $device public interfaces.ifTable.ifEntry.ifDescr.1)
ret3[$index]=$(snmpwalk $device public interfaces.ifTable.ifEntry.ifAdminStatus.1)
ret4[$index]=$(snmpwalk $device public interfaces.ifTable.ifEntry.ifOperStatus.1)
let index=index+1
done

i=0
while [[ $i -lt ${#ret1[*]} ]]
do
echo ${ret1[$i]},${ret2[$i]},${ret3[$i]},${ret4[$i]}
let i=i+1
done

### Problem is, $? is used to get Return code not the strings returned from there. It will work now. ###

hth.
Easy to suggest when don't know about the problem!
Rob Johnson_3
Regular Advisor

Re: snmp script question

See the attached text file. I copied the updated script you provided but it still has a small problem.

I can walk a single device but when I tried to run the script you provided against the same device, I get an snmp timeout.

Please take a look.

Muthukumar_5
Honored Contributor

Re: snmp script question

Message of "Timeout: No Response from lcacptc23.cpt.ca.kp.org" is a error message. I hope reachablity to lcacptc23.cpt.ca.kp.org is not working there.

How will check reachablity to lcacptc23.cpt.ca.kp.org machine. Check like with ping as,

#!/usr/bin/ksh
ping lcacptc23.cpt.ca.kp.org -n 1 1>/dev/null 2>&1
if [[ $? -ne 0 ]]
then
echo "It is not reachable to lcacptc23.cpt.ca.kp.org"
exit 1
fi

index=0
for device in `cat /opt/home/johnsonr/scripts/nodes`
do
ret1[$index]=$(snmpwalk $device public system.sysName.0)
ret2[$index]=$(snmpwalk $device public interfaces.ifTable.ifEntry.ifDescr.1)
ret3[$index]=$(snmpwalk $device public interfaces.ifTable.ifEntry.ifAdminStatus.1)
ret4[$index]=$(snmpwalk $device public interfaces.ifTable.ifEntry.ifOperStatus.1)
let index=index+1
done

i=0
while [[ $i -lt ${#ret1[*]} ]]
do
echo ${ret1[$i]},${ret2[$i]},${ret3[$i]},${ret4[$i]}
let i=i+1
done

Execute this script and let us know.

hth.

Easy to suggest when don't know about the problem!
Muthukumar_5
Honored Contributor

Re: snmp script question

Message of "Timeout: No Response from lcacptc23.cpt.ca.kp.org" is a error message. Try to debug that problem. Configuration with snmpd.conf is not working.

http://www.ibr.cs.tu-bs.de/pipermail/jasmin/2002-May/000522.html

hth.
Easy to suggest when don't know about the problem!
Hein van den Heuvel
Honored Contributor

Re: snmp script question


Rob,

Did you give the perl code a try?
I think all you need to do is to replace the dummy snmpwalk with the real one:

$_ = `snmpwalk $node public $column`;
if (/=\s*(.*)$/) {
$val = $1;
} else {
$val = "???";
}


This also allows for spaces after the "=" in the snmpwalk output which I now see in the actual data lines in your attached text file.

Modifies, but untested, code attached.

Hein.
Rob Johnson_3
Regular Advisor

Re: snmp script question

Correct. I didn't change the community string in the original script provided from public to what it is.

The perl script worked too. I actually like it better because it writes the output directly to a .csv file. That's the next thing I was going to request in the original script provided.

That's why I love this forum...when I need something like this, I always come away with a solution.

Thanks for you guys' help on this.