Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 import rosnode
00029 import rospy
00030 import xmlrpclib
00031 import psutil
00032
00033 ID = '/NODEINFO'
00034
00035 class NodeInfo(object):
00036 nodes = dict()
00037 def get_node_info(self, node_name):
00038 node_api = rosnode.get_api_uri(rospy.get_master(), node_name)
00039 try:
00040 code, msg, pid = xmlrpclib.ServerProxy(node_api[2]).getPid(ID)
00041 if node_name in self.nodes:
00042 return self.nodes[node_name]
00043 else:
00044 try:
00045 p = psutil.Process(pid)
00046 self.nodes[node_name] = p
00047 return p
00048 except:
00049 return False
00050 except xmlrpclib.socket.error:
00051 return False
00052
00053
00054 def get_all_node_info(self):
00055 infos = []
00056 for node_name in rosnode.get_node_names():
00057 info = self.get_node_info(node_name)
00058 if info is not False: infos.append((node_name, info))
00059 return infos
00060
00061 def get_all_node_fields(self, fields):
00062 processes = self.get_all_node_info()
00063 infos = []
00064 for name, p in processes:
00065 infos.append(self.as_dict(p, fields + ['cmdline', 'get_memory_info']))
00066 infos[-1]['node_name'] = name
00067 return infos
00068
00069 def kill_node(self, node_name):
00070 success, fail = rosnode.kill_nodes([node_name])
00071 return node_name in success
00072
00073 def as_dict(self, p, attrs=[], ad_value=None):
00074
00075 excluded_names = set(['send_signal', 'suspend', 'resume', 'terminate',
00076 'kill', 'wait', 'is_running', 'as_dict', 'parent',
00077 'get_children', 'nice'])
00078 retdict = dict()
00079 for name in set(attrs or dir(p)):
00080 if name.startswith('_'):
00081 continue
00082 if name.startswith('set_'):
00083 continue
00084 if name in excluded_names:
00085 continue
00086 try:
00087 attr = getattr(p, name)
00088 if callable(attr):
00089 if name == 'get_cpu_percent':
00090 ret = attr(interval=0)
00091 else:
00092 ret = attr()
00093 else:
00094 ret = attr
00095 except AccessDenied:
00096 ret = ad_value
00097 except NotImplementedError:
00098
00099
00100
00101 if attrs:
00102 raise
00103 continue
00104 if name.startswith('get'):
00105 if name[3] == '_':
00106 name = name[4:]
00107 elif name == 'getcwd':
00108 name = 'cwd'
00109 retdict[name] = ret
00110 return retdict