#!/moira/bin/perl -Tw

# $Id: nagios-wsh.gen,v 1.5 2008-04-17 17:24:37 zacheiss Exp $
# The following exit codes are defined and MUST BE CONSISTENT with the
# error codes the library uses:
$MR_DBMS_ERR = 47836421;
$MR_OCONFIG = 47836460;

$outdir = '/moira/dcm/nagios-wsh';

use DBI;

$dbh = DBI->connect("dbi:Oracle:moira", "moira", "moira")
    || exit $MR_DBMS_ERR;

$sth0 = $dbh->prepare("SELECT l.list_id, m.name " .
                      "FROM list l, machine m, serverhosts sh " .
                      "WHERE sh.value3 = l.name AND sh.service = " .
		      "'NAGIOS-WSH' AND m.mach_id = sh.mach_id")
    || exit $MR_DBMS_ERR;
$sth0->execute;

while (($root_list_id, $hostname) = $sth0->fetchrow_array) {
    umask 022;
    open(OUT, ">$outdir/$hostname") || exit $MR_OCONFIG;
    print OUT "# This file is automatically generated by Moira.  Do not edit.\n";
    $sth = $dbh->prepare("SELECT m.name, i.tag FROM machine m, imembers i " .
			 "WHERE i.list_id = " . $dbh->quote($root_list_id) .
			 "AND i.member_type = 'MACHINE' AND m.status = 1 " .
			 "AND i.member_id = m.mach_id AND i.direct = 1 ORDER BY m.name") 
	|| exit $MR_DBMS_ERR;
    $sth->execute;

    while (($name, $tag) = $sth->fetchrow_array) {
	next if $name eq "[NONE]";
	$name = lc($name);
	push(@allwshhosts, $name);
	print OUT <<END;
define host{
	host_name		$name
	alias			$name
	address			$name
	contact_groups          wsh
	use			generic-host
	}

define service{
  	host_name		$name
	contact_groups		wsh
	use			ping-service
	}

define hostescalation{
        host_name               $name
        contact_groups          wsh,wsh-mail
        first_notification      2
        last_notification       0
        notification_interval   0
        }

define serviceescalation{
        host_name               $name
        contact_groups          wsh,wsh-mail
        service_description     PING
        first_notification      2
        last_notification       0
        notification_interval   0
        }

END

	next if $tag == 0; 
	$sth1 = $dbh->prepare("SELECT s.string " .
			      "FROM strings s " .
			      "WHERE s.string_id = " . $tag)
	    || exit $MR_DBMS_ERR;
	$sth1->execute;
	my @tags = split /\s+/,$sth1->fetchrow_array;
	$sth1->finish;
	my %services;
	while (<@tags>) {
	    my $service = $_;
	    chomp $service;
	    if ($service =~ /^(FTP|NFS|HTTP|HTTPS|SMTP|SSHD|TELNET|TNS)$/i) {
		my $ucservice = $service;
		$ucservice =~ tr/a-z/A-Z/;
		my $lcservice = $service;
		$lcservice =~ tr/A-Z/a-z/;
		next if $services{$ucservice};
		$services{$ucservice}++;
		print OUT <<END;
define service{
	host_name		$name
	contact_groups		wsh
	use			$lcservice-service
	}

define serviceescalation{
	host_name		$name
	contact_groups		wsh,wsh-mail
	service_description	$ucservice
	first_notification	2
	last_notification	0
	notification_interval	0
	}

END
	    } elsif ($service =~ /^HTTPS-CERT$/i) {
		next if $services{'HTTPS-CERT'};
		$services{'HTTPS-CERT'}++;
		print OUT <<END;
define service{
        host_name               $name
        contact_groups          wsh-mail
        use                     https-cert-service
        }

END
	    } else { # Hopefully this is a URL of some sort
		my $protocol;
		my $server;
		my $path;
		if ($service =~ /^([a-z]*):\/\/([^\/]*)(.*)$/ ) {
		    $protocol = $1;
		    $server = $2;
		    $path = $3;
		    $protocol =~ tr/a-z/A-Z/;
		    $server =~ tr/A-Z/a-z/;
		}
		if ($protocol =~ /^HTTP(|S)$/) {
		    $path = "/" if ($path eq "");
		    if ($server eq $name && $path eq "/") { # this is a simple service
			push @tags,$protocol;
		    }
		    # prep a bunch of variables for the text we'll spit out
		    my $description = "$protocol-$server-$path";
		    $description =~ tr/A-Z/a-z/;
		    next if $services{$description};
		    $services{$description}++;
		    $description =~ s/-\//-/g;
		    $description =~ s/\/-/-/g;
		    $description =~ s/\//-/g;
		    $description =~ s/-$//g;
		    my $ucdescription = $description;
		    $ucdescription =~ tr/a-z/A-Z/;
		    my $SSL = "";
		    if ($protocol eq "HTTPS") {
			$SSL = "--SSL -k /var/nagios/etc/cert.pem";
		    }
		    print OUT <<END;
define command{
	command_name	check_$name-$description
	command_line	\$USER1\$/check_http -H $server $SSL -u $path
	}

define service{
	host_name		$name
	name			$description-service
	service_description	$ucdescription
	contact_groups		wsh
	check_command		check_$name-$description
	use			generic-service
	}

define serviceescalation{
	host_name		$name
	contact_groups		wsh,wsh-mail
	service_description	$ucdescription
	first_notification	2
	last_notification	0
	notification_interval	0
	}

END
		    if ($services{$protocol} && $name eq $server) {
		    print OUT <<END;
define servicedependency{
	dependent_host_name		$name
	dependent_service_description	$ucdescription
	host_name			$name
	service_description		$protocol
	execution_failure_criteria	n
	notification_failure_criteria	w,u,c
	}

END
		    }
		} else {
		    printf STDERR "Machine %s has unknown service %s; ignoring\n", $name, $service;
		}
	    }  # if SERVICE else URL
	} # while @tags
	if ($services{'HTTPS'} && $services{'HTTPS-CERT'}) {
	    print OUT <<END;
define servicedependency{
	dependent_host_name		$name
	dependent_service_description	HTTPS-CERT
	host_name			$name
	service_description		HTTPS
	execution_failure_criteria	n
	notification_failure_criteria	w,u,c
	}

END
	}
    } # while $sth->fetchrow_array

    print OUT <<END;
define hostgroup{
        hostgroup_name          wsh-hosts
        alias                   wsh-hosts
END

print OUT "\tmembers\t\t\t";

$maybecomma = "";
foreach $host (@allwshhosts) {
    print OUT "$maybecomma$host";
    $maybecomma = ",";
}

print OUT "\n\t}\n\n";

close(OUT);
}

$dbh->disconnect;

exit 0;


