Index: include/db.h =================================================================== --- include/db.h (revision 25526) +++ include/db.h (working copy) @@ -601,7 +601,7 @@ #define ZBX_DB_GET_HIST_COUNT 4 #define ZBX_DB_GET_HIST_DELTA 5 #define ZBX_DB_GET_HIST_VALUE 6 -char **DBget_history(zbx_uint64_t itemid, unsigned char value_type, int function, int clock_from, int clock_to, const char *field_name, int last_n); +char **DBget_history(zbx_uint64_t itemid, unsigned char value_type, int function, int delay, int clock_from, int clock_to, const char *field_name, int last_n); void DBfree_history(char **value); int DBtxn_status(); Index: src/zabbix_server/poller/checks_aggregate.c =================================================================== --- src/zabbix_server/poller/checks_aggregate.c (revision 25526) +++ src/zabbix_server/poller/checks_aggregate.c (working copy) @@ -142,7 +142,7 @@ const char *__function_name = "evaluate_aggregate"; char *sql = NULL; int sql_alloc = 1024, sql_offset = 0; - zbx_uint64_t itemid, *ids = NULL; + zbx_uint64_t itemid, delay, *ids = NULL; int ids_alloc = 0, ids_num = 0; DB_RESULT result; @@ -228,7 +228,7 @@ clock_from = time(NULL) - atoi(param); zbx_snprintf_alloc(&sql, &sql_alloc, &sql_offset, 256, - "select itemid,value_type" + "select itemid,value_type,delay" " from items" " where value_type in (%d,%d)" " and", @@ -240,9 +240,10 @@ while (NULL != (row = DBfetch(result))) { ZBX_STR2UINT64(itemid, row[0]); + ZBX_STR2UINT64(delay, row[2]); value_type = (unsigned char)atoi(row[1]); - h_value = DBget_history(itemid, value_type, item_func, clock_from, 0, NULL, 0); + h_value = DBget_history(itemid, value_type, item_func, clock_from, 0, delay, NULL, 0); if (NULL != h_value[0]) evaluate_one(&d, &num, grp_func, h_value[0], value_type); Index: src/libs/zbxserver/expression.c =================================================================== --- src/libs/zbxserver/expression.c (revision 25526) +++ src/libs/zbxserver/expression.c (working copy) @@ -946,7 +946,7 @@ if (FAIL == trigger_get_N_functionid(trigger, N_functionid, &functionid)) goto fail; - result = DBselect("select i.itemid,i.value_type" + result = DBselect("select i.itemid, i.delay, i.value_type" " from items i,functions f" " where i.itemid=f.itemid" " and f.functionid=" ZBX_FS_UI64, @@ -955,14 +955,16 @@ if (NULL != (row = DBfetch(result))) { zbx_uint64_t itemid; + zbx_uint64_t delay; unsigned char value_type; char **h_value; - if (ITEM_VALUE_TYPE_LOG == (value_type = (unsigned char)atoi(row[1]))) + if (ITEM_VALUE_TYPE_LOG == (value_type = (unsigned char)atoi(row[2]))) { ZBX_STR2UINT64(itemid, row[0]); + ZBX_STR2UINT64(delay, row[1]); - h_value = DBget_history(itemid, value_type, ZBX_DB_GET_HIST_VALUE, 0, 0, field_name, 1); + h_value = DBget_history(itemid, value_type, ZBX_DB_GET_HIST_VALUE, 0, 0, delay, field_name, 1); if (NULL != h_value[0]) { @@ -1010,7 +1012,7 @@ goto fail; result = DBselect( - "select i.itemid,i.value_type,i.valuemapid,i.units,i.lastvalue" + "select i.itemid,i.value_type,i.valuemapid,i.units,i.lastvalue, i.delay" " from items i,functions f" " where i.itemid=f.itemid" " and f.functionid=" ZBX_FS_UI64, @@ -1018,7 +1020,7 @@ if (NULL != (row = DBfetch(result)) && SUCCEED != DBis_null(row[4])) { - zbx_uint64_t itemid, valuemapid; + zbx_uint64_t itemid, valuemapid, delay; unsigned char value_type; char **h_value; char tmp[MAX_STRING_LEN]; @@ -1026,12 +1028,13 @@ ZBX_STR2UINT64(itemid, row[0]); value_type = atoi(row[1]); ZBX_STR2UINT64(valuemapid, row[2]); + ZBX_STR2UINT64(delay, row[5]); switch (value_type) { case ITEM_VALUE_TYPE_LOG: case ITEM_VALUE_TYPE_TEXT: - h_value = DBget_history(itemid, value_type, ZBX_DB_GET_HIST_VALUE, 0, 0, NULL, 1); + h_value = DBget_history(itemid, value_type, ZBX_DB_GET_HIST_VALUE, 0, 0, delay, NULL, 1); if (NULL != h_value[0]) *lastvalue = zbx_strdup(*lastvalue, h_value[0]); @@ -1097,7 +1100,7 @@ goto fail; result = DBselect( - "select i.itemid,i.value_type,i.valuemapid,i.units" + "select i.itemid,i.value_type,i.valuemapid,i.units,i.delay" " from items i,functions f" " where i.itemid=f.itemid" " and f.functionid=" ZBX_FS_UI64, @@ -1105,7 +1108,7 @@ if (NULL != (row = DBfetch(result))) { - zbx_uint64_t itemid, valuemapid; + zbx_uint64_t itemid, valuemapid, delay; unsigned char value_type; char **h_value; char tmp[MAX_STRING_LEN]; @@ -1113,8 +1116,9 @@ ZBX_STR2UINT64(itemid, row[0]); value_type = (unsigned char)atoi(row[1]); ZBX_STR2UINT64(valuemapid, row[2]); + ZBX_STR2UINT64(delay, row[4]); - h_value = DBget_history(itemid, value_type, ZBX_DB_GET_HIST_VALUE, 0, clock, NULL, 1); + h_value = DBget_history(itemid, value_type, ZBX_DB_GET_HIST_VALUE, 0, clock, delay, NULL, 1); if (NULL != h_value[0]) { Index: src/libs/zbxserver/evalfunc.c =================================================================== --- src/libs/zbxserver/evalfunc.c (revision 25526) +++ src/libs/zbxserver/evalfunc.c (working copy) @@ -154,7 +154,7 @@ if (NULL == item->h_lasteventid) { - h_value = DBget_history(item->itemid, item->value_type, ZBX_DB_GET_HIST_VALUE, 0, 0, "logeventid", 1); + h_value = DBget_history(item->itemid, item->value_type, ZBX_DB_GET_HIST_VALUE, 0, 0, item->delay, "logeventid", 1); if (NULL != h_value[0]) { @@ -220,7 +220,7 @@ if (NULL == item->h_lastsource) { - h_value = DBget_history(item->itemid, item->value_type, ZBX_DB_GET_HIST_VALUE, 0, 0, "source", 1); + h_value = DBget_history(item->itemid, item->value_type, ZBX_DB_GET_HIST_VALUE, 0, 0, item->delay, "source", 1); if (NULL != h_value[0]) { @@ -277,7 +277,7 @@ if (NULL == item->h_lastseverity) { - h_value = DBget_history(item->itemid, item->value_type, ZBX_DB_GET_HIST_VALUE, 0, 0, "severity", 1); + h_value = DBget_history(item->itemid, item->value_type, ZBX_DB_GET_HIST_VALUE, 0, 0, item->delay, "severity", 1); if (NULL != h_value[0]) { @@ -548,7 +548,7 @@ if (ZBX_FLAG_SEC == flag && NULL == arg2) { h_value = DBget_history(item->itemid, item->value_type, ZBX_DB_GET_HIST_COUNT, - now - arg1, now, NULL, 0); + now - arg1, now, item->delay, NULL, 0); if (NULL == h_value[0]) zbx_strlcpy(value, "0", MAX_BUFFER_LEN); @@ -564,12 +564,12 @@ goto skip_get_history; h_value = DBget_history(item->itemid, item->value_type, ZBX_DB_GET_HIST_VALUE, - 0, now, NULL, arg1); + 0, now, item->delay, NULL, arg1); } else { h_value = DBget_history(item->itemid, item->value_type, ZBX_DB_GET_HIST_VALUE, - now - arg1, now, NULL, 0); + now - arg1, now, item->delay, NULL, 0); } if (ZBX_FLAG_VALUES == flag && 0 == time_shift && @@ -665,7 +665,7 @@ if (ZBX_FLAG_SEC == flag) { - h_value = DBget_history(item->itemid, item->value_type, ZBX_DB_GET_HIST_SUM, now - arg1, now, NULL, 0); + h_value = DBget_history(item->itemid, item->value_type, ZBX_DB_GET_HIST_SUM, now - arg1, now, item->delay, NULL, 0); if (NULL != h_value[0]) { @@ -678,7 +678,7 @@ } else if (ZBX_FLAG_VALUES == flag) { - h_value = DBget_history(item->itemid, item->value_type, ZBX_DB_GET_HIST_VALUE, 0, now, NULL, arg1); + h_value = DBget_history(item->itemid, item->value_type, ZBX_DB_GET_HIST_VALUE, 0, now, item->delay, NULL, arg1); if (ITEM_VALUE_TYPE_UINT64 == item->value_type) { @@ -759,7 +759,7 @@ if (ZBX_FLAG_SEC == flag) { - h_value = DBget_history(item->itemid, item->value_type, ZBX_DB_GET_HIST_AVG, now - arg1, now, NULL, 0); + h_value = DBget_history(item->itemid, item->value_type, ZBX_DB_GET_HIST_AVG, now - arg1, now, item->delay, NULL, 0); if (NULL != h_value[0]) { @@ -772,7 +772,7 @@ } else if (ZBX_FLAG_VALUES == flag) { - h_value = DBget_history(item->itemid, item->value_type, ZBX_DB_GET_HIST_VALUE, 0, now, NULL, arg1); + h_value = DBget_history(item->itemid, item->value_type, ZBX_DB_GET_HIST_VALUE, 0, now, item->delay, NULL, arg1); for (h_num = 0; NULL != h_value[h_num]; h_num++) sum += atof(h_value[h_num]); @@ -899,7 +899,7 @@ else { history: - h_value = DBget_history(item->itemid, item->value_type, ZBX_DB_GET_HIST_VALUE, 0, now, NULL, arg1); + h_value = DBget_history(item->itemid, item->value_type, ZBX_DB_GET_HIST_VALUE, 0, now, item->delay, NULL, arg1); if (0 == time_shift && (ITEM_VALUE_TYPE_TEXT == item->value_type || ITEM_VALUE_TYPE_LOG == item->value_type)) { @@ -980,7 +980,7 @@ if (ZBX_FLAG_SEC == flag) { - h_value = DBget_history(item->itemid, item->value_type, ZBX_DB_GET_HIST_MIN, now - arg1, now, NULL, 0); + h_value = DBget_history(item->itemid, item->value_type, ZBX_DB_GET_HIST_MIN, now - arg1, now, item->delay, NULL, 0); if (NULL != h_value[0]) { @@ -993,7 +993,7 @@ } else if (ZBX_FLAG_VALUES == flag) { - h_value = DBget_history(item->itemid, item->value_type, ZBX_DB_GET_HIST_VALUE, 0, now, NULL, arg1); + h_value = DBget_history(item->itemid, item->value_type, ZBX_DB_GET_HIST_VALUE, 0, now, item->delay, NULL, arg1); if (ITEM_VALUE_TYPE_UINT64 == item->value_type) { @@ -1080,7 +1080,7 @@ if (ZBX_FLAG_SEC == flag) { - h_value = DBget_history(item->itemid, item->value_type, ZBX_DB_GET_HIST_MAX, now - arg1, now, NULL, 0); + h_value = DBget_history(item->itemid, item->value_type, ZBX_DB_GET_HIST_MAX, now - arg1, now, item->delay, NULL, 0); if (NULL != h_value[0]) { @@ -1093,7 +1093,7 @@ } else if (ZBX_FLAG_VALUES == flag) { - h_value = DBget_history(item->itemid, item->value_type, ZBX_DB_GET_HIST_VALUE, 0, now, NULL, arg1); + h_value = DBget_history(item->itemid, item->value_type, ZBX_DB_GET_HIST_VALUE, 0, now, item->delay, NULL, arg1); if (ITEM_VALUE_TYPE_UINT64 == item->value_type) { @@ -1180,7 +1180,7 @@ if (ZBX_FLAG_SEC == flag) { - h_value = DBget_history(item->itemid, item->value_type, ZBX_DB_GET_HIST_DELTA, now - arg1, now, NULL, 0); + h_value = DBget_history(item->itemid, item->value_type, ZBX_DB_GET_HIST_DELTA, now - arg1, now, item->delay, NULL, 0); if (NULL != h_value[0]) { @@ -1193,7 +1193,7 @@ } else if (ZBX_FLAG_VALUES == flag) { - h_value = DBget_history(item->itemid, item->value_type, ZBX_DB_GET_HIST_VALUE, 0, now, NULL, arg1); + h_value = DBget_history(item->itemid, item->value_type, ZBX_DB_GET_HIST_VALUE, 0, now, item->delay, NULL, arg1); if (ITEM_VALUE_TYPE_UINT64 == item->value_type) { @@ -1321,7 +1321,7 @@ res = 0; /* if values are no longer in history, consider them equal */ - h_value = DBget_history(item->itemid, item->value_type, ZBX_DB_GET_HIST_VALUE, 0, now, NULL, 2); + h_value = DBget_history(item->itemid, item->value_type, ZBX_DB_GET_HIST_VALUE, 0, now, item->delay, NULL, 2); for (i = 0; NULL != h_value[i]; i++) { @@ -1652,10 +1652,10 @@ if (SUCCEED == evaluate_STR_local(item, func, regexps, regexps_num, arg1, arg2, &found)) goto skip_get_history; - h_value = DBget_history(item->itemid, item->value_type, ZBX_DB_GET_HIST_VALUE, 0, now, NULL, arg2); + h_value = DBget_history(item->itemid, item->value_type, ZBX_DB_GET_HIST_VALUE, 0, now, item->delay, NULL, arg2); } else - h_value = DBget_history(item->itemid, item->value_type, ZBX_DB_GET_HIST_VALUE, now - arg2, now, NULL, 0); + h_value = DBget_history(item->itemid, item->value_type, ZBX_DB_GET_HIST_VALUE, now - arg2, now, item->delay, NULL, 0); if (ZBX_FLAG_VALUES == flag && ITEM_VALUE_TYPE_STR != item->value_type) { Index: src/libs/zbxdbhigh/db.c =================================================================== --- src/libs/zbxdbhigh/db.c (revision 25526) +++ src/libs/zbxdbhigh/db.c (working copy) @@ -2011,7 +2011,7 @@ * Author: Alexander Vladishev * * * ******************************************************************************/ -char **DBget_history(zbx_uint64_t itemid, unsigned char value_type, int function, int clock_from, int clock_to, +char **DBget_history(zbx_uint64_t itemid, unsigned char value_type, int function, int delay, int clock_from, int clock_to, const char *field_name, int last_n) { const char *__function_name = "DBget_history"; @@ -2055,8 +2055,14 @@ get_table_by_value_type(value_type), itemid); if (0 != clock_from) offset += zbx_snprintf(sql + offset, sizeof(sql) - offset, " and clock>%d", clock_from); - if (0 != clock_to) + if (0 != clock_to) { + if (0 != last_n) { + offset += zbx_snprintf(sql + offset, sizeof(sql) - offset, " and clock>=%d and clock<=%d", clock_to - delay * last_n - 3600, clock_to); + } + else { offset += zbx_snprintf(sql + offset, sizeof(sql) - offset, " and clock<=%d", clock_to); + } + } if (0 != last_n) {