7 Elements calculats

Visió general

Un element calculat permet crear un càlcul basat en el valors d'alguns elements existents. Per exemple, és possible que vulgueu calcular la mitjana horària d'algun valor d'element o calcular el valor total d'un grup d'elements. Per a això serveixen els elements calculats.

Els càlculs poden emprar aquestes dues:

  • valors individuals d'elements individuals
  • un filtre complex per seleccionar diversos elements per a l'agregació (per tindre més detalls, llegiu els càlculs afegits)

Els elements calculats són una manera de crear fonts de dades virtuals. Tots els càlculs els fa només el servidor Zabbix. Els valors són periòdics i es calculen a partir de l'expressió aritmètica emprada.

Les dades resultants s'emmagatzemen a la base de dades Zabbix com per a qualsevol altra element; s'emmagatzemen tant els valors històrics com les tendències i es poden emmagatzemar els gràfics generats.

Si el resultat del càlcul és un valor flotant, ho serà retallat a un nombre enter si el tipus d'informació d'element calculat és Numèric (sense signe).

A més, si no hi ha dades recents a la memòria cau i no hi ha un període de consulta definit a la funció, per defecte Zabbix anirà enrere fins a una setmana per consultar a la base de dades els valors històrics.

Els elements calculats comparteixen la seva sintaxi amb les expressions de trigger. Es permeten les comparacions amb cadenes. Els elements calculats es poden fer referència per macros o altres entitats iguals a qualsevol altre tipus d'element.

Per emprar elements calculats, trieu el tipus d'element Calculat.

Camps configurables

La clau és un identificador d'element únic (per equip). Podeu crear qualsevol nom de clau emprant símbols admesos.

La definició del càlcul s'ha d'introduir al camp Fórmula. Gairebé no hi ha connexió entre la fórmula i la clau. Els paràmetres clau no s'empren a la fórmula de cap manera.

La sintaxi d'una fórmula simple és:

 function(/host/key,<paràmetre1>,<paràmetre2>,...)

O:

function Una de les funcions admeses: last, min, max, avg, count, etc
host Equip de l'element emprat per al càlcul.
L'equip actual es pot ometre (com a function(//key,parameter,...)).
key Clau de l'element emprat per al càlcul.
paràmetre(s) Paràmetres de la funció, si escau.
S'admeten Sufixos de temps i sufixos de mida de memòria.

Les macros d'usuari de la fórmula s'ampliaran si s'empren per fer referència a un paràmetre de funció, un paràmetre de filtre d'element o una constant. Les macros d'usuari NO s'ampliaran pas si fan referència a una funció, nom d'equip, clau d'element, paràmetre de clau d'element o operador.

Una fórmula més complexa pot emprar una combinació de funcions, operadors i parèntesis. Podeu emprar totes les funcions admeses i operators a les expressions de triggers. La lògica i la precedència de l'operador són exactament la mateixa.

A diferència dels triggers, Zabbix processa els elements calculats en funció de l'interval d'actualització de l'element, no quan es rep un valor nou.

Tots els elements als quals fan referència les funcions d'històric a la fórmula d'elements calculats han d'existir i recopilar dades. A més, si canvieu la clau d'element d'un element de referència, haureu d'actualitzar manualment totes les fórmules amb aquesta clau.

Un element calculat pot quedar sense suport en diversos casos:

  • els articles de referència
    • no es troba
    • és inhabilitat
    • pertany a l'equip desactivat
    • no és compatible (excepte amb la funció nodata() i operadors amb valors desconeguts)
  • No hi ha dades per calcular una funció
  • Divisió per zero
  • S'empra una sintaxi incorrecta

Simple calculation formula

The syntax of a simple formula is:

function(/host/key,<parameter1>,<parameter2>,...)

where:

Element Description
function One of the supported functions: last, min, max, avg, count, etc
host Host of the item that is used for calculation.
The current host can be omitted (i.e. as in function(//key,parameter,...)).
key Key of the item that is used for calculation.
Items returning values with the binary or JSON data type are not supported.
If you change the item key of a referenced item, the key must be manually updated in the formula.
All items that are referenced by history functions must exist and be collecting data.
parameter(s) Parameters of the function, if required.
Time suffixes and memory size suffixes are supported.

See examples of calculated item formulas:

User macros in the formula will be resolved if used to reference a function parameter, item filter parameter, or a constant. User macros will NOT be resolved if referencing a function, host name, item key, item key parameter or operator.

Comparison to strings is allowed.

If there is no recent data in the cache and there is no defined querying period in the function, Zabbix will by default go as far back in the past as one week to query the database for historical values.

A calculated item may become unsupported in several cases:

  • referenced item
    • is not found
    • is disabled, or belongs to a disabled host
    • is not supported (except with nodata() function and operators with unknown values)
  • no data to calculate a function
  • division by zero
  • incorrect syntax used

Data aggregations

Data aggregations are based on a filter that selects multiple items.

To retrieve aggregates use one of the supported aggregate functions: avg, max, min, sum, etc. Then add the foreach function as the only parameter and its item filter to select the required items:

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.

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:

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

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

See examples of aggregate calculations:

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

Exemples d'ús

Exemple 3

Càlcul de l'amplada de banda total de l'eth0.

Suma de dues funcions:

last(//net.if.in[eth0,bytes])+last(//net.if.out[eth0,bytes])
Exemple 1

Càlcul del percentatge d'espai lliure de disc a '/'.

Ús de la funció last:

100*last(//vfs.fs.size[/,free])/last(//vfs.fs.size[/,total])

Zabbix prendrà els darrers valors per l'espai en disc lliure i el total, i calcularà el percentatge segons la fòrmula donada.

Exemple 4

Càlcul del percentatge de tràfic entrant.

Expressió més complexa:

100*last(//net.if.in[eth0,bytes])/(last(//net.if.in[eth0,bytes])+last(//net.if.out[eth0,bytes]))

Veieu també: Exemples de càlculs agregats

Exemple 2

Càlcul de la mitjana sobre 10 minuts del nombre de valors tractats per Zabbix.

Ús de la funció avg:

avg(/Zabbix Server/zabbix[wcache,values],10m)

Veieu que un ús intensiu d'elements calculats amb períodes llargs pot afectar el rendiment del servidor Zabbix.

(5) Total incoming traffic for host
sum(last_foreach(/host/net.if.in[*]))

Calculate the total of all items matching net.if.in[*] on the host.

(6) Total disk space for group
sum(last_foreach(/*/vfs.fs.size[/,total]?[group="MySQL Servers"]))

Calculate the total disk space for all MySQL Servers.

(7) Average processor load for group
avg(last_foreach(/*/system.cpu.load[,avg1]?[group="MySQL Servers"]))

Calculate the average processor load for all MySQL Servers.

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

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

(9) Average number of queries for group
avg(avg_foreach(/*/mysql.qps?[group="MySQL Servers"],5m))

Calculate the 5-minute average of the number of queries per second for MySQL Servers.

(10) Total number of successful DNS checks
sum(last_foreach(/*/net.dns[*,*,*]))

Calculate the total number of successful DNS checks across all hosts. The shown item form is net.dns[192.0.2.0,example.com,A] as an example of one possible key.

Note that wildcards must match the number of parameters in the key (here net.dns has three parameters: ip, name, type).

(11) Total number of unsupported items for group
sum(last_foreach(/*/zabbix[host,,items_unsupported]?[group="Zabbix servers"]))

Calculate the total number of unsupported items for Zabbix servers.

(12) Interface stats for group
sum(last_foreach(/*/net.if.out[eth0,bytes]?[group="video"])) / sum(last_foreach(/*/nginx_stat.sh[active]?[group="video"])) 

Calculate the interface stats for video group.

Examples of correct/incorrect syntax

Expressions (including function calls) cannot be used as history, trend, or foreach function 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)

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)

sum(/host/key, avg(10,2))

sum(/host/key,last(/host/key2))

Note that in an expression like:

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.