Snmpwalk Linux Bash doesn't return an array

2.2k views Asked by At

I am running a nagios server on opensuse linux linking via snmp to an esx 3.5 server

I am trying to get snmpwalk to return an array but it only returns a single value.

On the esx box I ahve the following shell to find the sizes of all of the snapshots-

/usr/bin/find /vmfs/volumes/ -name '*delta*.vmdk' -printf %f' '%s'\n' 

This returns the following when ran on the esx box-

[root@localhost root]# /bin/sh /etc/snmp/snmp_snapshots.sh
testnag01-000001-delta.vmdk 16840704
testnag01-000002-delta.vmdk 167835648
testnag01-000003-delta.vmdk 151058432

On the nagios box I have the following shell-

declare -a RESULT=$(/usr/bin/snmpwalk -v 2c -c public 10.10.0.20 .1.3.6.1.4.1.6876.57.101.2 | grep vmdk | awk {' print $4,$5 '} | sed 's/ /_size:/g' | sed 's/\"//g')

echo "snapshot 1" ${RESULT[0]}
echo "snapshot 2" ${RESULT[1]}

This only returns a single value, (the second one on the list)-

snapshot 1 testnag01-000002-delta.vmdk:167835648
snapshot 2

When I run the command on its own-

/usr/bin/snmpwalk -v 2c -c public 10.10.0.20 .1.3.6.1.4.1.6876.57.101.2

I get the following return-

SNMPv2-SMI::enterprises.6876.57.101.2 = STRING: "testnag01-000002-delta.vmdk 167835648"

So it looks like the command itself is failing to return more than one result?

I'm a noob here so I could very well be missing something obvious...

Thanks in advance

2

There are 2 answers

0
Brick On BEST ANSWER

I got the answer elsewhere but thought I'd post it here in case someone else has the same issue-

the last number in the OID returns the record number- so .2 returns the second value

Once I removed this .2 and used this command-

/usr/bin/snmpwalk -v 2c -c public 10.10.0.20 .1.3.6.1.4.1.6876.57.101

it worked fine!

4
konsolebox On

You forgot to place your values inside ():

declare -a RESULT=($(/usr/bin/snmpwalk -v 2c -c public 10.10.0.20 .1.3.6.1.4.1.6876.57.101.2 | grep vmdk | awk {' print $4,$5 '} | sed 's/ /_size:/g' | sed 's/\"//g'))

To prevent possible pathname expansion it's recommended to use read instead:

declare -a RESULT
IFS=$' \t\n' read -rd '' -a RESULT < <(/usr/bin/snmpwalk -v 2c -c public 10.10.0.20 .1.3.6.1.4.1.6876.57.101.2 | grep vmdk | awk {' print $4,$5 '} | sed 's/ /_size:/g' | sed 's/\"//g')

If you're going to read line by line use readarray and process substitution:

readarray -t RESULT < <(/usr/bin/snmpwalk -v 2c -c public 10.10.0.20 .1.3.6.1.4.1.6876.57.101.2 | grep vmdk | awk {' print $4,$5 '} | sed 's/ /_size:/g' | sed 's/\"//g')

Or here strings and command substitution:

readarray -t RESULT <<< "$(/usr/bin/snmpwalk -v 2c -c public 10.10.0.20 .1.3.6.1.4.1.6876.57.101.2 | grep vmdk | awk {' print $4,$5 '} | sed 's/ /_size:/g' | sed 's/\"//g')"

You could also use while read loop:

declare RESULT=()
while read -r LINE; do
    RESULT[${#RESULT[@]}]=$LINE
done < <(/usr/bin/snmpwalk -v 2c -c public 10.10.0.20 .1.3.6.1.4.1.6876.57.101.2 | grep vmdk | awk {' print $4,$5 '} | sed 's/ /_size:/g' | sed 's/\"//g')

A simpler approach to your command also is:

/usr/bin/snmpwalk -v 2c -c public 10.10.0.20 .1.3.6.1.4.1.6876.57.101.2 | awk -F '"' '/vmdk/{ t = $2; sub(/ /, "_size:", t); print t; }'