#!/bin/perl
# Get the host from the command line
$host=$ARGV[0];
$user=`id`;
$start=index($user, "(");
$end=index($user, ")");
$user=substr($user, $start+1, $end-$start-1);
if ($user eq 'root') {
	print "This can't be run as root, su to yourself and try again...\n";
	exit;
}
# Read in the existing file of stopped hosts
$host_list_file = '/opt/bin/alert/hosts.conf';
open(CONF, "< $host_list_file") or warn "Cannot open $host_list_file:$!\n";
@lines=<CONF>;
close(CONF);

$oktogo='true';
# Check if the host is already stopped
foreach $line (@lines) {
	chomp($line);
	if ($line eq $host) {
		print "Monitoring already stopped for $host.\n";
		$oktogo='false';	
		last;
	}
}

# Add host to list and rewrite file
if ($oktogo eq 'true') {
	push @lines, $host;
	open(CONF, "> $host_list_file") or warn "Cannot open $host_list_file for writing:!\n";
	foreach $line (@lines) {
		print CONF "$line\n";
	}
	close(CONF);

	# Send email to notify that monitoring was stopped
	open(SENDMAIL, "|/usr/lib/sendmail -oi -t") or warn "Cant fork for sendmail: $!\n";
	print SENDMAIL <<"EOF";
From: Fault_manager
To: root
Subject: Monitoring stopped for $host
Monitoring has been stopped for $host by $user.
EOF
close(SENDMAIL) or warn "Sendmail didn't close nicely";
}
print "Monitoring has now been stopped for $host by $user.\n";
print "You can view /opt/bin/alert/hosts.conf to see which hosts have monitoring stopped.\n";
