Ejemplo n.º 1
0
 /**
  * Import graphs.
  *
  * @throws Exception
  */
 protected function processGraphs()
 {
     $allGraphs = $this->getFormattedGraphs();
     if (empty($allGraphs)) {
         return;
     }
     $graphsToCreate = array();
     $graphsToUpdate = array();
     foreach ($allGraphs as $graph) {
         $graphHostIds = array();
         if ($graph['ymin_item_1']) {
             $hostId = $this->referencer->resolveHostOrTemplate($graph['ymin_item_1']['host']);
             $itemId = $hostId ? $this->referencer->resolveItem($hostId, $graph['ymin_item_1']['key']) : false;
             if (!$itemId) {
                 throw new Exception(_s('Cannot find item "%1$s" on "%2$s" used as the Y axis MIN value for graph "%3$s".', $graph['ymin_item_1']['key'], $graph['ymin_item_1']['host'], $graph['name']));
             }
             $graph['ymin_itemid'] = $itemId;
         }
         if ($graph['ymax_item_1']) {
             $hostId = $this->referencer->resolveHostOrTemplate($graph['ymax_item_1']['host']);
             $itemId = $hostId ? $this->referencer->resolveItem($hostId, $graph['ymax_item_1']['key']) : false;
             if (!$itemId) {
                 throw new Exception(_s('Cannot find item "%1$s" on "%2$s" used as the Y axis MAX value for graph "%3$s".', $graph['ymax_item_1']['key'], $graph['ymax_item_1']['host'], $graph['name']));
             }
             $graph['ymax_itemid'] = $itemId;
         }
         if (isset($graph['gitems']) && $graph['gitems']) {
             foreach ($graph['gitems'] as &$gitem) {
                 $gitemHostId = $this->referencer->resolveHostOrTemplate($gitem['item']['host']);
                 if (!$gitemHostId) {
                     throw new Exception(_s('Cannot find host or template "%1$s" used in graph "%2$s".', $gitem['item']['host'], $graph['name']));
                 }
                 $gitem['itemid'] = $this->referencer->resolveItem($gitemHostId, $gitem['item']['key']);
                 $graphHostIds[$gitemHostId] = $gitemHostId;
             }
             unset($gitem);
         }
         // TODO: do this for all graphs at once
         $sql = 'SELECT g.graphid' . ' FROM graphs g,graphs_items gi,items i' . ' WHERE g.graphid=gi.graphid' . ' AND gi.itemid=i.itemid' . ' AND g.name=' . zbx_dbstr($graph['name']) . ' AND ' . dbConditionInt('i.hostid', $graphHostIds);
         $graphExists = DBfetch(DBselect($sql));
         if ($graphExists) {
             $dbGraph = API::Graph()->get(array('graphids' => $graphExists['graphid'], 'output' => array('graphid'), 'editable' => true));
             if (empty($dbGraph)) {
                 throw new Exception(_s('No permission for graph "%1$s".', $graph['name']));
             }
             $graph['graphid'] = $graphExists['graphid'];
             $graphsToUpdate[] = $graph;
         } else {
             $graphsToCreate[] = $graph;
         }
     }
     if ($this->options['graphs']['createMissing'] && $graphsToCreate) {
         API::Graph()->create($graphsToCreate);
     }
     if ($this->options['graphs']['updateExisting'] && $graphsToUpdate) {
         API::Graph()->update($graphsToUpdate);
     }
 }
Ejemplo n.º 2
0
 /**
  * Deletes discovery rules and prototypes from DB that are missing in XML.
  *
  * @return null
  */
 protected function deleteMissingDiscoveryRules()
 {
     if (!$this->options['discoveryRules']['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;
     }
     $discoveryRuleIdsXML = array();
     $allDiscoveryRules = $this->getFormattedDiscoveryRules();
     if ($allDiscoveryRules) {
         foreach ($allDiscoveryRules as $host => $discoveryRules) {
             $hostId = $this->referencer->resolveHostOrTemplate($host);
             foreach ($discoveryRules as $discoveryRule) {
                 $discoveryRuleId = $this->referencer->resolveItem($hostId, $discoveryRule['key_']);
                 if ($discoveryRuleId) {
                     $discoveryRuleIdsXML[$discoveryRuleId] = $discoveryRuleId;
                 }
             }
         }
     }
     $dbDiscoveryRuleIds = API::DiscoveryRule()->get(array('output' => array('itemid'), 'hostids' => $processedHostIds, 'preservekeys' => true, 'nopermissions' => true, 'inherited' => false));
     $discoveryRulesToDelete = array_diff_key($dbDiscoveryRuleIds, $discoveryRuleIdsXML);
     if ($discoveryRulesToDelete) {
         API::DiscoveryRule()->delete(array_keys($discoveryRulesToDelete));
     }
     // refresh discovery rules because templated ones can be inherited to host and used for prototypes
     $this->referencer->refreshItems();
     $hostPrototypeIdsXML = array();
     $triggerPrototypeIdsXML = array();
     $itemPrototypeIdsXML = array();
     $graphPrototypeIdsXML = array();
     foreach ($allDiscoveryRules as $host => $discoveryRules) {
         $hostId = $this->referencer->resolveHostOrTemplate($host);
         foreach ($discoveryRules as $discoveryRule) {
             $discoveryRuleId = $this->referencer->resolveItem($hostId, $discoveryRule['key_']);
             if ($discoveryRuleId) {
                 // gather host prototype IDs to delete
                 foreach ($discoveryRule['host_prototypes'] as $hostPrototype) {
                     $hostPrototypeId = $this->referencer->resolveHostPrototype($hostId, $discoveryRuleId, $hostPrototype['host']);
                     if ($hostPrototypeId) {
                         $hostPrototypeIdsXML[$hostPrototypeId] = $hostPrototypeId;
                     }
                 }
                 // gather trigger prototype IDs to delete
                 foreach ($discoveryRule['trigger_prototypes'] as $triggerPrototype) {
                     $triggerPrototypeId = $this->referencer->resolveTrigger($triggerPrototype['description'], $triggerPrototype['expression']);
                     if ($triggerPrototypeId) {
                         $triggerPrototypeIdsXML[$triggerPrototypeId] = $triggerPrototypeId;
                     }
                 }
                 // gather graph prototype IDs to delete
                 foreach ($discoveryRule['graph_prototypes'] as $graphPrototype) {
                     $graphPrototypeId = $this->referencer->resolveGraph($hostId, $graphPrototype['name']);
                     if ($graphPrototypeId) {
                         $graphPrototypeIdsXML[$graphPrototypeId] = $graphPrototypeId;
                     }
                 }
                 // gather item prototype IDs to delete
                 foreach ($discoveryRule['item_prototypes'] as $itemPrototype) {
                     $itemPrototypeId = $this->referencer->resolveItem($hostId, $itemPrototype['key_']);
                     if ($itemPrototypeId) {
                         $itemPrototypeIdsXML[$itemPrototypeId] = $itemPrototypeId;
                     }
                 }
             }
         }
     }
     // delete missing host prototypes
     $dbHostPrototypeIds = API::HostPrototype()->get(array('output' => array('hostid'), 'discoveryids' => $discoveryRuleIdsXML, 'preservekeys' => true, 'nopermissions' => true, 'inherited' => false));
     $hostPrototypesToDelete = array_diff_key($dbHostPrototypeIds, $hostPrototypeIdsXML);
     if ($hostPrototypesToDelete) {
         API::HostPrototype()->delete(array_keys($hostPrototypesToDelete));
     }
     // delete missing trigger prototypes
     $dbTriggerPrototypeIds = API::TriggerPrototype()->get(array('output' => array('triggerid'), 'hostids' => $processedHostIds, 'preservekeys' => true, 'nopermissions' => true, 'inherited' => false));
     $triggerPrototypesToDelete = array_diff_key($dbTriggerPrototypeIds, $triggerPrototypeIdsXML);
     // unlike triggers that belong to multiple hosts, trigger prototypes do not, so we just delete them
     if ($triggerPrototypesToDelete) {
         API::TriggerPrototype()->delete(array_keys($triggerPrototypesToDelete));
     }
     // delete missing graph prototypes
     $dbGraphPrototypeIds = API::GraphPrototype()->get(array('output' => array('graphid'), 'hostids' => $processedHostIds, 'preservekeys' => true, 'nopermissions' => true, 'inherited' => false));
     $graphPrototypesToDelete = array_diff_key($dbGraphPrototypeIds, $graphPrototypeIdsXML);
     // unlike graphs that belong to multiple hosts, graph prototypes do not, so we just delete them
     if ($graphPrototypesToDelete) {
         API::GraphPrototype()->delete(array_keys($graphPrototypesToDelete));
     }
     // delete missing item prototypes
     $dbItemPrototypeIds = API::ItemPrototype()->get(array('output' => array('itemid'), 'hostids' => $processedHostIds, 'preservekeys' => true, 'nopermissions' => true, 'inherited' => false));
     $itemPrototypesToDelete = array_diff_key($dbItemPrototypeIds, $itemPrototypeIdsXML);
     if ($itemPrototypesToDelete) {
         API::ItemPrototype()->delete(array_keys($itemPrototypesToDelete));
     }
 }