#!/moira/bin/perl -Tw

# $Id: nagios-colo.gen 3956 2010-01-05 20:56:56Z zacheiss $
# 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-colo';

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-COLO' 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 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) = $sth->fetchrow_array) {
	next if $name eq "[NONE]";
	$name = lc($name);
	push(@allcolohosts, $name);
	print OUT <<END;
define host{
	host_name		$name
	alias			$name
	address			$name
	contact_groups          colo,dost
	use			generic-host
	}

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

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

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

END

    }

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

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

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

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

close(OUT);
}

$dbh->disconnect;

exit 0;


