Commit a4cdc6bc authored by Taddeüs Kroes's avatar Taddeüs Kroes

Added first version: working server and basic framework files

parents
*.css
*.js
ALL := style.css scripts.js
.PHONY: all
all: $(ALL)
%.css: %.sass
sass --no-cache $< | yui-compressor --type css --output $@
%.js: %.coffee
coffee --compile --output $(@D) $<
scripts.js: json2.js monitor.js
cat $^ | yui-compressor --type js --output $@
clean:
rm -f $(ALL) $(patsubst %.coffee,%.js,$(wildcard *.coffee))
<!doctype html>
<html>
<head>
<title>PcTaddeus monitor</title>
<link href="style.css" type="text/css" rel="stylesheet">
</head>
<body>
<script src="scripts.js" type="text/javascript"></script>
</body>
</html>
This diff is collapsed.
ws = new WebSocket('ws://localhost:12345')
#ws = new WebSocket('ws://80.56.96.111:12345')
ws.onopen = -> console.log 'open'
ws.onclose = -> console.log 'close'
ws.onerror = (e) -> console.log 'error', e
ws.onmessage = (msg) -> console.log 'msg', msg.data
#ws.onmessage = (msg) -> console.log 'msg', JSON.parse(msg.data)
#!/usr/bin/env python
import time
import socket
import json
import subprocess
import re
from threading import Thread
from twspy import websocket, TextMessage
def status_message():
with open('/proc/uptime', 'r') as f:
uptime, idletime = map(float, f.read().split(' '))
temps = []
for line in subprocess.check_output('sensors').split('\n'):
m = re.match(r'^Core \d+:\s*\+(\d+\.\d+)', line)
if m:
temps.append(float(m.group(1)))
cpu_idle = float(subprocess.check_output('mpstat').rsplit(' ', 1)[-1])
data = {
'uptime': uptime,
'temps': temps,
'cpu_usage': max(round(100 - cpu_idle, 2), 0)
}
return TextMessage(json.dumps(data))
if __name__ == '__main__':
server = websocket()
server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server.bind(('', 12345))
server.listen(5)
clients = []
def connect():
while True:
sock, address = server.accept()
print 'Client connected at %s:%d' % address
clients.append(sock)
t = Thread(target=connect)
t.daemon = True
t.start()
try:
while True:
if len(clients):
status = status_message()
for client in list(clients):
try:
client.send(status.frame())
except socket.error:
print 'Client disconnected'
clients.remove(client)
else:
time.sleep(5)
time.sleep(1)
except KeyboardInterrupt:
print 'Stopping server'
server.close()
body
background-color: #f1f1f1
#!/usr/bin/env sh
hash inotifywait || (echo "Install inotify-tools first"; exit 1)
make
while true; do
inotifywait --quiet --exclude \.swp\$ --event moved_to,attrib,modify \
. *.sass *.coffee
sleep 0.05s
make
done
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment