Refactoring: Changed all check parameters starting with an 'o' to the new rulespec...
[check_mk.git] / checks / azure_agent_info
blob69c852c5d67dcbe910fe4b568a982ca4baaf3735
1 #!/usr/bin/python
2 # -*- encoding: utf-8; py-indent-offset: 4 -*-
3 # +------------------------------------------------------------------+
4 # | ____ _ _ __ __ _ __ |
5 # | / ___| |__ ___ ___| | __ | \/ | |/ / |
6 # | | | | '_ \ / _ \/ __| |/ / | |\/| | ' / |
7 # | | |___| | | | __/ (__| < | | | | . \ |
8 # | \____|_| |_|\___|\___|_|\_\___|_| |_|_|\_\ |
9 # | |
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.
26 import json
28 factory_settings['azure_agent_info_levels'] = {
29 'warning_levels': (1, 10),
30 'exception_levels': (1, 1),
31 'remaining_reads_levels_lower': (6000, 3000),
32 'remaining_reads_unknown_state': 1,
36 def _update_remaining_reads(parsed, value):
37 try:
38 value = int(value)
39 except ValueError:
40 pass
42 current = parsed.get('remaining-reads', '_some_string')
43 if isinstance(current, str):
44 parsed['remaining-reads'] = value
45 return
46 parsed['remaining-reads'] = min(current, value)
49 def parse_azure_agent_info(info):
51 parsed = {}
52 for row in info:
53 key = row[0]
54 value = _AZURE_AGENT_SEPARATOR.join(row[1:])
56 if key == 'remaining-reads':
57 _update_remaining_reads(parsed, value)
58 continue
60 try:
61 value = json.loads(value)
62 except ValueError:
63 pass
65 if key == 'issue':
66 issues = parsed.setdefault('issues', {})
67 issues.setdefault(value['type'], []).append(value)
68 continue
70 parsed.setdefault(key, []).append(value)
72 return parsed
75 def discovery_azure_agent_info(_parsed):
76 yield None, {}
79 def check_azure_agent_info(_no_item, params, parsed):
81 for status, text in parsed.get('agent-bailout', []):
82 yield status, text
84 reads = parsed.get('remaining-reads')
85 # this is only reported for the Datasource Host, so None
86 # is ignored.
87 if reads is not None:
88 state, txt = 0, "Remaining API reads: %s" % reads
89 if not isinstance(reads, int):
90 yield params['remaining_reads_unknown_state'], txt
91 else:
92 warn, crit = params.get('remaining_reads_levels_lower', (None, None))
93 if crit is not None and reads <= crit:
94 state = 2
95 elif warn is not None and reads <= warn:
96 state = 1
97 yield state, txt, [('remaining_reads', reads, warn, crit, 0, 15000)]
99 groups = parsed.get('monitored-groups')
100 if groups is not None:
101 yield 0, "Monitored groups: %s" % ', '.join(groups[0])
103 issues = parsed.get('issues', {})
104 for type_ in ('warning', 'exception'):
105 count = len(issues.get(type_, ()))
106 state, txt = 0, "%d %ss" % (count, type_)
107 warn, crit = params.get('%s_levels' % type_, (None, None))
108 if crit is not None and count >= crit:
109 state = 2
110 elif warn is not None and count >= warn:
111 state = 1
112 yield state, txt
114 for i in sorted(issues.get('exception', []), key=lambda x: x["msg"]):
115 yield 0, "\nIssue in %s: Exception: %s (!!)" % (i["issued_by"], i["msg"])
116 for i in sorted(issues.get('warning', []), key=lambda x: x["msg"]):
117 yield 0, "\nIssue in %s: Warning: %s (!)" % (i["issued_by"], i["msg"])
118 for i in sorted(issues.get('info', []), key=lambda x: x["msg"]):
119 yield 0, "\nIssue in %s: Info: %s" % (i["issued_by"], i["msg"])
122 check_info['azure_agent_info'] = {
123 'parse_function': parse_azure_agent_info,
124 'inventory_function': discovery_azure_agent_info,
125 'check_function': check_azure_agent_info,
126 'service_description': "Azure Agent Info",
127 'default_levels_variable': "azure_agent_info_levels",
128 'has_perfdata': True,
129 'group': "azure_agent_info",
130 'includes': ['azure.include'],