From f835198a45711a9a7ffd247ee95d88df347333ec Mon Sep 17 00:00:00 2001 From: SourceDoctor Date: Tue, 17 Dec 2019 22:27:08 +0100 Subject: [PATCH] Globalize getColorForState function (#10944) * globalize getColorForState function --- LibreNMS/Alert/Transport.php | 17 +++++++++++++++++ LibreNMS/Alert/Transport/Canopsis.php | 2 +- LibreNMS/Alert/Transport/Hipchat.php | 1 - LibreNMS/Alert/Transport/Kayako.php | 8 ++++---- LibreNMS/Alert/Transport/Msteams.php | 22 ++-------------------- LibreNMS/Alert/Transport/Twilio.php | 4 ++-- 6 files changed, 26 insertions(+), 28 deletions(-) diff --git a/LibreNMS/Alert/Transport.php b/LibreNMS/Alert/Transport.php index 6950e0a11b..aa84fd3756 100644 --- a/LibreNMS/Alert/Transport.php +++ b/LibreNMS/Alert/Transport.php @@ -37,4 +37,21 @@ abstract class Transport implements TransportInterface } return $options; } + + /** + * Get the hex color string for a particular state + * @param integer $state State code from alert + * @return string Hex color, default to #337AB7 blue if state unrecognised + */ + public static function getColorForState($state) + { + $colors = array( + 0 => '#00FF00', // OK - green + 1 => '#FF0000', // Bad - red + 2 => '#337AB7', // Acknowledged - blue + 3 => '#FF0000', // Worse - red + 4 => '#F0AD4E', // Better - yellow + ); + return isset($colors[$state]) ? $colors[$state] : '#337AB7'; + } } diff --git a/LibreNMS/Alert/Transport/Canopsis.php b/LibreNMS/Alert/Transport/Canopsis.php index 13a626fecc..afae6fbcda 100644 --- a/LibreNMS/Alert/Transport/Canopsis.php +++ b/LibreNMS/Alert/Transport/Canopsis.php @@ -79,7 +79,7 @@ class Canopsis extends Transport $conn->close(); return true; } - + public static function configTemplate() { return [ diff --git a/LibreNMS/Alert/Transport/Hipchat.php b/LibreNMS/Alert/Transport/Hipchat.php index ba81fe9ad2..05426db815 100644 --- a/LibreNMS/Alert/Transport/Hipchat.php +++ b/LibreNMS/Alert/Transport/Hipchat.php @@ -76,7 +76,6 @@ class Hipchat extends Transport } } - $data[] = "message=" . urlencode($obj["msg"]); if ($version == 1) { $data[] = "room_id=" . urlencode($option["room_id"]); diff --git a/LibreNMS/Alert/Transport/Kayako.php b/LibreNMS/Alert/Transport/Kayako.php index ac07a09a09..e81d14fc94 100644 --- a/LibreNMS/Alert/Transport/Kayako.php +++ b/LibreNMS/Alert/Transport/Kayako.php @@ -56,23 +56,23 @@ class Kayako extends Transport 'signature' => $signature ); $post_data = http_build_query($protocol, '', '&'); - + $curl = curl_init(); curl_setopt($curl, CURLOPT_POST, true); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data); curl_exec($curl); - + $code = curl_getinfo($curl, CURLINFO_HTTP_CODE); if ($code != 200) { var_dump("Kayako returned Error, retry later"); return false; } - + return true; } - + public static function configTemplate() { return [ diff --git a/LibreNMS/Alert/Transport/Msteams.php b/LibreNMS/Alert/Transport/Msteams.php index 7fd899b49d..01235a7eaf 100644 --- a/LibreNMS/Alert/Transport/Msteams.php +++ b/LibreNMS/Alert/Transport/Msteams.php @@ -20,14 +20,14 @@ class Msteams extends Transport if (!empty($this->config)) { $opts['url'] = $this->config['msteam-url']; } - + return $this->contactMsteams($obj, $opts); } public function contactMsteams($obj, $opts) { $url = $opts['url']; - + $data = array( 'title' => $obj['title'], 'themeColor' => self::getColorForState($obj['state']), @@ -51,24 +51,6 @@ class Msteams extends Transport } return true; } - - /** - * Get the hex color string for a particular state - * @param integer $state State code from alert - * @return string Hex color, default to #337AB7 blue if state unrecognised - */ - public static function getColorForState($state) - { - $colors = array( - 0 => '#00FF00', // OK - green - 1 => '#FF0000', // Bad - red - 2 => '#337AB7', // Acknowledged - blue - 3 => '#FF0000', // Worse - red - 4 => '#F0AD4E', // Better - yellow - ); - - return isset($colors[$state]) ? $colors[$state] : '#337AB7'; - } public static function configTemplate() { diff --git a/LibreNMS/Alert/Transport/Twilio.php b/LibreNMS/Alert/Transport/Twilio.php index 836c97d0a5..887d8afa31 100644 --- a/LibreNMS/Alert/Transport/Twilio.php +++ b/LibreNMS/Alert/Transport/Twilio.php @@ -27,7 +27,7 @@ class Twilio extends Transport $twilio_opts['to'] = $this->config['twilio-to']; return $this->contacttwilio($obj, $twilio_opts); } - + public static function contactTwilio($obj, $opts) { $params = [ @@ -57,7 +57,7 @@ class Twilio extends Transport curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); curl_setopt($curl, CURLOPT_USERPWD, $params["sid"]. ":" . $params["token"]); curl_setopt($curl, CURLOPT_POSTFIELDS, $post); - + curl_exec($curl); if (curl_getinfo($curl, CURLINFO_RESPONSE_CODE)) {