/
influxdb_heartbeat.py
62 lines (48 loc) · 1.76 KB
/
influxdb_heartbeat.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import argparse
import commands
from influxdb import InfluxDBClient
from datetime import datetime, timedelta
def seconds_past(datetime, seconds):
return (datetime < datetime.now() - timedelta(seconds=seconds))
def sensor_data(controller, value, tags={}):
ident = controller
data = {
"measurement": str(ident),
"time": (
datetime.utcnow().replace(microsecond=0).isoformat() +
"Z"),
"tags": tags,
"fields": {"value": value, }
}
return data
def main(host='CHANGEME', port=8086):
"""Instantiate a connection to the InfluxDB."""
user = 'CHANGEME'
password = 'CHANGEME'
dbname = 'CHANGEME'
client = InfluxDBClient(host, port, user, password, dbname)
cpu_load = commands.getoutput("grep 'cpu ' /proc/stat | awk '{usage=($2+$4)*100/($2+$4+$5)} END {print usage}'")
#print("Querying data: " + query % cpu_load)
#result = client.query(query)
#print("Result: {0}".format(result))
data = sensor_data(
"CHANGEME",
cpu_load,
{"type": "cpu_load" }, )
client.write_points([data])
print("published to influx", data)
def parse_args():
"""Parse the args."""
parser = argparse.ArgumentParser(
description='example code to play with InfluxDB')
parser.add_argument('--host', type=str, required=False,
default='CHANGEME',
help='hostname of InfluxDB http API')
parser.add_argument('--port', type=int, required=False, default=8086,
help='port of InfluxDB http API')
return parser.parse_args()
if __name__ == '__main__':
args = parse_args()
main(host=args.host, port=args.port)