-
Notifications
You must be signed in to change notification settings - Fork 1
/
sys-state.py
90 lines (65 loc) · 1.7 KB
/
sys-state.py
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
90
#!/usr/bin/env python2
# encoding: utf-8
"""
Display the system usage.
"""
from __future__ import division, print_function, unicode_literals
__author__ = "Parantapa Bhattachara <pb [at] parantapa [dot] net>"
import time
from datetime import datetime
import psutil
from pbapps_common import do_main, COLORS, ICONS
MODULE = "sys-state"
SYMB_MEMORY = ICONS.fa_memory
SYMB_CPU = ICONS.fa_cpu
def get_date():
"""
Get date.
"""
now = datetime.now()
now = now.strftime("%a, %b %d %Y %I:%M:%S %p")
return [{
"name": "date",
"instance": "date",
"full_text": now
}]
def get_cpuusage():
"""
Get the cpu usage.
"""
usages = psutil.cpu_percent(None, True)
avg_usage = sum(usages) / len(usages)
color = COLORS.red if avg_usage >= 80 else COLORS.green
usages = [min(99.0, u) for u in usages]
usages = ["{:2.0f}%".format(u) for u in usages]
usages = " ".join(usages)
return [{
"name": "cpu_usage",
"instance": 0,
"full_text": "{} {}".format(SYMB_CPU, usages),
"color": color
}]
def get_memusage():
"""
Get the memory usage.
"""
mem = psutil.virtual_memory()
usage = min(99.0, mem.percent)
return [{
"name": "mem_usage",
"instance": 0,
"full_text": "{} {:2.0f}%".format(SYMB_MEMORY, usage),
"color": COLORS.green if usage < 80 else COLORS.red
}]
def blocks_iter():
while True:
blocks = []
blocks.extend(get_memusage())
blocks.extend(get_cpuusage())
blocks.extend(get_date())
yield blocks
time.sleep(1)
def main():
do_main(MODULE, 90, blocks_iter())
if __name__ == '__main__':
main()