2 # -*- encoding: utf-8; py-indent-offset: 4 -*-
3 # +------------------------------------------------------------------+
4 # | ____ _ _ __ __ _ __ |
5 # | / ___| |__ ___ ___| | __ | \/ | |/ / |
6 # | | | | '_ \ / _ \/ __| |/ / | |\/| | ' / |
7 # | | |___| | | | __/ (__| < | | | | . \ |
8 # | \____|_| |_|\___|\___|_|\_\___|_| |_|_|\_\ |
10 # | Copyright Mathias Kettner 2018 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 # The following two are needed both in the factory settings for
28 # the single check, as well as in the summary check.
29 _AZURE_VM_STATES_PROV
= {
34 _AZURE_VM_STATES_POWER
= {
35 # Power states listed here:
36 # https://docs.microsoft.com/en-us/azure/virtual-machines/windows/tutorial-manage-vm
40 "stopped": 1, # VMs in the stopped state still incur compute charges.
42 "deallocated": 0, # VMs in the Deallocated state do not incur compute charges.
46 factory_settings
['levels_azure_virtualmachines'] = {
47 "map_provisioning_states": _AZURE_VM_STATES_PROV
,
48 "map_power_states": _AZURE_VM_STATES_POWER
,
52 def _azure_vms_get_status(resource
, desired_type
):
53 for state
in resource
.get("specific_info", {}).get("statuses", []):
54 stat_type
, stat_raw
= state
.get("code", "/").split('/')[:2]
55 if stat_type
.startswith(desired_type
):
56 status
= stat_raw
.lower() if stat_raw
!= "-" else "unknown"
57 raw_msg
= state
.get("message")
58 return status
, " (%s)" % raw_msg
if raw_msg
else ""
63 def check_azure_virtualmachines(_item
, params
, resource
):
65 map_provisioning_states
= params
.get("map_provisioning_states", {})
66 map_power_states
= params
.get("map_power_states", {})
68 prov_state
, msg
= _azure_vms_get_status(resource
, 'ProvisioningState')
69 yield map_provisioning_states
.get(prov_state
, 1), "Provisioning %s%s" % (prov_state
, msg
)
71 power_state
, msg
= _azure_vms_get_status(resource
, 'PowerState')
72 yield map_power_states
.get(power_state
, 1), "VM %s%s" % (power_state
, msg
)
74 # This service may be present on the agent host, the VM itself,
75 # or the resource group host. No need to show the group in the latter case.
76 group
= resource
.get("group")
77 if host_name() != group
:
78 yield 0, "Resource group: %s" % group
80 for kv_pair
in azure_iter_informative_attrs(resource
):
81 yield 0, "%s: %s" % kv_pair
84 check_info
['azure_virtualmachines'] = {
85 'parse_function': parse_azure
,
86 'inventory_function': discover(),
87 'check_function': check_azure_virtualmachines
,
88 'service_description': "VM %s",
89 'includes': ['azure.include'],
90 'default_levels_variable': 'levels_azure_virtualmachines',
94 factory_settings
['levels_azure_virtualmachines_summary'] = {
95 "levels_provisioning": {
108 def _azure_vms_check_levels(count
, state
, levels
):
110 msg
= "%d %s" % (count
, state
)
111 warn_lower
, crit_lower
= levels
.get("levels_lower", (None, None))
112 warn_upper
, crit_upper
= levels
.get("levels", (None, None))
115 if crit_lower
is not None and count
<= crit_lower
:
117 msg
+= " (warn/crit below %d/%d)" % (warn_lower
, crit_lower
)
118 elif warn_lower
is not None and count
<= warn_lower
:
120 msg
+= " (warn/crit below %d/%d)" % (warn_lower
, crit_lower
)
122 if crit_upper
is not None and count
>= crit_upper
:
123 state
= max(state
, 2)
124 msg
+= " (warn/crit at %d/%d)" % (warn_upper
, crit_upper
)
125 elif warn_upper
is not None and count
>= warn_upper
:
126 state
= max(state
, 1)
127 msg
+= " (warn/crit at %d/%d)" % (warn_upper
, crit_upper
)
132 def discover_azure_virtualmachines_summary(parsed
):
137 def check_azure_virtualmachines_summary(_summary
, params
, parsed
):
139 fixed_resources_list
= sorted(parsed
.values())
140 provisionings
= [_azure_vms_get_status(r
, 'ProvisioningState')[0] for r
in fixed_resources_list
]
141 powers
= [_azure_vms_get_status(r
, 'PowerState')[0] for r
in fixed_resources_list
]
142 groups
= [r
.get("group") for r
in fixed_resources_list
]
144 levels_provisioning
= params
.get("levels_provisioning", {})
146 for prov_state
in sorted(set(provisionings
+ _AZURE_VM_STATES_PROV
.keys())):
147 count
= provisionings
.count(prov_state
)
148 prov_state_levels
= levels_provisioning
.get(prov_state
, {})
149 state_p
, msg_p
= _azure_vms_check_levels(count
, prov_state
, prov_state_levels
)
150 state
= max(state
, state_p
)
151 if state_p
!= 0 or count
:
153 yield state
, "Provisioning: %s" % ' / '.join(txt
)
155 levels_power
= params
.get("levels_power", {})
157 for pow_state
in sorted(set(powers
+ _AZURE_VM_STATES_POWER
.keys())):
158 count
= powers
.count(pow_state
)
159 pow_state_levels
= levels_power
.get(pow_state
, {})
160 state_p
, msg_p
= _azure_vms_check_levels(count
, pow_state
, pow_state_levels
)
161 state
= max(state
, state_p
)
162 if state_p
!= 0 or count
:
164 yield state
, "Power states: %s" % ' / '.join(txt
)
166 unique_groups
= sorted(set(groups
))
167 if unique_groups
!= [host_name()]:
168 group_count
= ("%s: %d" % (g
, groups
.count(g
)) for g
in unique_groups
)
169 yield 0, "VMs per group: %s\n" % ' / '.join(group_count
)
172 templ
= "%s: Provisioning %s, VM %s, Resource group: %s\n"
173 names
= (r
.get("name") for r
in fixed_resources_list
)
174 vms
= zip(names
, provisionings
, powers
, groups
)
176 yield 0, templ
% vmach
179 check_info
['azure_virtualmachines.summary'] = {
180 'inventory_function': discover_azure_virtualmachines_summary
,
181 'check_function': check_azure_virtualmachines_summary
,
182 'service_description': "VM Summary",
183 'includes': ['azure.include'],
184 'default_levels_variable': 'levels_azure_virtualmachines_summary',
185 'group': 'azure_vms_summary',