* FInished the anvil-manage-alerts tool. It can now send test alerts at a user-requested alert level.

Signed-off-by: Digimer <digimer@alteeve.ca>
main
Digimer 2 years ago
parent 8b7a44cf75
commit 6eb99a2168
  1. 14
      share/words.xml
  2. 114
      tools/anvil-manage-alerts

@ -556,7 +556,17 @@ The definition data passed in was:
<key name="error_0389">[ Error ] - I was asked to delete a notification over-ride, but no 'notification_uuid' was provides.</key> <key name="error_0389">[ Error ] - I was asked to delete a notification over-ride, but no 'notification_uuid' was provides.</key>
<key name="error_0390">[ Error ] - The alert recipient UUID: [#!variable!uuid!#] wasn't found in the database.</key> <key name="error_0390">[ Error ] - The alert recipient UUID: [#!variable!uuid!#] wasn't found in the database.</key>
<key name="error_0391">[ Error ] - The host UUID: [#!variable!uuid!#] wasn't found in the database.</key> <key name="error_0391">[ Error ] - The host UUID: [#!variable!uuid!#] wasn't found in the database.</key>
<key name="error_0392">[ Error ] - The switch '--test' requires an alert level to send the test '--alert X' at. Valid alert levels are;
* 1 or "critical"
* 2 or "warning"
* 3 or "notice"
* 4 or "info"</key>
<key name="error_0393">[ Error ] - The test alert requires an alert level, but the passed: [#!variable!level!#] is incorrect. Valid options are:
* 1 or "critical"
* 2 or "warning"
* 3 or "notice"
* 4 or "info"</key>
<!-- Files templates --> <!-- Files templates -->
<!-- NOTE: Translating these files requires an understanding of which lines are translatable --> <!-- NOTE: Translating these files requires an understanding of which lines are translatable -->
<!-- comments and which are command lines that can't be changed! --> <!-- comments and which are command lines that can't be changed! -->
@ -2782,6 +2792,8 @@ Proceed? [y/N]</key>
* Host: ...... [#!variable!old_host!#] * Host: ...... [#!variable!old_host!#]
* Alert Level: [#!variable!old_level!#] * Alert Level: [#!variable!old_level!#]
Proceed? [y/N]</key> Proceed? [y/N]</key>
<key name="message_0306">This is a test alert message sent at alert level: [#!variable!level!#].</key>
<key name="message_0307">Failed to send a test alert at level: [#!variable!level!#]. Is anyone listening at that level? Is the mail server configured?</key>
<!-- Translate names (protocols, etc) --> <!-- Translate names (protocols, etc) -->
<key name="name_0001">Normal Password</key> <!-- none in mail-server --> <key name="name_0001">Normal Password</key> <!-- none in mail-server -->

@ -22,6 +22,12 @@ $anvil->Get->switches({list => [
"add", "add",
"edit", "edit",
"delete", "delete",
"alert-overrides",
"alert-override-uuid",
"alert-override-recipient-uuid",
"alert-override-host-uuid",
"alert-override-alert-level",
"level",
"mail-servers", "mail-servers",
"mail-server-uuid", "mail-server-uuid",
"mail-server-address", "mail-server-address",
@ -31,17 +37,13 @@ $anvil->Get->switches({list => [
"mail-server-security", "mail-server-security",
"mail-server-authentication", "mail-server-authentication",
"mail-server-helo-domain", "mail-server-helo-domain",
"alert-overrides",
"alert-override-uuid",
"alert-override-recipient-uuid",
"alert-override-host-uuid",
"alert-override-alert-level",
"recipients", "recipients",
"recipient-uuid", "recipient-uuid",
"recipient-name", "recipient-name",
"recipient-email", "recipient-email",
"recipient-language", "recipient-language",
"recipient-level", "recipient-level",
"test",
"y", "y",
"yes", "yes",
]}); ]});
@ -78,6 +80,10 @@ elsif ($anvil->data->{switches}{"alert-overrides"})
}}); }});
handle_alert_overrides($anvil); handle_alert_overrides($anvil);
} }
elsif ($anvil->data->{switches}{"test"})
{
handle_test_alert($anvil);
}
else else
{ {
$anvil->data->{sys}{show}{mail_servers} = 1; $anvil->data->{sys}{show}{mail_servers} = 1;
@ -100,6 +106,68 @@ $anvil->nice_exit({exit_code => 0});
# Functions # # Functions #
############################################################################################################# #############################################################################################################
sub handle_test_alert
{
my ($anvil) = @_;
# * 1 or "critical"
# * 2 or "warning"
# * 3 or "notice"
# * 4 or "info"
my $level = "";
my $say_level = "";
if (($anvil->data->{switches}{level} eq "critical") or ($anvil->data->{switches}{level} eq "1"))
{
$level = "critical";
$say_level = $anvil->Words->string({key => "unit_0024"});
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => {
level => $level,
say_level => $say_level,
}});
}
elsif (($anvil->data->{switches}{level} eq "warning") or ($anvil->data->{switches}{level} eq "2"))
{
$level = "warning";
$say_level = $anvil->Words->string({key => "unit_0025"});
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => {
level => $level,
say_level => $say_level,
}});
}
elsif (($anvil->data->{switches}{level} eq "notice") or ($anvil->data->{switches}{level} eq "3"))
{
$level = "notice";
$say_level = $anvil->Words->string({key => "unit_0026"});
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => {
level => $level,
say_level => $say_level,
}});
}
elsif (($anvil->data->{switches}{level} eq "info") or ($anvil->data->{switches}{level} eq "4"))
{
$level = "info";
$say_level = $anvil->Words->string({key => "unit_0027"});
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => {
level => $level,
say_level => $say_level,
}});
}
my $alert_uuid = $anvil->Alert->register({debug => 2, alert_level => $level, message => "message_0306", variables => { level => $say_level }, set_by => $THIS_FILE, sort_position => 0});
if ($alert_uuid)
{
print $anvil->Words->string({key => "message_0306", variables => { level => $say_level }})."\n";
$anvil->Email->send_alerts();
}
else
{
# No one is listening, or no mail servers are configured.
print $anvil->Words->string({key => "message_0307", variables => { level => $say_level }})."\n";
}
return(0);
}
sub handle_alert_overrides sub handle_alert_overrides
{ {
my ($anvil) = @_; my ($anvil) = @_;
@ -1442,6 +1510,42 @@ sub check_switches
$anvil->data->{sys}{say_old_security} = ""; $anvil->data->{sys}{say_old_security} = "";
$anvil->data->{sys}{say_old_auth} = ""; $anvil->data->{sys}{say_old_auth} = "";
# If we're given 'test', make sure the level is provides.
if ($anvil->data->{switches}{test})
{
if (not $anvil->data->{switches}{level})
{
# Test requires a level
$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, 'print' => 1, key => "error_0392"});
$problem = 1;
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { problem => $problem }});
}
# Is the level valid?
# * 1 or "critical"
# * 2 or "warning"
# * 3 or "notice"
# * 4 or "info"
if (($anvil->data->{switches}{level} ne "critical") &&
($anvil->data->{switches}{level} ne "1") &&
($anvil->data->{switches}{level} ne "warning") &&
($anvil->data->{switches}{level} ne "2") &&
($anvil->data->{switches}{level} ne "notice") &&
($anvil->data->{switches}{level} ne "3") &&
($anvil->data->{switches}{level} ne "info") &&
($anvil->data->{switches}{level} ne "4"))
{
# Invalid test alert level.
$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, 'print' => 1, key => "error_0393", variables => {
level => $anvil->data->{switches}{level},
}});
$problem = 1;
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { problem => $problem }});
}
}
# Validate UUIDs. # Validate UUIDs.
foreach my $switch ("mail-server-uuid", "alert-override-uuid", "recipient-uuid", "alert-override-recipient-uuid", "alert-override-host-uuid") foreach my $switch ("mail-server-uuid", "alert-override-uuid", "recipient-uuid", "alert-override-recipient-uuid", "alert-override-host-uuid")
{ {

Loading…
Cancel
Save