How to get the difference between the last two values in influxdb

If you are measuring traffic, probably your are writting OVS data as accumulative bytes.
This is an extract of my influxdb data



You can get the newest two values with the following sentence:

influx -execute "SELECT value FROM packets_in WHERE dp_name='s3' AND port_name='port1' ORDER BY time DESC LIMIT 2" -database=faucet -precision=rfc3339

name: packets_in
time                 value
----                 -----
2017-03-16T20:00:23Z 326
2017-03-16T20:00:13Z 302

If you want to get the difference between them (326-302), you need the lastest influxdb version (here a post about how to upgrade your current version).
With the DIFFERENCE command, you can do it:

influx -execute "SELECT DIFFERENCE(value) * -1 FROM packets_in WHERE dp_name='s3' AND port_name='port1' ORDER BY time DESC LIMIT 1" -database=faucet -precision=rfc3339

name: packets_in
time                 difference
----                 ----------
2017-03-16T20:00:13Z 24

Enjoy it!





Comments

Popular posts from this blog

from: can't read /var/mail/ in Python scripts

Fix "Set scan parameters failed: Input/output error" on Ubuntu 16.04

How to install a WhatsApp bot based on Python