Go to the documentation of this file.00001
00002 import re
00003 import rospy
00004 from std_msgs.msg import Float32
00005 from collections import deque
00006 import os
00007
00008 def rosSafeName(name):
00009 return name.lower().replace("-", "_")
00010
00011 class NetworkStatus():
00012 def __init__(self):
00013 rospy.init_node('network')
00014 self.match_face = re.compile('(.+):(.*)')
00015 self.hz = rospy.get_param('~hz', 10)
00016 rospy.logdebug('publish network status (bps) at ' + str(self.hz) + 'Hz')
00017 rospy.logdebug('usage:\n$rosrun jsk_network_tools network_status.py _hz:=[Hz]')
00018 self.init_publisher()
00019 rospy.Timer(rospy.Duration(1.0/self.hz), self.publish)
00020 rospy.spin()
00021 def init_publisher(self):
00022 faces = self.read_net_file()
00023 self.faces_map = {}
00024 self.non_local_transmit_pub = rospy.Publisher(rosSafeName(os.uname()[1]) + "/nonlocal" + '/transmit', Float32)
00025 self.non_local_receive_pub = rospy.Publisher(rosSafeName(os.uname()[1]) + "/nonlocal" + '/receive', Float32)
00026 self.non_local_transmit_kbps_pub = rospy.Publisher(rosSafeName(os.uname()[1]) + "/nonlocal" + '/transmit_kbps', Float32)
00027 self.non_local_receive_kbps_pub = rospy.Publisher(rosSafeName(os.uname()[1]) + "/nonlocal" + '/receive_kbps', Float32)
00028 self.non_local_transmit_mbps_pub = rospy.Publisher(rosSafeName(os.uname()[1]) + "/nonlocal" + '/transmit_mbps', Float32)
00029 self.non_local_receive_mbps_pub = rospy.Publisher(rosSafeName(os.uname()[1]) + "/nonlocal" + '/receive_mbps', Float32)
00030 for face in faces:
00031 pub_transmit = rospy.Publisher(rosSafeName(os.uname()[1]) + "/" + face[0] + '/transmit', Float32)
00032 pub_receive = rospy.Publisher(rosSafeName(os.uname()[1]) + "/" + face[0] + '/receive', Float32)
00033 pub_transmit_kbps = rospy.Publisher(rosSafeName(os.uname()[1]) + "/" + face[0] + '/transmit_kbps', Float32)
00034 pub_receive_kbps = rospy.Publisher(rosSafeName(os.uname()[1]) + "/" + face[0] + '/receive_kbps', Float32)
00035 pub_transmit_mbps = rospy.Publisher(rosSafeName(os.uname()[1]) + "/" + face[0] + '/transmit_mbps', Float32)
00036 pub_receive_mbps = rospy.Publisher(rosSafeName(os.uname()[1]) + "/" + face[0] + '/receive_mbps', Float32)
00037 queue_transmit = deque()
00038 queue_reveice = deque()
00039
00040 for i in range(self.hz):
00041 queue_transmit.append(-1)
00042 queue_reveice.append(-1)
00043
00044 self.faces_map[face[0]] = {}
00045 self.faces_map[face[0]]["transmit"] = {"publisher": pub_transmit, "value": queue_transmit,
00046 "publisher_kbps": pub_transmit_kbps,
00047 "publisher_mbps": pub_transmit_mbps}
00048 self.faces_map[face[0]]["receive"] = {"publisher": pub_receive, "value": queue_reveice,
00049 "publisher_kbps": pub_receive_kbps,
00050 "publisher_mbps": pub_receive_mbps}
00051
00052 def publish(self, event):
00053 faces = self.read_net_file()
00054 non_local_transmit = 0
00055 non_local_receive = 0
00056 for face in faces:
00057 name = face[0]
00058 if name in self.faces_map:
00059 self.faces_map[name]["transmit"]["value"].append(int(face[1]))
00060 self.faces_map[name]["receive"]["value"].append(int(face[2]))
00061 transmit_value = (int(face[1]) - self.faces_map[name]["transmit"]["value"].popleft()) * 8
00062 receive_value = (int(face[2]) - self.faces_map[name]["receive"]["value"].popleft()) * 8
00063 self.faces_map[name]["transmit"]["publisher"].publish(Float32(transmit_value))
00064 self.faces_map[name]["receive"]["publisher"].publish(Float32(receive_value))
00065 self.faces_map[name]["transmit"]["publisher_kbps"].publish(Float32(transmit_value / 1024))
00066 self.faces_map[name]["receive"]["publisher_kbps"].publish(Float32(receive_value / 1024))
00067 self.faces_map[name]["transmit"]["publisher_mbps"].publish(Float32(transmit_value / 1024 / 1024))
00068 self.faces_map[name]["receive"]["publisher_mbps"].publish(Float32(receive_value / 1024 / 1024))
00069 if name != "lo":
00070 non_local_receive = receive_value + non_local_receive
00071 non_local_transmit = transmit_value + non_local_transmit
00072 self.non_local_transmit_pub.publish(Float32(non_local_transmit))
00073 self.non_local_receive_pub.publish(Float32(non_local_receive))
00074 self.non_local_transmit_kbps_pub.publish(Float32(non_local_transmit / 1024))
00075 self.non_local_receive_kbps_pub.publish(Float32(non_local_receive / 1024))
00076 self.non_local_transmit_mbps_pub.publish(Float32(non_local_transmit / 1024 / 1024))
00077 self.non_local_receive_mbps_pub.publish(Float32(non_local_receive / 1024 / 1024))
00078 def read_net_file(self):
00079 with open("/proc/net/dev") as f:
00080 status = f.readlines()
00081 ret = []
00082 for s in status:
00083 match = self.match_face.match(s)
00084 if match:
00085 nums = match.group(2).split()
00086 ret.append([match.group(1).strip(), nums[8], nums[0]])
00087 return ret
00088
00089 if __name__ == '__main__':
00090 NetworkStatus()