Netsaint Bulk Status Tool

From Bubba.org

Jump to: navigation, search

This is a perl cgi script that basically parses your Netsaint hosts.cfg file, and allows you to select multiple machines and quickly unmanage or manage them.

This is pretty old, so I'm not even sure it still works anymore. YMMV.

#!/usr/bin/perl
# 
# Netsaint Bulk Status Tool
# v.1 11/1/2001
#
# This script allows one to easily disable or enable netsaint
# checks on multiple machines quickly and easily.  Simply drop 
# this script into your <netsaint>/sbin directory, set the 
# correct execute permissions on it, check permissions 
# on the 2 files below, set your perl path above, and rename
# the script to nsmanage.cgi.  Questions and comments can be 
# addressed to [email protected]
#
# location of the netsaint.cmd file.  Note, this file must be
# writable by whatever uid is executing the cgi script
# (ie, whatever uid your webserver runs as)
$netsaint_rw="/local/netsaint/var/rw/netsaint.cmd";
 
# location of the hosts.cfg file.  Note, this file must
# be readable by whatever uid is executing the cgi script 
$hosts_cfg="/local/netsaint/etc/hosts.cfg";
 
# Todo:  Add an additional option that will let you 
# schedule downtime for multiple machines.  There is 
# little reason to implement this now since Netsaint 
# does not preserve scheduled downtimes across program
# restarts or shutdowns (hopefully they are working on
# this).
#################################################################
 
print "Content-type: text/html\n\n";
print "<html>\n<head>\n";
print "<title>Netsaint Bulk Status Tool</title>";
print "</head>\n<body bgcolor=#ffffff>\n<br>";
 
&ReadParse;
 
if (($FORM{option} eq "dis") && ($FORM{hosts})) {
   @hosts=split(/\,/,$FORM{hosts});
   foreach $host (@hosts) {
     print "Disabling netsaint host and service checks for $host .<br>\n";
     open(NSRW, ">$netsaint_rw") or print "Cannot open $netsaint_rw:$!\n";
     $time=time;
     # I'm not sure if all of these are required, nor if these
     # are the "correct" commands I should be using, but they
     # have seemed to work for us.
     print NSRW "[$time] DISABLE_HOST_SVC_CHECKS;$host\n";
     print NSRW "[$time] DISABLE_HOST_CHECK;$host\n";
     print NSRW "[$time] DISABLE_HOST_SVC_NOTIFICATIONS;$host\n";
     print NSRW "[$time] DISABLE_HOST_NOTIFICATIONS;$host\n";
     print NSRW "[$time] ACKNOWLEDGE_HOST_PROBLEM;$host;0;acknowledged\n";
     close(NSRW);
   }
} elsif (($FORM{option} eq "ren") && ($FORM{hosts})) {
   @hosts=split(/\,/,$FORM{hosts});
   foreach $host (@hosts) {
     print "Enabling netsaint host and service checks for $host .<br>\n";
     open(NSRW, ">$netsaint_rw") or print "Cannot open $netsaint_rw:$!\n";
     $time=time;
     print NSRW "[$time] ENABLE_HOST_SVC_CHECKS;$host\n";
     print NSRW "[$time] ENABLE_HOST_CHECK;$host\n";
     print NSRW "[$time] ENABLE_HOST_SVC_NOTIFICATIONS;$host\n";
     print NSRW "[$time] ENABLE_HOST_NOTIFICATIONS;$host\n";
     close(NSRW);
   }
} else { 
   print "<form method=post><table border=5><tr><td>\n";
   print "<b><center>Select Machine(s)</td><td>\n";
   print "<b>Perform Action on Selected Machines</td></tr><tr><td>\n";
   print "<center><select multiple size=15 name=hosts>\n";
   %hosts=&GetHosts;
   foreach $host (sort keys %hosts) {
	print "<option value=\"$host\">$host\n"; 
   }
   print "</select></td><td><select name=option size=1>\n";
   print "<option value=dis>Disable Netsaint Checks\n";
   print "<option value=ren>Enable Netsaint Checks\n";
   print "</select><br><br><input type=submit value=\"Perform";
   print " Action\"></td></tr></table>\n";
}
 
sub GetHosts {
    my %hosts=();
    open(CFG, "$hosts_cfg") or print "Cannot open $hosts_cfg: $!\n";
    while(<CFG>) {
	chomp;
	if (/^host\[(.*)\]\=/) {
		$hosts{$1}++;
	}
    }
    close(CFG);
    return %hosts;
}
 
sub ReadParse {
    foreach (@_) {
        $_=~s/\r\n/ /g;
    }
    local (*FORM) = @_;
    local ($request_method, $query_string, @key_value_pairs,
               $key_value, $key, $value);
 
    $request_method = $ENV{'REQUEST_METHOD'};
 
    if ($request_method eq "GET") {
        $query_string = $ENV{'QUERY_STRING'};
    } elsif ($request_method eq "POST") {
        read(STDIN,$query_string,$ENV{'CONTENT_LENGTH'});
    } else {
        print "Content-type: text/html\n\n";
    }
 
    @key_value_pairs = split(/&/, $query_string);
 
    foreach $key_value (@key_value_pairs) {
        ($key,$value) = split(/=/, $key_value);
        $value =~ tr/+/ /;
        $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
            $value =~ s/~!/ ~!/g; 
        if (defined($FORM{$key})) {
            $FORM{$key} = join(",",$FORM{$key}, $value);
        }
        else {
            $FORM{$key} = $value;
        }
    }
}