bin env python coding utf-8 from commands import getoutput import os f

 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
#!/bin/env python
# -*- coding: utf-8 -*-
from commands import getoutput
import os
from re import search
from time import sleep
import socket
_widgetName = "systeminfo"
_awesome_client_path = "%s/bin/awesome-client" % os.environ.get('HOME')
_spaces = ' '*4
def get_system_status():
global b1, b2, b3
# cpu %
try: cpup = getoutput("ps aux | awk 'NR > 0 { s +=$3 }; END {print s}'") + '%'
except: cpup = 'err'
# cpu temp
try: cput = getoutput("sensors").split('Temp3: ')[1].split('°C (low')[0]
except: cput = 'err'
# hddtemp
try:
s = socket.socket()
s.connect(('', 7634))
hddt = '+%s' % search(r'.*?\/dev\/(h|s)d.*?\|.*?\|(.*)\|.*\|', s.recv(1024)).group(2)
s.close()
except:
hddt = 'err'
# mem
try: mem = '%sMb' % search(r'.*cache:\s*([0-9]*)\s*', getoutput("free -m")).group(1)
except: mem = 'err'
try: # swap
f = open('/proc/meminfo', 'r')
swap_usage = search(r'.*\nSwapTotal:\s*(.*)\skB\nSwapFree:\s*(.*)\skB', f.read())
f.close()
swap = '%dMb' % ((long(swap_usage.group(1))-long(swap_usage.group(2))) / 1024L)
except: swap = 'err'
return '[ %s | %s | %s | %s | %s ]' % (cpup, cput, hddt, mem, swap)
if __name__ == "__main__":
while 1:
pipe = os.popen( _awesome_client_path, 'w')
pipe.write( "0 widget_tell %s %s%s\n" % (_widgetName, _spaces, get_system_status()) )
pipe.close()
sleep( 3 )