#!/usr/bin/perl ############################################################################## # Perl script written by Vahid Hedayati May 2013 # nagalert.cgi - uses backend nagalert (service script - other script part of this project) # Provides a web interface to stop/start alerts for selected host # Script must be made available in the cgi-bin folder of the host running nagios # Ensure you configure the first two variables to match your current setup # status.dat and nagalert (bash script) # When you load up the page, you will see the first action which is to enable/disable alerts # for selected hosts. # Once you have defined your action you can either select one host or use: # 1. CTRL (selective multiple hosts, scroll down and select another whilst holding down control key) # 2. SHIFT (select lots of hosts that are next to each other) # Once selected If you are stopping define the time 1 - 24 then choose minutes, hours, days. ############################################################################## # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . ############################################################################## use CGI; my $q = CGI->new; my $config_file="/var/nagios/status.dat"; my $nagios_service="/etc/init.d/nagalert"; print "Content-type: text/html\n\n"; print "Nagios Host Alert Controller"; my $action=$q->param('action'); my $timevalue=$q->param('timevalue'); my $timeperiod=$q->param('timeperiod'); my @hostnames = $q->param('hostname'); sub uniq { return keys %{{ map { $_ => 1 } @_ }}; } sub gen_action() { print "\n"; } sub gen_time() { print ""; print ""; print "
"; print "\n"; print ""; print "\n"; print "
"; } sub gather_servers() { open(FILE1, $config_file); print "\n"; while () { $line=$_; $host=$2 if /(.*)host_name=(.*)/; if ( $hosts !~ "" ) { push @hosts,"\n"; } } print sort(" ", uniq(@hosts)), "\n"; print "\n"; } sub gen_page() { print "--> Please note time definition not used by start action
"; print "
\n"; print "
"; &gen_action; print ""; &gather_servers; print ""; &gen_time; print ""; print ""; print "
"; print "
"; } if ($action eq 'start') { foreach $hostname (@hostnames) { print "Enabling alerts for $hostname"; print "
";
  		system("$nagios_service start $hostname");
		print "
"; } }elsif ($action eq 'stop') { my $stoptime=""; if ( ($timeperiod ne '' ) && ($timevalue ne '' ) ) { $stoptime="$timevalue$timeperiod"; } else { $stoptime="10M"; } foreach $hostname (@hostnames) { print "Disabling alerts for $hostname for $stoptime"; print "
";
  		system("$nagios_service stop $stoptime $hostname");
		print "
"; } }else{ &gen_page; }