File: snmp.tcl

package info (click to toggle)
scotty 2.1.9-1
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 9,984 kB
  • ctags: 4,313
  • sloc: ansic: 35,946; sh: 12,591; tcl: 8,122; yacc: 2,442; makefile: 898; lex: 370
file content (449 lines) | stat: -rw-r--r-- 10,963 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
# snmp.tcl --
#
# This file useful utilities for programming SNMP based applications
# in Tcl. Previous versions were written for the tkined(1) editor.
# These versions contain no tkined specific code anymore.
#
# Copyright (c) 1994-1996 Technical University of Braunschweig.
#
# See the file "license.terms" for information on usage and redistribution
# of this file, and for a DISCLAIMER OF ALL WARRANTIES.


# SNMP_Walk --
#
# Walk through a MIB tree and show the contents of the MIB variables.
#
# Arguments:
# s -		The SNMP session to use.
# oid -		The object identifier of the MIB subtree.

proc SNMP_Walk {s oid} {

    set ip [SNMPGetIP $s]
    if {[catch {nslook $ip} host]} { set host "" }
    set host [lindex $host 0]

    writeln "$host \[$ip\] \[[clock format [clock seconds]]\]:"

    set maxl 0
    if {[catch {
	set n 0
	$s walk vbl $oid {
	    set vb [lindex $vbl 0]
	    set name($n)  [mib name [lindex $vb 0]]
	    set value($n) [lindex $vb 2]
	    if {$maxl < [string length $name($n)]} {
		set maxl [string length $name($n)]
	    }
	    incr n
	}
    } msg]} {
	writeln $msg
	return
    }

    set txt ""
    for {set i 0} {$i < $n} {incr i} {
	append txt [format "  %-*s : %s\n" $maxl $name($i) $value($i)]
    }
    writeln $txt
}


# SNMP_Scalars --
#
# Retrieve all scalar variables of a MIB group into an array.
#
# Arguments:
# s -		The SNMP session to use.
# path -	The object identifier of the MIB group.
# name -	The name of an array where we store the values.

proc SNMP_Scalars {s path name} {

    upvar $name value
    catch {unset value}

    # Make sure value is an array and the path is a valid object
    # identifier and not an instance or a table. This is a hack.

    set value(foo) bar
    unset value(foo)
    if {[mib syntax $path] != ""} return

    set exception(noSuchObject)   1
    set exception(noSuchInstance) 1
    set exception(endOfMibView)   1

    # Build a list of all variables in this group that do not have any
    # sucessors (to avoid embedded tables).

    set varlist ""
    foreach var [mib successor [mib oid $path]] {
	if {[mib access $var] != "not-accessible"} {
	    lappend varlist "$var.0"
	}
    }

    # Try to retrieve the varlist in one get request. If this fails,
    # we use a get request to retrieve variables individually.
    # Note, I would like to honor noSuchName or tooBig errors, but
    # unfortunately some agents do not send this error code.

    if {[catch {$s get $varlist} vbl]} {
	set vbl ""
	foreach var $varlist {
	    if {[catch {$s get $var} v]} continue
	    lappend vbl [lindex $v 0]
	}
    }

    # This commented version below does it asynchonously. Fast on 
    # fast reliable networks but problematic if the net has problems.

#    if {[catch {$s get $varlist} vbl]} {
#	global _vbl_
#	foreach var $varlist {
#	    $s get $var {
#		if {"%E" == "noError"} {
#		    lappend _vbl_ [lindex "%V" 0]
#		}
#	    }
#	}
#	$s wait
#	set vbl $_vbl_
#	unset _vbl_
#    }

    set result ""
    foreach vb $vbl {
	set oid [lindex $vb 0]
	set syn [lindex $vb 1]
	set val [lindex $vb 2]
	if {[info exists exception($syn)]} continue
	set name [mib name $oid]
	set value($name) $val
	lappend result $name
    }
    return $result
}


# SNMP_ShowScalars --
#
# Show all scalar variables of a MIB group.
#
# Arguments:
# s -		The SNMP session to use.
# path -	The object identifier of the MIB group.

proc SNMP_ShowScalars {s path} {

    set ip [SNMPGetIP $s]
    if {[catch {nslook $ip} host]} { set host "" }
    set host [lindex $host 0]

    set txt "$host \[$ip\] \[[clock format [clock seconds]]\]:\n"

#    if {[catch {SNMP_Scalars $s $path values} msg]} {
#	append txt "$msg\n"
#	writeln $txt
#	return
#    }

    if {[catch {$s scalars $path values} msg]} {
	append txt "$msg\n"
	writeln $txt
	return
    }

    set maxl 0
    foreach name $msg {
	if {$maxl < [string length $name.0]} {
	    set maxl [string length $name.0]
	}
    }

    foreach name $msg {
	append txt [format "  %-*s : %s\n" $maxl $name.0 $values($name)]
    }
    writeln $txt
}


# SNMP_ShowTable --
#
# Show a complete MIB table. First determine which rows are supported
# by an agent and then walk through the table. The formating code is
# ugly and should be replaced by a better version.
#
# Arguments:
# s -		The SNMP session to use.
# table -	The object identifier of an snmp table.

proc SNMP_ShowTable {s table} {

    set ip [SNMPGetIP $s]
    if {[catch {nslook $ip} host]} { set host "" }
    set host [lindex $host 0]

    set table [mib oid $table]
    if {[mib syntax $table] == "SEQUENCE"} {
	set table [mib parent $table]
    }
    if {[mib syntax $table] != "SEQUENCE OF"} return

    set exception(noSuchObject)   1
    set exception(noSuchInstance) 1
    set exception(endOfMibView)   1

    writeln "$host \[$ip\] \[[clock format [clock seconds]]\]:"

    # Check if we can walk the rows simultaneously. We start a walk and
    # if we get a result, we will be happy. Otherwise, we will start a
    # very silly walk to collect the table element by element.

    set rows ""
    foreach var [mib successor [mib successor $table]] {
	if {[mib access $var] != "not-accessible"} {
	    lappend rows $var
	}
    }
    catch {
	$s walk vbl $rows {
	    set table $rows
	    break
	}
    }

    # Now walk through the table and fill the array named value,
    # indexed by row name : instance identifier, e.g.
    #	value(ifType:1) "le0"
    #	value(ifType:2) "le1"
    # The lists xorder and yorder contain the order of the row
    # names (xorder) and the instance identifier (yorder).

    set xorder ""
    set yorder ""
    if [catch {
	$s walk vbl $table {
	    foreach vb $vbl {
		set oid  [lindex $vb 0]
		set syn  [lindex $vb 1]
		set val  [lindex $vb 2]
		if {[info exists exception($syn)]} continue
		
		set lname [split [mib name $oid] .]
		set pfx [lindex $lname 0]
		set idx [join [lrange $lname 1 end] .]
		
		if {[lsearch $xorder $pfx] < 0} {
		    lappend xorder $pfx
		}
		if {[lsearch $yorder $idx] < 0} {
		    lappend yorder $idx
		}
		set value($pfx:$idx) $val
	    }
	}
    } msg] {
	writeln $msg
	return
    }

    if {![info exists xorder] || ![info exists yorder]} {
	return
    }
    
    # Calculate the max. length of all values for every row.

    foreach pfx $xorder {
	set xindex($pfx) [string length $pfx]
	foreach n [array names value $pfx:*] {
	    set len [string length $value($n)]
	    if {$len > $xindex($pfx)} {
		set xindex($pfx) $len
	    }
	}
    }

    # Try to display the table as a table. This is still no good solution... 

    while {[array names xindex] != ""} {
	set foo ""
	set total 0
	set fmt ""
	set txt ""
	foreach pfx $xorder {
	    incr total $xindex($pfx)
	    incr total 2
	    set fmt "  %$xindex($pfx)s"
	    append txt [format $fmt $pfx]
	    lappend foo $pfx
	}
	writeln $txt
	foreach idx $yorder {
	    set txt ""
	    foreach pfx $foo {
		set fmt "  %$xindex($pfx)s"
		# note, large table may be inconsistend
		if [catch {format $fmt $value($pfx:$idx)} xxx] {
		    set xxx [format $fmt "?"]
		}
		append txt $xxx
	    }
	    writeln $txt
	}
	foreach pfx $foo {
	    unset xindex($pfx) 
	}
    }

    writeln
}


# SNMP_SetValue --
#
# Set a value of a MIB variable.
#
# Arguments:
# s -		The SNMP session to use.
# oid -		The object identifier of the variable to set.
# w -		The root widget name or empty if running with tkined.

proc SNMP_SetValue {s oid {w ""}} {
    $s walk x $oid {
	set x [lindex $x 0]
	set value([mib name [lindex $x 0]]) [lindex $x 2]
    }
    set name [array names value]
    if {[llength $name] > 1} {
	if {$w != ""} {
	    set res [Dialog_Select $w.l questhead "Select an instance:" \
		    [lsort [array names value]] "select cancel"]
	} else {
	    set res [ined list "Select an instance:" \
		    [lsort [array names value]] "select cancel"]
	}
	if {[lindex $res 0] != "select"} return
	set name [lindex $res 1]
    }
    if {$name == ""} return
    if {$w != ""} {
	set res [Dialog_Request $w.r questhead \
		"Enter new value for $name:" $value($name) "ok cancel"]
    } else {
	set res [ined request "Enter new value for $name:" \
		[list [list $name $value($name) entry 20]] "ok cancel"]
    }
    if {[lindex $res 0] != "ok"} return
    set new [lindex $res 1]
    if [catch {$s set [list [list $name $new]]} msg] {
	error $msg
    }
}


# SNMP_CreateInstance --
#
# Set a value of a MIB variable.
#
# Arguments:
# s -		The SNMP session to use.
# oid -		The object identifier of the variable to set.
# w -		The root widget name or empty if running with tkined.

proc SNMP_CreateInstance {s oid {w ""}} {
    if {$w != ""} {
	set res [Dialog_Request $w.l questhead \
		"Enter an instance identifier:" [mib name $oid]. \
		"create cancel"]
    } else {
	set res [ined request "Enter an instance identifier:" \
		[list [list Instance [mib name $oid] entry 20]] \
		"create cancel"]
    }
    if {[lindex $res 0] != "create"} return
    set name [lindex $res 1]
    if {$w != ""} {
	set res [Dialog_Request $w.r questhead \
		"Enter new value for $name:" "" "ok cancel"]
    } else {
	set res [ined request "Enter new value for $name:" \
		[list [list Value "" entry 20]] "ok cancel"]
    }
    if {[lindex $res 0] != "ok"} return
    set new [lindex $res 1]
    if [catch {$s set [list [list $name $new]]} msg] {
	error $msg
    }
}


# SNMP_DescribeMibNode --
#
# Give a description of a node in the MIB tree.
#
# Arguments:
# oid -		The oid of the MIB node to describe.

proc SNMP_DescribeMibNode oid {

    if {[info proc writeln] == ""} {
        proc writeln args { puts [join $args] }
    }
    
    writeln "Descriptor:\t\t[mib name $oid]"
    if {[mib module $oid] != ""} {
	writeln "MIB Module:\t\t[mib module $oid]"
    }
    writeln "Identifier:\t\t[mib oid $oid]"
    if {[mib macro $oid] != ""} {
	writeln "SMI Macro:\t\t[mib macro $oid]"
    }
    set idx [mib index $oid]
    if {$idx != ""} {
	writeln "Table Index:\t\t$idx"
    }
    writeln "Max. Access:\t\t[mib access $oid]"
    set tc [mib tc $oid]
    if {$tc == ""} {
	if {[mib syntax $oid] != ""} {
	    writeln "ASN.1 Syntax:\t\t[mib syntax $oid]"
	}
    } else {
	writeln "ASN.1 Syntax:\t\t[lindex $tc 1]"
	if {[lindex $tc 2] != ""} {
	    writeln "Textual Convention:\t[lindex $tc 0]"
	    writeln "Display-Hint:\t\t[lindex $tc 2]"
	} elseif {[lindex $tc 3] != ""} {
	    writeln "Enumeration:\t\t[join [lindex $tc 3] {, }]"
	} else {
	    writeln "Textual Convention:\t[lindex $tc 0]"
        }
    }
    writeln "File:\t\t\t[mib file $oid]"
    set description [mib description $oid]
    if {$description != ""} {
	writeln "\n$description"
    }
    writeln
}


# SNMPGetIP --
#
# Return the IP destination address of a SNMP session. We also define 
# the writeln proc to a reasonable default if it is not defined yet.
#
# Arguments:
# s -		The SNMP session handle

proc SNMPGetIP {s} {
    if {[info proc writeln] == ""} {
	proc writeln args { puts [join $args] }
    }
    return [$s cget -address]
}