Search

03 August, 2020

Grafana with Influxdb and python client

Recently I came across this tool called Grafana. Its a tool that you can use to show graphs and statistics of all kinds. The most popular combination for setup , is Grafana with influxdb as a database. 

I am sharing my setup steps, and supporting python scripts that can be used to generate information and write back to the database. And finally we get to see some nice graphs.

Note: I am using a Mac with Catalina OS 10.15. Python 3.6+.

Installation Plan:


1. Influxdb
2. Grafana
3. influxdb client module for Python.

Install InfluxDB


brew install influxdb

Note (1): Find installation instructions here.
Note (2): if you are using zsh on linux, switch to default BASH as the a few commands mind not work (at the time of writing (5th Aug 2020)).

Start influxdb


brew services start influxdb

Launch influxdb shell (if command not found, install: sudo apt install influxdb-client):



Create, and USE a new database

> CREATE DATABASE NAME
> SHOW DATABASES
> USE NAME
> SHOW SERIES 

Delete a database


> DELETE DATABASE name

> delete DATABASE name


Delete data:

Delete all data from a table

> delete from table
> delete from table where .......

After this, add influxdb  configuration to Grafana. (Configuration > Data Sources > Add data source)



Writing Data to InfluxDB:


Data in InfluxDB is mostly, some value at some time with tags (like where , what , whatever info you want to tag). Essentially "time-series".
Time series has 0 to many POINTS.

Point = time (timestamp) ,
             a measurement ("e.g cpu_load"),
             at least one key-value field (e.g value=0.64 or temperature=60.5),
             zero-to-many key-value tags containing any metadata about the value (e,g host=server01, region=ASIA)

So measurement can be thought of as a SQL TABLE, where primary index is ALWAYS time, and tags and fields are columns.

Format for using influxdb line protocol


<measurement>[,<tag-key>=<tag-value>...] <field-key>=<field-value>...[timestamp]

Example insert:

> INSERT cpu,host=serverA, region=us_west value=0.54



You can query data directly from the console too:



Let's add some data in our database.
Here we added 4 points for a device. 


Now let's add a graph to our Grafana dashboard and configure our query as per our tags.




This is how it looks after we save the above query.



 
Note: You can check if grafana is working , by the command:

sudo systemctl status grafana-server

 

Some extra info for influxdb commands in case you need them:

Deleting some row:

One cannot delete by field (as of today Aug 2020), but you can delete by tags .

Or even time.

> delete from mydevice_performance where scenario_id='dev_performance' and count_write=65527
 
> delete from mydevice_performance where scenario_id='dev_performance' and time=1598148202949654016 

 

Getting count of rows:

Its almost like sql.

> select count(*) from mydevice_performance where scenario_id='dev_performance'

No comments:

Post a Comment