1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
|
<?php declare(strict_types = 0);
/*
** Zabbix
** Copyright (C) 2001-2023 Zabbix SIA
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
**/
class CSeverityHelper {
/**
* Get severity name by given state and configuration.
*
* @param int $severity
*
* @return string
*/
public static function getName(int $severity): string {
switch ($severity) {
case ZBX_SEVERITY_OK:
return _('OK');
case TRIGGER_SEVERITY_NOT_CLASSIFIED:
return _(CSettingsHelper::get(CSettingsHelper::SEVERITY_NAME_0));
case TRIGGER_SEVERITY_INFORMATION:
return _(CSettingsHelper::get(CSettingsHelper::SEVERITY_NAME_1));
case TRIGGER_SEVERITY_WARNING:
return _(CSettingsHelper::get(CSettingsHelper::SEVERITY_NAME_2));
case TRIGGER_SEVERITY_AVERAGE:
return _(CSettingsHelper::get(CSettingsHelper::SEVERITY_NAME_3));
case TRIGGER_SEVERITY_HIGH:
return _(CSettingsHelper::get(CSettingsHelper::SEVERITY_NAME_4));
case TRIGGER_SEVERITY_DISASTER:
return _(CSettingsHelper::get(CSettingsHelper::SEVERITY_NAME_5));
default:
return _('Unknown');
}
}
/**
* Get severity css style name.
*
* @param int|null $severity
* @param bool $type
*
* @return string|null
*/
public static function getStyle(?int $severity, bool $type = true): ?string {
if (!$type) {
return ZBX_STYLE_NORMAL_BG;
}
switch ($severity) {
case ZBX_SEVERITY_OK:
return ZBX_STYLE_NORMAL_BG;
case TRIGGER_SEVERITY_DISASTER:
return ZBX_STYLE_DISASTER_BG;
case TRIGGER_SEVERITY_HIGH:
return ZBX_STYLE_HIGH_BG;
case TRIGGER_SEVERITY_AVERAGE:
return ZBX_STYLE_AVERAGE_BG;
case TRIGGER_SEVERITY_WARNING:
return ZBX_STYLE_WARNING_BG;
case TRIGGER_SEVERITY_INFORMATION:
return ZBX_STYLE_INFO_BG;
case TRIGGER_SEVERITY_NOT_CLASSIFIED:
return ZBX_STYLE_NA_BG;
default:
return null;
}
}
/**
* Get severity status css style name.
*
* @param int $severity
*
* @return string|null
*/
public static function getStatusStyle(int $severity): ?string {
switch ($severity) {
case TRIGGER_SEVERITY_DISASTER:
return ZBX_STYLE_STATUS_DISASTER_BG;
case TRIGGER_SEVERITY_HIGH:
return ZBX_STYLE_STATUS_HIGH_BG;
case TRIGGER_SEVERITY_AVERAGE:
return ZBX_STYLE_STATUS_AVERAGE_BG;
case TRIGGER_SEVERITY_WARNING:
return ZBX_STYLE_STATUS_WARNING_BG;
case TRIGGER_SEVERITY_INFORMATION:
return ZBX_STYLE_STATUS_INFO_BG;
case TRIGGER_SEVERITY_NOT_CLASSIFIED:
return ZBX_STYLE_STATUS_NA_BG;
default:
return null;
}
}
/**
* Get severity color from configuration.
*
* @param int $severity
*
* @return string|null
*/
public static function getColor(int $severity): ?string {
switch ($severity) {
case TRIGGER_SEVERITY_DISASTER:
return CSettingsHelper::get(CSettingsHelper::SEVERITY_COLOR_5);
case TRIGGER_SEVERITY_HIGH:
return CSettingsHelper::get(CSettingsHelper::SEVERITY_COLOR_4);
case TRIGGER_SEVERITY_AVERAGE:
return CSettingsHelper::get(CSettingsHelper::SEVERITY_COLOR_3);
case TRIGGER_SEVERITY_WARNING:
return CSettingsHelper::get(CSettingsHelper::SEVERITY_COLOR_2);
case TRIGGER_SEVERITY_INFORMATION:
return CSettingsHelper::get(CSettingsHelper::SEVERITY_COLOR_1);
case TRIGGER_SEVERITY_NOT_CLASSIFIED:
default:
return CSettingsHelper::get(CSettingsHelper::SEVERITY_COLOR_0);
}
}
/**
* Generate array with severities options.
*
* @param int $min Minimal severity.
* @param int $max Maximum severity.
*
* @return array
*/
public static function getSeverities(int $min = TRIGGER_SEVERITY_NOT_CLASSIFIED,
int $max = TRIGGER_SEVERITY_COUNT - 1): array {
$severities = [];
foreach (range($min, $max) as $severity) {
$severities[] = [
'label' => self::getName($severity),
'value' => $severity,
'style' => self::getStyle($severity)
];
}
return $severities;
}
/**
* Returns HTML representation of severity cell containing severity name and color.
*
* @param int $severity Trigger, Event or Problem severity.
* @param array|string|null $text Trigger severity name.
* @param bool $force_normal True to return 'normal' class, false to return corresponding severity class.
* @param bool $return_as_div True to return severity cell as DIV element.
*
* @return CDiv|CCol
*/
public static function makeSeverityCell(int $severity, $text = null, bool $force_normal = false,
bool $return_as_div = false) {
if ($text === null) {
$text = CHtml::encode(self::getName($severity));
}
if ($force_normal) {
return new CCol($text);
}
$return = $return_as_div ? new CDiv($text) : new CCol($text);
return $return->addClass(self::getStyle($severity));
}
}
|