network_status.py
Go to the documentation of this file.
1 #!/usr/bin/python
2 import re
3 import rospy
4 from std_msgs.msg import Float32
5 from collections import deque
6 import os
7 
8 def rosSafeName(name):
9  return name.lower().replace("-", "_")
10 
11 class NetworkStatus():
12  def __init__(self):
13  rospy.init_node('network')
14  self.match_face = re.compile('(.+):(.*)')
15  self.hz = rospy.get_param('~hz', 10)
16  rospy.logdebug('publish network status (bps) at ' + str(self.hz) + 'Hz')
17  rospy.logdebug('usage:\n$rosrun jsk_network_tools network_status.py _hz:=[Hz]')
18  self.init_publisher()
19  rospy.Timer(rospy.Duration(1.0/self.hz), self.publish)
20  rospy.spin()
21  def init_publisher(self):
22  faces = self.read_net_file()
23  self.faces_map = {}
24  self.non_local_transmit_pub = rospy.Publisher(rosSafeName(os.uname()[1]) + "/nonlocal" + '/transmit', Float32)
25  self.non_local_receive_pub = rospy.Publisher(rosSafeName(os.uname()[1]) + "/nonlocal" + '/receive', Float32)
26  self.non_local_transmit_kbps_pub = rospy.Publisher(rosSafeName(os.uname()[1]) + "/nonlocal" + '/transmit_kbps', Float32)
27  self.non_local_receive_kbps_pub = rospy.Publisher(rosSafeName(os.uname()[1]) + "/nonlocal" + '/receive_kbps', Float32)
28  self.non_local_transmit_mbps_pub = rospy.Publisher(rosSafeName(os.uname()[1]) + "/nonlocal" + '/transmit_mbps', Float32)
29  self.non_local_receive_mbps_pub = rospy.Publisher(rosSafeName(os.uname()[1]) + "/nonlocal" + '/receive_mbps', Float32)
30  for face in faces:
31  pub_transmit = rospy.Publisher(rosSafeName(os.uname()[1]) + "/" + face[0] + '/transmit', Float32)
32  pub_receive = rospy.Publisher(rosSafeName(os.uname()[1]) + "/" + face[0] + '/receive', Float32)
33  pub_transmit_kbps = rospy.Publisher(rosSafeName(os.uname()[1]) + "/" + face[0] + '/transmit_kbps', Float32)
34  pub_receive_kbps = rospy.Publisher(rosSafeName(os.uname()[1]) + "/" + face[0] + '/receive_kbps', Float32)
35  pub_transmit_mbps = rospy.Publisher(rosSafeName(os.uname()[1]) + "/" + face[0] + '/transmit_mbps', Float32)
36  pub_receive_mbps = rospy.Publisher(rosSafeName(os.uname()[1]) + "/" + face[0] + '/receive_mbps', Float32)
37  queue_transmit = deque()
38  queue_reveice = deque()
39 
40  for i in range(self.hz):
41  queue_transmit.append(-1)
42  queue_reveice.append(-1)
43 
44  self.faces_map[face[0]] = {}
45  self.faces_map[face[0]]["transmit"] = {"publisher": pub_transmit, "value": queue_transmit,
46  "publisher_kbps": pub_transmit_kbps,
47  "publisher_mbps": pub_transmit_mbps}
48  self.faces_map[face[0]]["receive"] = {"publisher": pub_receive, "value": queue_reveice,
49  "publisher_kbps": pub_receive_kbps,
50  "publisher_mbps": pub_receive_mbps}
51 
52  def publish(self, event):
53  faces = self.read_net_file()
54  non_local_transmit = 0
55  non_local_receive = 0
56  for face in faces:
57  name = face[0]
58  if name in self.faces_map:
59  self.faces_map[name]["transmit"]["value"].append(int(face[1]))
60  self.faces_map[name]["receive"]["value"].append(int(face[2]))
61  transmit_value = (int(face[1]) - self.faces_map[name]["transmit"]["value"].popleft()) * 8
62  receive_value = (int(face[2]) - self.faces_map[name]["receive"]["value"].popleft()) * 8
63  self.faces_map[name]["transmit"]["publisher"].publish(Float32(transmit_value))
64  self.faces_map[name]["receive"]["publisher"].publish(Float32(receive_value))
65  self.faces_map[name]["transmit"]["publisher_kbps"].publish(Float32(transmit_value / 1024))
66  self.faces_map[name]["receive"]["publisher_kbps"].publish(Float32(receive_value / 1024))
67  self.faces_map[name]["transmit"]["publisher_mbps"].publish(Float32(transmit_value / 1024 / 1024))
68  self.faces_map[name]["receive"]["publisher_mbps"].publish(Float32(receive_value / 1024 / 1024))
69  if name != "lo":
70  non_local_receive = receive_value + non_local_receive
71  non_local_transmit = transmit_value + non_local_transmit
72  self.non_local_transmit_pub.publish(Float32(non_local_transmit))
73  self.non_local_receive_pub.publish(Float32(non_local_receive))
74  self.non_local_transmit_kbps_pub.publish(Float32(non_local_transmit / 1024))
75  self.non_local_receive_kbps_pub.publish(Float32(non_local_receive / 1024))
76  self.non_local_transmit_mbps_pub.publish(Float32(non_local_transmit / 1024 / 1024))
77  self.non_local_receive_mbps_pub.publish(Float32(non_local_receive / 1024 / 1024))
78  def read_net_file(self):
79  with open("/proc/net/dev") as f:
80  status = f.readlines()
81  ret = []
82  for s in status:
83  match = self.match_face.match(s)
84  if match:
85  nums = match.group(2).split()
86  ret.append([match.group(1).strip(), nums[8], nums[0]])
87  return ret
88 
89 if __name__ == '__main__':
def rosSafeName(name)


jsk_network_tools
Author(s): Yusuke Furuta
autogenerated on Tue Feb 6 2018 03:45:07