Completed support for custom templates
authorJamie Cameron <jcameron@webmin.com>
Thu, 6 Mar 2008 07:10:12 +0000 (07:10 +0000)
committerJamie Cameron <jcameron@webmin.com>
Thu, 6 Mar 2008 07:10:12 +0000 (07:10 +0000)
status/CHANGELOG
status/config.info
status/delete_tmpls.cgi
status/edit_mon.cgi
status/edit_tmpl.cgi
status/lang/en
status/list_tmpls.cgi
status/monitor.pl
status/save_tmpl.cgi
status/status-lib.pl

index 59285c0..2fbb6fc 100644 (file)
@@ -49,3 +49,5 @@ Added support for sending status messages via SMS, for common US mobile carriers
 Failure to run a command on a remote system is now properly detected.
 ---- Changes since 1.390 ----
 Added T-COM, Skytel, Vodaphone and several more as a carriers for SMS.
+---- Changes since 1.400 ----
+Email, SMS and SNMP messages sent by Webmin can be customized, with multiple message templates defined and a different one selected for each monitor.
index e02fced..95d77b2 100644 (file)
@@ -6,6 +6,7 @@ sort_mode=Display monitors sorted by,1,0-Order created,1-Description,2-Host,3-La
 columns=Columns for services table,1,2-2,1-1
 output=Capture output from programs run when down?,1,1-Yes,0-No
 subject_mode=Email subject mode,1,0-New style,1-Old style
+def_tmpl=Default template,10,-None,Named template
 pinger=Send pings with command,10,-Use builtin code,linux-Use command suitable for Linux or FreeBSD,Other command
 snmp_server=Send SNMP traps to server,3,None
 snmp_version=SNMP protocol version,1,3-v3,2-v2,1-v1
index 8c154b7..9d7ad3c 100644 (file)
@@ -10,6 +10,17 @@ $access{'edit'} || &error($text{'tmpls_ecannot'});
 @d = split(/\0/, $in{'d'});
 @d || &error($text{'dtmpls_enone'});
 
+# Check for users
+@servs = &list_services();
+foreach $d (@d) {
+       @users = grep { $_->{'tmpl'} eq $d } @servs;
+       if (@users) {
+               $tmpl = &get_template($d);
+               &error(&text('dtmpls_eusers', "<i>$tmpl->{'desc'}</i>",
+                       join(", ", map { "<i>$_->{'desc'}</i>" } @users)));
+               }
+       }
+
 # Waste them
 foreach $d (@d) {
        $tmpl = &get_template($d);
index a577fd5..8a4903c 100755 (executable)
@@ -151,8 +151,17 @@ print &ui_table_row($text{'mon_email'},
 # Show template to use
 @tmpls = &list_templates();
 if (@tmpls) {
+       if ($in{'type'}) {
+               ($deftmpl) = grep { $_->{'desc'} eq $config{'def_tmpl'}} @tmpls;
+               if ($deftmpl) {
+                       $tid = $deftmpl->{'id'};
+                       }
+               }
+       else {
+               $tid = $serv->{'tmpl'};
+               }
        print &ui_table_row($text{'mon_tmpl'},
-               &ui_select("tmpl", $serv->{'tmpl'},
+               &ui_select("tmpl", $tid,
                           [ [ "", "&lt;$text{'mon_notmpl'}&gt;" ],
                             map { [ $_->{'id'}, $_->{'desc'} ] } @tmpls ]));
        }
index 2bd107a..17624d5 100644 (file)
@@ -39,19 +39,19 @@ print &ui_table_row($text{'tmpl_desc'},
        &ui_textbox("desc", $tmpl->{'desc'}, 60));
 
 # Email message
-print &ui_table_row($text{'tmpl_msg'},
-       &ui_textarea("msg", $tmpl->{'msg'}, 5, 60));
+print &ui_table_row($text{'tmpl_email'},
+       &ui_textarea("email", $tmpl->{'email'}, 5, 60));
 
-# SMS message
+# SMS / pager message
 print &ui_table_row($text{'tmpl_sms'},
        &ui_radio("sms_def", $tmpl->{'sms'} ? 0 : 1,
                  [ [ 1, $text{'tmpl_sms1'} ], [ 0, $text{'tmpl_sms0'} ] ]).
        "<br>\n".
        &ui_textarea("sms", $tmpl->{'sms'}, 3, 60));
 
-# Pager message
-print &ui_table_row($text{'tmpl_pager'},
-       &ui_opt_textbox("pager", $tmpl->{'pager'}, 50, $text{'tmpl_sms1'}));
+# SNMP message
+print &ui_table_row($text{'tmpl_snmp'},
+       &ui_opt_textbox("snmp", $tmpl->{'snmp'}, 50, $text{'tmpl_sms1'}));
 
 # Save buttons
 print &ui_table_end();
index ed5c6b3..627a22d 100644 (file)
@@ -422,7 +422,7 @@ tmpls_title=Email Templates
 tmpls_ecannot=You are not allowed to edit email templates
 tmpls_none=No email templates have been defined yet. All alerts will be sent using the Webmin default messages.
 tmpls_desc=Description
-tmpls_msg=Email message
+tmpls_email=Email message
 tmpls_add=Add a new email template.
 tmpls_return=templates list
 tmpls_delete=Delete Selected Templates
@@ -431,11 +431,11 @@ tmpl_title1=Create Email Template
 tmpl_title2=Edit Email Template
 tmpl_header=Alert email template messages
 tmpl_desc=Template description
-tmpl_msg=Email message
-tmpl_sms=SMS message
+tmpl_email=Email message
+tmpl_sms=SMS / pager message
 tmpl_sms1=Webmin default
 tmpl_sms0=Custom message below ..
-tmpl_pager=Pager message
+tmpl_snmp=Pager message
 tmpl_instr=Instructions and substitutions
 tmpl_subs=The email, SMS and pager messages defined on this page can use the following substitutions :
 tmpl_subs_desc=Service description
@@ -447,5 +447,11 @@ tmpl_subs2=In addition, you can use conditional blocks like : <tt>$1 Service is
 tmpl_err=Failed to save email template
 tmpl_edesc=Missing template description
 tmpl_emsg=Missing email message
-tmpl_epager=Missing SMS message
-tmpl_epager=Missing pager message
+tmpl_esms=Missing SMS message
+tmpl_esnmp=Missing SNMP message
+tmpl_err2=Failed to delete email template
+tmpl_eusers=It is in use by the following monitors : $1
+
+dtmpls_err=Failed to delete templates
+dtmpls_enone=None selected
+dtmpls_eusers=$1 is in use by the following monitors : $2
index 038a003..3115d87 100644 (file)
@@ -12,10 +12,10 @@ if (@tmpls) {
        print &ui_form_start("delete_tmpls.cgi", "post");
        print &ui_links_row(\@links);
        @tds = ( "width=5" );
-       print &ui_columns_start([ "", $text{'tmpls_desc'}, $text{'tmpls_msg'} ],
-                               100, 0, \@tds);
+       print &ui_columns_start(
+           [ "", $text{'tmpls_desc'}, $text{'tmpls_email'} ], 100, 0, \@tds);
        foreach $tmpl (@tmpls) {
-               $msg = $tmpl->{'msg'};
+               $msg = $tmpl->{'email'};
                $msg = substr($msg, 0, 80)." ..." if (length($msg) > 80);
                print &ui_checked_columns_row(
                        [ "<a href='edit_tmpl.cgi?id=$tmpl->{'id'}'>".
index 5f524f8..db11a30 100755 (executable)
@@ -28,7 +28,9 @@ if ($ARGV[0] ne "--force") {
 @services = sort { &sort_func($a, $b) } @services;
 
 # Check for services that are down
-$now = localtime(time());
+$nowunix = time();
+$now = &make_date($nowunix);
+($nowdate, $nowtime) = split(/\s+/, $now);
 $thishost = &get_system_hostname();
 $ecount = 0;
 foreach $serv (@services) {
@@ -152,20 +154,20 @@ foreach $serv (@services) {
                        $subj = &text('monitor_sub_'.$suffix,
                                      $serv->{'desc'}, $host);
                        if ($notify{'pager'}) {
-                               $pager_msg .= &text('monitor_pager_'.$suffix,
-                                               $host, $serv->{'desc'}, $now);
+                               $pager_msg .= &make_message($suffix, $host,
+                                                           $serv, 'pager');
                                }
                        if ($notify{'sms'}) {
-                               $sms_msg .= &text('monitor_pager_'.$suffix,
-                                               $host, $serv->{'desc'}, $now);
+                               $sms_msg .= &make_message($suffix, $host,
+                                                          $serv, 'sms');
                                }
                        if ($notify{'snmp'}) {
-                               push(@snmp_msg, &text('monitor_snmp_'.$suffix,
-                                                     $host, $serv->{'desc'}));
+                               push(@snmp_msg, &make_message($suffix, $host,
+                                                              $serv, 'snmp'));
                                }
                        if ($notify{'email'}) {
-                               $thisemail .= &text('monitor_email_'.$suffix,
-                                                   $host, $serv->{'desc'}, $now)."\n";
+                               $thisemail .= &make_message($suffix, $host,
+                                                              $serv, 'email');
                                if ($out) {
                                        $thisemail .= $out;
                                        }
@@ -442,3 +444,44 @@ $t =~ s/([=\177-\377])/sprintf("=%2.2X",ord($1))/ge;
 return $t;
 }
 
+# make_message(status, host, &server, type)
+# Returns the message for some email, SMS or SNMP. May use a template, or
+# the built-in default.
+sub make_message
+{
+local ($suffix, $host, $serv, $type) = @_;
+local $tmpl = $serv->{'tmpl'} ? &get_template($serv->{'tmpl'}) : undef;
+if ($tmpl && $tmpl->{$type}) {
+       # Construct from template
+       local %hash = ( 'DESC' => $serv->{'desc'},
+                       'HOST' => $host,
+                       'DATE' => $nowdate,
+                       'TIME' => $nowtime,
+                       'STATUS' => $text{'mon_'.$suffix},
+                       uc($suffix) => 1,
+                     );
+       foreach my $k (keys %$serv) {
+               $hash{'SERVICE_'.uc($k)} = $serv->{$k};
+               }
+       local $rv = &substitute_template($tmpl->{$type}, \%hash);
+       $rv =~ s/[\r\n]+$//;
+       $rv .= "\n";
+       return $rv;
+       }
+else {
+       # Use built-in
+       if ($type eq 'sms') {
+               return &text('monitor_pager_'.$suffix,
+                            $host, $serv->{'desc'}, $now);
+               }
+       elsif ($type eq 'snmp') {
+               return &text('monitor_snmp_'.$suffix,
+                            $host, $serv->{'desc'});
+               }
+       elsif ($type eq 'email') {
+               return &text('monitor_email_'.$suffix,
+                            $host, $serv->{'desc'}, $now)."\n";
+               }
+       }
+}
+
index d9fccad..6e56c14 100644 (file)
@@ -16,6 +16,10 @@ else {
 
 if ($in{'delete'}) {
        # Remove this template
+       &error_setup($text{'tmpl_err2'});
+       @users = grep { $_->{'tmpl'} eq $tmpl->{'id'} } &list_services();
+       @users && &error(&text('tmpl_eusers',
+                       join(", ", map { "<i>$_->{'desc'}</i>" } @users)));
        &delete_template($tmpl);
        &webmin_log("delete", "template", $tmpl->{'desc'});
        }
@@ -23,8 +27,8 @@ else {
        # Validate and store inputs
        $in{'desc'} =~ /\S/ || &error($text{'tmpl_edesc'});
        $tmpl->{'desc'} = $in{'desc'};
-       $in{'msg'} =~ /\S/ || &error($text{'tmpl_emsg'});
-       $tmpl->{'msg'} = $in{'msg'};
+       $in{'email'} =~ /\S/ || &error($text{'tmpl_eemail'});
+       $tmpl->{'email'} = $in{'email'};
        if ($in{'sms_def'}) {
                delete($tmpl->{'sms'});
                }
@@ -32,12 +36,12 @@ else {
                $in{'sms'} =~ /\S/ || &error($text{'tmpl_esms'});
                $tmpl->{'sms'} = $in{'sms'};
                }
-       if ($in{'pager_def'}) {
-               delete($tmpl->{'pager'});
+       if ($in{'snmp_def'}) {
+               delete($tmpl->{'snmp'});
                }
        else {
-               $in{'pager'} =~ /\S/ || &error($text{'tmpl_epager'});
-               $tmpl->{'pager'} = $in{'pager'};
+               $in{'snmp'} =~ /\S/ || &error($text{'tmpl_esnmp'});
+               $tmpl->{'snmp'} = $in{'snmp'};
                }
 
        # Save or create
index 764a9f0..87f8852 100644 (file)
@@ -468,11 +468,11 @@ sub get_template
 {
 local ($id) = @_;
 local %tmpl;
-&read_file("$templates_dir/$id", \%tmpl);
+&read_file("$templates_dir/$id", \%tmpl) || return undef;
 $tmpl{'id'} = $id;
 $tmpl{'file'} = "$templates_dir/$id";
-$tmpl{'msg'} =~ s/\\n/\n/g;
-$tmpl{'msg'} =~ s/\\\\/\\/g;
+$tmpl{'email'} =~ s/\\n/\n/g;
+$tmpl{'email'} =~ s/\\\\/\\/g;
 return \%tmpl;
 }
 
@@ -484,8 +484,8 @@ local ($tmpl) = @_;
 $tmpl->{'id'} ||= time().$$;
 $tmpl->{'file'} = "$templates_dir/$tmpl->{'id'}";
 local %write = %$tmpl;
-$write{'msg'} =~ s/\\/\\\\/g;
-$write{'msg'} =~ s/\n/\\n/g;
+$write{'email'} =~ s/\\/\\\\/g;
+$write{'email'} =~ s/\n/\\n/g;
 if (!-d $templates_dir) {
        &make_dir($templates_dir, 0755);
        }