-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
98 lines (76 loc) · 3.3 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
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
import json
import os.path
from types import SimpleNamespace
from datetime import date, datetime
from threading import Thread
import time
import requests
CONFIG_PATH = "config.json"
with open(CONFIG_PATH) as f:
config = json.loads(f.read(), object_hook=lambda d: SimpleNamespace(**d))
LOG_DIRECTORY = "Logs"
class ServiceMonitor:
def __init__(self, service_prop):
self.prop = service_prop
self.is_updated = False
self.thread = Thread(target=self.main)
self.thread.start()
def main(self):
while True:
try:
self.check()
except Exception as e:
print("Exception ", self.prop.group_name, self.prop.name, e)
pass
time.sleep(self.prop.period)
def check(self):
data = {}
files = {}
json_data = {}
auth = None
if hasattr(self.prop, "auth"):
if self.prop.auth.type == "basic":
auth = (self.prop.auth.username, self.prop.auth.password)
if hasattr(self.prop, "payload"):
if hasattr(self.prop.payload, "form_data"):
for form_data in self.prop.payload.form_data:
if form_data.type == "text":
data[form_data.key] = form_data.value
elif form_data.type == "file":
files[form_data.key] = open(form_data.value, 'rb')
if hasattr(self.prop, "json"):
json_data = self.prop.payload.json
response = None
if self.prop.method == "POST":
response = requests.post(self.prop.url, data=data, json=json_data, files=files, auth=auth)
elif self.prop.method == "GET":
response = requests.get(self.prop.url, data=data, json=json_data, files=files, auth=auth)
save_directory = os.path.join(LOG_DIRECTORY, self.prop.group_name, self.prop.name)
if not os.path.exists(save_directory):
os.makedirs(save_directory)
now = datetime.now()
print(now.strftime("%d/%m/%Y %H:%M:%S"),self.prop.group_name + " -> " + self.prop.name + " -> " + str(response.status_code))
if response and response.status_code == 200:
if hasattr(self.prop, "response"):
if self.prop.response.type == "file":
pass # write response.content in the file
elif self.prop.response.type == "json":
file_name = os.path.join(save_directory,str(time.time())+"_200.json")
with open(file_name, "w+") as f:
f.write(json.dumps(response.json()))
else:
pass # write response.status_code in the file
else:
file_name = os.path.join(save_directory, str(time.time()) +"_"+str(response.status_code)+ ".txt")
with open(file_name, "w+") as f:
f.write(response.text)
inherited_properties = ["period", "auth", "method", "response", "group_name"]
for group in config.groups:
for service in group.services:
if service.enabled:
for key in inherited_properties:
if not hasattr(service, key) and hasattr(group, key):
setattr(service, key, getattr(group, key))
ServiceMonitor(service)
while True:
time.sleep(1)