forked from formorer/icinga2-slack-notification
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslack-notification
executable file
·77 lines (63 loc) · 2.29 KB
/
slack-notification
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
#!/usr/bin/perl
use strict;
use warnings;
use WebService::Slack::WebApi;
use Env;
use Config::IniFiles;
use Switch;
# "NOTIFICATIONTYPE" = "$notification.type$"
# "SERVICEDESC" = "$service.name$"
# "HOSTALIAS" = "$host.display_name$",
# "HOSTNAME" = "$host.name$",
# "HOSTADDRESS" = "$address$",
# "SERVICESTATE" = "$service.state$",
# "LONGDATETIME" = "$icinga.long_date_time$",
# "SERVICEOUTPUT" = "$service.output$",
# "NOTIFICATIONAUTHORNAME" = "$notification.author$",
# "NOTIFICATIONCOMMENT" = "$notification.comment$",
# "HOSTDISPLAYNAME" = "$host.display_name$",
# "SERVICEDISPLAYNAME" = "$service.display_name$",
# this uses the webapi. use https://api.slack.com/web to get a token
my $cfg_file = shift;
if (! -f $cfg_file) {
print STDERR "No configuration given or file not found.\nUsage: slack-notification <config.ini>\n";
exit 1;
}
my $cfg = Config::IniFiles->new( -file => $cfg_file );
if (! defined($cfg->val('Authentication', 'Token'))) {
print STDERR "Token not defined in configfile";
exit 1;
}
my $servicestate = $ENV{SERVICESTATE};
my $icon = '';
switch ($servicestate) {
case "CRITICAL" {
$icon = defined($cfg->val('Format', 'CRITICAL')) ?
$cfg->val('Format', 'CRITICAL') : ''; }
case "WARNING" {
$icon = defined($cfg->val('Format', 'WARNING')) ?
$cfg->val('Format', 'WARNING') : ''; }
case "OK" {
$icon = defined($cfg->val('Format', 'OK')) ?
$cfg->val('Format', 'WARNING') : ''; }
case "UNKNOWN" {
$icon = defined($cfg->val('Format', 'UNKNOWN')) ?
$cfg->val('Format', 'WARNING') : ''; }
}
my $icinga_hostname = $cfg->val('General', 'WebHostname');
my $slack = WebService::Slack::WebApi->new(token => $cfg->val('Authentication', 'Token'));
my $posted_message = $slack->chat->post_message(
channel => $cfg->val('General', 'Channel') || '#general',
text => sprintf (
'%s HOST: <http://%s/icingaweb2/monitoring/host/services?host=%s|%s> SERVICE: <http://%s/icingaweb2/monitoring/service/show?host=%s&service=%s|%s> STATE: %s',
$icon,
$icinga_hostname,
$ENV{HOSTNAME},
$ENV{HOSTALIAS},
$icinga_hostname,
$ENV{HOSTNAME},
$ENV{SERVICEDESC},
$ENV{SERVICEDISPLAYNAME},
$servicestate ),
username => 'icinga'
);