[comment]: # ({19cf85e8-bfe1a94d})
# 1 Aggregate calculations

[comment]: # ({/19cf85e8-bfe1a94d})

[comment]: # ({96203d1d-f8e62abe})
#### Overview

Aggregate calculations are a [calculated item](/manual/config/items/itemtypes/calculated) 
type allowing to collect information from several items by Zabbix server and 
then calculate an aggregate, depending on the aggregate function used.

Aggregate calculations do not require any agent running on the host
being monitored.

[comment]: # ({/96203d1d-f8e62abe})

[comment]: # ({ab0c22a1-63d83a0c})
#### Syntax

To retrieve aggregates use one of the supported [aggregate functions](/manual/appendix/functions/aggregate): `avg`, `max`, `min`, `sum`, etc.
Then add the **foreach** function as the only parameter and its item filter to select the required items:

```default
aggregate_function(function_foreach(/host/key?[group="host group"],timeperiod))
```

A **foreach** function (e.g. *avg_foreach*, *count_foreach*, etc.) returns one aggregate value for each selected item.
Items are selected by using the item filter (`/host/key?[group="host group"]`), from item history.
For more details, see [foreach functions](/manual/appendix/functions/aggregate/foreach).

If some of the items have no data for the requested period, they are ignored in the calculation.
If no items have data, the function will return an error.

Alternatively you may list several items as parameters for aggregation:

```default
aggregate_function(function(/host/key,parameter),function(/host2/key2,parameter),...)
```

Note that `function` here must be a history/trend function.

::: noteclassic
If the aggregate results in a float value it will be trimmed to an integer if the aggregated item type of information is *Numeric (unsigned)*.
:::

User macros and low-level discovery macros are supported in:

-   item key parameters
-   function parameters
-   filter conditions (host group name and tag name)
-   expression constants

An aggregate calculation may become unsupported if:

-   none of the referenced items is found (which may happen if the item key is incorrect, none of the items exists or all included groups are incorrect)
-   no data to calculate a function

[comment]: # ({/ab0c22a1-63d83a0c})

[comment]: # ({3bbe172c-3bbe172c})
#### Usage examples

Examples of keys for aggregate calculations.

[comment]: # ({/3bbe172c-3bbe172c})

[comment]: # ({1306478c-874bcf94})
##### Example 1

Total disk space of host group 'MySQL Servers'.

```default
sum(last_foreach(/*/vfs.fs.size[/,total]?[group="MySQL Servers"]))
```

[comment]: # ({/1306478c-874bcf94})

[comment]: # ({5c3dc788-3182672a})
##### Example 2

Sum of latest values of all items matching net.if.in\[\*\] on the host.

```default
sum(last_foreach(/host/net.if.in[*]))
```

[comment]: # ({/5c3dc788-3182672a})

[comment]: # ({beca1bb5-cdf2da8b})
##### Example 3

Average processor load of host group 'MySQL Servers'.

```default
avg(last_foreach(/*/system.cpu.load[,avg1]?[group="MySQL Servers"]))
```

[comment]: # ({/beca1bb5-cdf2da8b})

[comment]: # ({c12aa12c-db9c8fce})
##### Example 4

5-minute average of the number of queries per second for host group 'MySQL Servers'.

```default
avg(avg_foreach(/*/mysql.qps?[group="MySQL Servers"],5m))
```

[comment]: # ({/c12aa12c-db9c8fce})

[comment]: # ({c083097e-0233edd3})
##### Example 5

Average CPU load on all hosts in multiple host groups that have the specific tags.

```default
avg(last_foreach(/*/system.cpu.load?[(group="Servers A" or group="Servers B" or group="Servers C") and (tag="Service:" or tag="Importance:High")]))
```

[comment]: # ({/c083097e-0233edd3})

[comment]: # ({0a45c034-30448f5d})
##### Example 6

Calculation used on the latest item value sums of a whole host group.

```default
sum(last_foreach(/*/net.if.out[eth0,bytes]?[group="video"])) / sum(last_foreach(/*/nginx_stat.sh[active]?[group="video"])) 
```

[comment]: # ({/0a45c034-30448f5d})

[comment]: # ({dd370fd1-3e9404a6})
##### Example 7

The total number of unsupported items in host group 'Zabbix servers'.

```default
sum(last_foreach(/*/zabbix[host,,items_unsupported]?[group="Zabbix servers"]))
```

##### Example 8

Sum of the latest numeric results of DNS checks across all hosts.
The shown item form is `net.dns[192.0.2.1,example.com,A]` as an example of one possible key.

```default
sum(last_foreach(/*/net.dns[*,*,*]))
```

Note that wildcards must match the number of parameters in the key (here [`net.dns`](/manual/config/items/itemtypes/zabbix_agent#net.dns) has three parameters: ip, name, type).

[comment]: # ({/dd370fd1-3e9404a6})

[comment]: # ({f6401cd3-f094da29})

##### Examples of correct/incorrect syntax

Expressions (including function calls) cannot be used as history, trend, or foreach [function](/manual/appendix/functions) parameters.
However, those functions themselves can be used in other (non-historical) function parameters.

|Expression|Example|
|-|---------|
|Valid|`avg(last(/host/key1),last(/host/key2)*10,last(/host/key1)*100)`<br>`max(avg(avg_foreach(/*/system.cpu.load?[group="Servers A"],5m)),avg(avg_foreach(/*/system.cpu.load?[group="Servers B"],5m)),avg(avg_foreach(/*/system.cpu.load?[group="Servers C"],5m)))`|
|Invalid|`sum(/host/key,10+2)`<br>`sum(/host/key, avg(10,2))`<br>`sum(/host/key,last(/host/key2))`|

Note that in an expression like:

```default
sum(sum_foreach(//resptime[*],5m))/sum(count_foreach(//resptime[*],5m))
```

it cannot be guaranteed that both parts of the equation will always have the same set of values.
While one part of the expression is evaluated, a new value for the requested period may arrive and then the other part of the expression will have a different set of values.

[comment]: # ({/f6401cd3-f094da29})
