Issues (2963)

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

1
<?php
2
/* Copyright (C) 2015 Daniel Preussker <[email protected]>
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
 * VictorOps Generic-API Transport - Based on PagerDuty transport
18
 *
19
 * @author f0o <[email protected]>
20
 * @author laf <[email protected]>
21
 * @copyright 2015 f0o, laf, LibreNMS
22
 * @license GPL
23
 */
24
25
namespace LibreNMS\Alert\Transport;
26
27
use LibreNMS\Alert\Transport;
28
use LibreNMS\Enum\AlertState;
29
30
class Victorops extends Transport
31
{
32
    public function deliverAlert($obj, $opts)
33
    {
34
        if (! empty($this->config)) {
35
            $opts['url'] = $this->config['victorops-url'];
36
        }
37
38
        return $this->contactVictorops($obj, $opts);
39
    }
40
41
    public function contactVictorops($obj, $opts)
42
    {
43
        $url = $opts['url'];
44
45
        $protocol = [
46
            'entity_id' => strval($obj['id'] ? $obj['id'] : $obj['uid']),
47
            'state_start_time' => strtotime($obj['timestamp']),
48
            'entity_display_name' => $obj['title'],
49
            'state_message' => $obj['msg'],
50
            'monitoring_tool' => 'librenms',
51
        ];
52
        if ($obj['state'] == AlertState::RECOVERED) {
53
            $protocol['message_type'] = 'recovery';
54
        } elseif ($obj['state'] == AlertState::ACKNOWLEDGED) {
55
            $protocol['message_type'] = 'acknowledgement';
56
        } elseif ($obj['state'] == AlertState::ACTIVE) {
57
            $protocol['message_type'] = 'critical';
58
        }
59
60
        foreach ($obj['faults'] as $fault => $data) {
61
            $protocol['state_message'] .= $data['string'];
62
        }
63
64
        $curl = curl_init();
65
        set_curl_proxy($curl);
66
        curl_setopt($curl, CURLOPT_URL, $url);
67
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
68
        curl_setopt($curl, CURLOPT_HTTPHEADER, ['Content-type' => 'application/json']);
69
        curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($protocol));
70
        $ret = curl_exec($curl);
0 ignored issues
show
The assignment to $ret is dead and can be removed.
Loading history...
71
        $code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
72
        if ($code != 200) {
73
            var_dump('VictorOps returned Error, retry later'); //FIXME: propper debuging
74
75
            return false;
76
        }
77
78
        return true;
79
    }
80
81
    public static function configTemplate()
82
    {
83
        return [
84
            'config' => [
85
                [
86
                    'title' => 'Post URL',
87
                    'name' => 'victorops-url',
88
                    'descr' => 'Victorops Post URL',
89
                    'type' => 'text',
90
                ],
91
            ],
92
            'validation' => [
93
                'victorops-url' => 'required|string',
94
            ],
95
        ];
96
    }
97
}
98