zabbix-agent2/src/libs/zbxdbcache/dbsync.c
/******************************************************************************
 *                                                                            *
 * Function: zbx_dbsync_compare_triggers                                      *
 *                                                                            *
 * Purpose: compares triggers table with cached configuration data            *
 *                                                                            *
 * Parameter: sync - [OUT] the changeset                                      *
 *                                                                            *
 * Return value: SUCCEED - the changeset was successfully calculated          *
 *               FAIL    - otherwise                                          *
 *                                                                            *
 * Comment: The 16th and 17th fields (starting with 0) are placeholders for   *
 *          serialized expression/recovery expression.                        *
 *          The 18th field is placeholder for trigger timer flag (set if      *
 *          expression/recovery expression contains timer functions).         *
 *                                                                            *
 ******************************************************************************/
zbx_dbsync_compare_triggers

Purpose: compares triggers table with cached configuration data

Parameter: sync - [OUT] the changeset

Return value: SUCCEED - the changeset was successfully calculated
FAIL    - otherwise

Comment: The 16th and 17th fields (starting with 0) are placeholders for
serialized expression/recovery expression.
The 18th field is placeholder for trigger timer flag (set if
expression/recovery expression contains timer functions).
zbx_dbsync_compare_triggers Purpose : compares triggers table with cached configuration data Parameter : sync - [ OUT ] the changeset Return value : SUCCEED - the changeset was successfully calculated FAIL - otherwise Comment : The 16th and 17th fields ( starting with 0 ) are placeholders for serialized expression / recovery expression . The 18th field is placeholder for trigger timer flag ( set if expression / recovery expression contains timer functions ) .
int	zbx_dbsync_compare_triggers(zbx_dbsync_t *sync)
{
	DB_ROW			dbrow;
	DB_RESULT		result;
	zbx_hashset_t		ids;
	zbx_hashset_iter_t	iter;
	zbx_uint64_t		rowid;
	ZBX_DC_TRIGGER		*trigger;
	char			**row;
	if (NULL == (result = DBselect(
			"select t.triggerid,t.description,t.expression,t.error,t.priority,t.type,t.value,"
				"t.state,t.lastchange,t.status,t.recovery_mode,t.recovery_expression,"
				"t.correlation_mode,t.correlation_tag,t.opdata,t.event_name,null,null,null"
			" from triggers t"
			" where t.triggerid in (select distinct tg.triggerid"
				" from hosts h,items i,functions f,triggers tg"
				" where h.hostid=i.hostid"
					" and i.itemid=f.itemid"
					" and f.triggerid=tg.triggerid"
					" and h.status in (%d,%d)"
					" and tg.flags<>%d)",
			HOST_STATUS_MONITORED, HOST_STATUS_NOT_MONITORED,
			ZBX_FLAG_DISCOVERY_PROTOTYPE)))
	{
		return FAIL;
	}
	dbsync_prepare(sync, 19, dbsync_trigger_preproc_row);
	if (ZBX_DBSYNC_INIT == sync->mode)
	{
		sync->dbresult = result;
		return SUCCEED;
	}
	zbx_hashset_create(&ids, dbsync_env.cache->triggers.num_data, ZBX_DEFAULT_UINT64_HASH_FUNC,
			ZBX_DEFAULT_UINT64_COMPARE_FUNC);
	while (NULL != (dbrow = DBfetch(result)))
	{
		ZBX_STR2UINT64(rowid, dbrow[0]);
		zbx_hashset_insert(&ids, &rowid, sizeof(rowid));
		row = dbsync_preproc_row(sync, dbrow);
		if (NULL == (trigger = (ZBX_DC_TRIGGER *)zbx_hashset_search(&dbsync_env.cache->triggers, &rowid)))
		{
			dbsync_add_row(sync, rowid, ZBX_DBSYNC_ROW_ADD, row);
		}
		else
		{
			if (FAIL == dbsync_compare_trigger(trigger, row))
				dbsync_add_row(sync, rowid, ZBX_DBSYNC_ROW_UPDATE, row);
		}
	}
	zbx_hashset_iter_reset(&dbsync_env.cache->triggers, &iter);
	while (NULL != (trigger = (ZBX_DC_TRIGGER *)zbx_hashset_iter_next(&iter)))
	{
		if (NULL == zbx_hashset_search(&ids, &trigger->triggerid))
			dbsync_add_row(sync, trigger->triggerid, ZBX_DBSYNC_ROW_REMOVE, NULL);
	}
	zbx_hashset_destroy(&ids);
	DBfree_result(result);
	return SUCCEED;
}
int zbx_dbsync_compare_triggers ( zbx_dbsync_t * sync ) { DB_ROW dbrow ; DB_RESULT result ; zbx_hashset_t ids ; zbx_hashset_iter_t iter ; zbx_uint64_t rowid ; ZBX_DC_TRIGGER * trigger ; char * * row ; if ( NULL == ( result = DBselect ( " " " " " " " " " " " " " " " " " " " " " " , HOST_STATUS_MONITORED , HOST_STATUS_NOT_MONITORED , ZBX_FLAG_DISCOVERY_PROTOTYPE ) ) ) { return FAIL ; } dbsync_prepare ( sync , 19 , dbsync_trigger_preproc_row ) ; if ( ZBX_DBSYNC_INIT == sync -> mode ) { sync -> dbresult = result ; return SUCCEED ; } zbx_hashset_create ( & ids , dbsync_env . cache -> triggers . num_data , ZBX_DEFAULT_UINT64_HASH_FUNC , ZBX_DEFAULT_UINT64_COMPARE_FUNC ) ; while ( NULL != ( dbrow = DBfetch ( result ) ) ) { ZBX_STR2UINT64 ( rowid , dbrow [ 0 ] ) ; zbx_hashset_insert ( & ids , & rowid , sizeof ( rowid ) ) ; row = dbsync_preproc_row ( sync , dbrow ) ; if ( NULL == ( trigger = ( ZBX_DC_TRIGGER * ) zbx_hashset_search ( & dbsync_env . cache -> triggers , & rowid ) ) ) { dbsync_add_row ( sync , rowid , ZBX_DBSYNC_ROW_ADD , row ) ; } else { if ( FAIL == dbsync_compare_trigger ( trigger , row ) ) dbsync_add_row ( sync , rowid , ZBX_DBSYNC_ROW_UPDATE , row ) ; } } zbx_hashset_iter_reset ( & dbsync_env . cache -> triggers , & iter ) ; while ( NULL != ( trigger = ( ZBX_DC_TRIGGER * ) zbx_hashset_iter_next ( & iter ) ) ) { if ( NULL == zbx_hashset_search ( & ids , & trigger -> triggerid ) ) dbsync_add_row ( sync , trigger -> triggerid , ZBX_DBSYNC_ROW_REMOVE , NULL ) ; } zbx_hashset_destroy ( & ids ) ; DBfree_result ( result ) ; return SUCCEED ; }
Function: zbx_dbsync_compare_triggers                                      
                                                                            
 Purpose: compares triggers table with cached configuration data            
                                                                            
 Parameter: sync - [OUT] the changeset                                      
                                                                            
 Return value: SUCCEED - the changeset was successfully calculated          
               FAIL    - otherwise                                          
                                                                            
 Comment: The 16th and 17th fields (starting with 0) are placeholders for   
          serialized expression/recovery expression.
Function : zbx_dbsync_compare_triggers Purpose : compares triggers table with cached configuration data Parameter : sync - [ OUT ] the changeset Return value : SUCCEED - the changeset was successfully calculated FAIL - otherwise Comment : The 16th and 17th fields ( starting with 0 ) are placeholders for serialized expression / recovery expression .