Finished off template creation / deletion
authorJamie Cameron <jcameron@webmin.com>
Wed, 5 Mar 2008 01:37:10 +0000 (01:37 +0000)
committerJamie Cameron <jcameron@webmin.com>
Wed, 5 Mar 2008 01:37:10 +0000 (01:37 +0000)
status/delete_tmpls.cgi [new file with mode: 0644]
status/edit_tmpl.cgi [new file with mode: 0644]
status/lang/en
status/list_tmpls.cgi
status/log_parser.pl
status/save_tmpl.cgi [new file with mode: 0644]
status/status-lib.pl

diff --git a/status/delete_tmpls.cgi b/status/delete_tmpls.cgi
new file mode 100644 (file)
index 0000000..8c154b7
--- /dev/null
@@ -0,0 +1,23 @@
+#!/usr/local/bin/perl
+# Delete several templates
+
+require './status-lib.pl';
+$access{'edit'} || &error($text{'tmpls_ecannot'});
+
+# Validate inputs
+&error_setup($text{'dtmpls_err'});
+&ReadParse();
+@d = split(/\0/, $in{'d'});
+@d || &error($text{'dtmpls_enone'});
+
+# Waste them
+foreach $d (@d) {
+       $tmpl = &get_template($d);
+       if ($tmpl) {
+               &delete_template($tmpl);
+               }
+       }
+
+&webmin_log("deletes", "tmpl", scalar(@d));
+&redirect("list_tmpls.cgi");
+
diff --git a/status/edit_tmpl.cgi b/status/edit_tmpl.cgi
new file mode 100644 (file)
index 0000000..2bd107a
--- /dev/null
@@ -0,0 +1,63 @@
+#!/usr/local/bin/perl
+# Show a form for editing or creating a template
+
+require './status-lib.pl';
+$access{'edit'} || &error($text{'tmpls_ecannot'});
+&ReadParse();
+
+if ($in{'new'}) {
+       &ui_print_header(undef, $text{'tmpl_title1'}, "");
+       $tmpl = { };
+       }
+else {
+       &ui_print_header(undef, $text{'tmpl_title2'}, "");
+       $tmpl = &get_template($in{'id'});
+       }
+
+# Collapsible section with instructions
+print &ui_hidden_start($text{'tmpl_instr'}, 'instr', 0,
+                      "edit_tmpl.cgi?new=$in{'new'}&id=$in{'id'}");
+print $text{'tmpl_subs'},"<p>\n";
+print "<table>\n";
+foreach $s ('DESC', 'HOST', 'DATE', 'TIME', 'STATUS') {
+       print "<tr> <td><tt>\$\{$s\}</tt></td>\n";
+       print "<td>",$text{'tmpl_subs_'.lc($s)},"</td> </tr>\n";
+       }
+print "</table>\n";
+print &text('tmpl_subs2', '${IF-DOWN}', '${ELSE-DOWN}', '${ENDIF-DOWN}'),
+      "<br>\n";
+print &ui_hidden_end();
+
+# Start of form
+print &ui_form_start("save_tmpl.cgi", "post");
+print &ui_hidden("new", $in{'new'});
+print &ui_hidden("id", $in{'id'});
+print &ui_table_start($text{'tmpl_header'}, "width=100%", 2);
+
+# Description of this template
+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));
+
+# SMS 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'}));
+
+# Save buttons
+print &ui_table_end();
+print &ui_form_end($in{'new'} ?
+               [ [ undef, $text{'create'} ] ] :
+               [ [ undef, $text{'save'} ], [ 'delete', $text{'delete'} ] ]);
+
+&ui_print_footer("list_tmpls.cgi", $text{'tmpls_return'});
+
index 787f2be..51d2757 100644 (file)
@@ -16,7 +16,7 @@ index_refresh=Refresh Status
 index_refreshdesc=Perform an immediate refresh of all monitor statuses, instead of waiting for the next scheduled update.
 index_delete=Delete Selected
 index_group=Members of $1
-index_tmpls=List Templates
+index_tmpls=Edit Email Templates
 index_tmplsdesc=View and edit templates used to construct email messages sent when monitored services go down.
 
 type_apache=Apache Webserver
@@ -251,6 +251,10 @@ log_modify=Modified monitor $1
 log_delete=Deleted monitor $1
 log_sched=Changed scheduled monitoring
 log_deletes=Deleted $1 monitors
+log_tmpl_create=Created email template $1
+log_tmpl_modify=Modified email template $1
+log_tmpl_delete=Deleted email template $1
+log_tmpl_deletes=Deleted $1 email templates
 
 acl_edit=Can edit and create monitors?
 acl_sched=Can change scheduled monitoring?
@@ -413,3 +417,34 @@ rssh_wrongpass=Incorrect login
 rssh_failed=Connection failed
 rssh_error=SSH error
 
+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_add=Add a new email template.
+tmpls_return=templates list
+tmpls_delete=Delete Selected Templates
+
+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_sms1=Webmin default
+tmpl_sms0=Custom message below ..
+tmpl_pager=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
+tmpl_subs_host=Host being monitored
+tmpl_subs_date=Current date
+tmpl_subs_time=Current time
+tmpl_subs_status=Service status, such as <i>Down</i>
+tmpl_subs2=In addition, you can use conditional blocks like : <tt>$1 Service is down $2 Service is OK $3</tt>
+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
index 6835caa..038a003 100644 (file)
@@ -2,6 +2,7 @@
 # List all email templates
 
 require './status-lib.pl';
+$access{'edit'} || &error($text{'tmpls_ecannot'});
 &ui_print_header(undef, $text{'tmpls_title'}, "");
 
 @tmpls = &list_templates();
@@ -11,13 +12,14 @@ if (@tmpls) {
        print &ui_form_start("delete_tmpls.cgi", "post");
        print &ui_links_row(\@links);
        @tds = ( "width=5" );
-       print &ui_columns_start([ "", $text{'tmpl_desc'}, $text{'tmpl_msg'} ],
+       print &ui_columns_start([ "", $text{'tmpls_desc'}, $text{'tmpls_msg'} ],
                                100, 0, \@tds);
        foreach $tmpl (@tmpls) {
                $msg = $tmpl->{'msg'};
-               $msg = substr($msg, 0, 8)." ..." if (length($msg) > 80);
+               $msg = substr($msg, 0, 80)." ..." if (length($msg) > 80);
                print &ui_checked_columns_row(
-                       [ $tmpl->{'desc'}, &html_escape($msg) ],
+                       [ "<a href='edit_tmpl.cgi?id=$tmpl->{'id'}'>".
+                         "$tmpl->{'desc'}</a>", &html_escape($msg) ],
                        \@tds, "d", $tmpl->{'id'});
                }
        print &ui_columns_end();
index c63d128..b2e9cbe 100644 (file)
@@ -8,7 +8,14 @@ do 'status-lib.pl';
 sub parse_webmin_log
 {
 local ($user, $script, $action, $type, $object, $p) = @_;
-if ($action eq "deletes") {
+if ($type eq "template" && $action eq "deletes") {
+       return &text("log_tmpl_deletes", $object);
+       }
+elsif ($type eq "template") {
+       return &text("log_tmpl_${action}",
+                    "<i>".&html_escape($p->{'desc'})."</i>");
+       }
+elsif ($action eq "deletes") {
        return &text("log_deletes", $object);
        }
 else {
diff --git a/status/save_tmpl.cgi b/status/save_tmpl.cgi
new file mode 100644 (file)
index 0000000..d9fccad
--- /dev/null
@@ -0,0 +1,49 @@
+#!/usr/local/bin/perl
+# Create, update or delete a template
+
+require './status-lib.pl';
+$access{'edit'} || &error($text{'tmpls_ecannot'});
+&ReadParse();
+&error_setup($text{'tmpl_err'});
+
+# Get the template
+if (!$in{'new'}) {
+       $tmpl = &get_template($in{'id'});
+       }
+else {
+       $tmpl = { };
+       }
+
+if ($in{'delete'}) {
+       # Remove this template
+       &delete_template($tmpl);
+       &webmin_log("delete", "template", $tmpl->{'desc'});
+       }
+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'};
+       if ($in{'sms_def'}) {
+               delete($tmpl->{'sms'});
+               }
+       else {
+               $in{'sms'} =~ /\S/ || &error($text{'tmpl_esms'});
+               $tmpl->{'sms'} = $in{'sms'};
+               }
+       if ($in{'pager_def'}) {
+               delete($tmpl->{'pager'});
+               }
+       else {
+               $in{'pager'} =~ /\S/ || &error($text{'tmpl_epager'});
+               $tmpl->{'pager'} = $in{'pager'};
+               }
+
+       # Save or create
+       &save_template($tmpl);
+       &webmin_log($in{'new'} ? "create" : "modify", "template",
+                   $tmpl->{'desc'});
+       }
+&redirect("list_tmpls.cgi");
+
index 713a9d0..764a9f0 100644 (file)
@@ -468,7 +468,7 @@ sub get_template
 {
 local ($id) = @_;
 local %tmpl;
-&read_file("$templates_dir/$id");
+&read_file("$templates_dir/$id", \%tmpl);
 $tmpl{'id'} = $id;
 $tmpl{'file'} = "$templates_dir/$id";
 $tmpl{'msg'} =~ s/\\n/\n/g;
@@ -477,7 +477,7 @@ return \%tmpl;
 }
 
 # save_template(&template)
-# Creates or saves an email template
+# Creates or saves an email template. Also does locking.
 sub save_template
 {
 local ($tmpl) = @_;
@@ -486,15 +486,20 @@ $tmpl->{'file'} = "$templates_dir/$tmpl->{'id'}";
 local %write = %$tmpl;
 $write{'msg'} =~ s/\\/\\\\/g;
 $write{'msg'} =~ s/\n/\\n/g;
+if (!-d $templates_dir) {
+       &make_dir($templates_dir, 0755);
+       }
+&lock_file($tmpl->{'file'});
 &write_file($tmpl->{'file'}, \%write);
+&unlock_file($tmpl->{'file'});
 }
 
 # delete_template(&template)
-# Removes an existing template
+# Removes an existing template. Also does locking.
 sub delete_template
 {
 local ($tmpl) = @_;
-&unlink_file($tmpl->{'file'});
+&unlink_logged($tmpl->{'file'});
 }
 
 1;