1 # vim: tabstop=4 shiftwidth=4 softtabstop=4
2 # -*- coding: utf-8 -*-
6 from linkpair.device import Device
7 from linkpair.port import Port
8 from linkpair.collect.agent.base_agent import BaseAgent
11 class LinuxNetAgent(BaseAgent):
13 Linux Network Collector Agent
16 self.get_configuration()
18 def get_configuration(self):
23 res_ipaddr = self._runner.exec_cmd("ip addr show")
24 for i in range(0, len(res_ipaddr)):
25 port_start = res_ipaddr[i].strip()
27 re.search(r'(\d+): (\S+): <(\S+)> mtu (\d+) (.*)',
28 port_start)) is not None:
29 match = self._u.d_pop()
30 port_meta["if_index"] = match.group(1).strip()
31 port_name = match.group(2).strip()
32 port_meta["if_state"] = match.group(3).strip()
33 port_meta["mtu"] = match.group(4).strip()
34 port_meta["if_other_state"] = match.group(5).strip()
35 interfaces.append(port_name)
37 self._cu.add_port(Port(port_name, Port.DEFAULT_TYPE, port_meta))
43 re.search(r'link/ether (\S+) brd (\S+)',
44 port_start)) is not None:
45 match = self._u.d_pop()
46 port_meta["mac_addr"] = match.group(1).strip()
47 port_meta["mac_bcast_addr"] = match.group(2).strip()
49 re.search(r'inet (\S+) brd (\S+) scope (\S+)',
50 port_start)) is not None:
51 match = self._u.d_pop()
52 port_meta["inet_addr"] = match.group(1).strip()
53 port_meta["inet_bcast_addr"] = match.group(2).strip()
54 port_meta["if_scope"] = match.group(3).strip()
56 for interface in interfaces:
57 # interface informations
58 res_ethtool = self._runner.exec_cmd("ethtool " + interface)
59 self._parse_ethtool_output(res_ethtool, port_meta)
61 # interface driver informations
62 res_ethtool = self._runner.exec_cmd("ethtool -i " + interface)
63 self._parse_ethtool_output(res_ethtool, port_meta)
65 # interface statistics
66 res_ethtool = self._runner.exec_cmd("ethtool -S " + interface)
67 self._parse_ethtool_output(res_ethtool, port_meta)
69 # interface offload settings
70 res_ethtool = self._runner.exec_cmd("ethtool -k " + interface)
71 self._parse_ethtool_output(res_ethtool, port_meta)
73 self._cu.add_port(Port(interface, Port.DEFAULT_TYPE, port_meta))
77 def _parse_ethtool_output(self, res_ethtool, port_meta):
80 multi_line_property = ""
83 for i in range(0, len(res_ethtool)):
84 ethtool_line = res_ethtool[i].rstrip()
86 re.search(r'(\S.*):\s+(\S.*)', ethtool_line)) is not None:
87 match = self._u.d_pop()
88 if_property = match.group(1).strip()
89 if re.search(r' link modes', if_property):
91 multi_line_property = if_property
92 multi_line_value = match.group(2).strip()
95 port_meta[multi_line_property] = multi_line_value
97 multi_line_property = ""
99 port_meta[if_property] = match.group(2).strip()
101 elif multi_line and self._u.d_push(
102 re.search(r'\s+(\S.*)', ethtool_line)) is not None:
103 match = self._u.d_pop()
104 multi_line_value += " " + match.group(1).strip()