OSDN Git Service

add routing informations for tunneling remote_ip to OVSAgent
[ti2/ti2.git] / linkpair / collect / agent / linuxnet_agent.py
1 # vim: tabstop=4 shiftwidth=4 softtabstop=4
2 # -*- coding: utf-8 -*-
3 #
4
5 __version__ = '1.1'
6
7 import re
8 from linkpair.device import Device
9 from linkpair.port import Port
10 from linkpair.collect.agent.base_agent import BaseAgent
11
12
13 class LinuxNetAgent(BaseAgent):
14     '''
15     Linux Network Collector Agent
16     '''
17     def run(self):
18         self.get_configuration()
19
20     def get_configuration(self):
21         interfaces = []
22         port_name = ""
23         port_meta = {}
24         found_if = False
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()
28             if self._u.d_push(
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)
38                 if found_if:
39                     self._cu.add_port(Port(port_name, Port.DEFAULT_TYPE, port_meta))
40                     port_name = ""
41                     port_meta = {}
42                 else:
43                     found_if = True
44             elif self._u.d_push(
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()
50             elif self._u.d_push(
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()
57                 
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()
63                 if self._u.d_push(
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()
69
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()
74                 if self._u.d_push(
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()
80                     
81             self._cu.add_port(Port(interface, Port.DEFAULT_TYPE, port_meta))
82             interface = ""
83             port_meta = {}
84