コード例 #1
0
 /**
  * Get discovery rules related objects from database.
  *
  * @param array $items
  *
  * @return array
  */
 protected function prepareDiscoveryRules(array $items)
 {
     foreach ($items as &$item) {
         $item['itemPrototypes'] = array();
         $item['graphPrototypes'] = array();
         $item['triggerPrototypes'] = array();
         $item['hostPrototypes'] = array();
     }
     unset($item);
     // gather item prototypes
     $prototypes = API::ItemPrototype()->get(array('discoveryids' => zbx_objectValues($items, 'itemid'), 'output' => $this->dataFields['discoveryrule'], 'selectApplications' => API_OUTPUT_EXTEND, 'selectDiscoveryRule' => array('itemid'), 'inherited' => false, 'preservekeys' => true));
     // gather value maps
     $valueMapIds = zbx_objectValues($prototypes, 'valuemapid');
     $DbValueMaps = DBselect('SELECT vm.valuemapid, vm.name FROM valuemaps vm WHERE ' . dbConditionInt('vm.valuemapid', $valueMapIds));
     $valueMaps = array();
     while ($valueMap = DBfetch($DbValueMaps)) {
         $valueMaps[$valueMap['valuemapid']] = $valueMap['name'];
     }
     foreach ($prototypes as $prototype) {
         $prototype['valuemap'] = array();
         if ($prototype['valuemapid']) {
             $prototype['valuemap']['name'] = $valueMaps[$prototype['valuemapid']];
         }
         $items[$prototype['discoveryRule']['itemid']]['itemPrototypes'][] = $prototype;
     }
     // gather graph prototypes
     $graphs = API::GraphPrototype()->get(array('discoveryids' => zbx_objectValues($items, 'itemid'), 'selectDiscoveryRule' => API_OUTPUT_EXTEND, 'selectGraphItems' => API_OUTPUT_EXTEND, 'output' => API_OUTPUT_EXTEND, 'inherited' => false, 'preservekeys' => true));
     $graphs = $this->prepareGraphs($graphs);
     foreach ($graphs as $graph) {
         $items[$graph['discoveryRule']['itemid']]['graphPrototypes'][] = $graph;
     }
     // gather trigger prototypes
     $triggers = API::TriggerPrototype()->get(array('discoveryids' => zbx_objectValues($items, 'itemid'), 'output' => API_OUTPUT_EXTEND, 'selectDiscoveryRule' => API_OUTPUT_EXTEND, 'selectItems' => array('flags', 'type'), 'inherited' => false, 'preservekeys' => true, 'expandData' => true));
     foreach ($triggers as $trigger) {
         foreach ($trigger['items'] as $item) {
             if ($item['flags'] == ZBX_FLAG_DISCOVERY_CREATED || $item['type'] == ITEM_TYPE_HTTPTEST) {
                 continue 2;
             }
         }
         $trigger['expression'] = explode_exp($trigger['expression']);
         $items[$trigger['discoveryRule']['itemid']]['triggerPrototypes'][] = $trigger;
     }
     // gather host prototypes
     $hostPrototypes = API::HostPrototype()->get(array('discoveryids' => zbx_objectValues($items, 'itemid'), 'output' => API_OUTPUT_EXTEND, 'selectGroupLinks' => API_OUTPUT_EXTEND, 'selectGroupPrototypes' => API_OUTPUT_EXTEND, 'selectDiscoveryRule' => API_OUTPUT_EXTEND, 'selectTemplates' => API_OUTPUT_EXTEND, 'inherited' => false, 'preservekeys' => true));
     // replace group prototype group IDs with references
     $groupIds = array();
     foreach ($hostPrototypes as $hostPrototype) {
         foreach ($hostPrototype['groupLinks'] as $groupLink) {
             $groupIds[$groupLink['groupid']] = $groupLink['groupid'];
         }
     }
     $groups = $this->getGroupsReferences($groupIds);
     // export the groups used in group prototypes
     $this->data['groups'] += $groups;
     foreach ($hostPrototypes as $hostPrototype) {
         foreach ($hostPrototype['groupLinks'] as &$groupLink) {
             $groupLink['groupid'] = $groups[$groupLink['groupid']];
         }
         unset($groupLink);
         $items[$hostPrototype['discoveryRule']['itemid']]['hostPrototypes'][] = $hostPrototype;
     }
     return $items;
 }
コード例 #2
0
 /**
  * Unlinks the templates from the given hosts. If $tragetids is set to null, the templates will be unlinked from
  * all hosts.
  *
  * @param array      $templateids
  * @param null|array $targetids		the IDs of the hosts to unlink the templates from
  * @param bool       $clear			delete all of the inherited objects from the hosts
  */
 protected function unlink($templateids, $targetids = null, $clear = false)
 {
     $flags = $clear ? array(ZBX_FLAG_DISCOVERY_NORMAL, ZBX_FLAG_DISCOVERY_RULE) : array(ZBX_FLAG_DISCOVERY_NORMAL, ZBX_FLAG_DISCOVERY_RULE, ZBX_FLAG_DISCOVERY_PROTOTYPE);
     // check that all triggers on templates that we unlink, don't have items from another templates
     $sql = 'SELECT DISTINCT t.description' . ' FROM triggers t,functions f,items i' . ' WHERE t.triggerid=f.triggerid' . ' AND f.itemid=i.itemid' . ' AND ' . dbConditionInt('i.hostid', $templateids) . ' AND EXISTS (' . 'SELECT ff.triggerid' . ' FROM functions ff,items ii' . ' WHERE ff.itemid=ii.itemid' . ' AND ff.triggerid=t.triggerid' . ' AND ' . dbConditionInt('ii.hostid', $templateids, true) . ')' . ' AND t.flags=' . ZBX_FLAG_DISCOVERY_NORMAL;
     if ($dbTrigger = DBfetch(DBSelect($sql, 1))) {
         self::exception(ZBX_API_ERROR_PARAMETERS, _s('Cannot unlink trigger "%s", it has items from template that is left linked to host.', $dbTrigger['description']));
     }
     $sqlFrom = ' triggers t,hosts h';
     $sqlWhere = ' EXISTS (' . 'SELECT ff.triggerid' . ' FROM functions ff,items ii' . ' WHERE ff.triggerid=t.templateid' . ' AND ii.itemid=ff.itemid' . ' AND ' . dbConditionInt('ii.hostid', $templateids) . ')' . ' AND ' . dbConditionInt('t.flags', $flags);
     if (!is_null($targetids)) {
         $sqlFrom = ' triggers t,functions f,items i,hosts h';
         $sqlWhere .= ' AND ' . dbConditionInt('i.hostid', $targetids) . ' AND f.itemid=i.itemid' . ' AND t.triggerid=f.triggerid' . ' AND h.hostid=i.hostid';
     }
     $sql = 'SELECT DISTINCT t.triggerid,t.description,t.flags,t.expression,h.name as host' . ' FROM ' . $sqlFrom . ' WHERE ' . $sqlWhere;
     $dbTriggers = DBSelect($sql);
     $triggers = array(ZBX_FLAG_DISCOVERY_NORMAL => array(), ZBX_FLAG_DISCOVERY_PROTOTYPE => array());
     $triggerids = array();
     while ($trigger = DBfetch($dbTriggers)) {
         $triggers[$trigger['flags']][$trigger['triggerid']] = array('description' => $trigger['description'], 'expression' => explode_exp($trigger['expression']), 'triggerid' => $trigger['triggerid'], 'host' => $trigger['host']);
         if (!in_array($trigger['triggerid'], $triggerids)) {
             array_push($triggerids, $trigger['triggerid']);
         }
     }
     if (!empty($triggers[ZBX_FLAG_DISCOVERY_NORMAL])) {
         if ($clear) {
             $result = API::Trigger()->delete(array_keys($triggers[ZBX_FLAG_DISCOVERY_NORMAL]), true);
             if (!$result) {
                 self::exception(ZBX_API_ERROR_INTERNAL, _('Cannot unlink and clear triggers'));
             }
         } else {
             DB::update('triggers', array('values' => array('templateid' => 0), 'where' => array('triggerid' => array_keys($triggers[ZBX_FLAG_DISCOVERY_NORMAL]))));
             foreach ($triggers[ZBX_FLAG_DISCOVERY_NORMAL] as $trigger) {
                 info(_s('Unlinked: Trigger "%1$s" on "%2$s".', $trigger['description'], $trigger['host']));
             }
         }
     }
     if (!empty($triggers[ZBX_FLAG_DISCOVERY_PROTOTYPE])) {
         if ($clear) {
             $result = API::TriggerPrototype()->delete(array_keys($triggers[ZBX_FLAG_DISCOVERY_PROTOTYPE]), true);
             if (!$result) {
                 self::exception(ZBX_API_ERROR_INTERNAL, _('Cannot unlink and clear triggers'));
             }
         } else {
             DB::update('triggers', array('values' => array('templateid' => 0), 'where' => array('triggerid' => array_keys($triggers[ZBX_FLAG_DISCOVERY_PROTOTYPE]))));
             foreach ($triggers[ZBX_FLAG_DISCOVERY_PROTOTYPE] as $trigger) {
                 info(_s('Unlinked: Trigger prototype "%1$s" on "%2$s".', $trigger['description'], $trigger['host']));
             }
         }
     }
     /* ITEMS, DISCOVERY RULES {{{ */
     $sqlFrom = ' items i1,items i2,hosts h';
     $sqlWhere = ' i2.itemid=i1.templateid' . ' AND ' . dbConditionInt('i2.hostid', $templateids) . ' AND ' . dbConditionInt('i1.flags', $flags) . ' AND h.hostid=i1.hostid';
     if (!is_null($targetids)) {
         $sqlWhere .= ' AND ' . dbConditionInt('i1.hostid', $targetids);
     }
     $sql = 'SELECT DISTINCT i1.itemid,i1.flags,i1.name,i1.hostid,h.name as host' . ' FROM ' . $sqlFrom . ' WHERE ' . $sqlWhere;
     $dbItems = DBSelect($sql);
     $items = array(ZBX_FLAG_DISCOVERY_NORMAL => array(), ZBX_FLAG_DISCOVERY_RULE => array(), ZBX_FLAG_DISCOVERY_PROTOTYPE => array());
     while ($item = DBfetch($dbItems)) {
         $items[$item['flags']][$item['itemid']] = array('name' => $item['name'], 'host' => $item['host']);
     }
     if (!empty($items[ZBX_FLAG_DISCOVERY_RULE])) {
         if ($clear) {
             $result = API::DiscoveryRule()->delete(array_keys($items[ZBX_FLAG_DISCOVERY_RULE]), true);
             if (!$result) {
                 self::exception(ZBX_API_ERROR_INTERNAL, _('Cannot unlink and clear discovery rules'));
             }
         } else {
             DB::update('items', array('values' => array('templateid' => 0), 'where' => array('itemid' => array_keys($items[ZBX_FLAG_DISCOVERY_RULE]))));
             foreach ($items[ZBX_FLAG_DISCOVERY_RULE] as $discoveryRule) {
                 info(_s('Unlinked: Discovery rule "%1$s" on "%2$s".', $discoveryRule['name'], $discoveryRule['host']));
             }
         }
     }
     if (!empty($items[ZBX_FLAG_DISCOVERY_NORMAL])) {
         if ($clear) {
             $result = API::Item()->delete(array_keys($items[ZBX_FLAG_DISCOVERY_NORMAL]), true);
             if (!$result) {
                 self::exception(ZBX_API_ERROR_INTERNAL, _('Cannot unlink and clear items'));
             }
         } else {
             DB::update('items', array('values' => array('templateid' => 0), 'where' => array('itemid' => array_keys($items[ZBX_FLAG_DISCOVERY_NORMAL]))));
             foreach ($items[ZBX_FLAG_DISCOVERY_NORMAL] as $item) {
                 info(_s('Unlinked: Item "%1$s" on "%2$s".', $item['name'], $item['host']));
             }
         }
     }
     if (!empty($items[ZBX_FLAG_DISCOVERY_PROTOTYPE])) {
         if ($clear) {
             $result = API::Itemprototype()->delete(array_keys($items[ZBX_FLAG_DISCOVERY_PROTOTYPE]), true);
             if (!$result) {
                 self::exception(ZBX_API_ERROR_INTERNAL, _('Cannot unlink and clear item prototypes'));
             }
         } else {
             DB::update('items', array('values' => array('templateid' => 0), 'where' => array('itemid' => array_keys($items[ZBX_FLAG_DISCOVERY_PROTOTYPE]))));
             foreach ($items[ZBX_FLAG_DISCOVERY_PROTOTYPE] as $item) {
                 info(_s('Unlinked: Item prototype "%1$s" on "%2$s".', $item['name'], $item['host']));
             }
         }
     }
     /* }}} ITEMS, DISCOVERY RULES */
     // host prototypes
     // we need only to unlink host prototypes. in case of unlink and clear they will be deleted together with LLD rules.
     if (!$clear && isset($items[ZBX_FLAG_DISCOVERY_RULE])) {
         $discoveryRuleIds = array_keys($items[ZBX_FLAG_DISCOVERY_RULE]);
         $hostPrototypes = DBfetchArrayAssoc(DBSelect('SELECT DISTINCT h.hostid,h.host,h3.host AS parent_host' . ' FROM hosts h' . ' INNER JOIN host_discovery hd ON h.hostid=hd.hostid' . ' INNER JOIN hosts h2 ON h.templateid=h2.hostid' . ' INNER JOIN host_discovery hd2 ON h.hostid=hd.hostid' . ' INNER JOIN items i ON hd.parent_itemid=i.itemid' . ' INNER JOIN hosts h3 ON i.hostid=h3.hostid' . ' WHERE ' . dbConditionInt('hd.parent_itemid', $discoveryRuleIds)), 'hostid');
         if ($hostPrototypes) {
             DB::update('hosts', array('values' => array('templateid' => 0), 'where' => array('hostid' => array_keys($hostPrototypes))));
             DB::update('group_prototype', array('values' => array('templateid' => 0), 'where' => array('hostid' => array_keys($hostPrototypes))));
             foreach ($hostPrototypes as $hostPrototype) {
                 info(_s('Unlinked: Host prototype "%1$s" on "%2$s".', $hostPrototype['host'], $hostPrototype['parent_host']));
             }
         }
     }
     /* GRAPHS {{{ */
     $sqlFrom = ' graphs g,hosts h';
     $sqlWhere = ' EXISTS (' . 'SELECT ggi.graphid' . ' FROM graphs_items ggi,items ii' . ' WHERE ggi.graphid=g.templateid' . ' AND ii.itemid=ggi.itemid' . ' AND ' . dbConditionInt('ii.hostid', $templateids) . ')' . ' AND ' . dbConditionInt('g.flags', $flags);
     if (!is_null($targetids)) {
         $sqlFrom = ' graphs g,graphs_items gi,items i,hosts h';
         $sqlWhere .= ' AND ' . dbConditionInt('i.hostid', $targetids) . ' AND gi.itemid=i.itemid' . ' AND g.graphid=gi.graphid' . ' AND h.hostid=i.hostid';
     }
     $sql = 'SELECT DISTINCT g.graphid,g.name,g.flags,h.name as host' . ' FROM ' . $sqlFrom . ' WHERE ' . $sqlWhere;
     $dbGraphs = DBSelect($sql);
     $graphs = array(ZBX_FLAG_DISCOVERY_NORMAL => array(), ZBX_FLAG_DISCOVERY_PROTOTYPE => array());
     while ($graph = DBfetch($dbGraphs)) {
         $graphs[$graph['flags']][$graph['graphid']] = array('name' => $graph['name'], 'graphid' => $graph['graphid'], 'host' => $graph['host']);
     }
     if (!empty($graphs[ZBX_FLAG_DISCOVERY_PROTOTYPE])) {
         if ($clear) {
             $result = API::GraphPrototype()->delete(array_keys($graphs[ZBX_FLAG_DISCOVERY_PROTOTYPE]), true);
             if (!$result) {
                 self::exception(ZBX_API_ERROR_INTERNAL, _('Cannot unlink and clear graph prototypes'));
             }
         } else {
             DB::update('graphs', array('values' => array('templateid' => 0), 'where' => array('graphid' => array_keys($graphs[ZBX_FLAG_DISCOVERY_PROTOTYPE]))));
             foreach ($graphs[ZBX_FLAG_DISCOVERY_PROTOTYPE] as $graph) {
                 info(_s('Unlinked: Graph prototype "%1$s" on "%2$s".', $graph['name'], $graph['host']));
             }
         }
     }
     if (!empty($graphs[ZBX_FLAG_DISCOVERY_NORMAL])) {
         if ($clear) {
             $result = API::Graph()->delete(array_keys($graphs[ZBX_FLAG_DISCOVERY_NORMAL]), true);
             if (!$result) {
                 self::exception(ZBX_API_ERROR_INTERNAL, _('Cannot unlink and clear graphs.'));
             }
         } else {
             DB::update('graphs', array('values' => array('templateid' => 0), 'where' => array('graphid' => array_keys($graphs[ZBX_FLAG_DISCOVERY_NORMAL]))));
             foreach ($graphs[ZBX_FLAG_DISCOVERY_NORMAL] as $graph) {
                 info(_s('Unlinked: Graph "%1$s" on "%2$s".', $graph['name'], $graph['host']));
             }
         }
     }
     /* }}} GRAPHS */
     // http tests
     $sqlWhere = '';
     if (!is_null($targetids)) {
         $sqlWhere = ' AND ' . dbConditionInt('ht1.hostid', $targetids);
     }
     $sql = 'SELECT DISTINCT ht1.httptestid,ht1.name,h.name as host' . ' FROM httptest ht1' . ' INNER JOIN httptest ht2 ON ht2.httptestid=ht1.templateid' . ' INNER JOIN hosts h ON h.hostid=ht1.hostid' . ' WHERE ' . dbConditionInt('ht2.hostid', $templateids) . $sqlWhere;
     $dbHttpTests = DBSelect($sql);
     $httpTests = array();
     while ($httpTest = DBfetch($dbHttpTests)) {
         $httpTests[$httpTest['httptestid']] = array('name' => $httpTest['name'], 'host' => $httpTest['host']);
     }
     if (!empty($httpTests)) {
         if ($clear) {
             $result = API::HttpTest()->delete(array_keys($httpTests), true);
             if (!$result) {
                 self::exception(ZBX_API_ERROR_INTERNAL, _('Cannot unlink and clear Web scenarios.'));
             }
         } else {
             DB::update('httptest', array('values' => array('templateid' => 0), 'where' => array('httptestid' => array_keys($httpTests))));
             foreach ($httpTests as $httpTest) {
                 info(_s('Unlinked: Web scenario "%1$s" on "%2$s".', $httpTest['name'], $httpTest['host']));
             }
         }
     }
     /* APPLICATIONS {{{ */
     $sql = 'SELECT at.application_templateid,at.applicationid,h.name,h.host,h.hostid' . ' FROM applications a1,application_template at,applications a2,hosts h' . ' WHERE a1.applicationid=at.applicationid' . ' AND at.templateid=a2.applicationid' . ' AND ' . dbConditionInt('a2.hostid', $templateids) . ' AND a1.hostid=h.hostid';
     if ($targetids) {
         $sql .= ' AND ' . dbConditionInt('a1.hostid', $targetids);
     }
     $query = DBselect($sql);
     $applicationTemplates = array();
     while ($applicationTemplate = DBfetch($query)) {
         $applicationTemplates[] = array('applicationid' => $applicationTemplate['applicationid'], 'application_templateid' => $applicationTemplate['application_templateid'], 'name' => $applicationTemplate['name'], 'hostid' => $applicationTemplate['hostid'], 'host' => $applicationTemplate['host']);
     }
     if ($applicationTemplates) {
         // unlink applications from templates
         DB::delete('application_template', array('application_templateid' => zbx_objectValues($applicationTemplates, 'application_templateid')));
         if ($clear) {
             // delete inherited applications that are no longer linked to any templates
             $applications = DBfetchArray(DBselect('SELECT a.applicationid' . ' FROM applications a' . ' LEFT JOIN application_template at ON a.applicationid=at.applicationid ' . ' WHERE ' . dbConditionInt('a.applicationid', zbx_objectValues($applicationTemplates, 'applicationid')) . ' AND at.applicationid IS NULL'));
             $result = API::Application()->delete(zbx_objectValues($applications, 'applicationid'), true);
             if (!$result) {
                 self::exception(ZBX_API_ERROR_INTERNAL, _('Cannot unlink and clear applications.'));
             }
         } else {
             foreach ($applicationTemplates as $application) {
                 info(_s('Unlinked: Application "%1$s" on "%2$s".', $application['name'], $application['host']));
             }
         }
     }
     /* }}} APPLICATIONS */
     parent::unlink($templateids, $targetids);
 }
        $graph = reset($graphs);
        order_result($graph['hosts'], 'name');
        $graph['host'] = reset($graph['hosts']);
        $caption = $graph['host']['name'] . NAME_DELIMITER . $graph['name'];
    }
    if ($this->data['screen']['templateid']) {
        $selectButton = new CButton('select', _('Select'), 'javascript: return PopUp("popup.php?srctbl=graphs&srcfld1=graphid&srcfld2=name' . '&dstfrm=' . $screenForm->getName() . '&dstfld1=resourceid&dstfld2=caption' . '&templated_hosts=1&only_hostid=' . $this->data['screen']['templateid'] . '&writeonly=1", 800, 450);', 'formlist');
    } else {
        $selectButton = new CButton('select', _('Select'), 'javascript: return PopUp("popup.php?srctbl=graphs&srcfld1=graphid&srcfld2=name' . '&dstfrm=' . $screenForm->getName() . '&dstfld1=resourceid&dstfld2=caption' . '&real_hosts=1&with_graphs=1&writeonly=1", 800, 450);', 'formlist');
    }
    $screenFormList->addVar('resourceid', $id);
    $screenFormList->addRow(_('Graph name'), array(new CTextBox('caption', $caption, ZBX_TEXTBOX_STANDARD_SIZE, true), $selectButton), false, null, 'nowrap');
} elseif ($resourceType == SCREEN_RESOURCE_LLD_GRAPH) {
    $caption = '';
    $id = 0;
    $graphPrototypes = API::GraphPrototype()->get(array('output' => array('name'), 'graphids' => $resourceId, 'selectHosts' => array('name')));
    if ($graphPrototypes) {
        $id = $resourceId;
        $graphPrototype = reset($graphPrototypes);
        order_result($graphPrototype['hosts'], 'name');
        $graphPrototype['host'] = reset($graphPrototype['hosts']);
        $caption = $graphPrototype['host']['name'] . NAME_DELIMITER . $graphPrototype['name'];
    }
    if ($this->data['screen']['templateid']) {
        $selectButton = new CButton('select', _('Select'), 'javascript: return PopUp("popup.php?srctbl=graph_prototypes&srcfld1=graphid&srcfld2=name' . '&dstfrm=' . $screenForm->getName() . '&dstfld1=resourceid&dstfld2=caption' . '&only_hostid=' . $this->data['screen']['templateid'] . '&templated_hosts=1&writeonly=1", 800, 450);', 'formlist');
    } else {
        $selectButton = new CButton('select', _('Select'), 'javascript: return PopUp("popup.php?srctbl=graph_prototypes&srcfld1=graphid&srcfld2=name' . '&dstfrm=' . $screenForm->getName() . '&dstfld1=resourceid&dstfld2=caption' . '&real_hosts=1&writeonly=1", 800, 450);', 'formlist');
    }
    $screenFormList->addVar('resourceid', $id);
    $screenFormList->addRow(_('Graph name'), array(new CTextBox('caption', $caption, ZBX_TEXTBOX_STANDARD_SIZE, true), $selectButton), false, null, 'nowrap');
    $screenFormList->addRow(_('Max columns'), new CNumericBox('max_columns', $maxColumns, 3, false, false, false));
コード例 #4
0
 /**
  * Delete Item prototypes.
  *
  * @param int|string|array $prototypeids
  * @param bool             $nopermissions
  *
  * @return array
  */
 public function delete($prototypeids, $nopermissions = false)
 {
     if (empty($prototypeids)) {
         self::exception(ZBX_API_ERROR_PARAMETERS, _('Empty input parameter.'));
     }
     $prototypeids = zbx_toHash($prototypeids);
     $options = array('itemids' => $prototypeids, 'editable' => true, 'preservekeys' => true, 'output' => API_OUTPUT_EXTEND, 'selectHosts' => array('name'));
     $delItemPrototypes = $this->get($options);
     // TODO: remove $nopermissions hack
     if (!$nopermissions) {
         foreach ($prototypeids as $prototypeid) {
             if (!isset($delItemPrototypes[$prototypeid])) {
                 self::exception(ZBX_API_ERROR_PERMISSIONS, _('No permissions to referred object or it does not exist!'));
             }
             if ($delItemPrototypes[$prototypeid]['templateid'] != 0) {
                 self::exception(ZBX_API_ERROR_PARAMETERS, _('Cannot delete templated items'));
             }
         }
     }
     // first delete child items
     $parentItemids = $prototypeids;
     $childPrototypeids = array();
     do {
         $dbItems = DBselect('SELECT itemid FROM items WHERE ' . dbConditionInt('templateid', $parentItemids));
         $parentItemids = array();
         while ($dbItem = DBfetch($dbItems)) {
             $parentItemids[$dbItem['itemid']] = $dbItem['itemid'];
             $childPrototypeids[$dbItem['itemid']] = $dbItem['itemid'];
         }
     } while (!empty($parentItemids));
     $options = array('output' => API_OUTPUT_EXTEND, 'itemids' => $childPrototypeids, 'nopermissions' => true, 'preservekeys' => true, 'selectHosts' => array('name'));
     $delItemPrototypesChilds = $this->get($options);
     $delItemPrototypes = array_merge($delItemPrototypes, $delItemPrototypesChilds);
     $prototypeids = array_merge($prototypeids, $childPrototypeids);
     // delete graphs with this item prototype
     $delGraphPrototypes = API::GraphPrototype()->get(array('itemids' => $prototypeids, 'output' => API_OUTPUT_SHORTEN, 'nopermissions' => true, 'preservekeys' => true));
     if (!empty($delGraphPrototypes)) {
         $result = API::GraphPrototype()->delete(zbx_objectValues($delGraphPrototypes, 'graphid'), true);
         if (!$result) {
             self::exception(ZBX_API_ERROR_PARAMETERS, _('Cannot delete graph prototype'));
         }
     }
     // check if any graphs are referencing this item
     $this->checkGraphReference($prototypeids);
     // CREATED ITEMS
     $createdItems = array();
     $sql = 'SELECT itemid FROM item_discovery WHERE ' . dbConditionInt('parent_itemid', $prototypeids);
     $dbItems = DBselect($sql);
     while ($item = DBfetch($dbItems)) {
         $createdItems[$item['itemid']] = $item['itemid'];
     }
     if (!empty($createdItems)) {
         $result = API::Item()->delete($createdItems, true);
         if (!$result) {
             self::exception(ZBX_API_ERROR_PARAMETERS, _('Cannot delete items created by low level discovery.'));
         }
     }
     // TRIGGER PROTOTYPES
     $delTriggerPrototypes = API::TriggerPrototype()->get(array('itemids' => $prototypeids, 'output' => API_OUTPUT_SHORTEN, 'nopermissions' => true, 'preservekeys' => true));
     if (!empty($delTriggerPrototypes)) {
         $result = API::TriggerPrototype()->delete(zbx_objectValues($delTriggerPrototypes, 'triggerid'), true);
         if (!$result) {
             self::exception(ZBX_API_ERROR_PARAMETERS, _('Cannot delete trigger prototype'));
         }
     }
     // ITEM PROTOTYPES
     DB::delete('items', array('itemid' => $prototypeids));
     // TODO: remove info from API
     foreach ($delItemPrototypes as $item) {
         $host = reset($item['hosts']);
         info(_s('Deleted: Item prototype "%1$s" on "%2$s".', $item['name'], $host['name']));
     }
     return array('prototypeids' => $prototypeids);
 }
コード例 #5
0
 /**
  * Get discovery rules related objects from database.
  *
  * @param array $items
  *
  * @return array
  */
 protected function prepareDiscoveryRules(array $items)
 {
     foreach ($items as &$item) {
         $item['itemPrototypes'] = array();
         $item['graphPrototypes'] = array();
         $item['triggerPrototypes'] = array();
     }
     unset($item);
     // gather item prototypes
     $prototypes = API::ItemPrototype()->get(array('discoveryids' => zbx_objectValues($items, 'itemid'), 'output' => array('hostid', 'multiplier', 'type', 'snmp_community', 'snmp_oid', 'name', 'key_', 'delay', 'history', 'trends', 'status', 'value_type', 'trapper_hosts', 'units', 'delta', 'snmpv3_securityname', 'snmpv3_securitylevel', 'snmpv3_authpassphrase', 'snmpv3_privpassphrase', 'formula', 'valuemapid', 'delay_flex', 'params', 'ipmi_sensor', 'data_type', 'authtype', 'username', 'password', 'publickey', 'privatekey', 'interfaceid', 'port', 'description', 'inventory_link', 'flags'), 'selectApplications' => API_OUTPUT_EXTEND, 'inherited' => false, 'preservekeys' => true));
     // gather value maps
     $valueMapIds = zbx_objectValues($prototypes, 'valuemapid');
     $DbValueMaps = DBselect('SELECT vm.valuemapid, vm.name FROM valuemaps vm WHERE ' . dbConditionInt('vm.valuemapid', $valueMapIds));
     $valueMaps = array();
     while ($valueMap = DBfetch($DbValueMaps)) {
         $valueMaps[$valueMap['valuemapid']] = $valueMap['name'];
     }
     foreach ($prototypes as $prototype) {
         $prototype['valuemap'] = array();
         if ($prototype['valuemapid']) {
             $prototype['valuemap']['name'] = $valueMaps[$prototype['valuemapid']];
         }
         $items[$prototype['parent_itemid']]['itemPrototypes'][] = $prototype;
     }
     // gather graph prototypes
     $graphs = API::GraphPrototype()->get(array('discoveryids' => zbx_objectValues($items, 'itemid'), 'selectDiscoveryRule' => API_OUTPUT_EXTEND, 'selectGraphItems' => API_OUTPUT_EXTEND, 'output' => API_OUTPUT_EXTEND, 'inherited' => false, 'preservekeys' => true));
     $graphs = $this->prepareGraphs($graphs);
     foreach ($graphs as $graph) {
         $items[$graph['discoveryRule']['itemid']]['graphPrototypes'][] = $graph;
     }
     // gather trigger prototypes
     $triggers = API::TriggerPrototype()->get(array('discoveryids' => zbx_objectValues($items, 'itemid'), 'output' => API_OUTPUT_EXTEND, 'selectDiscoveryRule' => API_OUTPUT_EXTEND, 'selectItems' => array('flags', 'type'), 'inherited' => false, 'preservekeys' => true, 'expandData' => true));
     foreach ($triggers as $trigger) {
         foreach ($trigger['items'] as $item) {
             if ($item['flags'] == ZBX_FLAG_DISCOVERY_CREATED || $item['type'] == ITEM_TYPE_HTTPTEST) {
                 continue 2;
             }
         }
         $trigger['expression'] = explode_exp($trigger['expression']);
         $items[$trigger['discoveryRule']['itemid']]['triggerPrototypes'][] = $trigger;
     }
     return $items;
 }
コード例 #6
0
 /**
  * Copies all of the graphs from the source discovery to the target discovery rule.
  *
  * @throws APIException if graph saving fails
  *
  * @param array $srcDiscovery    The source discovery rule to copy from
  * @param array $dstDiscovery    The target discovery rule to copy to
  *
  * @return array
  */
 protected function copyGraphPrototypes(array $srcDiscovery, array $dstDiscovery)
 {
     // fetch source graphs
     $srcGraphs = API::GraphPrototype()->get(array('discoveryids' => $srcDiscovery['itemid'], 'output' => API_OUTPUT_EXTEND, 'selectGraphItems' => API_OUTPUT_EXTEND, 'selectHosts' => API_OUTPUT_REFER, 'preservekeys' => true));
     if (!$srcGraphs) {
         return array();
     }
     $srcItemIds = array();
     foreach ($srcGraphs as $key => $graph) {
         // skip graphs with items from multiple hosts
         if (count($graph['hosts']) > 1) {
             unset($srcGraphs[$key]);
             continue;
         }
         // skip graphs with http items
         if (httpItemExists($graph['gitems'])) {
             unset($srcGraphs[$key]);
             continue;
         }
         // save all used item ids to map them to the new items
         foreach ($graph['gitems'] as $item) {
             $srcItemIds[$item['itemid']] = $item['itemid'];
         }
         if ($graph['ymin_itemid']) {
             $srcItemIds[$graph['ymin_itemid']] = $graph['ymin_itemid'];
         }
         if ($graph['ymax_itemid']) {
             $srcItemIds[$graph['ymax_itemid']] = $graph['ymax_itemid'];
         }
     }
     // fetch source items
     $items = API::Item()->get(array('itemids' => $srcItemIds, 'output' => API_OUTPUT_EXTEND, 'preservekeys' => true, 'filter' => array('flags' => null)));
     $srcItems = array();
     $itemKeys = array();
     foreach ($items as $item) {
         $srcItems[$item['itemid']] = $item;
         $itemKeys[$item['key_']] = $item['key_'];
     }
     // fetch newly cloned items
     $newItems = API::Item()->get(array('hostids' => $dstDiscovery['hostid'], 'filter' => array('key_' => $itemKeys, 'flags' => null), 'output' => API_OUTPUT_EXTEND, 'preservekeys' => true));
     $items = array_merge($dstDiscovery['items'], $newItems);
     $dstItems = array();
     foreach ($items as $item) {
         $dstItems[$item['key_']] = $item;
     }
     $dstGraphs = $srcGraphs;
     foreach ($dstGraphs as &$graph) {
         unset($graph['graphid']);
         unset($graph['templateid']);
         foreach ($graph['gitems'] as &$gitem) {
             // replace the old item with the new one with the same key
             $item = $srcItems[$gitem['itemid']];
             $gitem['itemid'] = $dstItems[$item['key_']]['itemid'];
             unset($gitem['gitemid'], $gitem['graphid']);
         }
         // replace the old axis items with the new one with the same key
         if ($graph['ymin_itemid']) {
             $yMinSrcItem = $srcItems[$graph['ymin_itemid']];
             $graph['ymin_itemid'] = $dstItems[$yMinSrcItem['key_']]['itemid'];
         }
         if ($graph['ymax_itemid']) {
             $yMaxSrcItem = $srcItems[$graph['ymax_itemid']];
             $graph['ymax_itemid'] = $dstItems[$yMaxSrcItem['key_']]['itemid'];
         }
     }
     // save graphs
     $rs = API::Graph()->create($dstGraphs);
     if (!$rs) {
         self::exception(ZBX_API_ERROR_PARAMETERS, _('Cannot clone graph prototypes.'));
     }
     return $rs;
 }
コード例 #7
0
         // skip trigger prototypes with web items
         if (httpItemExists($triggerPrototype['items'])) {
             continue;
         }
         $prototypeList[$triggerPrototype['triggerid']] = $triggerPrototype['description'];
     }
     if ($prototypeList) {
         order_result($prototypeList);
         $listBox = new CListBox('triggerprototypes', null, 8);
         $listBox->setAttribute('disabled', 'disabled');
         $listBox->addItems($prototypeList);
         $hostList->addRow(_('Trigger prototypes'), $listBox);
     }
 }
 // Graph prototypes
 $hostGraphPrototypes = API::GraphPrototype()->get(array('hostids' => $_REQUEST['hostid'], 'discoveryids' => $hostDiscoveryRuleids, 'inherited' => false, 'selectHosts' => array('hostid'), 'output' => array('graphid', 'name')));
 if (!empty($hostGraphPrototypes)) {
     $prototypeList = array();
     foreach ($hostGraphPrototypes as $graphPrototype) {
         if (count($graphPrototype['hosts']) == 1) {
             $prototypeList[$graphPrototype['graphid']] = $graphPrototype['name'];
         }
     }
     order_result($prototypeList);
     $listBox = new CListBox('graphPrototypes', null, 8);
     $listBox->setAttribute('disabled', 'disabled');
     $listBox->addItems($prototypeList);
     $hostList->addRow(_('Graph prototypes'), $listBox);
 }
 // host prototypes
 $hostPrototypes = API::HostPrototype()->get(array('discoveryids' => $hostDiscoveryRuleids, 'inherited' => false, 'output' => array('hostid', 'name')));
コード例 #8
0
 /**
  * Validates screen items.
  *
  * If the $dbScreenItems parameter is given, the screen items will be matched
  * against the ones given in $dbScreenItems. If a screen item is not present in
  * $dbScreenItems, a ZBX_API_ERROR_PERMISSIONS exception will be thrown.
  *
  * @throws APIException if a validation error occurred.
  *
  * @param array $screenItems
  * @param array $dbScreenItems
  */
 protected function checkInput(array $screenItems, array $dbScreenItems = array())
 {
     $hostGroupsIds = array();
     $hostIds = array();
     $graphIds = array();
     $itemIds = array();
     $mapIds = array();
     $screenIds = array();
     $itemPrototypeIds = array();
     $graphPrototypeIds = array();
     $screenItems = $this->extendFromObjects($screenItems, $dbScreenItems, array('resourcetype'));
     $validStyles = array(SCREEN_RESOURCE_CLOCK => array(TIME_TYPE_LOCAL, TIME_TYPE_SERVER, TIME_TYPE_HOST), SCREEN_RESOURCE_DATA_OVERVIEW => array(STYLE_TOP, STYLE_LEFT), SCREEN_RESOURCE_TRIGGERS_OVERVIEW => array(STYLE_TOP, STYLE_LEFT), SCREEN_RESOURCE_HOSTS_INFO => array(STYLE_VERTICAL, STYLE_HORIZONTAL), SCREEN_RESOURCE_TRIGGERS_INFO => array(STYLE_VERTICAL, STYLE_HORIZONTAL));
     foreach ($screenItems as $screenItem) {
         // check permissions
         if (isset($screenItem['screenitemid']) && !isset($dbScreenItems[$screenItem['screenitemid']])) {
             self::exception(ZBX_API_ERROR_PERMISSIONS, _('No permissions to referred object or it does not exist!'));
         }
         if (!$this->isValidResourceType($screenItem['resourcetype'])) {
             self::exception(ZBX_API_ERROR_PARAMETERS, _('Incorrect resource type provided for screen item.'));
         }
         if (!isset($screenItem['resourceid'])) {
             $screenItem['resourceid'] = null;
         }
         // check resource id
         switch ($screenItem['resourcetype']) {
             case SCREEN_RESOURCE_HOSTS_INFO:
             case SCREEN_RESOURCE_TRIGGERS_INFO:
             case SCREEN_RESOURCE_TRIGGERS_OVERVIEW:
             case SCREEN_RESOURCE_HOSTGROUP_TRIGGERS:
             case SCREEN_RESOURCE_DATA_OVERVIEW:
                 $overviewResources = array(SCREEN_RESOURCE_TRIGGERS_OVERVIEW, SCREEN_RESOURCE_DATA_OVERVIEW);
                 if (in_array($screenItem['resourcetype'], $overviewResources)) {
                     if (!$screenItem['resourceid']) {
                         self::exception(ZBX_API_ERROR_PARAMETERS, _('No host group ID provided for screen element.'));
                     }
                 }
                 if ($screenItem['resourceid']) {
                     $hostGroupsIds[$screenItem['resourceid']] = $screenItem['resourceid'];
                 }
                 break;
             case SCREEN_RESOURCE_HOST_TRIGGERS:
                 if ($screenItem['resourceid']) {
                     $hostIds[$screenItem['resourceid']] = $screenItem['resourceid'];
                 }
                 break;
             case SCREEN_RESOURCE_GRAPH:
                 if (!$screenItem['resourceid']) {
                     self::exception(ZBX_API_ERROR_PARAMETERS, _('No graph ID provided for screen element.'));
                 }
                 $graphIds[$screenItem['resourceid']] = $screenItem['resourceid'];
                 break;
             case SCREEN_RESOURCE_LLD_GRAPH:
                 if (!$screenItem['resourceid']) {
                     self::exception(ZBX_API_ERROR_PARAMETERS, _('No graph prototype ID provided for screen element.'));
                 }
                 $graphPrototypeIds[$screenItem['resourceid']] = $screenItem['resourceid'];
                 break;
             case SCREEN_RESOURCE_LLD_SIMPLE_GRAPH:
                 if (!$screenItem['resourceid']) {
                     self::exception(ZBX_API_ERROR_PARAMETERS, _('No item prototype ID provided for screen element.'));
                 }
                 $itemPrototypeIds[$screenItem['resourceid']] = $screenItem['resourceid'];
                 break;
             case SCREEN_RESOURCE_SIMPLE_GRAPH:
             case SCREEN_RESOURCE_PLAIN_TEXT:
                 if (!$screenItem['resourceid']) {
                     self::exception(ZBX_API_ERROR_PARAMETERS, _('No item ID provided for screen element.'));
                 }
                 $itemIds[$screenItem['resourceid']] = $screenItem['resourceid'];
                 break;
             case SCREEN_RESOURCE_CLOCK:
                 if (isset($screenItem['style'])) {
                     if ($screenItem['style'] == TIME_TYPE_HOST) {
                         if (!$screenItem['resourceid']) {
                             self::exception(ZBX_API_ERROR_PARAMETERS, _('No item ID provided for screen element.'));
                         }
                         $itemIds[$screenItem['resourceid']] = $screenItem['resourceid'];
                     } elseif ($screenItem['resourceid']) {
                         self::exception(ZBX_API_ERROR_PARAMETERS, _('Cannot set resource ID for screen element.'));
                     }
                 }
                 break;
             case SCREEN_RESOURCE_MAP:
                 if (!$screenItem['resourceid']) {
                     self::exception(ZBX_API_ERROR_PARAMETERS, _('No map ID provided for screen element.'));
                 }
                 $mapIds[$screenItem['resourceid']] = $screenItem['resourceid'];
                 break;
             case SCREEN_RESOURCE_SCREEN:
                 if (!$screenItem['resourceid']) {
                     self::exception(ZBX_API_ERROR_PARAMETERS, _('No screen ID provided for screen element.'));
                 }
                 $screenIds[$screenItem['resourceid']] = $screenItem['resourceid'];
                 break;
             case SCREEN_RESOURCE_ACTIONS:
             case SCREEN_RESOURCE_SERVER_INFO:
             case SCREEN_RESOURCE_SYSTEM_STATUS:
             case SCREEN_RESOURCE_URL:
                 if ($screenItem['resourceid']) {
                     self::exception(ZBX_API_ERROR_PARAMETERS, _('Cannot set resource ID for screen element.'));
                 }
                 break;
         }
         // check url
         if ($screenItem['resourcetype'] == SCREEN_RESOURCE_URL) {
             if (!isset($screenItem['url']) || zbx_empty($screenItem['url'])) {
                 self::exception(ZBX_API_ERROR_PARAMETERS, _('No URL provided for screen element.'));
             }
         }
         // check "Show lines"
         if (isset($screenItem['elements'])) {
             switch ($screenItem['resourcetype']) {
                 case SCREEN_RESOURCE_ACTIONS:
                 case SCREEN_RESOURCE_EVENTS:
                 case SCREEN_RESOURCE_HOSTGROUP_TRIGGERS:
                 case SCREEN_RESOURCE_HOST_TRIGGERS:
                 case SCREEN_RESOURCE_PLAIN_TEXT:
                     if ($screenItem['elements'] < 1 || $screenItem['elements'] > 100) {
                         self::exception(ZBX_API_ERROR_PARAMETERS, _s('Incorrect value "%1$s" for "%2$s" field: must be between %3$s and %4$s.', $screenItem['elements'], 'elements', 1, 100));
                     }
                     break;
             }
         }
         // check 'max_columns' parameter for LLD screen resources:
         // is set and valid for create method, and is valid for update method, if set
         $dbScreenItem = isset($screenItem['screenitemid']) ? $dbScreenItems[$screenItem['screenitemid']] : null;
         $lldResources = array(SCREEN_RESOURCE_LLD_GRAPH, SCREEN_RESOURCE_LLD_SIMPLE_GRAPH);
         if (in_array($screenItem['resourcetype'], $lldResources)) {
             $set = isset($screenItem['max_columns']);
             $valid = $set && $this->isValidMaxColumns($screenItem['max_columns']);
             $error = $dbScreenItem ? $set && !$valid : !$set || !$valid;
             if ($error) {
                 self::exception(ZBX_API_ERROR_PARAMETERS, _('Incorrect max columns provided for screen element.'));
             }
         }
         if (isset($validStyles[$screenItem['resourcetype']]) && !in_array($screenItem['style'], $validStyles[$screenItem['resourcetype']])) {
             self::exception(ZBX_API_ERROR_PARAMETERS, _('Incorrect style provided for screen element.'));
         }
     }
     // check host groups
     if ($hostGroupsIds) {
         $dbHostGroups = API::HostGroup()->get(array('output' => array('groupid'), 'groupids' => $hostGroupsIds, 'editable' => true, 'preservekeys' => true));
         foreach ($hostGroupsIds as $hostGroupsId) {
             if (!isset($dbHostGroups[$hostGroupsId])) {
                 self::exception(ZBX_API_ERROR_PERMISSIONS, _s('Incorrect host group ID "%1$s" provided for screen element.', $hostGroupsId));
             }
         }
     }
     // check hosts
     if ($hostIds) {
         $dbHosts = API::Host()->get(array('output' => array('hostid'), 'hostids' => $hostIds, 'editable' => true, 'preservekeys' => true));
         foreach ($hostIds as $hostId) {
             if (!isset($dbHosts[$hostId])) {
                 self::exception(ZBX_API_ERROR_PERMISSIONS, _s('Incorrect host ID "%1$s" provided for screen element.', $hostId));
             }
         }
     }
     // check graphs
     if ($graphIds) {
         $dbGraphs = API::Graph()->get(array('output' => array('graphid'), 'graphids' => $graphIds, 'editable' => true, 'preservekeys' => true));
         foreach ($graphIds as $graphId) {
             if (!isset($dbGraphs[$graphId])) {
                 self::exception(ZBX_API_ERROR_PERMISSIONS, _s('Incorrect graph ID "%1$s" provided for screen element.', $graphId));
             }
         }
     }
     // check graph prototypes
     if ($graphPrototypeIds) {
         $dbGraphPrototypes = API::GraphPrototype()->get(array('output' => array('graphid'), 'graphids' => $graphPrototypeIds, 'editable' => true, 'preservekeys' => true));
         foreach ($graphPrototypeIds as $graphPrototypeId) {
             if (!isset($dbGraphPrototypes[$graphPrototypeId])) {
                 self::exception(ZBX_API_ERROR_PERMISSIONS, _s('Incorrect graph prototype ID "%1$s" provided for screen element.', $graphPrototypeId));
             }
         }
     }
     // check items
     if ($itemIds) {
         $dbItems = API::Item()->get(array('output' => array('itemid'), 'itemids' => $itemIds, 'editable' => true, 'preservekeys' => true, 'webitems' => true));
         foreach ($itemIds as $itemId) {
             if (!isset($dbItems[$itemId])) {
                 self::exception(ZBX_API_ERROR_PERMISSIONS, _s('Incorrect item ID "%1$s" provided for screen element.', $itemId));
             }
         }
     }
     // check item prototypes
     if ($itemPrototypeIds) {
         $dbItemPrototypes = API::ItemPrototype()->get(array('output' => array('itemid'), 'itemids' => $itemPrototypeIds, 'editable' => true, 'preservekeys' => true));
         foreach ($itemPrototypeIds as $itemPrototypeId) {
             if (!isset($dbItemPrototypes[$itemPrototypeId])) {
                 self::exception(ZBX_API_ERROR_PERMISSIONS, _s('Incorrect item prototype ID "%1$s" provided for screen element.', $itemPrototypeId));
             }
         }
     }
     // check maps
     if ($mapIds) {
         $dbMaps = API::Map()->get(array('output' => array('sysmapid'), 'sysmapids' => $mapIds, 'editable' => true, 'preservekeys' => true));
         foreach ($mapIds as $mapId) {
             if (!isset($dbMaps[$mapId])) {
                 self::exception(ZBX_API_ERROR_PERMISSIONS, _s('Incorrect map ID "%1$s" provided for screen element.', $mapId));
             }
         }
     }
     // check screens
     if ($screenIds) {
         $dbScreens = API::Screen()->get(array('output' => array('screenid'), 'screenids' => $screenIds, 'editable' => true, 'preservekeys' => true));
         if (count($dbScreens) < count($screenIds)) {
             $dbTemplateScreens = API::TemplateScreen()->get(array('output' => array('screenid'), 'screenids' => $screenIds, 'editable' => true, 'preservekeys' => true));
             if ($dbTemplateScreens) {
                 $dbScreens = zbx_array_merge($dbScreens, $dbTemplateScreens);
             }
         }
         foreach ($screenIds as $screenId) {
             if (!isset($dbScreens[$screenId])) {
                 self::exception(ZBX_API_ERROR_PERMISSIONS, _s('Incorrect screen ID "%1$s" provided for screen element.', $screenId));
             }
         }
     }
 }
コード例 #9
0
        $graph = reset($graphs);
        order_result($graph['hosts'], 'name');
        $graph['host'] = reset($graph['hosts']);
        $caption = $graph['host']['name'] . NAME_DELIMITER . $graph['name'];
    }
    if ($this->data['screen']['templateid']) {
        $selectButton = (new CButton('select', _('Select')))->addClass(ZBX_STYLE_BTN_GREY)->onClick('javascript: return PopUp("popup.php?srctbl=graphs&srcfld1=graphid&srcfld2=name' . '&dstfrm=' . $form->getName() . '&dstfld1=resourceid&dstfld2=caption' . '&templated_hosts=1&only_hostid=' . $this->data['screen']['templateid'] . '");');
    } else {
        $selectButton = (new CButton('select', _('Select')))->addClass(ZBX_STYLE_BTN_GREY)->onClick('javascript: return PopUp("popup.php?srctbl=graphs&srcfld1=graphid&srcfld2=name' . '&dstfrm=' . $form->getName() . '&dstfld1=resourceid&dstfld2=caption' . '&real_hosts=1&with_graphs=1");');
    }
    $form->addVar('resourceid', $id);
    $screenFormList->addRow(_('Graph'), [(new CTextBox('caption', $caption, true))->setWidth(ZBX_TEXTAREA_STANDARD_WIDTH), (new CDiv())->addClass(ZBX_STYLE_FORM_INPUT_MARGIN), $selectButton]);
} elseif ($resourceType == SCREEN_RESOURCE_LLD_GRAPH) {
    $caption = '';
    $id = 0;
    $graphPrototypes = API::GraphPrototype()->get(['output' => ['name'], 'graphids' => $resourceId, 'selectHosts' => ['name']]);
    if ($graphPrototypes) {
        $id = $resourceId;
        $graphPrototype = reset($graphPrototypes);
        order_result($graphPrototype['hosts'], 'name');
        $graphPrototype['host'] = reset($graphPrototype['hosts']);
        $caption = $graphPrototype['host']['name'] . NAME_DELIMITER . $graphPrototype['name'];
    }
    if ($this->data['screen']['templateid']) {
        $selectButton = (new CButton('select', _('Select')))->addClass(ZBX_STYLE_BTN_GREY)->onClick('javascript: return PopUp("popup.php?srctbl=graph_prototypes&srcfld1=graphid&srcfld2=name' . '&dstfrm=' . $form->getName() . '&dstfld1=resourceid&dstfld2=caption' . '&only_hostid=' . $this->data['screen']['templateid'] . '&templated_hosts=1");');
    } else {
        $selectButton = (new CButton('select', _('Select')))->addClass(ZBX_STYLE_BTN_GREY)->onClick('javascript: return PopUp("popup.php?srctbl=graph_prototypes&srcfld1=graphid&srcfld2=name' . '&dstfrm=' . $form->getName() . '&dstfld1=resourceid&dstfld2=caption' . '&real_hosts=1");');
    }
    $form->addVar('resourceid', $id);
    $screenFormList->addRow(_('Graph prototype'), [(new CTextBox('caption', $caption, true))->setWidth(ZBX_TEXTAREA_STANDARD_WIDTH), (new CDiv())->addClass(ZBX_STYLE_FORM_INPUT_MARGIN), $selectButton]);
    $screenFormList->addRow(_('Max columns'), (new CNumericBox('max_columns', $maxColumns, 3, false, false, false))->setWidth(ZBX_TEXTAREA_NUMERIC_STANDARD_WIDTH));
コード例 #10
0
 protected function addRelatedObjects(array $options, array $result)
 {
     $result = parent::addRelatedObjects($options, $result);
     $itemIds = array_keys($result);
     // adding items
     if (!is_null($options['selectItems'])) {
         if ($options['selectItems'] != API_OUTPUT_COUNT) {
             $relationMap = $this->createRelationMap($result, 'parent_itemid', 'itemid', 'item_discovery');
             $items = API::ItemPrototype()->get(array('output' => $options['selectItems'], 'itemids' => $relationMap->getRelatedIds(), 'nopermissions' => true, 'preservekeys' => true));
             $result = $relationMap->mapMany($result, $items, 'items', $options['limitSelects']);
         } else {
             $items = API::ItemPrototype()->get(array('discoveryids' => $itemIds, 'nopermissions' => true, 'countOutput' => true, 'groupCount' => true));
             $items = zbx_toHash($items, 'parent_itemid');
             foreach ($result as $itemid => $item) {
                 $result[$itemid]['items'] = isset($items[$itemid]) ? $items[$itemid]['rowscount'] : 0;
             }
         }
     }
     // adding triggers
     if (!is_null($options['selectTriggers'])) {
         if ($options['selectTriggers'] != API_OUTPUT_COUNT) {
             $relationMap = new CRelationMap();
             $res = DBselect('SELECT id.parent_itemid,f.triggerid' . ' FROM item_discovery id,items i,functions f' . ' WHERE ' . dbConditionInt('id.parent_itemid', $itemIds) . ' AND id.itemid=i.itemid' . ' AND i.itemid=f.itemid');
             while ($relation = DBfetch($res)) {
                 $relationMap->addRelation($relation['parent_itemid'], $relation['triggerid']);
             }
             $triggers = API::TriggerPrototype()->get(array('output' => $options['selectTriggers'], 'triggerids' => $relationMap->getRelatedIds(), 'preservekeys' => true));
             $result = $relationMap->mapMany($result, $triggers, 'triggers', $options['limitSelects']);
         } else {
             $triggers = API::TriggerPrototype()->get(array('discoveryids' => $itemIds, 'countOutput' => true, 'groupCount' => true));
             $triggers = zbx_toHash($triggers, 'parent_itemid');
             foreach ($result as $itemid => $item) {
                 $result[$itemid]['triggers'] = isset($triggers[$itemid]) ? $triggers[$itemid]['rowscount'] : 0;
             }
         }
     }
     // adding graphs
     if (!is_null($options['selectGraphs'])) {
         if ($options['selectGraphs'] != API_OUTPUT_COUNT) {
             $relationMap = new CRelationMap();
             $res = DBselect('SELECT id.parent_itemid,gi.graphid' . ' FROM item_discovery id,items i,graphs_items gi' . ' WHERE ' . dbConditionInt('id.parent_itemid', $itemIds) . ' AND id.itemid=i.itemid' . ' AND i.itemid=gi.itemid');
             while ($relation = DBfetch($res)) {
                 $relationMap->addRelation($relation['parent_itemid'], $relation['graphid']);
             }
             $graphs = API::GraphPrototype()->get(array('output' => $options['selectGraphs'], 'graphids' => $relationMap->getRelatedIds(), 'preservekeys' => true));
             $result = $relationMap->mapMany($result, $graphs, 'graphs', $options['limitSelects']);
         } else {
             $graphs = API::GraphPrototype()->get(array('discoveryids' => $itemIds, 'countOutput' => true, 'groupCount' => true));
             $graphs = zbx_toHash($graphs, 'parent_itemid');
             foreach ($result as $itemid => $item) {
                 $result[$itemid]['graphs'] = isset($graphs[$itemid]) ? $graphs[$itemid]['rowscount'] : 0;
             }
         }
     }
     // adding hosts
     if ($options['selectHostPrototypes'] !== null) {
         if ($options['selectHostPrototypes'] != API_OUTPUT_COUNT) {
             $relationMap = $this->createRelationMap($result, 'parent_itemid', 'hostid', 'host_discovery');
             $hostPrototypes = API::HostPrototype()->get(array('output' => $options['selectHostPrototypes'], 'hostids' => $relationMap->getRelatedIds(), 'nopermissions' => true, 'preservekeys' => true));
             $result = $relationMap->mapMany($result, $hostPrototypes, 'hostPrototypes', $options['limitSelects']);
         } else {
             $hostPrototypes = API::HostPrototype()->get(array('discoveryids' => $itemIds, 'nopermissions' => true, 'countOutput' => true, 'groupCount' => true));
             $hostPrototypes = zbx_toHash($hostPrototypes, 'parent_itemid');
             foreach ($result as $itemid => $item) {
                 $result[$itemid]['hostPrototypes'] = isset($hostPrototypes[$itemid]) ? $hostPrototypes[$itemid]['rowscount'] : 0;
             }
         }
     }
     if ($options['selectFilter'] !== null) {
         $formulaRequested = $this->outputIsRequested('formula', $options['selectFilter']);
         $evalFormulaRequested = $this->outputIsRequested('eval_formula', $options['selectFilter']);
         $conditionsRequested = $this->outputIsRequested('conditions', $options['selectFilter']);
         $filters = array();
         foreach ($result as $rule) {
             $filters[$rule['itemid']] = array('evaltype' => $rule['evaltype'], 'formula' => isset($rule['formula']) ? $rule['formula'] : '');
         }
         // adding conditions
         if ($formulaRequested || $evalFormulaRequested || $conditionsRequested) {
             $conditions = API::getApiService()->select('item_condition', array('output' => array('item_conditionid', 'macro', 'value', 'itemid', 'operator'), 'filter' => array('itemid' => $itemIds), 'preservekeys' => true, 'sortfield' => 'item_conditionid'));
             $relationMap = $this->createRelationMap($conditions, 'itemid', 'item_conditionid');
             $filters = $relationMap->mapMany($filters, $conditions, 'conditions');
             foreach ($filters as &$filter) {
                 // in case of a custom expression - use the given formula
                 if ($filter['evaltype'] == CONDITION_EVAL_TYPE_EXPRESSION) {
                     $formula = $filter['formula'];
                 } else {
                     // sort the conditions by macro before generating the formula
                     $conditions = zbx_toHash($filter['conditions'], 'item_conditionid');
                     $conditions = order_macros($conditions, 'macro');
                     $formulaConditions = array();
                     foreach ($conditions as $condition) {
                         $formulaConditions[$condition['item_conditionid']] = $condition['macro'];
                     }
                     $formula = CConditionHelper::getFormula($formulaConditions, $filter['evaltype']);
                 }
                 // generate formulaids from the effective formula
                 $formulaIds = CConditionHelper::getFormulaIds($formula);
                 foreach ($filter['conditions'] as &$condition) {
                     $condition['formulaid'] = $formulaIds[$condition['item_conditionid']];
                 }
                 unset($condition);
                 // generated a letter based formula only for rules with custom expressions
                 if ($formulaRequested && $filter['evaltype'] == CONDITION_EVAL_TYPE_EXPRESSION) {
                     $filter['formula'] = CConditionHelper::replaceNumericIds($formula, $formulaIds);
                 }
                 if ($evalFormulaRequested) {
                     $filter['eval_formula'] = CConditionHelper::replaceNumericIds($formula, $formulaIds);
                 }
             }
             unset($filter);
         }
         // add filters to the result
         foreach ($result as &$rule) {
             $rule['filter'] = $filters[$rule['itemid']];
         }
         unset($rule);
     }
     return $result;
 }
コード例 #11
0
ファイル: popup.php プロジェクト: TonywalkerCN/Zabbix
 if ($multiselect) {
     $header = array(array(new CCheckBox('all_graphs', null, "javascript: checkAll('" . $form->getName() . "', 'all_graphs', 'graphs');"), _('Description')), _('Graph type'));
 } else {
     $header = array(_('Name'), _('Graph type'));
 }
 $table->setHeader($header);
 if ($pageFilter->hostsSelected) {
     $options = array('output' => API_OUTPUT_EXTEND, 'hostids' => $hostid, 'selectHosts' => array('name'), 'preservekeys' => true);
     if (!is_null($writeonly)) {
         $options['editable'] = true;
     }
     if (!is_null($templated)) {
         $options['templated'] = $templated;
     }
     if ($graphPrototypesPopup) {
         $graphs = API::GraphPrototype()->get($options);
     } else {
         $graphs = API::Graph()->get($options);
     }
     order_result($graphs, 'name');
 } else {
     $graphs = array();
 }
 foreach ($graphs as $graph) {
     $host = reset($graph['hosts']);
     $graph['hostname'] = $host['name'];
     $description = new CSpan($graph['name'], 'link');
     $graph['name'] = $graph['hostname'] . NAME_DELIMITER . $graph['name'];
     if ($multiselect) {
         $js_action = 'javascript: addValue(' . zbx_jsvalue($reference) . ', ' . zbx_jsvalue($graph['graphid']) . ');';
     } else {
コード例 #12
0
            // skip trigger prototypes with web items
            if (httpItemExists($triggerPrototype['items'])) {
                continue;
            }
            $prototypeList[$triggerPrototype['triggerid']] = $triggerPrototype['description'];
        }
        if ($prototypeList) {
            order_result($prototypeList);
            $listBox = new CListBox('triggerprototypes', null, 8);
            $listBox->setAttribute('disabled', 'disabled');
            $listBox->addItems($prototypeList);
            $hostList->addRow(_('Trigger prototypes'), $listBox);
        }
    }
    // Graph prototypes
    $hostGraphPrototypes = API::GraphPrototype()->get(array('hostids' => $_REQUEST['hostid'], 'discoveryids' => $hostDiscoveryRuleids, 'inherited' => false, 'selectHosts' => API_OUTPUT_EXTEND, 'output' => API_OUTPUT_EXTEND));
    if (!empty($hostGraphPrototypes)) {
        $prototypeList = array();
        foreach ($hostGraphPrototypes as $graphPrototype) {
            if (count($graphPrototype['hosts']) == 1) {
                $prototypeList[$graphPrototype['graphid']] = $graphPrototype['name'];
            }
        }
        order_result($prototypeList);
        $listBox = new CListBox('graphPrototypes', null, 8);
        $listBox->setAttribute('disabled', 'disabled');
        $listBox->addItems($prototypeList);
        $hostList->addRow(_('Graph prototypes'), $listBox);
    }
}
$divTabs->addTab('hostTab', _('Host'), $hostList);
コード例 #13
0
 public function addRelatedObjects(array $options, array $result)
 {
     $result = parent::addRelatedObjects($options, $result);
     $itemids = array_keys($result);
     // adding applications
     if ($options['selectApplications'] !== null && $options['selectApplications'] != API_OUTPUT_COUNT) {
         $relationMap = $this->createRelationMap($result, 'itemid', 'applicationid', 'items_applications');
         $applications = API::Application()->get(['output' => $options['selectApplications'], 'applicationids' => $relationMap->getRelatedIds(), 'preservekeys' => true]);
         $result = $relationMap->mapMany($result, $applications, 'applications');
     }
     // adding application prototypes
     if ($options['selectApplicationPrototypes'] !== null && $options['selectApplicationPrototypes'] != API_OUTPUT_COUNT) {
         $pkFieldId = $this->pk('application_prototype');
         $outputFields = [$pkFieldId => $this->fieldId($pkFieldId, 'ap')];
         if (is_array($options['selectApplicationPrototypes'])) {
             foreach ($options['selectApplicationPrototypes'] as $field) {
                 if ($this->hasField($field, 'application_prototype')) {
                     $outputFields[$field] = $this->fieldId($field, 'ap');
                 }
             }
             $outputFields = implode(',', $outputFields);
         } else {
             $outputFields = 'ap.*';
         }
         $relationMap = $this->createRelationMap($result, 'itemid', 'application_prototypeid', 'item_application_prototype');
         $application_prototypes = DBfetchArray(DBselect('SELECT ' . $outputFields . ' FROM application_prototype ap' . ' WHERE ' . dbConditionInt('ap.application_prototypeid', $relationMap->getRelatedIds())));
         $application_prototypes = zbx_toHash($application_prototypes, 'application_prototypeid');
         $result = $relationMap->mapMany($result, $application_prototypes, 'applicationPrototypes');
     }
     // adding triggers
     if (!is_null($options['selectTriggers'])) {
         if ($options['selectTriggers'] != API_OUTPUT_COUNT) {
             $relationMap = $this->createRelationMap($result, 'itemid', 'triggerid', 'functions');
             $triggers = API::TriggerPrototype()->get(['output' => $options['selectTriggers'], 'triggerids' => $relationMap->getRelatedIds(), 'preservekeys' => true]);
             if (!is_null($options['limitSelects'])) {
                 order_result($triggers, 'description');
             }
             $result = $relationMap->mapMany($result, $triggers, 'triggers', $options['limitSelects']);
         } else {
             $triggers = API::TriggerPrototype()->get(['countOutput' => true, 'groupCount' => true, 'itemids' => $itemids]);
             $triggers = zbx_toHash($triggers, 'itemid');
             foreach ($result as $itemid => $item) {
                 if (isset($triggers[$itemid])) {
                     $result[$itemid]['triggers'] = $triggers[$itemid]['rowscount'];
                 } else {
                     $result[$itemid]['triggers'] = 0;
                 }
             }
         }
     }
     // adding graphs
     if (!is_null($options['selectGraphs'])) {
         if ($options['selectGraphs'] != API_OUTPUT_COUNT) {
             $relationMap = $this->createRelationMap($result, 'itemid', 'graphid', 'graphs_items');
             $graphs = API::GraphPrototype()->get(['output' => $options['selectGraphs'], 'graphids' => $relationMap->getRelatedIds(), 'preservekeys' => true]);
             if (!is_null($options['limitSelects'])) {
                 order_result($graphs, 'name');
             }
             $result = $relationMap->mapMany($result, $graphs, 'graphs', $options['limitSelects']);
         } else {
             $graphs = API::GraphPrototype()->get(['countOutput' => true, 'groupCount' => true, 'itemids' => $itemids]);
             $graphs = zbx_toHash($graphs, 'itemid');
             foreach ($result as $itemid => $item) {
                 if (isset($graphs[$itemid])) {
                     $result[$itemid]['graphs'] = $graphs[$itemid]['rowscount'];
                 } else {
                     $result[$itemid]['graphs'] = 0;
                 }
             }
         }
     }
     // adding discoveryrule
     if ($options['selectDiscoveryRule'] !== null && $options['selectDiscoveryRule'] != API_OUTPUT_COUNT) {
         $relationMap = $this->createRelationMap($result, 'itemid', 'parent_itemid', 'item_discovery');
         $discoveryRules = API::DiscoveryRule()->get(['output' => $options['selectDiscoveryRule'], 'itemids' => $relationMap->getRelatedIds(), 'nopermissions' => true, 'preservekeys' => true]);
         $result = $relationMap->mapOne($result, $discoveryRules, 'discoveryRule');
     }
     return $result;
 }
コード例 #14
0
         $listBox = (new CListBox('itemsPrototypes', null, 8))->setAttribute('disabled', 'disabled')->addItems($prototypeList);
         $templateList->addRow(_('Item prototypes'), $listBox);
     }
     // Trigger prototypes
     $hostTriggerPrototypes = API::TriggerPrototype()->get(['hostids' => $data['templateId'], 'discoveryids' => $hostDiscoveryRuleids, 'inherited' => false, 'output' => API_OUTPUT_EXTEND]);
     if (!empty($hostTriggerPrototypes)) {
         $prototypeList = [];
         foreach ($hostTriggerPrototypes as $triggerPrototype) {
             $prototypeList[$triggerPrototype['triggerid']] = $triggerPrototype['description'];
         }
         order_result($prototypeList);
         $listBox = (new CListBox('triggerprototypes', null, 8))->setAttribute('disabled', 'disabled')->addItems($prototypeList);
         $templateList->addRow(_('Trigger prototypes'), $listBox);
     }
     // Graph prototypes
     $hostGraphPrototypes = API::GraphPrototype()->get(['hostids' => $data['templateId'], 'discoveryids' => $hostDiscoveryRuleids, 'inherited' => false, 'output' => API_OUTPUT_EXTEND]);
     if (!empty($hostGraphPrototypes)) {
         $prototypeList = [];
         foreach ($hostGraphPrototypes as $graphPrototype) {
             $prototypeList[$graphPrototype['graphid']] = $graphPrototype['name'];
         }
         order_result($prototypeList);
         $listBox = (new CListBox('graphPrototypes', null, 8))->setAttribute('disabled', 'disabled')->addItems($prototypeList);
         $templateList->addRow(_('Graph prototypes'), $listBox);
     }
 }
 // screens
 $screens = API::TemplateScreen()->get(['inherited' => false, 'templateids' => $data['templateId'], 'output' => ['screenid', 'name']]);
 if (!empty($screens)) {
     $screensList = [];
     foreach ($screens as $screen) {
コード例 #15
0
 /**
  * Resolves and retrieves effective graph prototype used in this screen item.
  *
  * @return array|bool
  */
 protected function getGraphPrototype()
 {
     if ($this->graphPrototype === null) {
         $defaultOptions = ['output' => ['graphid', 'name', 'graphtype', 'show_legend', 'show_3d', 'templated'], 'selectDiscoveryRule' => ['hostid']];
         $options = [];
         /*
          * If screen item is dynamic or is templated screen, real graph prototype is looked up by "name"
          * used as resource ID for this screen item and by current host.
          */
         if (($this->screenitem['dynamic'] == SCREEN_DYNAMIC_ITEM || $this->isTemplatedScreen) && $this->hostid) {
             $currentGraphPrototype = API::GraphPrototype()->get(['output' => ['name'], 'graphids' => [$this->screenitem['resourceid']]]);
             $currentGraphPrototype = reset($currentGraphPrototype);
             $options['hostids'] = [$this->hostid];
             $options['filter'] = ['name' => $currentGraphPrototype['name']];
         } else {
             $options['graphids'] = [$this->screenitem['resourceid']];
         }
         $options = zbx_array_merge($defaultOptions, $options);
         $selectedGraphPrototype = API::GraphPrototype()->get($options);
         $this->graphPrototype = reset($selectedGraphPrototype);
     }
     return $this->graphPrototype;
 }
コード例 #16
0
 /**
  * Get discovery rules related objects from database.
  *
  * @param array $items
  *
  * @return array
  */
 protected function prepareDiscoveryRules(array $items)
 {
     foreach ($items as &$item) {
         $item['itemPrototypes'] = [];
         $item['graphPrototypes'] = [];
         $item['triggerPrototypes'] = [];
         $item['hostPrototypes'] = [];
         // unset unnecessary condition fields
         foreach ($item['filter']['conditions'] as &$condition) {
             unset($condition['item_conditionid'], $condition['itemid']);
         }
         unset($condition);
     }
     unset($item);
     // gather item prototypes
     $prototypes = API::ItemPrototype()->get(['output' => $this->dataFields['item_prototype'], 'selectApplications' => ['name'], 'selectApplicationPrototypes' => ['name'], 'selectDiscoveryRule' => ['itemid'], 'discoveryids' => zbx_objectValues($items, 'itemid'), 'inherited' => false, 'preservekeys' => true]);
     $valuemapids = [];
     foreach ($prototypes as $prototype) {
         $valuemapids[$prototype['valuemapid']] = true;
     }
     // Value map IDs that are zeroes, should be skipped.
     unset($valuemapids[0]);
     if ($this->data['valueMaps']) {
         /*
          * If there is an option "valueMaps", some value maps may already been selected. Copy the result and remove
          * value map IDs that should not be selected again.
          */
         foreach ($this->data['valueMaps'] as $valuemapid => $valuemap) {
             if (array_key_exists($valuemapid, $valuemapids)) {
                 unset($valuemapids[$valuemapid]);
             }
         }
     }
     if ($valuemapids) {
         $this->data['valueMaps'] += API::ValueMap()->get(['output' => ['valuemapid', 'name'], 'selectMappings' => ['value', 'newvalue'], 'valuemapids' => array_keys($valuemapids), 'preservekeys' => true]);
     }
     foreach ($prototypes as $prototype) {
         $prototype['valuemap'] = [];
         if ($prototype['valuemapid'] != 0) {
             $prototype['valuemap']['name'] = $this->data['valueMaps'][$prototype['valuemapid']]['name'];
         }
         $items[$prototype['discoveryRule']['itemid']]['itemPrototypes'][] = $prototype;
     }
     // gather graph prototypes
     $graphs = API::GraphPrototype()->get(['discoveryids' => zbx_objectValues($items, 'itemid'), 'selectDiscoveryRule' => API_OUTPUT_EXTEND, 'selectGraphItems' => API_OUTPUT_EXTEND, 'output' => API_OUTPUT_EXTEND, 'inherited' => false, 'preservekeys' => true]);
     $graphs = $this->prepareGraphs($graphs);
     foreach ($graphs as $graph) {
         $items[$graph['discoveryRule']['itemid']]['graphPrototypes'][] = $graph;
     }
     // gather trigger prototypes
     $triggers = API::TriggerPrototype()->get(['discoveryids' => zbx_objectValues($items, 'itemid'), 'output' => ['expression', 'description', 'url', 'status', 'priority', 'comments', 'type'], 'selectDiscoveryRule' => API_OUTPUT_EXTEND, 'selectDependencies' => ['description', 'expression'], 'selectItems' => ['itemid', 'flags', 'type'], 'inherited' => false, 'preservekeys' => true]);
     $triggers = $this->prepareTriggers($triggers);
     foreach ($triggers as $trigger) {
         $items[$trigger['discoveryRule']['itemid']]['triggerPrototypes'][] = $trigger;
     }
     // gather host prototypes
     $hostPrototypes = API::HostPrototype()->get(['discoveryids' => zbx_objectValues($items, 'itemid'), 'output' => API_OUTPUT_EXTEND, 'selectGroupLinks' => API_OUTPUT_EXTEND, 'selectGroupPrototypes' => API_OUTPUT_EXTEND, 'selectDiscoveryRule' => API_OUTPUT_EXTEND, 'selectTemplates' => API_OUTPUT_EXTEND, 'inherited' => false, 'preservekeys' => true]);
     // replace group prototype group IDs with references
     $groupIds = [];
     foreach ($hostPrototypes as $hostPrototype) {
         foreach ($hostPrototype['groupLinks'] as $groupLink) {
             $groupIds[$groupLink['groupid']] = true;
         }
     }
     $groups = $this->getGroupsReferences(array_keys($groupIds));
     // export the groups used in group prototypes
     $this->data['groups'] += $groups;
     foreach ($hostPrototypes as $hostPrototype) {
         foreach ($hostPrototype['groupLinks'] as &$groupLink) {
             $groupLink['groupid'] = $groups[$groupLink['groupid']];
         }
         unset($groupLink);
         $items[$hostPrototype['discoveryRule']['itemid']]['hostPrototypes'][] = $hostPrototype;
     }
     return $items;
 }
コード例 #17
0
     }
     // Trigger prototypes
     $hostTriggerPrototypes = API::TriggerPrototype()->get(array('hostids' => $templateid, 'discoveryids' => $hostDiscoveryRuleids, 'inherited' => false, 'output' => API_OUTPUT_EXTEND));
     if (!empty($hostTriggerPrototypes)) {
         $prototypeList = array();
         foreach ($hostTriggerPrototypes as $triggerPrototype) {
             $prototypeList[$triggerPrototype['triggerid']] = $triggerPrototype['description'];
         }
         order_result($prototypeList);
         $listBox = new CListBox('triggerprototypes', null, 8);
         $listBox->setAttribute('disabled', 'disabled');
         $listBox->addItems($prototypeList);
         $templateList->addRow(_('Trigger prototypes'), $listBox);
     }
     // Graph prototypes
     $hostGraphPrototypes = API::GraphPrototype()->get(array('hostids' => $templateid, 'discoveryids' => $hostDiscoveryRuleids, 'inherited' => false, 'output' => API_OUTPUT_EXTEND));
     if (!empty($hostGraphPrototypes)) {
         $prototypeList = array();
         foreach ($hostGraphPrototypes as $graphPrototype) {
             $prototypeList[$graphPrototype['graphid']] = $graphPrototype['name'];
         }
         order_result($prototypeList);
         $listBox = new CListBox('graphPrototypes', null, 8);
         $listBox->setAttribute('disabled', 'disabled');
         $listBox->addItems($prototypeList);
         $templateList->addRow(_('Graph prototypes'), $listBox);
     }
 }
 // screens
 $screens = API::TemplateScreen()->get(array('inherited' => false, 'templateids' => $templateid, 'output' => array('screenid', 'name')));
 if (!empty($screens)) {
コード例 #18
0
         // skip trigger prototypes with web items
         if (httpItemExists($triggerPrototype['items'])) {
             continue;
         }
         $prototypeList[$triggerPrototype['triggerid']] = $triggerPrototype['description'];
     }
     if ($prototypeList) {
         order_result($prototypeList);
         $listBox = new CListBox('triggerprototypes', null, 8);
         $listBox->setAttribute('disabled', 'disabled');
         $listBox->addItems($prototypeList);
         $hostList->addRow(_('Trigger prototypes'), $listBox);
     }
 }
 // Graph prototypes
 $hostGraphPrototypes = API::GraphPrototype()->get(['output' => ['graphid', 'name'], 'selectHosts' => ['hostid'], 'hostids' => [$data['clone_hostid']], 'discoveryids' => $hostDiscoveryRuleIds, 'inherited' => false]);
 if ($hostGraphPrototypes) {
     $prototypeList = [];
     foreach ($hostGraphPrototypes as $graphPrototype) {
         if (count($graphPrototype['hosts']) == 1) {
             $prototypeList[$graphPrototype['graphid']] = $graphPrototype['name'];
         }
     }
     order_result($prototypeList);
     $listBox = new CListBox('graphPrototypes', null, 8);
     $listBox->setAttribute('disabled', 'disabled');
     $listBox->addItems($prototypeList);
     $hostList->addRow(_('Graph prototypes'), $listBox);
 }
 // host prototypes
 $hostPrototypes = API::HostPrototype()->get(['output' => ['hostid', 'name'], 'discoveryids' => $hostDiscoveryRuleIds, 'inherited' => false]);
コード例 #19
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));
     }
 }
コード例 #20
0
ファイル: CDiscoveryRule.php プロジェクト: itnihao/zatree-2.2
 protected function addRelatedObjects(array $options, array $result)
 {
     $result = parent::addRelatedObjects($options, $result);
     $itemIds = array_keys($result);
     // adding items
     if (!is_null($options['selectItems'])) {
         if ($options['selectItems'] != API_OUTPUT_COUNT) {
             $relationMap = $this->createRelationMap($result, 'parent_itemid', 'itemid', 'item_discovery');
             $items = API::ItemPrototype()->get(array('output' => $options['selectItems'], 'nodeids' => $options['nodeids'], 'itemids' => $relationMap->getRelatedIds(), 'nopermissions' => true, 'preservekeys' => true));
             $result = $relationMap->mapMany($result, $items, 'items', $options['limitSelects']);
         } else {
             $items = API::ItemPrototype()->get(array('nodeids' => $options['nodeids'], 'discoveryids' => $itemIds, 'nopermissions' => true, 'countOutput' => true, 'groupCount' => true));
             $items = zbx_toHash($items, 'parent_itemid');
             foreach ($result as $itemid => $item) {
                 $result[$itemid]['items'] = isset($items[$itemid]) ? $items[$itemid]['rowscount'] : 0;
             }
         }
     }
     // adding triggers
     if (!is_null($options['selectTriggers'])) {
         if ($options['selectTriggers'] != API_OUTPUT_COUNT) {
             $relationMap = new CRelationMap();
             $res = DBselect('SELECT id.parent_itemid,f.triggerid' . ' FROM item_discovery id,items i,functions f' . ' WHERE ' . dbConditionInt('id.parent_itemid', $itemIds) . ' AND id.itemid=i.itemid' . ' AND i.itemid=f.itemid');
             while ($relation = DBfetch($res)) {
                 $relationMap->addRelation($relation['parent_itemid'], $relation['triggerid']);
             }
             $triggers = API::TriggerPrototype()->get(array('output' => $options['selectTriggers'], 'nodeids' => $options['nodeids'], 'triggerids' => $relationMap->getRelatedIds(), 'preservekeys' => true));
             $result = $relationMap->mapMany($result, $triggers, 'triggers', $options['limitSelects']);
         } else {
             $triggers = API::TriggerPrototype()->get(array('nodeids' => $options['nodeids'], 'discoveryids' => $itemIds, 'countOutput' => true, 'groupCount' => true));
             $triggers = zbx_toHash($triggers, 'parent_itemid');
             foreach ($result as $itemid => $item) {
                 $result[$itemid]['triggers'] = isset($triggers[$itemid]) ? $triggers[$itemid]['rowscount'] : 0;
             }
         }
     }
     // adding graphs
     if (!is_null($options['selectGraphs'])) {
         if ($options['selectGraphs'] != API_OUTPUT_COUNT) {
             $relationMap = new CRelationMap();
             $res = DBselect('SELECT id.parent_itemid,gi.graphid' . ' FROM item_discovery id,items i,graphs_items gi' . ' WHERE ' . dbConditionInt('id.parent_itemid', $itemIds) . ' AND id.itemid=i.itemid' . ' AND i.itemid=gi.itemid');
             while ($relation = DBfetch($res)) {
                 $relationMap->addRelation($relation['parent_itemid'], $relation['graphid']);
             }
             $graphs = API::GraphPrototype()->get(array('output' => $options['selectGraphs'], 'nodeids' => $options['nodeids'], 'graphids' => $relationMap->getRelatedIds(), 'preservekeys' => true));
             $result = $relationMap->mapMany($result, $graphs, 'graphs', $options['limitSelects']);
         } else {
             $graphs = API::GraphPrototype()->get(array('nodeids' => $options['nodeids'], 'discoveryids' => $itemIds, 'countOutput' => true, 'groupCount' => true));
             $graphs = zbx_toHash($graphs, 'parent_itemid');
             foreach ($result as $itemid => $item) {
                 $result[$itemid]['graphs'] = isset($graphs[$itemid]) ? $graphs[$itemid]['rowscount'] : 0;
             }
         }
     }
     // adding hosts
     if ($options['selectHostPrototypes'] !== null) {
         if ($options['selectHostPrototypes'] != API_OUTPUT_COUNT) {
             $relationMap = $this->createRelationMap($result, 'parent_itemid', 'hostid', 'host_discovery');
             $hostPrototypes = API::HostPrototype()->get(array('output' => $options['selectHostPrototypes'], 'nodeids' => $options['nodeids'], 'hostids' => $relationMap->getRelatedIds(), 'nopermissions' => true, 'preservekeys' => true));
             $result = $relationMap->mapMany($result, $hostPrototypes, 'hostPrototypes', $options['limitSelects']);
         } else {
             $hostPrototypes = API::HostPrototype()->get(array('nodeids' => $options['nodeids'], 'discoveryids' => $itemIds, 'nopermissions' => true, 'countOutput' => true, 'groupCount' => true));
             $hostPrototypes = zbx_toHash($hostPrototypes, 'parent_itemid');
             foreach ($result as $itemid => $item) {
                 $result[$itemid]['hostPrototypes'] = isset($hostPrototypes[$itemid]) ? $hostPrototypes[$itemid]['rowscount'] : 0;
             }
         }
     }
     return $result;
 }
コード例 #21
0
 /**
  * Import discovery rules.
  *
  * @throws Exception
  */
 protected function processDiscoveryRules()
 {
     $allDiscoveryRules = $this->getFormattedDiscoveryRules();
     if (empty($allDiscoveryRules)) {
         return;
     }
     // unset rules that are related to hosts we did not process
     foreach ($allDiscoveryRules as $host => $discoveryRules) {
         if (!$this->referencer->isProcessedHost($host)) {
             unset($allDiscoveryRules[$host]);
         }
     }
     $itemsToCreate = array();
     $itemsToUpdate = array();
     foreach ($allDiscoveryRules as $host => $discoveryRules) {
         $hostid = $this->referencer->resolveHostOrTemplate($host);
         foreach ($discoveryRules as $item) {
             $item['hostid'] = $hostid;
             if (isset($item['interface_ref'])) {
                 $item['interfaceid'] = $this->referencer->interfacesCache[$hostid][$item['interface_ref']];
             }
             unset($item['item_prototypes']);
             unset($item['trigger_prototypes']);
             unset($item['graph_prototypes']);
             unset($item['host_prototypes']);
             $itemId = $this->referencer->resolveItem($hostid, $item['key_']);
             if ($itemId) {
                 $item['itemid'] = $itemId;
                 $itemsToUpdate[] = $item;
             } else {
                 $itemsToCreate[] = $item;
             }
         }
     }
     // create/update discovery rules and add processed rules to array $processedRules
     $processedRules = array();
     if ($this->options['discoveryRules']['createMissing'] && $itemsToCreate) {
         $newItemsIds = API::DiscoveryRule()->create($itemsToCreate);
         foreach ($newItemsIds['itemids'] as $inum => $itemid) {
             $item = $itemsToCreate[$inum];
             $this->referencer->addItemRef($item['hostid'], $item['key_'], $itemid);
         }
         foreach ($itemsToCreate as $item) {
             $processedRules[$item['hostid']][$item['key_']] = 1;
         }
     }
     if ($this->options['discoveryRules']['updateExisting'] && $itemsToUpdate) {
         API::DiscoveryRule()->update($itemsToUpdate);
         foreach ($itemsToUpdate as $item) {
             $processedRules[$item['hostid']][$item['key_']] = 1;
         }
     }
     // refresh discovery rules because templated ones can be inherited to host and used for prototypes
     $this->referencer->refreshItems();
     // process prototypes
     $prototypesToUpdate = array();
     $prototypesToCreate = array();
     $hostPrototypesToUpdate = array();
     $hostPrototypesToCreate = array();
     foreach ($allDiscoveryRules as $host => $discoveryRules) {
         $hostid = $this->referencer->resolveHostOrTemplate($host);
         foreach ($discoveryRules as $item) {
             // if rule was not processed we should not create/update any of its prototypes
             if (!isset($processedRules[$hostid][$item['key_']])) {
                 continue;
             }
             $item['hostid'] = $hostid;
             $itemId = $this->referencer->resolveItem($hostid, $item['key_']);
             // prototypes
             foreach ($item['item_prototypes'] as $prototype) {
                 $prototype['hostid'] = $hostid;
                 $applicationsIds = array();
                 foreach ($prototype['applications'] as $application) {
                     $applicationsIds[] = $this->referencer->resolveApplication($hostid, $application['name']);
                 }
                 $prototype['applications'] = $applicationsIds;
                 if (isset($prototype['interface_ref'])) {
                     $prototype['interfaceid'] = $this->referencer->interfacesCache[$hostid][$prototype['interface_ref']];
                 }
                 if ($prototype['valuemap']) {
                     $valueMapId = $this->referencer->resolveValueMap($prototype['valuemap']['name']);
                     if (!$valueMapId) {
                         throw new Exception(_s('Cannot find value map "%1$s" used for item prototype "%2$s" of discovery rule "%3$s" on "%4$s".', $prototype['valuemap']['name'], $prototype['name'], $item['name'], $host));
                     }
                     $prototype['valuemapid'] = $valueMapId;
                 }
                 $prototypeId = $this->referencer->resolveItem($hostid, $prototype['key_']);
                 $prototype['rule'] = array('hostid' => $hostid, 'key' => $item['key_']);
                 if ($prototypeId) {
                     $prototype['itemid'] = $prototypeId;
                     $prototypesToUpdate[] = $prototype;
                 } else {
                     $prototypesToCreate[] = $prototype;
                 }
             }
             // host prototype
             foreach ($item['host_prototypes'] as $hostPrototype) {
                 // resolve group prototypes
                 $groupLinks = array();
                 foreach ($hostPrototype['group_links'] as $groupLink) {
                     $groupId = $this->referencer->resolveGroup($groupLink['group']['name']);
                     if (!$groupId) {
                         throw new Exception(_s('Cannot find host group "%1$s" for host prototype "%2$s" of discovery rule "%3$s" on "%4$s".', $groupLink['group']['name'], $hostPrototype['name'], $item['name'], $host));
                     }
                     $groupLinks[] = array('groupid' => $groupId);
                 }
                 $hostPrototype['groupLinks'] = $groupLinks;
                 $hostPrototype['groupPrototypes'] = $hostPrototype['group_prototypes'];
                 unset($hostPrototype['group_links'], $hostPrototype['group_prototypes']);
                 // resolve templates
                 $templates = array();
                 foreach ($hostPrototype['templates'] as $template) {
                     $templateId = $this->referencer->resolveTemplate($template['name']);
                     if (!$templateId) {
                         throw new Exception(_s('Cannot find template "%1$s" for host prototype "%2$s" of discovery rule "%3$s" on "%4$s".', $template['name'], $hostPrototype['name'], $item['name'], $host));
                     }
                     $templates[] = array('templateid' => $templateId);
                 }
                 $hostPrototype['templates'] = $templates;
                 $hostPrototypeId = $this->referencer->resolveHostPrototype($hostid, $itemId, $hostPrototype['host']);
                 if ($hostPrototypeId) {
                     $hostPrototype['hostid'] = $hostPrototypeId;
                     $hostPrototypesToUpdate[] = $hostPrototype;
                 } else {
                     $hostPrototype['ruleid'] = $itemId;
                     $hostPrototypesToCreate[] = $hostPrototype;
                 }
             }
             if (isset($item['interface_ref'])) {
                 $item['interfaceid'] = $this->referencer->interfacesCache[$hostid][$item['interface_ref']];
             }
             unset($item['item_prototypes']);
             unset($item['trigger_prototypes']);
             unset($item['graph_prototypes']);
             unset($item['host_prototypes']);
             $itemsId = $this->referencer->resolveItem($hostid, $item['key_']);
             if ($itemsId) {
                 $item['itemid'] = $itemsId;
                 $itemsToUpdate[] = $item;
             } else {
                 $itemsToCreate[] = $item;
             }
         }
     }
     if ($prototypesToCreate) {
         foreach ($prototypesToCreate as &$prototype) {
             $prototype['ruleid'] = $this->referencer->resolveItem($prototype['rule']['hostid'], $prototype['rule']['key']);
         }
         unset($prototype);
         $newPrototypeIds = API::ItemPrototype()->create($prototypesToCreate);
         foreach ($newPrototypeIds['itemids'] as $inum => $itemid) {
             $item = $prototypesToCreate[$inum];
             $this->referencer->addItemRef($item['hostid'], $item['key_'], $itemid);
         }
     }
     if ($prototypesToUpdate) {
         foreach ($prototypesToCreate as &$prototype) {
             $prototype['ruleid'] = $this->referencer->resolveItem($prototype['rule']['hostid'], $prototype['rule']['key']);
         }
         unset($prototype);
         API::ItemPrototype()->update($prototypesToUpdate);
     }
     if ($hostPrototypesToCreate) {
         API::HostPrototype()->create($hostPrototypesToCreate);
     }
     if ($hostPrototypesToUpdate) {
         API::HostPrototype()->update($hostPrototypesToUpdate);
     }
     // refresh prototypes because templated ones can be inherited to host and used in triggers prototypes or graph prototypes
     $this->referencer->refreshItems();
     // first we need to create item prototypes and only then graph prototypes
     $triggersToCreate = array();
     $triggersToUpdate = array();
     $graphsToCreate = array();
     $graphsToUpdate = array();
     foreach ($allDiscoveryRules as $host => $discoveryRules) {
         $hostid = $this->referencer->resolveHostOrTemplate($host);
         foreach ($discoveryRules as $item) {
             // if rule was not processed we should not create/update any of its prototypes
             if (!isset($processedRules[$hostid][$item['key_']])) {
                 continue;
             }
             // trigger prototypes
             foreach ($item['trigger_prototypes'] as $trigger) {
                 $triggerId = $this->referencer->resolveTrigger($trigger['description'], $trigger['expression']);
                 if ($triggerId) {
                     $trigger['triggerid'] = $triggerId;
                     $triggersToUpdate[] = $trigger;
                 } else {
                     $triggersToCreate[] = $trigger;
                 }
             }
             // graph prototypes
             foreach ($item['graph_prototypes'] 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 prototype "%3$s" of discovery rule "%4$s" on "%5$s".', $graph['ymin_item_1']['key'], $graph['ymin_item_1']['host'], $graph['name'], $item['name'], $host));
                     }
                     $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 prototype "%3$s" of discovery rule "%4$s" on "%5$s".', $graph['ymax_item_1']['key'], $graph['ymax_item_1']['host'], $graph['name'], $item['name'], $host));
                     }
                     $graph['ymax_itemid'] = $itemId;
                 }
                 foreach ($graph['gitems'] as &$gitem) {
                     if (!($gitemHostId = $this->referencer->resolveHostOrTemplate($gitem['item']['host']))) {
                         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::GraphPrototype()->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 ($triggersToCreate) {
         API::TriggerPrototype()->create($triggersToCreate);
     }
     if ($triggersToUpdate) {
         API::TriggerPrototype()->update($triggersToUpdate);
     }
     if ($graphsToCreate) {
         API::GraphPrototype()->create($graphsToCreate);
     }
     if ($graphsToUpdate) {
         API::GraphPrototype()->update($graphsToUpdate);
     }
 }
コード例 #22
0
ファイル: graphs.php プロジェクト: itnihao/zatree-2.2
    $data = array('pageFilter' => $pageFilter, 'hostid' => $pageFilter->hostid > 0 ? $pageFilter->hostid : get_request('hostid'), 'parent_discoveryid' => get_request('parent_discoveryid'), 'graphs' => array(), 'discovery_rule' => empty($_REQUEST['parent_discoveryid']) ? null : $discovery_rule, 'displayNodes' => is_array(get_current_nodeid()) && $pageFilter->groupid == 0 && $pageFilter->hostid == 0);
    $sortfield = getPageSortField('name');
    $sortorder = getPageSortOrder();
    // get graphs
    $options = array('hostids' => $data['hostid'] ? $data['hostid'] : null, 'groupids' => !$data['hostid'] && $pageFilter->groupid > 0 ? $pageFilter->groupid : null, 'discoveryids' => empty($_REQUEST['parent_discoveryid']) ? null : get_request('parent_discoveryid'), 'editable' => true, 'output' => array('graphid', 'name', 'graphtype'), 'limit' => $config['search_limit'] + 1);
    $data['graphs'] = empty($_REQUEST['parent_discoveryid']) ? API::Graph()->get($options) : API::GraphPrototype()->get($options);
    if ($sortfield == 'graphtype') {
        foreach ($data['graphs'] as $gnum => $graph) {
            $data['graphs'][$gnum]['graphtype'] = graphType($graph['graphtype']);
        }
    }
    order_result($data['graphs'], $sortfield, $sortorder);
    $data['paging'] = getPagingLine($data['graphs'], array('graphid'), array('hostid' => get_request('hostid'), 'parent_discoveryid' => get_request('parent_discoveryid')));
    // get graphs after paging
    $options = array('graphids' => zbx_objectValues($data['graphs'], 'graphid'), 'output' => array('graphid', 'name', 'templateid', 'graphtype', 'width', 'height'), 'selectDiscoveryRule' => array('itemid', 'name'), 'selectHosts' => $data['hostid'] ? null : array('name'), 'selectTemplates' => $data['hostid'] ? null : array('name'));
    $data['graphs'] = empty($_REQUEST['parent_discoveryid']) ? API::Graph()->get($options) : API::GraphPrototype()->get($options);
    foreach ($data['graphs'] as $gnum => $graph) {
        $data['graphs'][$gnum]['graphtype'] = graphType($graph['graphtype']);
    }
    // nodes
    if ($data['displayNodes']) {
        foreach ($data['graphs'] as $key => $graph) {
            $data['graphs'][$key]['nodename'] = get_node_name_by_elid($graph['graphid'], true);
        }
    }
    order_result($data['graphs'], $sortfield, $sortorder);
    // render view
    $graphView = new CView('configuration.graph.list', $data);
    $graphView->render();
    $graphView->show();
}
コード例 #23
0
 public function addRelatedObjects(array $options, array $result)
 {
     $result = parent::addRelatedObjects($options, $result);
     $itemids = array_keys($result);
     // adding applications
     if ($options['selectApplications'] !== null && $options['selectApplications'] != API_OUTPUT_COUNT) {
         $relationMap = $this->createRelationMap($result, 'itemid', 'applicationid', 'items_applications');
         $applications = API::Application()->get(array('output' => $options['selectApplications'], 'applicationids' => $relationMap->getRelatedIds(), 'preservekeys' => true));
         $result = $relationMap->mapMany($result, $applications, 'applications');
     }
     // adding triggers
     if (!is_null($options['selectTriggers'])) {
         if ($options['selectTriggers'] != API_OUTPUT_COUNT) {
             $relationMap = $this->createRelationMap($result, 'itemid', 'triggerid', 'functions');
             $triggers = API::TriggerPrototype()->get(array('output' => $options['selectTriggers'], 'triggerids' => $relationMap->getRelatedIds(), 'preservekeys' => true));
             if (!is_null($options['limitSelects'])) {
                 order_result($triggers, 'description');
             }
             $result = $relationMap->mapMany($result, $triggers, 'triggers', $options['limitSelects']);
         } else {
             $triggers = API::TriggerPrototype()->get(array('countOutput' => true, 'groupCount' => true, 'itemids' => $itemids));
             $triggers = zbx_toHash($triggers, 'itemid');
             foreach ($result as $itemid => $item) {
                 if (isset($triggers[$itemid])) {
                     $result[$itemid]['triggers'] = $triggers[$itemid]['rowscount'];
                 } else {
                     $result[$itemid]['triggers'] = 0;
                 }
             }
         }
     }
     // adding graphs
     if (!is_null($options['selectGraphs'])) {
         if ($options['selectGraphs'] != API_OUTPUT_COUNT) {
             $relationMap = $this->createRelationMap($result, 'itemid', 'graphid', 'graphs_items');
             $graphs = API::GraphPrototype()->get(array('output' => $options['selectGraphs'], 'graphids' => $relationMap->getRelatedIds(), 'preservekeys' => true));
             if (!is_null($options['limitSelects'])) {
                 order_result($graphs, 'name');
             }
             $result = $relationMap->mapMany($result, $graphs, 'graphs', $options['limitSelects']);
         } else {
             $graphs = API::GraphPrototype()->get(array('countOutput' => true, 'groupCount' => true, 'itemids' => $itemids));
             $graphs = zbx_toHash($graphs, 'itemid');
             foreach ($result as $itemid => $item) {
                 if (isset($graphs[$itemid])) {
                     $result[$itemid]['graphs'] = $graphs[$itemid]['rowscount'];
                 } else {
                     $result[$itemid]['graphs'] = 0;
                 }
             }
         }
     }
     // adding discoveryrule
     if ($options['selectDiscoveryRule'] !== null && $options['selectDiscoveryRule'] != API_OUTPUT_COUNT) {
         $relationMap = $this->createRelationMap($result, 'itemid', 'parent_itemid', 'item_discovery');
         $discoveryRules = API::DiscoveryRule()->get(array('output' => $options['selectDiscoveryRule'], 'itemids' => $relationMap->getRelatedIds(), 'nopermissions' => true, 'preservekeys' => true));
         $result = $relationMap->mapOne($result, $discoveryRules, 'discoveryRule');
     }
     return $result;
 }
コード例 #24
0
 /**
  * Unlinks the templates from the given hosts. If $tragetids is set to null, the templates will be unlinked from
  * all hosts.
  *
  * @param $templateids
  * @param null|array $targetids the IDs of the hosts to unlink the templates from
  * @param bool $clear           delete all of the inherited objects from the hosts
  *
  * @return void
  */
 protected function unlink($templateids, $targetids = null, $clear = false)
 {
     $flags = $clear ? array(ZBX_FLAG_DISCOVERY_NORMAL, ZBX_FLAG_DISCOVERY) : array(ZBX_FLAG_DISCOVERY_NORMAL, ZBX_FLAG_DISCOVERY, ZBX_FLAG_DISCOVERY_CHILD);
     /* TRIGGERS {{{ */
     // check that all triggers on templates that we unlink, don't have items from another templates
     $sql = 'SELECT DISTINCT t.description' . ' FROM triggers t,functions f,items i' . ' WHERE t.triggerid=f.triggerid' . ' AND f.itemid=i.itemid' . ' AND ' . dbConditionInt('i.hostid', $templateids) . ' AND EXISTS (' . 'SELECT ff.triggerid' . ' FROM functions ff,items ii' . ' WHERE ff.itemid=ii.itemid' . ' AND ff.triggerid=t.triggerid' . ' AND ' . dbConditionInt('ii.hostid', $templateids, true) . ')' . ' AND t.flags=' . ZBX_FLAG_DISCOVERY_NORMAL;
     if ($dbTrigger = DBfetch(DBSelect($sql, 1))) {
         self::exception(ZBX_API_ERROR_PARAMETERS, _s('Cannot unlink trigger "%s", it has items from template that is left linked to host.', $dbTrigger['description']));
     }
     $sqlFrom = ' triggers t,hosts h';
     $sqlWhere = ' EXISTS (' . 'SELECT ff.triggerid' . ' FROM functions ff,items ii' . ' WHERE ff.triggerid=t.templateid' . ' AND ii.itemid=ff.itemid' . ' AND ' . dbConditionInt('ii.hostid', $templateids) . ')' . ' AND ' . dbConditionInt('t.flags', $flags);
     if (!is_null($targetids)) {
         $sqlFrom = ' triggers t,functions f,items i,hosts h';
         $sqlWhere .= ' AND ' . dbConditionInt('i.hostid', $targetids) . ' AND f.itemid=i.itemid' . ' AND t.triggerid=f.triggerid' . ' AND h.hostid=i.hostid';
     }
     $sql = 'SELECT DISTINCT t.triggerid,t.description,t.flags,t.expression,h.name as host' . ' FROM ' . $sqlFrom . ' WHERE ' . $sqlWhere;
     $dbTriggers = DBSelect($sql);
     $triggers = array(ZBX_FLAG_DISCOVERY_NORMAL => array(), ZBX_FLAG_DISCOVERY_CHILD => array());
     $triggerids = array();
     while ($trigger = DBfetch($dbTriggers)) {
         $triggers[$trigger['flags']][$trigger['triggerid']] = array('description' => $trigger['description'], 'expression' => explode_exp($trigger['expression']), 'triggerid' => $trigger['triggerid'], 'host' => $trigger['host']);
         if (!in_array($trigger['triggerid'], $triggerids)) {
             array_push($triggerids, $trigger['triggerid']);
         }
     }
     if (!empty($triggers[ZBX_FLAG_DISCOVERY_NORMAL])) {
         if ($clear) {
             $result = API::Trigger()->delete(array_keys($triggers[ZBX_FLAG_DISCOVERY_NORMAL]), true);
             if (!$result) {
                 self::exception(ZBX_API_ERROR_INTERNAL, _('Cannot unlink and clear triggers'));
             }
         } else {
             DB::update('triggers', array('values' => array('templateid' => 0), 'where' => array('triggerid' => array_keys($triggers[ZBX_FLAG_DISCOVERY_NORMAL]))));
             foreach ($triggers[ZBX_FLAG_DISCOVERY_NORMAL] as $trigger) {
                 info(_s('Unlinked: Trigger "%1$s" on "%2$s".', $trigger['description'], $trigger['host']));
             }
         }
     }
     if (!empty($triggers[ZBX_FLAG_DISCOVERY_CHILD])) {
         if ($clear) {
             $result = API::TriggerPrototype()->delete(array_keys($triggers[ZBX_FLAG_DISCOVERY_CHILD]), true);
             if (!$result) {
                 self::exception(ZBX_API_ERROR_INTERNAL, _('Cannot unlink and clear triggers'));
             }
         } else {
             DB::update('triggers', array('values' => array('templateid' => 0), 'where' => array('triggerid' => array_keys($triggers[ZBX_FLAG_DISCOVERY_CHILD]))));
             foreach ($triggers[ZBX_FLAG_DISCOVERY_CHILD] as $trigger) {
                 info(_s('Unlinked: Trigger prototype "%1$s" on "%2$s".', $trigger['description'], $trigger['host']));
             }
         }
     }
     /* }}} TRIGGERS */
     /* ITEMS, DISCOVERY RULES {{{ */
     $sqlFrom = ' items i1,items i2,hosts h';
     $sqlWhere = ' i2.itemid=i1.templateid' . ' AND ' . dbConditionInt('i2.hostid', $templateids) . ' AND ' . dbConditionInt('i1.flags', $flags) . ' AND h.hostid=i1.hostid';
     if (!is_null($targetids)) {
         $sqlWhere .= ' AND ' . dbConditionInt('i1.hostid', $targetids);
     }
     $sql = 'SELECT DISTINCT i1.itemid,i1.flags,i1.name,i1.hostid,h.name as host' . ' FROM ' . $sqlFrom . ' WHERE ' . $sqlWhere;
     $dbItems = DBSelect($sql);
     $items = array(ZBX_FLAG_DISCOVERY_NORMAL => array(), ZBX_FLAG_DISCOVERY => array(), ZBX_FLAG_DISCOVERY_CHILD => array());
     while ($item = DBfetch($dbItems)) {
         $items[$item['flags']][$item['itemid']] = array('name' => $item['name'], 'host' => $item['host']);
     }
     if (!empty($items[ZBX_FLAG_DISCOVERY])) {
         if ($clear) {
             $result = API::DiscoveryRule()->delete(array_keys($items[ZBX_FLAG_DISCOVERY]), true);
             if (!$result) {
                 self::exception(ZBX_API_ERROR_INTERNAL, _('Cannot unlink and clear discovery rules'));
             }
         } else {
             DB::update('items', array('values' => array('templateid' => 0), 'where' => array('itemid' => array_keys($items[ZBX_FLAG_DISCOVERY]))));
             foreach ($items[ZBX_FLAG_DISCOVERY] as $discoveryRule) {
                 info(_s('Unlinked: Discovery rule "%1$s" on "%2$s".', $discoveryRule['name'], $discoveryRule['host']));
             }
         }
     }
     if (!empty($items[ZBX_FLAG_DISCOVERY_NORMAL])) {
         if ($clear) {
             $result = API::Item()->delete(array_keys($items[ZBX_FLAG_DISCOVERY_NORMAL]), true);
             if (!$result) {
                 self::exception(ZBX_API_ERROR_INTERNAL, _('Cannot unlink and clear items'));
             }
         } else {
             DB::update('items', array('values' => array('templateid' => 0), 'where' => array('itemid' => array_keys($items[ZBX_FLAG_DISCOVERY_NORMAL]))));
             foreach ($items[ZBX_FLAG_DISCOVERY_NORMAL] as $item) {
                 info(_s('Unlinked: Item "%1$s" on "%2$s".', $item['name'], $item['host']));
             }
         }
     }
     if (!empty($items[ZBX_FLAG_DISCOVERY_CHILD])) {
         if ($clear) {
             $result = API::Itemprototype()->delete(array_keys($items[ZBX_FLAG_DISCOVERY_CHILD]), true);
             if (!$result) {
                 self::exception(ZBX_API_ERROR_INTERNAL, _('Cannot unlink and clear item prototypes'));
             }
         } else {
             DB::update('items', array('values' => array('templateid' => 0), 'where' => array('itemid' => array_keys($items[ZBX_FLAG_DISCOVERY_CHILD]))));
             foreach ($items[ZBX_FLAG_DISCOVERY_CHILD] as $item) {
                 info(_s('Unlinked: Item prototype "%1$s" on "%2$s".', $item['name'], $item['host']));
             }
         }
     }
     /* }}} ITEMS, DISCOVERY RULES */
     /* GRAPHS {{{ */
     $sqlFrom = ' graphs g,hosts h';
     $sqlWhere = ' EXISTS (' . 'SELECT ggi.graphid' . ' FROM graphs_items ggi,items ii' . ' WHERE ggi.graphid=g.templateid' . ' AND ii.itemid=ggi.itemid' . ' AND ' . dbConditionInt('ii.hostid', $templateids) . ')' . ' AND ' . dbConditionInt('g.flags', $flags);
     if (!is_null($targetids)) {
         $sqlFrom = ' graphs g,graphs_items gi,items i,hosts h';
         $sqlWhere .= ' AND ' . dbConditionInt('i.hostid', $targetids) . ' AND gi.itemid=i.itemid' . ' AND g.graphid=gi.graphid' . ' AND h.hostid=i.hostid';
     }
     $sql = 'SELECT DISTINCT g.graphid,g.name,g.flags,h.name as host' . ' FROM ' . $sqlFrom . ' WHERE ' . $sqlWhere;
     $dbGraphs = DBSelect($sql);
     $graphs = array(ZBX_FLAG_DISCOVERY_NORMAL => array(), ZBX_FLAG_DISCOVERY_CHILD => array());
     while ($graph = DBfetch($dbGraphs)) {
         $graphs[$graph['flags']][$graph['graphid']] = array('name' => $graph['name'], 'graphid' => $graph['graphid'], 'host' => $graph['host']);
     }
     if (!empty($graphs[ZBX_FLAG_DISCOVERY_CHILD])) {
         if ($clear) {
             $result = API::GraphPrototype()->delete(array_keys($graphs[ZBX_FLAG_DISCOVERY_CHILD]), true);
             if (!$result) {
                 self::exception(ZBX_API_ERROR_INTERNAL, _('Cannot unlink and clear graph prototypes'));
             }
         } else {
             DB::update('graphs', array('values' => array('templateid' => 0), 'where' => array('graphid' => array_keys($graphs[ZBX_FLAG_DISCOVERY_CHILD]))));
             foreach ($graphs[ZBX_FLAG_DISCOVERY_CHILD] as $graph) {
                 info(_s('Unlinked: Graph prototype "%1$s" on "%2$s".', $graph['name'], $graph['host']));
             }
         }
     }
     if (!empty($graphs[ZBX_FLAG_DISCOVERY_NORMAL])) {
         if ($clear) {
             $result = API::Graph()->delete(array_keys($graphs[ZBX_FLAG_DISCOVERY_NORMAL]), true);
             if (!$result) {
                 self::exception(ZBX_API_ERROR_INTERNAL, _('Cannot unlink and clear graphs.'));
             }
         } else {
             DB::update('graphs', array('values' => array('templateid' => 0), 'where' => array('graphid' => array_keys($graphs[ZBX_FLAG_DISCOVERY_NORMAL]))));
             foreach ($graphs[ZBX_FLAG_DISCOVERY_NORMAL] as $graph) {
                 info(_s('Unlinked: Graph "%1$s" on "%2$s".', $graph['name'], $graph['host']));
             }
         }
     }
     /* }}} GRAPHS */
     /* APPLICATIONS {{{ */
     $sqlFrom = ' applications a1,applications a2,hosts h';
     $sqlWhere = ' a2.applicationid=a1.templateid' . ' AND ' . dbConditionInt('a2.hostid', $templateids) . ' AND h.hostid=a1.hostid';
     if (!is_null($targetids)) {
         $sqlWhere .= ' AND ' . dbConditionInt('a1.hostid', $targetids);
     }
     $sql = 'SELECT DISTINCT a1.applicationid,a1.name,a1.hostid,h.name as host' . ' FROM ' . $sqlFrom . ' WHERE ' . $sqlWhere;
     $dbApplications = DBSelect($sql);
     $applications = array();
     while ($application = DBfetch($dbApplications)) {
         $applications[$application['applicationid']] = array('name' => $application['name'], 'hostid' => $application['hostid'], 'host' => $application['host']);
     }
     if (!empty($applications)) {
         if ($clear) {
             $result = API::Application()->delete(array_keys($applications), true);
             if (!$result) {
                 self::exception(ZBX_API_ERROR_INTERNAL, _('Cannot unlink and clear applications'));
             }
         } else {
             DB::update('applications', array('values' => array('templateid' => 0), 'where' => array('applicationid' => array_keys($applications))));
             foreach ($applications as $application) {
                 info(_s('Unlinked: Application "%1$s" on "%2$s".', $application['name'], $application['host']));
             }
         }
     }
     /* }}} APPLICATIONS */
     $cond = array('templateid' => $templateids);
     if (!is_null($targetids)) {
         $cond['hostid'] = $targetids;
     }
     DB::delete('hosts_templates', $cond);
     if (!is_null($targetids)) {
         $hosts = API::Host()->get(array('hostids' => $targetids, 'output' => array('hostid', 'host'), 'nopermissions' => true));
     } else {
         $hosts = API::Host()->get(array('templateids' => $templateids, 'output' => array('hostid', 'host'), 'nopermissions' => true));
     }
     if (!empty($hosts)) {
         $templates = API::Template()->get(array('templateids' => $templateids, 'output' => array('hostid', 'host'), 'nopermissions' => true));
         $hosts = implode(', ', zbx_objectValues($hosts, 'host'));
         $templates = implode(', ', zbx_objectValues($templates, 'host'));
         info(_s('Templates "%1$s" unlinked from hosts "%2$s".', $templates, $hosts));
     }
 }