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 #ifndef ZABBIX_VMWARE_H
20 #define ZABBIX_VMWARE_H
21 
22 #include "common.h"
23 #include "threads.h"
24 
25 /* the vmware service state */
26 #define ZBX_VMWARE_STATE_NEW		0x001
27 #define ZBX_VMWARE_STATE_READY		0x002
28 #define ZBX_VMWARE_STATE_FAILED		0x004
29 
30 #define ZBX_VMWARE_STATE_MASK		0x0FF
31 
32 #define ZBX_VMWARE_STATE_UPDATING	0x100
33 #define ZBX_VMWARE_STATE_UPDATING_PERF	0x200
34 #define ZBX_VMWARE_STATE_REMOVING	0x400
35 
36 #define ZBX_VMWARE_STATE_BUSY		(ZBX_VMWARE_STATE_UPDATING | ZBX_VMWARE_STATE_UPDATING_PERF \
37 							| ZBX_VMWARE_STATE_REMOVING)
38 
39 /* the vmware performance counter state */
40 #define ZBX_VMWARE_COUNTER_NEW		0x00
41 #define ZBX_VMWARE_COUNTER_READY	0x01
42 #define ZBX_VMWARE_COUNTER_UPDATING	0x10
43 
44 #define ZBX_VMWARE_EVENT_KEY_UNINITIALIZED	__UINT64_C(0xffffffffffffffff)
45 
46 /* performance counter data */
47 typedef struct
48 {
49 	/* the counter id */
50 	zbx_uint64_t		counterid;
51 
52 	/* the counter values for various instances */
53 	/*    pair->first  - instance               */
54 	/*    pair->second - value                  */
55 	zbx_vector_ptr_pair_t	values;
56 
57 	/* the counter state, see ZBX_VMAWRE_COUNTER_* defines */
58 	unsigned char		state;
59 }
60 zbx_vmware_perf_counter_t;
61 
62 /* an entity monitored with performance counters */
63 typedef struct
64 {
65 	/* entity type: HostSystem or VirtualMachine */
66 	char			*type;
67 
68 	/* entity id */
69 	char			*id;
70 
71 	/* the performance counter refresh rate */
72 	int			refresh;
73 
74 	/* timestamp when the entity was queried last time */
75 	int			last_seen;
76 
77 	/* the performance counters to monitor */
78 	zbx_vector_ptr_t	counters;
79 
80 	/* the performance counter query instance name */
81 	char			*query_instance;
82 
83 	/* error information */
84 	char			*error;
85 }
86 zbx_vmware_perf_entity_t;
87 
88 typedef struct
89 {
90 	char		*name;
91 	char		*uuid;
92 	char		*id;
93 	zbx_uint64_t	capacity;
94 	zbx_uint64_t	free_space;
95 	zbx_uint64_t	uncommitted;
96 
97 }
98 zbx_vmware_datastore_t;
99 
100 #define ZBX_VMWARE_DEV_TYPE_NIC		1
101 #define ZBX_VMWARE_DEV_TYPE_DISK	2
102 typedef struct
103 {
104 	int	type;
105 	char	*instance;
106 	char	*label;
107 }
108 zbx_vmware_dev_t;
109 
110 /* file system data */
111 typedef struct
112 {
113 	char		*path;
114 	zbx_uint64_t	capacity;
115 	zbx_uint64_t	free_space;
116 }
117 zbx_vmware_fs_t;
118 
119 /* the vmware virtual machine data */
120 typedef struct
121 {
122 	char			*uuid;
123 	char			*id;
124 	char			**props;
125 	zbx_vector_ptr_t	devs;
126 	zbx_vector_ptr_t	file_systems;
127 }
128 zbx_vmware_vm_t;
129 
130 /* the vmware hypervisor data */
131 typedef struct
132 {
133 	char			*uuid;
134 	char			*id;
135 	char			*clusterid;
136 	char			**props;
137 	zbx_vector_ptr_t	datastores;
138 	zbx_vector_ptr_t	vms;
139 }
140 zbx_vmware_hv_t;
141 
142 /* index virtual machines by uuids */
143 typedef struct
144 {
145 	zbx_vmware_vm_t	*vm;
146 	zbx_vmware_hv_t	*hv;
147 }
148 zbx_vmware_vm_index_t;
149 
150 /* the vmware cluster data */
151 typedef struct
152 {
153 	char	*id;
154 	char	*name;
155 	char	*status;
156 }
157 zbx_vmware_cluster_t;
158 
159 /* the vmware event data */
160 typedef struct
161 {
162 	zbx_uint64_t	key;		/* event's key, used to fill logeventid */
163 	char		*message;	/* event's fullFormattedMessage */
164 	int		timestamp;	/* event's time stamp */
165 }
166 zbx_vmware_event_t;
167 
168 /* the vmware service data object */
169 typedef struct
170 {
171 	char	*error;
172 
173 	zbx_hashset_t		hvs;
174 	zbx_hashset_t		vms_index;
175 	zbx_vector_ptr_t	clusters;
176 	zbx_vector_ptr_t	events;			/* vector of pointers to zbx_vmware_event_t structures */
177 	int			max_query_metrics;	/* max count of Datastore perfCounters in one request */
178 }
179 zbx_vmware_data_t;
180 
181 /* the vmware service data */
182 typedef struct
183 {
184 	char			*url;
185 	char			*username;
186 	char			*password;
187 
188 	/* the service type - vCenter or vSphere */
189 	unsigned char		type;
190 
191 	/* the service state - see ZBX_VMWARE_STATE_* defines */
192 	int			state;
193 
194 	int			lastcheck;
195 	int			lastperfcheck;
196 
197 	/* The last vmware service access time. If a service is not accessed for a day it is removed */
198 	int			lastaccess;
199 
200 	/* the vmware service instance contents */
201 	char			*contents;
202 
203 	/* the performance counters */
204 	zbx_hashset_t		counters;
205 
206 	/* list of entities to monitor with performance counters */
207 	zbx_hashset_t		entities;
208 
209 	/* the service data object that is swapped with a new one during service update */
210 	zbx_vmware_data_t	*data;
211 
212 	/* lastlogsize when vmware.eventlog[] item was polled last time */
213 	zbx_uint64_t		eventlog_last_key;
214 }
215 zbx_vmware_service_t;
216 
217 #define ZBX_VMWARE_PERF_INTERVAL_UNKNOWN	0
218 #define ZBX_VMWARE_PERF_INTERVAL_NONE		-1
219 
220 /* the vmware collector data */
221 typedef struct
222 {
223 	zbx_vector_ptr_t	services;
224 	zbx_hashset_t		strpool;
225 }
226 zbx_vmware_t;
227 
228 /* the vmware collector statistics */
229 typedef struct
230 {
231 	zbx_uint64_t	memory_used;
232 	zbx_uint64_t	memory_total;
233 }
234 zbx_vmware_stats_t;
235 
236 ZBX_THREAD_ENTRY(vmware_thread, args);
237 
238 void	zbx_vmware_init(void);
239 void	zbx_vmware_destroy(void);
240 
241 void	zbx_vmware_lock(void);
242 void	zbx_vmware_unlock(void);
243 
244 int	zbx_vmware_get_statistics(zbx_vmware_stats_t *stats);
245 
246 #if defined(HAVE_LIBXML2) && defined(HAVE_LIBCURL)
247 
248 zbx_vmware_service_t	*zbx_vmware_get_service(const char* url, const char* username, const char* password);
249 
250 int	zbx_vmware_service_get_counterid(zbx_vmware_service_t *service, const char *path, zbx_uint64_t *counterid);
251 int	zbx_vmware_service_add_perf_counter(zbx_vmware_service_t *service, const char *type, const char *id,
252 		zbx_uint64_t counterid, const char *instance);
253 zbx_vmware_perf_entity_t	*zbx_vmware_service_get_perf_entity(zbx_vmware_service_t *service, const char *type,
254 		const char *id);
255 
256 #define ZBX_VM_NONAME_XML	"noname.xml"
257 
258 #define ZBX_XPATH_VM_QUICKSTATS(property)								\
259 	"/*/*/*/*/*/*[local-name()='propSet'][*[local-name()='name'][text()='summary']]"		\
260 		"/*[local-name()='val']/*[local-name()='quickStats']/*[local-name()='" property "']"
261 
262 #define ZBX_XPATH_VM_RUNTIME(property)									\
263 	"/*/*/*/*/*/*[local-name()='propSet'][*[local-name()='name'][text()='summary']]"		\
264 		"/*[local-name()='val']/*[local-name()='runtime']/*[local-name()='" property "']"
265 
266 #define ZBX_XPATH_VM_CONFIG(property)									\
267 	"/*/*/*/*/*/*[local-name()='propSet'][*[local-name()='name'][text()='summary']]"		\
268 		"/*[local-name()='val']/*[local-name()='config']/*[local-name()='" property "']"
269 
270 #define ZBX_XPATH_VM_STORAGE(property)									\
271 	"/*/*/*/*/*/*[local-name()='propSet'][*[local-name()='name'][text()='summary']]"		\
272 		"/*[local-name()='val']/*[local-name()='storage']/*[local-name()='" property "']"
273 
274 #define ZBX_XPATH_VM_HARDWARE(property)									\
275 	"/*/*/*/*/*/*[local-name()='propSet'][*[local-name()='name'][text()='config.hardware']]"	\
276 		"/*[local-name()='val']/*[local-name()='" property "']"
277 
278 #define ZBX_XPATH_VM_GUESTDISKS()									\
279 	"/*/*/*/*/*/*[local-name()='propSet'][*[local-name()='name'][text()='guest.disk']]"		\
280 	"/*/*[local-name()='GuestDiskInfo']"
281 
282 #define ZBX_XPATH_VM_UUID()										\
283 	"/*/*/*/*/*/*[local-name()='propSet'][*[local-name()='name'][text()='config.uuid']]"		\
284 		"/*[local-name()='val']"
285 
286 #define ZBX_XPATH_VM_INSTANCE_UUID()									\
287 	"/*/*/*/*/*/*[local-name()='propSet'][*[local-name()='name'][text()='config.instanceUuid']]"	\
288 		"/*[local-name()='val']"
289 
290 #define ZBX_XPATH_HV_QUICKSTATS(property)								\
291 	"/*/*/*/*/*[local-name()='propSet'][*[local-name()='name'][text()='summary.quickStats']]"	\
292 		"/*[local-name()='val']/*[local-name()='" property "']"
293 
294 #define ZBX_XPATH_HV_CONFIG(property)									\
295 	"/*/*/*/*/*[local-name()='propSet'][*[local-name()='name'][text()='summary.config']]"		\
296 		"/*[local-name()='val']/*[local-name()='" property "']"
297 
298 #define ZBX_XPATH_HV_CONFIG_PRODUCT(property)								\
299 	"/*/*/*/*/*[local-name()='propSet'][*[local-name()='name'][text()='summary.config']]"		\
300 		"/*[local-name()='val']/*[local-name()='product']"					\
301 		"/*[local-name()='" property "']"
302 
303 #define ZBX_XPATH_HV_HARDWARE(property)									\
304 	"/*/*/*/*/*[local-name()='propSet'][*[local-name()='name'][text()='summary.hardware']]"		\
305 		"/*[local-name()='val']/*[local-name()='" property "']"
306 
307 #define ZBX_XPATH_HV_SENSOR_STATUS(sensor)								\
308 	"/*/*/*/*/*[local-name()='propSet'][*[local-name()='name']"					\
309 		"[text()='runtime.healthSystemRuntime.systemHealthInfo']]"				\
310 		"/*[local-name()='val']/*[local-name()='numericSensorInfo']"				\
311 		"[*[local-name()='name'][text()='" sensor "']]"						\
312 		"/*[local-name()='healthState']/*[local-name()='key']"
313 
314 #define ZBX_XPATH_HV_STATUS()										\
315 	"/*/*/*/*/*[local-name()='propSet'][*[local-name()='name'][text()='overallStatus']]"		\
316 		"/*[local-name()='val']"
317 
318 #define ZBX_XPATH_VMWARE_ABOUT(property)								\
319 	"/*/*/*/*/*[local-name()='about']/*[local-name()='" property "']"
320 
321 #	define ZBX_XPATH_NN(NN)			"*[local-name()='" NN "']"
322 #	define ZBX_XPATH_LN(LN)			"/" ZBX_XPATH_NN(LN)
323 #	define ZBX_XPATH_LN1(LN1)		"/" ZBX_XPATH_LN(LN1)
324 #	define ZBX_XPATH_LN2(LN1, LN2)		"/" ZBX_XPATH_LN(LN1) ZBX_XPATH_LN(LN2)
325 #	define ZBX_XPATH_LN3(LN1, LN2, LN3)	"/" ZBX_XPATH_LN(LN1) ZBX_XPATH_LN(LN2) ZBX_XPATH_LN(LN3)
326 
327 char	*zbx_xml_read_value(const char *data, const char *xpath);
328 int	zbx_xml_read_values(const char *data, const char *xpath, zbx_vector_str_t *values);
329 int	zbx_xml_try_read_value(const char *data, const char *xpath, char **value, char **error);
330 
331 
332 /* hypervisor properties */
333 #define ZBX_VMWARE_HVPROP_OVERALL_CPU_USAGE		0
334 #define ZBX_VMWARE_HVPROP_FULL_NAME			1
335 #define ZBX_VMWARE_HVPROP_HW_NUM_CPU_CORES		2
336 #define ZBX_VMWARE_HVPROP_HW_CPU_MHZ			3
337 #define ZBX_VMWARE_HVPROP_HW_CPU_MODEL			4
338 #define ZBX_VMWARE_HVPROP_HW_NUM_CPU_THREADS		5
339 #define ZBX_VMWARE_HVPROP_HW_MEMORY_SIZE		6
340 #define ZBX_VMWARE_HVPROP_HW_MODEL			7
341 #define ZBX_VMWARE_HVPROP_HW_UUID			8
342 #define ZBX_VMWARE_HVPROP_HW_VENDOR			9
343 #define ZBX_VMWARE_HVPROP_MEMORY_USED			10
344 #define ZBX_VMWARE_HVPROP_HEALTH_STATE			11
345 #define ZBX_VMWARE_HVPROP_UPTIME			12
346 #define ZBX_VMWARE_HVPROP_VERSION			13
347 #define ZBX_VMWARE_HVPROP_NAME				14
348 #define ZBX_VMWARE_HVPROP_STATUS			15
349 
350 #define ZBX_VMWARE_HVPROPS_NUM				16
351 
352 /* virtual machine properties */
353 #define ZBX_VMWARE_VMPROP_CPU_NUM			0
354 #define ZBX_VMWARE_VMPROP_CPU_USAGE			1
355 #define ZBX_VMWARE_VMPROP_NAME				2
356 #define ZBX_VMWARE_VMPROP_MEMORY_SIZE			3
357 #define ZBX_VMWARE_VMPROP_MEMORY_SIZE_BALLOONED		4
358 #define ZBX_VMWARE_VMPROP_MEMORY_SIZE_COMPRESSED	5
359 #define ZBX_VMWARE_VMPROP_MEMORY_SIZE_SWAPPED		6
360 #define ZBX_VMWARE_VMPROP_MEMORY_SIZE_USAGE_GUEST	7
361 #define ZBX_VMWARE_VMPROP_MEMORY_SIZE_USAGE_HOST	8
362 #define ZBX_VMWARE_VMPROP_MEMORY_SIZE_PRIVATE		9
363 #define ZBX_VMWARE_VMPROP_MEMORY_SIZE_SHARED		10
364 #define ZBX_VMWARE_VMPROP_POWER_STATE			11
365 #define ZBX_VMWARE_VMPROP_STORAGE_COMMITED		12
366 #define ZBX_VMWARE_VMPROP_STORAGE_UNSHARED		13
367 #define ZBX_VMWARE_VMPROP_STORAGE_UNCOMMITTED		14
368 #define ZBX_VMWARE_VMPROP_UPTIME			15
369 
370 #define ZBX_VMWARE_VMPROPS_NUM				16
371 
372 /* vmware service types */
373 #define ZBX_VMWARE_TYPE_UNKNOWN	0
374 #define ZBX_VMWARE_TYPE_VSPHERE	1
375 #define ZBX_VMWARE_TYPE_VCENTER	2
376 
377 #endif	/* defined(HAVE_LIBXML2) && defined(HAVE_LIBCURL) */
378 
379 #endif	/* ZABBIX_VMWARE_H */
380