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 typedef struct
47 {
48 	char		*name;
49 	zbx_uint64_t	value;
50 }
51 zbx_str_uint64_pair_t;
52 
53 ZBX_PTR_VECTOR_DECL(str_uint64_pair, zbx_str_uint64_pair_t)
54 int	zbx_str_uint64_pair_name_compare(const void *p1, const void *p2);
55 
56 /* performance counter data */
57 typedef struct
58 {
59 	/* the counter id */
60 	zbx_uint64_t			counterid;
61 
62 	/* the counter values for various instances */
63 	/*    pair->name  - instance                */
64 	/*    pair->value - value                   */
65 	zbx_vector_str_uint64_pair_t	values;
66 
67 	/* the counter state, see ZBX_VMAWRE_COUNTER_* defines */
68 	unsigned char			state;
69 }
70 zbx_vmware_perf_counter_t;
71 
72 /* an entity monitored with performance counters */
73 typedef struct
74 {
75 	/* entity type: HostSystem or VirtualMachine */
76 	char			*type;
77 
78 	/* entity id */
79 	char			*id;
80 
81 	/* the performance counter refresh rate */
82 	int			refresh;
83 
84 	/* timestamp when the entity was queried last time */
85 	int			last_seen;
86 
87 	/* the performance counters to monitor */
88 	zbx_vector_ptr_t	counters;
89 
90 	/* the performance counter query instance name */
91 	char			*query_instance;
92 
93 	/* error information */
94 	char			*error;
95 }
96 zbx_vmware_perf_entity_t;
97 
98 typedef struct
99 {
100 	zbx_uint64_t	partitionid;
101 	char		*diskname;
102 }
103 zbx_vmware_diskextent_t;
104 
105 ZBX_PTR_VECTOR_DECL(vmware_diskextent, zbx_vmware_diskextent_t *)
106 
107 #define ZBX_VMWARE_DS_NONE		0
108 #define ZBX_VMWARE_DS_MOUNTED		1
109 #define ZBX_VMWARE_DS_ACCESSIBLE	2
110 #define ZBX_VMWARE_DS_READ		4
111 #define ZBX_VMWARE_DS_WRITE		8
112 #define ZBX_VMWARE_DS_READWRITE		(ZBX_VMWARE_DS_READ | ZBX_VMWARE_DS_WRITE)
113 #define ZBX_VMWARE_DS_READ_FILTER	(ZBX_VMWARE_DS_MOUNTED | ZBX_VMWARE_DS_ACCESSIBLE | ZBX_VMWARE_DS_READ)
114 #define ZBX_VMWARE_DS_WRITE_FILTER	(ZBX_VMWARE_DS_MOUNTED | ZBX_VMWARE_DS_ACCESSIBLE | ZBX_VMWARE_DS_READWRITE)
115 
116 typedef struct
117 {
118 	char				*name;
119 	char				*uuid;
120 	char				*id;
121 	zbx_uint64_t			capacity;
122 	zbx_uint64_t			free_space;
123 	zbx_uint64_t			uncommitted;
124 	zbx_vector_str_uint64_pair_t	hv_uuids_access;
125 	zbx_vector_vmware_diskextent_t	diskextents;
126 }
127 zbx_vmware_datastore_t;
128 
129 int	vmware_ds_name_compare(const void *d1, const void *d2);
130 ZBX_PTR_VECTOR_DECL(vmware_datastore, zbx_vmware_datastore_t *)
131 
132 typedef struct
133 {
134 	zbx_uint64_t	partitionid;
135 	int		multipath_total;
136 	int		multipath_active;
137 }
138 zbx_vmware_hvdisk_t;
139 
140 ZBX_VECTOR_DECL(vmware_hvdisk, zbx_vmware_hvdisk_t)
141 
142 typedef struct
143 {
144 	char				*name;
145 	zbx_vector_vmware_hvdisk_t	hvdisks;
146 }
147 zbx_vmware_dsname_t;
148 
149 int	vmware_dsname_compare(const void *d1, const void *d2);
150 ZBX_PTR_VECTOR_DECL(vmware_dsname, zbx_vmware_dsname_t *)
151 
152 typedef struct
153 {
154 	char			*name;
155 	char			*id;
156 }
157 zbx_vmware_datacenter_t;
158 
159 int	vmware_dc_name_compare(const void *d1, const void *d2);
160 ZBX_PTR_VECTOR_DECL(vmware_datacenter, zbx_vmware_datacenter_t *)
161 
162 #define ZBX_VMWARE_DEV_TYPE_NIC		1
163 #define ZBX_VMWARE_DEV_TYPE_DISK	2
164 typedef struct
165 {
166 	int	type;
167 	char	*instance;
168 	char	*label;
169 }
170 zbx_vmware_dev_t;
171 
172 /* file system data */
173 typedef struct
174 {
175 	char		*path;
176 	zbx_uint64_t	capacity;
177 	zbx_uint64_t	free_space;
178 }
179 zbx_vmware_fs_t;
180 
181 /* the vmware virtual machine data */
182 typedef struct
183 {
184 	char			*uuid;
185 	char			*id;
186 	char			**props;
187 	zbx_vector_ptr_t	devs;
188 	zbx_vector_ptr_t	file_systems;
189 }
190 zbx_vmware_vm_t;
191 
192 /* the vmware hypervisor data */
193 typedef struct
194 {
195 	char				*uuid;
196 	char				*id;
197 	char				*clusterid;
198 	char				*datacenter_name;
199 	char				*parent_name;
200 	char				*parent_type;
201 	char				*ip;
202 	char				**props;
203 	zbx_vector_vmware_dsname_t	dsnames;
204 	zbx_vector_ptr_t		vms;
205 }
206 zbx_vmware_hv_t;
207 
208 /* index virtual machines by uuids */
209 typedef struct
210 {
211 	zbx_vmware_vm_t	*vm;
212 	zbx_vmware_hv_t	*hv;
213 }
214 zbx_vmware_vm_index_t;
215 
216 /* the vmware cluster data */
217 typedef struct
218 {
219 	char	*id;
220 	char	*name;
221 	char	*status;
222 }
223 zbx_vmware_cluster_t;
224 
225 /* the vmware eventlog state */
226 typedef struct
227 {
228 	zbx_uint64_t	last_key;	/* lastlogsize when vmware.eventlog[] item was polled last time */
229 	unsigned char	skip_old;	/* skip old event log records */
230 	unsigned char	oom;		/* no enough memory to store new events */
231 	zbx_uint64_t	req_sz;		/* memory size required to store events */
232 }
233 zbx_vmware_eventlog_state_t;
234 
235 /* the vmware event data */
236 typedef struct
237 {
238 	zbx_uint64_t	key;		/* event's key, used to fill logeventid */
239 	char		*message;	/* event's fullFormattedMessage */
240 	int		timestamp;	/* event's time stamp */
241 }
242 zbx_vmware_event_t;
243 
244 /* the vmware service data object */
245 typedef struct
246 {
247 	char	*error;
248 
249 	zbx_hashset_t			hvs;
250 	zbx_hashset_t			vms_index;
251 	zbx_vector_ptr_t		clusters;
252 	zbx_vector_ptr_t		events;			/* vector of pointers to zbx_vmware_event_t structures */
253 	int				max_query_metrics;	/* max count of Datastore perfCounters in one request */
254 	zbx_vector_vmware_datastore_t	datastores;
255 	zbx_vector_vmware_datacenter_t	datacenters;
256 }
257 zbx_vmware_data_t;
258 
259 /* the vmware service data */
260 typedef struct
261 {
262 	char				*url;
263 	char				*username;
264 	char				*password;
265 
266 	/* the service type - vCenter or vSphere */
267 	unsigned char			type;
268 
269 	/* the service state - see ZBX_VMWARE_STATE_* defines */
270 	int				state;
271 
272 	int				lastcheck;
273 	int				lastperfcheck;
274 
275 	/* The last vmware service access time. If a service is not accessed for a day it is removed */
276 	int				lastaccess;
277 
278 	/* the vmware service instance version */
279 	char				*version;
280 
281 	/* the vmware service instance version numeric */
282 	unsigned short			major_version;
283 	unsigned short			minor_version;
284 
285 	/* the vmware service instance fullname */
286 	char				*fullname;
287 
288 	/* the performance counters */
289 	zbx_hashset_t			counters;
290 
291 	/* list of entities to monitor with performance counters */
292 	zbx_hashset_t			entities;
293 
294 	/* the service data object that is swapped with a new one during service update */
295 	zbx_vmware_data_t		*data;
296 
297 	/* lastlogsize when vmware.eventlog[] item was polled last time and skip old flag*/
298 	zbx_vmware_eventlog_state_t	eventlog;
299 }
300 zbx_vmware_service_t;
301 
302 #define ZBX_VMWARE_PERF_INTERVAL_UNKNOWN	0
303 #define ZBX_VMWARE_PERF_INTERVAL_NONE		-1
304 
305 /* the vmware collector data */
306 typedef struct
307 {
308 	zbx_vector_ptr_t	services;
309 	zbx_hashset_t		strpool;
310 	zbx_uint64_t		strpool_sz;
311 }
312 zbx_vmware_t;
313 
314 /* the vmware collector statistics */
315 typedef struct
316 {
317 	zbx_uint64_t	memory_used;
318 	zbx_uint64_t	memory_total;
319 }
320 zbx_vmware_stats_t;
321 
322 ZBX_THREAD_ENTRY(vmware_thread, args);
323 
324 int	zbx_vmware_init(char **error);
325 void	zbx_vmware_destroy(void);
326 
327 void	zbx_vmware_lock(void);
328 void	zbx_vmware_unlock(void);
329 
330 int	zbx_vmware_get_statistics(zbx_vmware_stats_t *stats);
331 
332 #if defined(HAVE_LIBXML2) && defined(HAVE_LIBCURL)
333 
334 zbx_vmware_service_t	*zbx_vmware_get_service(const char* url, const char* username, const char* password);
335 
336 int	zbx_vmware_service_get_counterid(zbx_vmware_service_t *service, const char *path, zbx_uint64_t *counterid,
337 		int *unit);
338 int	zbx_vmware_service_add_perf_counter(zbx_vmware_service_t *service, const char *type, const char *id,
339 		zbx_uint64_t counterid, const char *instance);
340 zbx_vmware_perf_entity_t	*zbx_vmware_service_get_perf_entity(zbx_vmware_service_t *service, const char *type,
341 		const char *id);
342 
343 /* hypervisor properties */
344 #define ZBX_VMWARE_HVPROP_OVERALL_CPU_USAGE		0
345 #define ZBX_VMWARE_HVPROP_FULL_NAME			1
346 #define ZBX_VMWARE_HVPROP_HW_NUM_CPU_CORES		2
347 #define ZBX_VMWARE_HVPROP_HW_CPU_MHZ			3
348 #define ZBX_VMWARE_HVPROP_HW_CPU_MODEL			4
349 #define ZBX_VMWARE_HVPROP_HW_NUM_CPU_THREADS		5
350 #define ZBX_VMWARE_HVPROP_HW_MEMORY_SIZE		6
351 #define ZBX_VMWARE_HVPROP_HW_MODEL			7
352 #define ZBX_VMWARE_HVPROP_HW_UUID			8
353 #define ZBX_VMWARE_HVPROP_HW_VENDOR			9
354 #define ZBX_VMWARE_HVPROP_MEMORY_USED			10
355 #define ZBX_VMWARE_HVPROP_HEALTH_STATE			11
356 #define ZBX_VMWARE_HVPROP_UPTIME			12
357 #define ZBX_VMWARE_HVPROP_VERSION			13
358 #define ZBX_VMWARE_HVPROP_NAME				14
359 #define ZBX_VMWARE_HVPROP_STATUS			15
360 #define ZBX_VMWARE_HVPROP_MAINTENANCE			16
361 #define ZBX_VMWARE_HVPROP_SENSOR			17
362 #define ZBX_VMWARE_HVPROP_NET_NAME			18
363 
364 #define ZBX_VMWARE_HVPROPS_NUM				19
365 
366 /* virtual machine properties */
367 #define ZBX_VMWARE_VMPROP_CPU_NUM			0
368 #define ZBX_VMWARE_VMPROP_CPU_USAGE			1
369 #define ZBX_VMWARE_VMPROP_NAME				2
370 #define ZBX_VMWARE_VMPROP_MEMORY_SIZE			3
371 #define ZBX_VMWARE_VMPROP_MEMORY_SIZE_BALLOONED		4
372 #define ZBX_VMWARE_VMPROP_MEMORY_SIZE_COMPRESSED	5
373 #define ZBX_VMWARE_VMPROP_MEMORY_SIZE_SWAPPED		6
374 #define ZBX_VMWARE_VMPROP_MEMORY_SIZE_USAGE_GUEST	7
375 #define ZBX_VMWARE_VMPROP_MEMORY_SIZE_USAGE_HOST	8
376 #define ZBX_VMWARE_VMPROP_MEMORY_SIZE_PRIVATE		9
377 #define ZBX_VMWARE_VMPROP_MEMORY_SIZE_SHARED		10
378 #define ZBX_VMWARE_VMPROP_POWER_STATE			11
379 #define ZBX_VMWARE_VMPROP_STORAGE_COMMITED		12
380 #define ZBX_VMWARE_VMPROP_STORAGE_UNSHARED		13
381 #define ZBX_VMWARE_VMPROP_STORAGE_UNCOMMITTED		14
382 #define ZBX_VMWARE_VMPROP_UPTIME			15
383 #define ZBX_VMWARE_VMPROP_IPADDRESS			16
384 #define ZBX_VMWARE_VMPROP_GUESTHOSTNAME			17
385 #define ZBX_VMWARE_VMPROP_GUESTFAMILY			18
386 #define ZBX_VMWARE_VMPROP_GUESTFULLNAME			19
387 #define ZBX_VMWARE_VMPROP_FOLDER			20
388 
389 #define ZBX_VMWARE_VMPROPS_NUM				21
390 
391 /* vmware service types */
392 #define ZBX_VMWARE_TYPE_UNKNOWN	0
393 #define ZBX_VMWARE_TYPE_VSPHERE	1
394 #define ZBX_VMWARE_TYPE_VCENTER	2
395 
396 #define ZBX_VMWARE_SOAP_DATACENTER	"Datacenter"
397 #define ZBX_VMWARE_SOAP_FOLDER		"Folder"
398 #define ZBX_VMWARE_SOAP_CLUSTER		"ClusterComputeResource"
399 #define ZBX_VMWARE_SOAP_DEFAULT		"VMware"
400 #define ZBX_VMWARE_SOAP_DS		"Datastore"
401 #define ZBX_VMWARE_SOAP_HV		"HostSystem"
402 #define ZBX_VMWARE_SOAP_VM		"VirtualMachine"
403 
404 /* Indicates the unit of measure represented by a counter or statistical value */
405 #define ZBX_VMWARE_UNIT_UNDEFINED		0
406 #define ZBX_VMWARE_UNIT_JOULE			1
407 #define ZBX_VMWARE_UNIT_KILOBYTES		2
408 #define ZBX_VMWARE_UNIT_KILOBYTESPERSECOND	3
409 #define ZBX_VMWARE_UNIT_MEGABYTES		4
410 #define ZBX_VMWARE_UNIT_MEGABYTESPERSECOND	5
411 #define ZBX_VMWARE_UNIT_MEGAHERTZ		6
412 #define ZBX_VMWARE_UNIT_MICROSECOND		7
413 #define ZBX_VMWARE_UNIT_MILLISECOND		8
414 #define ZBX_VMWARE_UNIT_NUMBER			9
415 #define ZBX_VMWARE_UNIT_PERCENT			10
416 #define ZBX_VMWARE_UNIT_SECOND			11
417 #define ZBX_VMWARE_UNIT_TERABYTES		12
418 #define ZBX_VMWARE_UNIT_WATT			13
419 #define ZBX_VMWARE_UNIT_CELSIUS			14
420 
421 #endif	/* defined(HAVE_LIBXML2) && defined(HAVE_LIBCURL) */
422 
423 #endif	/* ZABBIX_VMWARE_H */
424