#!/usr/bin/perl
#
# Script to automatically generate service escalations for Nagios.
#
# USAGE: Reads config information from STDIN (cat /etc/nagios/hosts/* | ./escalations.pl)
# and spits out a configuration file on STDOUT
#
# Licensed under LGPL version 2
# Copyright 2006 Broadwick Corporation
#
# By: Ian Kilgore


# Be sure to include the person/group that was ORIGNALLY paged as well as any additional people you want paged.
$contactgroups = "foo,bar"


while (<>) {
  %services;
  if ($_ =~ /define\s+service/) {  # "define" (one or more space characters) "service"
    while (<>) {
      if ($_ =~ /host_name\s+([A-Za-z0-9\-\.]+)/) {  # "host_name" (one or more space characters) (hostname)
        $hostname = $1;
      }
      elsif ($_ =~ /service_description\s+([A-Za-z0-9_]+)/) {
        $service = $1;
      }
      elsif ($_ =~ /\}/) {
	push(@{$services{$hostname}}, $service);
	last;
      }
    }
  }
}

foreach my $host (keys %services){
  foreach my $service (@{$services{$host}}) {
# We chose some sane defaults, feel free to change to your liking
	print "
define serviceescalation{
	host_name		$host
	service_description	$service
	first_notification	2
	last_notification	0
	contact_groups		$contactgroups
	notification_interval	60
	}";
  }
}



