2 # -*- encoding: utf-8; py-indent-offset: 4 -*-
3 # +------------------------------------------------------------------+
4 # | ____ _ _ __ __ _ __ |
5 # | / ___| |__ ___ ___| | __ | \/ | |/ / |
6 # | | | | '_ \ / _ \/ __| |/ / | |\/| | ' / |
7 # | | |___| | | | __/ (__| < | | | | . \ |
8 # | \____|_| |_|\___|\___|_|\_\___|_| |_|_|\_\ |
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"] = {
57 def parse_esx_vsphere_objects(info
):
59 Obj
= collections
.namedtuple("EsxObj", ["name", "hostsystem", "state"])
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
73 # .--Single--------------------------------------------------------------.
75 # | / ___|(_)_ __ __ _| | ___ |
76 # | \___ \| | '_ \ / _` | |/ _ \ |
77 # | ___) | | | | | (_| | | __/ |
78 # | |____/|_|_| |_|\__, |_|\___| |
80 # '----------------------------------------------------------------------'
83 def inventory_esx_vsphere_objects(parsed
):
88 def check_esx_vsphere_objects(item
, params
, parsed
):
92 obj
= parsed
.get(item
)
94 yield 3, "Missing item: %s" % item
98 what
, name
= item
.split()
100 yield 3, "Virtual machine %s is missing" % name
102 yield 3, "No data about host system %s" % name
105 state
= params
.get("states", {}).get(obj
.state
, 3)
106 yield state
, "power state: %s" % obj
.state
109 if obj
.state
== "poweredOn":
110 yield 0, 'running on [%s]' % obj
.hostsystem
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
):
129 def check_esx_vsphere_objects_count(_no_item
, params
, parsed
):
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")]
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
)
146 hosts
= sorted(hosts
)
147 if count
< distribution
["hosts_count"]:
148 yield distribution
.get(
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",