zabbix-agent2/src/zabbix_server/actions.c
/******************************************************************************
 *                                                                            *
 * Function: check_duptime_condition                                          *
 *                                                                            *
 * Purpose: check uptime condition for discovery                              *
 *                                                                            *
 * 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_duptime_condition

Purpose: check uptime condition for discovery

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_duptime_condition Purpose : check uptime condition for discovery 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_duptime_condition(const zbx_vector_ptr_t *esc_events, zbx_condition_t *condition)
{
	char			*sql = NULL;
	size_t			sql_alloc = 0, i;
	DB_RESULT		result;
	DB_ROW			row;
	int			objects[2] = {EVENT_OBJECT_DHOST, EVENT_OBJECT_DSERVICE};
	zbx_vector_uint64_t	objectids[2];
	int			condition_value_i;
	if (CONDITION_OPERATOR_LESS_EQUAL != condition->op && CONDITION_OPERATOR_MORE_EQUAL != condition->op)
		return NOTSUPPORTED;
	condition_value_i = atoi(condition->value);
	zbx_vector_uint64_create(&objectids[0]);
	zbx_vector_uint64_create(&objectids[1]);
	get_object_ids_discovery(esc_events, objectids);
	for (i = 0; i < (int)ARRSIZE(objects); i++)
	{
		size_t	sql_offset = 0;
		if (0 == objectids[i].values_num)
			continue;
		if (EVENT_OBJECT_DHOST == objects[i])
		{
			zbx_snprintf_alloc(&sql, &sql_alloc, &sql_offset,
					"select dhostid,status,lastup,lastdown"
					" from dhosts"
					" where");
			DBadd_condition_alloc(&sql, &sql_alloc, &sql_offset, "dhostid",
					objectids[i].values, objectids[i].values_num);
		}
		else	
		{
			zbx_snprintf_alloc(&sql, &sql_alloc, &sql_offset,
					"select dserviceid,status,lastup,lastdown"
					" from dservices"
					" where");
			DBadd_condition_alloc(&sql, &sql_alloc, &sql_offset, "dserviceid",
					objectids[i].values, objectids[i].values_num);
		}
		result = DBselect("%s", sql);
		while (NULL != (row = DBfetch(result)))
		{
			zbx_uint64_t	objectid;
			int		now, tmp_int;
			ZBX_STR2UINT64(objectid, row[0]);
			now = time(NULL);
			tmp_int = DOBJECT_STATUS_UP == atoi(row[1]) ? atoi(row[2]) : atoi(row[3]);
			switch (condition->op)
			{
				case CONDITION_OPERATOR_LESS_EQUAL:
					if (0 != tmp_int && (now - tmp_int) <= condition_value_i)
						add_condition_match(esc_events, condition, objectid, objects[i]);
					break;
				case CONDITION_OPERATOR_MORE_EQUAL:
					if (0 != tmp_int && (now - tmp_int) >= condition_value_i)
						add_condition_match(esc_events, condition, objectid, objects[i]);
					break;
			}
		}
		DBfree_result(result);
	}
	zbx_vector_uint64_destroy(&objectids[0]);
	zbx_vector_uint64_destroy(&objectids[1]);
	zbx_free(sql);
	return SUCCEED;
}
static int check_duptime_condition ( const zbx_vector_ptr_t * esc_events , zbx_condition_t * condition ) { char * sql = NULL ; size_t sql_alloc = 0 , i ; DB_RESULT result ; DB_ROW row ; int objects [ 2 ] = { EVENT_OBJECT_DHOST , EVENT_OBJECT_DSERVICE } ; zbx_vector_uint64_t objectids [ 2 ] ; int condition_value_i ; if ( CONDITION_OPERATOR_LESS_EQUAL != condition -> op && CONDITION_OPERATOR_MORE_EQUAL != condition -> op ) return NOTSUPPORTED ; condition_value_i = atoi ( condition -> value ) ; zbx_vector_uint64_create ( & objectids [ 0 ] ) ; zbx_vector_uint64_create ( & objectids [ 1 ] ) ; get_object_ids_discovery ( esc_events , objectids ) ; for ( i = 0 ; i < ( int ) ARRSIZE ( objects ) ; i ++ ) { size_t sql_offset = 0 ; if ( 0 == objectids [ i ] . values_num ) continue ; if ( EVENT_OBJECT_DHOST == objects [ i ] ) { zbx_snprintf_alloc ( & sql , & sql_alloc , & sql_offset , " " " " " " ) ; DBadd_condition_alloc ( & sql , & sql_alloc , & sql_offset , " " , objectids [ i ] . values , objectids [ i ] . values_num ) ; } else { zbx_snprintf_alloc ( & sql , & sql_alloc , & sql_offset , " " " " " " ) ; DBadd_condition_alloc ( & sql , & sql_alloc , & sql_offset , " " , objectids [ i ] . values , objectids [ i ] . values_num ) ; } result = DBselect ( " " , sql ) ; while ( NULL != ( row = DBfetch ( result ) ) ) { zbx_uint64_t objectid ; int now , tmp_int ; ZBX_STR2UINT64 ( objectid , row [ 0 ] ) ; now = time ( NULL ) ; tmp_int = DOBJECT_STATUS_UP == atoi ( row [ 1 ] ) ? atoi ( row [ 2 ] ) : atoi ( row [ 3 ] ) ; switch ( condition -> op ) { case CONDITION_OPERATOR_LESS_EQUAL : if ( 0 != tmp_int && ( now - tmp_int ) <= condition_value_i ) add_condition_match ( esc_events , condition , objectid , objects [ i ] ) ; break ; case CONDITION_OPERATOR_MORE_EQUAL : if ( 0 != tmp_int && ( now - tmp_int ) >= condition_value_i ) add_condition_match ( esc_events , condition , objectid , objects [ i ] ) ; break ; } } DBfree_result ( result ) ; } zbx_vector_uint64_destroy ( & objectids [ 0 ] ) ; zbx_vector_uint64_destroy ( & objectids [ 1 ] ) ; zbx_free ( sql ) ; return SUCCEED ; }
Function: check_duptime_condition                                          
                                                                            
 Purpose: check uptime condition for discovery                              
                                                                            
 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_duptime_condition Purpose : check uptime condition for discovery 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
/* EVENT_OBJECT_DSERVICE */