-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinflux_writer.py
72 lines (52 loc) · 1.98 KB
/
influx_writer.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
70
71
72
import configparser
import logging
import sys
import time
from influxdb import InfluxDBClient
from timeout import timeout
FORMAT = '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
logging.basicConfig(level=logging.INFO, format=FORMAT)
logger = logging.getLogger(__name__)
configFilePath = r'config-gateway.properties'
def send_events_to_influxdb(parser):
logger.info("Starting up..")
config_parser = configparser.ConfigParser()
config_parser.read(configFilePath)
config_section = config_parser['gateway']
read_events(config_section, parser)
def non_blank_lines(f):
for l in f:
line = l.rstrip()
if line:
yield line
def read_events(config_section, parser):
influxdb_client = get_influxdb_client(config_section, logger)
logger.info("Listening for events")
for line in non_blank_lines(sys.stdin):
logger.info("Event received: {}".format(line))
payload = parser(line)
if payload is not None:
logger.info("Write points: {0}".format(payload))
write_influxdb(influxdb_client, payload, logger)
def get_influxdb_client(config_section, logger):
logger.info("Using database: '{0}'".format(config_section['DATABASE']))
return InfluxDBClient(config_section['HOSTNAME'],
config_section['PORT'],
config_section['USER'],
config_section['PASSWORD'],
config_section['DATABASE'],
ssl=True, verify_ssl=True)
def write_influxdb(influxdb_client, payload, logger):
while True:
try:
influx_http_call(influxdb_client, payload)
except Exception as e:
logger.exception(str(e))
logger.exception("Error writing to InfluxDB. Retrying in 30sec")
time.sleep(30)
continue
else:
break
@timeout(30)
def influx_http_call(influxdb_client, payload):
influxdb_client.write_points(payload)