Posted on 2006-05-22 07:17:50-07 by lisaparma
traptomysql
hi all, im beginner in net-snmp and perl... i want to send my snmptrap to mysql... this is an example from net-snmp that send trap to email... #!/usr/bin/perl # This is a snmptrapd handler script to convert snmp traps into email # messages. # Usage: # Put a line like the following in your snmptrapd.conf file: # traphandle TRAPOID|default c:/usr/bin/traptomysql [-f FROM] [-s SMTPSERVER]b ADDRESSES # FROM defaults to "root" # SMTPSERVER defaults to "localhost" use Net::SMTP; use Getopt::Std; $opts{'s'} = "localhost"; $opts{'f'} = 'root@' . `hostname`; chomp($opts{'f'}); getopts("hs:f:", \%opts); if ($opts{'h'}) { print " traptoemail [-s smtpserver] [-f fromaddress] toaddress [...] traptoemail shouldn't be called interatively by a user. It is designed to be called as an snmptrapd extension via a \"traphandle\" directive in the snmptrapd.conf file. See the snmptrapd.conf file for details. Options: -s smtpserver Sets the smtpserver for where to send the mail through. -f fromaddress Sets the email address to be used on the From: line. toaddress Where you want the email sent to. "; exit; } die "no recepients to send mail to" if ($#ARGV < 0); # process the trap: $hostname = <STDIN>; chomp($hostname); $ipaddress = <STDIN>; chomp($ipaddress); $maxlen = 0; while(<STDIN>) { ($oid, $value) = /([^\s]+)\s+(.*)/; push @oids, $oid; push @values, $value; $maxlen = (length($oid) > $maxlen) ? length($oid) : $maxlen; } $maxlen = 60 if ($maxlen > 60); $formatstr = "%" . $maxlen . "s %s\n"; die "illegal trap" if ($#oids < 1); # send the message $message = Net::SMTP->new($opts{'s'}) || die "can't talk to server $opts{'s'}\n"; $message->mail($opts{'f'}); $message->to(@ARGV) || die "failed to send to the recepients ",join(",",@ARGV),": $!"; $message->data(); $message->datasend("To: " . join(", ",@ARGV) . "\n"); $message->datasend("From: $opts{f}\n"); $message->datasend("Subject: trap received from $hostname: $values[1]\n"); $message->datasend("\n"); $message->datasend("Host: $hostname ($ipaddress)\n"); for($i = 0; $i <= $#oids; $i++) { $message->datasend(sprintf($formatstr, $oids[$i], $values[$i])); } $message->dataend(); $message->quit; but i dont understand what the script means can somebody help me to get the script for trap to mysql?! please... thx b4:)
Direct Responses: 2332 | Write a response
Posted on 2006-05-22 08:06:54-07 by gellyfish in response to 2331
Re: traptomysql

I don't think you have a question about Net::SNMP::Interfaces which is a module specifically for access the network interface OIDs and doesn't concern itself with SNMP traps at all.

However the code above is designed to be run by the snmptrapd and is passed the information about the trap on STDIN, you will need to read the documentation of your SNMP server to find out how to configure the trap handling to have the script run. In order to have the trap information put into a database you would replace the parts that send the mail message with a database insert using the DBI.

You'll probably find that because this is a more general question and not one about a particular CPAN module that you will get more and better answers somewhere like Perlmonks or comp.lang.perl.misc

/J\

Direct Responses: Write a response
Perl Weekly newsletter
A free weekly newsletter for people who are busy to read all the blogs. click here to check it out.