Ejemplo n.º 1
0
 /**
  * Validate conditions.
  *
  * @static
  *
  * @param array $conditions
  * @param int   $conditions['conditiontype']
  * @param array $conditions['value']
  *
  * @return bool
  */
 public static function validateConditions($conditions, $update = false)
 {
     $conditions = zbx_toArray($conditions);
     $hostGroupIdsAll = array();
     $templateIdsAll = array();
     $triggerIdsAll = array();
     $hostIdsAll = array();
     $discoveryRuleIdsAll = array();
     $proxyIdsAll = array();
     $proxyidsAll = array();
     // build validators
     $timePeriodValidator = new CTimePeriodValidator();
     $discoveryCheckTypeValidator = new CSetValidator(array('values' => array_keys(discovery_check_type2str())));
     $discoveryObjectStatusValidator = new CSetValidator(array('values' => array_keys(discovery_object_status2str())));
     $triggerSeverityValidator = new CSetValidator(array('values' => array_keys(getSeverityCaption())));
     $discoveryObjectValidator = new CSetValidator(array('values' => array_keys(discovery_object2str())));
     $triggerValueValidator = new CSetValidator(array('values' => array_keys(trigger_value2str())));
     $eventTypeValidator = new CSetValidator(array('values' => array_keys(eventType())));
     foreach ($conditions as $condition) {
         // on create operator is mandatory and needs validation, but on update it must be validated only if it's set
         if (!$update || $update && isset($condition['operator'])) {
             $operatorValidator = new CSetValidator(array('values' => get_operators_by_conditiontype($condition['conditiontype'])));
             if (!$operatorValidator->validate($condition['operator'])) {
                 self::exception(ZBX_API_ERROR_PARAMETERS, _('Incorrect action condition operator.'));
             }
         }
         if (!$update || $update && isset($condition['value'])) {
             // validate condition values depending on condition type
             switch ($condition['conditiontype']) {
                 case CONDITION_TYPE_HOST_GROUP:
                     if (!$condition['value']) {
                         self::exception(ZBX_API_ERROR_PARAMETERS, _('Empty action condition.'));
                     }
                     $hostGroupIdsAll[$condition['value']] = $condition['value'];
                     break;
                 case CONDITION_TYPE_TEMPLATE:
                     if (!$condition['value']) {
                         self::exception(ZBX_API_ERROR_PARAMETERS, _('Empty action condition.'));
                     }
                     $templateIdsAll[$condition['value']] = $condition['value'];
                     break;
                 case CONDITION_TYPE_TRIGGER:
                     if (!$condition['value']) {
                         self::exception(ZBX_API_ERROR_PARAMETERS, _('Empty action condition.'));
                     }
                     $triggerIdsAll[$condition['value']] = $condition['value'];
                     break;
                 case CONDITION_TYPE_HOST:
                     if (!$condition['value']) {
                         self::exception(ZBX_API_ERROR_PARAMETERS, _('Empty action condition.'));
                     }
                     $hostIdsAll[$condition['value']] = $condition['value'];
                     break;
                 case CONDITION_TYPE_DRULE:
                     if (!$condition['value']) {
                         self::exception(ZBX_API_ERROR_PARAMETERS, _('Empty action condition.'));
                     }
                     $discoveryRuleIdsAll[$condition['value']] = $condition['value'];
                     break;
                 case CONDITION_TYPE_DCHECK:
                     if (!$condition['value']) {
                         self::exception(ZBX_API_ERROR_PARAMETERS, _('Empty action condition.'));
                     }
                     $proxyIdsAll[$condition['value']] = $condition['value'];
                     break;
                 case CONDITION_TYPE_PROXY:
                     if (!$condition['value']) {
                         self::exception(ZBX_API_ERROR_PARAMETERS, _('Empty action condition.'));
                     }
                     $proxyidsAll[$condition['value']] = $condition['value'];
                     break;
                 case CONDITION_TYPE_DOBJECT:
                     if (zbx_empty($condition['value'])) {
                         self::exception(ZBX_API_ERROR_PARAMETERS, _('Empty action condition.'));
                     } elseif (!$discoveryObjectValidator->validate($condition['value'])) {
                         self::exception(ZBX_API_ERROR_PARAMETERS, _('Incorrect action condition discovery object.'));
                     }
                     break;
                 case CONDITION_TYPE_TIME_PERIOD:
                     if (!$timePeriodValidator->validate($condition['value'])) {
                         self::exception(ZBX_API_ERROR_PARAMETERS, $timePeriodValidator->getError());
                     }
                     break;
                 case CONDITION_TYPE_DHOST_IP:
                     if (zbx_empty($condition['value'])) {
                         self::exception(ZBX_API_ERROR_PARAMETERS, _('Empty action condition.'));
                     } else {
                         if (!validate_ip_range($condition['value'])) {
                             self::exception(ZBX_API_ERROR_PARAMETERS, _s('Incorrect action condition ip "%1$s".', $condition['value']));
                         }
                     }
                     break;
                 case CONDITION_TYPE_DSERVICE_TYPE:
                     if (zbx_empty($condition['value'])) {
                         self::exception(ZBX_API_ERROR_PARAMETERS, _('Empty action condition.'));
                     } elseif (!$discoveryCheckTypeValidator->validate($condition['value'])) {
                         self::exception(ZBX_API_ERROR_PARAMETERS, _('Incorrect action condition discovery check.'));
                     }
                     break;
                 case CONDITION_TYPE_DSERVICE_PORT:
                     if (zbx_empty($condition['value'])) {
                         self::exception(ZBX_API_ERROR_PARAMETERS, _('Empty action condition.'));
                     } elseif (!validate_port_list($condition['value'])) {
                         self::exception(ZBX_API_ERROR_PARAMETERS, _s('Incorrect action condition port "%1$s".', $condition['value']));
                     }
                     break;
                 case CONDITION_TYPE_DSTATUS:
                     if (zbx_empty($condition['value'])) {
                         self::exception(ZBX_API_ERROR_PARAMETERS, _('Empty action condition.'));
                     } elseif (!$discoveryObjectStatusValidator->validate($condition['value'])) {
                         self::exception(ZBX_API_ERROR_PARAMETERS, _('Incorrect action condition discovery status.'));
                     }
                     break;
                 case CONDITION_TYPE_MAINTENANCE:
                     if (!zbx_empty($condition['value'])) {
                         self::exception(ZBX_API_ERROR_PARAMETERS, _('Maintenance action condition value must be empty.'));
                     }
                     break;
                 case CONDITION_TYPE_TRIGGER_SEVERITY:
                     if (zbx_empty($condition['value'])) {
                         self::exception(ZBX_API_ERROR_PARAMETERS, _('Empty action condition.'));
                     } elseif (!$triggerSeverityValidator->validate($condition['value'])) {
                         self::exception(ZBX_API_ERROR_PARAMETERS, _('Incorrect action condition trigger severity.'));
                     }
                     break;
                 case CONDITION_TYPE_TRIGGER_VALUE:
                     if (zbx_empty($condition['value'])) {
                         self::exception(ZBX_API_ERROR_PARAMETERS, _('Empty action condition.'));
                     } elseif (!$triggerValueValidator->validate($condition['value'])) {
                         self::exception(ZBX_API_ERROR_PARAMETERS, _('Incorrect action condition trigger value.'));
                     }
                     break;
                 case CONDITION_TYPE_EVENT_TYPE:
                     if (zbx_empty($condition['value'])) {
                         self::exception(ZBX_API_ERROR_PARAMETERS, _('Empty action condition.'));
                     } elseif (!$eventTypeValidator->validate($condition['value'])) {
                         self::exception(ZBX_API_ERROR_PARAMETERS, _('Incorrect action condition event type.'));
                     }
                     break;
                 case CONDITION_TYPE_TRIGGER_NAME:
                 case CONDITION_TYPE_NODE:
                 case CONDITION_TYPE_DUPTIME:
                 case CONDITION_TYPE_DVALUE:
                 case CONDITION_TYPE_APPLICATION:
                 case CONDITION_TYPE_HOST_NAME:
                 case CONDITION_TYPE_HOST_METADATA:
                     if (zbx_empty($condition['value'])) {
                         self::exception(ZBX_API_ERROR_PARAMETERS, _('Empty action condition.'));
                     }
                     break;
                 default:
                     self::exception(ZBX_API_ERROR_PARAMETERS, _('Incorrect action condition type.'));
                     break;
             }
         }
     }
     if (!API::HostGroup()->isWritable($hostGroupIdsAll)) {
         self::exception(ZBX_API_ERROR_PARAMETERS, _('Incorrect action condition host group. Host group does not exist or you have no access to it.'));
     }
     if (!API::Host()->isWritable($hostIdsAll)) {
         self::exception(ZBX_API_ERROR_PARAMETERS, _('Incorrect action condition host. Host does not exist or you have no access to it.'));
     }
     if (!API::Template()->isWritable($templateIdsAll)) {
         self::exception(ZBX_API_ERROR_PARAMETERS, _('Incorrect action condition template. Template does not exist or you have no access to it.'));
     }
     if (!API::Trigger()->isWritable($triggerIdsAll)) {
         self::exception(ZBX_API_ERROR_PARAMETERS, _('Incorrect action condition trigger. Trigger does not exist or you have no access to it.'));
     }
     if (!API::DRule()->isWritable($discoveryRuleIdsAll)) {
         self::exception(ZBX_API_ERROR_PARAMETERS, _('Incorrect action condition discovery rule. Discovery rule does not exist or you have no access to it.'));
     }
     if (!API::DCheck()->isWritable($proxyIdsAll)) {
         self::exception(ZBX_API_ERROR_PARAMETERS, _('Incorrect action condition discovery check. Discovery check does not exist or you have no access to it.'));
     }
     if (!API::Proxy()->isWritable($proxyidsAll)) {
         self::exception(ZBX_API_ERROR_PARAMETERS, _('Incorrect action condition proxy. Proxy does not exist or you have no access to it.'));
     }
     return true;
 }
Ejemplo n.º 2
0
/**
 * Validate items array for bar reports - IDs, color, permissions, etc.
 * Color validation for graph items is only for "Distribution of values for multiple periods" section ($config == 1).
 * Automatically set caption like item name if none is set. If no axis side is set, set LEFT side as default.
 *
 * @param array $items
 *
 * @return mixed	valid items array on success or false on failure
 */
function validateBarReportItems($items = array())
{
    $config = get_request('config', 1);
    if (!isset($items) || !$items) {
        return false;
    }
    $fields = array('itemid', 'calc_fnc');
    if ($config == 1) {
        array_push($fields, 'color');
    }
    foreach ($items as $item) {
        foreach ($fields as $field) {
            if (!isset($item[$field])) {
                show_error_message(_s('Missing "%1$s" field for item.', $field));
                return false;
            }
        }
        $itemIds[$item['itemid']] = $item['itemid'];
    }
    $validItems = API::Item()->get(array('nodeids' => get_current_nodeid(true), 'itemids' => $itemIds, 'webitems' => true, 'output' => array('name', 'value_type'), 'preservekeys' => true, 'selectHosts' => array('name')));
    $items = zbx_toHash($items, 'itemid');
    $allowedValueTypes = array(ITEM_VALUE_TYPE_FLOAT, ITEM_VALUE_TYPE_UINT64);
    foreach ($validItems as &$item) {
        if (!in_array($item['value_type'], $allowedValueTypes)) {
            show_error_message(_s('Cannot add a non-numeric item "%1$s".', $item['name']));
            return false;
        }
        // add host name and set caption like item name for valid items
        $item['host'] = reset($item['hosts']);
        unset($item['hosts']);
        // if no caption set, set default caption like item name
        if (!isset($items[$item['itemid']]['caption']) || zbx_empty($items[$item['itemid']]['caption'])) {
            $item['caption'] = $item['name'];
        } else {
            $validItems[$item['itemid']]['caption'] = $items[$item['itemid']]['caption'];
        }
        if (!isset($items[$item['itemid']]['axisside']) || zbx_empty($items[$item['itemid']]['axisside'])) {
            $items[$item['itemid']]['axisside'] = GRAPH_YAXIS_SIDE_LEFT;
        }
    }
    unset($item);
    // check axis value. 0 = count
    $calcFncValidator = new CSetValidator(array('values' => array(0, CALC_FNC_MIN, CALC_FNC_AVG, CALC_FNC_MAX)));
    $axisValidator = new CSetValidator(array('values' => array(GRAPH_YAXIS_SIDE_LEFT, GRAPH_YAXIS_SIDE_RIGHT)));
    if ($config == 1) {
        $colorValidator = new CColorValidator();
    }
    foreach ($items as $item) {
        if (!$calcFncValidator->validate($item['calc_fnc'])) {
            show_error_message(_s('Incorrect value for field "%1$s".', 'calc_fnc'));
            return false;
        }
        if (!$axisValidator->validate($item['axisside'])) {
            show_error_message(_s('Incorrect value for field "%1$s".', 'axisside'));
            return false;
        }
        if ($config == 1) {
            if (!$colorValidator->validate($item['color'])) {
                show_error_message($colorValidator->getError());
                return false;
            }
            $validItems[$item['itemid']]['color'] = $item['color'];
        }
        $validItems[$item['itemid']]['calc_fnc'] = $item['calc_fnc'];
        $validItems[$item['itemid']]['axisside'] = $item['axisside'];
    }
    return $validItems;
}
Ejemplo n.º 3
0
 /**
  * Validates the input parameters for the get() method.
  *
  * @throws APIException     if the input is invalid
  *
  * @param array     $options
  *
  * @return void
  */
 protected function validateGet(array $options)
 {
     $sourceValidator = new CSetValidator(array('values' => array_keys(eventSource())));
     if (!$sourceValidator->validate($options['source'])) {
         self::exception(ZBX_API_ERROR_PARAMETERS, _('Incorrect source value.'));
     }
     $objectValidator = new CSetValidator(array('values' => array_keys(eventObject())));
     if (!$objectValidator->validate($options['object'])) {
         self::exception(ZBX_API_ERROR_PARAMETERS, _('Incorrect object value.'));
     }
     $sourceObjectValidator = new CEventSourceObjectValidator();
     if (!$sourceObjectValidator->validate(array('source' => $options['source'], 'object' => $options['object']))) {
         self::exception(ZBX_API_ERROR_PARAMETERS, $sourceObjectValidator->getError());
     }
 }