-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfiguration.py
49 lines (44 loc) · 1.87 KB
/
configuration.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
import requests
import requests.exceptions
import platform
from cpuinfo import cpuinfo
from time import sleep
class Configuration(object):
def __init__(self, server, fqdn, secret, logger):
self.secret = secret
self.fqdn = fqdn
self.server = server
self.logger = logger
self.send_throttle = 120
self.monitor_enabled = {
"apache": False
}
def fetch(self):
configpath = "http://{}/client/config/{}/{}".format(self.server, self.fqdn, self.secret)
self.logger.debug("Fetching {}".format(configpath))
cpu_info = cpuinfo.get_cpu_info()
sysinfo = {
"arch": platform.machine(),
"dist": " ".join(list(platform.linux_distribution())),
"cpu": cpu_info["brand"],
"cores": cpu_info["count"]
}
try:
response = requests.get(configpath, params=sysinfo)
if response.status_code == 200:
server_config = response.json()
if not server_config["enabled"]:
self.logger.error("This client hasn't been accepted on the server yet.")
self.logger.info("Retrying in {} minutes".format(server_config["polling_time"]))
sleep(60 * server_config["polling_time"])
self.fetch()
else:
self.logger.info("Config download successful")
self.send_throttle = server_config["send_throttle"]
self.monitor_enabled = server_config["monitor_enabled"]
else:
self.logger.error("Server error {} at http://{}".format(response.status_code, self.server))
exit(1)
except requests.exceptions.ConnectionError as e:
self.logger.error("Cannot connect to the server at http://{}".format(self.server))
exit(1)