Issues (2963)

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

1
<?php
2
/*Copyright (c) 2019 GitStoph <https://github.com/GitStoph>
3
 * This program is free software: you can redistribute it and/or modify it
4
 * under the terms of the GNU General Public License as published by the
5
 * Free Software Foundation, either version 3 of the License, or (at your
6
 * option) any later version.  Please see LICENSE.txt at the top level of
7
 * the source code distribution for details. */
8
/**
9
 * API Transport
10
 *
11
 * @author GitStoph <https://github.com/GitStoph>
12
 * @copyright 2019 GitStoph
13
 * @license GPL
14
 */
15
16
namespace LibreNMS\Alert\Transport;
17
18
use LibreNMS\Alert\Transport;
19
use LibreNMS\Config;
20
use LibreNMS\Enum\AlertState;
21
22
class Alerta extends Transport
23
{
24
    public function deliverAlert($obj, $opts)
25
    {
26
        $opts['url'] = $this->config['alerta-url'];
27
        $opts['environment'] = $this->config['environment'];
28
        $opts['apikey'] = $this->config['apikey'];
29
        $opts['alertstate'] = $this->config['alertstate'];
30
        $opts['recoverstate'] = $this->config['recoverstate'];
31
32
        return $this->contactAlerta($obj, $opts);
33
    }
34
35
    public function contactAlerta($obj, $opts)
36
    {
37
        $host = $opts['url'];
38
        $curl = curl_init();
39
        $text = strip_tags($obj['msg']);
40
        $severity = ($obj['state'] == AlertState::RECOVERED ? $opts['recoverstate'] : $opts['alertstate']);
41
        $deviceurl = (Config::get('base_url') . 'device/device=' . $obj['device_id']);
42
        $devicehostname = $obj['hostname'];
43
        $data = [
44
            'resource' => $devicehostname,
45
            'event' => $obj['name'],
46
            'environment' => $opts['environment'],
47
            'severity' => $severity,
48
            'service' => [$obj['title']],
49
            'group' => $obj['name'],
50
            'value' => $obj['state'],
51
            'text' => $text,
52
            'tags' => [$obj['title']],
53
            'attributes' => [
54
                'sysName' => $obj['sysName'],
55
                'sysDescr' => $obj['sysDescr'],
56
                'os' => $obj['os'],
57
                'type' => $obj['type'],
58
                'ip' => $obj['ip'],
59
                'uptime' => $obj['uptime_long'],
60
                'moreInfo' => '<a href=' . $deviceurl . '>' . $devicehostname . '</a>',
61
            ],
62
            'origin' => $obj['rule'],
63
            'type' => $obj['title'],
64
        ];
65
        $alert_message = json_encode($data);
66
        set_curl_proxy($curl);
67
        $headers = ['Content-Type: application/json', 'Authorization: Key ' . $opts['apikey']];
68
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
69
        curl_setopt($curl, CURLOPT_URL, $host);
70
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
71
        curl_setopt($curl, CURLOPT_POST, true);
72
        curl_setopt($curl, CURLOPT_POSTFIELDS, $alert_message);
73
        $ret = curl_exec($curl);
74
        $code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
75
        if ($code != 201) {
76
            var_dump("API '$host' returned Error");
0 ignored issues
show
Security Debugging Code introduced by
var_dump('API ''.$host.'' returned Error') looks like debug code. Are you sure you do not want to remove it?
Loading history...
77
            var_dump('Params: ' . $alert_message);
78
            var_dump('Return: ' . $ret);
79
            var_dump('Headers: ', $headers);
80
81
            return 'HTTP Status code ' . $code;
82
        }
83
84
        return true;
85
    }
86
87
    public static function configTemplate()
88
    {
89
        return [
90
            'config' => [
91
                [
92
                    'title' => 'API Endpoint',
93
                    'name' => 'alerta-url',
94
                    'descr' => 'Alerta API URL',
95
                    'type' => 'text',
96
                ],
97
                [
98
                    'title' => 'Environment',
99
                    'name' => 'environment',
100
                    'descr' => 'An allowed environment from your alertad.conf.',
101
                    'type' => 'text',
102
                ],
103
                [
104
                    'title' => 'Api Key',
105
                    'name' => 'apikey',
106
                    'descr' => 'Your alerta api key with minimally write:alert permissions.',
107
                    'type' => 'text',
108
                ],
109
                [
110
                    'title' => 'Alert State',
111
                    'name' => 'alertstate',
112
                    'descr' => 'What severity you want Alerta to reflect when rule matches.',
113
                    'type' => 'text',
114
                ],
115
                [
116
                    'title' => 'Recover State',
117
                    'name' => 'recoverstate',
118
                    'descr' => 'What severity you want Alerta to reflect when rule unmatches/recovers.',
119
                    'type' => 'text',
120
                ],
121
            ],
122
            'validation' => [
123
                'alerta-url' => 'required|url',
124
                'apikey' => 'required|string',
125
            ],
126
        ];
127
    }
128
}
129