Set version to 1.5.0b9
[check_mk.git] / inventory / if
blobf58cef8cae6484be13018b1a4874a3a1f06cde02
1 #!/usr/bin/python
2 # -*- encoding: utf-8; py-indent-offset: 4 -*-
3 # +------------------------------------------------------------------+
4 # | ____ _ _ __ __ _ __ |
5 # | / ___| |__ ___ ___| | __ | \/ | |/ / |
6 # | | | | '_ \ / _ \/ __| |/ / | |\/| | ' / |
7 # | | |___| | | | __/ (__| < | | | | . \ |
8 # | \____|_| |_|\___|\___|_|\_\___|_| |_|_|\_\ |
9 # | |
10 # | Copyright Mathias Kettner 2013 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 def scan_inv_if(oid):
29 try:
30 return int(oid(".1.3.6.1.2.1.2.1.0")) >= 2
31 except ValueError:
32 return False
33 except TypeError:
34 return False
37 # TODO unify with other if inventory plugins
38 def inv_if(info, params):
39 def round_to_day(ts):
40 broken = time.localtime(ts)
41 return time.mktime((broken.tm_year, broken.tm_mon, broken.tm_mday, 0, 0, 0, broken.tm_wday, broken.tm_yday, broken.tm_isdst))
43 now = time.time()
45 port_info, uptime_info = info
46 uptime = parse_snmp_uptime(uptime_info[0][0])
48 usage_port_types = params.get("usage_port_types",
49 [ '6', '32', '62', '117', '127', '128', '129', '180', '181', '182', '205', '229' ])
50 unused_duration = params.get("unused_duration", 30 * 86400)
52 total_ethernet_ports = 0
53 available_ethernet_ports = 0
55 node = inv_tree_list("networking.interfaces:")
56 for if_index, if_descr, if_alias, if_type, if_speed, if_high_speed, \
57 if_oper_status, if_admin_status, if_phys_address, if_last_change in port_info:
59 if if_type in ( "231", "232" ):
60 continue # Useless entries for "TenGigabitEthernet2/1/21--Uncontrolled"
62 if not if_last_change or not if_speed:
63 continue # Ignore useless half-empty tables (e.g. Viprinet-Router)
65 # if_last_change can be of type Timeticks (100th of seconds) or
66 # a human readable time stamp (yurks)
67 try:
68 last_change = float(if_last_change) / 100.0
69 except:
70 # Example: 0:0:01:09.96
71 parts = if_last_change.split(":")
72 days = int(parts[0])
73 hours = int(parts[1])
74 minutes = int(parts[2])
75 seconds = float(parts[3])
76 last_change = seconds + 60*minutes + 3600*hours + 86400*days
78 if if_high_speed:
79 speed = int(if_high_speed) * 1000 * 1000
80 else:
81 speed = int(if_speed)
83 if last_change > 0:
84 state_age = uptime - last_change
86 # Assume counter rollover in case uptime is less than last_change and
87 # add 497 days (counter maximum).
88 # This way no negative chenge times are shown anymore. The state change is shown
89 # wrong in case it's really 497 days ago when state changed but there's no way to
90 # get the count of rollovers since change (or since uptime) and it's better the
91 # wrong negative state change is not shown anymore...
92 if state_age < 0:
93 state_age = 42949672 - last_change + uptime
95 else:
96 # Assume point of time of boot as last state change.
97 state_age = uptime
99 last_change_timestamp = round_to_day(now - state_age)
101 node.append({
102 "index" : int(if_index),
103 "description" : if_descr,
104 "alias" : if_alias,
105 "speed" : speed,
106 "phys_address" : if_render_mac_address(if_phys_address),
107 "oper_status" : int(if_oper_status),
108 "admin_status" : int(if_admin_status), # 1(up) or 2(down)
109 "port_type" : int(if_type),
110 "last_change" : int(last_change_timestamp),
114 if if_type in usage_port_types:
115 if_available = if_oper_status == '2' and state_age > unused_duration
116 total_ethernet_ports += 1
117 if if_available:
118 available_ethernet_ports += 1
119 node[-1]["available"] = if_available
120 else:
121 if_available = None
123 node = inv_tree("networking.")
124 node["available_ethernet_ports"] = available_ethernet_ports
125 node["total_ethernet_ports"] = total_ethernet_ports
126 node["total_interfaces"] = len(info)
128 inv_info['inv_if'] = {
129 "inv_function" : inv_if,
130 'snmp_info': [
131 ( ".1.3.6.1.2.1", [
132 "2.2.1.1", # ifIndex
133 "2.2.1.2", # ifDescr
134 "31.1.1.1.18", # ifAlias
135 "2.2.1.3", # ifType
136 "2.2.1.5", # ifSpeed
137 "31.1.1.1.15", # ifHighSpeed .. 1000 means 1Gbit
138 "2.2.1.8", # ifOperStatus
139 "2.2.1.7", # ifAdminStatus
140 BINARY("2.2.1.6"), # ifPhysAddress
141 "2.2.1.9", # ifLastChange
143 ( ".1.3.6.1.2.1.1", [ "3.0" ] ), # uptime
145 'snmp_scan_function': scan_inv_if,
146 'includes': ['if.include', 'uptime.include'],