/** * Deletes graphs from DB that are missing in XML. * * @return null */ protected function deleteMissingGraphs() { if (!$this->options['graphs']['deleteMissing']) { return; } $processedHostIds = $this->importedObjectContainer->getHostIds(); $processedTemplateIds = $this->importedObjectContainer->getTemplateIds(); $processedHostIds = array_merge($processedHostIds, $processedTemplateIds); // no hosts or templates have been processed if (!$processedHostIds) { return; } $graphsIdsXML = array(); // gather host IDs for graphs that exist in XML $allGraphs = $this->getFormattedGraphs(); if ($allGraphs) { foreach ($allGraphs as $graph) { if (isset($graph['gitems']) && $graph['gitems']) { foreach ($graph['gitems'] as $gitem) { $gitemHostId = $this->referencer->resolveHostOrTemplate($gitem['item']['host']); $graphId = $this->referencer->resolveGraph($gitemHostId, $graph['name']); if ($graphId) { $graphsIdsXML[$graphId] = $graphId; } } } } } $dbGraphIds = API::Graph()->get(array('output' => array('graphid'), 'hostids' => $processedHostIds, 'selectHosts' => array('hostid'), 'preservekeys' => true, 'nopermissions' => true, 'inherited' => false, 'filter' => array('flags' => ZBX_FLAG_DISCOVERY_NORMAL))); // check that potentially deletable graph belongs to same hosts that are in XML // if some graphs belong to more hosts than current XML contains, don't delete them $graphsToDelete = array_diff_key($dbGraphIds, $graphsIdsXML); $graphIdsToDelete = array(); $processedHostIds = array_flip($processedHostIds); foreach ($graphsToDelete as $graphId => $graph) { $graphHostIds = array_flip(zbx_objectValues($graph['hosts'], 'hostid')); if (!array_diff_key($graphHostIds, $processedHostIds)) { $graphIdsToDelete[] = $graphId; } } if ($graphIdsToDelete) { API::Graph()->delete($graphIdsToDelete); } $this->referencer->refreshGraphs(); }