From 97fb4687c85575f733c66668692dfb353a5c2cba Mon Sep 17 00:00:00 2001 From: Matthew Cox Date: Tue, 1 Nov 2016 13:49:06 +0100 Subject: [PATCH 1/3] Add CLI support for domain and token. Also move comments into POD Allow CLI usage help Add nagios.pl specific README (generated from POD). --- README_NAGIOS.md | 59 +++++++++++++++++++++++++++ nagios.pl | 104 +++++++++++++++++++++++++++++++---------------- 2 files changed, 129 insertions(+), 34 deletions(-) create mode 100644 README_NAGIOS.md mode change 100644 => 100755 nagios.pl diff --git a/README_NAGIOS.md b/README_NAGIOS.md new file mode 100644 index 0000000..1576fb3 --- /dev/null +++ b/README_NAGIOS.md @@ -0,0 +1,59 @@ +# NAME + +nagios.pl - A nagios/icinga plugin for sending alerts to Slack + +# AUTHOR + +Tiny Speck, Inc + +# SYNOPSIS + +**nagios.pl** \[**--help**\] \[**-field slack\_SOMETHING=value**\] + +## -h, --help + +This output + +## -field + +Provide **slack** related configuration. Possible choices include: + + * slack_channel - Target Slack channel for the message + * slack_domain - Slack domain for your team + * slack_token - Slack token for this integration + +# REQUIRES + +Perl5.004, [strict](https://metacpan.org/pod/strict), [warnings](https://metacpan.org/pod/warnings), [HTTP::Request](https://metacpan.org/pod/HTTP::Request), [LWP::UserAgent](https://metacpan.org/pod/LWP::UserAgent) + +# EXPORTS + +Nothing + +# DESCRIPTION + +A nagios/icinga plugin for sending alerts to Slack. See more documentation on the team services page at: + [https://my.slack.com/services/new/nagios](https://my.slack.com/services/new/nagios) + +An example Nagios config: + + define contact { + contact_name slack + alias Slack + service_notification_period 24x7 + host_notification_period 24x7 + service_notification_options w,u,c,r + host_notification_options d,r + service_notification_commands notify-service-by-slack + host_notification_commands notify-host-by-slack + } + + define command { + command_name notify-service-by-slack + command_line /usr/local/bin/slack_nagios.pl -field slack_channel=#alerts -field slack_domain=yourteam.slack.com -field slack_token=Som3th1ngR@nd0m + } + + define command { + command_name notify-host-by-slack + command_line /usr/local/bin/slack_nagios.pl -field slack_channel=#ops -field slack_domain=yourteam.slack.com -field slack_token=Som3th1ngR@nd0m + } diff --git a/nagios.pl b/nagios.pl old mode 100644 new mode 100755 index 6c8a13f..4daa701 --- a/nagios.pl +++ b/nagios.pl @@ -15,40 +15,67 @@ # See the License for the specific language governing permissions and # limitations under the License. +=head1 NAME -# -# A nagios/icinga plugin for sending alerts to Slack. See more documentation on the team services page at: -# https://my.slack.com/services/new/nagios -# -# Requires these perl modules: -# HTTP::Request -# LWP::UserAgent -# -# I am not a perl programmer. Beware. -# -# An example Nagios config: -# -# define contact { -# contact_name slack -# alias Slack -# service_notification_period 24x7 -# host_notification_period 24x7 -# service_notification_options w,u,c,r -# host_notification_options d,r -# service_notification_commands notify-service-by-slack -# host_notification_commands notify-host-by-slack -# } -# -# define command { -# command_name notify-service-by-slack -# command_line /usr/local/bin/slack_nagios.pl -field slack_channel=#alerts -# } -# -#define command { -# command_name notify-host-by-slack -# command_line /usr/local/bin/slack_nagios.pl -field slack_channel=#ops -# } -# +nagios.pl - A nagios/icinga plugin for sending alerts to Slack + +=head1 AUTHOR + +Tiny Speck, Inc + +=head1 SYNOPSIS + +B [B<--help>] [B<-field slack_SOMETHING=value>] + +=head2 -h, --help + +This output + +=head2 -field + +Provide B related configuration. Possible choices include: + + * slack_channel - Target Slack channel for the message + * slack_domain - Slack domain for your team + * slack_token - Slack token for this integration + +=head1 REQUIRES + +Perl5.004, L, L, L, L + +=head1 EXPORTS + +Nothing + +=head1 DESCRIPTION + +A nagios/icinga plugin for sending alerts to Slack. See more documentation on the team services page at: + L + +An example Nagios config: + + define contact { + contact_name slack + alias Slack + service_notification_period 24x7 + host_notification_period 24x7 + service_notification_options w,u,c,r + host_notification_options d,r + service_notification_commands notify-service-by-slack + host_notification_commands notify-host-by-slack + } + + define command { + command_name notify-service-by-slack + command_line /usr/local/bin/slack_nagios.pl -field slack_channel=#alerts -field slack_domain=yourteam.slack.com -field slack_token=Som3th1ngR@nd0m + } + + define command { + command_name notify-host-by-slack + command_line /usr/local/bin/slack_nagios.pl -field slack_channel=#ops -field slack_domain=yourteam.slack.com -field slack_token=Som3th1ngR@nd0m + } + +=cut use warnings; use strict; @@ -72,8 +99,17 @@ # my %opt_fields; -GetOptions("field=s%" => \%opt_fields); +GetOptions("field=s%" => \%opt_fields) + or exec( "pod2usage -v 1 $0 1>&2" ); + +# +# Allow these to be provided via command-line opts (fall back to defaults) +# +$opt_domain = $opt_fields{'slack_domain'} || $opt_domain; +delete($opt_fields{'slack_domain'}); +$opt_token = $opt_fields{'slack_token'} || $opt_token; +delete($opt_fields{'slack_token'}); # # DO THINGS From f3624f2a8d0a3c32fea31124600677720a038249 Mon Sep 17 00:00:00 2001 From: Matthew Cox Date: Tue, 1 Nov 2016 13:55:24 +0100 Subject: [PATCH 2/3] Provide a little warning about CLI usage and slack_token --- nagios.pl | 3 +++ 1 file changed, 3 insertions(+) diff --git a/nagios.pl b/nagios.pl index 4daa701..15b707f 100755 --- a/nagios.pl +++ b/nagios.pl @@ -39,6 +39,9 @@ =head2 -field * slack_domain - Slack domain for your team * slack_token - Slack token for this integration +B Providing the Slack token on the CLI might allow it to be +read from /proc or the like. + =head1 REQUIRES Perl5.004, L, L, L, L From b63ca5eb0a5f4fbebceae6f7b4072804bd1bc871 Mon Sep 17 00:00:00 2001 From: Matthew Cox Date: Tue, 1 Nov 2016 13:59:43 +0100 Subject: [PATCH 3/3] Complete the list of required packages --- nagios.pl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nagios.pl b/nagios.pl index 15b707f..ac59fa1 100755 --- a/nagios.pl +++ b/nagios.pl @@ -44,7 +44,7 @@ =head2 -field =head1 REQUIRES -Perl5.004, L, L, L, L +Perl5.004, L, L, L, L, L, L =head1 EXPORTS