B<spong-message> [B<--debug>] I<color> I<host> I<service> I<time> I<message>
+B<spong-message> [B<--debug>] B<--color|--status> I<color> B<--host>
+I<hostname> B<--service> I<service> B<--time> I<time> B<--summary> I<summary>
+[B<--file> I<filename> | B<--message> I<detailed message>
+
=head1 DESCRIPTION
This program is called by the L<spong-server> to send out alerts. The
use Getopt::Long;
use FileHandle;
use English;
+use POSIX qw(strftime);
my %opt;
@options = ( "help", "host=s", "service=s", "time=i", "color|status=s", "summary=s",
&debug( "spong.groups file loaded" );
if (! defined $RULES_MATCH) { $RULES_MATCH = "OLD" };
+if (! $DATEFMT) { $DATEFMT = "%m/%d/%y"; }
+if (! $TIMEFMT) { $TIMEFMT = "%H:%M:%S"; }
+if (! $DATETIMEFMT) { $DATETIMEFMT = "%c"; }
# Read the little message history database, so we can prevent message overload
#
# Starting sending out the messages to $person
+ my($subj,$body);
if ($func eq "all") {
foreach $f (keys(%MSGFUNCS)) {
if (defined $HUMANS{$person}->{$f}) {
+
+ ($subj,$body) = eval_template($person,$f);
+
# Call the message function as referenced by the MSGFUNCS hash
- eval { (&{$MSGFUNCS{$f}}($HUMANS{$person}->{$f})); };
+ eval { (&{$MSGFUNCS{$f}}($HUMANS{$person}->{$f},$subj,$body)); };
if ($@) {
&error("No message function defined for $f: $@");
return 0;
}
+#--------------------------------------------------------------------------
+# Find the proper template for the a contacts and evaulate it for substitute
+# expressions. Return the subject and body seperately.
+#---------------------------------------------------------------------------
+
+sub eval_template {
+ my($rcpt, $func) = @_;
+ my($subj, $body);
+
+ # Find the right template to use
+ my($templ);
+# $templ = $TEMPLATES{"DEFAULT"} ||
+# $TEMPLATES{"$rcpt"} ||
+# $TEMPLATES{"$func"} ||
+# $TEMPLATES{"$rcpt:$func"};
+
+ $templ =
+ $TEMPLATES{"$rcpt:$func"} ||
+ $TEMPLATES{"$func"} ||
+ $TEMPLATES{"$rcpt"} ||
+ $TEMPLATES{"DEFAULT"};
+
+ if (! $templ) {
+ &error("No custom message template or default template found");
+ return("","");
+ }
+
+ $subj = fill_in($templ->{'subject'}) if defined $templ->{'subject'};
+ $body = fill_in($templ->{'body'}) if defined $templ->{'body'};
+
+ if (! $subj && ! $body) {
+ &error("Template evaulated to null Subject and Body fields");
+ }
+
+ return ($subj, $body);
+}
+
+#---------------------------------------------------------------------------
+# Fill template strings substition variables.
+#---------------------------------------------------------------------------
+
+sub fill_in {
+ my( $str ) = @_;
+ my($tmp);
+
+ $str =~ s/!!HOST!!/$host/g;
+ $str =~ s/!!SERVICE!!/$service/g;
+ $str =~ s/!!COLOR!!/$color/g;
+ $str =~ s/!!STATUS!!/$color/g;
+ $str =~ s/!!WWWSPONG!!/$WWWSPONG/g;
+ $str =~ s/!!SUMMARY!!/$summary/g;
+ $str =~ s/!!DETAILED!!/$message/g;
+
+ if ($str =~ /!!CURTIME!!/) {
+ $tmp = strftime($DATETIMEFMT,localtime());
+ $str =~ s/!!CURTIME!!/$tmp/g;
+ }
+ if ($str =~ /!!DATE!!/) {
+ $tmp = strftime($DATEFMT,localtime($time));
+ $str =~ s/!!DATE!!/$tmp/g;
+ }
+ if ($str =~ /!!TIME!!/) {
+ $tmp = strftime($TIMEFMT,localtime($time));
+ $str =~ s/!!TIME!!/$tmp/g;
+ }
+ if ($str =~ /!!DATETIME!!/) {
+ $tmp = strftime($DATETIMEFMT,localtime($time));
+ $str =~ s/!!DATETIME!!/$tmp/g;
+ }
+
+ return $str;
+}
+
+
+# --------------------------------------------------------------------------
+# Send a normal notification message to $recipient via e-mail
+# --------------------------------------------------------------------------
+sub email {
+ my ($recipient,$subject,$body) = @_;
+
+ open( MAIL, "|$SENDMAIL" ) || die "Can't open sendmail: $!";
+ print MAIL "To: $recipient\n";
+ print MAIL "Subject: $subject\n";
+ print MAIL "Content-Type: text/plain; charset=us-ascii\n";
+ print MAIL "\n"; # Header/Body boundry
+ print MAIL "$body\n";
+ close( MAIL );
+}
# --------------------------------------------------------------------------
# Send a normal notification message to $recipient via e-mail