GUI CSS: Decoupled most styles for sidebar from the classic theme (CMK-1171)
[check_mk.git] / inventory / if
blob2320fe8e65d727af70b0e3c697c3d00a8e83b26d
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,
42 broken.tm_yday, broken.tm_isdst))
44 now = time.time()
46 port_info, uptime_info = info
47 uptime = parse_snmp_uptime(uptime_info[0][0])
49 usage_port_types = params.get(
50 "usage_port_types",
51 ['6', '32', '62', '117', '127', '128', '129', '180', '181', '182', '205', '229'])
52 unused_duration = params.get("unused_duration", 30 * 86400)
54 total_ethernet_ports = 0
55 available_ethernet_ports = 0
57 node = inv_tree_list("networking.interfaces:")
58 for if_index, if_descr, if_alias, if_type, if_speed, if_high_speed, \
59 if_oper_status, if_admin_status, if_phys_address, if_last_change in port_info:
61 if if_type in ("231", "232"):
62 continue # Useless entries for "TenGigabitEthernet2/1/21--Uncontrolled"
64 if not if_last_change or not if_speed:
65 continue # Ignore useless half-empty tables (e.g. Viprinet-Router)
67 # if_last_change can be of type Timeticks (100th of seconds) or
68 # a human readable time stamp (yurks)
69 try:
70 last_change = float(if_last_change) / 100.0
71 except:
72 # Example: 0:0:01:09.96
73 parts = if_last_change.split(":")
74 days = int(parts[0])
75 hours = int(parts[1])
76 minutes = int(parts[2])
77 seconds = float(parts[3])
78 last_change = seconds + 60 * minutes + 3600 * hours + 86400 * days
80 if if_high_speed:
81 speed = int(if_high_speed) * 1000 * 1000
82 else:
83 speed = int(if_speed)
85 if last_change > 0:
86 state_age = uptime - last_change
88 # Assume counter rollover in case uptime is less than last_change and
89 # add 497 days (counter maximum).
90 # This way no negative chenge times are shown anymore. The state change is shown
91 # wrong in case it's really 497 days ago when state changed but there's no way to
92 # get the count of rollovers since change (or since uptime) and it's better the
93 # wrong negative state change is not shown anymore...
94 if state_age < 0:
95 state_age = 42949672 - last_change + uptime
97 else:
98 # Assume point of time of boot as last state change.
99 state_age = uptime
101 last_change_timestamp = round_to_day(now - state_age)
103 node.append({
104 "index": int(if_index),
105 "description": if_descr,
106 "alias": if_alias,
107 "speed": speed,
108 "phys_address": if_render_mac_address(if_phys_address),
109 "oper_status": int(if_oper_status),
110 "admin_status": int(if_admin_status), # 1(up) or 2(down)
111 "port_type": int(if_type),
112 "last_change": int(last_change_timestamp),
115 if if_type in usage_port_types:
116 if_available = if_oper_status == '2' and state_age > unused_duration
117 total_ethernet_ports += 1
118 if if_available:
119 available_ethernet_ports += 1
120 node[-1]["available"] = if_available
121 else:
122 if_available = None
124 node = inv_tree("networking.")
125 node["available_ethernet_ports"] = available_ethernet_ports
126 node["total_ethernet_ports"] = total_ethernet_ports
127 node["total_interfaces"] = len(info)
130 inv_info['inv_if'] = {
131 "inv_function": inv_if,
132 'snmp_info': [
134 ".1.3.6.1.2.1",
136 "2.2.1.1", # ifIndex
137 "2.2.1.2", # ifDescr
138 "31.1.1.1.18", # ifAlias
139 "2.2.1.3", # ifType
140 "2.2.1.5", # ifSpeed
141 "31.1.1.1.15", # ifHighSpeed .. 1000 means 1Gbit
142 "2.2.1.8", # ifOperStatus
143 "2.2.1.7", # ifAdminStatus
144 BINARY("2.2.1.6"), # ifPhysAddress
145 "2.2.1.9", # ifLastChange
147 (".1.3.6.1.2.1.1", ["3.0"]), # uptime
149 'snmp_scan_function': scan_inv_if,
150 'includes': ['if.include', 'uptime.include'],