Refactoring: Changed remaining check parameters starting with an 's' to the new rules...
[check_mk.git] / checks / lnx_if
blob4633f471ee2d7b9c435bacd9a895e4d6e05a669e
1 #!/usr/bin/python
2 # -*- encoding: utf-8; py-indent-offset: 4 -*-
3 # +------------------------------------------------------------------+
4 # | ____ _ _ __ __ _ __ |
5 # | / ___| |__ ___ ___| | __ | \/ | |/ / |
6 # | | | | '_ \ / _ \/ __| |/ / | |\/| | ' / |
7 # | | |___| | | | __/ (__| < | | | | . \ |
8 # | \____|_| |_|\___|\___|_|\_\___|_| |_|_|\_\ |
9 # | |
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:
29 # <<<lnx_if>>>
30 # [start_iplink]
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
37 # [end_iplink]
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
42 # [lo]
43 # Link detected: yes
44 # [eth0]
45 # Speed: 65535Mb/s
46 # Duplex: Unknown! (255)
47 # Auto-negotiation: on
48 # Link detected: no
49 # Address: de:ad:be:af:00:01
50 # [eth1]
51 # Speed: 1000Mb/s
52 # Duplex: Full
53 # Auto-negotiation: on
54 # Link detected: yes
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):
63 line = lines.next()
64 return line[0], line[1:]
66 nic_info = {}
67 current_nic = None
68 index = 0
69 lines = iter(info)
70 iplink_stats = {}
71 while True:
72 try:
73 node, line = _get_next_node_line(lines)
74 iplink_stats.setdefault(node, {})
75 except StopIteration:
76 break
78 # This extra info from 'ip link' is used as fallback in case ethtool is missing
79 if line[0].startswith("[start_iplink]"):
80 try:
81 while True:
82 node, line = _get_next_node_line(lines)
83 if line[0].startswith("[end_iplink]"):
84 node, line = _get_next_node_line(lines)
85 break
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"):
90 continue
91 # Each interface in this block is represented by two lines
92 status_info = line
93 try:
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
100 pass
101 except StopIteration:
102 break
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]
110 index += 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]
116 iplink_stats = {}
117 elif len(line) == 2 and len(line[1].split()) >= 16:
118 # This looks like a perf-counter line
119 nic = line[0]
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]
123 elif current_nic:
124 # ethtool data line
125 nic_info[node][current_nic][line[0].strip()] = ":".join(line[1:]).strip()
127 return nic_info
130 def parse_lnx_if(info):
131 nic_info = if_lnx_extract_nic_info(info)
133 if_table = []
134 index = 0
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])
138 index += 1
139 ifIndex = attr.get("index", index)
140 ifDescr = nic
141 ifAlias = nic
143 # Guess type from name of interface
144 if nic == "lo":
145 ifType = 24
146 else:
147 ifType = 6
149 # Compute speed
150 speed_text = attr.get("Speed")
151 if speed_text is None:
152 ifSpeed = ''
153 else:
154 if speed_text == '65535Mb/s': # unknown
155 ifSpeed = ''
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
162 else:
163 ifSpeed = ''
165 # Performance counters
166 ifInOctets = counters[0]
167 inucast = counters[1] + counters[7]
168 inmcast = counters[7]
169 inbcast = 0
170 ifInDiscards = counters[3]
171 ifInErrors = counters[2]
172 ifOutOctets = counters[8]
173 outucast = counters[9]
174 outmcast = 0
175 outbcast = 0
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")
183 if ld == "yes":
184 ifOperStatus = 1
185 elif ld == "no":
186 ifOperStatus = 2
187 else:
188 # No information from ethtool. We consider interfaces up
189 # if they have been used at least some time since the
190 # system boot.
191 iplink_stats = attr.get("iplink_stats")
192 if iplink_stats:
193 if "UP" in iplink_stats.get("extra_info",
194 []) or iplink_stats.get("state") == "UP":
195 ifOperStatus = 1
196 elif iplink_stats.get("state") == "DOWN":
197 ifOperStatus = 2
198 else:
199 ifOperStatus = 4
200 else:
201 if ifInOctets > 0:
202 ifOperStatus = 1 # assume up
203 else:
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(":")])
209 else:
210 ifPhysAddress = ''
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
218 if_table.append(row)
220 return if_table
223 def inventory_lnx_if(parsed):
224 if linux_nic_check == "legacy":
225 return []
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',
242 'node_info': True,
243 'has_perfdata': True,
244 'group': 'if',
245 'default_levels_variable': 'if_default_levels',