#!/usr/bin/perl -w ########################################################################### # # # Module: trap.pl # # # # Description: This script is invoked by the "snmptrapd" utility, and # # accepts arguments from that utility and stores them in # # a database table for later processing. # # # # Notes: Invoke the snmptrapd utility as "snmptrapd -On" # # # # History: 03/07/2001 GEB Initial Coding # # 03/07/2001 GEB Add timestamp column # # # ########################################################################### ########################################################################### # PACKAGES # ########################################################################### use Pg; ########################################################################### # CONFIG FILES # ########################################################################### #-=======================================================================-# #- -# #- /etc/snmp/snmptrapd.conf -# #- ------------------------ -# #- traphandle default /root/trap.pl -# #- -# #-=======================================================================-# ########################################################################### # DATABASE SCHEMA # ########################################################################### #-=======================================================================-# #- -# #- CREATE SEQUENCE trap_seq ; -# #- -# #- CREATE TABLE trap ( -# #- trap_id int4 NOT NULL, -# #- timestamp timestamp NOT NULL, -# #- host_name text NOT NULL, -# #- host_addr character(15) NOT NULL -# #- ); -# #- -# #- CREATE TABLE trap_args ( -# #- trap_id int4 NOT NULL, -# #- oid_id text NOT NULL, -# #- oid_value text NOT NULL -# #- ); -# #- -# #- CREATE UNIQUE INDEX trap_idx on trap ( trap_id ); -# #- -# #- CREATE INDEX trap_args_idx on trap_args ( trap_id ); -# #- -# #-=======================================================================-# ########################################################################### # MAIN # ########################################################################### $conn = Pg::connectdb("dbname=nms"); die $conn->errorMessage unless PGRES_CONNECTION_OK eq $conn->status; $result = $conn->exec( "BEGIN WORK" ); die $conn->errorMessage unless PGRES_COMMAND_OK eq $result->resultStatus; $result = $conn->exec( "SELECT NEXTVAL('trap_seq')" ); die $conn->errorMessage unless PGRES_TUPLES_OK eq $result->resultStatus; @row = $result->fetchrow; $trap_id = $row[0]; $host_name = ; $host_addr = ; chop( $host_name ); chop( $host_addr ); $result = $conn->exec( " INSERT INTO TRAP ( TRAP_ID, TIMESTAMP, HOST_NAME, HOST_ADDR ) VALUES ( $trap_id, CURRENT_TIMESTAMP, '$host_name', '$host_addr' ) " ); die $conn->errorMessage unless PGRES_COMMAND_OK eq $result->resultStatus; while ( ) { if ( /^([\.0-9]*) +"?(.*?)"?$/ ) { $oid_id = $1; $oid_value = $2; $oid_value =~ s/(['"])/\\$1/g; $result = $conn->exec( " INSERT INTO TRAP_ARGS ( TRAP_ID, OID_ID, OID_VALUE ) VALUES ( $trap_id, '$oid_id', '$oid_value' ) " ); die $conn->errorMessage unless PGRES_COMMAND_OK eq $result->resultStatus; } } $result = $conn->exec( "COMMIT WORK" ); die $conn->errorMessage unless PGRES_COMMAND_OK eq $result->resultStatus; exit( 0 ); ########################################################################### # END OF PERL SCRIPT # ###########################################################################