Hi !
Here is a patch to allow the use of AVG function with integers (not just float) :
Here is a patch to allow the use of AVG function with integers (not just float) :
Code:
From: Vincent Bernat <[email protected]> Date: Tue, 27 Feb 2007 11:09:49 +0000 (+0100) Subject: Allow the use of AVG on integers X-Git-Url: http://localhost/cgi-bin/gitweb.cgi?p=zabbix-wallix-1.1.6.git;a=commitdiff_plain;h=e69e72b022fe60ab44489128627059bc62b604f0 Allow the use of AVG on integers --- diff --git a/src/zabbix_server/evalfunc.c b/src/zabbix_server/evalfunc.c index f1a9364..6cd0527 100644 --- a/src/zabbix_server/evalfunc.c +++ b/src/zabbix_server/evalfunc.c @@ -352,12 +352,14 @@ static int evaluate_AVG(char *value,DB_ITEM *item,int parameter,int flag) DB_ROW row; char sql[MAX_STRING_LEN]; + char table[MAX_STRING_LEN]; int now; int res = SUCCEED; int rows; double sum=0; + zbx_uint64_t sum_uint64=0; - if(item->value_type != ITEM_VALUE_TYPE_FLOAT) + if( (item->value_type != ITEM_VALUE_TYPE_FLOAT) && (item->value_type != ITEM_VALUE_TYPE_UINT64)) { return FAIL; } @@ -366,7 +368,15 @@ static int evaluate_AVG(char *value,DB_ITEM *item,int parameter,int flag) if(flag == ZBX_FLAG_SEC) { - snprintf(sql,sizeof(sql)-1,"select avg(value) from history where clock>%d and itemid=%d",now-parameter,item->itemid); + if(item->value_type == ITEM_VALUE_TYPE_UINT64) + { + strscpy(table,"history_uint"); + } + else + { + strscpy(table,"history"); + } + snprintf(sql,sizeof(sql)-1,"select avg(value) from %s where clock>%d and itemid=%d",table,now-parameter,item->itemid); result = DBselect(sql); row = DBfetch(result); @@ -384,22 +394,47 @@ static int evaluate_AVG(char *value,DB_ITEM *item,int parameter,int flag) } else if(flag == ZBX_FLAG_VALUES) { - snprintf(sql,sizeof(sql)-1,"select value from history where itemid=%d order by clock desc",item->itemid); - result = DBselectN(sql, parameter); - rows=0; - while((row=DBfetch(result))) + if(item->value_type == ITEM_VALUE_TYPE_UINT64) { - sum+=atof(row[0]); - rows++; + strscpy(table,"history_uint"); } - if(rows == 0) + else + { + strscpy(table,"history"); + } + snprintf(sql,sizeof(sql)-1,"select value from %s where itemid=%d order by clock desc",table,item->itemid); + result = DBselectN(sql, parameter); + row = DBfetch(result); + rows = 0; + if(!row || DBis_null(row[0])==SUCCEED) { zabbix_log(LOG_LEVEL_DEBUG, "Result for AVG is empty" ); res = FAIL; } else { - snprintf(value,MAX_STRING_LEN-1,"%f", sum/(double)rows); + if(item->value_type == ITEM_VALUE_TYPE_UINT64) + { + while((row=DBfetch(result))) + { + rows++; +#ifdef HAVE_ATOLL + sum_uint64+=atoll(row[0]); +#else + sum_uint64+=atol(row[0]); +#endif + } + snprintf(value,MAX_STRING_LEN-1,ZBX_FS_UI64, sum_uint64/rows); + } + else + { + while((row=DBfetch(result))) + { + sum+=atof(row[0]); + rows++; + } + snprintf(value,MAX_STRING_LEN-1,"%f", sum/(double)rows); + } } } else
Comment