forked from minimaxir/system-dashboard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflask_system.py
68 lines (55 loc) · 1.8 KB
/
flask_system.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
import json
import logging
import os
import time
import psutil
from flask import Flask
from flask_cors import CORS, cross_origin
app = Flask(__name__)
CORS(app) # Necessary since API is running locally
# Should match the period (in seconds) in Freeboard
period = 1
# Disable Flask console messages: http://stackoverflow.com/a/18379764
log = logging.getLogger('werkzeug')
log.setLevel(logging.ERROR)
@cross_origin()
@app.route('/')
def index():
disk_write_data_start = psutil.disk_io_counters(perdisk=False)
io_data_start = psutil.net_io_counters()
# Some metrics are only reported in values since uptime,
# so sample over a period (in seconds) to get rate.
time.sleep(period)
cpu_data = psutil.cpu_percent(interval=None)
ram_data = psutil.virtual_memory()
disk_data = psutil.disk_usage('/')
disk_write_data = psutil.disk_io_counters(perdisk=False)
io_data = psutil.net_io_counters()
data = {
'cpu': {
'percent': cpu_data
},
'ram': {
'percent': ram_data[2],
'total': ram_data[0],
'used': ram_data[3]
},
'disk': {
'total': disk_data[0],
'used': disk_data[1],
'percent': disk_data[3],
'read_bytes_sec': (disk_write_data[2] - disk_write_data_start[2])
/ period,
'write_bytes_sec': (disk_write_data[3] - disk_write_data_start[3])
/ period
},
'io': {
'sent_bytes_sec': (io_data[0] - io_data_start[0]) / period,
'received_bytes_sec': (io_data[1] - io_data_start[1]) / period
}
}
return json.dumps(data)
if __name__ == "__main__":
print("System API up at http://localhost:5002")
PORT = int(os.getenv('PORT', 5002))
app.run(port=PORT, threaded=True)