File: monitor.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 (264 lines) | stat: -rw-r--r-- 7,776 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
# monitor.tcl --
#
# This file contains the implementation of some monitoring
# procedures which usually run in the background and create
# events using the event command. This is work in progress
# so this might change in future versions.
#
# Copyright (c) 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.

package require Tnm
package provide TnmMonitor $tnm(version)

proc TnmGetStatusProc {job status vbl} {
    array set cx [$job attribute status]
    switch $status {
	noError {
	    set value [lindex [lindex $vbl 0] 2]
	    if {$value != $cx(value)} {
		event raise GetStatus:Change $cx(session) $cx(oid) $value
	    }
	    set cx(value) $value
	    $job attribute status [array get cx]
	}
	noSuchName {
	    event raise GetStatus:NoSuchName $cx(session) $cx(oid)
	}
	noResponse {
	    event raise GetStatus:NoResponse $cx(session)
	}
    }
}

proc TnmGetStatusCmd {} {
    set job [job current]
    array set cx [$job attribute status]
    $cx(session) get $cx(oid) \
	    [subst { TnmGetStatusProc "$job" "%E" "%V" } ]
}

proc Tnm_MonitorStatus {s tree seconds} {
    set result ""
    set ms [expr $seconds * 1000]
    foreach var $tree {
	$s walk vbl $var {
	    set cx(session)  [eval snmp session [$s configure]]
	    set cx(oid)	     [lindex [lindex $vbl 0] 0]
	    set cx(value)    [lindex [lindex $vbl 0] 2]
	    set j [job create -command TnmGetStatusCmd -interval $ms]
	    $j attribute status [array get cx]
	    lappend result $j
	}
    }
    return $result
}

#########################################################################

proc TnmGetValueProc {job status vbl} {
    array set cx [$job attribute status]
    switch $status {
	noError {
	    set value [lindex [lindex $vbl 0] 2]
	    event raise GetValue:Value $cx(session) $cx(oid) $value
	}
	noSuchName {
	    event raise GetValue:NoSuchName $cx(session) $cx(oid)
	}
	noResponse {
	    event raise GetValue:NoResponse $cx(session)
	}
    }
}

proc TnmGetValueCmd {} {
    set job [job current]
    array set cx [$job attribute status]
    $cx(session) get $cx(oid) \
	    [subst { TnmGetValueProc "$job" "%E" "%V" } ]
}

proc Tnm_MonitorValue {s tree seconds} {
    set result ""
    set ms [expr $seconds * 1000]
    foreach var $tree {
	$s walk vbl $var {
	    set cx(session)  [eval snmp session [$s configure]]
	    set cx(oid)	     [lindex [lindex $vbl 0] 0]
	    set j [job create -command TnmGetValue -interval $ms]
	    $j attribute status [array get cx]
	    lappend result $j
	}
    }
    return $result
}

#########################################################################

proc TnmGetUpTimeProc {job status vbl} {
    switch $status {
	noError {
	    array set cx [$job attribute status]
	    set uptime [lindex [lindex $vbl 0] 2]
	    if {$uptime < $cx(sysUpTime)} {
		event raise SysUpTime:Restart $cx(session) $uptime
	    }
	    set cx(sysUpTime) $uptime
	    $job attribute status [array get cx]
	}
	noResponse {
	    event raise SysUpTime:NoResponse $cx(session)
	}
    }
}

proc TnmGetUpTimeCmd {} {
    set job [job current]
    array set cx [$job attribute status]
    $cx(session) get sysUpTime.0 \
	    [subst { TnmGetUpTimeProc "$job" "%E" "%V" } ]
}

proc Tnm_MonitorSysUpTime {s seconds} {
    set ms [expr $seconds * 1000]
    set vbl [$s get sysUpTime.0]
    set cx(session)      [eval snmp session [$s configure]]
    set cx(sysUpTime)    [mib scan sysUpTime [lindex [lindex $vbl 0] 2]]
    set j [job create -command TnmGetUpTime -interval $ms]
    $j attribute status [array get cx]
    return $j
}

#########################################################################

#
# Calculate the interface utilisation. This is done using the formula
#
# util = ( 8 * ( delta (ifInOctets, t1, t0) 
#              + delta (ifOutOctets, t1, t0) ) / (t1 - t0) ) / ifSpeed
#
# This formula returns incorrect results for full-duplex point to point
# links. In this case, the following formula should be used:
#
# util = ( 8 * max ( delta (ifInOctets, t1, t0) ,
#                    delta (ifOutOctets, t1, t0) ) / (t1 - t0) ) / ifSpeed
#
# See Simple Times, 1(5), November/December, 1992 for more details.
#

proc TnmGetIfLoadProc {job status vbl} {

    if {$status != "noError"} return

    array set cx [$job attribute status]

    set ifIndex	     $cx(ifIndex)
    set sysUpTime    [mib scan sysUpTime [lindex [lindex $vbl 0] 2]]
    set ifOperStatus [lindex [lindex $vbl 1] 2]
    set ifInOctets   [lindex [lindex $vbl 2] 2]
    set ifOutOctets  [lindex [lindex $vbl 3] 2]
    
    # be careful with Tcl's broken arithmetic
    
    if {[catch {expr $ifInOctets - $cx(ifInOctets)} deltaIn]} {
	set deltaIn  [expr double($ifInOctets) - $cx(ifInOctets)]
    }
    if {[catch {expr $ifOutOctets - $cx(ifOutOctets)} deltaOut]} {
	set deltaOut [expr double($ifOutOctets) - $cx(ifOutOctets)]
    }
    
    if {$cx(fullduplex)} {
	set delta [expr $deltaIn > $deltaOut ? $deltaIn : $deltaOut]
    } else {
	set delta [expr $deltaIn + $deltaOut]
    }
    
    if {$sysUpTime > $cx(sysUpTime) && $cx(ifSpeed) > 0} {
	set secs [expr ($sysUpTime - $cx(sysUpTime)) / 100.0]
	set val  [expr (8.0 * $delta / $secs) / $cx(ifSpeed) * 100]
    } else {
	set val 0
    }

    event raise ifLoad $cx(session) $ifIndex $cx(ifDescr) $ifOperStatus $val
    if {$ifOperStatus != $cx(ifOperStatus)} {
	event raise ifStatusChange $ifIndex $cx(ifDescr) $ifOperStatus
    }

    set cx(sysUpTime)    $sysUpTime
    set cx(ifInOctets)   $ifInOctets
    set cx(ifOutOctets)  $ifOutOctets
    set cx(ifOperStatus) $ifOperStatus
    $job attribute status [array get cx]
}

proc TnmGetIfLoad {} {
    set job [job current]
    array set cx [$job attribute status]
    set i $cx(ifIndex)
    set vbl "sysUpTime.0 ifOperStatus.$i ifInOctets.$i ifOutOctets.$i"
    $cx(session) get $vbl [subst { TnmGetIfLoadProc "$job" "%E" "%V" } ]
}

#
# The following procedure walks the ifTable and starts an interface 
# load monitoring procedure for every interface. We retrieve some 
# initial status information from the agent to initialize the monitor
# jobs.
#

proc Tnm_MonitorIfLoad {s seconds {iterations {}}} {

    set ms [expr $seconds * 1000]
    set result ""

    # The list of full duplex interface types. Note, IANAifType 
    # (RFC 1573) uses slightly different encodings than RFC 1213. 
    # We use RFC 1213 style here.
    
    set fullDuplex {
	regular1822 hdh1822 ddn-x25 rfc877-x25 lapb sdlc ds1 e1 
	basicISDN primaryISDN propPointToPointSerial ppp slip ds3 sip 
	frame-relay
    }

    $s walk vbl ifIndex {
	set ifIndex [lindex [lindex $vbl 0] 2]

	set vbl [$s get [list sysUpTime.0 \
                          ifInOctets.$ifIndex ifOutOctets.$ifIndex \
                          ifSpeed.$ifIndex ifDescr.$ifIndex \
			  ifType.$ifIndex ifOperStatus.$ifIndex]]

	set cx(session)      [eval snmp session [$s configure]]
	set cx(ifIndex)      $ifIndex
	set cx(sysUpTime)    [lindex [lindex $vbl 0] 2]
	set cx(sysUpTime)    [mib scan sysUpTime $cx(sysUpTime)]
	set cx(ifInOctets)   [lindex [lindex $vbl 1] 2]
	set cx(ifOutOctets)  [lindex [lindex $vbl 2] 2]
	set cx(ifSpeed)      [lindex [lindex $vbl 3] 2]
	set cx(ifDescr)      [lindex [lindex $vbl 4] 2]
	set cx(ifType)       [lindex [lindex $vbl 5] 2]
	set cx(ifOperStatus) [lindex [lindex $vbl 6] 2]
	set cx(fullduplex)   [expr [lsearch $fullDuplex $cx(ifType)] < 0]

	set j [job create -command TnmGetIfLoad -interval $ms]
	if {$iterations != ""} {
	    $j configure -iterations $iterations
	}

	$j attribute status [array get cx]
	lappend result $j
    }

    return $result
}

#########################################################################

proc show {args} {
    puts stderr $args
}