-
Notifications
You must be signed in to change notification settings - Fork 0
/
report.py
147 lines (130 loc) · 5.63 KB
/
report.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
#!/usr/bin/env python3
###########################################################
## RPi Monitor Dashboard ##
## https://github.com/nekromoff/rpi-monitor-dashboard ##
## Copyright (c) 2024+ Daniel Duris, [email protected] ##
## License: MIT ##
## Version: 3.0 ##
###########################################################
__version__="3.0"
import sys
# tomli/tomllib compatibility layer as Python 3.11+ contains tomllib by default
if sys.version_info >= (3, 11):
import tomllib
else:
import tomli as tomllib
import subprocess
import json
import requests
import binascii
with open("config.toml", "rb") as f:
config = tomllib.load(f)
content={}
# always extract hostname
output=subprocess.run("/usr/bin/hostname", universal_newlines = True, stdout = subprocess.PIPE)
hostname=output.stdout.strip()
content["hostname"]=hostname
headers = {"User-Agent": "RPi Monitor Dashboard/"+__version__, "X-Hostname": hostname, "X-Crc32": str(binascii.crc32(open("report.py", "rb").read())), "Content-Type": "application/json"}
# check for available updates
response=requests.head(config["receiver"], headers = headers)
# check for python script update and overwrite, if auto update enabled
if "X-Update" in response.headers and response.headers["X-Update"][2]=="1" and config['auto_update']==True:
response=requests.get(config["receiver"]+'?update=3', headers = headers)
try:
if len(response.text)>0:
subprocess.run('cp report.py report.py.bak', shell = True)
f = open("report.py", "w")
f.write(response.text)
f.close()
except Exception:
# ignore, skip
pass
# check for one-time commands and proceed with execution
if "X-Update" in response.headers and response.headers["X-Update"][1]=="1":
response=requests.get(config["receiver"]+'?update=2', headers = headers)
commands=response.text.split("\n")
try:
for line_no, command in enumerate(commands):
command=command.replace("{hostname}", hostname)
subprocess.run([command], shell = True)
except Exception:
# ignore, skip
pass
# check for config update and proceed with update
if "X-Update" in response.headers and response.headers["X-Update"][0]=="1":
# backup current config in case the new one is messed up
subprocess.run('cp config.toml config.toml.bak', shell = True)
response=requests.get(config["receiver"]+'?update=1', headers = headers)
current_config=open("config.toml", "rt").read()
current_config_lines=current_config.split("\n")
new_config=''
for line_no, line in enumerate(current_config_lines):
new_config=new_config+line+"\n"
# search for our special line
pos=line.strip().find("#-#*+*#-#")
# if found, assemble new config from existing receiver and config update received
if pos!=-1:
new_config=new_config+response.text;
new_config=new_config.strip()
f = open("config.toml", "w")
f.write(new_config)
f.close()
# validate new config
try:
with open("config.toml", "rb") as f:
test_config = tomllib.load(f)
# once validated, confirm it back to receiver
# content["_config"]=open("config.toml", "rt").read()
#json_data = json.dumps(content)
#response = requests.post(config["receiver"], data=json_data, headers=headers)
except tomllib.TOMLDecodeError:
# new config parse fail, invalid TOML config, fallback to backup config
subprocess.run('cp config.toml.bak config.toml', shell = True)
# backup cleanup
subprocess.run('rm config.toml.bak', shell = True)
break;
with open("config.toml", "rb") as f:
config = tomllib.load(f)
# include config contents in the payload
content["_config"]=open("config.toml", "rt").read()
for name, command in config["commands"].items():
try:
# simple command (TOML string)
if isinstance(command, str):
command=command.replace("{hostname}", hostname)
parts=command.split(" ")
output=subprocess.run(parts, universal_newlines = True, stdout = subprocess.PIPE)
content[name]=output.stdout.strip()
# command with text search and extraction (TOML array)
elif isinstance(command, list):
command[0]=command[0].replace("{hostname}", hostname)
parts=command[0].split(" ")
output=subprocess.run(parts, universal_newlines = True, stdout = subprocess.PIPE)
content[name]=""
for string in command[1]:
for line in output.stdout.split("\n"):
if string in line:
content[name]=content[name]+line.strip()+"\n"
content[name]=content[name].strip()
except Exception:
# ignore, skip
pass
try:
for name, command in config["commands_shell"].items():
command=command.replace("{hostname}", hostname)
subprocess.run([command], shell = True)
except Exception:
# ignore, skip
pass
# post payload
json_data = json.dumps(content)
response = requests.post(config["receiver"], data = json_data, headers = headers)
headers.pop("Content-Type")
# if screenshot command exists, upload image
try:
if config["commands_shell"]["screenshot"]:
files = {'screenshot': open(hostname+".png", 'rb')}
r = requests.post(config["receiver"], files = files, headers = headers)
except Exception:
# ignore, skip
pass