-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
66 lines (52 loc) · 1.96 KB
/
main.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
import os
import time
import json
import importlib
import schedule
from dotenv import load_dotenv
import influxdb_client
from influxdb_client.client.write_api import SYNCHRONOUS
load_dotenv()
influx_url = os.getenv("INFLUX_URL")
influx_token = os.getenv("INFLUX_TOKEN")
influx_org = os.getenv("INFLUX_ORG")
influx_bucket = os.getenv("INFLUX_BUCKET")
client = influxdb_client.InfluxDBClient(url=influx_url, token=influx_token, org=influx_org)
write_api = client.write_api(write_options=SYNCHRONOUS)
def create_influxdb_point(measurement, data):
point = influxdb_client.Point(measurement)
for key, value in data.items():
if key == 'host':
# hotfix for dot in hostname
point = point.tag(key, value.split('.')[0])
else:
point = point.field(key, value)
return point
def load_config():
with open('config/modules_config.json', 'r') as f:
config = json.load(f)
return config.get("modules", [])
def run_module(module_name):
module = importlib.import_module(f"modules.{module_name}")
data = module.collect_data()
if isinstance(data, list):
# If collect data return multiple records
for record in data:
point = create_influxdb_point(module_name, record)
write_api.write(bucket=influx_bucket, org=influx_org, record=point)
print(f"writing record for {module_name} finished.")
else:
point = create_influxdb_point(module_name, data)
write_api.write(bucket=influx_bucket, org=influx_org, record=point)
print(f"writing record for {module_name} finished.")
def main():
modules_config = load_config()
for module_config in modules_config:
module_name = module_config["name"]
interval_seconds = module_config.get("interval_seconds", False)
schedule.every(interval_seconds).seconds.do(
run_module, module_name=module_name)
while True:
schedule.run_pending()
time.sleep(1)
main()