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 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
|
<?php
/*
** Copyright (C) 2001-2025 Zabbix SIA
**
** This program is free software: you can redistribute it and/or modify it under the terms of
** the GNU Affero General Public License as published by the Free Software Foundation, version 3.
**
** 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 Affero General Public License for more details.
**
** You should have received a copy of the GNU Affero General Public License along with this program.
** If not, see <https://www.gnu.org/licenses/>.
**/
/**
* Check if user has read permissions for host groups.
*
* @param array $groupids
*
* @return bool
*/
function isReadableHostGroups(array $groupids) {
return count($groupids) == API::HostGroup()->get([
'countOutput' => true,
'groupids' => $groupids
]);
}
/**
* Check if user has read permissions for template groups.
*
* @param array $groupids
*
* @return bool
*/
function isReadableTemplateGroups(array $groupids) {
return count($groupids) == API::TemplateGroup()->get([
'countOutput' => true,
'groupids' => $groupids
]);
}
/**
* Check if user has write permissions for host groups.
*
* @param array $groupids
*
* @return bool
*/
function isWritableHostGroups(array $groupids) {
return count($groupids) == API::HostGroup()->get([
'countOutput' => true,
'groupids' => $groupids,
'editable' => true
]);
}
/**
* Get sub-groups of selected host groups or template groups.
*
* @param array $groupids
* @param array $ms_groups [OUT] The list of groups for multiselect.
* @param string $context Context of hosts or templates.
*
* @return array
*/
function getSubGroups(array $groupids, ?array &$ms_groups = null, string $context = 'host', array $options = []) {
$entity = $context === 'host' ? API::HostGroup() : API::TemplateGroup();
$db_groups = $groupids
? $entity->get([
'output' => ['groupid', 'name'],
'groupids' => $groupids,
'preservekeys' => true
] + $options)
: [];
if ($ms_groups !== null) {
$ms_groups = CArrayHelper::renameObjectsKeys($db_groups, ['groupid' => 'id']);
}
$db_groups_names = [];
foreach ($db_groups as $db_group) {
$db_groups_names[] = $db_group['name'].'/';
}
if ($db_groups_names) {
$db_groups += $entity->get([
'output' => ['groupid'],
'search' => ['name' => $db_groups_names],
'searchByAny' => true,
'startSearch' => true,
'preservekeys' => true
] + $options);
}
return array_keys($db_groups);
}
/**
* Get sub-groups of selected template groups.
*
* @param array $template_groupids
* @param array $ms_template_groups [OUT] The list of groups for multiselect.
* @param array $options Additional API options to select template groups.
*
* @return array
*/
function getTemplateSubGroups(array $template_groupids, ?array &$ms_template_groups = null, array $options = []) {
$db_groups = $template_groupids
? API::TemplateGroup()->get([
'output' => ['groupid', 'name'],
'groupids' => $template_groupids,
'preservekeys' => true
] + $options)
: [];
if ($ms_template_groups !== null) {
$ms_template_groups = CArrayHelper::renameObjectsKeys($db_groups, ['groupid' => 'id']);
}
$db_groups_names = [];
foreach ($db_groups as $db_group) {
$db_groups_names[] = $db_group['name'].'/';
}
if ($db_groups_names) {
$db_groups += API::TemplateGroup()->get([
'output' => ['groupid'],
'search' => ['name' => $db_groups_names],
'searchByAny' => true,
'startSearch' => true,
'preservekeys' => true
] + $options);
}
return array_keys($db_groups);
}
/**
* Creates a hintbox suitable for Problem hosts widget.
*
* @param array $hosts Array of problematic hosts.
* @param array $data Array of host data, filter settings and
* severity configuration.
* @param array $data['filter']['severities'] Array of severities.
* @param string $data['hosts_data'][<hostid>]['host'] Host name.
* @param int $data['hosts_data'][<hostid>]['severities'][<severity>] Severity count.
* @param CUrl|null $url URL that leads to problems view having
* hostid in its filter.
*
* @return CTableInfo
*/
function makeProblemHostsHintBox(array $hosts, array $data, ?CUrl $url) {
// Set trigger severities as table header, ordered starting from highest severity.
$header = [_('Host')];
foreach (range(TRIGGER_SEVERITY_COUNT - 1, TRIGGER_SEVERITY_NOT_CLASSIFIED) as $severity) {
if (in_array($severity, $data['filter']['severities'])) {
$header[] = CSeverityHelper::getName($severity);
}
}
$maintenanceids = [];
foreach ($data['hosts_data'] as $host) {
if ($host['maintenance_status'] == HOST_MAINTENANCE_STATUS_ON) {
$maintenanceids[$host['maintenanceid']] = true;
}
}
$db_maintenances = $maintenanceids
? API::Maintenance()->get([
'output' => ['name', 'description'],
'maintenanceids' => array_keys($maintenanceids),
'preservekeys' => true
])
: [];
$table_inf = (new CTableInfo())->setHeader($header);
$popup_rows = 0;
foreach ($hosts as $hostid => $host) {
$host_data = $data['hosts_data'][$hostid];
if ($url !== null) {
$url->setArgument('hostids', [$hostid]);
$host_name = new CLink($host_data['host'], $url->getUrl());
}
else {
$host_name = $host_data['host'];
}
if ($host_data['maintenance_status'] == HOST_MAINTENANCE_STATUS_ON) {
if (array_key_exists($host_data['maintenanceid'], $db_maintenances)) {
$maintenance = $db_maintenances[$host_data['maintenanceid']];
$maintenance_icon = makeMaintenanceIcon($host_data['maintenance_type'], $maintenance['name'],
$maintenance['description']
);
}
else {
$maintenance_icon = makeMaintenanceIcon($host_data['maintenance_type'], _('Inaccessible maintenance'),
''
);
}
$host_name = [$host_name, $maintenance_icon];
}
$row = new CRow((new CCol($host_name))->addClass(ZBX_STYLE_NOWRAP));
foreach (range(TRIGGER_SEVERITY_COUNT - 1, TRIGGER_SEVERITY_NOT_CLASSIFIED) as $severity) {
if (in_array($severity, $data['filter']['severities'])) {
$row->addItem(
($host_data['severities'][$severity] != 0)
? (new CCol($host_data['severities'][$severity]))
->addClass(CSeverityHelper::getStyle($severity))
: ''
);
}
}
$table_inf->addRow($row);
if (++$popup_rows == ZBX_WIDGET_ROWS) {
break;
}
}
return $table_inf;
}
/**
* Enriches host groups array by parent groups.
*
* @param array $groups
* @param string $groups[<groupid>]['groupid']
* @param string $groups[<groupid>]['name']
*
* @return array
*/
function enrichParentGroups(array $groups) {
$parents = [];
foreach ($groups as $group) {
$parent = explode('/', $group['name']);
while (array_pop($parent) && $parent) {
$parent_name = implode('/', $parent);
$parents[$parent_name] = $parent_name;
}
}
if ($parents) {
foreach ($groups as $group) {
if (array_key_exists($group['name'], $parents)) {
unset($parents[$group['name']]);
}
}
}
if ($parents) {
$groups += API::HostGroup()->get([
'output' => ['groupid', 'name'],
'filter' => [
'name' => $parents
],
'preservekeys' => true
]);
}
return $groups;
}
/**
* Enriches template groups array by parent groups.
*
* @param array $groups
* @param string $groups[<groupid>]['groupid']
* @param string $groups[<groupid>]['name']
*
* @return array
*/
function enrichParentTemplateGroups(array $groups) {
$parents = [];
foreach ($groups as $group) {
$parent = explode('/', $group['name']);
while (array_pop($parent) && $parent) {
$parent_name = implode('/', $parent);
$parents[$parent_name] = $parent_name;
}
}
if ($parents) {
foreach ($groups as $group) {
if (array_key_exists($group['name'], $parents)) {
unset($parents[$group['name']]);
}
}
}
if ($parents) {
$groups += API::TemplateGroup()->get([
'output' => ['groupid', 'name'],
'filter' => [
'name' => $parents
],
'preservekeys' => true
]);
}
return $groups;
}
|