tcl scripting for snmp

Unanswered Question

I am trying to make a script that will give me the uptime of the router and send it to a database. The problem I have, the part of the script that doesn't work, work if I execute it from the tclshell on the router.

tcl
set value [snmp_getone public 1.3.6.1.2.1.1.3.0]
regexp {oid='(.*)'.*val='(.*)'} $value ignore oid _snmp_result
set _snmp_result [expr $_snmp_result]

Has you can see, it work well and give me the information I want.

router#tclsh
router(tcl)#set value [snmp_getone public 1.3.6.1.2.1.1.3.0]
{<obj oid='sysUpTime.0' val='1810190'/>}
stnley600(tcl)#regexp {oid='(.*)'.*val='(.*)'} $value ignore oid _snmp_result
1
router(tcl)#set _snmp_result [expr $_snmp_result]
1810190

But if I execute from a tcl file I have an error that tell me it didn't recognize the command snmp_getone

router#event manager run routeruptime.tcl
invalid command name "snmp_getone"
    while executing
"snmp_getone public 1.3.6.1.2.1.1.3.0"
    invoked from within
"$slave eval $Contents"
    (procedure "eval_script" line 7)
    invoked from within
"eval_script slave $scriptname"
    invoked from within
"if {$security_level == 1} {       #untrusted script
     interp create -safe slave
     interp share {} stdin slave
     interp share {} stdout slave
..."
    (file "tmpsys:/lib/tcl/base.tcl" line 50)
Tcl policy execute failed: invalid command name "snmp_getone"

I am wondering if I need to import something else. Right now, all I have is this

::cisco::eem::event_register_none

namespace import ::cisco::eem::*
namespace import ::cisco::lib::*


I am not a pro in tcl scripting so your help would be very appreciated.

Thanks

I have this problem too.
0 votes
  • 1
  • 2
  • 3
  • 4
  • 5
Overall Rating: 0 (0 ratings)
Loading.
Joe Clarke Thu, 07/15/2010 - 18:14

You are mixing tclsh code with EEM Tcl code.  The snmp_* commands are not available in EEM Tcl.  If you want to get SNMP data in an EEM Tcl policy, you need to use the sys_reqinfo_snmp command.  In your example, this code should work:

array set snmp_res [sys_reqinfo_snmp oid 1.3.6.1.2.1.1.3.0 get_type exact]

set snmp_val $snmp_res(value)

action_syslog msg "Uptime is $snmp_val timeticks"

Actions

This Discussion

Related Content