This repository has been archived by the owner on Mar 7, 2024. It is now read-only.
forked from itsjohannawren/pd-nag-connector
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpagerduty.cgi
executable file
·185 lines (139 loc) · 5.35 KB
/
pagerduty.cgi
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#!/usr/bin/env perl
use warnings;
use strict;
use CGI;
use JSON;
use LWP::UserAgent;
# =============================================================================
my $CONFIG = {
# Nagios/Ubuntu defaults
'command_file' => '/var/lib/nagios3/rw/nagios.cmd', # External commands file
# Icinga/CentOS defaults
#'command_file' => '/var/spool/icinga/cmd/icinga.cmd', # External commands file
# Icinga acknowledgement TTL
'ack_ttl' => 0, # Time in seconds the acknowledgement in Icinga last before
# it times out automatically. 0 means the acknowledgement
# never expires. If you're using Nagios this MUST be 0.
};
# =============================================================================
sub ackHost {
my ($time, $host, $comment, $author, $sticky, $notify, $persistent) = @_;
# Open the external commands file
if (! open (NAGIOS, '>>', $CONFIG->{'command_file'})) {
# Well shizzle
return (undef, $!);
}
# Success! Write the command
if ($CONFIG->{'ack_ttl'} <= 0) {
printf (NAGIOS "[%u] ACKNOWLEDGE_HOST_PROBLEM;%s;%u;%u;%u;%s;%s\n", $time, $host, $sticky, $notify, $persistent, $author, $comment);
} else {
printf (NAGIOS "[%u] ACKNOWLEDGE_HOST_PROBLEM_EXPIRE;%s;%u;%u;%u;%u;%s;%s\n", $time, $host, $sticky, $notify, $persistent, ($time + $CONFIG->{'ack_ttl'}), $author, $comment);
}
# Close the file handle
close (NAGIOS);
# Return with happiness
return (1, undef);
}
# =============================================================================
sub deackHost {
my ($time, $host) = @_;
# Open the external commands file
if (! open (NAGIOS, '>>', $CONFIG->{'command_file'})) {
# Well shizzle
return (undef, $!);
}
# Success! Write the command
printf (NAGIOS "[%u] REMOVE_HOST_ACKNOWLEDGEMENT;%s\n", $time, $host);
# Close the file handle
close (NAGIOS);
# Return with happiness
return (1, undef);
}
# =============================================================================
sub ackService {
my ($time, $host, $service, $comment, $author, $sticky, $notify, $persistent) = @_;
# Open the external commands file
if (! open (NAGIOS, '>>', $CONFIG->{'command_file'})) {
# Well shizzle
return (undef, $!);
}
# Success! Write the command
if ($CONFIG->{'ack_ttl'} <= 0) {
printf (NAGIOS "[%u] ACKNOWLEDGE_SVC_PROBLEM;%s;%s;%u;%u;%u;%s;%s\n", $time, $host, $service, $sticky, $notify, $persistent, $author, $comment);
} else {
printf (NAGIOS "[%u] ACKNOWLEDGE_SVC_PROBLEM_EXPIRE;%s;%s;%u;%u;%u;%u;%s;%s\n", $time, $host, $service, $sticky, $notify, $persistent, ($time + $CONFIG->{'ack_ttl'}), $author, $comment);
}
# Close the file handle
close (NAGIOS);
# Return with happiness
return (1, undef);
}
# =============================================================================
sub deackService {
my ($time, $host, $service) = @_;
# Open the external commands file
if (! open (NAGIOS, '>>', $CONFIG->{'command_file'})) {
# Well shizzle
return (undef, $!);
}
# Success! Write the command
printf (NAGIOS "[%u] REMOVE_SVC_ACKNOWLEDGEMENT;%s;%s\n", $time, $host, $service);
# Close the file handle
close (NAGIOS);
# Return with happiness
return (1, undef);
}
# =============================================================================
my ($TIME, $QUERY, $POST, $JSON);
$TIME = time ();
$QUERY = CGI->new ();
if (! defined ($POST = $QUERY->param ('POSTDATA'))) {
print ("Status: 400 Requests must be POSTs\n\n400 Requests must be POSTs\n");
exit (0);
}
if (! defined ($JSON = JSON->new ()->utf8 ()->decode ($POST))) {
print ("Status: 400 Request payload must be JSON blob\n\n400 Request payload must JSON blob\n");
exit (0);
}
if ((ref ($JSON) ne 'HASH') || ! defined ($JSON->{'messages'}) || (ref ($JSON->{'messages'}) ne 'ARRAY')) {
print ("Status: 400 JSON blob does not match the expected format\n\n400 JSON blob does not match expected format\n");
exit (0);
}
my ($message, $return);
$return = {
'status' => 'okay',
'messages' => {}
};
MESSAGE: foreach $message (@{$JSON->{'messages'}}) {
my ($hostservice, $status, $error);
if ((ref ($message) ne 'HASH') || ! defined ($message->{'type'})) {
next MESSAGE;
}
$hostservice = $message->{'data'}->{'incident'}->{'trigger_summary_data'};
if (! defined ($hostservice)) {
next MESSAGE;
}
if ($message->{'type'} eq 'incident.acknowledge') {
if ($hostservice->{'SERVICEDESC'} eq "") {
($status, $error) = ackHost ($TIME, $hostservice->{'HOSTNAME'}, 'Acknowledged by PagerDuty', 'PagerDuty', 2, 0, 0);
} else {
($status, $error) = ackService ($TIME, $hostservice->{'HOSTNAME'}, $hostservice->{'SERVICEDESC'}, 'Acknowledged by PagerDuty', 'PagerDuty', 2, 0, 0);
}
$return->{'messages'}{$message->{'id'}} = {
'status' => ($status ? 'okay' : 'fail'),
'message' => ($error ? $error : undef)
};
} elsif ($message->{'type'} eq 'incident.unacknowledge') {
if (! defined ($hostservice->{'SERVICEDESC'})) {
($status, $error) = deackHost ($TIME, $hostservice->{'HOSTNAME'});
} else {
($status, $error) = deackService ($TIME, $hostservice->{'HOSTNAME'}, $hostservice->{'SERVICEDESC'});
}
$return->{'messages'}->{$message->{'id'}} = {
'status' => ($status ? 'okay' : 'fail'),
'message' => ($error ? $error : undef)
};
$return->{'status'} = ($status eq 'okay' ? $return->{'status'} : 'fail');
}
}
printf ("Status: 200 Okay\nContent-type: application/json\n\n%s\n", JSON->new ()->utf8 ()->encode ($return));