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 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730
|
<?php
/*
** Zabbix
** Copyright (C) 2001-2019 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 containing methods for operations with graph.
*/
class CGraph extends CGraphGeneral {
protected $tableName = 'graphs';
protected $tableAlias = 'g';
protected $sortColumns = ['graphid', 'name', 'graphtype'];
public function __construct() {
parent::__construct();
$this->errorMessages = array_merge($this->errorMessages, [
self::ERROR_TEMPLATE_HOST_MIX =>
_('Graph "%1$s" with templated items cannot contain items from other hosts.'),
self::ERROR_MISSING_GRAPH_NAME => _('Missing "name" field for graph.'),
self::ERROR_MISSING_GRAPH_ITEMS => _('Missing items for graph "%1$s".'),
self::ERROR_MISSING_REQUIRED_VALUE => _('No "%1$s" given for graph.'),
self::ERROR_GRAPH_SUM => _('Cannot add more than one item with type "Graph sum" on graph "%1$s".')
]);
}
/**
* Get graph data.
*
* @param array $options
*
* @return array
*/
public function get($options = []) {
$result = [];
$sqlParts = [
'select' => ['graphs' => 'g.graphid'],
'from' => ['graphs' => 'graphs g'],
'where' => [],
'group' => [],
'order' => [],
'limit' => null
];
$defOptions = [
'groupids' => null,
'templateids' => null,
'hostids' => null,
'graphids' => null,
'itemids' => null,
'templated' => null,
'inherited' => null,
'editable' => false,
'nopermissions' => null,
// filter
'filter' => null,
'search' => null,
'searchByAny' => null,
'startSearch' => false,
'excludeSearch' => false,
'searchWildcardsEnabled' => null,
// output
'output' => API_OUTPUT_EXTEND,
'selectGroups' => null,
'selectTemplates' => null,
'selectHosts' => null,
'selectItems' => null,
'selectGraphItems' => null,
'selectDiscoveryRule' => null,
'selectGraphDiscovery' => null,
'countOutput' => false,
'groupCount' => false,
'preservekeys' => false,
'sortfield' => '',
'sortorder' => '',
'limit' => null
];
$options = zbx_array_merge($defOptions, $options);
// permission check
if (self::$userData['type'] != USER_TYPE_SUPER_ADMIN && !$options['nopermissions']) {
$permission = $options['editable'] ? PERM_READ_WRITE : PERM_READ;
$userGroups = getUserGroupsByUserId(self::$userData['userid']);
// check permissions by graph items
$sqlParts['where'][] = 'NOT EXISTS ('.
'SELECT NULL'.
' FROM graphs_items gi,items i,hosts_groups hgg'.
' LEFT JOIN rights r'.
' ON r.id=hgg.groupid'.
' AND '.dbConditionInt('r.groupid', $userGroups).
' WHERE g.graphid=gi.graphid'.
' AND gi.itemid=i.itemid'.
' AND i.hostid=hgg.hostid'.
' GROUP BY i.hostid'.
' HAVING MAX(permission)<'.zbx_dbstr($permission).
' OR MIN(permission) IS NULL'.
' OR MIN(permission)='.PERM_DENY.
')';
// check permissions by Y min item
$sqlParts['where'][] = 'NOT EXISTS ('.
'SELECT NULL'.
' FROM items i,hosts_groups hgg'.
' LEFT JOIN rights r'.
' ON r.id=hgg.groupid'.
' AND '.dbConditionInt('r.groupid', $userGroups).
' WHERE g.ymin_type='.GRAPH_YAXIS_TYPE_ITEM_VALUE.
' AND g.ymin_itemid=i.itemid'.
' AND i.hostid=hgg.hostid'.
' GROUP BY i.hostid'.
' HAVING MAX(permission)<'.zbx_dbstr($permission).
' OR MIN(permission) IS NULL'.
' OR MIN(permission)='.PERM_DENY.
')';
// check permissions by Y max item
$sqlParts['where'][] = 'NOT EXISTS ('.
'SELECT NULL'.
' FROM items i,hosts_groups hgg'.
' LEFT JOIN rights r'.
' ON r.id=hgg.groupid'.
' AND '.dbConditionInt('r.groupid', $userGroups).
' WHERE g.ymax_type='.GRAPH_YAXIS_TYPE_ITEM_VALUE.
' AND g.ymax_itemid=i.itemid'.
' AND i.hostid=hgg.hostid'.
' GROUP BY i.hostid'.
' HAVING MAX(permission)<'.zbx_dbstr($permission).
' OR MIN(permission) IS NULL'.
' OR MIN(permission)='.PERM_DENY.
')';
}
// groupids
if (!is_null($options['groupids'])) {
zbx_value2array($options['groupids']);
$sqlParts['from']['graphs_items'] = 'graphs_items gi';
$sqlParts['from']['items'] = 'items i';
$sqlParts['from']['hosts_groups'] = 'hosts_groups hg';
$sqlParts['where'][] = dbConditionInt('hg.groupid', $options['groupids']);
$sqlParts['where'][] = 'hg.hostid=i.hostid';
$sqlParts['where']['gig'] = 'gi.graphid=g.graphid';
$sqlParts['where']['igi'] = 'i.itemid=gi.itemid';
$sqlParts['where']['hgi'] = 'hg.hostid=i.hostid';
if ($options['groupCount']) {
$sqlParts['group']['hg'] = 'hg.groupid';
}
}
// templateids
if (!is_null($options['templateids'])) {
zbx_value2array($options['templateids']);
if (!is_null($options['hostids'])) {
zbx_value2array($options['hostids']);
$options['hostids'] = array_merge($options['hostids'], $options['templateids']);
}
else {
$options['hostids'] = $options['templateids'];
}
}
// hostids
if (!is_null($options['hostids'])) {
zbx_value2array($options['hostids']);
$sqlParts['from']['graphs_items'] = 'graphs_items gi';
$sqlParts['from']['items'] = 'items i';
$sqlParts['where'][] = dbConditionInt('i.hostid', $options['hostids']);
$sqlParts['where']['gig'] = 'gi.graphid=g.graphid';
$sqlParts['where']['igi'] = 'i.itemid=gi.itemid';
if ($options['groupCount']) {
$sqlParts['group']['i'] = 'i.hostid';
}
}
// graphids
if (!is_null($options['graphids'])) {
zbx_value2array($options['graphids']);
$sqlParts['where'][] = dbConditionInt('g.graphid', $options['graphids']);
}
// itemids
if (!is_null($options['itemids'])) {
zbx_value2array($options['itemids']);
$sqlParts['from']['graphs_items'] = 'graphs_items gi';
$sqlParts['where']['gig'] = 'gi.graphid=g.graphid';
$sqlParts['where'][] = dbConditionInt('gi.itemid', $options['itemids']);
if ($options['groupCount']) {
$sqlParts['group']['gi'] = 'gi.itemid';
}
}
// templated
if (!is_null($options['templated'])) {
$sqlParts['from']['graphs_items'] = 'graphs_items gi';
$sqlParts['from']['items'] = 'items i';
$sqlParts['from']['hosts'] = 'hosts h';
$sqlParts['where']['igi'] = 'i.itemid=gi.itemid';
$sqlParts['where']['ggi'] = 'g.graphid=gi.graphid';
$sqlParts['where']['hi'] = 'h.hostid=i.hostid';
if ($options['templated']) {
$sqlParts['where'][] = 'h.status='.HOST_STATUS_TEMPLATE;
}
else {
$sqlParts['where'][] = 'h.status<>'.HOST_STATUS_TEMPLATE;
}
}
// inherited
if (!is_null($options['inherited'])) {
if ($options['inherited']) {
$sqlParts['where'][] = 'g.templateid IS NOT NULL';
}
else {
$sqlParts['where'][] = 'g.templateid IS NULL';
}
}
// search
if (is_array($options['search'])) {
zbx_db_search('graphs g', $options, $sqlParts);
}
// filter
if (is_null($options['filter'])) {
$options['filter'] = [];
}
if (is_array($options['filter'])) {
if (!array_key_exists('flags', $options['filter'])) {
$options['filter']['flags'] = [ZBX_FLAG_DISCOVERY_NORMAL, ZBX_FLAG_DISCOVERY_CREATED];
}
$this->dbFilter('graphs g', $options, $sqlParts);
if (isset($options['filter']['host'])) {
zbx_value2array($options['filter']['host']);
$sqlParts['from']['graphs_items'] = 'graphs_items gi';
$sqlParts['from']['items'] = 'items i';
$sqlParts['from']['hosts'] = 'hosts h';
$sqlParts['where']['gig'] = 'gi.graphid=g.graphid';
$sqlParts['where']['igi'] = 'i.itemid=gi.itemid';
$sqlParts['where']['hi'] = 'h.hostid=i.hostid';
$sqlParts['where']['host'] = dbConditionString('h.host', $options['filter']['host']);
}
if (isset($options['filter']['hostid'])) {
zbx_value2array($options['filter']['hostid']);
$sqlParts['from']['graphs_items'] = 'graphs_items gi';
$sqlParts['from']['items'] = 'items i';
$sqlParts['where']['gig'] = 'gi.graphid=g.graphid';
$sqlParts['where']['igi'] = 'i.itemid=gi.itemid';
$sqlParts['where']['hostid'] = dbConditionInt('i.hostid', $options['filter']['hostid']);
}
}
// limit
if (zbx_ctype_digit($options['limit']) && $options['limit']) {
$sqlParts['limit'] = $options['limit'];
}
$sqlParts = $this->applyQueryOutputOptions($this->tableName(), $this->tableAlias(), $options, $sqlParts);
$sqlParts = $this->applyQuerySortOptions($this->tableName(), $this->tableAlias(), $options, $sqlParts);
$dbRes = DBselect($this->createSelectQueryFromParts($sqlParts), $sqlParts['limit']);
while ($graph = DBfetch($dbRes)) {
if ($options['countOutput']) {
if ($options['groupCount']) {
$result[] = $graph;
}
else {
$result = $graph['rowscount'];
}
}
else {
$result[$graph['graphid']] = $graph;
}
}
if ($options['countOutput']) {
return $result;
}
if (isset($options['expandName'])) {
$result = CMacrosResolverHelper::resolveGraphNameByIds($result);
}
if ($result) {
$result = $this->addRelatedObjects($options, $result);
}
// removing keys (hash -> array)
if (!$options['preservekeys']) {
$result = zbx_cleanHashes($result);
}
return $result;
}
protected function inherit($graph, $hostids = null) {
$graphTemplates = API::Template()->get([
'itemids' => zbx_objectValues($graph['gitems'], 'itemid'),
'output' => ['templateid'],
'nopermissions' => true
]);
if (empty($graphTemplates)) {
return true;
}
$graphTemplate = reset($graphTemplates);
$chdHosts = API::Host()->get([
'templateids' => $graphTemplate['templateid'],
'output' => ['hostid', 'host'],
'preservekeys' => true,
'hostids' => $hostids,
'nopermissions' => true,
'templated_hosts' => true
]);
$graph = $this->get([
'graphids' => $graph['graphid'],
'nopermissions' => true,
'selectGraphItems' => API_OUTPUT_EXTEND,
'output' => API_OUTPUT_EXTEND
]);
$graph = reset($graph);
foreach ($chdHosts as $chdHost) {
$tmpGraph = $graph;
$tmpGraph['templateid'] = $graph['graphid'];
$tmpGraph['gitems'] = getSameGraphItemsForHost($tmpGraph['gitems'], $chdHost['hostid']);
if (!$tmpGraph['gitems']) {
self::exception(ZBX_API_ERROR_PARAMETERS, _s('Graph "%1$s" cannot inherit. No required items on "%2$s".', $tmpGraph['name'], $chdHost['host']));
}
if ($tmpGraph['ymax_itemid'] > 0) {
$ymaxItemid = getSameGraphItemsForHost([['itemid' => $tmpGraph['ymax_itemid']]], $chdHost['hostid']);
if (!$ymaxItemid) {
self::exception(ZBX_API_ERROR_PARAMETERS, _s('Graph "%1$s" cannot inherit. No required items on "%2$s" (Ymax value item).', $tmpGraph['name'], $chdHost['host']));
}
$ymaxItemid = reset($ymaxItemid);
$tmpGraph['ymax_itemid'] = $ymaxItemid['itemid'];
}
if ($tmpGraph['ymin_itemid'] > 0) {
$yminItemid = getSameGraphItemsForHost([['itemid' => $tmpGraph['ymin_itemid']]], $chdHost['hostid']);
if (!$yminItemid) {
self::exception(ZBX_API_ERROR_PARAMETERS, _s('Graph "%1$s" cannot inherit. No required items on "%2$s" (Ymin value item).', $tmpGraph['name'], $chdHost['host']));
}
$yminItemid = reset($yminItemid);
$tmpGraph['ymin_itemid'] = $yminItemid['itemid'];
}
// check if templated graph exists
$chdGraphs = $this->get([
'filter' => ['templateid' => $tmpGraph['graphid'], 'flags' => [ZBX_FLAG_DISCOVERY_PROTOTYPE, ZBX_FLAG_DISCOVERY_NORMAL]],
'output' => API_OUTPUT_EXTEND,
'selectGraphItems' => API_OUTPUT_EXTEND,
'preservekeys' => true,
'hostids' => $chdHost['hostid']
]);
if ($chdGraph = reset($chdGraphs)) {
if ($tmpGraph['name'] !== $chdGraph['name']) {
$graphExists = $this->get([
'output' => ['graphid'],
'hostids' => $chdHost['hostid'],
'filter' => [
'name' => $tmpGraph['name'],
'flags' => null
],
'nopermissions' => true,
'limit' => 1
]);
if ($graphExists) {
self::exception(ZBX_API_ERROR_PARAMETERS, _s(
'Graph "%1$s" already exists on "%2$s".',
$tmpGraph['name'],
$chdHost['host']
));
}
}
elseif ($chdGraph['flags'] != $tmpGraph['flags']) {
self::exception(ZBX_API_ERROR_PARAMETERS, _('Graph with same name but other type exist.'));
}
$tmpGraph['graphid'] = $chdGraph['graphid'];
$this->updateReal($tmpGraph, $chdGraph);
}
// check if graph with same name and items exists
else {
$chdGraph = $this->get([
'filter' => ['name' => $tmpGraph['name'], 'flags' => null],
'output' => API_OUTPUT_EXTEND,
'selectGraphItems' => API_OUTPUT_EXTEND,
'preservekeys' => true,
'nopermissions' => true,
'hostids' => $chdHost['hostid']
]);
if ($chdGraph = reset($chdGraph)) {
if ($chdGraph['templateid'] != 0) {
self::exception(ZBX_API_ERROR_PARAMETERS, _s('Graph "%1$s" already exists on "%2$s" (inherited from another template).', $tmpGraph['name'], $chdHost['host']));
}
elseif ($chdGraph['flags'] != $tmpGraph['flags']) {
self::exception(ZBX_API_ERROR_PARAMETERS, _('Graph with same name but other type exist.'));
}
$chdGraphItemItems = API::Item()->get([
'output' => ['itemid', 'key_', 'hostid'],
'itemids' => zbx_objectValues($chdGraph['gitems'], 'itemid'),
'preservekeys' => true
]);
if (count($chdGraph['gitems']) == count($tmpGraph['gitems'])) {
foreach ($tmpGraph['gitems'] as $gitem) {
foreach ($chdGraph['gitems'] as $chdGraphItem) {
$chdGraphItemItem = $chdGraphItemItems[$chdGraphItem['itemid']];
if ($gitem['key_'] == $chdGraphItemItem['key_']
&& bccomp($chdHost['hostid'], $chdGraphItemItem['hostid']) == 0) {
continue 2;
}
}
self::exception(ZBX_API_ERROR_PARAMETERS, _s('Graph "%1$s" already exists on "%2$s" (items are not identical).', $tmpGraph['name'], $chdHost['host']));
}
$tmpGraph['graphid'] = $chdGraph['graphid'];
$this->updateReal($tmpGraph, $chdGraph);
}
else {
self::exception(ZBX_API_ERROR_PARAMETERS, _s('Graph "%1$s" already exists on "%2$s" (items are not identical).', $tmpGraph['name'], $chdHost['host']));
}
}
else {
$graphid = $this->createReal($tmpGraph);
$tmpGraph['graphid'] = $graphid;
}
}
$this->inherit($tmpGraph);
}
}
/**
* Inherit template graphs from template to host.
*
* @param array $data
*
* @return bool
*/
public function syncTemplates($data) {
$data['templateids'] = zbx_toArray($data['templateids']);
$data['hostids'] = zbx_toArray($data['hostids']);
$dbLinks = DBSelect(
'SELECT ht.hostid,ht.templateid'.
' FROM hosts_templates ht'.
' WHERE '.dbConditionInt('ht.hostid', $data['hostids']).
' AND '.dbConditionInt('ht.templateid', $data['templateids'])
);
$linkage = [];
while ($link = DBfetch($dbLinks)) {
if (!isset($linkage[$link['templateid']])) {
$linkage[$link['templateid']] = [];
}
$linkage[$link['templateid']][$link['hostid']] = 1;
}
$graphs = $this->get([
'hostids' => $data['templateids'],
'preservekeys' => true,
'output' => API_OUTPUT_EXTEND,
'selectGraphItems' => API_OUTPUT_EXTEND,
'selectHosts' => ['hostid']
]);
foreach ($graphs as $graph) {
foreach ($data['hostids'] as $hostid) {
if (isset($linkage[$graph['hosts'][0]['hostid']][$hostid])) {
$this->inherit($graph, $hostid);
}
}
}
return true;
}
/**
* Delete graphs.
*
* @param array $graphids
*
* @return array
*/
public function delete(array $graphids) {
$this->validateDelete($graphids, $db_graphs);
CGraphManager::delete($graphids);
$this->addAuditBulk(AUDIT_ACTION_DELETE, AUDIT_RESOURCE_GRAPH, $db_graphs);
return ['graphids' => $graphids];
}
/**
* Validates the input parameters for the delete() method.
*
* @param array $graphids [IN/OUT]
* @param array $db_graphs [OUT]
*
* @throws APIException if the input is invalid.
*/
private function validateDelete(array &$graphids, array &$db_graphs = null) {
$api_input_rules = ['type' => API_IDS, 'flags' => API_NOT_EMPTY, 'uniq' => true];
if (!CApiInputValidator::validate($api_input_rules, $graphids, '/', $error)) {
self::exception(ZBX_API_ERROR_PARAMETERS, $error);
}
$db_graphs = $this->get([
'output' => ['graphid', 'name', 'templateid'],
'graphids' => $graphids,
'editable' => true,
'preservekeys' => true
]);
foreach ($graphids as $graphid) {
if (!array_key_exists($graphid, $db_graphs)) {
self::exception(ZBX_API_ERROR_PERMISSIONS,
_('No permissions to referred object or it does not exist!')
);
}
if ($db_graphs[$graphid]['templateid'] != 0) {
self::exception(ZBX_API_ERROR_PARAMETERS, _('Cannot delete templated graph.'));
}
}
}
protected function addRelatedObjects(array $options, array $result) {
$result = parent::addRelatedObjects($options, $result);
$graphids = array_keys($result);
// adding Items
if ($options['selectItems'] !== null && $options['selectItems'] !== API_OUTPUT_COUNT) {
$relationMap = $this->createRelationMap($result, 'graphid', 'itemid', 'graphs_items');
$items = API::Item()->get([
'output' => $options['selectItems'],
'itemids' => $relationMap->getRelatedIds(),
'webitems' => true,
'nopermissions' => true,
'preservekeys' => true
]);
$result = $relationMap->mapMany($result, $items, 'items');
}
// adding discoveryRule
if ($options['selectDiscoveryRule'] !== null) {
$dbRules = DBselect(
'SELECT id.parent_itemid,gd.graphid'.
' FROM graph_discovery gd,item_discovery id,graphs_items gi'.
' WHERE '.dbConditionInt('gd.graphid', $graphids).
' AND gd.parent_graphid=gi.graphid'.
' AND gi.itemid=id.itemid'
);
$relationMap = new CRelationMap();
while ($relation = DBfetch($dbRules)) {
$relationMap->addRelation($relation['graphid'], $relation['parent_itemid']);
}
$discoveryRules = API::DiscoveryRule()->get([
'output' => $options['selectDiscoveryRule'],
'itemids' => $relationMap->getRelatedIds(),
'nopermissions' => true,
'preservekeys' => true
]);
$result = $relationMap->mapOne($result, $discoveryRules, 'discoveryRule');
}
// adding graph discovery
if ($options['selectGraphDiscovery'] !== null) {
$graphDiscoveries = API::getApiService()->select('graph_discovery', [
'output' => $this->outputExtend($options['selectGraphDiscovery'], ['graphid']),
'filter' => ['graphid' => array_keys($result)],
'preservekeys' => true
]);
$relationMap = $this->createRelationMap($graphDiscoveries, 'graphid', 'graphid');
$graphDiscoveries = $this->unsetExtraFields($graphDiscoveries, ['graphid'],
$options['selectGraphDiscovery']
);
$result = $relationMap->mapOne($result, $graphDiscoveries, 'graphDiscovery');
}
return $result;
}
/**
* Validate create.
*
* @param array $graphs
*/
protected function validateCreate(array $graphs) {
$itemIds = $this->validateItemsCreate($graphs);
$this->validateItems($itemIds, $graphs);
parent::validateCreate($graphs);
}
/**
* Validate update.
*
* @param array $graphs
* @param array $dbGraphs
*/
protected function validateUpdate(array $graphs, array $dbGraphs) {
// check for "itemid" when updating graph with only "gitemid" passed
foreach ($graphs as &$graph) {
if (isset($graph['gitems'])) {
foreach ($graph['gitems'] as &$gitem) {
if (isset($gitem['gitemid']) && !isset($gitem['itemid'])) {
$dbGitems = zbx_toHash($dbGraphs[$graph['graphid']]['gitems'], 'gitemid');
$gitem['itemid'] = $dbGitems[$gitem['gitemid']]['itemid'];
}
}
unset($gitem);
}
}
unset($graph);
$itemIds = $this->validateItemsUpdate($graphs);
$this->validateItems($itemIds, $graphs);
parent::validateUpdate($graphs, $dbGraphs);
}
/**
* Validates items.
*
* @param array $itemIds
* @param array $graphs
*/
protected function validateItems(array $itemIds, array $graphs) {
$dbItems = API::Item()->get([
'output' => ['name', 'value_type'],
'itemids' => $itemIds,
'webitems' => true,
'editable' => true,
'preservekeys' => true
]);
// check if items exist and user has permission to access those items
foreach ($itemIds as $itemId) {
if (!isset($dbItems[$itemId])) {
self::exception(ZBX_API_ERROR_PARAMETERS, _('No permissions to referred object or it does not exist!'));
}
}
$allowedValueTypes = [ITEM_VALUE_TYPE_FLOAT, ITEM_VALUE_TYPE_UINT64];
// get value type and name for these items
foreach ($graphs as $graph) {
// graph items
foreach ($graph['gitems'] as $graphItem) {
$item = $dbItems[$graphItem['itemid']];
if (!in_array($item['value_type'], $allowedValueTypes)) {
self::exception(ZBX_API_ERROR_PARAMETERS, _s(
'Cannot add a non-numeric item "%1$s" to graph "%2$s".',
$item['name'],
$graph['name']
));
}
}
// Y axis min
if (isset($graph['ymin_itemid']) && $graph['ymin_itemid']
&& isset($graph['ymin_type']) && $graph['ymin_type'] == GRAPH_YAXIS_TYPE_ITEM_VALUE) {
$item = $dbItems[$graph['ymin_itemid']];
if (!in_array($item['value_type'], $allowedValueTypes)) {
self::exception(ZBX_API_ERROR_PARAMETERS, _s(
'Cannot add a non-numeric item "%1$s" to graph "%2$s".',
$item['name'],
$graph['name']
));
}
}
// Y axis max
if (isset($graph['ymax_itemid']) && $graph['ymax_itemid']
&& isset($graph['ymax_type']) && $graph['ymax_type'] == GRAPH_YAXIS_TYPE_ITEM_VALUE) {
$item = $dbItems[$graph['ymax_itemid']];
if (!in_array($item['value_type'], $allowedValueTypes)) {
self::exception(ZBX_API_ERROR_PARAMETERS, _s(
'Cannot add a non-numeric item "%1$s" to graph "%2$s".',
$item['name'],
$graph['name']
));
}
}
}
}
}
|