Shwettha RR
Shwettha RR

Reputation: 181

How to get the difference between the current value and the previous value in the influxdb?

am using collectd to monitor the system metric .... For network metric I need to know what will be the incoming and the outgoing traffic per minute ...But Rx(total number of packets transmitted) and Tx(total number of packets received) packets is giving the data from since the machine is last rebooted...

So i need to query the influxdb to get the difference between current value and the previous value. How to query to get the difference.....

Am using the query like

select difference(last("value")) from interface_rx WHERE  "type" = 'if_octets' > now() - 1h group by time(10s)

Please help me ..Am not getting the exact output like i need

enter image description here

Am getting the following error when i used the query

Select difference(last("value")) from interface_rx WHERE "type" = 'if_octets' and time > now() - 1h group by time(1m)

enter image description here

enter image description here

Upvotes: 0

Views: 5072

Answers (1)

It should be something like this

select difference(last("value")) from interface_rx WHERE "type" = 'if_octets' and time > now() - 1h group by time(1m)

This query should return about 60 values; 1 difference per minute for the past hour. The last in this case means that for every 1 minute period the last value is used (in case there are more than one)

Differences to your query: and time > now() - 1h was missing - this may have been an editing error but it did not seem valid. if you want the statistic per minute then you need to group by time(1m). You were grouping by 10 seconds so the result would have been the difference for every 10 seconds.

What you also may want to do is fill missing values with the previous available value (and respectively you'd get a zero for that period) select difference(last("value")) from interface_rx WHERE "type" = 'if_octets' and time > now() - 1h group by time(1m) fill(previous) EDIT: fill(previous) is probably not what you want though because if most recent values are not recorded yet they will be substituted with older ones and you'll get difference=0 as a most recent value.

If you only want the very last value you could do something like select difference(last("value")) from interface_rx WHERE "type" = 'if_octets' and time > now() - 1h group by time(1m) fill(previous) order by time DESC limit 1

Upvotes: 1

Related Questions