예제 #1
0
 /**
  * Sort conditions by formula id as if they were numbers.
  *
  * @param array		$conditions		conditions
  * @return array
  */
 public static function sortConditionsByFormulaId($conditions)
 {
     uasort($conditions, function ($condition1, $condition2) {
         return CConditionHelper::compareFormulaIds($condition1['formulaid'], $condition2['formulaid']);
     });
     return $conditions;
 }
// status
$enabledCheckBox = (new CCheckBox('status', ITEM_STATUS_ACTIVE))->setChecked($this->data['status'] == ITEM_STATUS_ACTIVE);
$itemFormList->addRow(_('Enabled'), $enabledCheckBox);
/*
 * Condition tab
 */
$conditionFormList = new CFormList();
// type of calculation
$conditionFormList->addRow(_('Type of calculation'), [new CComboBox('evaltype', $this->data['evaltype'], null, [CONDITION_EVAL_TYPE_AND_OR => _('And/Or'), CONDITION_EVAL_TYPE_AND => _('And'), CONDITION_EVAL_TYPE_OR => _('Or'), CONDITION_EVAL_TYPE_EXPRESSION => _('Custom expression')]), (new CDiv())->addClass(ZBX_STYLE_FORM_INPUT_MARGIN), (new CSpan(''))->setId('expression'), (new CTextBox('formula', $this->data['formula']))->setWidth(ZBX_TEXTAREA_STANDARD_WIDTH)->setId('formula')->setAttribute('placeholder', 'A or (B and C) …')], 'conditionRow');
// macros
$conditionTable = (new CTable())->setId('conditions')->setAttribute('style', 'width: 100%;')->setHeader([_('Label'), _('Macro'), '', _('Regular expression'), _('Action')]);
$conditions = $this->data['conditions'];
if (!$conditions) {
    $conditions = [['macro' => '', 'value' => '', 'formulaid' => num2letter(0)]];
} else {
    $conditions = CConditionHelper::sortConditionsByFormulaId($conditions);
}
// fields
foreach ($conditions as $i => $condition) {
    // formula id
    $formulaId = [new CSpan($condition['formulaid']), new CVar('conditions[' . $i . '][formulaid]', $condition['formulaid'])];
    // macro
    $macro = (new CTextBox('conditions[' . $i . '][macro]', $condition['macro'], false, 64))->setWidth(ZBX_TEXTAREA_MACRO_WIDTH)->addClass(ZBX_STYLE_UPPERCASE)->addClass('macro')->setAttribute('placeholder', '{#MACRO}')->setAttribute('data-formulaid', $condition['formulaid']);
    // value
    $value = (new CTextBox('conditions[' . $i . '][value]', $condition['value'], false, 255))->setWidth(ZBX_TEXTAREA_MACRO_VALUE_WIDTH)->setAttribute('placeholder', _('regular expression'));
    // delete button
    $deleteButtonCell = [(new CButton('conditions_' . $i . '_remove', _('Remove')))->addClass(ZBX_STYLE_BTN_LINK)->addClass('element-table-remove')];
    $row = [$formulaId, $macro, new CSpan(_('matches')), $value, (new CCol($deleteButtonCell))->addClass(ZBX_STYLE_NOWRAP)];
    $conditionTable->addRow($row, 'form_row');
}
$conditionTable->setFooter(new CCol((new CButton('macro_add', _('Add')))->addClass(ZBX_STYLE_BTN_LINK)->addClass('element-table-add')));
예제 #3
0
 /**
  * Converts a formula with letters to a formula with IDs and updates it.
  *
  * @param string 	$actionId
  * @param string 	$formulaWithLetters		formula with letters
  * @param array 	$conditions
  */
 protected function updateFormula($actionId, $formulaWithLetters, array $conditions)
 {
     $formulaIdToConditionId = array();
     foreach ($conditions as $condition) {
         $formulaIdToConditionId[$condition['formulaid']] = $condition['conditionid'];
     }
     $formula = CConditionHelper::replaceLetterIds($formulaWithLetters, $formulaIdToConditionId);
     DB::updateByPk('actions', $actionId, array('formula' => $formula));
 }
예제 #4
0
             $data['action']['def_longdata'] = getRequest('def_longdata', ACTION_DEFAULT_MSG_AUTOREG);
         } else {
             $data['action']['def_shortdata'] = getRequest('def_shortdata');
             $data['action']['def_longdata'] = getRequest('def_longdata');
             $data['action']['r_shortdata'] = getRequest('r_shortdata');
             $data['action']['r_longdata'] = getRequest('r_longdata');
         }
     }
 }
 if (!$data['actionid'] && !hasRequest('form_refresh') && $data['eventsource'] == EVENT_SOURCE_TRIGGERS) {
     $data['action']['filter']['conditions'] = array(array('formulaid' => 'A', 'conditiontype' => CONDITION_TYPE_MAINTENANCE, 'operator' => CONDITION_OPERATOR_NOT_IN, 'value' => ''), array('formulaid' => 'B', 'conditiontype' => CONDITION_TYPE_TRIGGER_VALUE, 'operator' => CONDITION_OPERATOR_EQUAL, 'value' => TRIGGER_VALUE_TRUE));
 }
 $data['allowedConditions'] = get_conditions_by_eventsource($data['eventsource']);
 $data['allowedOperations'] = get_operations_by_eventsource($data['eventsource']);
 if (!hasRequest('add_condition')) {
     $data['action']['filter']['conditions'] = CConditionHelper::sortConditionsByFormulaId($data['action']['filter']['conditions']);
 }
 // new condition
 $data['new_condition'] = array('conditiontype' => isset($data['new_condition']['conditiontype']) ? $data['new_condition']['conditiontype'] : CONDITION_TYPE_TRIGGER_NAME, 'operator' => isset($data['new_condition']['operator']) ? $data['new_condition']['operator'] : CONDITION_OPERATOR_LIKE, 'value' => isset($data['new_condition']['value']) ? $data['new_condition']['value'] : '');
 if (!str_in_array($data['new_condition']['conditiontype'], $data['allowedConditions'])) {
     $data['new_condition']['conditiontype'] = $data['allowedConditions'][0];
 }
 // new operation
 if (!empty($data['new_operation'])) {
     if (!is_array($data['new_operation'])) {
         $data['new_operation'] = array('operationtype' => 0, 'esc_period' => 0, 'esc_step_from' => 1, 'esc_step_to' => 1, 'evaltype' => 0);
     }
 }
 // render view
 $actionView = new CView('configuration.action.edit', $data);
 $actionView->render();
예제 #5
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;
 }