3
package NetSNMP::manager;
6
use Apache::Constants qw(:common);
7
use CGI qw(:standard delete_all);
10
use NetSNMP::manager::displaytable qw(displaytable displaygraph);
13
$NetSNMP::manager::hostname = 'localhost'; # Host that serves the mSQL Database
14
$NetSNMP::manager::dbname = 'snmp'; # mySQL Database name
15
$NetSNMP::manager::user = 'root';
16
# $NetSNMP::manager::pass = "password";
17
$NetSNMP::manager::imagebase = "/home/hardaker/src/snmp/manager"; # <=== CHANGE ME ====
18
$NetSNMP::manager::redimage = "/graphics/red.gif";
19
$NetSNMP::manager::greenimage = "/graphics/green.gif";
20
#$NetSNMP::manager::verbose = 1;
21
$NetSNMP::manager::tableparms = "border=1 bgcolor=\"#c0c0e0\"";
22
$NetSNMP::manager::headerparms = "border=1 bgcolor=\"#b0e0b0\"";
24
# init the snmp library
25
$SNMP::save_descriptions=1;
28
%NetSNMP::manager::myorder = qw(id 0 oidindex 1 host 2 updated 3);
34
# get info from handler
35
my $hostname = $r->dir_config('hostname') || $NetSNMP::manager::hostname;
36
my $dbname = $r->dir_config('dbname') || $NetSNMP::manager::dbname;
37
my $sqluser = $r->dir_config('user') || $NetSNMP::manager::user;
38
my $pass = $r->dir_config('pass') || $NetSNMP::manager::pass;
39
my $verbose = $r->dir_config('verbose') || $NetSNMP::manager::verbose;
41
#===========================================================================
43
#===========================================================================
45
my ($dbh, $query, $remuser);
47
$remuser = $ENV{'REMOTE_USER'};
48
$remuser = "guest" if (!defined($remuser) || $remuser eq "");
50
#===========================================================================
51
# Connect to the mSQL database with the appropriate driver
52
#===========================================================================
53
($dbh = DBI->connect("DBI:mysql:database=$dbname;host=$hostname", $sqluser, $pass))
54
or die "\tConnect not ok: $DBI::errstr\n";
56
#===========================================================================
57
# stats Images, for inclusion on another page. (ie, slashdot user box)
58
#===========================================================================
59
if (my $group = param('groupstat')) {
60
$r->content_type("image/gif");
61
$r->send_http_header();
62
my $cur = getcursor($dbh, "select host from usergroups as ug, hostgroups as hg where ug.groupname = '$group' and hg.groupname = '$group' and user = '$remuser'");
63
while (my $row = $cur->fetchrow_hashref ) {
64
if (checkhost($dbh, $group, $row->{'host'})) {
65
open(I, "$NetSNMP::manager::imagebase$NetSNMP::manager::redimage");
66
while(read(I, $_, 4096)) { print; }
70
open(I, "$NetSNMP::manager::imagebase$NetSNMP::manager::greenimage");
71
while(read(I, $_, 4096)) { print; }
79
my @out = localtime($time);
80
my $ret = $out[4] . "-" . $out[3] . "-" . $out[5] . " " . $out[2] . " " . $out[1];
81
# print STDERR "$time: $ret\n";
87
# Graphing of historical data
89
if ((param('displaygraph') || param('dograph')) && param('table')) {
90
my $host = param('host');
91
my $group = param('group');
92
if (!isuser($dbh, $remuser, $group)) {
93
$r->content_type("image/png");
94
$r->send_http_header();
95
print "Unauthorized access to that group ($group)\n";
96
return Exit($dbh, $group);
98
my $table = param('table');
101
if (!param('dograph')) {
102
$r->content_type("text/html");
103
$r->send_http_header();
104
print "<body bgcolor=\"#ffffff\">\n";
106
print "<table border=1><tr><td>\n";
109
print "<tr align=top><th></th><th>Select indexes<br>to graph</th></tr>\n";
111
my $handle = getcursor($dbh, "SELECT sql_small_result distinct(oidindex) FROM $table where host = '$host'");
113
while ( $row = $handle->fetchrow_hashref ) {
114
print "<tr><td>$row->{oidindex}</td><td><input type=checkbox value=1 name=" . 'graph_' . displaytable::to_unique_key($row->{'oidindex'}) . "></td></tr>\n";
121
print "<tr align=top><th></th><th>Select Columns<br>to graph</th></tr>\n";
122
my $handle = getcursor($dbh, "SELECT * FROM $table limit 1");
123
my $row = $handle->fetchrow_hashref;
124
map { print "<tr><td>$_</td><td><input type=checkbox value=1 name=column_" . displaytable::to_unique_key($_) . "></td></tr>\n"; } keys(%$row);
127
print "</td></tr></table>\n";
129
print "<br>Graph as a Rate: <input type=checkbox value=1 name=graph_as_rate><br>\n";
130
print "<br>Maximum Y Value: <input type=text value=inf name=max_y><br>\n";
131
print "<br>Minimum Y Value: <input type=text value=-inf name=min_y><br>\n";
133
print "<input type=hidden name=table value=\"$table\">\n";
134
print "<input type=hidden name=host value=\"$host\">\n";
135
print "<input type=hidden name=dograph value=1>\n";
136
print "<input type=hidden name=group value=\"$group\">\n";
137
print "<input type=submit name=\"Make Graph\">\n";
141
my $handle = getcursor($dbh, "SELECT distinct(oidindex) FROM $table where host = '$host' order by oidindex");
142
return Exit($dbh, $group);
144
if (param('graph_all_data')) {
145
$clause = "host = '$host'";
147
my $handle = getcursor($dbh, "SELECT distinct(oidindex) FROM $table where host = '$host'");
149
while ( $row = $handle->fetchrow_hashref ) {
150
# print STDERR "graph test: " . $row->{'oidindex'} . "=" . "graph_" . displaytable::to_unique_key($row->{'oidindex'}) . "=" . param("graph_" . displaytable::to_unique_key($row->{'oidindex'})) . "\n";
151
if (param("graph_" . displaytable::to_unique_key($row->{'oidindex'}))) {
152
$clause .= " or oidindex = " . $row->{'oidindex'} . "";
156
my $handle = getcursor($dbh, "SELECT * FROM $table limit 1");
157
my $row = $handle->fetchrow_hashref;
158
map { push @columns, $_ if (param('column_' . displaytable::to_unique_key($_))) } keys(%$row);
161
$clause =~ s/\( or /\(/;
162
if ($clause =~ /\(\)/ || $#columns == -1) {
163
$r->content_type("text/html");
164
$r->send_http_header();
165
print "<body bgcolor=\"#ffffff\">\n";
166
print "<h1>No Data to Graph</h1>\n";
167
print STDERR "No data to graph: $clause, $#columns\n";
168
return Exit($dbh, "$group");
170
$clause .= " and host = '$host'";
173
# print STDERR "graphing clause: $clause\n";
175
# all is ok, display the graph
177
$r->content_type("image/png");
178
$r->send_http_header();
180
print STDERR "graphing clause: $clause, columns: ", join(", ",@columns), "\n";
182
push (@args, '-rate', '60') if (param('graph_as_rate'));
183
push (@args, '-max', param('max_y')) if (param('max_y') && param('max_y') =~ /^[-.\d]+$/);
184
push (@args, '-min', param('min_y')) if (param('min_y') && param('min_y') =~ /^[-.\d]+$/);
187
displaygraph($dbh, $table,
188
# '-xcol', "date_format(updated,'%m-%d-%y %h:%i')",
189
'-xcol', "unix_timestamp(updated)",
191
'x_labels_vertical', '1',
193
'x_number_format', \&date_format,
194
'y_label', 'Count/Min',
198
'-clauses', "$clause order by updated",
200
'-columns', \@columns,
201
'-indexes', ['oidindex']);
202
print STDERR "$ret rows graphed\n";
206
#===========================================================================
208
#===========================================================================
209
$r->content_type("text/html");
210
$r->send_http_header();
211
print "<body bgcolor=\"#ffffff\">\n";
212
print "<h1>UCD-SNMP Management Console</h1>\n";
215
#===========================================================================
216
# Display mib related data information
217
#===========================================================================
218
if (param('displayinfo')) {
219
makemibtable(param('displayinfo'));
220
return Exit($dbh, "");
223
#===========================================================================
224
# Display a generic sql table of any kind (debugging).
225
#===========================================================================
226
# if (my $disptable = param('displaytable')) {
227
# if (param('editable') == 1) {
228
# print "<form submit=dont>\n";
229
# displaytable($disptable, -editable, 1);
232
# displaytable($disptable);
234
# return Exit($dbh, "");
237
#===========================================================================
238
# Get host and group from CGI query.
239
#===========================================================================
240
my $host = param('host');
241
my $group = param('group');
243
#===========================================================================
244
# Editable user information
245
#===========================================================================
247
if (param('setuponcall')) {
248
print "<title>oncall schedule for user: $remuser</title>\n";
249
print "<h2>oncall schedule for user: $remuser</h2>\n";
250
print "<p>Please select your oncall schedule and mailing addresses for your groups below:";
251
if (!isexpert($remuser)) {
253
print "<li>Values for the days/hours fields can be comma seperated lists of hours/days/ranges. EG: hours: 7-18,0-4.\n";
256
print "<form method=post><input type=hidden name=setuponcall value=1>\n";
257
displaytable($dbh, 'oncall',
258
'-clauses',"where user = '$remuser' order by groupname",
259
'-select','id, user, groupname, email, pager, days, hours',
263
'-indexes', ['id','user','groupname'],
266
print "<input type=submit value=\"submit changes\">\n";
268
return Exit($dbh, $group);
271
#===========================================================================
272
# show the list of groups a user belongs to.
273
#===========================================================================
274
if (!defined($group)) {
275
my @groups = getgroupsforuser($dbh, $remuser);
276
print "<title>Net-SNMP Group List</title>\n";
277
print "<h2>Host groupings you may access:</h2>\n";
278
if (!isexpert($remuser)) {
280
print "<li>Click on a group to operate or view the hosts in that group.\n";
281
print "<li>Click on a red status light below to list the problems found.\n";
286
displaytable($dbh, 'usergroups',
287
'-clauses', "where (user = '$remuser')",
288
'-select', 'distinct groupname',
290
'-printonly', ['groupname'],
291
'-datalink', sub { my $q = self_url();
294
return if ($key ne "groupname");
295
return addtoken($q,"group=$h");
300
my($dbh, $junk, $data) = @_;
301
if (!defined($data)) {
302
print "<th>Status</th>";
306
$cur = getcursor($dbh, "select host from hostgroups where groupname = '$data->{groupname}'");
307
while ( $row = $cur->fetchrow_hashref ) {
308
if (checkhost($dbh, $data->{'groupname'},
310
print "<td><a href=\"" . addtoken($q,"group=$data->{groupname}&summarizegroup=1") . "\"><img border=0 src=$NetSNMP::manager::redimage></a></td>\n";
314
print "<td><img src=$NetSNMP::manager::greenimage></td>\n";
318
return Exit($dbh, $group);
320
if ($#groups == -1) {
321
print "You are not configured to use the Net-SNMP-manager, please contact your system administrator.";
322
return Exit($dbh, $group);
328
#===========================================================================
329
# reject un-authorized people accessing a certain group
330
#===========================================================================
331
if (!isuser($dbh, $remuser, $group)) {
332
print "Unauthorized access to that group ($group)\n";
333
return Exit($dbh, $group);
336
#===========================================================================
337
# add a new host to a group
338
#===========================================================================
339
if (defined(my $newhost = param('newhost'))) {
340
if (isadmin($dbh, $remuser, $group)) {
341
if ($dbh->do("select * from hostgroups where host = '$newhost' and groupname = '$group'") eq "0E0") {
342
$dbh->do("insert into hostgroups(host,groupname) values('$newhost','$group')") ;
344
print "<b>ERROR: host $newhost already in $group</b>\n";
346
CGI::delete('newhost');
350
#===========================================================================
351
# display setup configuration for a group
352
#===========================================================================
353
if (defined(param('setupgroup'))) {
354
if (isadmin($dbh, $remuser, $group)) {
355
setupgroup($dbh, $group);
357
print "<h2>You're not able to perform setup operations for group $group\n";
359
return Exit($dbh, $group);
362
#===========================================================================
363
# save configuration information submitted about a group
364
#===========================================================================
365
if (defined(param('setupgroupsubmit')) &&
366
isadmin($dbh, $remuser, $group)) {
367
setupgroupsubmit($dbh, $group);
369
param(-name => 'group', -value => $group);
370
print "<a href=\"" . self_url() . "\">Entries submitted</a>";
371
return Exit($dbh, $group);
374
#===========================================================================
376
#===========================================================================
377
if (defined(param('userprefs'))) {
378
setupuserpreferences($dbh, $remuser, $group);
379
return Exit($dbh, $group);
382
#===========================================================================
383
# save submitted user preferences
384
#===========================================================================
385
if (defined(param('setupuserprefssubmit')) &&
386
isadmin($dbh, $remuser, $group)) {
387
setupusersubmit($dbh, $remuser, $group);
389
param(-name => 'group', -value => $group);
390
print "<a href=\"" . self_url() . "\">Entries submitted</a>";
391
return Exit($dbh, $group);
394
#===========================================================================
395
# summarize problems in a group
396
#===========================================================================
397
if (defined(param('summarizegroup'))) {
398
print "<title>group problem summary: $group</title>\n";
399
print "<h2>The following is a list of problems in the group \"$group\":</h2>\n";
400
summarizeerrors($dbh, "where groupname = '$group'");
401
return Exit($dbh, $group);
404
#===========================================================================
405
# summarize problems on a host
406
#===========================================================================
407
if (defined($host) && defined(param('summarizehost'))) {
408
print "<title>host summary: $host</title>\n";
409
print "<h2>The following is a list of problems for the host \"$host\":</h2>\n";
410
summarizeerrors($dbh, "where groupname = '$group' and host = '$host'");
411
return Exit($dbh, $group);
414
#===========================================================================
415
# display a list of hosts in a group
416
#===========================================================================
417
if (!defined($host)) {
418
print "<title>Net-SNMP Host $host</title>\n";
419
print "<h2>Hosts in the group \"$group\":</h2>\n";
420
if (!isexpert($remuser)) {
422
if (isadmin($dbh, $remuser, $group)) {
425
print "<li>Make sure you <a href=\"" . addtoken($q,"group=$group&setupgroup=1") . "\">set up the host</a> for the SNMP tables you want to monitor.\n";
427
print "<li>Click on a hostname to operate on or view the information tables associated with that group.\n";
428
print "<li>Click on a red status light below to list the problems found in with a particular host.\n";
431
displaytable($dbh, 'hostgroups',
433
'-clauses', "where (groupname = '$group')",
434
'-select', 'distinct host, sysObjectId, sysDescr, sysUpTime, versionTag',
435
'-datalink', sub { my $q = self_url();
438
return if ($key ne "host");
439
return addtoken($q,"host=$h");
444
my($dbh, $junk, $data) = @_;
445
if (!defined($data)) {
446
print "<th>Status</th>";
449
if (checkhost($dbh, $group, $data->{'host'})) {
450
print "<td><a href=\"" . addtoken($q,"group=$group&summarizehost=1&host=$data->{host}") . "\"><img border=0 src=$NetSNMP::manager::redimage></a></td>\n";
452
print "<td><img src=$NetSNMP::manager::greenimage></td>\n";
456
if (isadmin($dbh, $remuser, $group)) {
457
addhostentryform($group);
460
print "<a href=\"" . addtoken($q,"group=$group&setupgroup=1") . "\">setup group $group</a>\n";
462
return Exit($dbh, $group);
465
#===========================================================================
466
# setup the host's history records
467
#===========================================================================
468
if (param('setuphost')) {
469
print "<title>Net-SNMP history setup for host: $host</title>\n";
470
print "<h2>Net-SNMP history setup for the host: \"$host\"</h2>\n";
471
print "<p>Enter the number of days to keep the data for a given table for the host \"$host\":\n";
472
if (!isexpert($remuser)) {
474
print "<li>Numbers must be greater than or equal to 1 to enable history logging.\n";
477
print "<form method=post><input type=hidden name=setuphost value=1><input type=hidden name=host value=\"$host\"><input type=hidden name=group value=\"$group\">\n";
478
displaytable($dbh, 'hosttables',
479
'-clauses',"where host = '$host' and groupname = '$group'",
480
'-select','groupname, host, tablename, keephistory',
484
'-indexes', ['groupname','host','tablename'],
487
print "<input type=submit value=\"submit changes\">\n";
489
return Exit($dbh, $group);
492
#===========================================================================
493
# display a huge table of history about something
494
#===========================================================================
495
if (param('displayhistory')) {
496
if (!isuser($dbh, $remuser, $group)) {
497
print "Unauthorized access to that group ($group)\n";
498
return Exit($dbh, $group);
500
displaytable($dbh, param('table'),
501
'-clauses', "where (host = '$host')",
502
'-dolink', \&linktodisplayinfo,
503
'-dontdisplaycol', "select * from userprefs where user = '$remuser' and groupname = '$group' and tablename = ? and columnname = ? and displayit = 'N'"
505
return Exit($dbh, $group);
508
#===========================================================================
509
# display inforamation about a host
510
# optionally add new collection tables
511
#===========================================================================
512
showhost($dbh, $host, $group, $remuser);
513
if (isadmin($dbh, $remuser, $group)) {
514
if (param('newtables')) {
515
my $x = param('newtables');
518
print "<br>Illegal table names in addition list: $x<br>\n"
520
my @x = split(/\s+/,$x);
522
$dbh->do("insert into hosttables(host, groupname, tablename, keephistory) values('$host','$group','$i','0')");
524
print "<br>adding: ",join(", ",@x),"<br>\n";
527
print "<br>Add new MIB Tables or Groups that you want to collect for this host: <form><input type=hidden name=host value=\"$host\"><input type=hidden name=group value=\"$group\"><input name=\"newtables\" type=text><br><input type=submit value=\"add tables\"></form>\n";
531
print "<a href=\"" . addtoken($q, "setuphost=1&host=$host&group=$group") . "\">setup host $host</a>\n";
533
return Exit($dbh, $group);
535
#===========================================================================
537
#===========================================================================
541
# add a token to a url string. Use either a ? or an & depending on
546
return "$url&$token" if ($url =~ /\?/);
547
return "$url?$token";
551
# summarizeerrors(DB-HANDLE, CLAUSE):
552
# summarize the list of errors in a given CLAUSE
554
sub summarizeerrors {
557
$clause = "where" if ($clause eq "");
558
my $clause2 = $clause;
559
$clause2 =~ s/ host / hosterrors.host /;
562
displaytable($dbh, 'hosterrors, hostgroups', # , hostgroups
563
'-select', "hosterrors.host as host, errormsg",
565
'-title', "Fatal Errors",
566
'-clauses', "$clause2 and hosterrors.host = hostgroups.host",
572
print "<td><img src=\"$NetSNMP::manager::redimage\"></td>\n";
575
my $tabletop = "<br><table $NetSNMP::manager::tableparms><tr $NetSNMP::manager::headerparms><th><b>Host</b></th><th><b>Table</b></th><th><b>Description</b></th></tr>\n";
578
getcursor($dbh, "SELECT * FROM hosttables $clause");
580
while (my $row = $cursor->fetchrow_hashref ) {
582
my $exprs = getcursor($dbh, "SELECT * FROM errorexpressions where (tablename = '$row->{tablename}')");
584
while (my $expr = $exprs->fetchrow_hashref ) {
585
my $errors = getcursor($dbh, "select * from $row->{tablename} where $expr->{expression} and host = '$row->{host}'");
586
while (my $error = $errors->fetchrow_hashref ) {
587
print $tabletop if ($donetop++ == 0);
588
print "<tr><td>$row->{host}</td><td>$row->{tablename}</td><td>$expr->{returnfield}: $error->{$expr->{returnfield}}</td></tr>";
597
# genericlly get a cursor for a given sql command, displaying and
598
# printing errors where necessary.
604
( $cursor = $dbh->prepare( $cmd ))
605
or print "\nnot ok: $DBI::errstr\n";
607
or print( "\tnot ok: $DBI::errstr\n" );
613
# sorts $a and $b against the order in the mib or against the hard
614
# coded special list.
617
my $a = $displaytable::a;
618
my $b = $displaytable::b;
619
my $mb = $SNMP::MIB{SNMP::translateObj($b)};
620
my $ma = $SNMP::MIB{SNMP::translateObj($a)};
622
return $NetSNMP::manager::myorder{$a} <=> $NetSNMP::manager::myorder{$b} if ((defined($NetSNMP::manager::myorder{$a}) || !defined($ma->{'subID'})) && (defined($NetSNMP::manager::myorder{$b}) || !defined($mb->{'subID'})));
623
return 1 if (defined($NetSNMP::manager::myorder{$b}) || !defined($mb->{'subID'}));
624
return -1 if (defined($NetSNMP::manager::myorder{$a}) || !defined($ma->{'subID'}));
626
$ma->{'subID'} <=> $mb->{'subID'};
630
# checkhost(GROUP, HOST):
631
# if anything in a host is an error, as defined by the
632
# errorexpressions table, return 1, else 0
640
return 2 if ($dbh->do("select * from hosterrors where host = '$host'") ne "0E0");
642
# get a list of tables we want to display
643
$tblh = getcursor($dbh, "SELECT * FROM hosttables where (host = '$host' and groupname = '$group')");
646
my($exprs, $tablelist);
647
while ( $tablelist = $tblh->fetchrow_hashref ) {
648
$exprs = getcursor($dbh, "SELECT * FROM errorexpressions where (tablename = '$tablelist->{tablename}')");
649
while(my $expr = $exprs->fetchrow_hashref) {
650
if ($dbh->do("select * from $tablelist->{tablename} where $expr->{expression} and host = '$host'") ne "0E0") {
661
# display all the tables monitored for a given host (in a group).
671
print "<title>Net-SNMP manager report for host: $host</title>\n";
672
print "<h2>Monitored information for the host $host</h2>\n";
673
if (!isexpert($remuser)) {
675
print "<li>Click on a column name for information about the data in that column.\n";
676
print "<li>Click on a column name or table name for information about the data in the table.\n";
677
print "<li>If you are <a href=\"" . addtoken($q, "setuphost=1&host=$host&group=$group") . "\">collecting past history</a> for a data set, links will appear below the table that allow you to view and/or graph the historic data.\n";
681
# does the host have a serious error?
683
my $errlist = getcursor($dbh, "SELECT * FROM hosterrors where (host = '$host')");
684
if ( $dbh->do("SELECT * FROM hosterrors where (host = '$host')") ne "0E0") {
685
displaytable($dbh, 'hosterrors',
686
'-clauses', "where (host = '$host')",
687
'-dontdisplaycol', "select * from userprefs where user = '$remuser' and groupname = '$group' and tablename = ? and columnname = ? and displayit = 'N'",
693
print "<td><img src=\"$NetSNMP::manager::redimage\"></td>\n";
697
# get a list of tables we want to display
698
my $tblh = getcursor($dbh, "SELECT * FROM hosttables where (host = '$host' and groupname = '$group')");
702
while ( $tablelist = $tblh->fetchrow_hashref ) {
704
displaytable($dbh, $tablelist->{'tablename'},
705
'-clauses', "where (host = '$host') order by oidindex",
706
'-dontdisplaycol', "select * from userprefs where user = '$remuser' and groupname = '$group' and tablename = ? and columnname = ? and displayit = 'N'",
707
'-sort', \&mykeysort,
708
'-dolink', \&linktodisplayinfo,
709
'-beginhook', \&printredgreen);
710
if ($tablelist->{'keephistory'}) {
714
print "<a href=\"" . addtoken($q, "displayhistory=1&host=$host&group=$group&table=$tablelist->{'tablename'}hist") . "\">[table]</a>\n";
715
print "<a href=\"" . addtoken($q, "displaygraph=1&host=$host&group=$group&table=$tablelist->{'tablename'}hist") . "\">[graph]</a>\n";
722
# linktodisplayinfo(STRING):
724
# returns a url to the appropriate displayinfo link if STRING is a
727
sub linktodisplayinfo {
728
return if (exists($NetSNMP::manager::myorder{shift}));
729
return self_url() . "&displayinfo=" . shift;
732
# printredgreen(TABLENAME, DATA):
734
# display a red or a green dot in a table dependent on the table's
735
# values and associated expression
737
# DATA is NULL when in a header row (displaying header names).
741
my $tablename = shift;
743
my ($exprs, $expr, $img);
745
if (!defined($data)) {
751
my $cmd = "SELECT * FROM errorexpressions where (tablename = '$tablename')";
752
print " $cmd\n" if ($NetSNMP::manager::verbose);
753
( $exprs = $dbh->prepare( $cmd ) )
754
or die "\nnot ok: $DBI::errstr\n";
756
or print( "\tnot ok: $DBI::errstr\n" );
758
$img = $NetSNMP::manager::greenimage;
759
while($expr = $exprs->fetchrow_hashref) {
760
if ($dbh->do("select oidindex from $tablename where host = '$data->{host}' and oidindex = '$data->{oidindex}' and $expr->{expression}") ne "0E0") {
761
$img = $NetSNMP::manager::redimage;
764
print "<td><img src=$img></td>";
768
# display information about a given mib node as a table.
771
my $dispinfo = shift;
772
# display information about a data type in a table
773
my $mib = $SNMP::MIB{SNMP::translateObj($dispinfo)};
774
print "<table $NetSNMP::manager::tableparms><tr><td>\n";
775
foreach my $i (qw(label type access status units hint moduleID description enums)) {
776
# foreach my $i (keys(%$mib)) {
777
next if (!defined($$mib{$i}) || $$mib{$i} eq "");
778
next if (ref($$mib{$i}) eq "HASH" && $#{keys(%{$$mib{$i}})} == -1);
779
print "<tr><td>$i</td><td>";
780
if (ref($$mib{$i}) eq "HASH") {
781
print "<table $NetSNMP::manager::tableparms><tr><td>\n";
782
foreach my $j (sort { $$mib{$i}{$a} <=> $$mib{$i}{$b} } keys(%{$$mib{$i}})) {
783
print "<tr><td>$$mib{$i}{$j}</td><td>$j</td></tr>";
789
print "</td></tr>\n";
794
# given a user, get all the groups he belongs to.
795
sub getgroupsforuser {
796
my (@ret, $cursor, $row);
797
my ($dbh, $remuser) = @_;
798
( $cursor = $dbh->prepare( "SELECT * FROM usergroups where (user = '$remuser')"))
799
or die "\nnot ok: $DBI::errstr\n";
801
or print( "\tnot ok: $DBI::errstr\n" );
803
while ( $row = $cursor->fetchrow_hashref ) {
804
push(@ret, $row->{'groupname'});
809
# given a host, get all the groups it belongs to.
810
sub gethostsforgroup {
811
my (@ret, $cursor, $row);
812
my ($dbh, $group) = @_;
813
( $cursor = $dbh->prepare( "SELECT * FROM hostgroups where (groupname = '$group')"))
814
or die "\nnot ok: $DBI::errstr\n";
816
or print( "\tnot ok: $DBI::errstr\n" );
818
while ( $row = $cursor->fetchrow_hashref ) {
819
push(@ret, $row->{'host'});
824
# display the host add entry box
825
sub addhostentryform {
827
print "<form method=\"get\" action=\"" . self_url() . "\">\n";
828
print "Add a new host to the group \"$group\": <input type=\"text\" name=\"newhost\"><br>";
829
print "<input type=\"hidden\" name=\"group\" value=\"$group\">";
830
print "<input type=submit value=\"Add Hosts\">\n";
841
my ($dbh, $remuser, $group) = @_;
842
return 0 if (!defined($remuser) || !defined($group));
843
return 1 if ($dbh->do("select * from usergroups where user = '$remuser' and groupname = '$group' and isadmin = 'Y'") ne "0E0");
847
#is user a member of this group?
849
my ($dbh, $remuser, $group) = @_;
850
return 0 if (!defined($remuser) || !defined($group));
851
return 1 if ($dbh->do("select * from usergroups where user = '$remuser' and groupname = '$group'") ne "0E0");
855
# displayconfigarray(HOSTS, NAMES, CONFIG):
857
# displays an array of generic check buttons to turn on/off certain
859
sub displayconfigarray {
866
if ($config{'-check'}) {
867
( $cmd = $dbh->prepare( $config{'-check'} ) )
868
or die "\nnot ok: $DBI::errstr\n";
871
print "<table $NetSNMP::manager::tableparms>\n";
872
print "<tr><td></td>";
874
foreach $j (@$names) {
876
$nj = $j->[0] if ($config{'-arrayrefs'} || $config{'-arrayref2'});
877
print "<td>$nj</td>";
879
foreach my $i (@$hosts) {
881
$ni = $i->[0] if ($config{'-arrayrefs'} || $config{'-arrayref1'});
882
print "<tr><td>$ni</td>";
883
foreach $j (@$names) {
885
$nj = $j->[0] if ($config{'-arrayrefs'} || $config{'-arrayref2'});
886
my $checked = "checked" if (defined($cmd) && $cmd->execute($ni,$nj) ne "0E0");
887
print "<td><input type=checkbox $checked value=y name=" . $config{prefix} . $ni . $nj . "></td>\n";
895
sub adddefaulttables {
896
my ($dbh, $names) = @_;
898
# add in known expression tables.
899
my $handle = getcursor($dbh, "SELECT * FROM errorexpressions");
902
while($row = $handle->fetchrow_hashref) {
903
foreach $i (@$names) {
904
if ($i->[0] eq $row->{tablename}) {
908
push @$names, [$row->{tablename}];
913
# display the setup information page for a given group.
919
my ($hosts, $names) = gethostandgroups($dbh, $group);
920
adddefaulttables($dbh, $names);
922
print "<form method=\"post\" action=\"" . self_url() . "\">\n";
923
print "<input type=hidden text=\"setupgroupsubmit\" value=\"y\">";
924
displayconfigarray($dbh, $hosts, $names,
926
-check, "select * from hosttables where (host = ? and tablename = ? and groupname = '$group')");
927
print "<input type=hidden name=group value=\"$group\">\n";
928
print "<input type=submit value=submit name=\"setupgroupsubmit\">\n";
932
# a wrapper around fetching arrays of everything in a table.
937
my $selectwhat = $config{'-select'} || "*";
940
$handle = getcursor($dbh, "SELECT $selectwhat FROM $table $config{-clauses}");
941
return $handle->fetchall_arrayref;
945
# get a list of all tablenames and hostnames for a given group.
947
sub gethostandgroups {
952
my $names = getarrays($dbh, 'hosttables',
953
"-select", 'distinct tablename',
954
"-clauses", "where groupname = '$group'");
956
my $hosts = getarrays($dbh, 'hostgroups',
957
"-select", 'distinct host',
958
"-clauses", "where groupname = '$group'");
960
return ($hosts, $names);
963
sub setupgroupsubmit {
967
my ($hosts, $names) = gethostandgroups($dbh, $group);
968
adddefaulttables($dbh, $names);
970
foreach my $i (@$hosts) {
971
$dbh->do("delete from hosttables where host = '${$i}[0]' and groupname = '$group'");
973
my $rep = $dbh->prepare("insert into hosttables(host,tablename,groupname) values(?,?,'$group')");
975
foreach my $i (@$hosts) {
976
foreach my $j (@$names) {
977
if (param("${$i}[0]" . "${$j}[0]")) {
978
print "test: ","${$i}[0] : ${$j}[0]<br>\n";
979
$rep->execute("${$i}[0]", "${$j}[0]") || print "$! $DBI::errstr<br>\n";
987
# save user pref data submitted by the user
989
sub setupusersubmit {
990
my ($dbh, $remuser, $group) = @_;
991
my $tables = getarrays($dbh, 'hosttables',
992
"-select", 'distinct tablename',
993
"-clauses", "where groupname = '$group'");
995
$dbh->do("delete from userprefs where user = '$remuser' and groupname = '$group'");
996
my $rep = $dbh->prepare("insert into userprefs(user, groupname, tablename, columnname, displayit) values('$remuser', '$group', ?, ?, 'N')");
999
foreach my $i (@$tables) {
1000
my $sth = $dbh->prepare("select * from ${$i}[0] where 1 = 0");
1003
foreach $j (@{$sth->{NAME}}) {
1004
if (param("${$i}[0]" . "$j")) {
1005
$rep->execute("${$i}[0]", "$j");
1012
my ($dbh, $group) = @_;
1013
my $tq = self_url();
1016
print "<a href=\"$tq\">[TOP]</a>\n";
1017
print "<a href=\"$tq?userprefs=1&group=$group\">[display options]</a>\n";
1018
print "<a href=\"$tq?setuponcall=1\">[setup oncall schedule]</a>\n";
1019
if (defined($group)) {
1020
print "<a href=\"$tq?group=$group\">[group: $group]</a>\n";
1021
print "<a href=\"$tq?group=$group&summarizegroup=1\">[summarize errors]</a>\n";
1023
$dbh->disconnect() if (defined($dbh));
1029
# setup user preferences by displaying a configuration array of
1030
# checkbuttons for each table.
1032
sub setupuserpreferences {
1033
my ($dbh, $remuser, $group) = @_;
1034
my $tables = getarrays($dbh, 'hosttables',
1035
"-select", 'distinct tablename',
1036
"-clauses", "where groupname = '$group'");
1038
print "<h3>Select the columns from the tables that you want to <b>hide</b> below and click on submit:</h3>\n";
1039
print "<form method=\"post\" action=\"" . self_url() . "\">\n";
1042
foreach my $i (@$tables) {
1043
my $sth = $dbh->prepare("select * from ${$i}[0] where 1 = 0");
1045
displayconfigarray($dbh, [${$i}[0]], $sth->{NAME},
1046
-check, "select * from userprefs where (tablename = ? and columnname = ? and user = '$remuser' and groupname = '$group' and displayit = 'N')");
1049
print "<input type=hidden name=group value=\"$group\">\n";
1050
print "<input type=submit value=submit name=\"setupuserprefssubmit\">\n";