-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmon.py
282 lines (241 loc) · 8.76 KB
/
mon.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
import asyncio
import datetime
import json
import os.path
import platform as plt
import signal
import sys
import threading
import time
from collections import defaultdict
from sys import exit
from notifypy import Notify
import pandas
import psutil
import requests
import websockets
from cpuinfo import get_cpu_info
from c_utils import API_URL, p_info, header
HOST = '0.0.0.0'
PORT = ''
rules = dict()
stats = {
"cpu": 0,
"mem": 0,
"disk": 0
}
time_period = {
"hour": 1,
"week": 7,
"month": 30
}
previous_report = None
reporting_time = None
def report(resource, rule, _stats):
global previous_report, reporting_time
if previous_report and reporting_time:
if rule == previous_report['report']['activity']['rule'] and (
datetime.datetime.now() - reporting_time).total_seconds() < 3600:
# print(f'suppressing report for {3600 - (datetime.datetime.now() - reporting_time).total_seconds()} seconds')
return
p_info("Reporting server...")
rep = {
"sys_id": system_config['sys_id'],
"v_token": system_config['v_token'],
"report": {
"activity": {
"resource": resource,
"rule": rule,
"message": f"{resource} crossed {rule['max_limit']}%"
},
"stats": _stats
}
}
res = send_request('post', API_URL + '/api/system/mon', pl=rep)
if res.status_code != 200:
p_info("Reporting failed!", res.status_code)
previous_report = rep
reporting_time = datetime.datetime.now()
def monitor():
global rules
secs = defaultdict(list)
while True:
_10_sec = gen_data(10)
for r in _10_sec:
secs[r].append(_10_sec[r])
if r in rules:
if _10_sec[r] >= rules[r]['max_limit']:
print("sys in trouble", _10_sec)
report(r, rules[r], _10_sec)
if len(secs[list(secs.keys())[0]]) == 360:
for i in secs:
secs[i] = sum(secs[i]) / 360
if not os.path.exists(os.path.join(os.path.expanduser("~"), ".sysmon")):
os.mkdir(os.path.join(os.path.expanduser("~"), ".sysmon"))
with open(os.path.join(os.path.expanduser("~"), ".sysmon", "hourly_logs.txt"), 'a+') as log:
json.dump({f"{datetime.datetime.now()}": secs}, log)
log.write("\n")
secs.clear()
def send_request(method, url, pl):
while True:
try:
if method == "get":
return requests.get(url, json=pl)
if method == "post":
return requests.post(url, json=pl)
if method == "patch":
return requests.patch(url, json=pl)
except Exception as e:
print(e, 'waiting for connection...')
time.sleep(10)
def gen_data(span=1):
# RAM
mem = psutil.virtual_memory()
# DISKS
used, total = 0, 0
for i in psutil.disk_partitions():
disk = psutil.disk_usage(i.mountpoint)
total += disk.total
used += disk.used
# CPU
cpu = psutil.cpu_percent(span)
data = {
"cpu": cpu,
"mem": mem.percent,
"disk": round(used * 100 / total, 2)
}
return data
def gen_spec():
uname_result = plt.uname()
cpu_info = get_cpu_info()
cores, l_cores = psutil.cpu_count(False), psutil.cpu_count()
spec = {
"Node": uname_result.node,
"Operating System Name": uname_result.system,
"Operating System Version": uname_result.version,
"Machine": uname_result.machine,
"Processor": {
"Processor Name": cpu_info['brand_raw'],
"Base Frequency": cpu_info['hz_actual_friendly'],
"Number of Cores (Logical Cores)": f"{cores} ({l_cores})",
"Architecture": cpu_info['arch']
},
"Memory": {
"Total Physical Memory": f"{round(psutil.virtual_memory().total / 2 ** 30, 2)} GB"
}
}
return json.dumps({"spec": spec})
def gen_report(t):
global time_period
data = {}
if not os.path.exists(os.path.join(os.path.expanduser("~"), ".sysmon", "hourly_logs.txt")):
with open(os.path.join(os.path.expanduser("~"), ".sysmon", "hourly_logs.txt"), 'w') as log:
pass
with open(os.path.join(os.path.expanduser("~"), ".sysmon", "hourly_logs.txt"), 'r') as log:
while log:
chunk = log.readline().strip()
if chunk:
line = list((json.loads(chunk)).items())[0]
if (datetime.datetime.now() - datetime.datetime.fromisoformat(line[0])).days <= time_period[t]:
data[datetime.datetime.fromisoformat(line[0])] = line[1]
else:
continue
else:
break
df = pandas.DataFrame.from_dict(data=data, orient="index")
return df.to_json(date_format="iso")
async def handler(ws):
p_info("New connection:", ws.request_headers["Origin"], pre="INFO")
try:
async for message in ws:
if message == "cpd":
await ws.send(json.dumps({"stats": gen_data()}))
elif message == "spec":
await ws.send(gen_spec())
elif message == "update_mon":
update_mon()
elif message.startswith("report"):
await ws.send('{"report": ' + gen_report(message.split("-")[1]) + '}')
else:
await ws.send(json.dumps({"stats": gen_data()}))
except websockets.WebSocketException as err:
print("\nConnection CLOSED: ", err)
async def main():
async with websockets.serve(handler, HOST, PORT) as websocket:
addr = websocket.server._sockets[0].getsockname()
p_info(addr, pre="INFO")
payload = {
"port": addr[1],
"sys_id": system_config['sys_id'],
"v_token": system_config['v_token']
}
p_info("Sending patch request...", pre="INFO", end=' ')
if send_request("patch", API_URL + "/api/system/mon", payload).status_code == 200:
print("PATCHED")
p_info("Listening for connections...", pre="INFO")
await asyncio.Future()
exit(1)
def shutdown(sig, frame):
header("SHUT DOWN")
p_info("SHUTDOWN SIGNAL RECEIVED - Notifying server...", pre="INFO")
try:
p_info("RESPONSE:", requests.post(API_URL + "/api/system/activity", json={
"sys_id": system_config['sys_id'],
"v_token": system_config['v_token'],
"activity": ["SHUTDOWN", f"desc: {str(sig)}", "mon shutting down!"]
}).status_code, pre="INFO")
except ConnectionError or TimeoutError or Exception as err:
p_info(err, pre="ERROR")
p_info("Exiting...", pre="INFO")
exit(0)
def update_mon():
global rules
p_info("Connecting server...", pre="INFO", end=' ')
res = send_request("get", API_URL + "/api/system/mon", {
"sys_id": system_config['sys_id'],
"v_token": system_config['v_token']
})
if res.status_code == 200:
print("ok")
res = res.json()
if not res['enable_mon']:
p_info("MON DISABLED", pre="INFO")
p_info("Exiting...", pre="INFO")
exit(0)
else:
rules = res['rules']
else:
print()
p_info(f"{res.status_code} - {res.json()['message']} Exiting...", pre="ERROR")
exit(0)
if __name__ == "__main__":
try:
notif = Notify()
notif.title = "Mon"
notif.message = "Mon started!"
notif.application_name = "SysMon"
notif.icon = os.path.join(os.path.dirname(__file__), "favicon.png")
notif.send()
except Exception as e:
notif = Notify()
notif.title = "Mon"
notif.message = "Mon started!"
notif.application_name = "SysMon"
notif.send()
with open(os.path.join(os.path.expanduser('~'), ".sysmon", "sysmon_agent.config"), 'r') as conf:
system_config = json.load(conf)
if not ("sys_id" in system_config and "v_token" in system_config):
p_info("Wrong configuration file, re-install mon", pre="ERROR")
exit(1)
if not system_config:
exit(1)
if len(sys.argv) == 2:
PORT = sys.argv[1]
signal.signal(signal.SIGTERM, shutdown)
signal.signal(signal.SIGINT, shutdown)
threading.Thread(target=monitor, daemon=True).start()
update_mon()
try:
asyncio.run(main())
except KeyboardInterrupt or Exception as e:
p_info(f"Exiting...\n{e}", pre="ERROR")