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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
|
import collections
import psutil
from functools import wraps
def safe(name):
def decorator_safe(func):
"""
Decorator for try-catch on string assembly
"""
@wraps(func)
def wrap_safe(*args, **kwargs):
try:
return func(*args, **kwargs)
except Exception as e:
return "Failed to get {}: {}".format(name, e)
return wrap_safe
return decorator_safe
def get_proc_attrib(attr):
if callable(attr):
try:
return attr()
except psutil.Error:
return None
else:
return attr
@safe("cpu/mem info")
def _cpu_mem_str():
vm = psutil.virtual_memory()
cpu_tp = psutil.cpu_times_percent(0.1)
str_items = []
str_items.append(
"CPU: Idle: {}% User: {}% System: {}% IOwait: {}%\n".format(
cpu_tp.idle, cpu_tp.user, cpu_tp.system, cpu_tp.iowait
)
)
str_items.append(
"MEM: total {} Gb available: {} Gb used: {} Gb free: {} Gb active: {} Gb inactive: {} Gb shared: {} Gb\n".format(
round(vm.total / 1e9, 2),
round(vm.available / 1e9, 2),
round(vm.used / 1e9, 2),
round(vm.free / 1e9, 2),
round(vm.active / 1e9, 2),
round(vm.inactive / 1e9, 2),
round(vm.shared / 1e9, 2),
)
)
str_items.append("Used swap: {}%\n".format(psutil.swap_memory().percent))
return "".join(str_items)
@safe("processes tree")
def _proc_tree_str():
tree = collections.defaultdict(list)
for p in psutil.process_iter():
try:
tree[p.ppid()].append(p.pid)
except (psutil.NoSuchProcess, psutil.ZombieProcess):
pass
# on systems supporting PID 0, PID 0's parent is usually 0
if 0 in tree and 0 in tree[0]:
tree[0].remove(0)
return _print_proc_tree(min(tree), tree)
def _print_proc_tree(parent_root, tree, indent_root=''):
stack = [(parent_root, indent_root, "")]
str_items = list()
while len(stack) > 0:
try:
parent, indent, prefix = stack.pop()
p = psutil.Process(parent)
name = get_proc_attrib(p.name)
str_items.append("{}({}, '{}'".format(prefix, parent, name if name else '?'))
exe = get_proc_attrib(p.exe)
if exe:
str_items.append(" [{}]".format(exe))
str_items.append(") ")
str_items.append(" st: {}".format(p.status()))
str_items.append(" mem: {}%".format(round(p.memory_percent(), 2)))
ndfs = get_proc_attrib(p.num_fds)
if ndfs and ndfs > 0:
str_items.append(" fds: {}".format(ndfs))
conns = get_proc_attrib(p.connections)
if conns and len(conns) > 1:
str_items.append(" num con: {}".format(len(conns)))
ths = get_proc_attrib(p.num_threads)
if ths and ths > 1:
str_items.append(" threads: {}".format(ths))
str_items.append("\n")
except psutil.Error:
name = "?"
str_items.append("({}, '{}')\n".format(parent, name))
if parent not in tree:
continue
child = tree[parent][-1]
stack.append((child, indent + " ", indent + "`_ "))
children = tree[parent][:-1]
children.reverse()
for child in children:
stack.append((child, indent + "| ", indent + "|- "))
return "".join(str_items)
@safe("network info")
def _network_conn_str():
str_items = list()
counters = psutil.net_io_counters()
str_items.append(
"\nPackSent: {} PackRecv: {} ErrIn: {} ErrOut: {} DropIn: {} DropOut: {}\n\n".format(
counters.packets_sent,
counters.packets_recv,
counters.errin,
counters.errout,
counters.dropin,
counters.dropout,
)
)
ifaces = psutil.net_if_addrs()
conns = psutil.net_connections()
list_ip = collections.defaultdict(list)
for con in conns:
list_ip[con.laddr.ip].append(con)
for name, addrs in ifaces.iteritems():
str_items.append("{}:\n".format(name))
for ip in addrs:
str_items.append(" {}".format(ip.address))
if ip.netmask:
str_items.append(" mask={}".format(ip.netmask))
if ip.broadcast:
str_items.append(" bc={}".format(ip.broadcast))
str_items.append("\n")
for con in list_ip[ip.address]:
str_items.append(" {}".format(con.laddr.port))
if con.raddr:
str_items.append(" <--> {} : {}".format(con.raddr.ip, con.raddr.port))
str_items.append(" (stat: {}".format(con.status))
if con.pid:
str_items.append(" proc: {} (pid={})".format(psutil.Process(con.pid).exe(), con.pid))
str_items.append(")\n")
del list_ip[ip.address]
str_items.append("***\n")
for ip, conns in list_ip.iteritems():
str_items.append(" {}\n".format(ip))
for con in conns:
str_items.append(" {}".format(con.laddr.port))
if con.raddr:
str_items.append(" <--> {} : {}".format(con.raddr.ip, con.raddr.port))
str_items.append(" (stat: {}".format(con.status))
if con.pid:
str_items.append(" proc: {} (pid={})".format(psutil.Process(con.pid).exe(), con.pid))
str_items.append(")\n")
return "".join(str_items)
@safe("info")
def get_system_info():
str_items = list()
str_items.append("\n --- CPU MEM --- \n")
str_items.append(_cpu_mem_str())
str_items.append("\n")
str_items.append("\n --- PROCESSES TREE --- \n")
str_items.append(_proc_tree_str())
str_items.append("\n")
str_items.append("\n --- NETWORK INFO --- \n")
str_items.append(_network_conn_str())
str_items.append("\n")
return "".join(str_items)
|