-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
254 lines (176 loc) · 7.46 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
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
import os
import subprocess
from pathlib import Path
from datetime import datetime
from dotenv import load_dotenv
from flask import Flask, render_template, redirect, url_for, flash
from flask_caching import Cache
from libs.pi_system_info import PiSystemInfo
from libs.log_utils import LoggerSingleton
load_dotenv('.env')
PORT = os.getenv("PORT", 8080)
INDEX_PAGE_CACHE_TIMEOUT = int(os.getenv("INDEX_PAGE_CACHE_TIMEOUT", 10))
INDEX_PAGE_TITLE = os.getenv("INDEX_PAGE_TITLE", 'Raspberry Pi System Info')
CPU_ORANGE_TEMP_THRESHOLD = float(os.getenv("CPU_ORANGE_TEMP_THRESHOLD", 50))
CPU_RED_TEMP_THRESHOLD = float(os.getenv("CPU_RED_TEMP_THRESHOLD", 60))
TEXT_GREEN_COLOR = os.getenv("TEXT_GREEN_COLOR", "#00FF40")
TEXT_ORANGE_COLOR = os.getenv("TEXT_ORANGE_COLOR", "#FF8C00")
TEXT_RED_COLOR = os.getenv("TEXT_RED_COLOR", "#CC0000")
TEXT_DATETIME_FORMAT = os.getenv("TEXT_DATETIME_FORMAT", "%d-%b-%Y, %H : %M : %S")
logger = LoggerSingleton(
log_dir=Path(os.getenv("LOGS_PATH", "logs")),
log_file=os.getenv("LOG_FILENAME"),
level=os.getenv("LOG_LEVEL"),
msg_format=os.getenv("LOG_MSG_FORMAT"),
date_format=os.getenv("LOG_DATETIME_FORMAT"),
colored=True
).get_logger()
pi_sys_info = PiSystemInfo(logger=logger)
config = {
"CACHE_TYPE": "SimpleCache",
"CACHE_DEFAULT_TIMEOUT": 30,
"SECRET_KEY": "pi_system'info"
}
app = Flask(__name__)
app.config.from_mapping(config)
cache = Cache(app)
app.logger.handlers = logger.handlers
app.logger.setLevel(logger.level)
@app.route('/')
@cache.cached(timeout=INDEX_PAGE_CACHE_TIMEOUT)
def index(logger=logger):
logger.info('Request index.html')
return render_template("index.html", title=INDEX_PAGE_TITLE, index_url=url_for('index'))
@app.route('/restart')
def restart(logger=logger):
flash("Rebooting... please wait.<br>This will take approx. one minute.", 'info')
logger.info('Restart initiated from web interface')
subprocess.Popen(["sudo", "reboot"])
return render_template('system_action_pending.html', title=INDEX_PAGE_TITLE, index_url=url_for('index'), action="Restart")
@app.route('/shutdown')
def shutdown(logger=logger):
flash("Shutting down.<br>When the LEDs on the board stop flashing, it should be safe to unplug your Raspberry Pi.", 'info')
logger.info('Shutdown initiated from web interface')
subprocess.Popen(["sudo", "halt"])
return render_template('system_action_pending.html', title=INDEX_PAGE_TITLE, index_url=url_for('index'), action="Shutdown")
@app.errorhandler(404)
def page_not_found_error(error):
return render_template('error.html', title=INDEX_PAGE_TITLE, error_code="404",
error_message="Page not found", redirect_delay=5, index_url=url_for('index')), 404
@app.errorhandler(500)
def internal_server_error(error):
return render_template('error.html', title=INDEX_PAGE_TITLE, error_code="500",
error_message="Internal server error", redirect_delay=5, index_url=url_for('index')), 500
@app.context_processor
def pi_hostname(logger=logger):
return dict(pi_hostname=pi_sys_info.hostname)
@app.context_processor
def pi_model(logger=logger):
return dict(pi_model=pi_sys_info.model)
@app.context_processor
def pi_os(logger=logger):
return dict(pi_os=pi_sys_info.os_name)
@app.context_processor
def uptime_since(logger=logger):
return dict(uptime_since=pi_sys_info.get_uptime_since().strftime(TEXT_DATETIME_FORMAT))
@app.context_processor
def uptime_pretty(logger=logger):
return dict(uptime_pretty=pi_sys_info.get_uptime_pretty())
@app.context_processor
def current_time(logger=logger):
return dict(current_time=datetime.now().strftime(TEXT_DATETIME_FORMAT))
@app.context_processor
def cpu_architecture(logger=logger):
return dict(cpu_architecture=pi_sys_info.cpu_architecture)
@app.context_processor
def cpu_model_name(logger=logger):
return dict(cpu_model_name=pi_sys_info.cpu_model_name)
@app.context_processor
def cpu_hardware_type(logger=logger):
return dict(cpu_hardware_type=pi_sys_info.cpu_hardware_type)
@app.context_processor
def cpu_serial_number(logger=logger):
return dict(cpu_serial_number=pi_sys_info.cpu_serial_number)
@app.context_processor
def cpu_revision(logger=logger):
return dict(cpu_revision=pi_sys_info.cpu_revision)
@app.context_processor
def cpu_core_count(logger=logger):
return dict(cpu_core_count=pi_sys_info.cpu_core_count)
@app.context_processor
def cpu_core_frequency(logger=logger):
return dict(cpu_core_frequency=pi_sys_info.get_cpu_core_frequency())
@app.context_processor
def cpu_cache_sizes(logger=logger):
return dict(cpu_cache_sizes=pi_sys_info.cpu_cache_sizes)
@app.context_processor
def cpu_core_voltage(logger=logger):
return dict(cpu_core_voltage=f"{pi_sys_info.get_cpu_core_voltage(): .3f}")
@app.context_processor
def cpu_usage(logger=logger):
return dict(cpu_usage=pi_sys_info.get_cpu_usage())
@app.context_processor
def cpu_temperature(logger=logger):
temperature = pi_sys_info.get_cpu_temperature()
color = TEXT_GREEN_COLOR
if CPU_ORANGE_TEMP_THRESHOLD < temperature < CPU_RED_TEMP_THRESHOLD:
color = TEXT_ORANGE_COLOR
elif temperature >= CPU_RED_TEMP_THRESHOLD:
color = TEXT_RED_COLOR
return dict(cpu_temperature={'temperature': temperature, 'color': color})
@app.context_processor
def ram_info(logger=logger):
return dict(ram_info=pi_sys_info.get_ram_info())
@app.context_processor
def ethernet_ip_info(logger=logger):
ip_info = pi_sys_info.get_ip_info('eth0')
default_str = 'Not connected'
address = ip_info.get('ip', '')
mask = ip_info.get('mask', '')
return dict(
ethernet_ip_address=address if len(address) > 0 else default_str,
ethernet_network_mask=mask if len(mask) > 0 else default_str
)
@app.context_processor
def ethernet_mac_address(logger=logger):
address = pi_sys_info.get_mac_address('eth0')
return dict(ethernet_mac_address=address if address is not None and len(address) > 0 else 'Unknown')
@app.context_processor
def wifi_ip_address(logger=logger):
ip_info = pi_sys_info.get_ip_info('wlan0')
default_str = 'Not connected'
address = ip_info.get('ip', '')
mask = ip_info.get('mask', '')
return dict(
wifi_ip_address=address if len(address) > 0 else default_str,
wifi_network_mask=mask if len(mask) > 0 else default_str
)
@app.context_processor
def wifi_mac_address(logger=logger):
address = pi_sys_info.get_mac_address('wlan0')
return dict(wifi_mac_address=address if address is not None and len(address) > 0 else 'Unknown')
@app.context_processor
def bluetooth_mac_address(logger=logger):
address = pi_sys_info.get_bluetooth_mac_address()
return dict(bluetooth_mac_address=address if address is not None and len(address) > 0 else 'Unknown')
@app.context_processor
def available_wifi_networks(logger=logger):
return dict(available_wifi_networks=pi_sys_info.get_available_wifi_networks())
@app.context_processor
def disk_usage_info(logger=logger):
return dict(disk_usage_info=pi_sys_info.get_disk_usage_info())
@app.context_processor
def running_process_info(logger=logger):
return dict(running_process_info=pi_sys_info.get_running_process_info())
@app.context_processor
def utility_processor(logger=logger):
def short_date(a,b,c):
return u'{0}{1}, {2}'.format(a, b,c)
return dict(short_date=short_date)
if __name__ == "__main__":
logger.info("Started")
try:
app.run(host="0.0.0.0", port=PORT, debug=False)
except KeyboardInterrupt:
logger.info("Stopped")
exit()