-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
57 lines (45 loc) · 1.54 KB
/
Copy pathapp.py
File metadata and controls
57 lines (45 loc) · 1.54 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
from flask import Flask
import psutil
import json
import time
import datetime
import os
app = Flask(__name__)
class SystemStats:
def __init__(self):
self.cpu_percent = psutil.cpu_percent()
self.virtual_memory = psutil.virtual_memory().percent
self.uptime = str(datetime.timedelta(seconds=time.time() - psutil.boot_time()))
self.process_count = len(psutil.pids())
network = psutil.net_if_addrs()
self.network = dict()
for i in network:
if i != 'lo':
self.network[i] = network[i][0].address
sensors = psutil.sensors_temperatures()
self.sensors = dict()
for i in sensors:
self.sensors[i] = list()
for j in sensors[i]:
self.sensors[i].append({j.label: j.current})
if os.path.exists("./config.txt"):
config_file = open("./config.txt", 'r')
content = config_file.read()
devices = content.split(',')
else:
devices = ['/']
self.devices = []
for i in devices:
self.devices.append({'name': i.strip(), 'space': psutil.disk_usage(i.strip()).percent})
def to_json(self):
return json.dumps(self, default=lambda o: o.__dict__, sort_keys=False, indent=4)
@app.after_request
def apply_caching(response):
response.headers['Content-Type'] = 'application/json'
return response
@app.route('/')
def get():
p1 = SystemStats()
return p1.to_json()
if __name__ == '__main__':
app.run(host='0.0.0.0', port=4000)