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

Now using psutil for sys info (cross-platform)

parent b2feaa96
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
<body> <body>
<div class="header"> <div class="header">
<div class="center"> <div class="center">
<i class="icon-gear"></i><span id="release">-</span> <i class="icon-gear"></i><span id="osname">-</span>
<span id="status" class="right"></span> <span id="status" class="right"></span>
</div> </div>
</div> </div>
......
el = (id) -> document.getElementById(id) el = (id) -> document.getElementById(id)
set = (id, value) -> el(id).innerHTML = value set = (id, value) -> el(id).innerHTML = value
values = (e for e in el('content').getElementsByTagName('span')) \ values = (e for e in el('content').getElementsByTagName('span')) \
.concat(el('release')) .concat(el('osname'))
connect = -> connect = ->
val.innerHTML = 'Connecting...' for val in values val.innerHTML = 'Connecting...' for val in values
...@@ -26,19 +26,17 @@ connect = -> ...@@ -26,19 +26,17 @@ connect = ->
console.log 'msg', msg.data console.log 'msg', msg.data
data = JSON.parse(msg.data) data = JSON.parse(msg.data)
set('release', data.release) if data.release set('osname', data.osname) if data.osname
set('uptime', fmt_time(data.uptime)) if data.uptime set('uptime', fmt_time(data.uptime)) if data.uptime
set('cpu-usage', "#{data.cpu_usage}%")
set('memory', fmt_kbytes_usage(data.memory))
set('disk', fmt_kbytes_usage(data.disk))
if data.temps.length if data.temps
el('temp').innerHTML = ("#{deg}&#8451;" for deg in data.temps) \ el('temp').innerHTML = ("#{deg}&#8451;" for deg in data.temps) \
.join('&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;') .join('&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;')
else
set('cpu-usage', "#{data.cpu_usage}%") if data.cpu_usage set('temp', '-')
set('memory', fmt_kbytes_usage(data.memory)) if data.memory
set('disk', fmt_kbytes_usage(data.disk)) if data.disk
fmt_time = (total) -> fmt_time = (total) ->
total = Math.round total total = Math.round total
...@@ -58,8 +56,8 @@ fmt_time = (total) -> ...@@ -58,8 +56,8 @@ fmt_time = (total) ->
str str
fmt_kbytes_usage = ([used, total]) -> fmt_kbytes_usage = ([used, total]) ->
used = Math.round used / 1000 used = Math.round used / 1e6
total = Math.round total / 1000 total = Math.round total / 1e6
perc = Math.round used / total * 100 perc = Math.round used / total * 100
return "#{used}MB / #{total}MB (#{perc}%)" return "#{used}MB / #{total}MB (#{perc}%)"
......
...@@ -3,20 +3,27 @@ import time ...@@ -3,20 +3,27 @@ import time
import socket import socket
import json import json
import re import re
import psutil
import platform
from subprocess import check_output from subprocess import check_output
from threading import Thread from threading import Thread
from twspy import websocket, Frame, OPCODE_TEXT, WebkitDeflateFrame from twspy import websocket, Frame, OPCODE_TEXT, WebkitDeflateFrame
def osname():
if platform.system() == 'Linux':
return 'Linux %s %s (%s)' % platform.dist()
#return '%s %s' % (platform.system(), platform.release())
return platform.platform()
def stats(): def stats():
# Release # OS identification
dist, codename = check_output(['lsb_release', '-sdc']).rstrip().split('\n') yield 'osname', osname()
yield 'release', '%s (%s)' % (dist, codename)
# Uptime # Uptime
with open('/proc/uptime', 'r') as f: yield 'uptime', time.time() - psutil.get_boot_time()
uptime, idletime = map(float, f.read().split(' '))
yield 'uptime', uptime
# CPU temperature # CPU temperature
try: try:
...@@ -28,39 +35,23 @@ def stats(): ...@@ -28,39 +35,23 @@ def stats():
if m: if m:
temps.append(float(m.group(1))) temps.append(float(m.group(1)))
assert len(temps) == psutil.NUM_CPUS
yield 'temps', temps yield 'temps', temps
except: except:
pass pass
# CPU usage # CPU usage
with open('/proc/stat', 'r') as f: cpu = psutil.cpu_times()
line = f.readlines()[0].rstrip().split() total = sum(cpu)
assert line[0] == 'cpu' yield 'cpu_usage', round(float(total - cpu.idle) / total * 100, 2)
numbers = map(int, line[1:])
total = sum(numbers)
idle = numbers[3]
yield 'cpu_usage', round(float(total - idle) / total * 100, 2)
# Memory usage # Memory usage
with open('/proc/meminfo', 'r') as f: mem = psutil.phymem_usage()
for line in f: yield 'memory', (mem.used, mem.total)
if line.startswith('MemTotal'):
assert line.endswith('kB\n')
total = int(line.split()[1])
elif line.startswith('MemFree'):
assert line.endswith('kB\n')
used = total - int(line.split()[1])
yield 'memory', (used, total)
break
# Disk usage # Disk usage
for line in check_output('df').split('\n'): disk = psutil.disk_usage('/')
parts = line.split() yield 'disk', (disk.used, disk.total)
if parts[0].startswith('/dev/') and parts[5] == '/':
used, avail = map(int, parts[2:4])
yield 'disk', (used, used + avail)
break
if __name__ == '__main__': if __name__ == '__main__':
......
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