-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.py
More file actions
605 lines (485 loc) · 19.2 KB
/
functions.py
File metadata and controls
605 lines (485 loc) · 19.2 KB
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
import keyboards
import asyncio
import yaml
import os
from PIL import Image, ImageDraw, ImageFont
from aiogram import Dispatcher, F
from aiogram.types import Message, InlineKeyboardMarkup, InlineKeyboardButton, CallbackQuery, FSInputFile
from aiogram.filters import Command
from aiogram.fsm.context import FSMContext
from aiogram.fsm.state import State, StatesGroup
from dotenv import load_dotenv
#=======# GLOBAL VARS #=======#
load_dotenv("envs/.security")
shellpw = os.getenv("SHELL_PASSWD")
buttonTypesLogs = {
"syslog": "TXT",
"auth": "TXT",
"dmesg": "TXT"
}
buttonTypesStatus = {
"mem": "TXT",
"cpu": "TXT",
"disks": "TXT"
}
with open("configs/image.yml", "r", encoding="utf-8") as config:
image_settings = yaml.safe_load(config)
fp = image_settings['font_path']
fs = image_settings['font_size']
p0 = image_settings['padding'][0]
p1 = image_settings['padding'][1]
lh = image_settings['line_height']
bc = image_settings['bg_color']
tc = image_settings['text_color']
sp = image_settings['save_path']
mw = image_settings['max_image_width']
mh = image_settings['max_image_height']
with open("configs/mconfiguration.yml", "r", encoding="utf-8") as config:
mconfiguration = yaml.safe_load(config)
#=======# SYSTEM VARS #=======#
systemctl_status = ""
security_lastlogs = mconfiguration["security_lastlogs"]
systemctl_statuses = [
"status",
"start",
"stop",
"restart"
]
#=======# STATUS VARS #=======#
status_prefix = "status_"
status_lines = mconfiguration["status_lines"]
statuses = [
f"{status_prefix}mem",
f"{status_prefix}cpu",
f"{status_prefix}disks"
]
scan_disks = [
"/dev/*"
]
ps_limiter = mconfiguration["ps_limiter"]
#=======# LOGS VARS #=======#
log_prefix = "logs_"
log_lines = mconfiguration["log_lines"]
main_log_dir = "/var/log"
log_paths = {
f"{log_prefix}syslog": "/var/log/syslog",
f"{log_prefix}auth": "/var/log/auth.log",
f"{log_prefix}dmesg": "/var/log/dmesg"
}
log_commands = {
f"{log_prefix}syslog": f"sudo tail -n {log_lines} {log_paths[f'{log_prefix}syslog']}",
f"{log_prefix}auth": f"sudo tail -n {log_lines} {log_paths[f'{log_prefix}auth']}",
f"{log_prefix}dmesg": f"sudo tail -n {log_lines} {log_paths[f'{log_prefix}dmesg']}"
}
#=======# CONFIGURATION VARS #=======#
#=======# GLOBAL FUNCTIONS #=======#
class waitings(StatesGroup):
waiting_systemctl = State()
waiting_shellpw = State()
waiting_shell_command = State()
waiting_edit_configuration = State()
def update_conf(updates: dict):
global mconfiguration
for var, value in updates.items():
mconfiguration[var] = value
with open("configs/mconfiguration.yml", "w", encoding="utf-8") as config:
for var, value in mconfiguration.items():
config.write(f"{var}: {value}\n")
async def answerw(message: Message, state: FSMContext):
cs = await state.get_state()
global systemctl_status
global shellpw
if cs == waitings.waiting_systemctl:
uinput = message.text.strip()
if len(uinput.split()) > 1:
await message.answer(
f"<b>Service name is incorrect, exit</b>",
parse_mode="HTML"
)
return
check = await command_shell(f"sudo systemctl status {uinput}")
if "code 4" in check:
await message.answer(
f"<b>Service not found, exit</b>",
parse_mode="HTML"
)
return
if systemctl_status == "status":
path = await command_image(f"echo 'Checking...\n';sudo systemctl status {uinput} --no-pager")
img = FSInputFile(path)
await message.answer_photo(
img,
caption=f"<b>Systemctl status {uinput} result</b>",
parse_mode="HTML"
)
elif systemctl_status == "stop":
path = await command_image(f"echo 'Stopping...\n';sudo systemctl stop {uinput}; echo 'Checking...\n';sudo systemctl status {uinput} --no-pager")
img = FSInputFile(path)
await message.answer_photo(
img,
caption=f"<b>Systemctl stop {uinput} result</b>",
parse_mode="HTML"
)
elif systemctl_status == "start":
path = await command_image(f"echo 'Starting...\n';sudo systemctl start {uinput};echo 'Checking...\n';sudo systemctl status {uinput} --no-pager")
img = FSInputFile(path)
await message.answer_photo(
img,
caption=f"<b>Systemctl start {uinput} result</b>",
parse_mode="HTML"
)
else:
path = await command_image(f"echo 'Restarting...\n';sudo systemctl restart {uinput}; echo 'Checking...\n';sudo systemctl status {uinput} --no-pager")
img = FSInputFile(path)
await message.answer_photo(
img,
caption=f"<b>Systemctl restart {uinput} result</b>",
parse_mode="HTML"
)
await state.clear()
if cs == waitings.waiting_shellpw:
uinput = message.text.strip()
if uinput != shellpw:
await message.answer(
f"<b>Password wrong, exit</b>",
parse_mode="HTML"
)
await state.clear()
return
await message.answer(
f"<b>Success. Enter command</b>",
parse_mode="HTML"
)
await state.set_state(waitings.waiting_shell_command)
return
if cs == waitings.waiting_edit_configuration:
uinput = message.text.split("\n")
result = {}
for uinput_str in uinput:
fin_str = uinput_str.split("=")
if fin_str[0] in mconfiguration:
result[fin_str[0]] = fin_str[1]
else:
await message.answer(
f"<b>Wrong system var, exit</b>",
parse_mode="HTML"
)
await state.clear()
return
update_conf(result)
await message.answer(
f"<b>Configuration updated</b>",
parse_mode="HTML"
)
await state.clear()
async def tbut(callback: CallbackQuery):
type_button = callback.data.split(":", 1)[1]
if type_button in buttonTypesLogs:
buttonTypesLogs[type_button] = "IMG" if buttonTypesLogs[type_button] == "TXT" else "TXT"
await callback.answer()
await callback.message.edit_reply_markup(
reply_markup=keyboards.get_logs_keyboard(buttonTypesLogs, log_prefix)
)
elif type_button in buttonTypesStatus:
buttonTypesStatus[type_button] = "IMG" if buttonTypesStatus[type_button] == "TXT" else "TXT"
await callback.answer()
await callback.message.edit_reply_markup(
reply_markup=keyboards.get_status_keyboard(buttonTypesStatus, status_prefix)
)
def render_image(text: str) -> str:
try:
font = ImageFont.truetype(fp, fs)
except OSError:
print("E: FONT NOT FOUND")
lines = text.splitlines() or [""]
MAX_WIDTH_CHARS = 94
processed_lines = []
for line in lines:
if len(line) > MAX_WIDTH_CHARS:
line = line[:MAX_WIDTH_CHARS - 1] + ">"
processed_lines.append(line)
lines = processed_lines
if hasattr(font, 'getbbox'):
char_width = font.getbbox("A")[2]
else:
char_width = font.getsize("A")[0]
img_width = min(len(max(lines, key=len)) * char_width + 2 * p0, mw)
img_height = min(len(lines) * lh + 2 * p1, mh)
img = Image.new("RGB", (img_width, img_height), bc)
draw = ImageDraw.Draw(img)
y = p1
for line in lines:
draw.text((p0, y), line, fill=tc, font=font)
y += lh
img.save(sp)
return sp
async def command_image(cmd: str) -> str:
proc = await asyncio.create_subprocess_shell(
cmd,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.STDOUT
)
stdout, _ = await proc.communicate()
output = stdout.decode('utf-8', errors='replace')
if not output.strip():
output = "[OUTPUT IS EMPTY]"
image_path = render_image(output)
return image_path
async def command_shell(cmd: str) -> str:
try:
proc = await asyncio.create_subprocess_shell(
cmd,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE
)
stdout, stderr = await proc.communicate()
if proc.returncode == 0:
output = stdout.decode('utf-8').strip()
else:
output = f"E: (code {proc.returncode}):\n{stderr.decode('utf-8').strip()}"
return output
except Exception as e:
return f"E: EXCEPTION - \n{str(e)}"
#=======# START FUNCTIONS #=======#
async def start(message: Message):
await message.answer(
f"<b>Activated admin keyboard</b>",
parse_mode="HTML",
reply_markup=keyboards.get_system_keyboard()
)
#=======# SYSTEM FUNCTIONS #=======#
async def system(message: Message):
hostname = await command_shell("hostname")
ip = await command_shell("hostname -I")
await message.answer(
f"<b>USER: </b>{message.from_user.first_name}\n\n"
f"<b>VM: </b>{hostname}\n"
f"<b>IP: </b>{ip}\n",
parse_mode="HTML",
reply_markup=keyboards.get_system_usage_keyboard()
)
async def system_security(callback: CallbackQuery):
uname = await command_shell("uname -a")
users = await command_shell("who")
lastl = await command_image(f"last -n {security_lastlogs}")
await callback.message.answer(
f"<b><u>SYSTEM SECURITY</u></b>\n"
f"\n<b>VM extended:</b>\n"
f"<pre>{uname}</pre>\n"
f"\n<b>Users in system: </b>\n"
f"<pre>{users}</pre>",
parse_mode="HTML"
)
img = FSInputFile(lastl)
await callback.message.answer_photo(
img,
caption=f"<b>Last {security_lastlogs} logins in system</b>",
parse_mode="HTML"
)
async def system_systemctl(callback: CallbackQuery):
await callback.answer()
svc_active = await command_shell("sudo systemctl list-units --all --type=service --state=active | grep service | wc -l")
svc_inactive = await command_shell("sudo systemctl list-units --all --type=service --state=inactive | grep service | wc -l")
await callback.message.answer(
f"<b><u>SYSTEMCTL STATUS</u></b>\n\n"
f"<b>Active services:</b> {svc_active}\n"
f"<b>Inactive services:</b> {svc_inactive}",
parse_mode="HTML",
reply_markup=keyboards.get_system_systemctl_keyboard()
)
async def systemctl_definer(callback: CallbackQuery, state: FSMContext):
await callback.answer()
global systemctl_status
if callback.data == "systemctl_status":
systemctl_status = "status"
elif callback.data == "systemctl_stop":
systemctl_status = "stop"
elif callback.data == "systemctl_start":
systemctl_status = "start"
else:
systemctl_status = "restart"
await callback.message.answer(
f"<b>Enter systemctl UNIT name to <u>{systemctl_status}</u></b>",
parse_mode="HTML"
)
await state.set_state(waitings.waiting_systemctl)
async def system_shell(callback: CallbackQuery, state: FSMContext):
await callback.answer()
await callback.message.answer(
f"<b>Enter shell password</b>",
parse_mode="HTML"
)
await state.set_state(waitings.waiting_shellpw)
async def shell_command(message: Message, state: FSMContext):
shell_command = message.text.strip()
path = await command_image(shell_command)
img = FSInputFile(path)
await message.answer_photo(
img,
caption=f"<b>Command result</b>",
parse_mode="HTML"
)
await state.clear()
#=======# STATUS FUNCTIONS #=======#
async def status(message: Message):
hostname = await command_shell("hostname")
ip = await command_shell("hostname -I")
la = await command_shell(f"uptime | grep -Eo '[0-9],[0-9]{{2}}.*'")
mem_total = await command_shell(r"""free -m | awk 'NR==2 {printf "%.2f\n",$2/1024}'""")
mem_used = await command_shell(r"""free -m | awk 'NR==2 {printf "%.2f\n",$3/1024}'""")
mem_avail = await command_shell(r"""free -m | awk 'NR==2 {printf "%.2f\n",$7/1024}'""")
cpus = await command_shell(r"""top -bn1 | grep -Eo "[0-9].*" | awk 'NR==3 {print}' | column | sed 's/, / /g'""")
uptime = await command_shell(f"uptime -p")
counter = 1
priv_disks = await command_shell(f"df -h | awk 'NR==1 {{print}}'") + "\n"
for disk in scan_disks:
output = await command_shell(f"df -h | grep -E '^{disk}'")
if counter == len(scan_disks):
priv_disks += f"{output}"
else:
priv_disks += f"{output}\n"
await message.answer(
f"<b>Status of {hostname}</b>\n"
f"{uptime}\n"
f"\n<b><u>MEMORY STATUS</u></b>\n"
f"<b>T/U/A: {mem_total} Gb | {mem_used} Gb | {mem_avail} Gb</b>\n"
f"\n<b><u>CPU STATUS</u></b>\n"
f"<b>Load Average:</b> {la}\n"
f"<b>CPUs:</b> {cpus}\n"
f"\n<b><u>DISKS STATUS</u></b>\n"
f"<pre>{priv_disks}</pre>",
parse_mode="HTML",
reply_markup=keyboards.get_status_keyboard(buttonTypesStatus, status_prefix)
)
async def status_definer(callback: CallbackQuery):
await callback.answer()
if callback.data == f"{status_prefix}mem":
if buttonTypesStatus["mem"] == "TXT":
mem_top = await command_shell(f"ps -eo 'pid ppid user %mem %cpu command' --sort=-%mem | awk '{{print substr($0,1,50)}}' | head -n {ps_limiter}")
await callback.message.answer(
f"<b>TOP {ps_limiter} MEMORY PROCESSES</b>\n\n"
f"<pre>{mem_top}</pre>",
parse_mode="HTML"
)
else:
path = await command_image(f"ps -eo 'pid ppid user %mem %cpu command' --sort=-%mem | head -n {ps_limiter}")
img = FSInputFile(path)
await callback.message.answer_photo(
img,
caption=f"<b>Last {ps_limiter} lines of ps sorted by memory usage</b>",
parse_mode="HTML"
)
elif callback.data == f"{status_prefix}cpu":
if buttonTypesStatus["cpu"] == "TXT":
cpu_top = await command_shell(f"ps -eo 'pid ppid user %mem %cpu command' --sort=-%cpu | awk '{{print substr($0,1,50)}}' | head -n {ps_limiter}")
await callback.message.answer(
f"<b>TOP {ps_limiter} CPU PROCESSES</b>\n\n"
f"<pre>{cpu_top}</pre>",
parse_mode="HTML"
)
else:
path = await command_image(f"ps -eo 'pid ppid user %mem %cpu command' --sort=-%cpu | head -n {ps_limiter}")
img = FSInputFile(path)
await callback.message.answer_photo(
img,
caption=f"<b>Last {ps_limiter} lines of ps sorted by CPU usage</b>",
parse_mode="HTML"
)
else:
if buttonTypesStatus["disks"] == "TXT":
df_stat = await command_shell("df -h")
lsblk = await command_shell("lsblk | grep -v 'loop'")
await callback.message.answer(
f"<b>DF</b>\n"
f"<pre>{df_stat}</pre>\n"
f"\n<b>LSBLK</b>\n"
f"<pre>{lsblk}</pre>",
parse_mode="HTML"
)
else:
path = await command_image("echo '|=============| DF |=============|\n';df -h; echo '\n|=============| LSBLK |=============|\n';lsblk | grep -v 'loop'")
img = FSInputFile(path)
await callback.message.answer_photo(
img,
caption=f"<b>Disks information</b>",
parse_mode="HTML"
)
#=======# LOGS FUNCTIONS #=======#
async def logs(message: Message):
du_logs = await command_shell("sudo du -sh /var/log | awk '{print $1}'")
await message.answer(
f"<b>Log directory:</b> {main_log_dir}\n"
f"<b>Size:</b> {du_logs}",
parse_mode="HTML",
reply_markup=keyboards.get_logs_keyboard(buttonTypesLogs, log_prefix)
)
async def logs_definer(callback: CallbackQuery):
await callback.answer()
for log_type, path in log_paths.items():
if log_type == callback.data:
command = log_commands[log_type]
if buttonTypesLogs[log_type[len(log_prefix):]] == "TXT":
output = await command_shell(command)
await callback.message.answer(
f"<b>Last {log_lines} of {log_paths[log_type]}</b>\n\n"
f"<pre>{output}</pre>",
parse_mode="HTML"
)
else:
path = await command_image(command)
img = FSInputFile(path)
await callback.message.answer_photo(
img,
caption=f"<b>Last {log_lines} of {log_paths[log_type]}</b>",
parse_mode="HTML"
)
#=======# CONFIGURATION FUNCTIONS #=======#
async def configuration(message: Message):
output = ""
for var, value in mconfiguration.items():
output += f"{var}: {value}\n"
await message.answer(
f"<b><u>CONFIGURATION PARAMS</u></b>\n\n"
f"{output}",
parse_mode="HTML",
reply_markup=keyboards.get_configuration_keyboard()
)
async def configuration_edit(callback: CallbackQuery, state: FSMContext):
await callback.answer()
example = f"param1=10\nparam2=20"
await callback.message.answer(
f"<b>Enter configuration params to edit</b>\n\n"
f"<b>Examle:</b>\n"
f"{example}",
parse_mode="HTML"
)
await state.set_state(waitings.waiting_edit_configuration)
#=======# FALLBACK FUNCTION #=======#
async def fallback(message: Message):
await message.answer(
f"<b>Activate admin keyboard:</b> /start",
parse_mode="HTML"
)
#=======# HANDLERS #=======#
def register_handlers(dp: Dispatcher):
dp.callback_query.register(tbut, F.data.startswith("tbut:"))
dp.message.register(start, Command("start"))
dp.message.register(system, F.text == "System")
dp.message.register(status, F.text == "Status")
dp.message.register(logs, F.text == "Logs")
dp.message.register(configuration, F.text == "Configuration")
dp.callback_query.register(system_security, F.data == "system_security")
dp.callback_query.register(system_systemctl, F.data == "system_systemctl")
dp.callback_query.register(system_shell, F.data == "system_shell")
dp.callback_query.register(configuration_edit, F.data == "configuration_edit")
dp.message.register(answerw, waitings.waiting_systemctl)
dp.message.register(answerw, waitings.waiting_shellpw)
dp.message.register(answerw, waitings.waiting_edit_configuration)
dp.message.register(shell_command, waitings.waiting_shell_command)
for fsystemctl_type_data in systemctl_statuses:
dp.callback_query.register(systemctl_definer, F.data == f"systemctl_{fsystemctl_type_data}")
for fstatus_type_data in statuses:
dp.callback_query.register(status_definer, F.data == fstatus_type_data)
for flog_type_data, path in log_paths.items():
dp.callback_query.register(logs_definer, F.data == flog_type_data)
dp.message.register(fallback)