How to get slow monitoring data

Connect to InfluxDB

Load a config file


In [1]:
import json

In [2]:
with open('db.json','r') as fp:
    c = json.load(fp)

Load InfluxDB package


In [3]:
from influxdb import InfluxDBClient

Set your database information


In [4]:
client = InfluxDBClient(
    c['DBHOST'],
    443,
    c['DBUSER'],
    c['DBPASS'],
    c['DBNAME'],
    ssl=True,
    verify_ssl=True
)

Print measurements (Simular to a MySQL's table)


In [5]:
rs = client.query("SHOW MEASUREMENTS")
for row in rs.get_points():
    print(row)


{'name': 'cosine_con_status'}
{'name': 'cosine_con_val'}
{'name': 'fortigate'}
{'name': 'korac'}
{'name': 'lutron'}
{'name': 'pcw'}
{'name': 'rad7'}
{'name': 'rrsdehumi'}
{'name': 't_rh_o2'}
{'name': 'temphum'}
{'name': 'towerlamp'}
{'name': 'weather'}
{'name': 'y2ltemphum'}

Get data from a specific measurement


In [6]:
query = """SELECT * FROM "weather" WHERE time > now() - 7d"""

Export CSV


In [7]:
import csv

In [8]:
rs = client.query(query)
with open('data.csv', 'w') as csvfile:
    firstrow = next(rs.get_points())
    writer = csv.DictWriter(csvfile, fieldnames=firstrow.keys())
    writer.writeheader()
    writer.writerow(firstrow)
    for row in rs.get_points():
        writer.writerow(row)

Numpy


In [9]:
%matplotlib inline

In [10]:
import matplotlib
import matplotlib.pyplot as plt

In [11]:
import numpy as np

In [12]:
temp = []
rs = client.query(query)
for item in rs.get_points():
    temp.append(item['T1H'])

In [13]:
temp = np.array(temp)

In [14]:
plt.plot(temp)


Out[14]:
[<matplotlib.lines.Line2D at 0x7f47b72497b8>]

PyROOT


In [15]:
from array import array

In [16]:
import ROOT


Welcome to JupyROOT 6.10/02

In [17]:
%jsroot on

In [18]:
h1f = ROOT.TH1F('h1f', 'Y2 Temperature', 100, 15, 40)

In [19]:
rs = client.query(query)
for item in rs.get_points():
    h1f.Fill(item['T1H'])

In [20]:
c = ROOT.TCanvas("CanvasName","Canvas Title",800,600)
h1f.Draw()
c.Draw()


Export data


In [21]:
f = ROOT.TFile('test.root', 'RECREATE')
d = array( 'f', [ 0 ])
tree = ROOT.TTree("tree", "tree")
tree.Branch('temperature', d, 'temp/F')
rs = client.query(query)
for item in rs.get_points():
    d[0] = item['T1H']
    tree.Fill()
f.Write()
f.Close()