#!/usr/bin/perl
#
# Make an attempt at converting a bb-hosts file into
# a "mon.cf" file.
#
# I wouldn't recommend using the output of this in the
# long-term, but use it just to get yourself up and running
# with mon while you learn the syntax of the configuration
# file.
#
# Jim Trocki, trockij@transmeta.com
#
# $Id: convert-bb-hosts,v 1.3 1998/01/18 17:32:45 trockij Exp $
#
#    Copyright (C) 1998, Jim Trocki
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
use Getopt::Std;
getopts ("d");

while (<>) {
    next if (/^\s*#/ || /^\s*$/);
    chomp;

    ($ip, $host, $directives) =
    	/^\s*(\d+\.\d+\.\d+\.\d+)\s+([a-zA-Z0-9-]+)(\s*#.*)?/;

    if (!defined($directives)) {
    	print "watch $host\n";
	print "\tservice fping\n";
	print "\t\tinterval 10m\n";
	print "\t\tmonitor fping.monitor\n";
	print "\t\tperiod wd {Sun-Sat}\n";
	print "\t\t\talert mail.alert user\n";
	print "\t\t\talertevery 1h\n";
	print "\n";
	next;
    }
    $directives =~ s/BBDISPLAY//i;
    $directives =~ s/BBPAGER//i;
    $directives =~ s/^\s*#\s*//;
    $directives =~ s/\s*$//;

    @services = split (/\s+/, $directives);
    next if (@services == 0);

    print "watch $host\n";
    print "\tservice fping\n";
    print "\t\tinterval 10m\n";
    print "\t\tmonitor fping.monitor\n";
    print "\t\tperiod wd {Sun-Sat}\n";
    print "\t\t\talert mail.alert user\n";
    print "\t\t\talertevery 1h\n";

    foreach $s (@services) {
	if ($s =~ /^ftp/i) {
	    print "\tservice ftp\n";
	    print "\t\tinterval 5m\n";
	    print "\t\tmonitor ftp.monitor\n";
	    print "\t\tperiod wd {Sun-Sat}\n";
	    print "\t\t\talert mail.alert user\n";
	    print "\t\t\talertevery 1h\n";
	} elsif ($s =~ /^smtp/i) {
	    print "\tservice smtp\n";
	    print "\t\tinterval 8m\n";
	    print "\t\tmonitor smtp.monitor\n";
	    print "\t\tperiod wd {Sun-Sat}\n";
	    print "\t\t\talert mail.alert user\n";
	    print "\t\t\talertevery 1h\n";
	} elsif ($s =~ /^pop3/i) {
	    print "\tservice pop3\n";
	    print "\t\tinterval 18m\n";
	    print "\t\tmonitor pop3.monitor\n";
	    print "\t\tperiod wd {Sun-Sat}\n";
	    print "\t\t\talert mail.alert user\n";
	    print "\t\t\talertevery 1h\n";
	} elsif ($s =~ /^http/i) {
	    print "\tservice http\n";
	    print "\t\tinterval 12m\n";
	    print "\t\tmonitor http.monitor\n";
	    print "\t\tperiod wd {Sun-Sat}\n";
	    print "\t\t\talert mail.alert user\n";
	    print "\t\t\talertevery 1h\n";
	} else {
	    print STDERR "I don't know about the '$s' directive!\n";
	}
    }
    print "\n";
}
