Refactoring: Changed all check parameters starting with an 'o' to the new rulespec...
[check_mk.git] / checks / esx_vsphere_objects
blobdfe5bf7c1b248192fd5ff5b7aa483ea651471025
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:
28 # <<<esx_vsphere_objects:sep(9)>>>
29 # hostsystem esx.wacker.corp
30 # virtualmachine LinuxI
31 # virtualmachine OpenSUSE_II
32 # virtualmachine OpenSUSE_III
33 # virtualmachine OpenSUSE_IV
34 # virtualmachine OpenSUSE_V
35 # virtualmachine WindowsXP I
36 # virtualmachine LinuxII
37 # virtualmachine LinuxIII
38 # virtualmachine LinuxIV
39 # virtualmachine LinuxV
40 # virtualmachine OpenSUSE_I
42 vsphere_object_names = {
43 "hostsystem": "HostSystem",
44 "virtualmachine": "VM",
47 factory_settings["esx_vsphere_objects_default_levels"] = {
48 "states": {
49 "poweredOn": 0,
50 "poweredOff": 1,
51 "suspended": 1,
52 "unknown": 3,
57 def parse_esx_vsphere_objects(info):
58 parsed = {}
59 Obj = collections.namedtuple("EsxObj", ["name", "hostsystem", "state"])
60 for line in info:
61 if len(line) < 2:
62 continue
63 if len(line) < 4:
64 line += [''] * (4 - len(line))
65 obj_type = vsphere_object_names.get(line[0], "Unknown Object")
66 name = "%s %s" % (obj_type, line[1])
67 obj = Obj(name, line[2], line[3])
68 parsed[obj.name] = obj
70 return parsed
73 # .--Single--------------------------------------------------------------.
74 # | ____ _ _ |
75 # | / ___|(_)_ __ __ _| | ___ |
76 # | \___ \| | '_ \ / _` | |/ _ \ |
77 # | ___) | | | | | (_| | | __/ |
78 # | |____/|_|_| |_|\__, |_|\___| |
79 # | |___/ |
80 # '----------------------------------------------------------------------'
83 def inventory_esx_vsphere_objects(parsed):
84 for key in parsed:
85 yield key, {}
88 def check_esx_vsphere_objects(item, params, parsed):
89 if params is None:
90 params = {}
92 obj = parsed.get(item)
93 if obj is None:
94 yield 3, "Missing item: %s" % item
95 return
97 if not obj.state:
98 what, name = item.split()
99 if what == "VM":
100 yield 3, "Virtual machine %s is missing" % name
101 else:
102 yield 3, "No data about host system %s" % name
103 return
105 state = params.get("states", {}).get(obj.state, 3)
106 yield state, "power state: %s" % obj.state
108 if obj.hostsystem:
109 if obj.state == "poweredOn":
110 yield 0, 'running on [%s]' % obj.hostsystem
111 else:
112 yield 0, 'defined on [%s]' % obj.hostsystem
115 check_info['esx_vsphere_objects'] = {
116 "parse_function": parse_esx_vsphere_objects,
117 "inventory_function": inventory_esx_vsphere_objects,
118 "check_function": check_esx_vsphere_objects,
119 "service_description": "%s",
120 "group": "esx_vsphere_objects",
121 "default_levels_variable": "esx_vsphere_objects_default_levels",
125 def inventory_esx_vsphere_objects_count(parsed):
126 yield None, {}
129 def check_esx_vsphere_objects_count(_no_item, params, parsed):
130 if params is None:
131 params = {}
133 virtualmachines = [o for o in parsed.itervalues() if o.name.startswith("VM ")]
134 yield 0, "Virtualmachines: %d" % len(virtualmachines), [('vms', len(virtualmachines))]
136 hostsystems = [o for o in parsed.itervalues() if o.name.startswith("HostSystem")]
137 if not hostsystems:
138 return
140 yield 0, "Hostsystems: %d" % len(hostsystems), [('hosts', len(hostsystems))]
142 for distribution in params.get("distribution", []):
143 ruled_vms = distribution.get("vm_names", [])
144 hosts = set(vm.hostsystem for vm in virtualmachines if vm.name[3:] in ruled_vms)
145 count = len(hosts)
146 hosts = sorted(hosts)
147 if count < distribution["hosts_count"]:
148 yield distribution.get(
149 "state",
150 2), ("VMs %s are running on %d host%s: %s" %
151 (', '.join(ruled_vms), count, '' if count == 1 else 's', ', '.join(hosts)))
154 check_info['esx_vsphere_objects.count'] = {
155 "inventory_function": inventory_esx_vsphere_objects_count,
156 "check_function": check_esx_vsphere_objects_count,
157 "service_description": "Object count",
158 "has_perfdata": True,
159 "group": "esx_vsphere_objects_count",