Issues (2963)

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

1
<?php
2
/* Copyright (C) 2015 James Campbell <[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
/* Copyright (C) 2015 Daniel Preussker <[email protected]>
17
 * This program is free software: you can redistribute it and/or modify
18
 * it under the terms of the GNU General Public License as published by
19
 * the Free Software Foundation, either version 3 of the License, or
20
 * (at your option) any later version.
21
 *
22
 * This program is distributed in the hope that it will be useful,
23
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
25
 * GNU General Public License for more details.
26
 *
27
 * You should have received a copy of the GNU General Public License
28
 * along with this program.  If not, see <https://www.gnu.org/licenses/>. */
29
30
/**
31
 * Pushover API Transport
32
 *
33
 * @author neokjames <[email protected]>
34
 * @copyright 2015 neokjames, f0o, LibreNMS
35
 * @license GPL
36
 */
37
38
namespace LibreNMS\Alert\Transport;
39
40
use LibreNMS\Alert\Transport;
41
use LibreNMS\Enum\AlertState;
42
43
class Pushover extends Transport
44
{
45
    public function deliverAlert($obj, $opts)
46
    {
47
        $pushover_opts = $this->config;
48
        $pushover_opts['options'] = $this->parseUserOptions($this->config['options']);
49
50
        return $this->contactPushover($obj, $pushover_opts);
51
    }
52
53
    public function contactPushover($obj, $api)
54
    {
55
        $data = [];
56
        $data['token'] = $api['appkey'];
57
        $data['user'] = $api['userkey'];
58
        switch ($obj['severity']) {
59
            case 'critical':
60
                $data['priority'] = 1;
61
                if (! empty($api['options']['sound_critical'])) {
62
                    $data['sound'] = $api['options']['sound_critical'];
63
                }
64
                break;
65
            case 'warning':
66
                $data['priority'] = 1;
67
                if (! empty($api['options']['sound_warning'])) {
68
                    $data['sound'] = $api['options']['sound_warning'];
69
                }
70
                break;
71
        }
72
        switch ($obj['state']) {
73
            case AlertState::RECOVERED:
74
                $data['priority'] = 0;
75
                if (! empty($api['options']['sound_ok'])) {
76
                    $data['sound'] = $api['options']['sound_ok'];
77
                }
78
                break;
79
        }
80
        $data['title'] = $obj['title'];
81
        $data['message'] = $obj['msg'];
82
        if ($api['options']) {
83
            $data = array_merge($data, $api['options']);
84
        }
85
        $curl = curl_init();
86
        set_curl_proxy($curl);
87
        curl_setopt($curl, CURLOPT_URL, 'https://api.pushover.net/1/messages.json');
88
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
89
        curl_setopt($curl, CURLOPT_SAFE_UPLOAD, true);
90
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
91
        $ret = curl_exec($curl);
0 ignored issues
show
The assignment to $ret is dead and can be removed.
Loading history...
92
        $code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
93
        if ($code != 200) {
94
            var_dump('Pushover returned error'); //FIXME: proper debugging
95
96
            return 'HTTP Status code ' . $code;
97
        }
98
99
        return true;
100
    }
101
102
    public static function configTemplate()
103
    {
104
        return [
105
            'config' => [
106
                [
107
                    'title' => 'Api Key',
108
                    'name'  => 'appkey',
109
                    'descr' => 'Api Key',
110
                    'type'  => 'text',
111
                ],
112
                [
113
                    'title' => 'User Key',
114
                    'name'  => 'userkey',
115
                    'descr' => 'User Key',
116
                    'type'  => 'text',
117
                ],
118
                [
119
                    'title' => 'Pushover Options',
120
                    'name'  => 'options',
121
                    'descr' => 'Pushover options',
122
                    'type'  => 'textarea',
123
                ],
124
            ],
125
            'validation' => [
126
                'appkey' => 'required',
127
                'userkey' => 'required',
128
            ],
129
        ];
130
    }
131
}
132