-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.py
165 lines (146 loc) · 4.54 KB
/
logger.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#!/usr/bin/python -p
# import some basic libs
import time
import sys
import datetime
import argparse
import textwrap
import logging
# import InfluxDB client
from influxdb import InfluxDBClient
# import our own libs
from config import load_config, load_measurements
from loxone import loxclient
parser = argparse.ArgumentParser(
description="Logger script for getting stats from Miniserver to InfluxDB"
)
parser.add_argument(
"-v", "--verbose", action="store_true", help="Enable verbose output"
)
parser.add_argument(
"-d", "--debug", action="store_true", help="Enable debug output"
)
parser.add_argument(
"--dry", action="store_true", help=textwrap.dedent('''\
Do not write to InfluxDB. Only get values from Miniserver."''')
)
# Allow user to set session and run number via args otherwise auto-generate
parser.add_argument(
"-s", "--session", default="dev", help="Logging session. Default is ''dev'"
)
parser.add_argument(
"-r", "--run", default=None,
help="Run number. Default is generated from datetime.now()"
)
parser.add_argument(
"-l", "--lib", default="requests",
help="Http library. Default is 'requests'"
)
parser.add_argument(
"-log", "--log-file", default=None,
help="Log into specified file, instead of stdout"
)
args = parser.parse_args()
# Initialize logging
lg = logging.getLogger(__name__)
log_format='%(asctime)s %(threadName)s [%(levelname)s] %(name)s %(message)s'
if args.verbose:
#print("Verbose output turned on")
loglvl = logging.INFO
elif args.debug:
#print("Debug output turned on")
loglvl = logging.DEBUG
else:
loglvl = logging.ERROR
if args.log_file is None:
logging.basicConfig(
format=log_format,
level=loglvl
)
else:
logging.basicConfig(
filename=args.log_file,
format=log_format,
level=loglvl
)
# Read configuration from config file
config_cache = load_config(input_file="secrets.yml")
host = config_cache["influxdb::host"]
port = config_cache["influxdb::port"]
dbname = config_cache["influxdb::db"]
user = config_cache["influxdb::user"]
password = config_cache["influxdb::password"]
loxusr = config_cache["loxone::user"]
loxpass = config_cache["loxone::password"]
loxhost = config_cache["loxone::host"]
# Sample period (s)
interval = 60
# Allow user to set session and run number via args otherwise auto-generate
if args.run is None:
now = datetime.datetime.now()
runNo = now.strftime("%Y%m%d%H%M")
else:
runNo = args.run
session = args.session
lg.debug("Session: {}".format(args.session))
lg.debug("Run number: {}".format(runNo))
def wait(n):
'''Wait until the next increment of n seconds'''
x = time.time()
lg.debug("It is {0} now, \
will wait {1}s with next execution".format(time.asctime(), n - (x % n)))
time.sleep(n - (x % n))
def get_measurements():
measurements = {}
# Get all measurements from Miniserver
lg.info("Getting measurements from Miniserver...")
for loxobject in load_measurements('measurements.txt'):
loxval = loxclient(
loxhost, loxusr, loxpass, obj=loxobject, strip=True, lib=args.lib
)
if loxval is None:
lg.error("Getting of value for {} failed.".format(loxobject))
continue
measurements[loxobject] = loxval
lg.debug("{0} = {1}".format(loxobject, loxval))
#lg.debug("Obtained measurements: {}".format(measurements))
lg.info("Obtained measurements: {}".format(measurements))
return measurements
# Create the InfluxDB object
client = InfluxDBClient(host, port, user, password, dbname)
def write_measurements(data):
json_body = [
{
"measurement":
session,
"tags": {"run": runNo},
"time": iso,
"fields": data
}
]
# Write JSON to InfluxDB
lg.debug("The write json content is: {}".format(json_body))
client.write_points(json_body)
if args.dry:
from timeit import Timer
t_collect = Timer(
"get_measurements()", "from __main__ import get_measurements"
)
print(
'Time to get all measurements: {0}'.format(t_collect.timeit(number=1))
)
exit(0)
# Run until keyboard out
try:
while True:
iso = time.ctime()
measurements = get_measurements()
if measurements == {}:
lg.error("We didn't receive any values from Miniserver. \
Not going to push anything to InfluxDB.")
else:
write_measurements(measurements)
# Wait for next sample
wait(interval)
except KeyboardInterrupt:
pass