1 /*
2 ** Zabbix
3 ** Copyright (C) 2001-2021 Zabbix SIA
4 **
5 ** This program is free software; you can redistribute it and/or modify
6 ** it under the terms of the GNU General Public License as published by
7 ** the Free Software Foundation; either version 2 of the License, or
8 ** (at your option) any later version.
9 **
10 ** This program is distributed in the hope that it will be useful,
11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ** GNU General Public License for more details.
14 **
15 ** You should have received a copy of the GNU General Public License
16 ** along with this program; if not, write to the Free Software
17 ** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18 **/
19 
20 #ifndef ZABBIX_PERFMON_H
21 #define ZABBIX_PERFMON_H
22 
23 #if !defined(_WINDOWS) && !defined(__MINGW32__)
24 #	error "This module is only available for Windows OS"
25 #endif
26 
27 /* this struct must be only modified along with mapping builtin_counter_ref[] in perfmon.c */
28 typedef enum
29 {
30 	PCI_PROCESSOR_QUEUE_LENGTH = 0,
31 	PCI_SYSTEM_UP_TIME,
32 	PCI_PROCESSOR_TIME,
33 	PCI_INFORMATION_PROCESSOR_TIME,
34 	PCI_TOTAL_SESSIONS,
35 	PCI_MAX_INDEX = PCI_TOTAL_SESSIONS
36 }
37 zbx_builtin_counter_ref_t;
38 
39 typedef enum
40 {
41 	PERF_COUNTER_NOTSUPPORTED = 0,
42 	PERF_COUNTER_INITIALIZED,
43 	PERF_COUNTER_GET_SECOND_VALUE,	/* waiting for the second raw value (needed for some, e.g. rate, counters) */
44 	PERF_COUNTER_ACTIVE
45 }
46 zbx_perf_counter_status_t;
47 
48 typedef enum
49 {
50 	PERF_COUNTER_LANG_DEFAULT = 0,
51 	PERF_COUNTER_LANG_EN
52 }
53 zbx_perf_counter_lang_t;
54 
55 typedef struct perf_counter_id
56 {
57 	struct perf_counter_id	*next;
58 	unsigned long		pdhIndex;
59 	wchar_t			name[PDH_MAX_COUNTER_NAME];
60 }
61 zbx_perf_counter_id_t;
62 
63 typedef struct perf_counter_data
64 {
65 	struct perf_counter_data	*next;
66 	char				*name;
67 	char				*counterpath;
68 	int				interval;
69 	zbx_perf_counter_lang_t		lang;
70 	zbx_perf_counter_status_t	status;
71 	HCOUNTER			handle;
72 	PDH_RAW_COUNTER			rawValues[2];	/* rate counters need two raw values */
73 	int				olderRawValue;	/* index of the older of both values */
74 	double				*value_array;	/* a circular buffer of values */
75 	int				value_current;	/* index of the last stored value */
76 	int				value_count;	/* number of values in the array */
77 	double				sum;		/* sum of last value_count values */
78 }
79 zbx_perf_counter_data_t;
80 
81 PDH_STATUS	zbx_PdhMakeCounterPath(const char *function, PDH_COUNTER_PATH_ELEMENTS *cpe, char *counterpath);
82 PDH_STATUS	zbx_PdhOpenQuery(const char *function, PDH_HQUERY query);
83 PDH_STATUS	zbx_PdhAddCounter(const char *function, zbx_perf_counter_data_t *counter, PDH_HQUERY query,
84 		const char *counterpath, zbx_perf_counter_lang_t lang, PDH_HCOUNTER *handle);
85 PDH_STATUS	zbx_PdhCollectQueryData(const char *function, const char *counterpath, PDH_HQUERY query);
86 PDH_STATUS	zbx_PdhGetRawCounterValue(const char *function, const char *counterpath, PDH_HCOUNTER handle, PPDH_RAW_COUNTER value);
87 
88 PDH_STATUS	calculate_counter_value(const char *function, const char *counterpath, zbx_perf_counter_lang_t lang, double *value);
89 wchar_t		*get_counter_name(DWORD pdhIndex);
90 int		check_counter_path(char *counterPath, int convert_from_numeric);
91 int		init_builtin_counter_indexes(void);
92 DWORD 		get_builtin_object_index(zbx_builtin_counter_ref_t counter_ref);
93 DWORD 		get_builtin_counter_index(zbx_builtin_counter_ref_t counter_ref);
94 wchar_t		*get_all_counter_names(HKEY reg_key, wchar_t *reg_value_name);
95 
96 #define	get_builtin_object_name(ctr)	get_counter_name(get_builtin_object_index(ctr))
97 #define	get_builtin_counter_name(ctr)	get_counter_name(get_builtin_counter_index(ctr))
98 
99 #endif /* ZABBIX_PERFMON_H */
100