diff --git a/src/zabbix_server/housekeeper/housekeeper.c b/src/zabbix_server/housekeeper/housekeeper.c index ac235e3..c98f8fa 100644 --- a/src/zabbix_server/housekeeper/housekeeper.c +++ b/src/zabbix_server/housekeeper/housekeeper.c @@ -159,6 +159,10 @@ typedef struct } zbx_hk_history_rule_t; +/* Used in housekeeping_process_rule() and the caller */ +#define IDTYPE_UINT64 0 +#define IDTYPE_STR 1 + /* The history item rules, used for housekeeping history and trends tables */ /* The order of the rules must match the order of value types in zbx_item_value_type_t. */ static zbx_hk_history_rule_t hk_history_rules[] = { @@ -721,21 +725,27 @@ static int housekeeping_history_and_trends(int now) * * * Purpose: removes old records from a table according to the specified rule * * * - * Parameters: now - [IN] the current time in seconds * - * rule - [IN/OUT] the housekeeping rule specifying table to * + * Parameters: now - [IN] the current time in seconds * + * rule - [IN/OUT] the housekeeping rule specifying table to * * clean and the required data (fields, filters, time) * + * idtype - [IN] IDTYPE_UINT64 or IDTYPE_STR * * * * Return value: the number of deleted records * * * * Author: Andris Zeila * * * ******************************************************************************/ -static int housekeeping_process_rule(int now, zbx_hk_rule_t *rule) +static int housekeeping_process_rule(int now, zbx_hk_rule_t *rule, int idtype) { DB_RESULT result; DB_ROW row; int keep_from, deleted = 0; + if (idtype > IDTYPE_STR || idtype < IDTYPE_UINT64) { + zabbix_log(LOG_LEVEL_WARNING, "In %s(): Unknown ID type %d", __func__, idtype); + return deleted; + } + zabbix_log(LOG_LEVEL_DEBUG, "In %s() table:'%s' field_name:'%s' filter:'%s' min_clock:%d now:%d", __func__, rule->table, rule->field_name, rule->filter, rule->min_clock, now); @@ -760,10 +770,14 @@ static int housekeeping_process_rule(int now, zbx_hk_rule_t *rule) char buffer[MAX_STRING_LEN]; char *sql = NULL; size_t sql_alloc = 0, sql_offset; - zbx_vector_uint64_t ids; + zbx_vector_uint64_t ids_uint64; + zbx_vector_str_t ids_str; int ret; - zbx_vector_uint64_create(&ids); + if (IDTYPE_UINT64 == idtype) + zbx_vector_uint64_create(&ids_uint64); + else + zbx_vector_str_create(&ids_str); rule->min_clock = MIN(keep_from, rule->min_clock + HK_MAX_DELETE_PERIODS * hk_period); @@ -786,30 +800,52 @@ static int housekeeping_process_rule(int now, zbx_hk_rule_t *rule) while (NULL != (row = DBfetch(result))) { - zbx_uint64_t id; + if (IDTYPE_UINT64 == idtype) { + zbx_uint64_t id; - ZBX_STR2UINT64(id, row[0]); - zbx_vector_uint64_append(&ids, id); + ZBX_STR2UINT64(id, row[0]); + zbx_vector_uint64_append(&ids_uint64, id); + } else + zbx_vector_str_append(&ids_str, row[0]); } DBfree_result(result); - if (0 == ids.values_num) - break; + if (IDTYPE_UINT64 == idtype) { + if (0 == ids_uint64.values_num) + break; + } else { + if (0 == ids_str.values_num) + break; + } sql_offset = 0; zbx_snprintf_alloc(&sql, &sql_alloc, &sql_offset, "delete from %s where", rule->table); - DBadd_condition_alloc(&sql, &sql_alloc, &sql_offset, rule->field_name, ids.values, - ids.values_num); + + if (IDTYPE_UINT64 == idtype) { + DBadd_condition_alloc(&sql, &sql_alloc, &sql_offset, + rule->field_name, ids_uint64.values, ids_uint64.values_num); + } else { + DBadd_str_condition_alloc(&sql, &sql_alloc, &sql_offset, + rule->field_name, ids_str.values, ids_str.values_num); + } if (ZBX_DB_OK > (ret = DBexecute("%s", sql))) break; deleted += ret; - zbx_vector_uint64_clear(&ids); + + if (IDTYPE_UINT64 == idtype) + zbx_vector_uint64_clear(&ids_uint64); + else + zbx_vector_str_clear(&ids_str); } zbx_free(sql); - zbx_vector_uint64_destroy(&ids); + + if (IDTYPE_UINT64 == idtype) + zbx_vector_uint64_destroy(&ids_uint64); + else + zbx_vector_str_destroy(&ids_str); } zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%d", __func__, deleted); @@ -1075,7 +1111,7 @@ static int housekeeping_services(int now) static zbx_hk_rule_t rule = {"service_alarms", "servicealarmid", "", 0, &cfg.hk.services}; if (ZBX_HK_OPTION_ENABLED == cfg.hk.services_mode) - return housekeeping_process_rule(now, &rule); + return housekeeping_process_rule(now, &rule, IDTYPE_UINT64); return 0; } @@ -1085,7 +1121,7 @@ static int housekeeping_audit(int now) static zbx_hk_rule_t rule = {"auditlog", "auditid", "", 0, &cfg.hk.audit}; if (ZBX_HK_OPTION_ENABLED == cfg.hk.audit_mode) - return housekeeping_process_rule(now, &rule); + return housekeeping_process_rule(now, &rule, IDTYPE_STR); return 0; } @@ -1156,7 +1192,7 @@ static int housekeeping_events(int now) DBfree_result(result); } - deleted += housekeeping_process_rule(now, rule); + deleted += housekeeping_process_rule(now, rule, IDTYPE_UINT64); } return deleted;