Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor alert transports to classes #7844

Merged
merged 4 commits into from Dec 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
60 changes: 60 additions & 0 deletions LibreNMS/Alert/Transport/Api.php
@@ -0,0 +1,60 @@
<?php
/* Copyright (C) 2014 Daniel Preussker <f0o@devilcode.org>
* 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/>. */

/**
* API Transport
* @author f0o <f0o@devilcode.org>
* @copyright 2014 f0o, LibreNMS
* @license GPL
* @package LibreNMS
* @subpackage Alerts
*/

namespace LibreNMS\Alert\Transport;

use LibreNMS\Interfaces\Alert\Transport;

class Api implements Transport
{
public function deliverAlert($obj, $opts)
{
foreach ($opts as $method => $apis) {
// var_dump($method); //FIXME: propper debuging
foreach ($apis as $api) {
// var_dump($api); //FIXME: propper debuging
list($host, $api) = explode("?", $api, 2);
foreach ($obj as $k => $v) {
$api = str_replace("%" . $k, $method == "get" ? urlencode($v) : $v, $api);
}
// var_dump($api); //FIXME: propper debuging
$curl = curl_init();
set_curl_proxy($curl);
curl_setopt($curl, CURLOPT_URL, ($method == "get" ? $host."?".$api : $host));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, strtoupper($method));
curl_setopt($curl, CURLOPT_POSTFIELDS, $api);
$ret = curl_exec($curl);
$code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ($code != 200) {
var_dump("API '$host' returned Error"); //FIXME: propper debuging
var_dump("Params: ".$api); //FIXME: propper debuging
var_dump("Return: ".$ret); //FIXME: propper debuging
return 'HTTP Status code '.$code;
}
}
}
return true;
}
}
103 changes: 103 additions & 0 deletions LibreNMS/Alert/Transport/Boxcar.php
@@ -0,0 +1,103 @@
<?php
/* Copyright (C) 2015 James Campbell <neokjames@gmail.com>
* 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 <f0o@devilcode.org>
* 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 <jan@trick77.com>
* @copyright 2015 trick77, neokjames, f0o, LibreNMS
* @license GPL
* @package LibreNMS
* @subpackage Alerts
*/

namespace LibreNMS\Alert\Transport;

use LibreNMS\Interfaces\Alert\Transport;

class Boxcar implements Transport
{
public function deliverAlert($obj, $opts)
{
global $config;
foreach ($opts as $api) {
$data = array();
$data['user_credentials'] = $api['access_token'];
$data['notification[source_name]'] = $config['project_id'];
switch ($obj['severity']) {
case "critical":
$severity = "Critical";
if (!empty($api['sound_critical'])) {
$data['notification[sound]'] = $api['sound_critical'];
}
break;
case "warning":
$severity = "Warning";
if (!empty($api['sound_warning'])) {
$data['notification[sound]'] = $api['sound_warning'];
}
break;
}
switch ($obj['state']) {
case 0:
$title_text = "OK";
if (!empty($api['sound_ok'])) {
$data['notification[sound]'] = $api['sound_ok'];
}
break;
case 1:
$title_text = $severity;
break;
case 2:
$title_text = "Acknowledged";
break;
}
$data['notification[title]'] = $title_text . " - " . $obj['hostname'] . " - " . $obj['name'];
$message_text = "Timestamp: " . $obj['timestamp'];
if (!empty($obj['faults'])) {
$message_text .= "\n\nFaults:\n";
foreach ($obj['faults'] as $k => $faults) {
$message_text .= "#" . $k . " " . $faults['string'] . "\n";
}
}
$data['notification[long_message]'] = $message_text;
$curl = curl_init();
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);
$ret = curl_exec($curl);
$code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ($code != 201) {
var_dump("Boxcar returned error"); //FIXME: proper debugging
return false;
}
}
return true;
}
}
71 changes: 71 additions & 0 deletions LibreNMS/Alert/Transport/Canopsis.php
@@ -0,0 +1,71 @@
<?php
namespace LibreNMS\Alert\Transport;

use LibreNMS\Interfaces\Alert\Transport;

class Canopsis implements Transport
{
public function deliverAlert($obj, $opts)
{
// Configurations
$host = $opts["host"];
$port = $opts["port"];
$user = $opts["user"];
$pass = $opts["passwd"];
$vhost = $opts["vhost"];
$exchange = "canopsis.events";

// Connection
$conn = new \PhpAmqpLib\Connection\AMQPConnection($host, $port, $user, $pass, $vhost);
$ch = $conn->channel();

// Declare exchange (if not exist)
// exchange_declare($exchange, $type, $passive=false, $durable=false, $auto_delete=true, $internal=false, $nowait=false, $arguments=null, $ticket=null)
$ch->exchange_declare($exchange, 'topic', false, true, false);

// Create Canopsis event, see: https://github.com/capensis/canopsis/wiki/Event-specification
switch ($obj['severity']) {
case "ok":
$state = 0;
break;
case "warning":
$state = 1;
break;
case "critical":
$state = 2;
break;
default:
$state = 3;
}
$msg_body = array(
"timestamp" => time(),
"connector" => "librenms",
"connector_name" => "LibreNMS1",
"event_type" => "check",
"source_type" => "resource",
"component" => $obj['hostname'],
"resource" => $obj['faults'][1]['storage_descr'],
"state" => $state,
"state_type" => 1,
"output" => $obj['msg'],
"display_name" => "librenms"
);
$msg_raw = json_encode($msg_body);

// Build routing key
if ($msg_body['source_type'] == "resource") {
$msg_rk = $msg_rk . "." . $msg_body['resource'];
} else {
$msg_rk = $msg_body['connector'] . "." . $msg_body['connector_name'] . "." . $msg_body['event_type'] . "." . $msg_body['source_type'] . "." . $msg_body['component'];
}

// Publish Event
$msg = new \PhpAmqpLib\Message\AMQPMessage($msg_raw, array('content_type' => 'application/json', 'delivery_mode' => 2));
$ch->basic_publish($msg, $exchange, $msg_rk);

// Close connection
$ch->close();
$conn->close();
return true;
}
}
47 changes: 47 additions & 0 deletions LibreNMS/Alert/Transport/Ciscospark.php
@@ -0,0 +1,47 @@
<?php
/*
* LibreNMS
*
* Copyright (c) 2017 Søren Friis Rosiak <sorenrosiak@gmail.com>
* 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. Please see LICENSE.txt at the top level of
* the source code distribution for details.
*/
namespace LibreNMS\Alert\Transport;

use LibreNMS\Interfaces\Alert\Transport;

class Ciscospark implements Transport
{
public function deliverAlert($obj, $opts)
{
$token = $opts['token'];
$roomId = $opts['roomid'];
$text = strip_tags($obj['msg']);
$data = array(
'roomId' => $roomId,
'text' => $text
);
$curl = curl_init();
set_curl_proxy($curl);
curl_setopt($curl, CURLOPT_URL, 'https://api.ciscospark.com/v1/messages');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Content-type' => 'application/json',
'Expect:',
'Authorization: Bearer ' . $token
));
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
$ret = curl_exec($curl);
$code = curl_getinfo($curl, CURLINFO_HTTP_CODE);

if ($code != 200) {
var_dump("Cisco Spark returned Error, retry later");
return false;
}

return true;
}
}
@@ -1,3 +1,4 @@
<?php
/* Copyright (C) 2015 Daniel Preussker <f0o@librenms.org>
* 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
Expand All @@ -20,20 +21,29 @@
* @package LibreNMS
* @subpackage Alerts
*/
namespace LibreNMS\Alert\Transport;

$url = 'https://platform.clickatell.com/messages/http/send?apiKey='.$opts['token'].'&to='.implode(',',$opts['to']).'&content='.urlencode($obj['title']);
use LibreNMS\Interfaces\Alert\Transport;

$curl = curl_init($url);
set_curl_proxy($curl);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
class Clickatell implements Transport
{
public function deliverAlert($obj, $opts)
{
$url = 'https://platform.clickatell.com/messages/http/send?apiKey=' . $opts['token'] . '&to=' . implode(',', $opts['to']) . '&content=' . urlencode($obj['title']);

$ret = curl_exec($curl);
$code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if( $code > 200 ) {
if( $debug ) {
var_dump($ret);
$curl = curl_init($url);
set_curl_proxy($curl);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

$ret = curl_exec($curl);
$code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ($code > 200) {
if ($debug) {
var_dump($ret);
}
return false;
}
return true;
}
return false;
}
return true;
@@ -1,3 +1,4 @@
<?php
/* Copyright (C) 2014 Daniel Preussker <f0o@devilcode.org>
* 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
Expand All @@ -20,6 +21,15 @@
* @package LibreNMS
* @subpackage Alerts
*/
namespace LibreNMS\Alert\Transport;

var_dump($obj);
return true;
use LibreNMS\Interfaces\Alert\Transport;

class Dummy implements Transport
{
public function deliverAlert($obj, $opts)
{
var_dump($obj);
return true;
}
}