Ad Widget

Collapse

PATH: AVG on integers too

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Vince2
    Member
    • Oct 2006
    • 40

    #1

    PATH: AVG on integers too

    Hi !

    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
    Last edited by Vince2; 27-02-2007, 15:42.
  • Simone Lazzaris
    Junior Member
    • Mar 2007
    • 1

    #2
    VERY very useful

    Very useful; I've just implemented a zabbix monitoring service for our network and I was puzzled when I've discovered (by gdb-debugging the zabbix_server executable) that the average function doesn't work with integer values.

    I strongly push for inclusion of this patch in the stable branch, 'cos I don't see any reasonable ragion for not having the average function on integers.

    Shall I better open a bug ticket for this ?

    Comment

    • Alexei
      Founder, CEO
      Zabbix Certified Trainer
      Zabbix Certified SpecialistZabbix Certified Professional
      • Sep 2004
      • 5654

      #3
      The patch (with minor modifications) has been integrated into both 1.1.x and 1.3.x. Thanks!
      Alexei Vladishev
      Creator of Zabbix, Product manager
      New York | Tokyo | Riga
      My Twitter

      Comment

      Working...