1 | <?php |
||
2 | /* Copyright (C) 2019 LibreNMS |
||
3 | * This program is free software: you can redistribute it and/or modify |
||
4 | * it under the terms of the GNU General Public License as published by |
||
5 | * the Free Software Foundation, either version 3 of the License, or |
||
6 | * (at your option) any later version. |
||
7 | * |
||
8 | * This program is distributed in the hope that it will be useful, |
||
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the |
||
11 | * GNU General Public License for more details. |
||
12 | * |
||
13 | * You should have received a copy of the GNU General Public License |
||
14 | * along with this program. If not, see <https://www.gnu.org/licenses/>. */ |
||
15 | |||
16 | /** |
||
17 | * Alertmanager Transport |
||
18 | * |
||
19 | * @copyright 2019 LibreNMS |
||
20 | * @license GPL |
||
21 | */ |
||
22 | |||
23 | namespace LibreNMS\Alert\Transport; |
||
24 | |||
25 | use LibreNMS\Alert\Transport; |
||
26 | use LibreNMS\Config; |
||
27 | use LibreNMS\Enum\AlertState; |
||
28 | |||
29 | class Alertmanager extends Transport |
||
30 | { |
||
31 | public function deliverAlert($obj, $opts) |
||
32 | { |
||
33 | $alertmanager_opts = $this->parseUserOptions($this->config['alertmanager-options']); |
||
34 | $alertmanager_opts['url'] = $this->config['alertmanager-url']; |
||
35 | |||
36 | return $this->contactAlertmanager($obj, $alertmanager_opts); |
||
37 | } |
||
38 | |||
39 | public function contactAlertmanager($obj, $api) |
||
40 | { |
||
41 | if ($obj['state'] == AlertState::RECOVERED) { |
||
42 | $alertmanager_status = 'endsAt'; |
||
43 | } else { |
||
44 | $alertmanager_status = 'startsAt'; |
||
45 | } |
||
46 | $gen_url = (Config::get('base_url') . 'device/device=' . $obj['device_id']); |
||
47 | $alertmanager_msg = strip_tags($obj['msg']); |
||
48 | $data = [[ |
||
49 | $alertmanager_status => date('c'), |
||
50 | 'generatorURL' => $gen_url, |
||
51 | 'annotations' => [ |
||
52 | 'summary' => $obj['name'], |
||
53 | 'title' => $obj['title'], |
||
54 | 'description' => $alertmanager_msg, |
||
55 | ], |
||
56 | 'labels' => [ |
||
57 | 'alertname' => $obj['name'], |
||
58 | 'severity' => $obj['severity'], |
||
59 | 'instance' => $obj['hostname'], |
||
60 | ], |
||
61 | ]]; |
||
62 | |||
63 | $url = $api['url']; |
||
64 | unset($api['url']); |
||
65 | foreach ($api as $label => $value) { |
||
66 | $data[0]['labels'][$label] = $value; |
||
67 | } |
||
68 | |||
69 | return $this->postAlerts($url, $data); |
||
70 | } |
||
71 | |||
72 | public static function postAlerts($url, $data) |
||
73 | { |
||
74 | $curl = curl_init(); |
||
75 | set_curl_proxy($curl); |
||
76 | curl_setopt($curl, CURLOPT_HTTPHEADER, ['Content-Type: application/json']); |
||
77 | curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); |
||
78 | curl_setopt($curl, CURLOPT_TIMEOUT, 5); |
||
79 | curl_setopt($curl, CURLOPT_TIMEOUT_MS, 5000); |
||
80 | curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 5); |
||
81 | curl_setopt($curl, CURLOPT_CONNECTTIMEOUT_MS, 5000); |
||
82 | curl_setopt($curl, CURLOPT_POST, true); |
||
83 | |||
84 | $alert_message = json_encode($data); |
||
85 | curl_setopt($curl, CURLOPT_POSTFIELDS, $alert_message); |
||
86 | |||
87 | foreach (explode(',', $url) as $am) { |
||
88 | $post_url = ($am . '/api/v2/alerts'); |
||
89 | curl_setopt($curl, CURLOPT_URL, $post_url); |
||
90 | $ret = curl_exec($curl); |
||
91 | if ($ret === false || curl_errno($curl)) { |
||
92 | logfile("Failed to contact $post_url: " . curl_error($curl)); |
||
93 | continue; |
||
94 | } |
||
95 | $code = curl_getinfo($curl, CURLINFO_HTTP_CODE); |
||
96 | if ($code == 200) { |
||
97 | curl_close($curl); |
||
98 | |||
99 | return true; |
||
100 | } |
||
101 | } |
||
102 | |||
103 | $err = "Unable to POST to Alertmanager at $post_url ."; |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Loading history...
|
|||
104 | |||
105 | if ($ret === false || curl_errno($curl)) { |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
106 | $err .= ' cURL error: ' . curl_error($curl); |
||
107 | } else { |
||
108 | $err .= ' HTTP status: ' . curl_getinfo($curl, CURLINFO_HTTP_CODE); |
||
109 | } |
||
110 | |||
111 | curl_close($curl); |
||
112 | |||
113 | logfile($err); |
||
114 | |||
115 | return $err; |
||
116 | } |
||
117 | |||
118 | public static function configTemplate() |
||
119 | { |
||
120 | return [ |
||
121 | 'config' => [ |
||
122 | [ |
||
123 | 'title' => 'Alertmanager URL(s)', |
||
124 | 'name' => 'alertmanager-url', |
||
125 | 'descr' => 'Alertmanager Webhook URL(s). Can contain comma-separated URLs', |
||
126 | 'type' => 'text', |
||
127 | ], |
||
128 | [ |
||
129 | 'title' => 'Alertmanager Options', |
||
130 | 'name' => 'alertmanager-options', |
||
131 | 'descr' => 'Alertmanager Options', |
||
132 | 'type' => 'textarea', |
||
133 | ], |
||
134 | ], |
||
135 | 'validation' => [ |
||
136 | 'alertmanager-url' => 'required|string', |
||
137 | ], |
||
138 | ]; |
||
139 | } |
||
140 | } |
||
141 |