Debian Codegraph @

local/tkmib

raw
1 #!/usr/bin/perl
2 #!/usr/bin/perl -w
3
4 require 5;
5
6 # attempt to determine if they have the proper modules installed.
7
8 # SNMP
9 my $havesnmp = eval {require SNMP;};
10
11 # the Tk packages
12
13 my $havetk = eval {require Tk;
14 require Tk::Table;
15 require Tk::HList;
16 require Tk::FileSelect;
17 require Tk::Dialog;};
18 if (!$havesnmp) {
19 print "
20 ERROR: You don't have the SNMP perl module installed. Please obtain this by
21 getting the latest source release of the net-snmp toolkit from
22 http://www.net-snmp.org/download/ . The perl module is contained in
23 the perl/SNMP directory. See the INSTALL file there for
24 instructions.
25 ";
26 }
27
28 if (!$havetk) {
29 print "
30 ERROR: You don't have the Tk module installed. You should be able to
31 install this by running (as root):
32
33 perl -MCPAN -e 'install Tk'
34 ";
35 }
36
37 if (!$havetk || !$havesnmp) {
38 print "\n";
39 exit;
40 }
41
42 if ($havetk) {
43 # Tk doesn't seem to like require so we force use here.
44 eval {import Tk;
45 import Tk::Table;
46 import Tk::HList;
47 import Tk::FileSelect;
48 import Tk::Dialog;
49 import SNMP;};
50 }
51
52 use Getopt::Std;
53 use Data::Dumper;
54
55 $host = 'localhost';
56 $OID = '.1.3.6.1';
57 $opts{'f'} = $ENV{'HOME'} . "/.snmp/tkmibrc";
58
59 getopts("hp:v:a:A:x:X:n:u:l:r:t:o:c:Cf:", \%opts);
60
61 # default session options
62 print "setting opts\n";
63 %session_opts = (
64 'Community' => "public",
65 'RemotePort' => 161,
66 'Timeout' => 5000000,
67 'Retries' => 5,
68 'Version' => 1,
69 'AuthProto' => 'MD5',
70 'PrivProto' => 'DES',
71 'AuthPass' => '',
72 'PrivPass' => '',
73 'Context' => '',
74 'SecName' => 'initial',
75 'SecLevel' => 'authNoPriv',
76 );
77
78 sub usage {
79 print "
80 tkmib [-C] [-o OID] [SNMPCMD arguments] [host]
81 -f CONFIG_FILE load CONFIG_FILE after starting up. (default: ~/.snmp/tkmibrc)
82 (use -f /dev/null to not read one).
83
84 See the snmpcmd manual page for related SNMPCMD arguments. (Not all
85 options are currently supported.)
86 ";
87 exit();
88 }
89
90 usage() if ($opts{'h'});
91
92 # initialize defaults, may be overridden by config file below
93 @displayInfo=qw(type access status units hint moduleID enums indexes);
94 @saveoptions = ('displayoidas', 'writecolor', 'graphtime', 'graphdelta');
95 $displayoidas='full';
96 $writecolor = "blue";
97 $graphtime=5;
98 $graphdelta=1;
99 foreach $i (@displayInfo) {
100 $displayInfoStates{$i} = 1;
101 }
102
103
104 # source config file
105 do $opts{'f'} if ($opts{'f'} && -f $opts{'f'});
106
107 $session_opts{'UseLongNames'} => 1;
108 $session_opts{'RemotePort'} = $opts{'p'} if ($opts{'p'});
109 $session_opts{'Community'} = $opts{'c'} if ($opts{'c'});
110 $session_opts{'Version'} = $opts{'v'} if ($opts{'v'});
111 $session_opts{'AuthProto'} = $opts{'a'} if ($opts{'a'});
112 $session_opts{'AuthPass'} = $opts{'A'} if ($opts{'A'});
113 $session_opts{'PrivProto'} = $opts{'x'} if ($opts{'x'});
114 $session_opts{'PrivPass'} = $opts{'X'} if ($opts{'X'});
115 $session_opts{'Context'} = $opts{'n'} if ($opts{'n'});
116 $session_opts{'SecName'} = $opts{'u'} if ($opts{'u'});
117 $session_opts{'SecLevel'} = $opts{'l'} if ($opts{'l'});
118 $session_opts{'Retries'} = $opts{'r'} if ($opts{'r'});
119 $session_opts{'Timeout'} = $opts{'t'} if ($opts{'t'});
120
121 $host = shift if ($#ARGV > -1);
122 $session_opts{'Community'} = shift if ($#ARGV > -1);
123
124 @graphcolors=qw(blue red green yellow purple);
125
126 # initialize SNMP module
127 $SNMP::save_descriptions=1;
128 $SNMP::use_long_names=1;
129 $SNMP::use_enums=1;
130 $SNMP::verbose = 1;
131 my $tmpbd = 1;
132
133 $top = MainWindow->new();
134 $top->title("tkmib");
135
136 #Menus
137 $MenuFrame = $top->Frame(-relief => "raised",-borderwidth => 2);
138 $MenuFrame->pack(-fill => "x",-expand => 1);
139 $FileMenuBut = $MenuFrame->Menubutton(-pady => $tmpbd, -padx => $tmpbd, -text => "File",
140 -menuitems =>
141 [
142 # [Button => "Save Output", -command => [\&saveOutput]],
143 [Button => "Quit", -command => [\&exit]]
144 ]);
145 $FileMenuBut->pack(-side => 'left');
146
147 $MibMenuBut = $MenuFrame->Menubutton(-pady => $tmpbd, -padx => $tmpbd, -text => "Mib",
148 -menuitems =>
149 [[Button => "Find a mib node",
150 -command => sub { my $var;
151 entryBox("Find a Mib Node",
152 "Enter a mib node name to search for:",
153 \$var, \&findANode );}],
154 [Button => "Load a New Mib File", -command => [\&loadNewMibFile]],
155 [Button => "Load a New Mib Module",
156 -command => sub { my $var;
157 entryBox("Load a Module",
158 "Enter a SNMP MIB module name to load:",
159 \$var, \&loadIt);}]
160 ]);
161 $MibMenuBut->pack(-side => 'left');
162
163 $OptMenuBut = $MenuFrame->Menubutton(-pady => $tmpbd, -padx => $tmpbd, -text => "Options",
164 -menuitems =>
165 [[Cascade => "~Display", -menuitems =>
166 [
167 [Cascade => "~MIB Information"],
168 [Cascade => "~OID Display", -menuitems =>
169 [
170 [Radiobutton => 'full', -variable => \$displayoidas],
171 [Radiobutton => 'numeric', -variable => \$displayoidas],
172 [Radiobutton => 'short', -variable => \$displayoidas],
173 [Radiobutton => 'module', -variable => \$displayoidas]
174 ]
175 ],
176 [Button => "Writable Color",
177 -command => [\&entryBox,"Writable Color",
178 "Color for writable objects:",
179 \$writecolor]]
180 ]],
181 [Cascade => "Use SNMP Version", -menuitems =>
182 [
183 [Radiobutton => '1', -variable => \$session_opts{'Version'}],
184 [Radiobutton => '2c', -variable => \$session_opts{'Version'}],
185 [Radiobutton => '3', -variable => \$session_opts{'Version'}]
186 ]
187 ], # ends version number specification
188 [Cascade => "SNMPv1/2c options", -menuitems =>
189 [
190 [Button => "Community Name",
191 -command => [\&entryBox,"Community Name", "Community name to use:",
192 \$session_opts{'Community'}]]
193 ]
194 ],
195 [Cascade => "SNMP3 options", -menuitems =>
196 [
197 [Button => "Security Name",
198 -command => [\&entryBox,"Security Name", "Security Name to use:",
199 \$session_opts{'SecName'}]],
200 [Cascade => "Security Level", -menuitems =>
201 [
202 [Radiobutton => 'noAuthNoPriv',
203 -variable => \$session_opts{'SecLevel'}],
204 [Radiobutton => 'authNoPriv',
205 -variable => \$session_opts{'SecLevel'}],
206 [Radiobutton => 'authPriv',
207 -variable => \$session_opts{'SecLevel'}]
208 ]
209 ],
210 [Button => "Authentication Passphrase",
211 -command => [\&entryBox,"Authentication Passphrase",
212 "Authentication Passphrase to use:",
213 \$session_opts{'AuthPass'}]],
214 [Cascade => "Authentication Type", -menuitems =>
215 [
216 [Radiobutton => 'MD5',
217 -variable => \$session_opts{'AuthProto'}],
218 [Radiobutton => 'SHA',
219 -variable => \$session_opts{'AuthProto'}],
220 ]
221 ],
222 [Button => "Privacy Passphrase",
223 -command => [\&entryBox,"Privacy Passphrase",
224 "Privacy Passphrase to use:",
225 \$session_opts{'PrivPass'}]],
226 [Cascade => "Privacy Type", -menuitems =>
227 [
228 [Radiobutton => 'DES',
229 -variable => \$session_opts{'PrivProto'}],
230 ]
231 ],
232 ]
233 ],
234 [Button => "Time between graph polls",
235 -command => sub { entryBox("graph polls", "Time between graph polls:",
236 \$graphtime);}],
237 [Button => "Port number",
238 -command => sub { entryBox("Port Number", "SNMP Port number to use:",
239 \$session_opts{'RemotePort'});}],
240 [Button => "TimeOut",
241 -command => sub { entryBox("Time Out", "Timeout for SNMP requests:",
242 \$session_opts{'Timeout'});}],
243 [Button => "Retries",
244 -command => sub { entryBox("Retries",
245 "Number of Times to Retransmit Requests:",
246 \$session_opts{'Retries'});}],
247 [Button => "Save Options",
248 -command => \&save_options]
249 ])->pack(-side => 'left');
250
251 $tmp = $OptMenuBut->cget(-menu);
252 $OptMenuWidgets = $tmp->entrycget("Display", -menu);
253 $OptMenuWidgets = $OptMenuWidgets->entrycget("MIB Information", -menu);
254
255 $hlist=$top->Scrolled(qw(HList -itemtype imagetext -browsecmd main::showInfo
256 -command main::showChildren -width 80 -height 15));
257 $hlist->pack(-side => 'top', -expand => 1, -fill => 'both');
258 my $sFrame = $top->Frame(-relief => 'raised', -borderwidth => $tmpbd);
259 $sFrame->pack(-side => 'top', -fill => 'x');
260 $sFrame->Label(-pady => $tmpbd, -padx => $tmpbd, -text => 'OID: ', -relief => 'raised', -borderwidth => $tmpbd)
261 ->pack(-side => 'left');
262 $mibOID = $sFrame->Entry(-textvariable => \$OID, -relief => 'flat', -width => 40);
263 $mibOID->pack(-side => 'left');
264 $mibTextOID = $sFrame->Label(-pady => $tmpbd, -padx => $tmpbd, -text => '');
265 $mibTextOID->pack(-side => 'right');
266
267 $dispFrame=$top->Frame(-relief => 'raised', -borderwidth => $tmpbd);
268 $dispFrame->pack(-side => 'top', -fill =>'x');
269 for($i=0;$i<= $#displayInfo;$i++) {
270 createRow($i) if ($displayInfoStates{$displayInfo[$i]});
271 optionalWidget($i,$OptMenuWidgets, \$displayInfoStates{$displayInfo[$i]});
272 }
273
274 $descrFrame=$top->Frame(-relief => 'raised', -borderwidth => $tmpbd);
275 $descrFrame->pack(-side => 'top', -fill =>'x');
276 $descrFrame->Label(-pady => $tmpbd, -padx => $tmpbd, -text => 'Description:', -anchor => 'w')->pack(-side => 'top',
277 -fill => 'x');
278 $descr = $descrFrame->Scrolled(qw(Text -width 80 -height 4));
279 $descr->pack(-side => 'top', -fill => 'x');
280
281 $bFrame = $top->Frame(-relief => 'raised', -borderwidth => $tmpbd);
282 $bFrame->pack(-side => 'top', -fill => 'x');
283 $hostEntry = $bFrame->Entry(-textvariable => \$host, -width => 12);
284 $hostEntry->pack(-side => 'left');
285 $bFrame->Button(-pady => $tmpbd, -padx => $tmpbd, -text => 'graph', -command => \&snmpgraph)->pack(-side => 'right');
286 $tablebutton = $bFrame->Button(-pady => $tmpbd, -padx => $tmpbd, -text => 'table', -command => \&snmptable);
287 $tablebutton->pack(-side => 'right');
288 $bFrame->Button(-pady => $tmpbd, -padx => $tmpbd, -text => 'walk', -command => \&snmpwalk)->pack(-side => 'right');
289 $bFrame->Button(-pady => $tmpbd, -padx => $tmpbd, -text => 'getnext', -command => \&snmpgetnext)->pack(-side => 'right');
290 $bFrame->Button(-pady => $tmpbd, -padx => $tmpbd, -text => 'get', -command => \&snmpget)->pack(-side => 'right');
291 $bFrame->Button(-pady => $tmpbd, -padx => $tmpbd, -text => 'set', -command => [\&snmpsetbegin, 'OID'])->pack(-side => 'right');
292 $stopBut = $bFrame->Button(-pady => $tmpbd, -padx => $tmpbd, -text => 'stop', -command => sub { stop(1) },
293 -state => 'disabled');
294 $stopBut->pack(-side => 'right');
295 $oFrame = $top->Frame(-borderwidth => $tmpbd, -relief => 'raised');
296 $oFrame->pack(-side => 'top', -fill => 'both');
297 $output = $oFrame->Scrolled(qw(Text -width 80 -height 14));
298 $output->pack(-side => 'top', -fill => 'both', -expand => 1);
299
300 $tmpFrame = $top->Frame(-relief => 'raised', -borderwidth => $tmpbd);
301 $tmpFrame->pack(-side => 'top', -fill => 'x');
302 $tmpFrame->Label(-pady => $tmpbd, -padx => $tmpbd, -text => "Status: ", -anchor => 'w')
303 # -relief => 'raised', -borderwidth => $tmpbd)
304 ->pack(-side => 'left');
305 $status = $tmpFrame->Label(-pady => $tmpbd, -padx => $tmpbd, -anchor => 'w');
306 $status->pack(-side => 'left', -fill => 'x');
307
308 # initialize the browser
309 foreach $i (qw(.1 .1.3 .1.3.6 .1.3.6.1)) {
310 addMibOID($i);
311 }
312 showChildren("$OID");
313 if (defined($opts{'o'})) {
314 findANode($opts{'o'});
315 }
316
317 MainLoop();
318
319 sub insertresult {
320 my $oid = shift;
321 my $val = shift;
322 $oid = $OID if ($oid eq "OID");
323 $output->insert('end', $oid, "oid:$oid");
324 $output->tagBind("oid:$oid", '<1>', [sub{shift;
325 my $oid = shift;
326 findANode($oid);
327 my $tag = SNMP::translateObj($oid);
328 showInfo($tag);},$oid]);
329 $output->insert('end', " = ");
330 my $mib = $SNMP::MIB{format_oid("$oid",'numeric')};
331 $output->insert('end', $val, "value:$oid");
332 if ($mib->{'access'} =~ /(Write|Create)/) {
333 $output->tagConfigure("value:$oid", -foreground => $writecolor);
334 $output->tagBind("value:$oid", '<1>', [sub{shift;
335 my $oid = shift;
336 my $value = shift;
337 snmpsetmaybebegin($oid, $value);
338 findANode($oid);
339 my $tag = SNMP::translateObj($oid);
340 showInfo($tag);},format_oid($oid,'full'), $val]);
341 }
342 $output->insert('end', "\n");
343 }
344
345 sub insertvar {
346 my $var = shift;
347 my $name = get_oid($var);
348
349 insertresult($name,SNMP::Varbind::val($var));
350 }
351
352 sub snmpsetup {
353 my $oid = $OID;
354 my $tag = SNMP::translateObj($oid);
355 my $sess = new SNMP::Session(DestHost => $host, %session_opts);
356 my $var = new SNMP::Varbind([$oid]);
357 if (!defined($var)) {
358 print "ack: $@ $SNMP::ErrorStr $!\n";
359 }
360 stop(0);
361 initText();
362 $oid = "." . $oid if ($oid !~ /^\./);
363 return ($oid, $sess, $var);
364 }
365
366 sub initText {
367 if (ref($output) eq "Tk::Frame" && defined($$output{'_#text'})) {
368 $output->delete('0.0','end');
369 } else {
370 $output->destroy();
371 $output = $oFrame->Scrolled(qw(Text -width 80 -height 14));
372 $output->pack(-side => 'top', -fill => 'both', -expand => 1);
373 }
374 }
375
376 sub initTable {
377 $output->destroy();
378 $oFrame->packPropagate(0);
379 $output = $oFrame->Table(-columns => shift, -width => 80, -height => 14,
380 -fixedrows => 2, -fixedcolumns => 1);
381 $output->pack(-side => 'top', -fill => 'both', -expand => 1);
382 }
383
384 sub initCanvas {
385 $output->destroy();
386 $oFrame->packPropagate(0);
387 $output = $oFrame->Scrolled(qw(Canvas -width 80c -height 14c));
388 $output->pack(-side => 'top', -fill => 'both', -expand => 1);
389 }
390
391 sub snmpget {
392 (my $oid, my $sess, my $var) = snmpsetup();
393 $status->configure(-text => "getting: $host $community $oid");
394 $top->update();
395 my $val = $sess->get($var);
396 if ($sess->{ErrorStr}) {
397 $status->configure(-text => $sess->{ErrorStr});
398 } else {
399 insertvar($var);
400 $status->configure(-text => "");
401 }
402 }
403
404 sub snmpsetbegin {
405 my $startoid = shift;
406 my $startval = shift;
407 my $setwin = MainWindow->new();
408 $setwin->title("SNMP set");
409 my $varswin = $setwin->Frame(-relief => "raised",-borderwidth => $tmpbd);
410 my $vars = new SNMP::VarList;
411 $varswin->pack(-side => 'top');
412 my $buttons = $setwin->Frame(-relief => "raised")->pack(-side => 'top', -fill => "x",-expand => 1);
413 $buttons->Button(-pady => $tmpbd, -padx => $tmpbd, -text => 'Add a varbind', -command => [\&snmpsetbegin_addvar, $vars, $varswin, 'OID'])->pack(-side => 'left', -fill => "x",-expand => 1);
414 $buttons->Button(-pady => $tmpbd, -padx => $tmpbd, -text => 'perform set', -command => [\&snmpsetbegin_ok, $vars, $setwin, $varswin])->pack(-side => 'left');
415 $buttons->Button(-pady => $tmpbd, -padx => $tmpbd, -text => 'Cancel', -command => [sub { my $widget = shift; $varswin = shift; if ($setmain == $varswin) { $setmain = undef; } $widget->destroy();}, $setwin, $varswin])->pack(-side => 'right');
416 if ($startoid ne "") {
417 snmpsetbegin_addvar($vars, $varswin, $startoid, $startval);
418 }
419 if (!$setmain) {
420 $setmain = $varswin;
421 $setvars = $vars;
422 }
423 }
424
425 sub make_enum_button {
426 my $win = shift;
427 my $var = shift;
428 my @objs;
429 foreach my $i (@_) {
430 push @objs,[Radiobutton => $i, -variable => $var];
431 }
432 return $win->Menubutton(-pady => $tmpbd, -padx => $tmpbd, -textvariable => $var,
433 -relief => raised,
434 -menuitems => \@objs);
435 }
436
437 sub snmpsetmaybebegin {
438 my ($oid, $val) = @_;
439 if ($setmain) {
440 snmpsetbegin_addvar($setvars, $setmain, $oid, $val);
441 } else {
442 snmpsetbegin($oid, $val);
443 }
444 }
445
446
447 sub snmpsetbegin_addvar {
448 my ($vars, $place, $oid, $val) = @_;
449 $oid = $OID if ($oid eq "OID");
450 my $mib = $SNMP::MIB{format_oid("$oid",'numeric')};
451 my $var = new SNMP::Varbind([$oid, '', $val, $mib->{'type'} || 'INTEGER']);
452 push @$vars,$var;
453 my $frame = $place->Frame();
454 $frame->Entry(-textvariable => \$var->[0], -width => 20)->pack(-side => 'left');
455 make_enum_button($frame, \$var->[3], qw(OBJECTID OCTETSTR INTEGER NETADDR IPADDR COUNTER COUNTER64 GAUGE UINTEGER TICKS OPAQUE NULL))->pack(-side => 'left');
456 if (ref($mib->{'enums'}) eq HASH && scalar(keys(%{$mib->{'enums'}})) > 0) {
457 make_enum_button($frame, \$var->[2], keys(%{$mib->{'enums'}}))->pack(-side => 'left');
458 } else {
459 $frame->Entry(-textvariable => \$var->[2])->pack(-side => 'left');
460 }
461 $frame->pack(-expand => 1, -fill => 'x');
462 }
463
464 sub snmpsetbegin_ok {
465 my ($vars, $win, $frame) = @_;
466 snmpset($vars);
467 $setmain = undef if ($setmain == $frame);
468 $win->destroy();
469 }
470
471 sub snmpset {
472 my $vars = shift;
473 (my $oid, my $sess, my $var) = snmpsetup();
474 $status->configure(-text => "setting: $host -> " . Dumper($vars) . "\n");
475 $top->update();
476 my $val = $sess->set($vars);
477 if ($sess->{ErrorStr}) {
478 $output->insert('end', "Set failed.\nReason: $sess->{ErrorStr}");
479 $status->configure(-text => $sess->{ErrorStr});
480 } else {
481 foreach my $i (@$vars) {
482 insertvar($i);
483 }
484 $status->configure(-text => "");
485 }
486 }
487
488 sub snmpgetnext {
489 (my $oid, my $sess, my $var) = snmpsetup();
490 $status->configure(-text => "get next: $host $community $oid");
491 $top->update();
492 my $val = $sess->getnext($var);
493 if ($sess->{ErrorStr}) {
494 $status->configure(-text => $sess->{ErrorStr});
495 } else {
496 insertvar($var);
497 $status->configure(-text => "");
498 }
499 }
500
501 sub snmpwalk {
502 (my $oid, my $sess, my $var) = snmpsetup();
503 $status->configure(-text => "walking: $host $community $oid");
504 $top->update();
505 while (!$sess->{ErrorStr} && !$stopit) {
506 my $val = $sess->getnext($var);
507 last if (!defined($var->tag) ||
508 $sess->{ErrorStr} ||
509 $val eq "ENDOFMIBVIEW" ||
510 !is_in_subtree($oid, $var->tag . "." . $var->iid));
511 insertvar($var);
512 $top->update();
513 }
514 if ($sess->{ErrorStr}) {
515 $status->configure(-text => $sess->{ErrorStr});
516 $output->insert('end',"$sess->{ErrorStr} ($sess->{ErrorNum})\n");
517 } else {
518 $status->configure(-text => "");
519 }
520 stop(1);
521 }
522
523 sub snmptable {
524 (my $oid, my $sess, my $var) = snmpsetup();
525 $status->configure(-text => "collecting data: $host $community $oid");
526 $top->update();
527 my (%tb, @tags, @index, %tboids);
528 while (!$sess->{ErrorStr} && !$stopit) {
529 my $val = $sess->getnext($var);
530 last if (!defined($var->tag) ||
531 $sess->{ErrorStr} ||
532 $val eq "ENDOFMIBVIEW" ||
533 !is_in_subtree($oid, $var->tag . "." . $var->iid));
534 $newoid = SNMP::Varbind::tag($var).".".SNMP::Varbind::iid($var);
535 insertvar($var);
536 $top->update();
537 $newoid =~ /([^\.]+)\.([0-9\.]+)$/;
538 if (!grep(/$1/,@tags)) {
539 push @tags,$1;
540 }
541 if (!grep(/$2/,@index)) {
542 push @index,$2;
543 }
544 $tb{$2}{$1} = $var->val;
545 # $tboids{$2}{$1} = $var->tag;
546 $tboids{$2}{$1} = $newoid;
547 }
548 initTable($#tags+1);
549 for(my $k=0;$k <= $#tags;$k++) {
550 $output->put(1,$k+2,$tags[$k]);
551 }
552 $output->put(1,1,"Index");
553 for(my $i=0;$i <= $#index;$i++) {
554 $output->put($i+2,1,$index[$i]);
555 }
556 for(my $i=0;$i <= $#index; $i++) {
557 for(my $k=0;$k <= $#tags;$k++) {
558 my $mib = $SNMP::MIB{format_oid("$tboids{$index[$i]}{$tags[$k]}",'numeric')};
559 if ($mib->{'access'} =~ /(Write|Create)/) {
560 $output->put($i+2,$k+2,$output->Button(-fg => $writecolor, -pady => $tmpbd, -padx => $tmpbd, -text => $tb{$index[$i]}{$tags[$k]}, -command => [\&snmpsetmaybebegin, $tboids{$index[$i]}{$tags[$k]}, $tb{$index[$i]}{$tags[$k]}], -padx => 0, -pady => 0));
561 } else {
562 $output->put($i+2,$k+2,$tb{$index[$i]}{$tags[$k]});
563 }
564 }
565 }
566 $status->configure(-text => "");
567 stop(1);
568 }
569
570 sub snmpgraph {
571 ($graphoid, $graphsess, my $graphvar) = snmpsetup();
572 $top->update();
573 %graphtb = ();
574 @graphvars = ();
575 initCanvas();
576 $gcount=0;
577 $max=-1;
578 $min=2**32-1;
579 updateGraph();
580 $output->repeat($graphtime*1000, \&updateGraph);
581 }
582
583 sub updateGraph() {
584 $status->configure(-text => "collecting data: $host $community $graphoid");
585 my $oid = $graphoid;
586 my $tag = SNMP::translateObj($graphoid,0);
587 my $var = new SNMP::Varbind([$oid]);
588 $graphsess->{ErrorStr} = "";
589 while (!$graphsess->{ErrorStr} && !$stopit) {
590 my $val = $graphsess->getnext($var);
591 if ($#graphvars == -1 && SNMP::translateObj($var->tag) !~ /^$oid/) {
592 # if an exact oid, do a get instead.
593 $var = new SNMP::Varbind([$oid]);
594 $val = $graphsess->get($var);
595 }
596 if ($graphsess->{ErrorStr} ||
597 !defined($var->tag) ||
598 SNMP::translateObj($var->tag) !~ /^$oid/) {
599 last;
600 }
601 my $newoid = SNMP::translateObj(SNMP::Varbind::tag($var).".".SNMP::Varbind::iid($var));
602 $top->update();
603 $newoid =~ /$oid\.([0-9\.]+)$/;
604 if (defined($1)) {
605 if (!grep(/$1/,@graphvars)) {
606 push @graphvars,$1;
607 }
608 if ($graphdelta) {
609 if ($gcount > 0) {
610 $graphtb{$1}[$gcount-1] = $var->val - $prev{$1};
611 }
612 $prev{$1} = $var->val;
613 } else {
614 $graphtb{$1}[$gcount] = $var->val;
615 }
616 $max = $graphtb{$1}[$#{$graphtb{$1}}]
617 if ($#{$graphtb{$1}} >= 0 &&
618 $graphtb{$1}[$#{$graphtb{$1}}] > $max);
619 $min = $graphtb{$1}[$#{$graphtb{$1}}]
620 if ($#{$graphtb{$1}} >= 0 &&
621 $graphtb{$1}[$#{$graphtb{$1}}] < $min);
622 }
623 }
624 if ($gcount > 1) {
625 $output->delete('all');
626 my $canvas = $$output{'SubWidget'}{'canvas'};
627 my $h=$canvas->cget(-height);
628 foreach $i (@graphvars) {
629 my @a = ();
630 for(my $j=0; $j <= $#{$graphtb{$i}}; $j++) {
631 $a[$j*2] = $j;
632 $a[$j*2+1] = $h-(($h-3)*($graphtb{$i}[$j]-$min))/($max-$min)-3;
633 }
634 $output->createLine(@a, -fill => $graphcolors[$i%$#graphcolors]);
635 }
636 $output->create('text',5, $h-3, -text => "$max");
637 $output->create('text',5, 3, -text => "$min");
638 }
639 $gcount++;
640 $status->configure(-text => "sleeping for $graphtime seconds");
641 }
642
643 sub addMibOID {
644 my $i = shift;
645 $i = ".$i" if ($i !~ /^\./);
646 my $name = SNMP::translateObj($i,1);
647 if (defined($name)) {
648 $name =~ s/.*\.([^.]+)$/$1/;
649 } else {
650 return;
651 }
652 $i =~ s/^\.//;
653 $hlist->add($i, -text => $name);
654 }
655
656 sub showInfo {
657 my $full = shift;
658 $full = ".$full" if ($full !~ /^\./);
659 my $oid = $full;
660 my $tag = $oid;
661
662 if ($tag =~ /^[.0-9]+$/) {
663 # strip off index in case there is one
664 $tag = SNMP::translateObj("$oid");
665 $tag = ".iso.org.dod.internet.private.$tag" if $tag =~ /^enterprises/;
666 } else {
667 $full = SNMP::translateObj("$oid");
668 }
669
670 $tag =~ s/\.[.0-9]+$//;
671 $oid = SNMP::translateObj($tag);
672
673 if (!defined($last) || "$last" ne $oid) {
674 updateInfo($oid);
675 }
676 $OID = $full;
677 $mibOID->configure(-textvariable => \$OID);
678 $mibOID->update();
679 $last = $oid;
680 }
681
682 sub showAllChildren {
683 my $id = shift;
684 $id =~ s/^\.//;
685 my @pieces = split(/\./,$id);
686 my ($i, $lastvalid);
687 for($i = 0; $i <= $#pieces; $i++) {
688 my $a = join(".", @pieces[0..$i]);
689 if ($hlist->infoExists($a) && !($hlist->infoChildren($a))) {
690 showChildren(join(".", $a));
691 }
692 if ($hlist->infoExists($a)) {
693 $lastvalid = $a;
694 } else {
695 last;
696 }
697 }
698 $hlist->see($lastvalid);
699 $hlist->selectionClear($hlist->selectionGet);
700 $hlist->selectionSet($lastvalid);
701 }
702
703 sub showChildren {
704 $OID = shift;
705 $OID =~ s/^\.//;
706 my $oid = $OID;
707 $mibOID->configure(-textvariable => \$OID);
708 if ($hlist->infoChildren($oid)) {
709 my @a = $hlist->infoChildren($oid);
710 my $i;
711 foreach $i (@a) {
712 $hlist->deleteEntry($i);
713 }
714 } else {
715 $oid = ".$oid";
716 my $mib = $SNMP::MIB{format_oid($oid,'full')};
717 if (defined($mib)) {
718 my $children = $$mib{'children'};
719 if (ref($children) eq "ARRAY") {
720 foreach $i (sort {$$a{'subID'} <=> $$b{'subID'}} @{$children}) {
721 addMibOID($$i{'objectID'});
722 }
723 } else {
724 $status->configure(-text => SNMP::translateObj($oid,1) .
725 " has no children");
726 return;
727 }
728 }
729 }
730 $status->configure(-text => "");
731 }
732
733 sub updateInfo {
734 $OID = shift;
735 my $oid = $OID;
736 my $mib = $SNMP::MIB{format_oid("$oid",'numeric')};
737 if (!defined($mib->{'description'}) || $mib->{'description'} eq "") {
738 $oid =~ s/[.0-9]+$//;
739 $mib = $SNMP::MIB{format_oid("$oid",'numeric')};
740 }
741 if (defined($mib)) {
742 if ($mib->{'label'} =~ /Table$/) {
743 $tablebutton->configure(-state => 'normal');
744 } else {
745 $tablebutton->configure(-state => 'disabled');
746 }
747 $mibOID->configure(-text => $mib->{'objectID'});
748 $mibTextOID->configure(-text =>
749 SNMP::translateObj($mib->{'objectID'},1));
750 $descr->delete('0.0','end');
751 if (defined($mib->{'description'}) &&
752 $mib->{'description'} ne "") {
753 my $desc = $mib->{'description'};
754 $desc =~ s/\n[ \t]+/\n/g;
755 $desc =~ s/^\n//;
756 $descr->insert('end',$desc);
757 }
758 for($i=0; $i<= $#displayInfo;$i++) {
759 $dpyInfo[$i] = $mib->{$displayInfo[$i]};
760 if (ref($dpyInfo[$i]) eq HASH) {
761 my %hash = %{$dpyInfo[$i]};
762 $dpyInfo[$i] = "";
763 foreach $j (sort { $hash{$a} <=> $hash{$b} } keys(%hash)) {
764 $dpyInfo[$i] .= "$j = $hash{$j},";
765 }
766 } elsif (ref($dpyInfo[$i]) eq ARRAY) {
767 $dpyInfo[$i] = join(", ", @{$dpyInfo[$i]});
768 }
769 }
770 }
771 }
772
773 sub optionalWidget {
774 my $num = shift;
775 my $menu = shift;
776 my $var = shift;
777 $menu->checkbutton(-label => $displayInfo[$num],
778 -variable => $var,
779 -command => [\&toggleWidgetShown, $num, $var]);
780 }
781
782 sub createRow {
783 my $i = shift;
784 if (!$displayLabels[$i]) {
785 $displayLabels[$i] = $dispFrame->Label(-pady => $tmpbd, -padx => $tmpbd,
786 -text => $displayInfo[$i],
787 -anchor => 'w',
788 -borderwidth => $tmpbd);
789 }
790 if (!$displayEntries[$i]) {
791 $displayEntries[$i] = $dispFrame->Entry(-textvariable => \$dpyInfo[$i],
792 -width => 40, -relief => 'flat',
793 -borderwidth => $tmpbd);
794 }
795 $displayLabels[$i]->grid(-ipady => $tmpbd, -ipadx => $tmpbd,
796 -column => ($i%2)*2, -row => int($i/2),
797 -sticky => 'w');
798 $dpyInfo[$i] = "" if (!defined($dpyInfo[$i]));
799 $displayEntries[$i]->grid(-ipady => $tmpbd, -ipadx => $tmpbd, -column => ($i%2)*2 + 1, -row => int($i/2), -sticky => 'w');
800 }
801
802 sub toggleWidgetShown {
803 my ($num, $var) = @_;
804 if ($$var) {
805 createRow($num);
806 } else {
807 $displayLabels[$num]->gridForget();
808 $displayEntries[$num]->gridForget()
809 }
810 # my @widgets = $dispFrame->gridSlaves(-row => $num);
811 }
812
813 sub loadNewMibFile {
814 my $sel = $top->FileSelect();
815 my $file = $sel->Show();
816 if (defined($file)) {
817 SNMP::addMibFiles($file);
818 showChildren("1.3.6.1");
819 showChildren("1.3.6.1");
820 }
821 }
822
823 sub loadNewMibModule {
824 my $tmptop = MainWindow->new();
825 my $var = "";
826 $tmptop->Label(-pady => $tmpbd, -padx => $tmpbd, -text => "Enter a SNMP MIB module name")
827 ->pack(-side => 'top');
828 my $e = $tmptop->Entry(-textvariable => \$var);
829 $e->pack(-side => 'top');
830 $e->bind('<Return>',[\&loadIt,\$var,$tmptop]);
831 my $f = $tmptop->Frame();
832 $f->pack(-side => 'top');
833 $f->Button(-pady => $tmpbd, -padx => $tmpbd, -text => 'Ok', -command => [\&loadIt,"",\$var,$tmptop])
834 ->pack(-side => 'left');
835 $f->Button(-pady => $tmpbd, -padx => $tmpbd, -text => 'Cancel', -command => [sub { my $wid = shift;
836 $wid->destroy(); },
837 $tmptop])
838 ->pack(-side => 'left');
839 }
840
841 sub loadIt {
842 my $var = shift;
843 if ($var ne "") {
844 my $ret = SNMP::loadModules($var);
845 if ($ret) {
846 showChildren("1.3.6.1");
847 showChildren("1.3.6.1");
848 return 1;
849 } else {
850 $status->configure(-text => "Failed reading module $var");
851 return 0;
852 }
853 }
854 return 0;
855 }
856
857 sub stop {
858 $stopit = shift;
859 if ($stopit) {
860 $stopBut->configure(-state => 'disabled');
861 } else {
862 $stopBut->configure(-state => 'normal');
863 }
864 }
865
866 sub entryBox {
867 my $title = shift;
868 my $text = shift;
869 my $var = shift;
870 my $callback = shift;
871 my $top = MainWindow->new();
872 my $newvar = $$var if defined($var);
873 $top->title($title);
874 my $f = $top->Frame();
875 $f->pack(-side => 'top');
876 $f->Label(-pady => $tmpbd, -padx => $tmpbd,
877 -text => $text)->pack(-side => 'left');
878 my $e = $f->Entry(-textvariable => \$newvar);
879 $e->pack(-side => 'left');
880 $f = $top->Frame();
881 $f->pack(-side => 'bottom');
882 my $b = $f->Button(-pady => $tmpbd, -padx => $tmpbd, -text => 'Ok',
883 -command => [sub { my $w = shift;
884 my $v1 = shift;
885 my $v2 = shift;
886 my $call = shift;
887 my $ret = 1;
888 $$v1 = $$v2 if defined($v1);
889 $ret = $call->($$v2)
890 if defined($call);
891 $w->destroy() if ($ret);}, $top, $var,
892 \$newvar, $callback]);
893 $b->pack(-side => 'left');
894 $e->bind('<Return>',[$b,'invoke']);
895 $b = $f->Button(-pady => $tmpbd, -padx => $tmpbd,
896 -text => 'Cancel', -command => [sub { my $w = shift;
897 $w->destroy();}, $top
898 ]);
899 $b->pack(-side => 'right');
900 $e->bind('<Escape>',[$b,'invoke']);
901
902 }
903
904 sub findANode {
905 my $val = shift;
906 my $tag = SNMP::translateObj($val);
907 if ($tag) {
908 showAllChildren($tag);
909 return 1;
910 } else {
911 $top->Dialog(-text => "$val not found")->Show();
912 return 0;
913 }
914 }
915
916 sub test_version {
917 my ($gt, $major, $minor, $sub) = @_;
918 $SNMP::VERSION =~ /(\d)\.(\d).(\d)/;
919 if ($gt) {
920 if ($1 > $major || ($1 == $major && $2 > $minor) ||
921 ($1 == $major && $2 == $minor && $3 >= $sub)) {
922 return 1;
923 }
924 } else {
925 if ($1 < $major || ($1 == $major && $2 < $minor) || ($1 == $major && $2 == $minor && $3 < $sub)) {
926 return 1;
927 }
928 }
929 return 0;
930 }
931
932 sub save_options {
933 my $umask = umask();
934 umask 0077; # make sure its not readable by the world by default.
935 if (!open(O,">$opts{'f'}")) {
936 warn "can't save to $opts{'f'}\n";
937 umask $umask;
938 return;
939 }
940 umask $umask;
941 print O Data::Dumper->Dump([\%session_opts], [qw(*session_opts)]);
942 print O Data::Dumper->Dump([\%displayInfoStates], [qw(*displayInfoStates)]);
943 foreach my $var (@saveoptions) {
944 print O Data::Dumper->Dump([$$var], [$var]);
945 }
946 close(O);
947 $status->configure(-text => "saved options to $opts{'f'}");
948 }
949
950 # returns 1 if $oid2 is below $oid1 in the hierarchy
951 sub is_in_subtree {
952 my ($oid1, $oid2) = @_;
953 # get pure numeric
954 $oid1 = SNMP::translateObj($oid1) if ($oid1 !~ /^[\d\.]*$/);
955 $oid2 = SNMP::translateObj($oid2) if ($oid2 !~ /^[\d\.]*$/);
956
957 # has more on it or is exactly the same
958 return 1 if ($oid2 =~ /^$oid1\./ || $oid2 =~ /^$oid1$/);
959 return 0;
960 }
961
962 sub format_oid {
963 my ($oid, $type) = @_;
964 $oid =~ s/\.$//;
965 $type = $displayoidas if ($type eq "");
966
967 if ($type eq 'numeric') {
968 return SNMP::translateObj($oid) if ($oid !~ /^[\d\.]*$/);
969 return $oid;
970 } elsif ($type eq 'full') {
971 return SNMP::translateObj($oid, 1) if ($oid =~ /^[\d\.]*$/);
972 return SNMP::translateObj(SNMP::translateObj($oid), 1) if ($oid !~ /^\./);
973 return $oid;
974 } elsif ($type eq 'short' || $type eq 'module') {
975 $oid = SNMP::translateObj($oid) if ($oid =~ /^[\d\.]*$/);
976 $oid =~ s/.*\.([a-zA-Z]\w+)\.(.*)/$1.$2/;
977 if ($type eq 'module') {
978 $oid = $SNMP::MIB{format_oid($oid,'numeric')}->{'moduleID'} . "::" . $oid;
979 }
980 return $oid;
981 } elsif ($type eq 'module') {
982 $oid = SNMP::translateObj($oid) if ($oid =~ /^[\d\.]*$/);
983 $oid =~ s/.*\.([a-zA-Z]\w+)\.(.*)/$1.$2/;
984 return $oid;
985 } else {
986 warn 'unknown oid translation type: $type';
987 return $oid;
988 }
989 }
990
991 sub get_oid {
992 my ($var, $type) = @_;
993 return format_oid($var->tag . "." . $var->iid, $type);
994 }