2018-07-21 13:34:59 -06:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace LibreNMS\Alert;
|
|
|
|
|
|
2020-04-17 17:37:56 -05:00
|
|
|
use Illuminate\Support\Str;
|
2019-12-18 11:08:32 +01:00
|
|
|
use LibreNMS\Config;
|
2020-05-24 04:14:36 +02:00
|
|
|
use LibreNMS\Enum\AlertState;
|
2020-09-21 14:54:51 +02:00
|
|
|
use LibreNMS\Interfaces\Alert\Transport as TransportInterface;
|
2018-07-21 13:34:59 -06:00
|
|
|
|
|
|
|
|
abstract class Transport implements TransportInterface
|
|
|
|
|
{
|
|
|
|
|
protected $config;
|
|
|
|
|
|
2018-08-17 22:38:00 +01:00
|
|
|
/**
|
|
|
|
|
* Transport constructor.
|
|
|
|
|
* @param null $transport_id
|
|
|
|
|
*/
|
2018-07-21 13:34:59 -06:00
|
|
|
public function __construct($transport_id = null)
|
|
|
|
|
{
|
2020-09-21 14:54:51 +02:00
|
|
|
if (! empty($transport_id)) {
|
2020-09-21 15:59:34 +02:00
|
|
|
$sql = 'SELECT `transport_config` FROM `alert_transports` WHERE `transport_id`=?';
|
2018-07-21 13:34:59 -06:00
|
|
|
$this->config = json_decode(dbFetchCell($sql, [$transport_id]), true);
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-03-27 07:18:02 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Helper function to parse free form text box defined in ini style to key value pairs
|
|
|
|
|
*
|
|
|
|
|
* @param string $input
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
protected function parseUserOptions($input)
|
|
|
|
|
{
|
|
|
|
|
$options = [];
|
|
|
|
|
foreach (explode(PHP_EOL, $input) as $option) {
|
2020-04-17 17:37:56 -05:00
|
|
|
if (Str::contains($option, '=')) {
|
2020-09-21 14:54:51 +02:00
|
|
|
[$k,$v] = explode('=', $option, 2);
|
2019-03-27 07:18:02 -05:00
|
|
|
$options[$k] = trim($v);
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-09-21 14:54:51 +02:00
|
|
|
|
2019-03-27 07:18:02 -05:00
|
|
|
return $options;
|
|
|
|
|
}
|
2019-12-17 22:27:08 +01:00
|
|
|
|
2020-09-21 14:54:51 +02:00
|
|
|
/**
|
2019-12-17 22:27:08 +01:00
|
|
|
* Get the hex color string for a particular state
|
2020-09-21 14:54:51 +02:00
|
|
|
* @param int $state State code from alert
|
2019-12-17 22:27:08 +01:00
|
|
|
* @return string Hex color, default to #337AB7 blue if state unrecognised
|
|
|
|
|
*/
|
|
|
|
|
public static function getColorForState($state)
|
|
|
|
|
{
|
2020-09-21 14:54:51 +02:00
|
|
|
$colors = [
|
2020-05-24 04:14:36 +02:00
|
|
|
AlertState::CLEAR => Config::get('alert_colour.ok'),
|
|
|
|
|
AlertState::ACTIVE => Config::get('alert_colour.bad'),
|
|
|
|
|
AlertState::ACKNOWLEDGED => Config::get('alert_colour.acknowledged'),
|
|
|
|
|
AlertState::WORSE => Config::get('alert_colour.worse'),
|
|
|
|
|
AlertState::BETTER => Config::get('alert_colour.better'),
|
2020-09-21 14:54:51 +02:00
|
|
|
];
|
2019-12-18 11:08:32 +01:00
|
|
|
|
2019-12-17 22:27:08 +01:00
|
|
|
return isset($colors[$state]) ? $colors[$state] : '#337AB7';
|
|
|
|
|
}
|
2018-07-21 13:34:59 -06:00
|
|
|
}
|