Search code examples
prometheusgrafanapromql

Calculate min, max and average packet loss


I have metrics sent to prometheus in the form:

interface_stats{type="rx_dropped", device="eth0", host="host0.my.net"} 5 
interface_stats{type="rx_packets", device="eth0", host="host0.my.net"} 1000
interface_stats{type="rx_dropped", device="eth0", host="host1.my.net"} 3
interface_stats{type="rx_packets", device="eth0", host="host1.my.net"} 2000
interface_stats{type="rx_dropped", device="eth0", host="host2.my.net"} 9
interface_stats{type="rx_packets", device="eth0", host="host2.my.net"} 1000
.
.
.
interface_stats{type="rx_dropped", device="eth0", host="host325.my.net"} 12
interface_stats{type="rx_packets", device="eth0", host="host235.my.net"} 1000

I would like to calculate and display min, max and average packet loss for eth0 over time for all my hosts. All values are counters.

Is that possible?


Solution

  • The following PromQL query returns packet loss over the last 5 minutes (see 5m in square brackets) per each (device, host) if interface_stats is a counter:

    rate(interface_stats{type="rx_dropped"}[5m])
      / ignoring(type)
    rate(interface_stats{type="rx_packets"}[5m])
    

    You can increase 5m to 1h or to any other supported time duration in order to get the packet loss over the given time duration.

    If you need to get packet loss for a particular device and/or host, then just add the corresponding filter into curly braces. For example, the following query returns packet loss only for device="eth0" per each host:

    rate(interface_stats{type="rx_dropped",device="eth0"}[5m])
      / ignoring(type)
    rate(interface_stats{type="rx_packets",device="eth0"}[5m])
    

    If you need to get the average, max or min packet loss across hosts, then just wrap the query above into avg(), max() or min() aggregate functions. For example, the following query returns the average packet loss across all the hosts for device="eth0" over the last 5 minutes:

    avg(
      rate(interface_stats{type="rx_dropped",device="eth0"}[5m])
        / ignoring(type)
      rate(interface_stats{type="rx_packets",device="eth0"}[5m])
    )