Cleanup config.nodes_of
[check_mk.git] / checks / hyperv_vms
bloba38714243b6fd2ad97973602de23ef64aef7bebc
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 # <<<hyperv_vms>>>
29 # DMZ-DC1 Running 4.21:44:58 Operating normally
30 # DMZ-DC2 Running 4.21:44:47 Operating normally
32 # Another example, here with a snapshow with spaces in the name:
33 # <<<hyperv_vms>>>
34 # windows-hyperv2-z4058044 Running 21:33:08 Operating normally
35 # windows-hyperv2-z4058044_snap (23.05.2014 - 09:29:29) Running 18:20:34 Operating normally
36 # windows-hyperv2-z4065002 Running 11:04:50 Operating normally
37 # windows-hyperv2-z4065084 Running 1.10:42:33 Operating normally
38 # windows-hyperv2-z4133235 Running 1.03:52:18 Operating normally
40 # A broken version of the agent outputted this:
41 # <<<hyperv_vms>>>
42 # z4058044 Running 21:19:14 Operating normally
43 # z4058044_snap (2... Running 18:06:39 Operating normally
44 # z4065002 Running 10:50:55 Operating normally
45 # z4065084 Running 1.10:28:39 Operating normally
46 # z4133235 Running 1.03:38:23 Operating normally
48 # A Version with a plugin that uses tab as seperator and quotes the strings:
49 # <<<hyperv_vms:sep(9)>>>
50 # "Name" "State" "Uptime" "Status"
51 # "z4058013" "Running" "06:05:16" "Operating normally"
52 # "z4058020" "Running" "01:01:57" "Operating normally"
53 # "z4058021" "Running" "01:02:11" "Operating normally"
54 # "z4065012" "Running" "01:02:04" "Operating normally"
55 # "z4065013" "Running" "07:47:27" "Operating normally"
56 # "z4065020" "Running" "01:02:09" "Operating normally"
57 # "z4065025" "Running" "01:02:05" "Operating normally"
58 # "z4133199" "Running" "00:57:23" "Operating normally"
60 # result:
61 # {
62 # "windows-hyperv2-z4058044_snap (23.05.2014 - 09:29:29)" : {
63 # "vm_state" : "Running",
64 # "uptime" : "1.10:42:33",
65 # "state_msg" : "Operating normally",
66 # }
67 # }
70 def parse_hyperv_vms(info):
71 parsed = {}
72 for line in info:
73 # Remove quotes
74 line = [x.strip('"') for x in line]
75 if line[1].endswith("..."): # broken output
76 vm_name = line[0]
77 line = line[2:]
78 elif line[1].startswith("("):
79 idx = 2
80 while idx < len(line):
81 if line[idx].endswith(")"):
82 vm_name = " ".join(line[:idx + 1])
83 break
84 idx += 1
85 line = line[idx + 1:]
86 else:
87 vm_name = line[0]
88 line = line[1:]
90 if ':' in line[1]: # skip heading line
91 parsed[vm_name] = {
92 "state": line[0],
93 "uptime": line[1],
94 "state_msg": " ".join(line[2:]),
96 return parsed
99 def inventory_hyperv_vms(parsed):
100 return [(vm_name, {'state': vm["state"]}) for (vm_name, vm) in parsed.items()]
103 def check_hyperv_vms(item, params, parsed):
104 if item in parsed:
105 vm = parsed[item]
106 if vm["state"] == params['state']:
107 state = 0
108 message = "State is %s (%s)" % (vm["state"], vm["state_msg"])
109 else:
110 message = "State has changed from %s to %s (%s)" % (
111 params['state'],\
112 vm["state"],
113 vm["state_msg"])
114 state = 2
115 return state, message
118 check_info["hyperv_vms"] = {
119 "parse_function": parse_hyperv_vms,
120 "check_function": check_hyperv_vms,
121 "inventory_function": inventory_hyperv_vms,
122 "service_description": "VM %s",