--- C:/Users/JBurns/AppData/Local/Temp/checks_aggregate.c-revBASE.svn000.tmp.c Tue Sep 23 14:58:53 2014 +++ C:/SVN/ZABBIX/2.4.2/src/zabbix_server/poller/checks_aggregate.c Sun May 24 23:12:22 2015 @@ -29,6 +29,7 @@ #define ZBX_VALUE_FUNC_SUM 3 #define ZBX_VALUE_FUNC_COUNT 4 #define ZBX_VALUE_FUNC_LAST 5 +#define ZBX_VALUE_FUNC_RATE 6 /****************************************************************************** * * @@ -184,7 +185,63 @@ static void evaluate_history_func_last(zbx_vector_ { *result = values->values[0].value; } +/****************************************************************************** + * * + * Function: evaluate_history_func_rate * + * * + * Purpose: calculate the rate of change for the history value ZBXNEXT-2374 * + * * + * Parameters: values - [IN] a vector containing history values * + * value_type - [IN] the type of values. Only float/uint64 * + * values are supported. * + * result - [OUT] the result value/second (from timestamp) * + * * + * Author: James Burns * + * * + ******************************************************************************/ +static void evaluate_history_func_rate(zbx_vector_history_record_t *values, int value_type, history_value_t *result) { + int i; + double meanx=0.0, meany=0.0; + double dx=0.0, dy=0.0; + double sumdydx=0.0, sumdxdx=0.0; + + if (ITEM_VALUE_TYPE_UINT64 == value_type) + { + for (i = 0; i < values->values_num; i++){ + meanx += values->values[i].timestamp.sec; + meany += values->values[i].value.ui64; + } + meanx /= values->values_num; + meany /= values->values_num; + + for (i = 1; i < values->values_num; i++){ + dx = (values->values[i].timestamp.sec - meanx); + dy = (values->values[i].value.ui64-meany); + sumdydx += (dy*dx); + sumdxdx += (dx*dx); + } + sumdydx /= sumdxdx;//produce the gradient and store in sumdydx + result->ui64 = sumdydx; + } + else { + for (i = 1; i < values->values_num; i++){ + meanx += values->values[i].timestamp.sec; + meany += values->values[i].value.dbl; + } + meanx /= values->values_num; + meany /= values->values_num; + for (i = 1; i < values->values_num; i++){ + dx = (values->values[i].timestamp.sec - meanx); + dy = (values->values[i].value.dbl-meany); + sumdydx += (dy*dx); + sumdxdx += (dx*dx); + } + sumdydx /= sumdxdx;//produce the gradient and store in sumdydx + result->dbl = sumdydx; + } +} + /****************************************************************************** * * * Function: evaluate_history_func * @@ -225,6 +282,9 @@ static void evaluate_history_func(zbx_vector_histo case ZBX_VALUE_FUNC_LAST: evaluate_history_func_last(values, value_type, result); break; + case ZBX_VALUE_FUNC_RATE: + evaluate_history_func_rate(values, value_type, result); + break; } } @@ -509,6 +569,8 @@ int get_value_aggregate(DC_ITEM *item, AGENT_RESUL item_func = ZBX_VALUE_FUNC_COUNT; else if (0 == strcmp(tmp, "last")) item_func = ZBX_VALUE_FUNC_LAST; + else if (0 == strcmp(tmp, "rate")) + item_func = ZBX_VALUE_FUNC_RATE; else { SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid third parameter."));