-
Notifications
You must be signed in to change notification settings - Fork 1
/
InfluxDBReporter.py
69 lines (55 loc) · 1.87 KB
/
InfluxDBReporter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
from influxdb import InfluxDBClient
import socket
class InfluxDBReporter:
"""Simple class to report the metrics to InfluxDB"""
def __init__(self, host, username, password, port, database):
# Construct a InfluxDB client and our localhost name to report to the server
self.client = InfluxDBClient(host, port, username, password, database)
self.hostname = socket.gethostname()
def reportIO(self, id, metrics):
# First for bytes
b = metrics["bytes"]
json_body = [{
"points": [
[b["Read"], b["Write"], b["Total"], b["Sync"], b["Async"]]
],
"name": self.hostname + "." + id + ".disk.bytes",
"columns": ["Read", "Write", "Total", "Sync", "Async"]
}]
self.client.write_points(json_body)
# Then for IO request counts
c = metrics["count"]
json_body = [{
"points": [
[c["Read"], c["Write"], c["Total"], c["Sync"], c["Async"]]
],
"name": self.hostname + "." + id + ".disk.count",
"columns": ["Read", "Write", "Total", "Sync", "Async"]
}]
self.client.write_points(json_body)
def reportCpu(self, id, metrics):
points = [metrics["total"]]
columns = ["Total"]
# There might be many cpus, store values as CPU0-value, CPU1-value..
for idx, cpu in enumerate(metrics["cpus"]):
points.append(cpu)
columns.append("CPU" + str(idx))
json_body = [{
"points": [points],
"name": self.hostname + "." + id + ".cpu",
"columns": columns
}]
self.client.write_points(json_body)
def reportMemory(self, id, metrics):
points = []
columns = []
# Iterates over all memory metrics, can be altered to report only specific fields such as rss
for key, value in metrics.iteritems():
points.append(value)
columns.append(key)
json_body = [{
"points": [points],
"name": self.hostname + "." + id + ".memory",
"columns": columns
}]
self.client.write_points(json_body)