Hi list,

Here's a little improvement for the rancid patch that send traps on some events.

I added the following 3 events :

rancidNodeAdded when routers.db changed and a node has been added
rancidNodeDeleted when routers.db changed and a node has been deleted
rancidConfigurationChanged when a configuration of a router has changed and is about to be commited as a new version.

Attached are the improved patch and the new version of the MIB. I tested it a little, but you never know ;)

Regards,

--
Philippe Guillebert

Bull, Architect of an Open World
Tél : +33 (0)1 30 80 61 81
http://www.bull.com

diff -Naur bin_ori/control_rancid bin/control_rancid
--- bin_ori/control_rancid	2009-04-08 11:46:49.000000000 +0200
+++ bin/control_rancid	2009-04-08 16:00:00.000000000 +0200
@@ -42,6 +42,30 @@
 # 
 # control_rancid $GROUP
 #
+#----------------------------------------------------------------------------
+#
+# *** NOTICE ***
+#
+# This is a modified version of the "control_rancid" script from the rancid's
+# original distribution archive. The modifications were done in order to
+# implement the SNMP notifications required by the OpenNMS/RANCID integration
+# project.
+#
+# The modified version is distributed as a standard set of "patch" files; if
+# you are reading this comments from the working "control_rancid" script file,
+# the patch was succesfully applied on your system (a backup copy of the
+# original file should have been saved by the patch utility).
+#
+# Additional copyrights for this modified script are:
+#
+# Portion (c) Copyright 2009+ Rocco RIONERO
+# Portion (c) Copyright 2009+ The OpenNMS Group, Inc.
+#
+#
+# CUSTOM-VERSION:	0.1 (build 20090331-01)
+# BASED-ON-VERSION:	2.3.2a7
+#
+#----------------------------------------------------------------------------
 
 # print a usage message to stderr
 pr_usage() {
@@ -301,6 +325,10 @@
 	fi
 	$RCSSYS commit -m 'new router' $router
 	echo "Added $router"
+	# A new router has been added in the repository, send an event
+	if [ "$OPENNMS_NOTIFY_CMD" != "" ] ; then
+  	  $OPENNMS_NOTIFY_CMD --added $GROUP $router
+	fi
     done
     echo
     cd $DIR
@@ -337,10 +365,15 @@
 	    svn add $router
 	fi
 	echo "$RCSSYS added missing router $router"
+	# A new router has been added in the repository, send an event
+	if [ "$OPENNMS_NOTIFY_CMD" != "" ] ; then
+  	  $OPENNMS_NOTIFY_CMD --added $GROUP $router
+	fi
     fi
 done
 echo
-# delete configs from RCS for routers not listed in routers.up.
+# delete configs from RCS for routers not listed in routers.db.
+# NOTE: the comment above was wrong in the original script: adjusted (rock, 20090331-01)
 for router in `find . \( -name \*.new -prune -o -name CVS -prune -o -name .svn -prune \) -o -type f -print | sed -e 's/^.\///'` ; do
     grep -i "^$router:" ../router.db > /dev/null 2>&1
     if [ $? -eq 1 ]; then
@@ -348,6 +381,10 @@
 	$RCSSYS delete $router
 	$RCSSYS commit -m 'deleted router' $router
 	echo "Deleted $router"
+	# A router has been deleted in the repository, send an event
+	if [ "$OPENNMS_NOTIFY_CMD" != "" ] ; then
+  	  $OPENNMS_NOTIFY_CMD --deleted $GROUP $router
+	fi
     fi
 done
 cd $DIR
@@ -381,16 +418,31 @@
 echo "Trying to get all of the configs."
 par -q -n $PAR_COUNT -c "rancid-fe {}" $devlistfile
 
-# This section will generate a list of missed routers
-# and try to grab them again.  It will run through
-# $pass times.
-pass=$MAX_ROUNDS
+#--- begin (rock, 20090331-01 : OpenNMS/rancid integration) ---------------------------
+#
+# NOTE: In order to always send a device-specific success notification to OpenNMS,
+#	I had to slightly modify the logic of the retry-loop over the original script.
+#	I tried to conform as much as possible to the original coding style... -- rock
+
+
+# This section will generate a list of missed routers and try to grab them again.
+# It will run through up to $pass times (note: $MAX_ROUNDS is always non-zero and
+# $round is the number of the RETRY-round to be performed, i.e. NOT counting the
+# first attempt)
+
 round=1
-if [ -f $DIR/routers.up.missed ]; then
-    rm -f $DIR/routers.up.missed
-fi
-while [ $round -le $pass ]
+
+while :
 do
+    # delete any previous list of missed routers
+
+    if [ -f $DIR/routers.up.missed ]; then
+      rm -f $DIR/routers.up.missed
+    fi
+
+    # generate the current list of missed routers (if any)
+    # and send proper notifications to OpenNMS
+
     for router in `cat $devlistfile`
     do
 	OFS=$IFS
@@ -403,20 +455,43 @@
 	then
 	    echo "$router:$mfg" >> $DIR/routers.up.missed
 	    rm -f $router.new
+	    # send a router-missed notification
+	    if [ "$OPENNMS_NOTIFY_CMD" != "" ] ; then
+  	      $OPENNMS_NOTIFY_CMD --ko $GROUP $router
+	    fi
+	else
+	    # send a router-success notification
+	    if [ "$OPENNMS_NOTIFY_CMD" != "" ] ; then
+  	      $OPENNMS_NOTIFY_CMD --ok $GROUP $router
+	    fi
 	fi
     done
 
+    # if a "routers.up.missed" file exists, then some router was missed
+
     if [ -f $DIR/routers.up.missed ]; then
-	echo "====================================="
-	echo "Getting missed routers: round $round."
-	par -q -n $PAR_COUNT -c "rancid-fe \{}" $DIR/routers.up.missed
-	rm -f $DIR/routers.up.missed
-	round=`expr $round + 1`
+	if [ $round -le $MAX_ROUNDS ] ; then
+	  echo "====================================="
+	  echo "Getting missed routers: round $round."
+	  par -q -n $PAR_COUNT -c "rancid-fe \{}" $DIR/routers.up.missed
+	  round=`expr $round + 1`
+	else
+	  # there are still missed routers, but no more attempts left
+	  # note: here $round is the total number of attempts (including
+	  # the first one done before the retries)
+	  if [ "$OPENNMS_NOTIFY_CMD" != "" ] ; then
+  	    $OPENNMS_NOTIFY_CMD --failure "After $round total attempts there are still failing routers in group $GROUP: giving up"
+	  fi
+	  rm -f $DIR/routers.up.missed
+	  break
+	fi
     else
 	echo "All routers sucessfully completed."
-	round=`expr $pass + 1`
+	break
     fi
+
 done
+#--- end ------------------------------------------------------------------------------
 echo
 
 # Make sure that no empty/truncated configs are accepted.  The remainder are
@@ -441,6 +516,18 @@
 	then
 	    rm -f $router.new
 	else
+	    # Test if $router.new is different from $router
+	    # if yes, send a "RouterConfigurationChanged" trap
+	    diff $router.new $router 1>/dev/null 2>/dev/null
+	    if [ $? -ne 0 ]
+	    then
+		# There is a difference, send a trap
+		if [ "$OPENNMS_NOTIFY_CMD" != "" ] ; then
+		    $OPENNMS_NOTIFY_CMD --changed $GROUP $router
+		fi
+	    fi
+	    
+	    # overwrite the existing conf anyway, CVS will handle the diffs
 	    mv $router.new $router
 	    if [ $? -ne 0 ]; then
 		echo "Error: could not rename $router.new to $router"
diff -Naur bin_ori/rancid-trap bin/rancid-trap
--- bin_ori/rancid-trap	1970-01-01 01:00:00.000000000 +0100
+++ bin/rancid-trap	2009-04-08 16:08:11.000000000 +0200
@@ -0,0 +1,273 @@
+#!/bin/bash
+#
+#=============================================================================
+# rancid-trap - rancid's trap utility                   OpenNMS/RANCID Project
+#
+# Copyright (c) 2009+ Rocco Rionero
+# Copyright (c) 2009+ The OpenNMS Group, Inc.
+# All rights reserved everywhere.
+#
+# This program was developed and is maintained by Rocco RIONERO
+# ("the author") and is subject to dual-copyright according to
+# the terms set in "The OpenNMS Project Contributor Agreement".
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
+# USA.
+#
+# The author can be contacted at the following email address:
+#
+#	Rocco RIONERO
+#	rock(at)rionero.com
+#
+# (please, specify "rancid-trap-utility" in the subject of your message)
+#
+#-----------------------------------------------------------------------------
+# OpenNMS Network Management System is Copyright by The OpenNMS Group, Inc.
+#
+# RANCID application is Copyright by Terrapin Communications, Inc.
+#=============================================================================
+
+#-----------------------------------------------------------------------------
+#                 UNSUPPORTED BETA-RELEASE SOFTWARE VERSION
+#-----------------------------------------------------------------------------
+
+# This script is a "wrapper" for snmptrap; it is called by the modified rancid
+# scripts in order to send SNMPv1 traps to multiple receivers. The ability to
+# send SNMP traps was added during the development of the rancid's integration
+# with OpenNMS, in order to give proper feedback on rancid's operations to the
+# controlling management system.
+#
+# The SNMP OIDs used in this script were defined under the Private Enterprise
+# Number officially assigned to the author (a member of the OpenNMS/RANCID
+# project team) by IANA (http://www.iana.org/). A standard SNMPv1 MIB file
+# (RANCID-CUSTOM-MIB.mib) is included, so to be loaded by the trap receivers.
+#
+# If you need any information about the SNMP objects used, want to extend or
+# modify the above MIB file or make changes that may otherwise affect the OIDs
+# hierarchy defined, _PLEASE_ do not hesitate to the author to the above email
+# address. Thank you.
+
+
+SCRIPT_VERSION="0.1 (build 20090331-01)"
+
+
+# NOTE: the value of following variable assumes that the snmptrap utility
+#	is in the shell search path of the user executing this script; on
+#	your system might need to include the full pathname to the snmptrap
+#	binary
+
+SNMP_TRAP_CMD="snmptrap"
+
+
+
+
+#========================================================# 
+# ANY CHANGE DONE HERE WILL REQUIRE A DIFFERENT MIB FILE #
+#========================================================#
+
+# parent OIDs hierarchy
+
+OID_ENTERPRISES=".1.3.6.1.4.1"
+OID_RIONERO="${OID_ENTERPRISES}.31543"
+
+# rancid's custom OIDs hierarchy
+
+OID_RANCID="${OID_RIONERO}.1.1.2.1"
+OID_RANCID_OBJ="${OID_RANCID}.1"
+OID_RANCID_TRAP="${OID_RANCID}.2"
+
+OID_DeviceName="${OID_RANCID_OBJ}.1"
+OID_DeviceType="${OID_RANCID_OBJ}.2"
+OID_GroupName="${OID_RANCID_OBJ}.3"
+OID_FailureMessage="${OID_RANCID_OBJ}.4"
+
+# specific-trap numbers
+
+SpecificTrapGenericFailure=1
+SpecificTrapDownloadSuccess=2
+SpecificTrapDownloadFailure=3
+SpecificTrapGroupLocked=4
+SpecificTrapNodeAdded=5
+SpecificTrapNodeDeleted=6
+SpecificTrapConfigurationChanged=7
+
+
+#-----------------------------------------------------------------------------
+function ShowUsage() {
+cat << __EOT__
+rancid-trap version ${SCRIPT_VERSION}
+
+usage: rancid-trap COMMON_OPTIONS TRAP_OPTIONS
+
+COMMON_OPTIONS are:
+
+	[-c <community>]
+		(optional) specifies SNMPv1 community string; if missing a
+		default value of "public" will be used, if specified more
+		than once, the last value will be used
+
+	-r <receiver-host>
+		specifies the receiver of the trap; can be specified multiple
+		times to send the trap to multiple receivers (will always use
+		the same community string)
+
+TRAP_OPTIONS are:
+	--ok <groupName> <deviceName>
+		sends a "download ok" trap for the device <deviceName> as
+		processed within the group <groupName>; <deviceName>  will
+		also be used as sending-agent of the trap
+
+	--ko <groupName> <deviceName>
+		sends a "download failed" trap for the device <deviceName>
+		as processed within the group <groupName>; <deviceName>
+		will also be used as sending-agent of the trap
+
+	--added <groupName> <deviceName>
+		sends a "Node Added" trap for the device <deviceName>
+		as processed within the group <groupName>; <deviceName>
+		will also be used as sending-agent of the trap
+
+	--deleted <groupName> <deviceName>
+		sends a "Node Deleted" trap for the device <deviceName>
+		as processed within the group <groupName>; <deviceName>
+		will also be used as sending-agent of the trap
+
+	--changed <groupName> <deviceName>
+		sends a "NodeConfigurationChanged" trap for the device <deviceName>
+		as processed within the group <groupName>; <deviceName>
+		will also be used as sending-agent of the trap
+
+	--locked <groupName>
+		sends a "group locked" trap for the group <groupName>; the
+		sending-agent of the trap will be the localhost
+
+	--failure <message>
+		sends a "generic failure" trap using <message> as description
+		the sending-agent of the trap will be the localhost
+
+__EOT__
+
+exit 127
+}
+#-----------------------------------------------------------------------------
+
+
+
+### MAIN HERE ###
+
+# default SNMPv1 community string is "public"
+comm="-c public"
+
+
+# process command-line arguments
+
+rcvr=""
+
+while [ "${1}" != "" ] ; do
+
+  case "${1}" in
+
+           "-c") if [ "${2}" == "" ] ; then ShowUsage ; fi
+                 comm="-c ${2}"
+                 shift 2
+                 ;;
+
+           "-r") if [ "${2}" == "" ] ; then ShowUsage ; fi
+                 rcvr="${rcvr} ${2}"
+                 shift 2
+                 ;;
+
+    "--failure") if [ "${2}" == "" ] ; then ShowUsage ; fi
+                 trap=${SpecificTrapGenericFailure}
+                 sndr=""
+                 uptime=""
+                 vars=(${OID_FailureMessage} s "${2}")
+                 shift 2
+                 ;;
+
+         "--ok") if [ "${2}" == "" -o "${3}" == "" ] ; then ShowUsage ; fi
+                 trap=${SpecificTrapDownloadSuccess}
+                 sndr=${3}
+                 uptime="0"
+                 vars=(${OID_GroupName} s "${2}" ${OID_DeviceName} s "${3}")
+                 shift 3
+                 ;;
+
+         "--ko") if [ "${2}" == "" -o "${3}" == "" ] ; then ShowUsage ; fi
+                 trap=${SpecificTrapDownloadFailure}
+                 sndr=${3}
+                 uptime="0"
+                 vars=(${OID_GroupName} s "${2}" ${OID_DeviceName} s "${3}")
+                 shift 3
+                 ;;
+
+         "--added") if [ "${2}" == "" -o "${3}" == "" ] ; then ShowUsage ; fi
+                 trap=${SpecificTrapNodeAdded}
+                 sndr=${3}
+                 uptime="0"
+                 vars=(${OID_GroupName} s "${2}" ${OID_DeviceName} s "${3}")
+                 shift 3
+                 ;;
+
+         "--deleted") if [ "${2}" == "" -o "${3}" == "" ] ; then ShowUsage ; fi
+                 trap=${SpecificTrapNodeDeleted}
+                 sndr=${3}
+                 uptime="0"
+                 vars=(${OID_GroupName} s "${2}" ${OID_DeviceName} s "${3}")
+                 shift 3
+                 ;;
+
+         "--changed") if [ "${2}" == "" -o "${3}" == "" ] ; then ShowUsage ; fi
+                 trap=${SpecificTrapConfigurationChanged}
+                 sndr=${3}
+                 uptime="0"
+                 vars=(${OID_GroupName} s "${2}" ${OID_DeviceName} s "${3}")
+                 shift 3
+                 ;;
+
+     "--locked") if [ "${2}" == "" ] ; then ShowUsage ; fi
+                 trap=${SpecificTrapGroupLocked}
+                 sndr=""
+                 uptime=""
+                 vars=(${OID_GroupName} s "${2}")
+                 shift 2
+                 ;;
+
+              *) ShowUsage
+                 ;;
+
+  esac
+
+done
+
+
+# we need the trap and at least one receiver
+
+if [ "${trap}" == "" -o "${rcvr}" == "" ] ; then ShowUsage ; fi
+
+
+# do the dirty job...
+
+set -- ${rcvr}
+
+while [ "${1}" != "" ] ; do
+  ${SNMP_TRAP_CMD} -v1 ${comm} ${1} ${OID_RANCID_TRAP} "${sndr}" 6 "${trap}" "${uptime}" "${va...@]}"
+  ecode=$?
+  if [ ${ecode} -ne 0 ] ; then break ; fi
+  shift
+done
+
+exit ${ecode}
+
   ---
   --- SNMP v1 MIB FILE
   --- RANCID SNMP NOTIFICATIONS
   --- (as implemented for the OpenNMS/rancid integration project)
   ---
   --- Version 1.1 (2009 03 28)
   ---
   --- The enclosed MIB definitions were implemented under a private
   --- enterprise number officially assigned by IANA to a member of
   --- the OpenNMS/rancid integration project team.
   ---
   --- If you need any information or want to modify or extend this MIB,
   --- PLEASE, contact <iana-pen-cont...@rionero.com>. Thank you.
   ---
   ---
   --- History of changes:
   ---
   ---  1.1     corrected the descriptions for "rancidTrapGenericFailure"
   ---          and "rancidFailureMessage" (typical "copy-and-paste" error)
   ---
   ---  1.0     initial release
   ---


   RANCID-CUSTOM-MIB DEFINITIONS ::= BEGIN


   IMPORTS
       OBJECT-TYPE
           FROM RFC-1212
       enterprises
           FROM RFC1155-SMI
       DisplayString
           FROM RFC1213-MIB
       TRAP-TYPE
           FROM RFC-1215;



   ---
   --- Following are pre-requisites placed here to keep this module short
   --- and to avoid importing other not needed private MIBs
   ---

   RIONERO                  OBJECT IDENTIFIER ::= { enterprises 31543 }

   SoftwareApplications     OBJECT IDENTIFIER ::= { RIONERO 1 }

   OpenSource               OBJECT IDENTIFIER ::= { SoftwareApplications 1 }

   customizations           OBJECT IDENTIFIER ::= { OpenSource 2 }



   ---
   --- Here the actual module definitions begin
   ---

   rancidCustomMIB          OBJECT IDENTIFIER ::= { customizations 1 }

   rancidObjects            OBJECT IDENTIFIER ::= { rancidCustomMIB 1 }

   rancidTraps              OBJECT IDENTIFIER ::= { rancidCustomMIB 2 }



   ---
   --- data objects
   ---
   
   rancidDeviceName OBJECT-TYPE
       SYNTAX      OCTET STRING (SIZE (0..64))
       ACCESS      read-only
       STATUS      current
       DESCRIPTION "The name of a rancid's managed device."
       ::= { rancidObjects 1 }


   rancidDeviceType OBJECT-TYPE
       SYNTAX      OCTET STRING (SIZE (0..32))
       ACCESS      read-only
       STATUS      current
       DESCRIPTION "The name of a rancid's deviceType."
       ::= { rancidObjects 2 }


   rancidGroupName OBJECT-TYPE
       SYNTAX      OCTET STRING (SIZE (0..64))
       ACCESS      read-only
       STATUS      current
       DESCRIPTION "The name of a rancid's group of managed devices."
       ::= { rancidObjects 3 }


   rancidFailureMessage OBJECT-TYPE
       SYNTAX      DisplayString
       ACCESS      read-only
       STATUS      current
       DESCRIPTION "A string describing a failure condition for which
                   is not defined a more specific trap."
       ::= { rancidObjects 4 }




   --
   -- notifications, i.e., traps
   --

   rancidTrapGenericFailure TRAP-TYPE
       ENTERPRISE  rancidTraps
       VARIABLES   { rancidFailureMessage }
       DESCRIPTION "This trap is sent each time rancid needs to notify a
                   failure condition for which is not defined a more
                   specific trap."
   ::= 1


   rancidTrapDownloadSuccess TRAP-TYPE
       ENTERPRISE  rancidTraps
       VARIABLES   { rancidGroupName, rancidDeviceName }
       DESCRIPTION "This trap is sent each time rancid successfully downloads
                   the configuration from a device."
   ::= 2


   rancidTrapDownloadFailure TRAP-TYPE
       ENTERPRISE  rancidTraps
       VARIABLES   { rancidGroupName, rancidDeviceName }
       DESCRIPTION "This trap is sent each time rancid fails to download the
                   configuration from a device."
   ::= 3


   rancidTrapGroupLocked TRAP-TYPE
       ENTERPRISE  rancidTraps
       VARIABLES   { rancidGroupName }
       DESCRIPTION "This trap is sent each time rancid is not able to process
                   a group of devices due to the existence of a group's lock
                   file."
   ::= 4


   rancidNodeAdded TRAP-TYPE
       ENTERPRISE  rancidTraps
       VARIABLES   { rancidGroupName, rancidDeviceName }
       DESCRIPTION "This trap is sent each time rancid detects a node has 
                    been added to the configuration file."

   ::= 5


   rancidNodeDeleted TRAP-TYPE
       ENTERPRISE  rancidTraps
       VARIABLES   { rancidGroupName, rancidDeviceName }
       DESCRIPTION "This trap is sent each time rancid detects a node has 
                    been deleted to the configuration file."
   ::= 6


   rancidConfigurationChanged TRAP-TYPE
       ENTERPRISE  rancidTraps
       VARIABLES   { rancidGroupName, rancidDeviceName }
       DESCRIPTION "This trap is sent each time rancid detects that a 
                    configuration has been changed on a router."
   ::= 7


   END

begin:vcard
fn:Philippe Guillebert
n:Guillebert;Philippe
org:Bull Telecom;Operation Support Systems
title:Engineer
tel;work:+33 1 30 80 61 81
version:2.1
end:vcard

------------------------------------------------------------------------------
This SF.net email is sponsored by:
High Quality Requirements in a Collaborative Environment.
Download a free trial of Rational Requirements Composer Now!
http://p.sf.net/sfu/www-ibm-com
_______________________________________________
Please read the OpenNMS Mailing List FAQ:
http://www.opennms.org/index.php/Mailing_List_FAQ

opennms-devel mailing list

To *unsubscribe* or change your subscription options, see the bottom of this 
page:
https://lists.sourceforge.net/lists/listinfo/opennms-devel

Reply via email to