Refactoring: Moved check parameters from unsorted.py to dedicated modules (CMK-1393)
[check_mk.git] / checks / winperf_processor
blob112256e5e1c971f9349743a438d8f56943917bf3
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.
28 def inventory_winperf_util(info):
29 if len(info) <= 1:
30 return None
32 for line in info[1:]:
33 try:
34 if line[0] == '-232':
35 return [(None, {})]
36 except IndexError:
37 pass
40 def check_winperf_util(_no_item, params, info):
41 if not info:
42 return
44 this_time = int(float(info[0][0]))
46 what_map = {"-232": "util", "-96": "user", "-94": "privileged"}
48 winperf_lines = []
49 for line in info[1:]:
50 if line[0] in what_map:
51 winperf_lines.append(line)
53 if not winperf_lines:
54 return
56 wrapped = False
57 matched = False
58 for line in winperf_lines:
59 if line[0] in what_map:
60 matched = True
61 what = what_map[line[0]]
62 # Windows sends one counter for each CPU plus one counter that
63 # sums up to total (called _Total). We only need that last value.
64 ticks = int(line[-2])
65 num_cpus = len(line) - 3
66 try:
67 ticks_per_sec =\
68 get_rate("winperf_util.%s" % what, this_time, ticks, onwrap = RAISE)
69 except MKCounterWrapped:
70 wrapped = True
71 ticks_per_sec = 0
72 # We get the value of the PERF_100NSEC_TIMER_INV here.
73 # This counter type shows the average percentage of active time observed
74 # during the sample interval. This is an inverse counter. Counters of this
75 # type calculate active time by measuring the time that the service was
76 # inactive and then subtracting the percentage of active time from 100 percent.
78 # 1 tick = 100ns, convert to seconds
79 cpusecs_per_sec = ticks_per_sec / 10000000.0
81 if what == "util":
82 used_perc = 100.0 * (1 - cpusecs_per_sec)
83 else:
84 used_perc = 100.0 * cpusecs_per_sec
86 # Due to timeing invariancies the measured level can become > 100%.
87 # This makes users unhappy, so cut it off.
88 if used_perc < 0:
89 used_perc = 0
90 elif used_perc > 100:
91 used_perc = 100
93 if what == "util":
94 cores = []
95 for i in range(num_cpus):
96 core_ticks = int(line[1 + i])
97 try:
98 core_ticks_per_sec = get_rate(
99 "winperf_util.core%d.util" % i, this_time, core_ticks, onwrap=RAISE)
100 except MKCounterWrapped:
101 wrapped = True
102 core_ticks_per_sec = 0
103 core_cpusecs_per_sec = core_ticks_per_sec / 10000000.0
104 core_used_perc = 100.0 * (1 - core_cpusecs_per_sec)
105 # clamp percentage to the range 0-100
106 core_used_perc = min(100.0, max(0.0, core_used_perc))
107 cores.append(("core%d" % i, core_used_perc))
109 if not wrapped:
110 for status, infotext, perfdata in check_cpu_util(used_perc, params, this_time,
111 cores):
112 if perfdata and perfdata[0][0] == "util":
113 perfdata[0] = perfdata[0][:5] + (num_cpus,)
114 yield status, infotext, perfdata
115 elif what == "user":
116 yield 0, "%s perc: %.1f %%" % (what, used_perc), [(what, used_perc)]
117 else: # privileged
118 yield 0, "%s perc: %.1f %%" % (what, used_perc)
120 if not matched:
121 return
123 if wrapped:
124 # all counters initialized, NOW we can raise the exception
125 raise MKCounterWrapped("Counter wrap, skipping checks this time")
127 yield 0, "%d CPUs" % num_cpus
130 check_info["winperf_processor.util"] = {
131 'check_function': check_winperf_util,
132 'inventory_function': inventory_winperf_util,
133 'service_description': 'CPU utilization',
134 'has_perfdata': True,
135 'handle_real_time_checks': True,
136 'group': 'cpu_utilization_os',
137 'default_levels_variable': 'winperf_cpu_default_levels',
138 'includes': ["cpu_util.include", "winperf.include"],