OSDN Git Service

add ethtool\'s informations to LinuxNetAgent
[ti2/ti2.git] / linkpair / collect / agent / linuxnet_agent.py
1 # vim: tabstop=4 shiftwidth=4 softtabstop=4
2 # -*- coding: utf-8 -*-
3 #
4
5 import re
6 from linkpair.device import Device
7 from linkpair.port import Port
8 from linkpair.collect.agent.base_agent import BaseAgent
9
10
11 class LinuxNetAgent(BaseAgent):
12     '''
13     Linux Network Collector Agent
14     '''
15     def run(self):
16         self.get_configuration()
17
18     def get_configuration(self):
19         interfaces = []
20         port_name = ""
21         port_meta = {}
22         found_if = False
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()
26             if self._u.d_push(
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)
36                 if found_if:
37                     self._cu.add_port(Port(port_name, Port.DEFAULT_TYPE, port_meta))
38                     port_name = ""
39                     port_meta = {}
40                 else:
41                     found_if = True
42             elif self._u.d_push(
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()
48             elif self._u.d_push(
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()
55                 
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)
60
61             # interface driver informations
62             res_ethtool = self._runner.exec_cmd("ethtool -i " + interface)
63             self._parse_ethtool_output(res_ethtool, port_meta)
64
65             # interface statistics
66             res_ethtool = self._runner.exec_cmd("ethtool -S " + interface)
67             self._parse_ethtool_output(res_ethtool, port_meta)
68
69             # interface offload settings
70             res_ethtool = self._runner.exec_cmd("ethtool -k " + interface)
71             self._parse_ethtool_output(res_ethtool, port_meta)
72                     
73             self._cu.add_port(Port(interface, Port.DEFAULT_TYPE, port_meta))
74             interface = ""
75             port_meta = {}
76             
77     def _parse_ethtool_output(self, res_ethtool, port_meta):
78         if_property = ""
79         multi_line = False
80         multi_line_property = ""
81         multi_line_value = ""
82         
83         for i in range(0, len(res_ethtool)):
84             ethtool_line = res_ethtool[i].rstrip()
85             if self._u.d_push(
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):
90                     multi_line = True
91                     multi_line_property = if_property
92                     multi_line_value = match.group(2).strip()
93                 else:
94                     if multi_line:
95                         port_meta[multi_line_property] = multi_line_value
96                         multi_line = False
97                         multi_line_property = ""
98                         multi_line_value = ""
99                     port_meta[if_property] = match.group(2).strip()
100
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()
105             
106