Files
librenms-librenms/LibreNMS/Alert/Transport.php
Tony Murray 2c77edf4d2 Better handling of some alerting errors (#13446)
* Better handling of some alerting errors

* Better error output

* Consolidate simple template parsing

* Fixes reported by phpstan (one was a bug, yay!)

* add back forgotten trim

* don't remove the template if there is no match

* Match previous behavior, which was inconsistent.

* use anonymous class for tests instead

* Oopsie, Stringable is PHP8+

* fix style
2021-10-29 22:12:20 -05:00

146 lines
4.0 KiB
PHP

<?php
namespace LibreNMS\Alert;
use App\Models\AlertTransport;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Support\Str;
use LibreNMS\Config;
use LibreNMS\Enum\AlertState;
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
*/
public function __construct($transport = null)
{
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
*
* @param string $input
* @return array
*/
protected function parseUserOptions(string $input): array
{
$options = [];
foreach (preg_split('/\\r\\n|\\r|\\n/', $input, -1, PREG_SPLIT_NO_EMPTY) as $option) {
if (Str::contains($option, '=')) {
[$k, $v] = explode('=', $option, 2);
$options[$k] = trim($v);
}
}
return $options;
}
/**
* Get the hex color string for a particular state
*
* @param int $state State code from alert
* @return string Hex color, default to #337AB7 blue if state unrecognised
*/
public static function getColorForState($state)
{
$colors = [
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'),
];
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 = '<b>&bull;&bull;&bull;&bull;&bull;&bull;&bull;&bull;</b>';
} 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);
}
}