forked from tinyspeck/services-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nagios.pl
110 lines (86 loc) · 2.62 KB
/
nagios.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/usr/bin/perl
# Copyright 2013 Tiny Speck, Inc
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# 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
# }
#
use warnings;
use strict;
use Getopt::Long;
use HTTP::Request::Common qw(POST);
use HTTP::Status qw(is_client_error);
use LWP::UserAgent;
#
# Customizable vars. Set these to the information for your team
#
my $opt_domain = "foo.slack.com"; # Your team's domain
my $opt_token = ""; # The token from your Nagios services page
#
# Get command-line opts
#
my %opt_fields;
GetOptions("field=s%" => \%opt_fields);
#
# DO THINGS
#
my %event;
# Get all Nagios variables
while ((my $k, my $v) = each %ENV) {
next unless $k =~ /^(?:NAGIOS|ICINGA)_(.*)$/;
$event{$1} = $v;
}
# Merge in passed-in variables
%event = (%event, %opt_fields);
$event{"slack_version"} = "1.1";
#
# Make the request
#
my $ua = LWP::UserAgent->new;
$ua->timeout(15);
my $req = POST("https://${opt_domain}/services/hooks/nagios?token=${opt_token}", \%event);
my $s = $req->as_string;
print STDERR "Request:\n$s\n";
my $resp = $ua->request($req);
$s = $resp->as_string;
print STDERR "Response:\n$s\n";