-
Notifications
You must be signed in to change notification settings - Fork 26
/
lookout.py
executable file
·58 lines (50 loc) · 1.46 KB
/
lookout.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
#!/usr/bin/env python
PORT = 9890
import flask, os, sys, time, psutil, json, socket
from tornado.wsgi import WSGIContainer
from tornado.httpserver import HTTPServer
from tornado.ioloop import IOLoop
from multiprocessing import Process, Manager
if getattr(sys, 'frozen', None):
static = os.path.join(sys._MEIPASS, 'static')
else:
static = 'static'
app = flask.Flask(__name__, static_folder=static)
@app.route('/')
def frontend():
return flask.send_from_directory(app.static_folder, 'index.html')
@app.route('/raw')
def raw():
return flask.Response(
json.dumps(stats.copy()),
mimetype='application/json'
)
def update(stats):
while True:
diskused = 0
disktotal = 0
for i in psutil.disk_partitions():
try:
x = psutil.disk_usage(i.mountpoint)
diskused += x.used
disktotal += x.total
except OSError:
pass
stats['uptime'] = time.time() - psutil.boot_time()
stats['fqdn'] = socket.gethostname()
stats['cpuusage'] = psutil.cpu_percent(0)
stats['ramusage'] = psutil.virtual_memory()
stats['diskio'] = psutil.disk_io_counters()
stats['diskusage'] = [diskused, disktotal]
stats['netio'] = psutil.net_io_counters()
stats['swapusage'] = psutil.swap_memory()
time.sleep(1)
if __name__ == '__main__':
stats = Manager().dict()
Process(target=update, args=(stats, )).start()
if len(sys.argv) > 1:
PORT = sys.argv[1]
server = HTTPServer(WSGIContainer(app))
print 'Now listening on port ' + str(PORT)
server.listen(PORT)
IOLoop.instance().start()