#!/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);
# 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='false';
# Check if the host is already stopped
$pos=0;
foreach $line (@lines) {
        chomp($line);
        if ($line eq $host) {
		splice @lines, $pos, 1;
		$oktogo='true';
                last;
        } else {
		$pos++;
	}
}

if ($oktogo eq 'true') {
	open(CONF, "> $host_list_file") or warn "Cannot open $host_list_file for writing:!\n";
        foreach $line (@lines) {
		chomp($line);
                print CONF "$line\n";
        }
        close(CONF);
	# Send email to notify that monitoring was restarted
	open(SENDMAIL, "|/usr/lib/sendmail -oi -t") or warn "Cant fork for sendmail: $!\n";
	print SENDMAIL <<"EOF";
From: Fault_manager
To: root
Subject: Monitoring restarted for $host
Monitoring has been restarted for $host by $user.
EOF
	close(SENDMAIL) or warn "Sendmail didn't close nicely";
} else {
	print "Monitoring was not stopped for $host.\n";
}
