Removed legacy transports and templates code (#9646)

* Removed legacy transports and templates code

* Add notification

* abstract LnmsCommand

* Only issue alerts "legacy" alerts to mail for default contacts, etc.

* Prevent no contacts error when default contact is not specified, or there are no contacts for other reasons.

* remove eval \o/

* Update notifications.rss
This commit is contained in:
Neil Lathwood
2019-02-15 13:58:59 +00:00
committed by Tony Murray
parent 0835d8899e
commit 5d74839139
6 changed files with 25 additions and 298 deletions

View File

@@ -28,6 +28,7 @@ use LibreNMS\Alert\AlertData;
use LibreNMS\Alerting\QueryBuilderParser;
use LibreNMS\Authentication\LegacyAuth;
use LibreNMS\Alert\AlertUtil;
use LibreNMS\Config;
use PHPMailer\PHPMailer\PHPMailer;
/**
@@ -254,13 +255,14 @@ function RunRules($device_id)
*/
function GetContacts($results)
{
global $config, $authorizer;
global $config;
if (sizeof($results) == 0) {
return array();
if (empty($results)) {
return [];
}
if ($config['alert']['default_only'] === true || $config['alerts']['email']['default_only'] === true) {
return array(''.($config['alert']['default_mail'] ? $config['alert']['default_mail'] : $config['alerts']['email']['default']) => '');
if (Config::get('alert.default_only') === true || Config::get('alerts.email.default_only') === true) {
$email = Config::get('alert.default_mail', Config::get('alerts.email.default'));
return $email ? [$email => ''] : [];
}
$users = LegacyAuth::get()->getUserlist();
$contacts = array();
@@ -386,20 +388,6 @@ function populate($txt, $wrap = true)
return $txt;
}//end populate()
/**
* "Safely" run eval
* @param string $code Code to run
* @param array $obj Object with variables
* @return string|mixed
*/
function RunJail($code, $obj)
{
$ret = '';
@eval($code);
return $ret;
}//end RunJail()
/**
* Describe Alert
* @param array $alert Alert-Result from DB
@@ -822,8 +810,6 @@ function RunAlerts()
*/
function ExtTransports($obj)
{
global $config;
$tmp = false;
$type = new Template;
// If alert transport mapping exists, override the default transports
@@ -831,47 +817,28 @@ function ExtTransports($obj)
if (!$transport_maps) {
$transport_maps = AlertUtil::getDefaultAlertTransports();
$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
continue;
}
if (is_array($opts)) {
$opts = array_filter($opts);
}
$class = 'LibreNMS\\Alert\\Transport\\' . ucfirst($transport);
if (($opts === true || !empty($opts)) && $opts != false && class_exists($class)) {
$transport_maps[] = [
'transport_id' => null,
'transport_type' => $transport,
'opts' => $opts,
'legacy' => true,
];
}
}
unset($legacy_transports);
}
// alerting for default contacts, etc
if (Config::get('alert.transports.mail') === true && !empty($obj['contacts'])) {
$transport_maps[] = [
'transport_id' => null,
'transport_type' => 'mail',
'opts' => $obj,
];
}
foreach ($transport_maps as $item) {
$class = 'LibreNMS\\Alert\\Transport\\'.ucfirst($item['transport_type']);
//FIXME remove Deprecated noteice
$dep_notice = 'DEPRECATION NOTICE: https://t.libren.ms/deprecation-alerting';
if (class_exists($class)) {
//FIXME remove Deprecated transport
$transport_title = ($item['legacy'] === true) ? "Transport {$item['transport_type']} (%YTransport $dep_notice%n)" : "Transport {$item['transport_type']}";
$transport_title = "Transport {$item['transport_type']}";
$obj['transport'] = $item['transport_type'];
$obj['transport_name'] = $item['transport_name'];
$obj['alert'] = new AlertData($obj);
$obj['title'] = $type->getTitle($obj);
$obj['alert']['title'] = $obj['title'];
$obj['msg'] = $type->getBody($obj);
//FIXME remove Deprecated template check
if (preg_match('/{\/if}/', $type->getTemplate()->template)) {
c_echo(" :: %YTemplate $dep_notice :: Please update your template " . $type->getTemplate()->name . "%n" . PHP_EOL);
}
c_echo(" :: $transport_title => ");
$instance = new $class($item['transport_id']);
$tmp = $instance->deliverAlert($obj, $item['opts']);