-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrosmonitor.py
143 lines (119 loc) · 3.71 KB
/
rosmonitor.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
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
#!/usr/bin/env python
import re
import subprocess
import psutil
def run_command(command):
out = subprocess.Popen(command,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
stdout, _ = out.communicate()
return stdout
def get_nodes():
stdout = run_command(['rosnode', 'list'])
nodes = stdout[:-1].split('\n')
return nodes
def get_node_pid(node_name):
stdout = run_command(['rosnode', 'info', node_name])
r = re.findall(r'Pid: \d*', stdout)
items = []
for item in r:
pid = [int(s) for s in item.split() if s.isdigit()][0]
items.append(pid)
return items
def get_process_performance(pid):
stdout = run_command(
['ps', '-p', str(pid), '-o', '%cpu,%mem'])[:-1].split('\n')
items = [float(s) for s in stdout[-1].strip().split(" ") if s != ""]
return items
def bytes2human(n):
symbols = ('K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y')
prefix = {}
for i, s in enumerate(symbols):
prefix[s] = 1 << (i + 1) * 10
for s in reversed(symbols):
if n >= prefix[s]:
value = float(n) / prefix[s]
return '%.1f%s' % (value, s)
return "%sB" % n
def get_ports_by_pid(pid):
stdout = run_command(
['lsof', '-Pa', '-p', str(pid), '-i'])[:-1].split('\n')
connections = []
for line in stdout[1:]:
parts = line.split(' ')
parts = [part for part in parts if len(part) > 1]
parts = parts[7:]
proto = parts[0]
connection = parts[1]
connections.append([proto, connection])
return connections
def get_process_file(pid):
stdout = run_command(
['ps', '-p', str(pid), '-o', 'cmd'])[:-1].split('\n')[1]
return stdout
def get_rosmaster_pid():
stdout = run_command(['pgrep', '-f', 'rosmaster'])[:-1].split('\n')
return int(stdout[0])
def generate_ros_table():
table = []
processes = [['/rosmaster', [get_rosmaster_pid()]]]
processes.extend([[node, get_node_pid(node)] for node in get_nodes()])
sum_cpu = 0
sum_mem = 0
max_pid_len = 0
max_name_len = 0
for name, pid in processes:
performances = get_process_performance(pid[0])
connections = get_ports_by_pid(pid[0])
connections = [connection[1]
for connection in connections if connection[0] == 'UDP']
connections = " ".join(connections)
max_pid_len = max(max_pid_len, len(str(pid[0])))
max_name_len = max(max_name_len, len(name))
table.append([
name,
pid[0],
str(performances[0]) + "%",
str(performances[1]) + "%",
connections,
])
sum_cpu += performances[0]
sum_mem += performances[1]
table.append([
"-" * max_name_len,
"-" * max_pid_len,
str(sum_cpu) + "%",
str(sum_mem) + "%",
"",
])
return table
def main():
from tabulate import tabulate
import os
while True:
try:
table = generate_ros_table()
os.system('clear')
table_items = table[:-1]
table_items.sort(key=lambda x: x[2], reverse=True)
table[:-1] = table_items
print tabulate(table, headers=(
'name',
'pid',
'cpu',
'mem',
'UDP connections',
))
print 'press ctrl+c to exit'
except KeyboardInterrupt:
break
except ValueError as e:
print '[ERR] ' + e
continue
if __name__ == "__main__":
try:
get_rosmaster_pid()
except ValueError:
print '[ERR] Cannot detect rosmaster in your ROS system.'
exit(1)
main()