Files

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

122 lines
4.1 KiB
PHP
Raw Permalink Normal View History

<?php
/* Copyright (C) 2015 James Campbell <[email protected]>
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
2021-02-09 00:29:04 +01:00
* along with this program. If not, see <https://www.gnu.org/licenses/>. */
/* Copyright (C) 2015 Daniel Preussker <[email protected]>
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
2021-02-09 00:29:04 +01:00
* along with this program. If not, see <https://www.gnu.org/licenses/>. */
/**
* Pushover API Transport
2021-09-10 20:09:53 +02:00
*
* @author neokjames <[email protected]>
* @copyright 2015 neokjames, f0o, LibreNMS
* @license GPL
*/
2020-09-21 14:54:51 +02:00
namespace LibreNMS\Alert\Transport;
2018-07-21 13:34:59 -06:00
use LibreNMS\Alert\Transport;
2020-05-24 04:14:36 +02:00
use LibreNMS\Enum\AlertState;
use LibreNMS\Exceptions\AlertTransportDeliveryException;
use LibreNMS\Util\Http;
2018-07-21 13:34:59 -06:00
class Pushover extends Transport
{
public function deliverAlert(array $alert_data): bool
2018-07-21 13:34:59 -06:00
{
$options = $this->parseUserOptions($this->config['options']);
$url = 'https://api.pushover.net/1/messages.json';
2018-07-21 13:34:59 -06:00
$data = [];
$data['token'] = $this->config['appkey'];
$data['user'] = $this->config['userkey'];
switch ($alert_data['severity']) {
2018-07-21 13:34:59 -06:00
case 'critical':
$data['priority'] = 1;
if (! empty($options['sound_critical'])) {
$data['sound'] = $options['sound_critical'];
2018-07-21 13:34:59 -06:00
}
break;
case 'warning':
$data['priority'] = 1;
if (! empty($options['sound_warning'])) {
$data['sound'] = $options['sound_warning'];
2018-07-21 13:34:59 -06:00
}
break;
}
switch ($alert_data['state']) {
2020-05-24 04:14:36 +02:00
case AlertState::RECOVERED:
2018-07-21 13:34:59 -06:00
$data['priority'] = 0;
if (! empty($options['sound_ok'])) {
$data['sound'] = $options['sound_ok'];
2018-07-21 13:34:59 -06:00
}
break;
}
$data['title'] = $alert_data['title'];
$data['message'] = $alert_data['msg'];
if ($options) {
$data = array_merge($data, $options);
2018-07-21 13:34:59 -06:00
}
2020-09-21 14:54:51 +02:00
$res = Http::client()->asForm()->post($url, $data);
if ($res->successful()) {
return true;
2018-07-21 13:34:59 -06:00
}
2020-09-21 14:54:51 +02:00
throw new AlertTransportDeliveryException($alert_data, $res->status(), $res->body(), $data['message'], $data);
2018-07-21 13:34:59 -06:00
}
public static function configTemplate(): array
2018-07-21 13:34:59 -06:00
{
return [
'config' => [
[
'title' => 'Api Key',
'name' => 'appkey',
'descr' => 'Api Key',
'type' => 'password',
2018-07-21 13:34:59 -06:00
],
[
'title' => 'User Key',
'name' => 'userkey',
'descr' => 'User Key',
'type' => 'password',
2018-07-21 13:34:59 -06:00
],
[
'title' => 'Pushover Options',
'name' => 'options',
'descr' => 'Pushover options',
'type' => 'textarea',
],
],
'validation' => [
'appkey' => 'required',
'userkey' => 'required',
],
];
}
}