Issues (2963)

LibreNMS/Alert/Transport/Elasticsearch.php (1 issue)

1
<?php
2
/* LibreNMS
3
 *
4
 * Copyright (C) 2017 Paul Blasquez <[email protected]>
5
 * This program is free software: you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation, either version 3 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program.  If not, see <https://www.gnu.org/licenses/>. */
17
18
namespace LibreNMS\Alert\Transport;
19
20
use LibreNMS\Alert\Transport;
21
use LibreNMS\Enum\AlertState;
22
23
class Elasticsearch extends Transport
24
{
25
    public function deliverAlert($obj, $opts)
26
    {
27
        if (! empty($this->config)) {
28
            $opts['es_host'] = $this->config['es-host'];
29
            $opts['es_port'] = $this->config['es-port'];
30
            $opts['es_index'] = $this->config['es-pattern'];
31
            $opts['es_proxy'] = $this->config['es-proxy'];
32
        }
33
34
        return $this->contactElasticsearch($obj, $opts);
35
    }
36
37
    public function contactElasticsearch($obj, $opts)
38
    {
39
        $es_host = '127.0.0.1';
40
        $es_port = 9200;
41
        $index = strftime('librenms-%Y.%m.%d');
42
        $type = 'alert';
43
        $severity = $obj['severity'];
44
        $device = device_by_id_cache($obj['device_id']); // for event logging
0 ignored issues
show
The assignment to $device is dead and can be removed.
Loading history...
45
46
        if (! empty($opts['es_host'])) {
47
            if (preg_match('/[a-zA-Z]/', $opts['es_host'])) {
48
                $es_host = gethostbyname($opts['es_host']);
49
                if ($es_host === $opts['es_host']) {
50
                    return 'Alphanumeric hostname found but does not resolve to an IP.';
51
                }
52
            } elseif (filter_var($opts['es_host'], FILTER_VALIDATE_IP)) {
53
                $es_host = $opts['es_host'];
54
            } else {
55
                return 'Elasticsearch host is not a valid IP: ' . $opts['es_host'];
56
            }
57
        }
58
59
        if (! empty($opts['es_port']) && preg_match("/^\d+$/", $opts['es_port'])) {
60
            $es_port = $opts['es_port'];
61
        }
62
63
        if (! empty($opts['es_index'])) {
64
            $index = strftime($opts['es_index']);
65
        }
66
67
        $host = $es_host . ':' . $es_port . '/' . $index . '/' . $type;
68
69
        switch ($obj['state']) {
70
            case AlertState::RECOVERED:
71
                $state = 'ok';
72
                break;
73
            case AlertState::ACTIVE:
74
                $state = $severity;
75
                break;
76
            case AlertState::ACKNOWLEDGED:
77
                $state = 'acknowledged';
78
                break;
79
            case AlertState::WORSE:
80
                $state = 'worse';
81
                break;
82
            case AlertState::BETTER:
83
                $state = 'better';
84
                break;
85
            default:
86
                $state = 'unknown';
87
                break;
88
        }
89
90
        $data = [
91
            '@timestamp' => date('c'),
92
            'host' => gethostname(),
93
            'location' => $obj['location'],
94
            'title' => $obj['name'],
95
            'message' => $obj['string'],
96
            'device_id' => $obj['device_id'],
97
            'device_name' => $obj['hostname'],
98
            'device_hardware' => $obj['hardware'],
99
            'device_version' => $obj['version'],
100
            'state' => $state,
101
            'severity' => $severity,
102
            'first_occurrence' => $obj['timestamp'],
103
            'entity_type' => 'device',
104
            'entity_tab' => 'overview',
105
            'entity_id' => $obj['device_id'],
106
            'entity_name' => $obj['hostname'],
107
            'entity_descr' => $obj['sysDescr'],
108
        ];
109
110
        if (! empty($obj['faults'])) {
111
            foreach ($obj['faults'] as $k => $v) {
112
                $curl = curl_init();
113
                $data['message'] = $v['string'];
114
                switch (true) {
115
                    case array_key_exists('port_id', $v):
116
                        $data['entity_type'] = 'port';
117
                        $data['entity_tab'] = 'port';
118
                        $data['entity_id'] = $v['port_id'];
119
                        $data['entity_name'] = $v['ifName'];
120
                        $data['entity_descr'] = $v['ifAlias'];
121
                        break;
122
                    case array_key_exists('sensor_id', $v):
123
                        $data['entity_type'] = $v['sensor_class'];
124
                        $data['entity_tab'] = 'health';
125
                        $data['entity_id'] = $v['sensor_id'];
126
                        $data['entity_name'] = $v['sensor_descr'];
127
                        $data['entity_descr'] = $v['sensor_type'];
128
                        break;
129
                    case array_key_exists('mempool_id', $v):
130
                        $data['entity_type'] = 'mempool';
131
                        $data['entity_tab'] = 'health';
132
                        $data['entity_id'] = $v['mempool_id'];
133
                        $data['entity_name'] = $v['mempool_index'];
134
                        $data['entity_descr'] = $v['mempool_descr'];
135
                        break;
136
                    case array_key_exists('storage_id', $v):
137
                        $data['entity_type'] = 'storage';
138
                        $data['entity_tab'] = 'health';
139
                        $data['entity_id'] = $v['storage_id'];
140
                        $data['entity_name'] = $v['storage_index'];
141
                        $data['entity_descr'] = $v['storage_descr'];
142
                        break;
143
                    case array_key_exists('processor_id', $v):
144
                        $data['entity_type'] = 'processor';
145
                        $data['entity_tab'] = 'health';
146
                        $data['entity_id'] = $v['processor_id'];
147
                        $data['entity_name'] = $v['processor_type'];
148
                        $data['entity_descr'] = $v['processor_descr'];
149
                        break;
150
                    case array_key_exists('bgpPeer_id', $v):
151
                        $data['entity_type'] = 'bgp';
152
                        $data['entity_tab'] = 'routing';
153
                        $data['entity_id'] = $v['bgpPeer_id'];
154
                        $data['entity_name'] = 'local: ' . $v['bgpPeerLocalAddr'] . ' - AS' . $obj['bgpLocalAs'];
155
                        $data['entity_descr'] = 'remote: ' . $v['bgpPeerIdentifier'] . ' - AS' . $v['bgpPeerRemoteAs'];
156
                        break;
157
                    case array_key_exists('tunnel_id', $v):
158
                        $data['entity_type'] = 'ipsec_tunnel';
159
                        $data['entity_tab'] = 'routing';
160
                        $data['entity_id'] = $v['tunnel_id'];
161
                        $data['entity_name'] = $v['tunnel_name'];
162
                        $data['entity_descr'] = 'local: ' . $v['local_addr'] . ':' . $v['local_port'] . ', remote: ' . $v['peer_addr'] . ':' . $v['peer_port'];
163
                        break;
164
                    default:
165
                        $data['entity_type'] = 'generic';
166
                        break;
167
                }
168
                $alert_message = json_encode($data);
169
                curl_setopt($curl, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
170
                if ($opts['es_proxy'] === true) {
171
                    set_curl_proxy($curl);
172
                }
173
                curl_setopt($curl, CURLOPT_URL, $host);
174
                curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
175
                curl_setopt($curl, CURLOPT_POST, true);
176
                curl_setopt($curl, CURLOPT_POSTFIELDS, $alert_message);
177
178
                $ret = curl_exec($curl);
179
                $code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
180
                if ($code != 200 && $code != 201) {
181
                    return $host . ' returned HTTP Status code ' . $code . ' for ' . $alert_message;
182
                }
183
            }
184
        } else {
185
            $curl = curl_init();
186
            $alert_message = json_encode($data);
187
            curl_setopt($curl, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
188
            if ($opts['es_proxy'] === true) {
189
                set_curl_proxy($curl);
190
            }
191
            curl_setopt($curl, CURLOPT_URL, $host);
192
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
193
            curl_setopt($curl, CURLOPT_POST, true);
194
            curl_setopt($curl, CURLOPT_POSTFIELDS, $alert_message);
195
196
            $ret = curl_exec($curl);
197
            $code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
198
            if ($code != 200 && $code != 201) {
199
                return $host . ' returned HTTP Status code ' . $code . ' for ' . $alert_message;
200
            }
201
        }
202
203
        return true;
204
    }
205
206
    public static function configTemplate()
207
    {
208
        return [
209
            'config' => [
210
                [
211
                    'title' => 'Host',
212
                    'name' => 'es-host',
213
                    'descr' => 'Elasticsearch Host',
214
                    'type' => 'text',
215
                ],
216
                [
217
                    'title' => 'Port',
218
                    'name' => 'es-port',
219
                    'descr' => 'Elasticsearch Port',
220
                    'type' => 'text',
221
                ],
222
                [
223
                    'title' => 'Index Pattern',
224
                    'name' => 'es-pattern',
225
                    'descr' => 'Elasticsearch Index Pattern',
226
                    'type' => 'text',
227
                ],
228
                [
229
                    'title' => 'Use proxy if configured?',
230
                    'name' => 'es-proxy',
231
                    'descr' => 'Elasticsearch Proxy',
232
                    'type' => 'checkbox',
233
                    'default' => false,
234
                ],
235
            ],
236
            'validation' => [
237
                'es-host' => 'required|string',
238
                'es-port' => 'required|string',
239
                'es-pattern' => 'required|string',
240
            ],
241
        ];
242
    }
243
}
244