From deb405fac9ef5f982d5997da40ae73932478129d Mon Sep 17 00:00:00 2001 From: Tony Murray Date: Tue, 31 Jul 2018 15:53:03 -0500 Subject: [PATCH] Alert transport fixes (#8967) DO NOT DELETE THIS TEXT #### Please note > Please read this information carefully. You can run `./scripts/pre-commit.php` to check your code before submitting. - [x] Have you followed our [code guidelines?](http://docs.librenms.org/Developing/Code-Guidelines/) #### Testers If you would like to test this pull request then please run: `./scripts/github-apply `, i.e `./scripts/github-apply 5926` --- LibreNMS/Alert/Transport/Api.php | 4 +++- LibreNMS/Alert/Transport/Boxcar.php | 2 ++ LibreNMS/Alert/Transport/Ciscospark.php | 1 - LibreNMS/Alert/Transport/Hipchat.php | 2 ++ LibreNMS/Alert/Transport/Irc.php | 2 ++ LibreNMS/Alert/Transport/Mail.php | 4 ++-- LibreNMS/Alert/Transport/Playsms.php | 2 +- alerts.php | 2 +- html/ajax_form.php | 2 +- html/includes/forms/test-transport.inc.php | 15 +++++++++------ html/includes/print-alert-transports.php | 14 +++++++------- html/pages/settings/alerting.inc.php | 4 ++-- includes/functions.php | 2 ++ includes/init.php | 7 ------- scripts/test-alert.php | 4 ++-- scripts/test-template.php | 2 +- 16 files changed, 37 insertions(+), 32 deletions(-) diff --git a/LibreNMS/Alert/Transport/Api.php b/LibreNMS/Alert/Transport/Api.php index e99e413b06..0a4ee945b1 100644 --- a/LibreNMS/Alert/Transport/Api.php +++ b/LibreNMS/Alert/Transport/Api.php @@ -35,7 +35,7 @@ class Api extends Transport } $url = $this->config['api-url']; $method = $this->config['api-method']; - $this->contactAPI($obj, $url, $method); + return $this->contactAPI($obj, $url, $method); } private function deliverAlertOld($obj, $opts) @@ -73,6 +73,8 @@ class Api extends Transport var_dump("Return: ".$ret); //FIXME: propper debuging return 'HTTP Status code '.$code; } + + return true; } public static function configTemplate() diff --git a/LibreNMS/Alert/Transport/Boxcar.php b/LibreNMS/Alert/Transport/Boxcar.php index 24f8f33915..ff19b45d04 100644 --- a/LibreNMS/Alert/Transport/Boxcar.php +++ b/LibreNMS/Alert/Transport/Boxcar.php @@ -118,6 +118,8 @@ class Boxcar extends Transport var_dump("Boxcar returned error"); //FIXME: proper debugging return false; } + + return true; } public static function configTemplate() diff --git a/LibreNMS/Alert/Transport/Ciscospark.php b/LibreNMS/Alert/Transport/Ciscospark.php index 2d3445f503..c1c582de3b 100644 --- a/LibreNMS/Alert/Transport/Ciscospark.php +++ b/LibreNMS/Alert/Transport/Ciscospark.php @@ -34,7 +34,6 @@ class Ciscospark extends Transport 'roomId' => $room_id, 'text' => $text ); - $token = $token; $curl = curl_init(); set_curl_proxy($curl); diff --git a/LibreNMS/Alert/Transport/Hipchat.php b/LibreNMS/Alert/Transport/Hipchat.php index a1830fc994..b937bf3319 100644 --- a/LibreNMS/Alert/Transport/Hipchat.php +++ b/LibreNMS/Alert/Transport/Hipchat.php @@ -118,6 +118,8 @@ class Hipchat extends Transport var_dump("Return: " . $ret); return 'HTTP Status code ' . $code; } + + return true; } public static function configTemplate() diff --git a/LibreNMS/Alert/Transport/Irc.php b/LibreNMS/Alert/Transport/Irc.php index 3da9ff2ec6..2403489be7 100644 --- a/LibreNMS/Alert/Transport/Irc.php +++ b/LibreNMS/Alert/Transport/Irc.php @@ -46,6 +46,8 @@ class Irc extends Transport return true; } } + + return false; } public static function configTemplate() diff --git a/LibreNMS/Alert/Transport/Mail.php b/LibreNMS/Alert/Transport/Mail.php index 61182b5e5e..cffafa44bc 100644 --- a/LibreNMS/Alert/Transport/Mail.php +++ b/LibreNMS/Alert/Transport/Mail.php @@ -30,10 +30,10 @@ class Mail extends Transport { public function deliverAlert($obj, $opts) { - return $this->contactMail($obj, $opts); + return $this->contactMail($obj); } - public function contactMail($obj, $opts) + public function contactMail($obj) { if (empty($this->config['email'])) { $email = $obj['contacts']; diff --git a/LibreNMS/Alert/Transport/Playsms.php b/LibreNMS/Alert/Transport/Playsms.php index 872bf10698..50fe1e9de6 100644 --- a/LibreNMS/Alert/Transport/Playsms.php +++ b/LibreNMS/Alert/Transport/Playsms.php @@ -37,7 +37,7 @@ class Playsms extends Transport $playsms_opts['token'] = $this->config['playsms-token']; $playsms_opts['from'] = $this->config['playsms-from']; $playsms_opts['to'] = preg_split('/([,\r\n]+)/', $this->config['playsms-mobiles']); - return $this->contactDiscord($obj, $playsms_opts); + return $this->contactPlaysms($obj, $playsms_opts); } public function deliverAlertOld($obj, $opts) diff --git a/alerts.php b/alerts.php index 1880c0c39f..140af5908a 100755 --- a/alerts.php +++ b/alerts.php @@ -27,7 +27,7 @@ use LibreNMS\Util\FileLock; -$init_modules = ['alerts', 'alerts-cli']; +$init_modules = ['alerts', 'laravel']; require __DIR__ . '/includes/init.php'; $options = getopt('d::'); diff --git a/html/ajax_form.php b/html/ajax_form.php index b1f2f04019..50a0cec201 100644 --- a/html/ajax_form.php +++ b/html/ajax_form.php @@ -16,7 +16,7 @@ use LibreNMS\Authentication\Auth; -$init_modules = array('web', 'auth', 'alerts', 'eloquent'); +$init_modules = array('web', 'auth', 'alerts', 'laravel'); require realpath(__DIR__ . '/..') . '/includes/init.php'; set_debug(isset($_REQUEST['debug']) ? $_REQUEST['debug'] : false); diff --git a/html/includes/forms/test-transport.inc.php b/html/includes/forms/test-transport.inc.php index 6bc3bbfc36..fae3d44e1b 100644 --- a/html/includes/forms/test-transport.inc.php +++ b/html/includes/forms/test-transport.inc.php @@ -13,6 +13,7 @@ */ use LibreNMS\Authentication\Auth; +use LibreNMS\Config; if (!Auth::user()->hasGlobalAdmin()) { header('Content-type: text/plain'); @@ -47,19 +48,21 @@ $obj = array( "msg" => "This is a test alert", ); -$status = 'error'; +$response = ['status' => 'error']; if ($transport_id) { $transport = dbFetchCell("SELECT `transport_type` FROM `alert_transports` WHERE `transport_id` = ?", [$transport_id]); } $class = 'LibreNMS\\Alert\\Transport\\' . ucfirst($transport); if (class_exists($class)) { - $opts = $config['alert']['transports'][$transport]; + $opts = Config::get("alert.transports.$transport"); $instance = new $class($transport_id); - $tmp = $instance->deliverAlert($obj, $opts); - if ($tmp) { - $status = 'ok'; + $result = $instance->deliverAlert($obj, $opts); + if ($result === true) { + $response['status'] = 'ok'; + } else { + $response['message'] = $result; } } header('Content-type: application/json'); -echo _json_encode(array('status' => $status)); +echo json_encode($response); diff --git a/html/includes/print-alert-transports.php b/html/includes/print-alert-transports.php index ec6c3ae4d3..b4f1d0ebb3 100644 --- a/html/includes/print-alert-transports.php +++ b/html/includes/print-alert-transports.php @@ -47,9 +47,9 @@ foreach (dbFetchRows($query) as $transport) { } else { echo "No"; } - + echo ""; - + // Iterate through transport config template to display config details $class = 'LibreNMS\\Alert\\Transport\\'.ucfirst($transport['type']); if (!method_exists($class, 'configTemplate')) { @@ -58,10 +58,10 @@ foreach (dbFetchRows($query) as $transport) { } $tmp = call_user_func($class.'::configTemplate'); $transport_config = json_decode($transport['config'], true); - + foreach ($tmp['config'] as $item) { $val = $transport_config[$item['name']]; - + // Match value to key name for select inputs if ($item['type'] == 'select') { $val = array_search($val, $item['options']); @@ -144,10 +144,10 @@ foreach (dbFetchRows($query) as $group) { data: { type: "test-transport", transport_id: transport_id }, dataType: "json", success: function(data){ - if (data.status == 'ok') { + if (data.status === 'ok') { toastr.success('Test to ' + transport + ' ok'); } else { - toastr.error('Test to ' + transport + ' failed'); + toastr.error('Test to ' + transport + ' failed
' + data.message); } }, error: function(){ @@ -160,4 +160,4 @@ foreach (dbFetchRows($query) as $group) { trigger: 'hover', placement: 'top' }); - \ No newline at end of file + diff --git a/html/pages/settings/alerting.inc.php b/html/pages/settings/alerting.inc.php index 6c5d5b5e3e..0d4d36015b 100644 --- a/html/pages/settings/alerting.inc.php +++ b/html/pages/settings/alerting.inc.php @@ -1726,10 +1726,10 @@ echo ' data: { type: "test-transport", transport: transport }, dataType: "json", success: function(data){ - if (data.status == 'ok') { + if (data.status === 'ok') { toastr.success('Test to ' + transport + ' ok'); } else { - toastr.error('Test to ' + transport + ' failed'); + toastr.error('Test to ' + transport + ' failed
' + data.message); } }, error: function(){ diff --git a/includes/functions.php b/includes/functions.php index 91a8f2fbd8..4919a8d130 100644 --- a/includes/functions.php +++ b/includes/functions.php @@ -1100,6 +1100,8 @@ function send_mail($emails, $subject, $message, $html = false) } return $mail->send() ? true : $mail->ErrorInfo; } + + return "No contacts found"; } function formatCiscoHardware(&$device, $short = false) diff --git a/includes/init.php b/includes/init.php index 2511ebe2f3..b21c9a4089 100644 --- a/includes/init.php +++ b/includes/init.php @@ -90,13 +90,6 @@ if (module_selected('polling', $init_modules)) { if (module_selected('alerts', $init_modules)) { require_once $install_dir . '/includes/device-groups.inc.php'; require_once $install_dir . '/includes/alerts.inc.php'; - if (module_selected('alerts-cli', $init_modules)) { - // Boot Laravel - we only use it in alerting at present - require $install_dir . '/bootstrap/autoload.php'; - $app = require_once $install_dir . '/bootstrap/app.php'; - $kernel = $app->make(Illuminate\Contracts\Console\Kernel::class); - $kernel->bootstrap(); - } } // Display config.php errors instead of http 500 diff --git a/scripts/test-alert.php b/scripts/test-alert.php index 7d507d9bdd..98aa5c7c57 100755 --- a/scripts/test-alert.php +++ b/scripts/test-alert.php @@ -1,12 +1,12 @@ #!/usr/bin/env php