private static function dcheck($action, $params) { CDCheck::$error = array(); switch ($action) { default: $result = call_user_func(array('CDCheck', $action), $params); } self::$result = $result; }
/** * Get drule data * * @static * @param array $options * @return array */ public static function get($options = array()) { global $USER_DETAILS; $result = array(); $nodeCheck = false; $user_type = $USER_DETAILS['type']; $result = array(); $sort_columns = array('druleid', 'name'); // allowed columns for sorting $subselects_allowed_outputs = array(API_OUTPUT_REFER, API_OUTPUT_EXTEND); // allowed output options for [ select_* ] params $sql_parts = array('select' => array('drules' => 'dr.druleid'), 'from' => array('drules' => 'drules dr'), 'where' => array(), 'group' => array(), 'order' => array(), 'limit' => null); $def_options = array('nodeids' => null, 'druleids' => null, 'dhostids' => null, 'dserviceids' => null, 'dcheckids' => null, 'editable' => null, 'selectDHosts' => null, 'selectDServices' => null, 'selectDChecks' => null, 'filter' => null, 'search' => null, 'startSearch' => null, 'excludeSearch' => null, 'output' => API_OUTPUT_REFER, 'countOutput' => null, 'groupCount' => null, 'preservekeys' => null, 'sortfield' => '', 'sortorder' => '', 'limit' => null, 'limitSelects' => null); $options = zbx_array_merge($def_options, $options); // editable + PERMISSION CHECK if (USER_TYPE_SUPER_ADMIN == $user_type) { } else { if (is_null($options['editable']) && $USER_DETAILS['type'] == USER_TYPE_ZABBIX_ADMIN) { } else { if (!is_null($options['editable']) && $USER_DETAILS['type'] != USER_TYPE_SUPER_ADMIN) { return array(); } } } // nodeids $nodeids = !is_null($options['nodeids']) ? $options['nodeids'] : get_current_nodeid(); // druleids if (!is_null($options['druleids'])) { zbx_value2array($options['druleids']); $sql_parts['where']['druleid'] = DBcondition('dr.druleid', $options['druleids']); if (!$nodeCheck) { $nodeCheck = true; $sql_parts['where'][] = DBin_node('dr.druleid', $nodeids); } } // dhostids if (!is_null($options['dhostids'])) { zbx_value2array($options['dhostids']); if ($options['output'] != API_OUTPUT_SHORTEN) { $sql_parts['select']['dhostid'] = 'dh.dhostid'; } $sql_parts['from']['dhosts'] = 'dhosts dh'; $sql_parts['where']['dhostid'] = DBcondition('dh.dhostid', $options['dhostids']); $sql_parts['where']['dhdr'] = 'dh.druleid=dr.druleid'; if (!is_null($options['groupCount'])) { $sql_parts['group']['dhostid'] = 'dh.dhostid'; } if (!$nodeCheck) { $nodeCheck = true; $sql_parts['where'][] = DBin_node('dh.dhostid', $nodeids); } } // dserviceids if (!is_null($options['dserviceids'])) { zbx_value2array($options['dserviceids']); if ($options['output'] != API_OUTPUT_SHORTEN) { $sql_parts['select']['dserviceid'] = 'ds.dserviceid'; } $sql_parts['from']['dhosts'] = 'dhosts dh'; $sql_parts['from']['dservices'] = 'dservices ds'; $sql_parts['where']['dserviceid'] = DBcondition('ds.dserviceid', $options['dserviceids']); $sql_parts['where']['dhdr'] = 'dh.druleid=dr.druleid'; $sql_parts['where']['dhds'] = 'dh.dhostid=ds.dhostid'; if (!is_null($options['groupCount'])) { $sql_parts['group']['dserviceid'] = 'ds.dserviceid'; } if (!$nodeCheck) { $nodeCheck = true; $sql_parts['where'][] = DBin_node('ds.dserviceid', $nodeids); } } // node check !!!!! // should be last, after all ****IDS checks if (!$nodeCheck) { $nodeCheck = true; $sql_parts['where'][] = DBin_node('dr.druleid', $nodeids); } // output if ($options['output'] == API_OUTPUT_EXTEND) { $sql_parts['select']['drules'] = 'dr.*'; } // countOutput if (!is_null($options['countOutput'])) { $options['sortfield'] = ''; $sql_parts['select'] = array('count(DISTINCT dr.druleid) as rowscount'); //groupCount if (!is_null($options['groupCount'])) { foreach ($sql_parts['group'] as $key => $fields) { $sql_parts['select'][$key] = $fields; } } } // search if (!is_null($options['search'])) { zbx_db_search('drules dr', $options, $sql_parts); } // filter if (is_array($options['filter'])) { zbx_db_filter('drules dr', $options, $sql_parts); } // search if (is_array($options['search'])) { zbx_db_search('drules dr', $options, $sql_parts); } // order // restrict not allowed columns for sorting $options['sortfield'] = str_in_array($options['sortfield'], $sort_columns) ? $options['sortfield'] : ''; if (!zbx_empty($options['sortfield'])) { $sortorder = $options['sortorder'] == ZBX_SORT_DOWN ? ZBX_SORT_DOWN : ZBX_SORT_UP; $sql_parts['order'][] = 'dr.' . $options['sortfield'] . ' ' . $sortorder; if (!str_in_array('dr.' . $options['sortfield'], $sql_parts['select']) && !str_in_array('dr.*', $sql_parts['select'])) { $sql_parts['select'][] = 'dr.' . $options['sortfield']; } } // limit if (zbx_ctype_digit($options['limit']) && $options['limit']) { $sql_parts['limit'] = $options['limit']; } //------------ $druleids = array(); $sql_parts['select'] = array_unique($sql_parts['select']); $sql_parts['from'] = array_unique($sql_parts['from']); $sql_parts['where'] = array_unique($sql_parts['where']); $sql_parts['group'] = array_unique($sql_parts['group']); $sql_parts['order'] = array_unique($sql_parts['order']); $sql_select = ''; $sql_from = ''; $sql_where = ''; $sql_group = ''; $sql_order = ''; if (!empty($sql_parts['select'])) { $sql_select .= implode(',', $sql_parts['select']); } if (!empty($sql_parts['from'])) { $sql_from .= implode(',', $sql_parts['from']); } if (!empty($sql_parts['where'])) { $sql_where .= implode(' AND ', $sql_parts['where']); } if (!empty($sql_parts['group'])) { $sql_where .= ' GROUP BY ' . implode(',', $sql_parts['group']); } if (!empty($sql_parts['order'])) { $sql_order .= ' ORDER BY ' . implode(',', $sql_parts['order']); } $sql_limit = $sql_parts['limit']; $sql = 'SELECT ' . zbx_db_distinct($sql_parts) . ' ' . $sql_select . ' FROM ' . $sql_from . ' WHERE ' . $sql_where . $sql_group . $sql_order; $db_res = DBselect($sql, $sql_limit); while ($drule = DBfetch($db_res)) { if (!is_null($options['countOutput'])) { if (!is_null($options['groupCount'])) { $result[] = $drule; } else { $result = $drule['rowscount']; } } else { if ($options['output'] == API_OUTPUT_SHORTEN) { $result[$drule['druleid']] = array('druleid' => $drule['druleid']); } else { $druleids[$drule['druleid']] = $drule['druleid']; if (!is_null($options['selectDHosts']) && !isset($result[$drule['druleid']]['dhosts'])) { $result[$drule['druleid']]['dhosts'] = array(); } if (!is_null($options['selectDChecks']) && !isset($result[$drule['druleid']]['dchecks'])) { $result[$drule['druleid']]['dchecks'] = array(); } if (!is_null($options['selectDServices']) && !isset($result[$drule['druleid']]['dservices'])) { $result[$drule['druleid']]['dservices'] = array(); } // dhostids if (isset($drule['dhostid']) && is_null($options['selectDHosts'])) { if (!isset($result[$drule['druleid']]['dhosts'])) { $result[$drule['druleid']]['dhosts'] = array(); } $result[$drule['druleid']]['dhosts'][] = array('dhostid' => $drule['dhostid']); unset($drule['dhostid']); } // dchecks if (isset($drule['dcheckid']) && is_null($options['selectDChecks'])) { if (!isset($result[$drule['druleid']]['dchecks'])) { $result[$drule['druleid']]['dchecks'] = array(); } $result[$drule['druleid']]['dchecks'][] = array('dcheckid' => $drule['dcheckid']); unset($drule['dcheckid']); } // dservices if (isset($drule['dserviceid']) && is_null($options['selectDServices'])) { if (!isset($result[$drule['druleid']]['dservices'])) { $result[$drule['druleid']]['dservices'] = array(); } $result[$drule['druleid']]['dservices'][] = array('dserviceid' => $drule['dserviceid']); unset($drule['dserviceid']); } if (!isset($result[$drule['druleid']])) { $result[$drule['druleid']] = array(); } $result[$drule['druleid']] += $drule; } } } COpt::memoryPick(); if ($options['output'] != API_OUTPUT_EXTEND || !is_null($options['countOutput'])) { if (is_null($options['preservekeys'])) { $result = zbx_cleanHashes($result); } return $result; } // Adding Objects // Adding Discovery Checks if (!is_null($options['selectDChecks'])) { $obj_params = array('nodeids' => $nodeids, 'dhostids' => $dhostids, 'preservekeys' => 1); if (is_array($options['selectDChecks']) || str_in_array($options['selectDChecks'], $subselects_allowed_outputs)) { $obj_params['output'] = $options['selectDChecks']; $dchecks = CDCheck::get($obj_params); if (!is_null($options['limitSelects'])) { order_result($dchecks, 'name'); } foreach ($dchecks as $dcheckid => $dcheck) { unset($dchecks[$dcheckid]['dhosts']); $count = array(); foreach ($dcheck['dhosts'] as $dnum => $dhost) { if (!is_null($options['limitSelects'])) { if (!isset($count[$dhost['dhostid']])) { $count[$dhost['dhostid']] = 0; } $count[$dhost['dhostid']]++; if ($count[$dhost['dhostid']] > $options['limitSelects']) { continue; } } $result[$dhost['dhostid']]['dchecks'][] =& $dchecks[$dcheckid]; } } } else { if (API_OUTPUT_COUNT == $options['selectDChecks']) { $obj_params['countOutput'] = 1; $obj_params['groupCount'] = 1; $dchecks = CDCheck::get($obj_params); $dchecks = zbx_toHash($dchecks, 'druleid'); foreach ($result as $dhostid => $dhost) { if (isset($dchecks[$druleid])) { $result[$dhostid]['dchecks'] = $dchecks[$druleid]['rowscount']; } else { $result[$dhostid]['dchecks'] = 0; } } } } } // Adding Discovery Hosts if (!is_null($options['selectDHosts'])) { $obj_params = array('nodeids' => $nodeids, 'druleids' => $druleids, 'preservekeys' => 1); if (is_array($options['selectDHosts']) || str_in_array($options['selectDHosts'], $subselects_allowed_outputs)) { $obj_params['output'] = $options['selectDHosts']; $dhosts = CDHost::get($obj_params); if (!is_null($options['limitSelects'])) { order_result($dhosts, 'name'); } foreach ($dhosts as $dhostid => $dhost) { unset($dhosts[$dhostid]['drules']); foreach ($dhost['drules'] as $dnum => $drule) { if (!is_null($options['limitSelects'])) { if (!isset($count[$drule['druleid']])) { $count[$drule['druleid']] = 0; } $count[$drule['druleid']]++; if ($count[$drule['druleid']] > $options['limitSelects']) { continue; } } $result[$drule['druleid']]['dhosts'][] =& $dhosts[$dhostid]; } } } else { if (API_OUTPUT_COUNT == $options['selectDHosts']) { $obj_params['countOutput'] = 1; $obj_params['groupCount'] = 1; $dhosts = CDHost::get($obj_params); $dhosts = zbx_toHash($dhosts, 'druleid'); foreach ($result as $druleid => $drule) { if (isset($dhosts[$druleid])) { $result[$druleid]['dhosts'] = $dhosts[$druleid]['rowscount']; } else { $result[$druleid]['dhosts'] = 0; } } } } } COpt::memoryPick(); // removing keys (hash -> array) if (is_null($options['preservekeys'])) { $result = zbx_cleanHashes($result); } return $result; }