Continuously poll MIB variables

From CT3

Jump to: navigation, search

The script continuously polls a MIB variable and prints the value of the specified variable at fixed intervals (default: 500 milliseconds) for the specified amount of time (default: 15 seconds). It can be used to verify whether a MIB variable is changed in real time or periodically.

Contents

Installation

Installation instructions, for example:

  • Download the source file into flash:pm.tcl.
  • Configure alias exec pm tclsh flash:pm.tcl

Usage guidelines

Usage: pm variable [ community [ time [ interval ] ] ]

Command line parameters:

  • Variable: MIB variable to read. Symbolic names recognized by Cisco IOS can be used (for example, ifOutOctets). Names are case-sensitive.
  • Community: SNMP community used in SNMP GET requests (default: public)
  • Time: the measurement time in seconds (default: 15 seconds).
  • Interval: interval between MIB polls (default: 500 milliseconds).

Author

Ivan Pepelnjak, © 2008 NIL Data Communications

Source code

#
# Copyright (c) 2008 NIL Data Communications
# All rights reserved.
#
# by:       Ivan Pepelnjak, NIL Data Communications
# title:    Poll a MIB variable
# name:     pollMib.tcl
# desc:     The script continuously polls a MIB variable and prints
#           the values at fixed intervals for the specified amount of time.
#           It can be used to verify whether a MIB variable is changed in
#           real time or periodically.
#
# ios config:
#
#           * download the file into flash:pm.tcl
#           * configure alias exec pm tclsh flash:pm.tcl
#
#           invoke with poll oid [community [time(sec) [interval(msec)]]]
#
#           unless specified otherwise, the "public" community is used
#

proc snmpGet { oid result } {
  global snmpCommunity
  upvar $result r
  if { [info exists r] } { unset r }

  set getResult [ snmp_getone $snmpCommunity $oid ]
  if { [ regexp {snmp error.*text='(.*)'} $getResult ignore errtxt ] } { 
     error "snmpGet - $errtxt"; return 0 }
  if { [ regexp {oid='(.*)'.*val='(.*)'} $getResult ignore oid result ] } {
    if { ! [ string equal $result "NO_SUCH_INSTANCE_EXCEPTION" ] } {
      set r(OID) $oid ;
      set r(VALUE) $result ; 
      return 1;
    }
  }
  return 0;
}

set oid [lindex $argv 0]
set snmpCommunity [lindex $argv 1]
set maxtime [lindex $argv 2]
set delay   [lindex $argv 3]

if { [string equal $snmpCommunity ""] } { set snmpCommunity "public" }
if { [string equal $oid ""] } { 
  puts {Usage: pollMib.tcl oid [ community [ time(sec) [ interval(msec) ]]]}
  return
}
if { [string equal $maxtime ""] } { set maxtime 15 }
if { [string equal $delay ""] } { set delay 500 }

set maxcnt [ expr { $maxtime * 1000 / $delay } ]
set cnt 0

puts "\npolling $oid for $maxtime seconds ($maxcnt iterations)\n"
while { $cnt < $maxcnt } {
  snmpGet $oid getvar
  puts [format "%6.3f %-30s" [ expr { $delay * $cnt / 1000.0 } ] "$getvar(OID)=$getvar(VALUE)"]
  incr cnt
  if { $cnt != $maxcnt } { after $delay }
}
Personal tools

CT3

Main menu