From 0b8b97bb68e9c182c67e5c9126ce6bfb3f79926d Mon Sep 17 00:00:00 2001 From: Tony Murray Date: Wed, 6 Oct 2021 07:29:47 -0500 Subject: [PATCH] Push Notifications (Mobile and PC) (#13277) * Update manifest and add service worker cleanup icons a bit * Push notifications WIP * navigate working * cleanup * acknowledge wired up * Set VAPID keys on composer install * Component to control notification permissions. * Allow all user option to validate * Enable on browser load if transport exists. * Check for transport before showing user permissions translations * Documentation * style fixes * access via the attribute model * fix alerting test * update schema * cleanup subscription on disable * non-configurable db and table for webpush subscriptions (respect system connection) * revert AlertTransport change hopefully phpstan can figure it out * phpstan fixes * Support custom details display * Match transport names to brand's preferred display * less duplicate id errors * Tests are done in Laravel code now so remove legacy function usage... could be better, but ok * Style fixes * Style fixes 2 * Fix alert test * Doc updates requires HTTPS and GMP * unregister subscription when permission is set to denied * cleanup after user deletion * delete the right thing * fix whitespace * update install docs to include php-gmp * suggest ext-gmp * update javascript * Update functions.php Co-authored-by: Jellyfrog --- LibreNMS/Alert/RunAlerts.php | 2 +- LibreNMS/Alert/Transport.php | 91 ++- LibreNMS/Alert/Transport/Alerta.php | 3 +- LibreNMS/Alert/Transport/Alertmanager.php | 5 +- LibreNMS/Alert/Transport/Api.php | 5 +- LibreNMS/Alert/Transport/Boxcar.php | 3 +- LibreNMS/Alert/Transport/Browserpush.php | 89 +++ LibreNMS/Alert/Transport/Ciscospark.php | 5 +- LibreNMS/Alert/Transport/Clickatell.php | 3 +- LibreNMS/Alert/Transport/Discord.php | 5 +- LibreNMS/Alert/Transport/Elasticsearch.php | 6 +- LibreNMS/Alert/Transport/Gitlab.php | 7 +- LibreNMS/Alert/Transport/Googlechat.php | 4 + LibreNMS/Alert/Transport/Hipchat.php | 5 +- LibreNMS/Alert/Transport/Hue.php | 6 +- LibreNMS/Alert/Transport/Irc.php | 2 + LibreNMS/Alert/Transport/Jira.php | 7 +- LibreNMS/Alert/Transport/Kayako.php | 2 + LibreNMS/Alert/Transport/Linenotify.php | 4 + LibreNMS/Alert/Transport/Mail.php | 7 +- LibreNMS/Alert/Transport/Matrix.php | 3 +- LibreNMS/Alert/Transport/Mattermost.php | 7 +- LibreNMS/Alert/Transport/Msteams.php | 5 +- LibreNMS/Alert/Transport/Opsgenie.php | 3 +- LibreNMS/Alert/Transport/Osticket.php | 5 +- LibreNMS/Alert/Transport/Pagerduty.php | 4 +- LibreNMS/Alert/Transport/Playsms.php | 5 +- LibreNMS/Alert/Transport/Pushbullet.php | 3 +- LibreNMS/Alert/Transport/Pushover.php | 3 +- LibreNMS/Alert/Transport/Rocket.php | 3 +- LibreNMS/Alert/Transport/Sensu.php | 21 +- LibreNMS/Alert/Transport/Signalwire.php | 5 +- LibreNMS/Alert/Transport/Slack.php | 3 +- LibreNMS/Alert/Transport/Smseagle.php | 5 +- LibreNMS/Alert/Transport/Smsfeedback.php | 5 +- LibreNMS/Alert/Transport/Telegram.php | 3 +- LibreNMS/Alert/Transport/Twilio.php | 3 +- LibreNMS/Alert/Transport/Ukfastpss.php | 5 +- LibreNMS/Alert/Transport/Victorops.php | 5 +- LibreNMS/ComposerHelper.php | 5 + LibreNMS/Interfaces/Alert/Transport.php | 12 + LibreNMS/Util/Mail.php | 127 ++++ LibreNMS/Util/Proxy.php | 84 +++ app/Http/Controllers/AlertController.php | 58 ++ .../Controllers/AlertTransportController.php | 55 ++ .../PushNotificationController.php | 55 ++ app/Http/ViewComposers/MenuComposer.php | 2 + app/Models/Alert.php | 3 + app/Models/AlertTransport.php | 45 ++ app/Models/User.php | 28 +- app/Notifications/AlertNotification.php | 78 +++ app/Observers/UserObserver.php | 22 + app/Providers/AppServiceProvider.php | 1 + .../NotificationSubscriptionStatus.php | 33 + composer.json | 4 +- composer.lock | 633 +++++++++++++++++- config/debugbar.php | 1 + config/webpush.php | 48 ++ daily.php | 2 +- ...094310_create_push_subscriptions_table.php | 37 + doc/Alerting/Transports.md | 9 + doc/Alerting/index.md | 1 + doc/Installation/Install-LibreNMS.md | 10 +- html/images/android-chrome-144x144.png | Bin 0 -> 1996 bytes html/images/android-chrome-192x192.png | Bin 4486 -> 2430 bytes html/images/android-chrome-512x512.png | Bin 11887 -> 6000 bytes html/images/apple-touch-icon.png | Bin 4301 -> 2277 bytes html/images/manifest.json | 40 +- html/images/mstile-144x144.png | Bin 3259 -> 2056 bytes html/images/mstile-150x150.png | Bin 3426 -> 2078 bytes html/images/mstile-310x150.png | Bin 3970 -> 2289 bytes html/images/mstile-310x310.png | Bin 7325 -> 3812 bytes html/images/mstile-70x70.png | Bin 2501 -> 1549 bytes html/images/no-48.png | Bin 2482 -> 0 bytes html/images/pd_connect_button.png | Bin 3930 -> 0 bytes html/images/textbg.png | Bin 107 -> 0 bytes html/js/app.js | 2 +- html/js/librenms.js | 5 + html/js/register-service-worker.js | 79 +++ html/mix-manifest.json | 2 +- html/service-worker.js | 70 ++ includes/functions.php | 102 +-- includes/html/common/alerts.inc.php | 4 + includes/html/error-no-perm.inc.php | 4 +- includes/html/forms/ack-alert.inc.php | 90 --- includes/html/forms/alert-transports.inc.php | 12 +- includes/html/forms/test-transport.inc.php | 67 -- includes/html/modal/alert_ack.inc.php | 6 +- .../html/modal/edit_alert_transport.inc.php | 16 +- includes/html/pages/alerts.inc.php | 2 - includes/html/print-alert-rules.php | 20 - includes/html/print-alert-transports.php | 55 +- includes/html/table/alerts.inc.php | 5 + misc/db_schema.yaml | 16 + resources/lang/en/components.php | 12 + resources/views/alerts/modals/ack.blade.php | 4 +- ...notification-subscription-status.blade.php | 64 ++ resources/views/layouts/librenmsv1.blade.php | 15 +- resources/views/layouts/menu.blade.php | 10 + resources/views/user/preferences.blade.php | 4 + routes/web.php | 11 + tests/AlertingTest.php | 14 +- 102 files changed, 1993 insertions(+), 448 deletions(-) create mode 100644 LibreNMS/Alert/Transport/Browserpush.php create mode 100644 LibreNMS/Util/Mail.php create mode 100644 LibreNMS/Util/Proxy.php create mode 100644 app/Http/Controllers/AlertController.php create mode 100644 app/Http/Controllers/AlertTransportController.php create mode 100644 app/Http/Controllers/PushNotificationController.php create mode 100644 app/Models/AlertTransport.php create mode 100644 app/Notifications/AlertNotification.php create mode 100644 app/Observers/UserObserver.php create mode 100644 app/View/Components/NotificationSubscriptionStatus.php create mode 100644 config/webpush.php create mode 100644 database/migrations/2021_09_07_094310_create_push_subscriptions_table.php create mode 100644 html/images/android-chrome-144x144.png delete mode 100644 html/images/no-48.png delete mode 100644 html/images/pd_connect_button.png delete mode 100644 html/images/textbg.png create mode 100644 html/js/register-service-worker.js create mode 100644 html/service-worker.js delete mode 100644 includes/html/forms/ack-alert.inc.php delete mode 100644 includes/html/forms/test-transport.inc.php create mode 100644 resources/lang/en/components.php create mode 100644 resources/views/components/notification-subscription-status.blade.php diff --git a/LibreNMS/Alert/RunAlerts.php b/LibreNMS/Alert/RunAlerts.php index 88d3dc866a..6e159fd08b 100644 --- a/LibreNMS/Alert/RunAlerts.php +++ b/LibreNMS/Alert/RunAlerts.php @@ -521,7 +521,7 @@ class RunAlerts } foreach ($transport_maps as $item) { - $class = 'LibreNMS\\Alert\\Transport\\' . ucfirst($item['transport_type']); + $class = Transport::getClass($item['transport_type']); if (class_exists($class)) { //FIXME remove Deprecated transport $transport_title = "Transport {$item['transport_type']}"; diff --git a/LibreNMS/Alert/Transport.php b/LibreNMS/Alert/Transport.php index 565b5b73b3..c5edc3645f 100644 --- a/LibreNMS/Alert/Transport.php +++ b/LibreNMS/Alert/Transport.php @@ -2,6 +2,8 @@ namespace LibreNMS\Alert; +use App\Models\AlertTransport; +use Illuminate\Database\Eloquent\ModelNotFoundException; use Illuminate\Support\Str; use LibreNMS\Config; use LibreNMS\Enum\AlertState; @@ -10,20 +12,53 @@ use LibreNMS\Interfaces\Alert\Transport as TransportInterface; abstract class Transport implements TransportInterface { protected $config; + /** + * @var string + */ + protected $name; + + public static function make(string $type): TransportInterface + { + $class = self::getClass($type); + + return new $class(); + } /** * Transport constructor. * - * @param null $transport_id + * @param null $transport */ - public function __construct($transport_id = null) + public function __construct($transport = null) { - if (! empty($transport_id)) { - $sql = 'SELECT `transport_config` FROM `alert_transports` WHERE `transport_id`=?'; - $this->config = json_decode(dbFetchCell($sql, [$transport_id]), true); + if (! empty($transport)) { + if ($transport instanceof AlertTransport) { + $this->config = $transport->transport_config; + } else { + try { + $model = \App\Models\AlertTransport::findOrFail($transport); /** @var AlertTransport $model */ + $this->config = $model->transport_config; + } catch (ModelNotFoundException $e) { + $this->config = []; + } + } } } + /** + * @return string The display name of this transport + */ + public function name(): string + { + if ($this->name !== null) { + return $this->name; + } + + $path = explode('\\', get_called_class()); + + return array_pop($path); + } + /** * Helper function to parse free form text box defined in ini style to key value pairs * @@ -61,4 +96,50 @@ abstract class Transport implements TransportInterface return isset($colors[$state]) ? $colors[$state] : '#337AB7'; } + + /** + * Display the configuration details of this alert transport + * + * @return string + */ + public function displayDetails(): string + { + $output = ''; + + // Iterate through transport config template to display config details + $config = static::configTemplate(); + foreach ($config['config'] as $item) { + if ($item['type'] == 'oauth') { + continue; + } + + $val = $this->config[$item['name']]; + if ($item['type'] == 'password') { + $val = '••••••••'; + } elseif ($item['type'] == 'select') { + // Match value to key name for select inputs + $val = array_search($val, $item['options']); + } + + $output .= $item['title'] . ': ' . $val . PHP_EOL; + } + + return $output; + } + + /** + * Get the alert transport class from transport type. + * + * @param string $type + * @return string + */ + public static function getClass(string $type): string + { + return 'LibreNMS\\Alert\\Transport\\' . ucfirst($type); + } + + protected function isHtmlContent($content): bool + { + return $content !== strip_tags($content); + } } diff --git a/LibreNMS/Alert/Transport/Alerta.php b/LibreNMS/Alert/Transport/Alerta.php index f003d198f6..191a6b3f99 100644 --- a/LibreNMS/Alert/Transport/Alerta.php +++ b/LibreNMS/Alert/Transport/Alerta.php @@ -18,6 +18,7 @@ namespace LibreNMS\Alert\Transport; use LibreNMS\Alert\Transport; use LibreNMS\Config; use LibreNMS\Enum\AlertState; +use LibreNMS\Util\Proxy; class Alerta extends Transport { @@ -63,7 +64,7 @@ class Alerta extends Transport 'type' => $obj['title'], ]; $alert_message = json_encode($data); - set_curl_proxy($curl); + Proxy::applyToCurl($curl); $headers = ['Content-Type: application/json', 'Authorization: Key ' . $opts['apikey']]; curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); curl_setopt($curl, CURLOPT_URL, $host); diff --git a/LibreNMS/Alert/Transport/Alertmanager.php b/LibreNMS/Alert/Transport/Alertmanager.php index 50e7a7cf2d..abd0241bb6 100644 --- a/LibreNMS/Alert/Transport/Alertmanager.php +++ b/LibreNMS/Alert/Transport/Alertmanager.php @@ -25,9 +25,12 @@ namespace LibreNMS\Alert\Transport; use LibreNMS\Alert\Transport; use LibreNMS\Config; use LibreNMS\Enum\AlertState; +use LibreNMS\Util\Proxy; class Alertmanager extends Transport { + protected $name = 'Alert Manager'; + public function deliverAlert($obj, $opts) { $alertmanager_opts = $this->parseUserOptions($this->config['alertmanager-options']); @@ -72,7 +75,7 @@ class Alertmanager extends Transport public static function postAlerts($url, $data) { $curl = curl_init(); - set_curl_proxy($curl); + Proxy::applyToCurl($curl); curl_setopt($curl, CURLOPT_HTTPHEADER, ['Content-Type: application/json']); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_TIMEOUT, 5); diff --git a/LibreNMS/Alert/Transport/Api.php b/LibreNMS/Alert/Transport/Api.php index ac6cef843d..b12c92fb9b 100644 --- a/LibreNMS/Alert/Transport/Api.php +++ b/LibreNMS/Alert/Transport/Api.php @@ -25,9 +25,12 @@ namespace LibreNMS\Alert\Transport; use LibreNMS\Alert\Transport; +use LibreNMS\Util\Proxy; class Api extends Transport { + protected $name = 'API'; + public function deliverAlert($obj, $opts) { $url = $this->config['api-url']; @@ -70,7 +73,7 @@ class Api extends Transport } $client = new \GuzzleHttp\Client(); - $request_opts['proxy'] = get_guzzle_proxy(); + $request_opts['proxy'] = Proxy::forGuzzle(); if (isset($auth) && ! empty($auth[0])) { $request_opts['auth'] = $auth; } diff --git a/LibreNMS/Alert/Transport/Boxcar.php b/LibreNMS/Alert/Transport/Boxcar.php index b78eccd6d7..287d11c489 100644 --- a/LibreNMS/Alert/Transport/Boxcar.php +++ b/LibreNMS/Alert/Transport/Boxcar.php @@ -40,6 +40,7 @@ namespace LibreNMS\Alert\Transport; use LibreNMS\Alert\Transport; use LibreNMS\Config; use LibreNMS\Enum\AlertState; +use LibreNMS\Util\Proxy; class Boxcar extends Transport { @@ -101,7 +102,7 @@ class Boxcar extends Transport } $data['notification[long_message]'] = $message_text; $curl = curl_init(); - set_curl_proxy($curl); + Proxy::applyToCurl($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); diff --git a/LibreNMS/Alert/Transport/Browserpush.php b/LibreNMS/Alert/Transport/Browserpush.php new file mode 100644 index 0000000000..9bc69a9180 --- /dev/null +++ b/LibreNMS/Alert/Transport/Browserpush.php @@ -0,0 +1,89 @@ +. + * + * @package LibreNMS + * @link http://librenms.org + * @copyright 2021 Tony Murray + * @author Tony Murray + */ + +namespace LibreNMS\Alert\Transport; + +use App\Models\User; +use App\Notifications\AlertNotification; +use LibreNMS\Alert\Transport; +use Notification; + +class Browserpush extends Transport +{ + protected $name = 'Browser Push'; + + public function deliverAlert($alert_data, $opts) + { + $users = User::when($this->config['user'] ?? 0, function ($query, $user_id) { + return $query->where('user_id', $user_id); + })->get(); + + Notification::send($users, new AlertNotification( + $alert_data['alert_id'], + $alert_data['title'], + $alert_data['msg'], + )); + + return true; + } + + public static function configTemplate() + { + $users = [__('All Users') => 0]; + foreach (User::get(['user_id', 'username', 'realname']) as $user) { + $users[$user->realname ?: $user->username] = $user->user_id; + } + + return [ + 'config' => [ + [ + 'title' => 'User', + 'name' => 'user', + 'descr' => 'LibreNMS User', + 'type' => 'select', + 'options' => $users, + ], + ], + 'validation' => [ + 'user' => 'required|zero_or_exists:users,user_id', + ], + ]; + } + + public function displayDetails(): string + { + if ($this->config['user'] == 0) { + $count = \DB::table('push_subscriptions')->count(); + + return "All users: $count subscriptions"; + } elseif ($user = User::find($this->config['user'])) { + $count = $user->pushSubscriptions()->count(); + + return "User: $user->username ($count subscriptions)"; + } + + return 'User not found'; + } +} diff --git a/LibreNMS/Alert/Transport/Ciscospark.php b/LibreNMS/Alert/Transport/Ciscospark.php index fd3e617bf5..983cabc595 100644 --- a/LibreNMS/Alert/Transport/Ciscospark.php +++ b/LibreNMS/Alert/Transport/Ciscospark.php @@ -13,9 +13,12 @@ namespace LibreNMS\Alert\Transport; use LibreNMS\Alert\Transport; +use LibreNMS\Util\Proxy; class Ciscospark extends Transport { + protected $name = 'Cisco Spark'; + public function deliverAlert($obj, $opts) { if (empty($this->config)) { @@ -57,7 +60,7 @@ class Ciscospark extends Transport $data[$akey] = $text; $curl = curl_init(); - set_curl_proxy($curl); + Proxy::applyToCurl($curl); curl_setopt($curl, CURLOPT_URL, 'https://api.ciscospark.com/v1/messages'); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_HTTPHEADER, [ diff --git a/LibreNMS/Alert/Transport/Clickatell.php b/LibreNMS/Alert/Transport/Clickatell.php index 90dd6c7fe8..d4b2237808 100644 --- a/LibreNMS/Alert/Transport/Clickatell.php +++ b/LibreNMS/Alert/Transport/Clickatell.php @@ -24,6 +24,7 @@ namespace LibreNMS\Alert\Transport; use LibreNMS\Alert\Transport; +use LibreNMS\Util\Proxy; class Clickatell extends Transport { @@ -40,7 +41,7 @@ class Clickatell extends Transport $url = 'https://platform.clickatell.com/messages/http/send?apiKey=' . $opts['token'] . '&to=' . implode(',', $opts['to']) . '&content=' . urlencode($obj['title']); $curl = curl_init($url); - set_curl_proxy($curl); + Proxy::applyToCurl($curl); curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'GET'); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); diff --git a/LibreNMS/Alert/Transport/Discord.php b/LibreNMS/Alert/Transport/Discord.php index 2e54e8888c..cf55bef2e1 100644 --- a/LibreNMS/Alert/Transport/Discord.php +++ b/LibreNMS/Alert/Transport/Discord.php @@ -29,10 +29,11 @@ namespace LibreNMS\Alert\Transport; use LibreNMS\Alert\Transport; +use LibreNMS\Util\Proxy; class Discord extends Transport { - const ALERT_FIELDS_TO_DISCORD_FIELDS = [ + public const ALERT_FIELDS_TO_DISCORD_FIELDS = [ 'timestamp' => 'Timestamp', 'severity' => 'Severity', 'hostname' => 'Hostname', @@ -80,7 +81,7 @@ class Discord extends Transport $alert_message = json_encode($data); curl_setopt($curl, CURLOPT_HTTPHEADER, ['Content-Type: application/json']); - set_curl_proxy($curl); + Proxy::applyToCurl($curl); curl_setopt($curl, CURLOPT_URL, $host); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_POST, true); diff --git a/LibreNMS/Alert/Transport/Elasticsearch.php b/LibreNMS/Alert/Transport/Elasticsearch.php index b180dd6e2b..3e3a9a5ab6 100644 --- a/LibreNMS/Alert/Transport/Elasticsearch.php +++ b/LibreNMS/Alert/Transport/Elasticsearch.php @@ -19,6 +19,7 @@ namespace LibreNMS\Alert\Transport; use LibreNMS\Alert\Transport; use LibreNMS\Enum\AlertState; +use LibreNMS\Util\Proxy; class Elasticsearch extends Transport { @@ -41,7 +42,6 @@ class Elasticsearch extends Transport $index = strftime('librenms-%Y.%m.%d'); $type = 'alert'; $severity = $obj['severity']; - $device = device_by_id_cache($obj['device_id']); // for event logging if (! empty($opts['es_host'])) { if (preg_match('/[a-zA-Z]/', $opts['es_host'])) { @@ -168,7 +168,7 @@ class Elasticsearch extends Transport $alert_message = json_encode($data); curl_setopt($curl, CURLOPT_HTTPHEADER, ['Content-Type: application/json']); if ($opts['es_proxy'] === true) { - set_curl_proxy($curl); + Proxy::applyToCurl($curl); } curl_setopt($curl, CURLOPT_URL, $host); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); @@ -186,7 +186,7 @@ class Elasticsearch extends Transport $alert_message = json_encode($data); curl_setopt($curl, CURLOPT_HTTPHEADER, ['Content-Type: application/json']); if ($opts['es_proxy'] === true) { - set_curl_proxy($curl); + Proxy::applyToCurl($curl); } curl_setopt($curl, CURLOPT_URL, $host); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); diff --git a/LibreNMS/Alert/Transport/Gitlab.php b/LibreNMS/Alert/Transport/Gitlab.php index e05191c65c..858432131a 100644 --- a/LibreNMS/Alert/Transport/Gitlab.php +++ b/LibreNMS/Alert/Transport/Gitlab.php @@ -25,6 +25,7 @@ namespace LibreNMS\Alert\Transport; use LibreNMS\Alert\Transport; use LibreNMS\Enum\AlertState; +use LibreNMS\Util\Proxy; class Gitlab extends Transport { @@ -43,8 +44,6 @@ class Gitlab extends Transport { // Don't create tickets for resolutions if ($obj['state'] != AlertState::CLEAR) { - $device = device_by_id_cache($obj['device_id']); // for event logging - $project_id = $opts['project-id']; $project_key = $opts['key']; $details = 'Librenms alert for: ' . $obj['hostname']; @@ -60,7 +59,7 @@ class Gitlab extends Transport $postdata = ['fields' => $data]; $datastring = json_encode($postdata); - set_curl_proxy($curl); + Proxy::applyToCurl($curl); $headers = ['Accept: application/json', 'Content-Type: application/json', 'PRIVATE-TOKEN: ' . $project_key]; @@ -75,7 +74,7 @@ class Gitlab extends Transport $code = curl_getinfo($curl, CURLINFO_HTTP_CODE); if ($code == 200) { $gitlabout = json_decode($ret, true); - d_echo('Created GitLab issue ' . $gitlabout['key'] . ' for ' . $device); + d_echo('Created GitLab issue ' . $gitlabout['key'] . ' for ' . $obj['hostname']); return true; } else { diff --git a/LibreNMS/Alert/Transport/Googlechat.php b/LibreNMS/Alert/Transport/Googlechat.php index 597b27f90b..50648d4b77 100755 --- a/LibreNMS/Alert/Transport/Googlechat.php +++ b/LibreNMS/Alert/Transport/Googlechat.php @@ -24,10 +24,13 @@ namespace LibreNMS\Alert\Transport; use LibreNMS\Alert\Transport; +use LibreNMS\Util\Proxy; use Log; class Googlechat extends Transport { + protected $name = 'Google Chat'; + public function deliverAlert($obj, $opts) { $googlechat_conf['webhookurl'] = $this->config['googlechat-webhook']; @@ -43,6 +46,7 @@ class Googlechat extends Transport // Create a new cURL resource $ch = curl_init($data['webhookurl']); + Proxy::applyToCurl($ch); // Attach encoded JSON string to the POST fields curl_setopt($ch, CURLOPT_POSTFIELDS, $payload); diff --git a/LibreNMS/Alert/Transport/Hipchat.php b/LibreNMS/Alert/Transport/Hipchat.php index 1a722017ee..9126d59a85 100644 --- a/LibreNMS/Alert/Transport/Hipchat.php +++ b/LibreNMS/Alert/Transport/Hipchat.php @@ -25,9 +25,12 @@ namespace LibreNMS\Alert\Transport; use LibreNMS\Alert\Transport; +use LibreNMS\Util\Proxy; class Hipchat extends Transport { + protected $name = 'HipChat'; + public function deliverAlert($obj, $opts) { $hipchat_opts = $this->parseUserOptions($this->config['hipchat-options']); @@ -85,7 +88,7 @@ class Hipchat extends Transport $data[] = 'message_format=' . urlencode($option['message_format']); $data = implode('&', $data); - set_curl_proxy($curl); + Proxy::applyToCurl($curl); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_POST, true); diff --git a/LibreNMS/Alert/Transport/Hue.php b/LibreNMS/Alert/Transport/Hue.php index 9163b43f20..9b9a5a0ee7 100644 --- a/LibreNMS/Alert/Transport/Hue.php +++ b/LibreNMS/Alert/Transport/Hue.php @@ -26,6 +26,7 @@ namespace LibreNMS\Alert\Transport; use LibreNMS\Alert\Transport; use LibreNMS\Enum\AlertState; +use LibreNMS\Util\Proxy; /** * The Hue API currently is fairly limited for alerts. @@ -51,7 +52,6 @@ class Hue extends Transport if ($obj['state'] == AlertState::RECOVERED) { return true; } else { - $device = device_by_id_cache($obj['device_id']); // for event logging $hue_user = $opts['user']; $url = $opts['bridge'] . "/api/$hue_user/groups/0/action"; $curl = curl_init(); @@ -59,7 +59,7 @@ class Hue extends Transport $data = ['alert' => $duration]; $datastring = json_encode($data); - set_curl_proxy($curl); + Proxy::applyToCurl($curl); $headers = ['Accept: application/json', 'Content-Type: application/json']; @@ -73,7 +73,7 @@ class Hue extends Transport $ret = curl_exec($curl); $code = curl_getinfo($curl, CURLINFO_HTTP_CODE); if ($code == 200) { - d_echo('Sent alert to Phillips Hue Bridge ' . $opts['host'] . ' for ' . $device); + d_echo('Sent alert to Phillips Hue Bridge ' . $opts['host'] . ' for ' . $obj['hostname']); return true; } else { diff --git a/LibreNMS/Alert/Transport/Irc.php b/LibreNMS/Alert/Transport/Irc.php index d16096dbce..63b9bd1606 100644 --- a/LibreNMS/Alert/Transport/Irc.php +++ b/LibreNMS/Alert/Transport/Irc.php @@ -28,6 +28,8 @@ use LibreNMS\Config; class Irc extends Transport { + protected $name = 'IRC'; + public function deliverAlert($obj, $opts) { return $this->contactIrc($obj, $opts); diff --git a/LibreNMS/Alert/Transport/Jira.php b/LibreNMS/Alert/Transport/Jira.php index ceb789b47e..5010c96b91 100644 --- a/LibreNMS/Alert/Transport/Jira.php +++ b/LibreNMS/Alert/Transport/Jira.php @@ -24,6 +24,7 @@ namespace LibreNMS\Alert\Transport; use LibreNMS\Alert\Transport; +use LibreNMS\Util\Proxy; class Jira extends Transport { @@ -47,8 +48,6 @@ class Jira extends Transport return true; } - $device = device_by_id_cache($obj['device_id']); // for event logging - $username = $opts['username']; $password = $opts['password']; $prjkey = $opts['prjkey']; @@ -65,7 +64,7 @@ class Jira extends Transport $postdata = ['fields' => $data]; $datastring = json_encode($postdata); - set_curl_proxy($curl); + Proxy::applyToCurl($curl); $headers = ['Accept: application/json', 'Content-Type: application/json']; @@ -81,7 +80,7 @@ class Jira extends Transport $code = curl_getinfo($curl, CURLINFO_HTTP_CODE); if ($code == 200) { $jiraout = json_decode($ret, true); - d_echo('Created jira issue ' . $jiraout['key'] . ' for ' . $device); + d_echo('Created jira issue ' . $jiraout['key'] . ' for ' . $obj['hostname']); return true; } else { diff --git a/LibreNMS/Alert/Transport/Kayako.php b/LibreNMS/Alert/Transport/Kayako.php index 6704d5e50c..bf2b2732c2 100644 --- a/LibreNMS/Alert/Transport/Kayako.php +++ b/LibreNMS/Alert/Transport/Kayako.php @@ -14,6 +14,7 @@ namespace LibreNMS\Alert\Transport; use LibreNMS\Alert\Transport; use LibreNMS\Config; +use LibreNMS\Util\Proxy; class Kayako extends Transport { @@ -60,6 +61,7 @@ class Kayako extends Transport $post_data = http_build_query($protocol, '', '&'); $curl = curl_init(); + Proxy::applyToCurl($curl); curl_setopt($curl, CURLOPT_POST, true); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); diff --git a/LibreNMS/Alert/Transport/Linenotify.php b/LibreNMS/Alert/Transport/Linenotify.php index a687f7f3b7..2014104ae3 100644 --- a/LibreNMS/Alert/Transport/Linenotify.php +++ b/LibreNMS/Alert/Transport/Linenotify.php @@ -6,9 +6,12 @@ namespace LibreNMS\Alert\Transport; use LibreNMS\Alert\Transport; +use LibreNMS\Util\Proxy; class Linenotify extends Transport { + protected $name = 'LINE Notify'; + public function deliverAlert($obj, $opts) { $opts['line-notify-access-token'] = $this->config['line-notify-access-token']; @@ -23,6 +26,7 @@ class Linenotify extends Transport $lineFields = ['message' => $obj['msg']]; $curl = curl_init(); + Proxy::applyToCurl($curl); curl_setopt($curl, CURLOPT_URL, $lineUrl); curl_setopt($curl, CURLOPT_HTTPHEADER, $lineHead); curl_setopt($curl, CURLOPT_NOBODY, false); diff --git a/LibreNMS/Alert/Transport/Mail.php b/LibreNMS/Alert/Transport/Mail.php index cf62138d16..324578fd17 100644 --- a/LibreNMS/Alert/Transport/Mail.php +++ b/LibreNMS/Alert/Transport/Mail.php @@ -46,7 +46,7 @@ class Mail extends Transport $msg = preg_replace("/(?$message, 'msgtype'=>'m.text']; $client = new \GuzzleHttp\Client(); - $request_opts['proxy'] = get_guzzle_proxy(); + $request_opts['proxy'] = Proxy::forGuzzle(); $request_opts['headers'] = $request_heads; $request_opts['body'] = json_encode($body); $res = $client->request('PUT', $host, $request_opts); diff --git a/LibreNMS/Alert/Transport/Mattermost.php b/LibreNMS/Alert/Transport/Mattermost.php index 99b1d1aed8..3cfbf3bb07 100755 --- a/LibreNMS/Alert/Transport/Mattermost.php +++ b/LibreNMS/Alert/Transport/Mattermost.php @@ -24,6 +24,7 @@ namespace LibreNMS\Alert\Transport; use LibreNMS\Alert\Transport; +use LibreNMS\Util\Proxy; class Mattermost extends Transport { @@ -62,9 +63,7 @@ class Mattermost extends Transport 'icon_url' => $api['icon'], ]; - $device = device_by_id_cache($obj['device_id']); - - set_curl_proxy($curl); + Proxy::applyToCurl($curl); $httpheaders = ['Accept: application/json', 'Content-Type: application/json']; $alert_payload = json_encode($data); @@ -82,7 +81,7 @@ class Mattermost extends Transport return 'HTTP Status code ' . $code; } else { - d_echo('Mattermost message sent for ' . $device); + d_echo('Mattermost message sent for ' . $obj['hostname']); return true; } diff --git a/LibreNMS/Alert/Transport/Msteams.php b/LibreNMS/Alert/Transport/Msteams.php index 19d14f9245..8f43ad8200 100644 --- a/LibreNMS/Alert/Transport/Msteams.php +++ b/LibreNMS/Alert/Transport/Msteams.php @@ -13,9 +13,12 @@ namespace LibreNMS\Alert\Transport; use LibreNMS\Alert\Transport; +use LibreNMS\Util\Proxy; class Msteams extends Transport { + protected $name = 'Microsoft Teams'; + public function deliverAlert($obj, $opts) { if (! empty($this->config)) { @@ -34,7 +37,7 @@ class Msteams extends Transport 'text' => strip_tags($obj['msg'], '