zabbix-agent2/src/zabbix_server/actions.c
/******************************************************************************
 *                                                                            *
 * Function: process_actions_by_acknowledgements                              *
 *                                                                            *
 * Purpose: process actions for each acknowledgement in the array             *
 *                                                                            *
 * Parameters: event_ack        - [IN] vector for eventid/ackid pairs         *
 *                                                                            *
 ******************************************************************************/
process_actions_by_acknowledgements

Purpose: process actions for each acknowledgement in the array

Parameters: event_ack        - [IN] vector for eventid/ackid pairs
process_actions_by_acknowledgements Purpose : process actions for each acknowledgement in the array Parameters : event_ack - [ IN ] vector for eventid / ackid pairs
int	process_actions_by_acknowledgements(const zbx_vector_ptr_t *ack_tasks)
{
	zbx_vector_ptr_t	actions;
	zbx_hashset_t		uniq_conditions[EVENT_SOURCE_COUNT];
	int			i, j, k, processed_num = 0, knext = 0;
	zbx_vector_uint64_t	eventids;
	zbx_ack_task_t		*ack_task;
	zbx_vector_ptr_t	ack_escalations, events;
	zbx_ack_escalation_t	*ack_escalation;
	zbx_vector_ptr_t	esc_events[EVENT_SOURCE_COUNT];
	zbx_hashset_iter_t	iter;
	zbx_condition_t		*condition;
	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __func__);
	zbx_vector_ptr_create(&ack_escalations);
	for (i = 0; i < EVENT_SOURCE_COUNT; i++)
	{
		zbx_hashset_create(&uniq_conditions[i], 0, uniq_conditions_hash_func, uniq_conditions_compare_func);
		zbx_vector_ptr_create(&esc_events[i]);
	}
	zbx_vector_ptr_create(&actions);
	zbx_dc_get_actions_eval(&actions, ZBX_ACTION_OPCLASS_ACKNOWLEDGE);
	prepare_actions_conditions_eval(&actions, uniq_conditions);
	if (0 == actions.values_num)
		goto out;
	zbx_vector_uint64_create(&eventids);
	for (i = 0; i < ack_tasks->values_num; i++)
	{
		ack_task = (zbx_ack_task_t *)ack_tasks->values[i];
		zbx_vector_uint64_append(&eventids, ack_task->eventid);
	}
	zbx_vector_uint64_sort(&eventids, ZBX_DEFAULT_UINT64_COMPARE_FUNC);
	zbx_vector_uint64_uniq(&eventids, ZBX_DEFAULT_UINT64_COMPARE_FUNC);
	zbx_vector_ptr_create(&events);
	zbx_db_get_events_by_eventids(&eventids, &events);
	for (i = 0; i < events.values_num; i++)
	{
		DB_EVENT	*event = (DB_EVENT *)events.values[i];
		zbx_vector_ptr_append(&esc_events[event->source], (void*)event);
	}
	for (i = 0; i < EVENT_SOURCE_COUNT; i++)
	{
		if (0 == esc_events[i].values_num)
			continue;
		zbx_vector_ptr_sort(&esc_events[i], compare_events);
		zbx_hashset_iter_reset(&uniq_conditions[i], &iter);
		while (NULL != (condition = (zbx_condition_t *)zbx_hashset_iter_next(&iter)))
			check_events_condition(&esc_events[i], i, condition);
	}
	for (i = 0; i < eventids.values_num; i++)
	{
		int 		kcurr = knext;
		DB_EVENT	*event = (DB_EVENT *)events.values[i];
		while (knext < ack_tasks->values_num)
		{
			ack_task = (zbx_ack_task_t *)ack_tasks->values[knext];
			if (ack_task->eventid != event->eventid)
				break;
			knext++;
		}
		if (0 == event->eventid || 0 == event->trigger.triggerid)
			continue;
		for (j = 0; j < actions.values_num; j++)
		{
			zbx_action_eval_t	*action = (zbx_action_eval_t *)actions.values[j];
			if (action->eventsource != event->source)
				continue;
			if (SUCCEED != check_action_conditions(event->eventid, action))
				continue;
			for (k = kcurr; k < knext; k++)
			{
				ack_task = (zbx_ack_task_t *)ack_tasks->values[k];
				ack_escalation = (zbx_ack_escalation_t *)zbx_malloc(NULL, sizeof(zbx_ack_escalation_t));
				ack_escalation->taskid = ack_task->taskid;
				ack_escalation->acknowledgeid = ack_task->acknowledgeid;
				ack_escalation->actionid = action->actionid;
				ack_escalation->eventid = event->eventid;
				ack_escalation->triggerid = event->trigger.triggerid;
				zbx_vector_ptr_append(&ack_escalations, ack_escalation);
			}
		}
	}
	if (0 != ack_escalations.values_num)
	{
		zbx_db_insert_t	db_insert;
		zbx_db_insert_prepare(&db_insert, "escalations", "escalationid", "actionid", "status", "triggerid",
						"itemid", "eventid", "r_eventid", "acknowledgeid", NULL);
		zbx_vector_ptr_sort(&ack_escalations, ZBX_DEFAULT_UINT64_PTR_COMPARE_FUNC);
		for (i = 0; i < ack_escalations.values_num; i++)
		{
			ack_escalation = (zbx_ack_escalation_t *)ack_escalations.values[i];
			zbx_db_insert_add_values(&db_insert, __UINT64_C(0), ack_escalation->actionid,
				(int)ESCALATION_STATUS_ACTIVE, ack_escalation->triggerid, __UINT64_C(0),
				ack_escalation->eventid, __UINT64_C(0), ack_escalation->acknowledgeid);
		}
		zbx_db_insert_autoincrement(&db_insert, "escalationid");
		zbx_db_insert_execute(&db_insert);
		zbx_db_insert_clean(&db_insert);
		processed_num = ack_escalations.values_num;
	}
	zbx_vector_ptr_clear_ext(&events, (zbx_clean_func_t)zbx_db_free_event);
	zbx_vector_ptr_destroy(&events);
	zbx_vector_uint64_destroy(&eventids);
out:
	for (i = 0; i < EVENT_SOURCE_COUNT; i++)
	{
		zbx_vector_ptr_destroy(&esc_events[i]);
		conditions_eval_clean(&uniq_conditions[i]);
		zbx_hashset_destroy(&uniq_conditions[i]);
	}
	zbx_vector_ptr_clear_ext(&actions, (zbx_clean_func_t)zbx_action_eval_free);
	zbx_vector_ptr_destroy(&actions);
	zbx_vector_ptr_clear_ext(&ack_escalations, zbx_ptr_free);
	zbx_vector_ptr_destroy(&ack_escalations);
	zabbix_log(LOG_LEVEL_DEBUG, "End of %s() processed_num:%d", __func__, processed_num);
	return processed_num;
}
int process_actions_by_acknowledgements ( const zbx_vector_ptr_t * ack_tasks ) { zbx_vector_ptr_t actions ; zbx_hashset_t uniq_conditions [ EVENT_SOURCE_COUNT ] ; int i , j , k , processed_num = 0 , knext = 0 ; zbx_vector_uint64_t eventids ; zbx_ack_task_t * ack_task ; zbx_vector_ptr_t ack_escalations , events ; zbx_ack_escalation_t * ack_escalation ; zbx_vector_ptr_t esc_events [ EVENT_SOURCE_COUNT ] ; zbx_hashset_iter_t iter ; zbx_condition_t * condition ; zabbix_log ( LOG_LEVEL_DEBUG , " " , __func__ ) ; zbx_vector_ptr_create ( & ack_escalations ) ; for ( i = 0 ; i < EVENT_SOURCE_COUNT ; i ++ ) { zbx_hashset_create ( & uniq_conditions [ i ] , 0 , uniq_conditions_hash_func , uniq_conditions_compare_func ) ; zbx_vector_ptr_create ( & esc_events [ i ] ) ; } zbx_vector_ptr_create ( & actions ) ; zbx_dc_get_actions_eval ( & actions , ZBX_ACTION_OPCLASS_ACKNOWLEDGE ) ; prepare_actions_conditions_eval ( & actions , uniq_conditions ) ; if ( 0 == actions . values_num ) goto out ; zbx_vector_uint64_create ( & eventids ) ; for ( i = 0 ; i < ack_tasks -> values_num ; i ++ ) { ack_task = ( zbx_ack_task_t * ) ack_tasks -> values [ i ] ; zbx_vector_uint64_append ( & eventids , ack_task -> eventid ) ; } zbx_vector_uint64_sort ( & eventids , ZBX_DEFAULT_UINT64_COMPARE_FUNC ) ; zbx_vector_uint64_uniq ( & eventids , ZBX_DEFAULT_UINT64_COMPARE_FUNC ) ; zbx_vector_ptr_create ( & events ) ; zbx_db_get_events_by_eventids ( & eventids , & events ) ; for ( i = 0 ; i < events . values_num ; i ++ ) { DB_EVENT * event = ( DB_EVENT * ) events . values [ i ] ; zbx_vector_ptr_append ( & esc_events [ event -> source ] , ( void * ) event ) ; } for ( i = 0 ; i < EVENT_SOURCE_COUNT ; i ++ ) { if ( 0 == esc_events [ i ] . values_num ) continue ; zbx_vector_ptr_sort ( & esc_events [ i ] , compare_events ) ; zbx_hashset_iter_reset ( & uniq_conditions [ i ] , & iter ) ; while ( NULL != ( condition = ( zbx_condition_t * ) zbx_hashset_iter_next ( & iter ) ) ) check_events_condition ( & esc_events [ i ] , i , condition ) ; } for ( i = 0 ; i < eventids . values_num ; i ++ ) { int kcurr = knext ; DB_EVENT * event = ( DB_EVENT * ) events . values [ i ] ; while ( knext < ack_tasks -> values_num ) { ack_task = ( zbx_ack_task_t * ) ack_tasks -> values [ knext ] ; if ( ack_task -> eventid != event -> eventid ) break ; knext ++ ; } if ( 0 == event -> eventid || 0 == event -> trigger . triggerid ) continue ; for ( j = 0 ; j < actions . values_num ; j ++ ) { zbx_action_eval_t * action = ( zbx_action_eval_t * ) actions . values [ j ] ; if ( action -> eventsource != event -> source ) continue ; if ( SUCCEED != check_action_conditions ( event -> eventid , action ) ) continue ; for ( k = kcurr ; k < knext ; k ++ ) { ack_task = ( zbx_ack_task_t * ) ack_tasks -> values [ k ] ; ack_escalation = ( zbx_ack_escalation_t * ) zbx_malloc ( NULL , sizeof ( zbx_ack_escalation_t ) ) ; ack_escalation -> taskid = ack_task -> taskid ; ack_escalation -> acknowledgeid = ack_task -> acknowledgeid ; ack_escalation -> actionid = action -> actionid ; ack_escalation -> eventid = event -> eventid ; ack_escalation -> triggerid = event -> trigger . triggerid ; zbx_vector_ptr_append ( & ack_escalations , ack_escalation ) ; } } } if ( 0 != ack_escalations . values_num ) { zbx_db_insert_t db_insert ; zbx_db_insert_prepare ( & db_insert , " " , " " , " " , " " , " " , " " , " " , " " , " " , NULL ) ; zbx_vector_ptr_sort ( & ack_escalations , ZBX_DEFAULT_UINT64_PTR_COMPARE_FUNC ) ; for ( i = 0 ; i < ack_escalations . values_num ; i ++ ) { ack_escalation = ( zbx_ack_escalation_t * ) ack_escalations . values [ i ] ; zbx_db_insert_add_values ( & db_insert , __UINT64_C ( 0 ) , ack_escalation -> actionid , ( int ) ESCALATION_STATUS_ACTIVE , ack_escalation -> triggerid , __UINT64_C ( 0 ) , ack_escalation -> eventid , __UINT64_C ( 0 ) , ack_escalation -> acknowledgeid ) ; } zbx_db_insert_autoincrement ( & db_insert , " " ) ; zbx_db_insert_execute ( & db_insert ) ; zbx_db_insert_clean ( & db_insert ) ; processed_num = ack_escalations . values_num ; } zbx_vector_ptr_clear_ext ( & events , ( zbx_clean_func_t ) zbx_db_free_event ) ; zbx_vector_ptr_destroy ( & events ) ; zbx_vector_uint64_destroy ( & eventids ) ; out : for ( i = 0 ; i < EVENT_SOURCE_COUNT ; i ++ ) { zbx_vector_ptr_destroy ( & esc_events [ i ] ) ; conditions_eval_clean ( & uniq_conditions [ i ] ) ; zbx_hashset_destroy ( & uniq_conditions [ i ] ) ; } zbx_vector_ptr_clear_ext ( & actions , ( zbx_clean_func_t ) zbx_action_eval_free ) ; zbx_vector_ptr_destroy ( & actions ) ; zbx_vector_ptr_clear_ext ( & ack_escalations , zbx_ptr_free ) ; zbx_vector_ptr_destroy ( & ack_escalations ) ; zabbix_log ( LOG_LEVEL_DEBUG , " " , __func__ , processed_num ) ; return processed_num ; }
Function: process_actions_by_acknowledgements                              
                                                                            
 Purpose: process actions for each acknowledgement in the array             
                                                                            
 Parameters: event_ack        - [IN] vector for eventid/ackid pairs
Function : process_actions_by_acknowledgements Purpose : process actions for each acknowledgement in the array Parameters : event_ack - [ IN ] vector for eventid / ackid pairs