forked from mpellegrin/nagios-eventhandler-cachet
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcachet_notify
executable file
·194 lines (174 loc) · 5.97 KB
/
cachet_notify
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
186
187
188
189
190
191
192
193
194
#!/usr/bin/php
<?php
if ($argc != 6) {
echo 'Usage: ' . basename(__FILE__) . ' cachet_component service_name service_state service_state_type service_output' . "\n";
print_r($argv);
exit(1);
}
$cachet_url = 'https://xxxxx/api/v1/';
$api_key = 'xxxxx';
$proxy_ip = 'xxxxx';
$proxy_port = 'xxxxx';
$proxy_user = 'xxxxx';
$proxy_pass = 'xxxxx';
$incident_prefix = '[Nagios]';
$cachet_notify_subscribers = true; // Enable subscribers notifcation for incidents creation and updates
$cachet_incident_visible = true;
$cachet_component = $argv[1];
$service_name = $argv[2];
$service_status = $argv[3];
$service_status_type = $argv[4];
$service_output = $argv[5];
define('CACHET_STATUS_INVESTIGATING', 1);
define('CACHET_STATUS_IDENTIFIED', 2);
define('CACHET_STATUS_WATCHING', 3);
define('CACHET_STATUS_FIXED', 4);
define('CACHET_COMPONENT_STATUS_OPERATIONAL', 1);
define('CACHET_COMPONENT_STATUS_PERFORMANCE_ISSUES', 2);
define('CACHET_COMPONENT_STATUS_PARTIAL_OUTAGE', 3);
define('CACHET_COMPONENT_STATUS_MAJOR_OUTAGE', 4);
function cachet_query($api_part, $action = 'GET', $data = null) {
global $api_key, $cachet_url, $proxy_ip, $proxy_port, $proxy_user, $proxy_pass;
print_r($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $cachet_url . $api_part);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_PROXY, $proxy_ip);
curl_setopt($ch, CURLOPT_PROXYPORT, $proxy_port);
curl_setopt($ch, CURLOPT_PROXYUSERPWD, "$proxy_user:$proxy_pass");
if (in_array($action, array('GET', 'POST', 'PUT'))) {
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $action);
}
if ($data !== null && is_array($data)) {
$ch_data = http_build_query($data);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $ch_data);
}
$ch_headers = array(
'X-Cachet-Token: ' . $api_key
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $ch_headers);
curl_setopt($ch, CURLOPT_HEADER, false); // Don't return headers
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Return body
$http_body = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return array('code' => $http_code, 'body' => json_decode($http_body));
}
/* Find Cachet component ID */
$result = cachet_query('components?sort=id&order=asc&per_page=200');
if ($result['code'] != 200) {
echo 'Can\'t query components' . "\n";
exit(1);
}
$cachet_component_id = false;
foreach ($result['body']->data as $component) {
if ($cachet_component == $component->name) { // We nailed it
$cachet_component_id = $component->id;
break; // Yes, bad.
}
}
if ($cachet_component_id === false) {
echo 'Can\'t find component "' . $cachet_component . '"' . "\n";
exit(1);
}
/*
Determine what to to:
- if PROBLEM and HARD then create incident
- if RECOVERY and SOFT or HARD then update incident
PROBLEM = !OK = (WARNING | CRITICAL | UNKONWN)
RECOVERY = OK
*/
$results = cachet_query('incidents?sort=id&order=asc&per_page=200');
if ($result['code'] != 200) {
echo 'Can\'t get incidents' . "\n";
exit(1);
}
$cachet_incident_id = false;
foreach ($results['body']->data as $incident) {
if (($incident->name == $incident_prefix . ' ' . $service_name) and ($incident->status != CACHET_STATUS_FIXED)) {
$cachet_incident_id = $incident->id;
break; // Yes, bad.
}
}
if ($service_status != 'OK' && $cachet_incident_id !== false) {
exit(0);
}
if ($service_status == 'WARNING' && $service_status_type == 'HARD') {
//echo 'KO SOFT: not doing anything' . "\n";
//exit(0);
echo 'WARNING SOFT: creating incident' . "\n";
$query = array(
'name' => $incident_prefix . ' ' . $service_name,
'message' => $service_output,
'status' => CACHET_STATUS_WATCHING,
'visible' => $cachet_incident_visible,
'component_id' => $cachet_component_id,
'component_status' => CACHET_COMPONENT_STATUS_PERFORMANCE_ISSUES,
'notify' => $cachet_notify_subscribers,
);
$result = cachet_query('incidents', 'POST', $query);
if ($result['code'] != 200) {
echo 'Can\'t create incident' . "\n";
exit(1);
}
} elseif ($service_status == 'CRITICAL' && $service_status_type == 'HARD') {
echo 'KO HARD: creating incident' . "\n";
$query = array(
'name' => $incident_prefix . ' ' . $service_name,
'message' => $service_output,
'status' => CACHET_STATUS_INVESTIGATING,
'visible' => $cachet_incident_visible,
'component_id' => $cachet_component_id,
'component_status' => CACHET_COMPONENT_STATUS_MAJOR_OUTAGE,
'notify' => $cachet_notify_subscribers,
);
$result = cachet_query('incidents', 'POST', $query);
if ($result['code'] != 200) {
echo 'Can\'t create incident' . "\n";
exit(1);
}
} elseif ($service_status == 'OK') { // Recovery underway
echo 'OK: updating incident' . "\n";
/* Get the incident ID */
$results = cachet_query('incidents?sort=id&order=asc&per_page=200');
if ($result['code'] != 200) {
echo 'Can\'t get incidents' . "\n";
exit(1);
}
$cachet_incident_id = false;
foreach ($results['body']->data as $incident) {
if (($incident->name == $incident_prefix . ' ' . $service_name) and ($incident->status != CACHET_STATUS_FIXED)) {
$cachet_incident_id = $incident->id;
break; // Yes, bad.
}
}
if ($cachet_incident_id === false) {
echo 'Can\'t find incident "' . $incident_prefix . ' ' . $service_name . '"' . "\n";
exit(1);
}
/* Update the incident */
$query = array(
'message' => $service_output,
'status' => CACHET_STATUS_FIXED
);
$result = cachet_query('incidents/' . $cachet_incident_id . '/updates', 'POST', $query);
if ($result['code'] != 200) {
echo 'Can\'t update incident' . "\n";
exit(1);
}
/* Update the component */
$query = array(
'status' => CACHET_COMPONENT_STATUS_OPERATIONAL
);
$result = cachet_query('components/' . $cachet_component_id, 'PUT', $query);
if ($result['code'] != 200) {
echo 'Can\'t update component' . "\n";
exit(1);
}
} else {
echo 'Bad arguments' . "\n";
exit(1);
}
exit(0);