2 # -*- encoding: utf-8; py-indent-offset: 4 -*-
3 # +------------------------------------------------------------------+
4 # | ____ _ _ __ __ _ __ |
5 # | / ___| |__ ___ ___| | __ | \/ | |/ / |
6 # | | | | '_ \ / _ \/ __| |/ / | |\/| | ' / |
7 # | | |___| | | | __/ (__| < | | | | . \ |
8 # | \____|_| |_|\___|\___|_|\_\___|_| |_|_|\_\ |
10 # | Copyright Mathias Kettner 2014 mk@mathias-kettner.de |
11 # +------------------------------------------------------------------+
13 # This file is part of Check_MK.
14 # The official homepage is at http://mathias-kettner.de/check_mk.
16 # check_mk is free software; you can redistribute it and/or modify it
17 # under the terms of the GNU General Public License as published by
18 # the Free Software Foundation in version 2. check_mk is distributed
19 # in the hope that it will be useful, but WITHOUT ANY WARRANTY; with-
20 # out even the implied warranty of MERCHANTABILITY or FITNESS FOR A
21 # PARTICULAR PURPOSE. See the GNU General Public License for more de-
22 # tails. You should have received a copy of the GNU General Public
23 # License along with GNU Make; see the file COPYING. If not, write
24 # to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
25 # Boston, MA 02110-1301 USA.
27 # Example output from agent:
31 # 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default
32 # link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
33 # 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
34 # link/ether 00:27:13:b4:a9:ec brd ff:ff:ff:ff:ff:ff
35 # 3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP mode DORMANT group default qlen 1000
36 # link/ether 00:21:6a:10:8e:b8 brd ff:ff:ff:ff:ff:ff
38 # <<<lnx_if:sep(58)>>>
39 # lo: 4520 54 0 0 0 0 0 0 4520 54 0 0 0 0 0 0
40 # eth0: 0 0 0 0 0 0 0 0 1710 5 0 0 0 0 0 0
41 # eth1: 78505 555 0 0 0 0 0 0 132569 523 0 0 0 0 0 0
46 # Duplex: Unknown! (255)
47 # Auto-negotiation: on
49 # Address: de:ad:be:af:00:01
53 # Auto-negotiation: on
56 check_includes
['lnx_if'] = ["if.include"]
58 linux_nic_check
= "lnx_if"
61 def if_lnx_extract_nic_info(info
):
62 def _get_next_node_line(lines
):
64 return line
[0], line
[1:]
73 node
, line
= _get_next_node_line(lines
)
74 iplink_stats
.setdefault(node
, {})
78 # This extra info from 'ip link' is used as fallback in case ethtool is missing
79 if line
[0].startswith("[start_iplink]"):
82 node
, line
= _get_next_node_line(lines
)
83 if line
[0].startswith("[end_iplink]"):
84 node
, line
= _get_next_node_line(lines
)
86 # There some cases with an additional line. These are currently unused
87 # but need to be handled anyway. This particular line contains information
88 # provided by cisco about neighors (CDP)
89 if line
[0].startswith("alias"):
91 # Each interface in this block is represented by two lines
94 nic_name
= status_info
[1][:-1]
95 iplink_stats
[node
].setdefault(
96 nic_name
, {"extra_info": status_info
[2][1:-1].split(",")})
97 iplink_stats
[node
][nic_name
].update(
98 dict(zip(status_info
[3::2], status_info
[4::2])))
99 except: # In case of parse errors we simply ignore these lines
101 except StopIteration:
104 nic_info
.setdefault(node
, {})
105 # Be careful! On clustered hosts we have more than one perf-counters section
106 # and ethtool section. This needs to be handled. Sadly we have no section
107 # headers. Try to detect it by data format.
108 if line
[0].startswith('['):
109 current_nic
= line
[0][1:-1]
111 nic_info
[node
][current_nic
]['index'] = index
112 # The iplink_stats are only used within the perf-counters
113 # The (optional) ethtool section invalidates this info, otherwise it would
114 # be incorrectly reused in a followup section of another cluster node
115 # When the ethtool section is missing the data is also reset by the next [start_iplink]
117 elif len(line
) == 2 and len(line
[1].split()) >= 16:
118 # This looks like a perf-counter line
120 nic_info
[node
][nic
] = {"counters": map(int, line
[1].split())}
121 if nic
in iplink_stats
[node
]:
122 nic_info
[node
][nic
]['iplink_stats'] = iplink_stats
[node
][nic
]
125 nic_info
[node
][current_nic
][line
[0].strip()] = ":".join(line
[1:]).strip()
130 def parse_lnx_if(info
):
131 nic_info
= if_lnx_extract_nic_info(info
)
135 for node_info
, nic_attrs
in nic_info
.iteritems():
136 for nic
, attr
in nic_attrs
.iteritems():
137 counters
= attr
.get("counters", [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
139 ifIndex
= attr
.get("index", index
)
143 # Guess type from name of interface
150 speed_text
= attr
.get("Speed")
151 if speed_text
is None:
154 if speed_text
== '65535Mb/s': # unknown
156 elif speed_text
.endswith("Kb/s"):
157 ifSpeed
= int(speed_text
[:-4]) * 1000
158 elif speed_text
.endswith("Mb/s"):
159 ifSpeed
= int(speed_text
[:-4]) * 1000000
160 elif speed_text
.endswith("Gb/s"):
161 ifSpeed
= int(speed_text
[:-4]) * 1000000000
165 # Performance counters
166 ifInOctets
= counters
[0]
167 inucast
= counters
[1] + counters
[7]
168 inmcast
= counters
[7]
170 ifInDiscards
= counters
[3]
171 ifInErrors
= counters
[2]
172 ifOutOctets
= counters
[8]
173 outucast
= counters
[9]
176 ifOutDiscards
= counters
[11]
177 ifOutErrors
= counters
[10]
178 ifOutQLen
= counters
[12]
180 # Link state from ethtool. If ethtool has no information about
181 # this NIC, we set the state to unknown.
182 ld
= attr
.get("Link detected")
188 # No information from ethtool. We consider interfaces up
189 # if they have been used at least some time since the
191 iplink_stats
= attr
.get("iplink_stats")
193 if "UP" in iplink_stats
.get("extra_info",
194 []) or iplink_stats
.get("state") == "UP":
196 elif iplink_stats
.get("state") == "DOWN":
202 ifOperStatus
= 1 # assume up
204 ifOperStatus
= 4 # unknown (NIC has never been used)
206 if attr
.get("Address"):
207 ifPhysAddress
= "".join(
208 [chr(int(x
, 16)) for x
in attr
.get("Address", "").split(":")])
212 row
= [node_info
] + map(str, [
213 ifIndex
, ifDescr
, ifType
, ifSpeed
, ifOperStatus
, ifInOctets
, inucast
, inmcast
,
214 inbcast
, ifInDiscards
, ifInErrors
, ifOutOctets
, outucast
, outmcast
, outbcast
,
215 ifOutDiscards
, ifOutErrors
, ifOutQLen
, ifAlias
, ifPhysAddress
223 def inventory_lnx_if(parsed
):
224 if linux_nic_check
== "legacy":
227 # Always exclude dockers veth* interfaces on docker nodes
228 parsed
= [e
for e
in parsed
if not e
[2].startswith("veth")]
230 return inventory_if_common(parsed
, has_nodeinfo
=True)
233 def check_lnx_if(item
, params
, parsed
):
234 return check_if_common(item
, params
, parsed
, has_nodeinfo
=True)
237 check_info
["lnx_if"] = {
238 'parse_function': parse_lnx_if
,
239 'inventory_function': inventory_lnx_if
,
240 'check_function': check_lnx_if
,
241 'service_description': 'Interface %s',
243 'has_perfdata': True,
245 'default_levels_variable': 'if_default_levels',