forked from vozerov/munin-alert
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmunin-alert.pl
159 lines (120 loc) · 4.99 KB
/
munin-alert.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/usr/bin/perl
use LWP::UserAgent;
use Digest::MD5 qw(md5_hex);
use Sys::Syslog;
use threads;
use YAML qw(LoadFile);
use strict;
use warnings;
openlog('munin-alert', 'cons,pid', 'user');
my $msgid = -1;
my @threads;
my @data = ();
# Parsing config
my $config = LoadFile('/etc/munin-alert.yml');
my $hipchat_url = $config->{'hipchat'}->{'api_url'};
my $hipchat_key = $config->{'hipchat'}->{'api_key'};
my $hipchat_room = $config->{'hipchat'}->{'room_id'};
my $slack_channel = $config->{'slack'}->{'channel'};
my $slack_token = $config->{'slack'}->{'token'};
my $slack_username = $config->{'slack'}->{'username'};
my $slack_icon_url = $config->{'slack'}->{'icon_url'};
my $pagerduty_url = $config->{'pagerduty'}->{'api_url'};
my $pagerduty_key = $config->{'pagerduty'}->{'api_key'};
my %actions = (
'pagerduty' => \&sendToPagerDuty,
'hipchat' => \&sendToHipChat,
'slack' => \&sendToSlack
);
sub sendToPagerDuty {
my $msg = shift;
syslog('info', 'MSG: [%s] Sending alert (%s) to pagerduty', $msg->{'id'}, $msg->{'host'}.':'.$msg->{'graph'}.'; '.$msg->{'text'});
my $ua = LWP::UserAgent->new;
my $event_type;
$event_type = 'trigger' if (($msg->{'status'} eq 'critical') || ($msg->{'status'} eq 'warning') || ($msg->{'status'} eq 'unknown')) ;
$event_type = 'resolve' if ($msg->{'status'} eq 'ok');
my $hash = $msg->{'host'}.$msg->{'graph'};
my $incident_key = md5_hex($hash);
my $req = HTTP::Request->new(POST => $pagerduty_url);
$req->header('Content-type' => 'application/json');
my $post_data = '{ "service_key": "'.$pagerduty_key.'", "event_type": "'.$event_type.'", "incident_key": "'.$incident_key.'", "description": "'.$msg->{'host'}.':'.$msg->{'graph'}.'; '.$msg->{'text'}.'" }';
$req->content($post_data);
my $resp = $ua->request($req);
if ($resp->is_success) {
syslog('info', 'MSG: [%s] Successfully sent alert to pagerduty', $msg->{'id'});
}
else {
my $message = $resp->decoded_content;
syslog('warning', 'MSG: [%s] HTTP code: %s, message: %s', $msg->{'id'}, $resp->code, $message);
}
}
sub sendToHipChat {
my $msg = shift;
syslog('info', 'MSG: [%s] Sending alert (%s) to hipchat', $msg->{'id'}, $msg->{'host'}.':'.$msg->{'graph'}.'; '.$msg->{'text'});
my $ua = LWP::UserAgent->new;
my $server_endpoint = "https://api.hipchat.com/v1/rooms/message?format=json&auth_token=6cc3b70459ea70a12dfc01fd190d7d";
my $req = HTTP::Request->new(POST => $hipchat_url.'?format=json&auth_token='.$hipchat_key);
$req->header('Content-type' => 'application/x-www-form-urlencoded');
my $post_data = '='.$hipchat_room.'&from=MuninAlerts&message='.$msg->{'host'}.':'.$msg->{'graph'}.'; '.$msg->{'text'};
$req->content($post_data);
my $resp = $ua->request($req);
if ($resp->is_success) {
syslog('info', 'MSG: [%s] Successfully sent alert to hipchat', $msg->{'id'});
}
else {
my $message = $resp->decoded_content;
syslog('warning', 'MSG: [%s] HTTP code: %s, message: %s', $msg->{'id'}, $resp->code, $message);
}
}
sub sendToSlack {
my $msg = shift;
syslog('info', 'MSG: [%s] Sending alert (%s) to slack', $msg->{'id'}, $msg->{'host'}.':'.$msg->{'graph'}.'; '.$msg->{'text'});
my $ua = LWP::UserAgent->new;
my $server_endpoint = "https://slack.com/api/chat.postMessage";
my $req = HTTP::Request->new(POST => $slack_url);
$req->header('Content-type' => 'application/x-www-form-urlencoded');
my $post_data = 'token='.$slack_token.'&channel='.$slack_channel.'&username='.$slack_username.'&icon_url='.$slack_icon_url.'&text='.$msg->{'host'}.': '.$msg->{'graph'}.'; '.$msg->{'text'};
$req->content($post_data);
my $resp = $ua->request($req);
if ($resp->is_success) {
syslog('info', 'MSG: [%s] Successfully sent alert to slack', $resp->decoded_content);
}
else {
my $message = $resp->decoded_content;
syslog('warning', 'MSG: [%s] HTTP code: %s, message: %s', $msg->{'id'}, $resp->code, $message);
}
}
foreach my $line ( <STDIN> ) {
chomp $line;
if ($line =~ m/\:\:/g) {
$msgid++;
my @values = split(' :: ', $line);
# Setting message data
$data[$msgid]{'id'} = $msgid;
$data[$msgid]{'host'} = $values[-2];
$data[$msgid]{'graph'} = $values[-1];
} else {
# Removing spaces from start and end of line
$line =~ s/^\s+|\s+$//;
# Setting message text
$data[$msgid]{'text'} .= $line."; " if (length($line) > 0);
# Setting message status
$data[$msgid]{'status'} = 'ok' if ($line =~ m/OKs\:/g);
$data[$msgid]{'status'} = 'warning' if ($line =~ m/WARNINGs\:/g);
$data[$msgid]{'status'} = 'critical' if ($line =~ m/CRITICALs\:/g);
$data[$msgid]{'status'} = 'unknown' if ($line =~ m/UNKNOWNs\:/g);
}
}
foreach my $alert (@data) {
if (exists $config->{'munin'}->{$alert->{'status'}}) {
foreach my $action (@{$config->{'munin'}->{$alert->{'status'}}}) {
push @threads, threads->create($actions{$action}, $alert);
}
} else {
syslog('info', 'Nothing action is defined for status %s, skipping', $alert->{'status'});
}
}
foreach my $thread (@threads) {
$thread->join();
}
closelog();