From 6eb99a216842b443295c1d54e05dee3674fe6732 Mon Sep 17 00:00:00 2001 From: Digimer Date: Tue, 22 Nov 2022 01:10:53 -0500 Subject: [PATCH] * FInished the anvil-manage-alerts tool. It can now send test alerts at a user-requested alert level. Signed-off-by: Digimer --- share/words.xml | 14 ++++- tools/anvil-manage-alerts | 114 ++++++++++++++++++++++++++++++++++++-- 2 files changed, 122 insertions(+), 6 deletions(-) diff --git a/share/words.xml b/share/words.xml index 14c44ab4..4be9d6a3 100644 --- a/share/words.xml +++ b/share/words.xml @@ -556,7 +556,17 @@ The definition data passed in was: [ Error ] - I was asked to delete a notification over-ride, but no 'notification_uuid' was provides. [ Error ] - The alert recipient UUID: [#!variable!uuid!#] wasn't found in the database. [ Error ] - The host UUID: [#!variable!uuid!#] wasn't found in the database. - + [ 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" + [ 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" + @@ -2782,6 +2792,8 @@ Proceed? [y/N] * Host: ...... [#!variable!old_host!#] * Alert Level: [#!variable!old_level!#] Proceed? [y/N] + This is a test alert message sent at alert level: [#!variable!level!#]. + Failed to send a test alert at level: [#!variable!level!#]. Is anyone listening at that level? Is the mail server configured? Normal Password diff --git a/tools/anvil-manage-alerts b/tools/anvil-manage-alerts index 36b98797..06281ba4 100755 --- a/tools/anvil-manage-alerts +++ b/tools/anvil-manage-alerts @@ -22,6 +22,12 @@ $anvil->Get->switches({list => [ "add", "edit", "delete", + "alert-overrides", + "alert-override-uuid", + "alert-override-recipient-uuid", + "alert-override-host-uuid", + "alert-override-alert-level", + "level", "mail-servers", "mail-server-uuid", "mail-server-address", @@ -31,17 +37,13 @@ $anvil->Get->switches({list => [ "mail-server-security", "mail-server-authentication", "mail-server-helo-domain", - "alert-overrides", - "alert-override-uuid", - "alert-override-recipient-uuid", - "alert-override-host-uuid", - "alert-override-alert-level", "recipients", "recipient-uuid", "recipient-name", "recipient-email", "recipient-language", "recipient-level", + "test", "y", "yes", ]}); @@ -78,6 +80,10 @@ elsif ($anvil->data->{switches}{"alert-overrides"}) }}); handle_alert_overrides($anvil); } +elsif ($anvil->data->{switches}{"test"}) +{ + handle_test_alert($anvil); +} else { $anvil->data->{sys}{show}{mail_servers} = 1; @@ -100,6 +106,68 @@ $anvil->nice_exit({exit_code => 0}); # 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 { my ($anvil) = @_; @@ -1442,6 +1510,42 @@ sub check_switches $anvil->data->{sys}{say_old_security} = ""; $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. foreach my $switch ("mail-server-uuid", "alert-override-uuid", "recipient-uuid", "alert-override-recipient-uuid", "alert-override-host-uuid") {