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 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051
|
package NetSNMP::manager;
use strict ();
use warnings;
use Apache::Constants qw(:common);
use CGI qw(:standard delete_all);
use SNMP ();
use DBI ();
use NetSNMP::manager::displaytable qw(displaytable displaygraph);
# globals
$NetSNMP::manager::hostname = 'localhost'; # Host that serves the mSQL Database
$NetSNMP::manager::dbname = 'snmp'; # mySQL Database name
$NetSNMP::manager::user = 'root';
# $NetSNMP::manager::pass = "password";
$NetSNMP::manager::imagebase = "/home/hardaker/src/snmp/manager"; # <=== CHANGE ME ====
$NetSNMP::manager::redimage = "/graphics/red.gif";
$NetSNMP::manager::greenimage = "/graphics/green.gif";
#$NetSNMP::manager::verbose = 1;
$NetSNMP::manager::tableparms = "border=1 bgcolor=\"#c0c0e0\"";
$NetSNMP::manager::headerparms = "border=1 bgcolor=\"#b0e0b0\"";
# init the snmp library
$SNMP::save_descriptions=1;
#SNMP::init_mib();
%NetSNMP::manager::myorder = qw(id 0 oidindex 1 host 2 updated 3);
sub handler {
my $r = shift;
Apache->request($r);
# get info from handler
my $hostname = $r->dir_config('hostname') || $NetSNMP::manager::hostname;
my $dbname = $r->dir_config('dbname') || $NetSNMP::manager::dbname;
my $sqluser = $r->dir_config('user') || $NetSNMP::manager::user;
my $pass = $r->dir_config('pass') || $NetSNMP::manager::pass;
my $verbose = $r->dir_config('verbose') || $NetSNMP::manager::verbose;
#===========================================================================
# Global defines
#===========================================================================
my ($dbh, $query, $remuser);
$remuser = $ENV{'REMOTE_USER'};
$remuser = "guest" if (!defined($remuser) || $remuser eq "");
#===========================================================================
# Connect to the mSQL database with the appropriate driver
#===========================================================================
($dbh = DBI->connect("DBI:mysql:database=$dbname;host=$hostname", $sqluser, $pass))
or die "\tConnect not ok: $DBI::errstr\n";
#===========================================================================
# stats Images, for inclusion on another page. (ie, slashdot user box)
#===========================================================================
if (my $group = param('groupstat')) {
$r->content_type("image/gif");
$r->send_http_header();
my $cur = getcursor($dbh, "select host from usergroups as ug, hostgroups as hg where ug.groupname = '$group' and hg.groupname = '$group' and user = '$remuser'");
while (my $row = $cur->fetchrow_hashref ) {
if (checkhost($dbh, $group, $row->{'host'})) {
open(I, "$NetSNMP::manager::imagebase$NetSNMP::manager::redimage");
while(read(I, $_, 4096)) { print; }
close(I);
}
}
open(I, "$NetSNMP::manager::imagebase$NetSNMP::manager::greenimage");
while(read(I, $_, 4096)) { print; }
close(I);
return OK();
}
sub date_format {
my $time = shift;
my @out = localtime($time);
my $ret = $out[4] . "-" . $out[3] . "-" . $out[5] . " " . $out[2] . " " . $out[1];
# print STDERR "$time: $ret\n";
return $ret;
}
#
# Graphing of historical data
#
if ((param('displaygraph') || param('dograph')) && param('table')) {
my $host = param('host');
my $group = param('group');
if (!isuser($dbh, $remuser, $group)) {
$r->content_type("image/png");
$r->send_http_header();
print "Unauthorized access to that group ($group)\n";
return Exit($dbh, $group);
}
my $table = param('table');
my @columns;
if (!param('dograph')) {
$r->content_type("text/html");
$r->send_http_header();
print "<body bgcolor=\"#ffffff\">\n";
print "<form>\n";
print "<table border=1><tr><td>\n";
print "<table>\n";
print "<tr align=top><th></th><th>Select indexes<br>to graph</th></tr>\n";
my $handle = getcursor($dbh, "SELECT sql_small_result distinct(oidindex) FROM $table where host = '$host'");
my @cols;
while ( $row = $handle->fetchrow_hashref ) {
print "<tr><td>$row->{oidindex}</td><td><input type=checkbox value=1 name=" . 'graph_' . displaytable::to_unique_key($row->{'oidindex'}) . "></td></tr>\n";
}
print "</table>\n";
print "</td><td>\n";
print "<table>\n";
print "<tr align=top><th></th><th>Select Columns<br>to graph</th></tr>\n";
my $handle = getcursor($dbh, "SELECT * FROM $table limit 1");
my $row = $handle->fetchrow_hashref;
map { print "<tr><td>$_</td><td><input type=checkbox value=1 name=column_" . displaytable::to_unique_key($_) . "></td></tr>\n"; } keys(%$row);
print "</table>\n";
print "</td></tr></table>\n";
print "<br>Graph as a Rate: <input type=checkbox value=1 name=graph_as_rate><br>\n";
print "<br>Maximum Y Value: <input type=text value=inf name=max_y><br>\n";
print "<br>Minimum Y Value: <input type=text value=-inf name=min_y><br>\n";
print "<input type=hidden name=table value=\"$table\">\n";
print "<input type=hidden name=host value=\"$host\">\n";
print "<input type=hidden name=dograph value=1>\n";
print "<input type=hidden name=group value=\"$group\">\n";
print "<input type=submit name=\"Make Graph\">\n";
print "</form>\n";
my $handle = getcursor($dbh, "SELECT distinct(oidindex) FROM $table where host = '$host' order by oidindex");
return Exit($dbh, $group);
}
if (param('graph_all_data')) {
$clause = "host = '$host'";
} else {
my $handle = getcursor($dbh, "SELECT distinct(oidindex) FROM $table where host = '$host'");
$clause = "where (";
while ( $row = $handle->fetchrow_hashref ) {
# print STDERR "graph test: " . $row->{'oidindex'} . "=" . "graph_" . displaytable::to_unique_key($row->{'oidindex'}) . "=" . param("graph_" . displaytable::to_unique_key($row->{'oidindex'})) . "\n";
if (param("graph_" . displaytable::to_unique_key($row->{'oidindex'}))) {
$clause .= " or oidindex = " . $row->{'oidindex'} . "";
}
}
my $handle = getcursor($dbh, "SELECT * FROM $table limit 1");
my $row = $handle->fetchrow_hashref;
map { push @columns, $_ if (param('column_' . displaytable::to_unique_key($_))) } keys(%$row);
$clause .= ")";
$clause =~ s/\( or /\(/;
if ($clause =~ /\(\)/ || $#columns == -1) {
$r->content_type("text/html");
$r->send_http_header();
print "<body bgcolor=\"#ffffff\">\n";
print "<h1>No Data to Graph</h1>\n";
print STDERR "No data to graph: $clause, $#columns\n";
return Exit($dbh, "$group");
}
$clause .= " and host = '$host'";
}
# print STDERR "graphing clause: $clause\n";
# all is ok, display the graph
$r->content_type("image/png");
$r->send_http_header();
print STDERR "graphing clause: $clause, columns: ", join(", ",@columns), "\n";
my @args;
push (@args, '-rate', '60') if (param('graph_as_rate'));
push (@args, '-max', param('max_y')) if (param('max_y') && param('max_y') =~ /^[-.\d]+$/);
push (@args, '-min', param('min_y')) if (param('min_y') && param('min_y') =~ /^[-.\d]+$/);
my $ret =
displaygraph($dbh, $table,
# '-xcol', "date_format(updated,'%m-%d-%y %h:%i')",
'-xcol', "unix_timestamp(updated)",
'-pngparms', [
'x_labels_vertical', '1',
'x_tick_number', 6,
'x_number_format', \&date_format,
'y_label', 'Count/Min',
'title', $table,
# 'y_min_value', 0,
],
'-clauses', "$clause order by updated",
@args,
'-columns', \@columns,
'-indexes', ['oidindex']);
print STDERR "$ret rows graphed\n";
return OK();
}
#===========================================================================
# Start HTML.
#===========================================================================
$r->content_type("text/html");
$r->send_http_header();
print "<body bgcolor=\"#ffffff\">\n";
print "<h1>UCD-SNMP Management Console</h1>\n";
print "<hr>\n";
#===========================================================================
# Display mib related data information
#===========================================================================
if (param('displayinfo')) {
makemibtable(param('displayinfo'));
return Exit($dbh, "");
}
#===========================================================================
# Display a generic sql table of any kind (debugging).
#===========================================================================
# if (my $disptable = param('displaytable')) {
# if (param('editable') == 1) {
# print "<form submit=dont>\n";
# displaytable($disptable, -editable, 1);
# print "</form>\n";
# } else {
# displaytable($disptable);
# }
# return Exit($dbh, "");
# }
#===========================================================================
# Get host and group from CGI query.
#===========================================================================
my $host = param('host');
my $group = param('group');
#===========================================================================
# Editable user information
#===========================================================================
if (param('setuponcall')) {
print "<title>oncall schedule for user: $remuser</title>\n";
print "<h2>oncall schedule for user: $remuser</h2>\n";
print "<p>Please select your oncall schedule and mailing addresses for your groups below:";
if (!isexpert($remuser)) {
print "<ul>\n";
print "<li>Values for the days/hours fields can be comma seperated lists of hours/days/ranges. EG: hours: 7-18,0-4.\n";
print "</ul>\n";
}
print "<form method=post><input type=hidden name=setuponcall value=1>\n";
displaytable($dbh, 'oncall',
'-clauses',"where user = '$remuser' order by groupname",
'-select','id, user, groupname, email, pager, days, hours',
'-selectorder', 1,
'-notitle', 1,
'-editable', 1,
'-indexes', ['id','user','groupname'],
'-CGI', $CGI::Q
);
print "<input type=submit value=\"submit changes\">\n";
print "</form>\n";
return Exit($dbh, $group);
}
#===========================================================================
# show the list of groups a user belongs to.
#===========================================================================
if (!defined($group)) {
my @groups = getgroupsforuser($dbh, $remuser);
print "<title>Net-SNMP Group List</title>\n";
print "<h2>Host groupings you may access:</h2>\n";
if (!isexpert($remuser)) {
print "<ul>\n";
print "<li>Click on a group to operate or view the hosts in that group.\n";
print "<li>Click on a red status light below to list the problems found.\n";
print "</ul>\n";
}
if ($#groups > 0) {
displaytable($dbh, 'usergroups',
'-clauses', "where (user = '$remuser')",
'-select', 'distinct groupname',
'-notitle', 1,
'-printonly', ['groupname'],
'-datalink', sub { my $q = self_url();
my $key = shift;
my $h = shift;
return if ($key ne "groupname");
return addtoken($q,"group=$h");
},
'-beginhook',
sub {
my $q = self_url();
my($dbh, $junk, $data) = @_;
if (!defined($data)) {
print "<th>Status</th>";
return;
}
my ($cur, $row);
$cur = getcursor($dbh, "select host from hostgroups where groupname = '$data->{groupname}'");
while ( $row = $cur->fetchrow_hashref ) {
if (checkhost($dbh, $data->{'groupname'},
$row->{'host'})) {
print "<td><a href=\"" . addtoken($q,"group=$data->{groupname}&summarizegroup=1") . "\"><img border=0 src=$NetSNMP::manager::redimage></a></td>\n";
return;
}
}
print "<td><img src=$NetSNMP::manager::greenimage></td>\n";
}
);
$dbh->disconnect();
return Exit($dbh, $group);
} else {
if ($#groups == -1) {
print "You are not configured to use the Net-SNMP-manager, please contact your system administrator.";
return Exit($dbh, $group);
}
$group = $groups[0];
}
}
#===========================================================================
# reject un-authorized people accessing a certain group
#===========================================================================
if (!isuser($dbh, $remuser, $group)) {
print "Unauthorized access to that group ($group)\n";
return Exit($dbh, $group);
}
#===========================================================================
# add a new host to a group
#===========================================================================
if (defined(my $newhost = param('newhost'))) {
if (isadmin($dbh, $remuser, $group)) {
if ($dbh->do("select * from hostgroups where host = '$newhost' and groupname = '$group'") eq "0E0") {
$dbh->do("insert into hostgroups(host,groupname) values('$newhost','$group')") ;
} else {
print "<b>ERROR: host $newhost already in $group</b>\n";
}
CGI::delete('newhost');
}
}
#===========================================================================
# display setup configuration for a group
#===========================================================================
if (defined(param('setupgroup'))) {
if (isadmin($dbh, $remuser, $group)) {
setupgroup($dbh, $group);
} else {
print "<h2>You're not able to perform setup operations for group $group\n";
}
return Exit($dbh, $group);
}
#===========================================================================
# save configuration information submitted about a group
#===========================================================================
if (defined(param('setupgroupsubmit')) &&
isadmin($dbh, $remuser, $group)) {
setupgroupsubmit($dbh, $group);
delete_all();
param(-name => 'group', -value => $group);
print "<a href=\"" . self_url() . "\">Entries submitted</a>";
return Exit($dbh, $group);
}
#===========================================================================
# user preferences
#===========================================================================
if (defined(param('userprefs'))) {
setupuserpreferences($dbh, $remuser, $group);
return Exit($dbh, $group);
}
#===========================================================================
# save submitted user preferences
#===========================================================================
if (defined(param('setupuserprefssubmit')) &&
isadmin($dbh, $remuser, $group)) {
setupusersubmit($dbh, $remuser, $group);
delete_all();
param(-name => 'group', -value => $group);
print "<a href=\"" . self_url() . "\">Entries submitted</a>";
return Exit($dbh, $group);
}
#===========================================================================
# summarize problems in a group
#===========================================================================
if (defined(param('summarizegroup'))) {
print "<title>group problem summary: $group</title>\n";
print "<h2>The following is a list of problems in the group \"$group\":</h2>\n";
summarizeerrors($dbh, "where groupname = '$group'");
return Exit($dbh, $group);
}
#===========================================================================
# summarize problems on a host
#===========================================================================
if (defined($host) && defined(param('summarizehost'))) {
print "<title>host summary: $host</title>\n";
print "<h2>The following is a list of problems for the host \"$host\":</h2>\n";
summarizeerrors($dbh, "where groupname = '$group' and host = '$host'");
return Exit($dbh, $group);
}
#===========================================================================
# display a list of hosts in a group
#===========================================================================
if (!defined($host)) {
print "<title>Net-SNMP Host $host</title>\n";
print "<h2>Hosts in the group \"$group\":</h2>\n";
if (!isexpert($remuser)) {
print "<ul>\n";
if (isadmin($dbh, $remuser, $group)) {
my $q = self_url();
$q =~ s/\?.*//;
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";
}
print "<li>Click on a hostname to operate on or view the information tables associated with that group.\n";
print "<li>Click on a red status light below to list the problems found in with a particular host.\n";
print "</ul>\n";
}
displaytable($dbh, 'hostgroups',
'-notitle',0,
'-clauses', "where (groupname = '$group')",
'-select', 'distinct host, sysObjectId, sysDescr, sysUpTime, versionTag',
'-datalink', sub { my $q = self_url();
my $key = shift;
my $h = shift;
return if ($key ne "host");
return addtoken($q,"host=$h");
},
'-beginhook',
sub {
my $q = self_url();
my($dbh, $junk, $data) = @_;
if (!defined($data)) {
print "<th>Status</th>";
return;
}
if (checkhost($dbh, $group, $data->{'host'})) {
print "<td><a href=\"" . addtoken($q,"group=$group&summarizehost=1&host=$data->{host}") . "\"><img border=0 src=$NetSNMP::manager::redimage></a></td>\n";
} else {
print "<td><img src=$NetSNMP::manager::greenimage></td>\n";
}
}
);
if (isadmin($dbh, $remuser, $group)) {
addhostentryform($group);
my $q = self_url();
$q =~ s/\?.*//;
print "<a href=\"" . addtoken($q,"group=$group&setupgroup=1") . "\">setup group $group</a>\n";
}
return Exit($dbh, $group);
}
#===========================================================================
# setup the host's history records
#===========================================================================
if (param('setuphost')) {
print "<title>Net-SNMP history setup for host: $host</title>\n";
print "<h2>Net-SNMP history setup for the host: \"$host\"</h2>\n";
print "<p>Enter the number of days to keep the data for a given table for the host \"$host\":\n";
if (!isexpert($remuser)) {
print "<ul>\n";
print "<li>Numbers must be greater than or equal to 1 to enable history logging.\n";
print "</ul>\n";
}
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";
displaytable($dbh, 'hosttables',
'-clauses',"where host = '$host' and groupname = '$group'",
'-select','groupname, host, tablename, keephistory',
'-selectorder', 1,
'-notitle', 1,
'-editable', 1,
'-indexes', ['groupname','host','tablename'],
'-CGI', $CGI::Q
);
print "<input type=submit value=\"submit changes\">\n";
print "</form>\n";
return Exit($dbh, $group);
}
#===========================================================================
# display a huge table of history about something
#===========================================================================
if (param('displayhistory')) {
if (!isuser($dbh, $remuser, $group)) {
print "Unauthorized access to that group ($group)\n";
return Exit($dbh, $group);
}
displaytable($dbh, param('table'),
'-clauses', "where (host = '$host')",
'-dolink', \&linktodisplayinfo,
'-dontdisplaycol', "select * from userprefs where user = '$remuser' and groupname = '$group' and tablename = ? and columnname = ? and displayit = 'N'"
);
return Exit($dbh, $group);
}
#===========================================================================
# display inforamation about a host
# optionally add new collection tables
#===========================================================================
showhost($dbh, $host, $group, $remuser);
if (isadmin($dbh, $remuser, $group)) {
if (param('newtables')) {
my $x = param('newtables');
$x =~ s/,/ /g;
if (/[^\w\s]/) {
print "<br>Illegal table names in addition list: $x<br>\n"
} else {
my @x = split(/\s+/,$x);
foreach my $i (@x) {
$dbh->do("insert into hosttables(host, groupname, tablename, keephistory) values('$host','$group','$i','0')");
}
print "<br>adding: ",join(", ",@x),"<br>\n";
}
} else {
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";
}
my $q = self_url();
$q =~ s/\?.*//;
print "<a href=\"" . addtoken($q, "setuphost=1&host=$host&group=$group") . "\">setup host $host</a>\n";
}
return Exit($dbh, $group);
#===========================================================================
# END of handler
#===========================================================================
}
# add a token to a url string. Use either a ? or an & depending on
# existence of ?.
sub addtoken {
my $url = shift;
my $token = shift;
return "$url&$token" if ($url =~ /\?/);
return "$url?$token";
}
#
# summarizeerrors(DB-HANDLE, CLAUSE):
# summarize the list of errors in a given CLAUSE
#
sub summarizeerrors {
my $dbh = shift;
my $clause = shift;
$clause = "where" if ($clause eq "");
my $clause2 = $clause;
$clause2 =~ s/ host / hosterrors.host /;
# Major errors
displaytable($dbh, 'hosterrors, hostgroups', # , hostgroups
'-select', "hosterrors.host as host, errormsg",
'-notitle', 1,
'-title', "Fatal Errors",
'-clauses', "$clause2 and hosterrors.host = hostgroups.host",
'-beginhook', sub {
if ($#_ < 2) {
#doing header;
print "<td></td>";
} else {
print "<td><img src=\"$NetSNMP::manager::redimage\"></td>\n";
}});
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";
my $donetop = 0;
my $cursor =
getcursor($dbh, "SELECT * FROM hosttables $clause");
while (my $row = $cursor->fetchrow_hashref ) {
my $exprs = getcursor($dbh, "SELECT * FROM errorexpressions where (tablename = '$row->{tablename}')");
while (my $expr = $exprs->fetchrow_hashref ) {
my $errors = getcursor($dbh, "select * from $row->{tablename} where $expr->{expression} and host = '$row->{host}'");
while (my $error = $errors->fetchrow_hashref ) {
print $tabletop if ($donetop++ == 0);
print "<tr><td>$row->{host}</td><td>$row->{tablename}</td><td>$expr->{returnfield}: $error->{$expr->{returnfield}}</td></tr>";
}
}
}
print "</table>";
}
#
# getcursor(CMD):
# genericlly get a cursor for a given sql command, displaying and
# printing errors where necessary.
#
sub getcursor {
my $dbh = shift;
my $cmd = shift;
my $cursor;
( $cursor = $dbh->prepare( $cmd ))
or print "\nnot ok: $DBI::errstr\n";
( $cursor->execute )
or print( "\tnot ok: $DBI::errstr\n" );
return $cursor;
}
#
# mykeysort($a, $b)
# sorts $a and $b against the order in the mib or against the hard
# coded special list.
#
sub mykeysort {
my $a = $displaytable::a;
my $b = $displaytable::b;
my $mb = $SNMP::MIB{SNMP::translateObj($b)};
my $ma = $SNMP::MIB{SNMP::translateObj($a)};
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'})));
return 1 if (defined($NetSNMP::manager::myorder{$b}) || !defined($mb->{'subID'}));
return -1 if (defined($NetSNMP::manager::myorder{$a}) || !defined($ma->{'subID'}));
$ma->{'subID'} <=> $mb->{'subID'};
}
#
# checkhost(GROUP, HOST):
# if anything in a host is an error, as defined by the
# errorexpressions table, return 1, else 0
#
sub checkhost {
my $dbh = shift;
my $group = shift;
my $host = shift;
my ($tblh);
return 2 if ($dbh->do("select * from hosterrors where host = '$host'") ne "0E0");
# get a list of tables we want to display
$tblh = getcursor($dbh, "SELECT * FROM hosttables where (host = '$host' and groupname = '$group')");
# table data
my($exprs, $tablelist);
while ( $tablelist = $tblh->fetchrow_hashref ) {
$exprs = getcursor($dbh, "SELECT * FROM errorexpressions where (tablename = '$tablelist->{tablename}')");
while(my $expr = $exprs->fetchrow_hashref) {
if ($dbh->do("select * from $tablelist->{tablename} where $expr->{expression} and host = '$host'") ne "0E0") {
return 1;
}
}
}
return 0;
}
#
# showhost(HOST):
#
# display all the tables monitored for a given host (in a group).
#
sub showhost {
my $dbh = shift;
my $host = shift;
my $group = shift;
my $remuser = shift;
my $q = self_url();
$q =~ s/\?.*//;
# host header
print "<title>Net-SNMP manager report for host: $host</title>\n";
print "<h2>Monitored information for the host $host</h2>\n";
if (!isexpert($remuser)) {
print "<ul>\n";
print "<li>Click on a column name for information about the data in that column.\n";
print "<li>Click on a column name or table name for information about the data in the table.\n";
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";
print "</ul>\n";
}
# does the host have a serious error?
my $errlist = getcursor($dbh, "SELECT * FROM hosterrors where (host = '$host')");
if ( $dbh->do("SELECT * FROM hosterrors where (host = '$host')") ne "0E0") {
displaytable($dbh, 'hosterrors',
'-clauses', "where (host = '$host')",
'-dontdisplaycol', "select * from userprefs where user = '$remuser' and groupname = '$group' and tablename = ? and columnname = ? and displayit = 'N'",
'-beginhook', sub {
if ($#_ < 2) {
#doing header;
print "<td></td>";
} else {
print "<td><img src=\"$NetSNMP::manager::redimage\"></td>\n";
}});
}
# get a list of tables we want to display
my $tblh = getcursor($dbh, "SELECT * FROM hosttables where (host = '$host' and groupname = '$group')");
# table data
my($tablelist);
while ( $tablelist = $tblh->fetchrow_hashref ) {
displaytable($dbh, $tablelist->{'tablename'},
'-clauses', "where (host = '$host') order by oidindex",
'-dontdisplaycol', "select * from userprefs where user = '$remuser' and groupname = '$group' and tablename = ? and columnname = ? and displayit = 'N'",
'-sort', \&mykeysort,
'-dolink', \&linktodisplayinfo,
'-beginhook', \&printredgreen);
if ($tablelist->{'keephistory'}) {
my $q = self_url();
$q =~ s/\?.*//;
print "history: ";
print "<a href=\"" . addtoken($q, "displayhistory=1&host=$host&group=$group&table=$tablelist->{'tablename'}hist") . "\">[table]</a>\n";
print "<a href=\"" . addtoken($q, "displaygraph=1&host=$host&group=$group&table=$tablelist->{'tablename'}hist") . "\">[graph]</a>\n";
print "<br>\n";
}
}
}
#
# linktodisplayinfo(STRING):
#
# returns a url to the appropriate displayinfo link if STRING is a
# mib node.
#
sub linktodisplayinfo {
return if (exists($NetSNMP::manager::myorder{shift}));
return self_url() . "&displayinfo=" . shift;
}
# printredgreen(TABLENAME, DATA):
#
# display a red or a green dot in a table dependent on the table's
# values and associated expression
#
# DATA is NULL when in a header row (displaying header names).
#
sub printredgreen {
my $dbh = shift;
my $tablename = shift;
my $data = shift;
my ($exprs, $expr, $img);
if (!defined($data)) {
#doing header;
print "<td></td>";
return;
}
my $cmd = "SELECT * FROM errorexpressions where (tablename = '$tablename')";
print " $cmd\n" if ($NetSNMP::manager::verbose);
( $exprs = $dbh->prepare( $cmd ) )
or die "\nnot ok: $DBI::errstr\n";
( $exprs->execute )
or print( "\tnot ok: $DBI::errstr\n" );
$img = $NetSNMP::manager::greenimage;
while($expr = $exprs->fetchrow_hashref) {
if ($dbh->do("select oidindex from $tablename where host = '$data->{host}' and oidindex = '$data->{oidindex}' and $expr->{expression}") ne "0E0") {
$img = $NetSNMP::manager::redimage;
}
}
print "<td><img src=$img></td>";
}
#
# display information about a given mib node as a table.
#
sub makemibtable {
my $dispinfo = shift;
# display information about a data type in a table
my $mib = $SNMP::MIB{SNMP::translateObj($dispinfo)};
print "<table $NetSNMP::manager::tableparms><tr><td>\n";
foreach my $i (qw(label type access status units hint moduleID description enums)) {
# foreach my $i (keys(%$mib)) {
next if (!defined($$mib{$i}) || $$mib{$i} eq "");
next if (ref($$mib{$i}) eq "HASH" && $#{keys(%{$$mib{$i}})} == -1);
print "<tr><td>$i</td><td>";
if (ref($$mib{$i}) eq "HASH") {
print "<table $NetSNMP::manager::tableparms><tr><td>\n";
foreach my $j (sort { $$mib{$i}{$a} <=> $$mib{$i}{$b} } keys(%{$$mib{$i}})) {
print "<tr><td>$$mib{$i}{$j}</td><td>$j</td></tr>";
}
print "</table>\n";
} else {
print "$$mib{$i}";
}
print "</td></tr>\n";
}
print "</table>\n";
}
# given a user, get all the groups he belongs to.
sub getgroupsforuser {
my (@ret, $cursor, $row);
my ($dbh, $remuser) = @_;
( $cursor = $dbh->prepare( "SELECT * FROM usergroups where (user = '$remuser')"))
or die "\nnot ok: $DBI::errstr\n";
( $cursor->execute )
or print( "\tnot ok: $DBI::errstr\n" );
while ( $row = $cursor->fetchrow_hashref ) {
push(@ret, $row->{'groupname'});
}
@ret;
}
# given a host, get all the groups it belongs to.
sub gethostsforgroup {
my (@ret, $cursor, $row);
my ($dbh, $group) = @_;
( $cursor = $dbh->prepare( "SELECT * FROM hostgroups where (groupname = '$group')"))
or die "\nnot ok: $DBI::errstr\n";
( $cursor->execute )
or print( "\tnot ok: $DBI::errstr\n" );
while ( $row = $cursor->fetchrow_hashref ) {
push(@ret, $row->{'host'});
}
@ret;
}
# display the host add entry box
sub addhostentryform {
my $group = shift;
print "<form method=\"get\" action=\"" . self_url() . "\">\n";
print "Add a new host to the group \"$group\": <input type=\"text\" name=\"newhost\"><br>";
print "<input type=\"hidden\" name=\"group\" value=\"$group\">";
print "<input type=submit value=\"Add Hosts\">\n";
print "</form>";
}
#is an expert user?
sub isexpert {
return 0;
}
#is remuser a admin?
sub isadmin {
my ($dbh, $remuser, $group) = @_;
return 0 if (!defined($remuser) || !defined($group));
return 1 if ($dbh->do("select * from usergroups where user = '$remuser' and groupname = '$group' and isadmin = 'Y'") ne "0E0");
return 0;
}
#is user a member of this group?
sub isuser {
my ($dbh, $remuser, $group) = @_;
return 0 if (!defined($remuser) || !defined($group));
return 1 if ($dbh->do("select * from usergroups where user = '$remuser' and groupname = '$group'") ne "0E0");
return 0;
}
# displayconfigarray(HOSTS, NAMES, CONFIG):
#
# displays an array of generic check buttons to turn on/off certain
# variables.
sub displayconfigarray {
my $dbh = shift;
my $hosts = shift;
my $names = shift;
my %config = @_;
my $cmd;
if ($config{'-check'}) {
( $cmd = $dbh->prepare( $config{'-check'} ) )
or die "\nnot ok: $DBI::errstr\n";
}
print "<table $NetSNMP::manager::tableparms>\n";
print "<tr><td></td>";
my ($i, $j);
foreach $j (@$names) {
my $nj = $j;
$nj = $j->[0] if ($config{'-arrayrefs'} || $config{'-arrayref2'});
print "<td>$nj</td>";
}
foreach my $i (@$hosts) {
my $ni = $i;
$ni = $i->[0] if ($config{'-arrayrefs'} || $config{'-arrayref1'});
print "<tr><td>$ni</td>";
foreach $j (@$names) {
my $nj = $j;
$nj = $j->[0] if ($config{'-arrayrefs'} || $config{'-arrayref2'});
my $checked = "checked" if (defined($cmd) && $cmd->execute($ni,$nj) ne "0E0");
print "<td><input type=checkbox $checked value=y name=" . $config{prefix} . $ni . $nj . "></td>\n";
}
print "</tr>\n";
}
print "</tr>";
print "</table>";
}
sub adddefaulttables {
my ($dbh, $names) = @_;
my $row;
# add in known expression tables.
my $handle = getcursor($dbh, "SELECT * FROM errorexpressions");
expr:
while($row = $handle->fetchrow_hashref) {
foreach $i (@$names) {
if ($i->[0] eq $row->{tablename}) {
next expr;
}
}
push @$names, [$row->{tablename}];
}
}
#
# display the setup information page for a given group.
#
sub setupgroup {
my $dbh = shift;
my $group = shift;
my ($hosts, $names) = gethostandgroups($dbh, $group);
adddefaulttables($dbh, $names);
print "<form method=\"post\" action=\"" . self_url() . "\">\n";
print "<input type=hidden text=\"setupgroupsubmit\" value=\"y\">";
displayconfigarray($dbh, $hosts, $names,
-arrayrefs, 1,
-check, "select * from hosttables where (host = ? and tablename = ? and groupname = '$group')");
print "<input type=hidden name=group value=\"$group\">\n";
print "<input type=submit value=submit name=\"setupgroupsubmit\">\n";
print "</form>";
}
# a wrapper around fetching arrays of everything in a table.
sub getarrays {
my $dbh = shift;
my $table = shift;
my %config = @_;
my $selectwhat = $config{'-select'} || "*";
my $handle;
$handle = getcursor($dbh, "SELECT $selectwhat FROM $table $config{-clauses}");
return $handle->fetchall_arrayref;
}
#
# get a list of all tablenames and hostnames for a given group.
#
sub gethostandgroups {
my $dbh = shift;
my $group = shift;
my ($tbnms);
my $names = getarrays($dbh, 'hosttables',
"-select", 'distinct tablename',
"-clauses", "where groupname = '$group'");
my $hosts = getarrays($dbh, 'hostgroups',
"-select", 'distinct host',
"-clauses", "where groupname = '$group'");
return ($hosts, $names);
}
sub setupgroupsubmit {
my $dbh = shift;
my $group = shift;
my ($hosts, $names) = gethostandgroups($dbh, $group);
adddefaulttables($dbh, $names);
foreach my $i (@$hosts) {
$dbh->do("delete from hosttables where host = '${$i}[0]' and groupname = '$group'");
}
my $rep = $dbh->prepare("insert into hosttables(host,tablename,groupname) values(?,?,'$group')");
foreach my $i (@$hosts) {
foreach my $j (@$names) {
if (param("${$i}[0]" . "${$j}[0]")) {
print "test: ","${$i}[0] : ${$j}[0]<br>\n";
$rep->execute("${$i}[0]", "${$j}[0]") || print "$! $DBI::errstr<br>\n";
}
}
}
}
#
# save user pref data submitted by the user
#
sub setupusersubmit {
my ($dbh, $remuser, $group) = @_;
my $tables = getarrays($dbh, 'hosttables',
"-select", 'distinct tablename',
"-clauses", "where groupname = '$group'");
$dbh->do("delete from userprefs where user = '$remuser' and groupname = '$group'");
my $rep = $dbh->prepare("insert into userprefs(user, groupname, tablename, columnname, displayit) values('$remuser', '$group', ?, ?, 'N')");
my ($i, $j);
foreach my $i (@$tables) {
my $sth = $dbh->prepare("select * from ${$i}[0] where 1 = 0");
$sth->execute();
foreach $j (@{$sth->{NAME}}) {
if (param("${$i}[0]" . "$j")) {
$rep->execute("${$i}[0]", "$j");
}
}
}
}
sub Exit {
my ($dbh, $group) = @_;
my $tq = self_url();
$tq =~ s/\?.*//;
print "<hr>\n";
print "<a href=\"$tq\">[TOP]</a>\n";
print "<a href=\"$tq?userprefs=1&group=$group\">[display options]</a>\n";
print "<a href=\"$tq?setuponcall=1\">[setup oncall schedule]</a>\n";
if (defined($group)) {
print "<a href=\"$tq?group=$group\">[group: $group]</a>\n";
print "<a href=\"$tq?group=$group&summarizegroup=1\">[summarize errors]</a>\n";
}
$dbh->disconnect() if (defined($dbh));
return OK();
# exit shift;
}
#
# setup user preferences by displaying a configuration array of
# checkbuttons for each table.
#
sub setupuserpreferences {
my ($dbh, $remuser, $group) = @_;
my $tables = getarrays($dbh, 'hosttables',
"-select", 'distinct tablename',
"-clauses", "where groupname = '$group'");
print "<h3>Select the columns from the tables that you want to <b>hide</b> below and click on submit:</h3>\n";
print "<form method=\"post\" action=\"" . self_url() . "\">\n";
my ($i, $j);
foreach my $i (@$tables) {
my $sth = $dbh->prepare("select * from ${$i}[0] where 1 = 0");
$sth->execute();
displayconfigarray($dbh, [${$i}[0]], $sth->{NAME},
-check, "select * from userprefs where (tablename = ? and columnname = ? and user = '$remuser' and groupname = '$group' and displayit = 'N')");
print "<br>\n";
}
print "<input type=hidden name=group value=\"$group\">\n";
print "<input type=submit value=submit name=\"setupuserprefssubmit\">\n";
print "</form>";
}
|