-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathps.py
More file actions
90 lines (70 loc) · 2.78 KB
/
ps.py
File metadata and controls
90 lines (70 loc) · 2.78 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import re
from pygmi import Match, monitor, events, call, _
from plugins.dialog import check_dialog, dialog
_RE_MEMINFO = re.compile(r'^([^:]+):\s*(\d+)')
class Cpu(object):
def __init__(self, name='cpu', colors=None,
interval=2.0):
self.last_cpu_sum = 0
self.last_cpu_idle = 0
monitor.defmonitor(self._monitor_action,
name = name,
colors = colors,
interval = interval)
def click_event(button):
real_name = monitor.monitors[name].button.real_name
return Match('RightBarClick', button, real_name)
events.bind({
click_event(_): lambda *a: self._toggle_dialog(name)
})
def _monitor_action(self, monitor):
return 'C: %2d%%' % self._get_cpu_percent()
def _toggle_dialog(self, name):
if check_dialog(name):
return
dialog(call('top', '-b', '-i', '-n', '1'), name)
def _get_cpu_percent(self):
with open('/proc/stat') as f:
cpu_stat = [long(n) for n in f.readline()[3:].split()]
cpu_sum = sum(cpu_stat)
idle = cpu_stat[3]
idle_diff = idle - self.last_cpu_idle
idle_check = (0, idle_diff)
total = cpu_sum - self.last_cpu_sum - idle_check[idle_diff < 0]
self.last_cpu_sum = cpu_sum
self.last_cpu_idle = idle
return 100 - idle_check[idle_diff > 0] * 100 / total
# Real memory usage in Linux (include tmpfs)
# http://calimeroteknik.free.fr/blag/?article20/really-used-memory-on-gnu-linux
class Memory(object):
def __init__(self, name='memory', colors=None,
interval=6.0):
monitor.defmonitor(self._monitor_action,
name = name,
colors = colors,
interval = interval)
def click_event(button):
real_name = monitor.monitors[name].button.real_name
return Match('RightBarClick', button, real_name)
events.bind({
click_event(_): lambda *a: self._toggle_dialog(name)
})
def _monitor_action(self, monitor):
return 'M: %2d%%' % self._get_used_percent()
def _toggle_dialog(self, name):
if check_dialog(name):
return
free = call('free', '-h')
df = call('df', '-h', '--type=tmpfs')
dialog('%s\n\n%s' % (free, df), name)
def _get_used_percent(self):
def _extract(s):
m = _RE_MEMINFO.search(s)
return m.group(1), int(m.group(2))
with open('/proc/meminfo') as f:
meminfo = dict(map(_extract, f.readlines()))
return round(100 - (meminfo['MemFree'] +
meminfo['Buffers'] +
meminfo['SReclaimable'] +
meminfo['Cached'] -
meminfo['Shmem']) / float(meminfo['MemTotal']) * 100)