zabbix-agent2/src/zabbix_server/actions.c
/******************************************************************************
 *                                                                            *
 * Function: check_hostname_metadata_condition                                *
 *                                                                            *
 * Purpose: check metadata or host condition for auto registration            *
 *                                                                            *
 * Parameters: esc_events - [IN] events to check                              *
 *             condition  - [IN/OUT] condition for matching, outputs          *
 *                                   event ids that match condition           *
 *                                                                            *
 * Return value: SUCCEED - supported operator                                 *
 *               NOTSUPPORTED - not supported operator                        *
 *                                                                            *
 ******************************************************************************/
check_hostname_metadata_condition

Purpose: check metadata or host condition for auto registration

Parameters: esc_events - [IN] events to check
condition  - [IN/OUT] condition for matching, outputs
event ids that match condition

Return value: SUCCEED - supported operator
NOTSUPPORTED - not supported operator
check_hostname_metadata_condition Purpose : check metadata or host condition for auto registration Parameters : esc_events - [ IN ] events to check condition - [ IN / OUT ] condition for matching outputs event ids that match condition Return value : SUCCEED - supported operator NOTSUPPORTED - not supported operator
static int	check_hostname_metadata_condition(const zbx_vector_ptr_t *esc_events, zbx_condition_t *condition)
{
	char			*sql = NULL;
	size_t			sql_alloc = 0, sql_offset = 0;
	DB_RESULT		result;
	DB_ROW			row;
	int			object = EVENT_OBJECT_ZABBIX_ACTIVE;
	zbx_vector_uint64_t	objectids;
	const char		*condition_field;
	switch(condition->op)
	{
		case CONDITION_OPERATOR_LIKE:
		case CONDITION_OPERATOR_NOT_LIKE:
		case CONDITION_OPERATOR_REGEXP:
		case CONDITION_OPERATOR_NOT_REGEXP:
			break;
		default:
			return NOTSUPPORTED;
	}
	if (CONDITION_TYPE_HOST_NAME == condition->conditiontype)
		condition_field = "host";
	else
		condition_field = "host_metadata";
	zbx_vector_uint64_create(&objectids);
	get_object_ids(esc_events, &objectids);
	zbx_snprintf_alloc(&sql, &sql_alloc, &sql_offset,
			"select autoreg_hostid,%s"
			" from autoreg_host"
			" where",
			condition_field);
	DBadd_condition_alloc(&sql, &sql_alloc, &sql_offset, "autoreg_hostid", objectids.values, objectids.values_num);
	result = DBselect("%s", sql);
	while (NULL != (row = DBfetch(result)))
	{
		zbx_uint64_t	objectid;
		ZBX_STR2UINT64(objectid, row[0]);
		switch (condition->op)
		{
			case CONDITION_OPERATOR_LIKE:
				if (NULL != strstr(row[1], condition->value))
					add_condition_match(esc_events, condition, objectid, object);
				break;
			case CONDITION_OPERATOR_NOT_LIKE:
				if (NULL == strstr(row[1], condition->value))
					add_condition_match(esc_events, condition, objectid, object);
				break;
			case CONDITION_OPERATOR_REGEXP:
				if (NULL != zbx_regexp_match(row[1], condition->value, NULL))
					add_condition_match(esc_events, condition, objectid, object);
				break;
			case CONDITION_OPERATOR_NOT_REGEXP:
				if (NULL == zbx_regexp_match(row[1], condition->value, NULL))
					add_condition_match(esc_events, condition, objectid, object);
				break;
		}
	}
	DBfree_result(result);
	zbx_vector_uint64_destroy(&objectids);
	zbx_free(sql);
	return SUCCEED;
}
static int check_hostname_metadata_condition ( const zbx_vector_ptr_t * esc_events , zbx_condition_t * condition ) { char * sql = NULL ; size_t sql_alloc = 0 , sql_offset = 0 ; DB_RESULT result ; DB_ROW row ; int object = EVENT_OBJECT_ZABBIX_ACTIVE ; zbx_vector_uint64_t objectids ; const char * condition_field ; switch ( condition -> op ) { case CONDITION_OPERATOR_LIKE : case CONDITION_OPERATOR_NOT_LIKE : case CONDITION_OPERATOR_REGEXP : case CONDITION_OPERATOR_NOT_REGEXP : break ; default : return NOTSUPPORTED ; } if ( CONDITION_TYPE_HOST_NAME == condition -> conditiontype ) condition_field = " " ; else condition_field = " " ; zbx_vector_uint64_create ( & objectids ) ; get_object_ids ( esc_events , & objectids ) ; zbx_snprintf_alloc ( & sql , & sql_alloc , & sql_offset , " " " " " " , condition_field ) ; DBadd_condition_alloc ( & sql , & sql_alloc , & sql_offset , " " , objectids . values , objectids . values_num ) ; result = DBselect ( " " , sql ) ; while ( NULL != ( row = DBfetch ( result ) ) ) { zbx_uint64_t objectid ; ZBX_STR2UINT64 ( objectid , row [ 0 ] ) ; switch ( condition -> op ) { case CONDITION_OPERATOR_LIKE : if ( NULL != strstr ( row [ 1 ] , condition -> value ) ) add_condition_match ( esc_events , condition , objectid , object ) ; break ; case CONDITION_OPERATOR_NOT_LIKE : if ( NULL == strstr ( row [ 1 ] , condition -> value ) ) add_condition_match ( esc_events , condition , objectid , object ) ; break ; case CONDITION_OPERATOR_REGEXP : if ( NULL != zbx_regexp_match ( row [ 1 ] , condition -> value , NULL ) ) add_condition_match ( esc_events , condition , objectid , object ) ; break ; case CONDITION_OPERATOR_NOT_REGEXP : if ( NULL == zbx_regexp_match ( row [ 1 ] , condition -> value , NULL ) ) add_condition_match ( esc_events , condition , objectid , object ) ; break ; } } DBfree_result ( result ) ; zbx_vector_uint64_destroy ( & objectids ) ; zbx_free ( sql ) ; return SUCCEED ; }
Function: check_hostname_metadata_condition                                
                                                                            
 Purpose: check metadata or host condition for auto registration            
                                                                            
 Parameters: esc_events - [IN] events to check                              
             condition  - [IN/OUT] condition for matching, outputs          
                                   event ids that match condition           
                                                                            
 Return value: SUCCEED - supported operator                                 
               NOTSUPPORTED - not supported operator
Function : check_hostname_metadata_condition Purpose : check metadata or host condition for auto registration Parameters : esc_events - [ IN ] events to check condition - [ IN / OUT ] condition for matching outputs event ids that match condition Return value : SUCCEED - supported operator NOTSUPPORTED - not supported operator