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 "
\n";
print "\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 "\n";
print "No Data to Graph
\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 "\n";
print "UCD-SNMP Management Console
\n";
print "
\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 "\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 "oncall schedule for user: $remuser\n";
print "oncall schedule for user: $remuser
\n";
print "Please select your oncall schedule and mailing addresses for your groups below:";
if (!isexpert($remuser)) {
print "
\n";
print "- Values for the days/hours fields can be comma seperated lists of hours/days/ranges. EG: hours: 7-18,0-4.\n";
print "
\n";
}
print "\n";
return Exit($dbh, $group);
}
#===========================================================================
# show the list of groups a user belongs to.
#===========================================================================
if (!defined($group)) {
my @groups = getgroupsforuser($dbh, $remuser);
print "Net-SNMP Group List\n";
print "Host groupings you may access:
\n";
if (!isexpert($remuser)) {
print "\n";
print "- Click on a group to operate or view the hosts in that group.\n";
print "
- Click on a red status light below to list the problems found.\n";
print "
\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 "Status | ";
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 "{groupname}&summarizegroup=1") . "\"> | \n";
return;
}
}
print " | \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 "ERROR: host $newhost already in $group\n";
}
CGI::delete('newhost');
}
}
#===========================================================================
# display setup configuration for a group
#===========================================================================
if (defined(param('setupgroup'))) {
if (isadmin($dbh, $remuser, $group)) {
setupgroup($dbh, $group);
} else {
print "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 "Entries submitted";
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 "Entries submitted";
return Exit($dbh, $group);
}
#===========================================================================
# summarize problems in a group
#===========================================================================
if (defined(param('summarizegroup'))) {
print "group problem summary: $group\n";
print "The following is a list of problems in the group \"$group\":
\n";
summarizeerrors($dbh, "where groupname = '$group'");
return Exit($dbh, $group);
}
#===========================================================================
# summarize problems on a host
#===========================================================================
if (defined($host) && defined(param('summarizehost'))) {
print "host summary: $host\n";
print "The following is a list of problems for the host \"$host\":
\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 "Net-SNMP Host $host\n";
print "Hosts in the group \"$group\":
\n";
if (!isexpert($remuser)) {
print "\n";
if (isadmin($dbh, $remuser, $group)) {
my $q = self_url();
$q =~ s/\?.*//;
print "- Make sure you set up the host for the SNMP tables you want to monitor.\n";
}
print "
- Click on a hostname to operate on or view the information tables associated with that group.\n";
print "
- Click on a red status light below to list the problems found in with a particular host.\n";
print "
\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 "Status | ";
return;
}
if (checkhost($dbh, $group, $data->{'host'})) {
print "{host}") . "\"> | \n";
} else {
print " | \n";
}
}
);
if (isadmin($dbh, $remuser, $group)) {
addhostentryform($group);
my $q = self_url();
$q =~ s/\?.*//;
print "setup group $group\n";
}
return Exit($dbh, $group);
}
#===========================================================================
# setup the host's history records
#===========================================================================
if (param('setuphost')) {
print "Net-SNMP history setup for host: $host\n";
print "Net-SNMP history setup for the host: \"$host\"
\n";
print "
Enter the number of days to keep the data for a given table for the host \"$host\":\n";
if (!isexpert($remuser)) {
print "
\n";
print "- Numbers must be greater than or equal to 1 to enable history logging.\n";
print "
\n";
}
print "\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 "
Illegal table names in addition list: $x
\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 "
adding: ",join(", ",@x),"
\n";
}
} else {
print "
Add new MIB Tables or Groups that you want to collect for this host: \n";
}
my $q = self_url();
$q =~ s/\?.*//;
print "setup host $host\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 " | ";
} else {
print " | \n";
}});
my $tabletop = "
Host | Table | Description |
\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 "$row->{host} | $row->{tablename} | $expr->{returnfield}: $error->{$expr->{returnfield}} |
";
}
}
}
print "
";
}
#
# 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 "Net-SNMP manager report for host: $host\n";
print "Monitored information for the host $host
\n";
if (!isexpert($remuser)) {
print "\n";
print "- Click on a column name for information about the data in that column.\n";
print "
- Click on a column name or table name for information about the data in the table.\n";
print "
- If you are collecting past history for a data set, links will appear below the table that allow you to view and/or graph the historic data.\n";
print "
\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 " | ";
} else {
print " | \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 "{'tablename'}hist") . "\">[table]\n";
print "{'tablename'}hist") . "\">[graph]\n";
print "
\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 " | ";
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 " | ";
}
#
# 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 "\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 " |
$i | ";
if (ref($$mib{$i}) eq "HASH") {
print "\n";
foreach my $j (sort { $$mib{$i}{$a} <=> $$mib{$i}{$b} } keys(%{$$mib{$i}})) {
print " | $$mib{$i}{$j} | $j | ";
}
print " \n";
} else {
print "$$mib{$i}";
}
print " |
\n";
}
print "
\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 "";
}
#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 "";
}
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 "";
}
# 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]
\n";
$rep->execute("${$i}[0]", "${$j}[0]") || print "$! $DBI::errstr
\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 "
\n";
print "[TOP]\n";
print "[display options]\n";
print "[setup oncall schedule]\n";
if (defined($group)) {
print "[group: $group]\n";
print "[summarize errors]\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 "Select the columns from the tables that you want to hide below and click on submit:
\n";
print "";
}