Files

135 lines
4.8 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
* along with this program. If not, see <http://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
* along with this program. If not, see <http://www.gnu.org/licenses/>. */
/**
* Boxcar API Transport
* @author trick77 <[email protected]>
* @copyright 2015 trick77, neokjames, f0o, LibreNMS
* @license GPL
*/
namespace LibreNMS\Alert\Transport;
2018-07-21 13:34:59 -06:00
use LibreNMS\Alert\Transport;
use LibreNMS\Config;
2020-09-21 14:54:51 +02:00
use LibreNMS\Enum\AlertState;
2018-07-21 13:34:59 -06:00
class Boxcar extends Transport
{
public function deliverAlert($obj, $opts)
{
$boxcar_opts = $this->parseUserOptions($this->config['options']);
2018-07-21 13:34:59 -06:00
$boxcar_opts['access_token'] = $this->config['boxcar-token'];
2018-07-21 13:34:59 -06:00
return $this->contactBoxcar($obj, $boxcar_opts);
}
public static function contactBoxcar($obj, $api)
{
2020-09-21 14:54:51 +02:00
$data = [];
$data['user_credentials'] = $api['access_token'];
2018-07-21 13:34:59 -06:00
$data['notification[source_name]'] = Config::get('project_id', 'librenms');
switch ($obj['severity']) {
2020-09-21 15:59:34 +02:00
case 'critical':
$severity = 'Critical';
2020-09-21 14:54:51 +02:00
if (! empty($api['sound_critical'])) {
2018-07-21 13:34:59 -06:00
$data['notification[sound]'] = $api['sound_critical'];
}
2018-07-21 13:34:59 -06:00
break;
2020-09-21 15:59:34 +02:00
case 'warning':
$severity = 'Warning';
2020-09-21 14:54:51 +02:00
if (! empty($api['sound_warning'])) {
2018-07-21 13:34:59 -06:00
$data['notification[sound]'] = $api['sound_warning'];
}
break;
}
switch ($obj['state']) {
2020-05-24 04:14:36 +02:00
case AlertState::RECOVERED:
2020-09-21 15:59:34 +02:00
$title_text = 'OK';
2020-09-21 14:54:51 +02:00
if (! empty($api['sound_ok'])) {
2018-07-21 13:34:59 -06:00
$data['notification[sound]'] = $api['sound_ok'];
}
break;
2020-05-24 04:14:36 +02:00
case AlertState::Active:
2018-07-21 13:34:59 -06:00
$title_text = $severity;
break;
2020-05-24 04:14:36 +02:00
case AlertState::ACKNOWLEDGED:
2020-09-21 15:59:34 +02:00
$title_text = 'Acknowledged';
2018-07-21 13:34:59 -06:00
break;
}
2020-09-21 15:59:34 +02:00
$data['notification[title]'] = $title_text . ' - ' . $obj['hostname'] . ' - ' . $obj['name'];
$message_text = 'Timestamp: ' . $obj['timestamp'];
2020-09-21 14:54:51 +02:00
if (! empty($obj['faults'])) {
2018-07-21 13:34:59 -06:00
$message_text .= "\n\nFaults:\n";
foreach ($obj['faults'] as $k => $faults) {
2020-09-21 15:59:34 +02:00
$message_text .= '#' . $k . ' ' . $faults['string'] . "\n";
}
}
2018-07-21 13:34:59 -06:00
$data['notification[long_message]'] = $message_text;
2020-09-21 14:54:51 +02:00
$curl = curl_init();
2018-07-21 13:34:59 -06:00
set_curl_proxy($curl);
curl_setopt($curl, CURLOPT_URL, 'https://new.boxcar.io/api/notifications');
curl_setopt($curl, CURLOPT_SAFE_UPLOAD, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_exec($curl);
$code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ($code != 201) {
2020-09-21 15:59:34 +02:00
var_dump('Boxcar returned error'); //FIXME: proper debugging
2020-09-21 14:54:51 +02:00
2018-07-21 13:34:59 -06:00
return false;
}
2018-07-31 15:53:03 -05:00
return true;
2018-07-21 13:34:59 -06:00
}
public static function configTemplate()
{
return [
'config' => [
[
'title' => 'Access Token',
'name' => 'boxcar-token',
'descr' => 'Boxcar Access Token',
'type' => 'text',
],
[
'title' => 'Boxcar Options',
'name' => 'boxcar-options',
'descr' => 'Boxcar Options',
'type' => 'textarea',
2020-09-21 14:54:51 +02:00
],
2018-07-21 13:34:59 -06:00
],
'validation' => [
'boxcar-token' => 'required',
2020-09-21 14:54:51 +02:00
],
2018-07-21 13:34:59 -06:00
];
}
}