Set version to 1.5.0b9
[check_mk.git] / inventory / cisco_vlans
blob958440390bced9e146cac25c9dab9517d32d0f63
1 #!/usr/bin/python
2 # -*- encoding: utf-8; py-indent-offset: 4 -*-
3 # +------------------------------------------------------------------+
4 # | ____ _ _ __ __ _ __ |
5 # | / ___| |__ ___ ___| | __ | \/ | |/ / |
6 # | | | | '_ \ / _ \/ __| |/ / | |\/| | ' / |
7 # | | |___| | | | __/ (__| < | | | | . \ |
8 # | \____|_| |_|\___|\___|_|\_\___|_| |_|_|\_\ |
9 # | |
10 # | Copyright Mathias Kettner 2016 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.
28 # 1.3.6.1.4.1.9.9.68.1.2.2.1.1 --> vmVlanType: static(1), dynamic(2), multiVlan(3)
29 # 1.3.6.1.4.1.9.9.68.1.2.2.1.2 --> vmVlan: id of the vlan the port is asssigned to
30 # if type = 1 or 2.
31 # it's 0 if the port is not assigned to a vlan
32 # 1.3.6.1.4.1.9.9.68.1.2.2.1.4 --> vmVlans: the vlans the port is assigned to
33 # if the type = 3
35 # "The VLAN(s) the port is assigned to when the
36 # port's vmVlanType is set to multiVlan.
37 # This object is not instantiated if not applicable.
39 # The port is always assigned to one or more VLANs
40 # and the object may not be set so that there are
41 # no vlans assigned.
43 # Each octet within the value of this object specifies a
44 # set of eight VLANs, with the first octet specifying
45 # VLAN id 1 through 8, the second octet specifying VLAN
46 # ids 9 through 16, etc. Within each octet, the most
47 # significant bit represents the lowest numbered
48 # VLAN id, and the least significant bit represents the
49 # highest numbered VLAN id. Thus, each VLAN of the
50 # port is represented by a single bit within the
51 # value of this object. If that bit has a value of
52 # '1' then that VLAN is included in the set of
53 # VLANs; the VLAN is not included if its bit has a
54 # value of '0'."
57 # This function compresses a list of vlans, eg [1,3,4,7,8,9,...,1024],
58 # into a readable format: "1, 3-4, 7-1024" in case of multi-vlans
59 def parse_multi_vlan(vlan_multi):
61 def concatenate_vlans(vlan, subinfo):
62 if vlan not in subinfo:
63 subinfo.append(vlan)
64 return "-".join(map(str, subinfo))
66 vlans = []
67 for k, hex in enumerate(vlan_multi):
68 for l, bit in enumerate(bin(ord(hex))[2:]):
69 if bit == '1':
70 vlans.append(k*8 + l+1)
72 if not vlans:
73 return ""
75 infotexts = []
76 subinfo = vlans[:1]
77 last_vlan = vlans[0]
79 for vlan in vlans[1:]:
80 if vlan - last_vlan > 1:
81 infotexts.append(concatenate_vlans(last_vlan, subinfo))
82 subinfo = [vlan]
84 if vlan == vlans[-1]:
85 infotexts.append(concatenate_vlans(vlan, subinfo))
87 last_vlan = vlan
89 return ", ".join(infotexts)
92 def inv_cisco_vlans(info, params):
93 node = inv_tree("networking.interfaces:")
94 map_vlans = {
95 '1' : 'static',
96 '2' : 'dynamic',
97 '3' : 'multi-VLAN',
100 for if_id, vlan_type, vlan_single, vlan_multi in info:
101 vlan_readable = map_vlans.get(vlan_type, "")
102 vlans = None
103 if vlan_single != '0' and vlan_type in ['1', '2']:
104 vlans = vlan_single
105 elif vlan_type == '3':
106 vlans = parse_multi_vlan(vlan_multi)
108 if vlans:
109 for if_info in node:
110 if if_info["index"] == int(if_id):
111 if_info["vlans"] = vlans
112 if_info["vlantype"] = vlan_readable
113 break
116 inv_info['inv_cisco_vlans'] = {
117 "inv_function" : inv_cisco_vlans,
118 'snmp_info' : (".1.3.6.1.4.1.9.9.68.1.2.2.1", [
119 OID_END,
120 "1", # vmVlanType
121 "2", # vmVlan
122 "4", # vmVlans
124 'snmp_scan_function': lambda oid: "cisco" in oid(".1.3.6.1.2.1.1.1.0").lower(),