check_logfiles: 3.7.5.1
[omd.git] / packages / notifications-tt / notify-by-email.pl
blobbda7076cb9d09f36cfa6d2ab228ea4896c771948
1 #!/usr/bin/perl
3 # Notification Script used by OMD
4 # Joerg Linge 2011
7 use strict;
8 use warnings;
9 use Getopt::Long;
10 use Template;
12 my %macro;
13 my $template = '';
14 my $data = '';
15 my $output = '';
16 my $mail = $ENV{'OMD_ROOT'}.'/bin/mail -t';
17 my $verbose = 0;
19 GetOptions (
20 'option|o=s' => \%macro,
21 'type=s' => \%macro,
22 'template=s' => \$template,
23 'mail=s' => \$mail,
24 'verbose' => \$verbose,
27 if ( $template eq '' ){
28 print "Template not given\n";
29 usage();
30 exit 3;
32 if ( ! -e $template ){
33 print "Template \"$template\" not found\n";
34 exit 3;
37 map($macro{$_} =~ s/\\n/\n/gmx, keys %macro);
38 extract_ascii($macro{'LONGHOSTOUTPUT'});
39 extract_ascii($macro{'LONGSERVICEOUTPUT'});
40 process_template();
41 exit;
43 sub process_template {
44 open FILE, $template or die "Couldn't open file: $!";
45 while (<FILE>) {
46 #chomp;
47 next if ( /^#/ );
48 $data .= "$_";
50 close FILE;
51 my $template = Template->new({PRE_CHOMP => 1, POST_CHOMP => 0, EVAL_PERL => 1});
52 $template->process(\$data, \%macro, \$output) or die Template->error;
53 print $output if $verbose;
54 send_mail();
57 sub send_mail {
58 open (MAIL,"|$mail $macro{'CONTACTEMAIL'}") || die("Couldn't open $mail: $!");
59 print MAIL $output;
60 close MAIL;
63 sub usage {
64 print "
65 Usage:
66 $0 --template=<path to template> -o <MACRO>=<VALUE> -o ....
71 sub extract_ascii {
72 return unless defined $_[0];
73 $_[0] =~ s/.*
74 ^ASCII_NOTIFICATION_START$
75 \s*(.*)
76 ASCII_NOTIFICATION_END$
77 .*/$1/msx;