Tidy up some transport code from previous pr (#8927)

This commit is contained in:
Neil Lathwood
2018-08-17 22:38:00 +01:00
committed by GitHub
parent 50210234c1
commit bc2fe6aeb1
10 changed files with 47 additions and 30 deletions

View File

@@ -3,35 +3,42 @@ namespace LibreNMS\Alert;
class AlertUtil
{
// Return rule id from alert id
/**
*
* Get the rule_id for a specific alert
*
* @param $alert_id
* @return mixed|null
*/
private static function getRuleId($alert_id)
{
$query = "SELECT `rule_id` FROM `alerts` WHERE `id`=?";
return dbFetchCell($query, [$alert_id]);
}
// Return all alert transports mapped to a rule (includies transport groups)
// @retyurn array [$transport_id $transport_type]
/**
*
* Get the transport for a given alert_id
*
* @param $alert_id
* @return array
*/
public static function getAlertTransports($alert_id)
{
// Query for list of transport ids
$query = "SELECT b.transport_id, b.transport_type FROM alert_transport_map AS a LEFT JOIN alert_transports AS b ON b.transport_id=a.transport_or_group_id WHERE a.target_type='single' AND a.rule_id=? UNION DISTINCT SELECT d.transport_id, d.transport_type FROM alert_transport_map AS a LEFT JOIN alert_transport_groups AS b ON a.transport_or_group_id=b.transport_group_id LEFT JOIN transport_group_transport AS c ON b.transport_group_id=c.transport_group_id LEFT JOIN alert_transports AS d ON c.transport_id=d.transport_id WHERE a.target_type='group' AND a.rule_id=?";
$rule_id = self::getRuleId($alert_id);
return dbFetchRows($query, [$rule_id, $rule_id]);
}
// Return transports configured as default
// @return array [$transport_id $transport_type]
/**
*
* Returns the default transports
*
* @return array
*/
public static function getDefaultAlertTransports()
{
$query = "SELECT transport_id, transport_type FROM alert_transports WHERE is_default=true";
return dbFetchRows($query);
}
// Return list of transport types with a default configured
public static function getDefaultTransportList()
{
$query = "SELECT DISTINCT transport_type FROM alert_transports WHERE is_default=true ";
return dbFetchColumn($query);
}
}

View File

@@ -8,7 +8,10 @@ abstract class Transport implements TransportInterface
{
protected $config;
// Sets config field to an associative array of transport config values
/**
* Transport constructor.
* @param null $transport_id
*/
public function __construct($transport_id = null)
{
if (!empty($transport_id)) {

View File

@@ -1,5 +1,5 @@
<?php
/* Copyright (C) 2014 Daniel Preussker <f0o>
/* 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
@@ -11,11 +11,11 @@
* 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>. */
* along with this program. If not, see <http://www.gnu.org/licenses/>. */
/**
* API Transport
* @author f0o <f0o>
* @author f0o <f0o@devilcode.org>
* @copyright 2014 f0o, LibreNMS
* @license GPL
* @package LibreNMS

View File

@@ -35,4 +35,9 @@ interface Transport
* @return mixed Returns if the call was successful
*/
public function deliverAlert($alert_data, $opts);
/**
* @return array
*/
public static function configTemplate();
}

View File

@@ -17,7 +17,7 @@ header('Content-type: application/json');
if (!Auth::user()->hasGlobalAdmin()) {
die(json_encode([
'status' => 'error',
'message' => 'ERROR: You need to be admin.'
'message' => 'You need to be admin.'
]));
}
@@ -26,7 +26,7 @@ $message = '';
if (!is_numeric($vars['transport_id'])) {
$status = 'error';
$message = 'ERROR: No transport selected';
$message = 'No transport selected';
} else {
if (dbDelete('alert_transports', '`transport_id` = ?', [$vars['transport_id']])) {
dbDelete('alert_transport_map', '`target_type` = "single" AND `transport_or_group_id` = ?', [$vars['transport_id']]);
@@ -34,7 +34,7 @@ if (!is_numeric($vars['transport_id'])) {
$message = 'Alert transport has been deleted';
} else {
$message = 'ERROR: Alert transport has not been deleted';
$message = 'Alert transport has not been deleted';
}
}

View File

@@ -17,7 +17,7 @@ header('Content-type: application/json');
if (!Auth::user()->hasGlobalAdmin()) {
die(json_encode([
'status' => 'error',
'message' => 'ERROR: You need to be admin'
'message' => 'You need to be admin'
]));
}
@@ -51,6 +51,6 @@ if (is_array($transport)) {
} else {
die(json_encode([
'status' => 'error',
'message' => 'ERROR: No alert transport found'
'message' => 'No alert transport found'
]));
}

View File

@@ -17,7 +17,7 @@ header('Content-type: application/json');
if (!Auth::user()->hasGlobalAdmin()) {
die(json_encode([
'status' => 'error',
'message' => 'ERROR: You need to be admin'
'message' => 'You need to be admin'
]));
}
@@ -45,6 +45,6 @@ if (is_array($members)) {
} else {
die(json_encode([
'status' => 'error',
'message' => 'ERROR: No transport group found'
'message' => 'No transport group found'
]));
}

View File

@@ -36,7 +36,7 @@ $params = [];
if (!empty($_REQUEST['search'])) {
$query .= ' WHERE `transport_group_name` LIKE ?';
$params[] = '%' . mres($_REQUEST['search']) . '%';
$params[] = '%' . $vars['search'] . '%';
}
$total = dbFetchCell("SELECT COUNT(*) FROM `alert_transport_groups` $query", $params);

View File

@@ -32,17 +32,17 @@ if (!Auth::user()->hasGlobalRead()) {
$query = '';
$params = [];
if (!empty($_REQUEST['search'])) {
if (!empty($vars['search'])) {
$query .= ' WHERE `transport_name` LIKE ?';
$params[] = '%' . mres($_REQUEST['search']) . '%';
$params[] = '%' . $vars['search'] . '%';
}
$total = dbFetchCell("SELECT COUNT(*) FROM `alert_transports` $query", $params);
$more = false;
if (!empty($_REQUEST['limit'])) {
$limit = (int) $_REQUEST['limit'];
$page = isset($_REQUEST['page']) ? (int) $_REQUEST['page'] : 1;
$limit = (int) $vars['limit'];
$page = isset($vars['page']) ? (int) $vars['page'] : 1;
$offset = ($page - 1) * $limit;
$query .= " LIMIT $offset, $limit";

View File

@@ -820,7 +820,9 @@ function ExtTransports($obj)
if (!$transport_maps) {
$transport_maps = AlertUtil::getDefaultAlertTransports();
$legacy_transports = AlertUtil::getDefaultTransportList();
$legacy_transports = array_unique(array_map(function ($transports) {
return $transports['transport_type'];
}, $transport_maps));
foreach ($config['alert']['transports'] as $transport => $opts) {
if (in_array($transport, $legacy_transports)) {
// If it is a default transport type, then the alert has already been sent out, so skip