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