📄 manager.pm
字号:
#===========================================================================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 ) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -