1 # vim: tabstop=4 shiftwidth=4 softtabstop=4
2 # -*- coding: utf-8 -*-
8 from linkpair.device import Device
9 from linkpair.port import Port
10 from linkpair.collect.agent.base_agent import BaseAgent
13 class LinuxNetAgent(BaseAgent):
15 Linux Network Collector Agent
18 self.get_configuration()
20 def get_configuration(self):
25 res_ipaddr = self._runner.exec_cmd("ip addr show")
26 for i in range(0, len(res_ipaddr)):
27 port_start = res_ipaddr[i].strip()
29 re.search(r'(\d+): (\S+): <(\S+)> mtu (\d+) (.*)',
30 port_start)) is not None:
31 match = self._u.d_pop()
32 port_meta["if_index"] = match.group(1).strip()
33 port_name = match.group(2).strip()
34 port_meta["if_state"] = match.group(3).strip()
35 port_meta["mtu"] = match.group(4).strip()
36 port_meta["if_other_state"] = match.group(5).strip()
37 interfaces.append(port_name)
39 self._cu.add_port(Port(port_name, Port.DEFAULT_TYPE, port_meta))
45 re.search(r'link/ether (\S+) brd (\S+)',
46 port_start)) is not None:
47 match = self._u.d_pop()
48 port_meta["mac_addr"] = match.group(1).strip()
49 port_meta["mac_bcast_addr"] = match.group(2).strip()
51 re.search(r'inet (\S+) brd (\S+) scope (\S+)',
52 port_start)) is not None:
53 match = self._u.d_pop()
54 port_meta["inet_addr"] = match.group(1).strip()
55 port_meta["inet_bcast_addr"] = match.group(2).strip()
56 port_meta["if_scope"] = match.group(3).strip()
58 for interface in interfaces:
59 # interface informations
60 res_ethtool = self._runner.exec_cmd("ethtool -i " + interface)
61 for i in range(0, len(res_ethtool)):
62 ethtool_line = res_ethtool[i].strip()
64 re.search(r'(\S+): (\S+)',
65 ethtool_line)) is not None:
66 match = self._u.d_pop()
67 if_property = match.group(1).strip()
68 port_meta[if_property] = match.group(2).strip()
70 # interface statistics
71 res_ethtool = self._runner.exec_cmd("ethtool -S " + interface)
72 for i in range(0, len(res_ethtool)):
73 ethtool_line = res_ethtool[i].strip()
75 re.search(r'(\S+): (\S+)',
76 ethtool_line)) is not None:
77 match = self._u.d_pop()
78 if_property = match.group(1).strip()
79 port_meta[if_property] = match.group(2).strip()
81 self._cu.add_port(Port(interface, Port.DEFAULT_TYPE, port_meta))