Xandrex has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I am trying to configure SNMP trap forwarding on a system but when I run the test trap it keeps giving my a warning. Here is the command and its usage:

root@LEVLNSCS001 nasadmin# /nas/sbin/nas_snmptrap /nas/site/trap.cfg -m /nays/emccelerra.mib -r 1 -f 64 -i 5 -s 7 -d "test SNMP traps"
Use of uninitialized value in string eq at /nas/sbin/nas_snmptrap line 120, <SNMP> line 6.
root@LEVLNSCS001 nasadmin# /nas/sbin/nas_snmptrap unable to read configuration file.
usage: snmptrap config_file_path -m mib_file_path -r reason code -f facility eventid -s severity -d description

line 120 in nas_snmptrap starts with $token3

# Parse trap configuration lines, and send traps. Bad formats silently + ignored. $error = 0; open(SNMP, $naspath); while (<SNMP>) { my @token = split; if ($token[0] eq 'snmpmanager' && $token[2] eq ';' && $token[3] eq 'communityname') { if(snmptrap($token[1], $token[4])) { print STDERR ("error in sending out snmp pdu\n +"); $error = 1; } } } close(SNMP);


I'm not sure what the <SNMP> is referring to.

Contents of /nas/site/trap.cfg

#snmp trap configuration file
#example:
#snmpmanager 128.154.11.20 ; communityname public
#snmpmanager host1 ; communityname public
snmpmanager 10.52.37.99 ; communityname public

So anyways, I'm stuck. Anyone have ideas? Thank you.

Replies are listed 'Best First'.
Re: Need help with SNMP script
by Lotus1 (Vicar) on Oct 04, 2011 at 17:33 UTC

    To make the existing code work you need to skip the lines that don't have enough elements for the split to return for your comparisons:

    while (<SNMP>) { next if /^#|^\s*$/; ...

    or try this to use a regular expression instead;

    while (<DATA>) { print "$1\n" if /^snmpmanager\s+(\d+\.\d+\.\d+\.\d+)\s+;\s+communi +tyname/; } __DATA__ #test file #example: snmpmanager 10.52.37.99 ; communityname public

      Hi Lotus1, thanks for the quick response. We have another systems using the same script and it is running fine, so I would rather not make changes. I read elsewhere on this site that the warning "Use of uninitialized value in string eq" is just that, a warning. I guess I am just confused and don't understand what the expression is trying to do because I really don't understand the code. I don't know anything about code. I had to google
      just to write the original post.

        Sounds to me like the trap.cfg file must be different on the other machine. You could get ride of the comment lines (lines that start with '#'). Compare the cfg files for differences.

        <SNMP> is reading a line from the file in $naspath. The line gets split into the @token array and the elements compared with strings.

Re: Need help with SNMP script
by Anonymous Monk on Dec 09, 2011 at 11:11 UTC
    Check your trap.cfg file for any blank lines and remove them.