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 #define ZBX_VMWARE_DS_NONE		0
99 #define ZBX_VMWARE_DS_MOUNTED		1
100 #define ZBX_VMWARE_DS_ACCESSIBLE	2
101 #define ZBX_VMWARE_DS_READ		4
102 #define ZBX_VMWARE_DS_WRITE		8
103 #define ZBX_VMWARE_DS_READWRITE		(ZBX_VMWARE_DS_READ | ZBX_VMWARE_DS_WRITE)
104 #define ZBX_VMWARE_DS_READ_FILTER	(ZBX_VMWARE_DS_MOUNTED | ZBX_VMWARE_DS_ACCESSIBLE | ZBX_VMWARE_DS_READ)
105 #define ZBX_VMWARE_DS_WRITE_FILTER	(ZBX_VMWARE_DS_MOUNTED | ZBX_VMWARE_DS_ACCESSIBLE | ZBX_VMWARE_DS_READWRITE)
106 
107 typedef struct
108 {
109 	char				*name;
110 	char				*uuid;
111 	char				*id;
112 	zbx_uint64_t			capacity;
113 	zbx_uint64_t			free_space;
114 	zbx_uint64_t			uncommitted;
115 	zbx_vector_str_uint64_pair_t	hv_uuids_access;
116 }
117 zbx_vmware_datastore_t;
118 
119 int	vmware_ds_name_compare(const void *d1, const void *d2);
120 ZBX_PTR_VECTOR_DECL(vmware_datastore, zbx_vmware_datastore_t *)
121 
122 typedef struct
123 {
124 	char			*name;
125 	char			*id;
126 }
127 zbx_vmware_datacenter_t;
128 
129 int	vmware_dc_name_compare(const void *d1, const void *d2);
130 ZBX_PTR_VECTOR_DECL(vmware_datacenter, zbx_vmware_datacenter_t *)
131 
132 #define ZBX_VMWARE_DEV_TYPE_NIC		1
133 #define ZBX_VMWARE_DEV_TYPE_DISK	2
134 typedef struct
135 {
136 	int	type;
137 	char	*instance;
138 	char	*label;
139 }
140 zbx_vmware_dev_t;
141 
142 /* file system data */
143 typedef struct
144 {
145 	char		*path;
146 	zbx_uint64_t	capacity;
147 	zbx_uint64_t	free_space;
148 }
149 zbx_vmware_fs_t;
150 
151 /* the vmware virtual machine data */
152 typedef struct
153 {
154 	char			*uuid;
155 	char			*id;
156 	char			**props;
157 	zbx_vector_ptr_t	devs;
158 	zbx_vector_ptr_t	file_systems;
159 }
160 zbx_vmware_vm_t;
161 
162 /* the vmware hypervisor data */
163 typedef struct
164 {
165 	char			*uuid;
166 	char			*id;
167 	char			*clusterid;
168 	char			*datacenter_name;
169 	char			*parent_name;
170 	char			*parent_type;
171 	char			**props;
172 	zbx_vector_str_t	ds_names;
173 	zbx_vector_ptr_t	vms;
174 }
175 zbx_vmware_hv_t;
176 
177 /* index virtual machines by uuids */
178 typedef struct
179 {
180 	zbx_vmware_vm_t	*vm;
181 	zbx_vmware_hv_t	*hv;
182 }
183 zbx_vmware_vm_index_t;
184 
185 /* the vmware cluster data */
186 typedef struct
187 {
188 	char	*id;
189 	char	*name;
190 	char	*status;
191 }
192 zbx_vmware_cluster_t;
193 
194 /* the vmware eventlog state */
195 typedef struct
196 {
197 	zbx_uint64_t	last_key;	/* lastlogsize when vmware.eventlog[] item was polled last time */
198 	unsigned char	skip_old;	/* skip old event log records */
199 	unsigned char	oom;		/* no enough memory to store new events */
200 	zbx_uint64_t	req_sz;		/* memory size required to store events */
201 }
202 zbx_vmware_eventlog_state_t;
203 
204 /* the vmware event data */
205 typedef struct
206 {
207 	zbx_uint64_t	key;		/* event's key, used to fill logeventid */
208 	char		*message;	/* event's fullFormattedMessage */
209 	int		timestamp;	/* event's time stamp */
210 }
211 zbx_vmware_event_t;
212 
213 /* the vmware service data object */
214 typedef struct
215 {
216 	char	*error;
217 
218 	zbx_hashset_t			hvs;
219 	zbx_hashset_t			vms_index;
220 	zbx_vector_ptr_t		clusters;
221 	zbx_vector_ptr_t		events;			/* vector of pointers to zbx_vmware_event_t structures */
222 	int				max_query_metrics;	/* max count of Datastore perfCounters in one request */
223 	zbx_vector_vmware_datastore_t	datastores;
224 	zbx_vector_vmware_datacenter_t	datacenters;
225 }
226 zbx_vmware_data_t;
227 
228 /* the vmware service data */
229 typedef struct
230 {
231 	char				*url;
232 	char				*username;
233 	char				*password;
234 
235 	/* the service type - vCenter or vSphere */
236 	unsigned char			type;
237 
238 	/* the service state - see ZBX_VMWARE_STATE_* defines */
239 	int				state;
240 
241 	int				lastcheck;
242 	int				lastperfcheck;
243 
244 	/* The last vmware service access time. If a service is not accessed for a day it is removed */
245 	int				lastaccess;
246 
247 	/* the vmware service instance version */
248 	char				*version;
249 
250 	/* the vmware service instance version numeric */
251 	unsigned short			major_version;
252 	unsigned short			minor_version;
253 
254 	/* the vmware service instance fullname */
255 	char				*fullname;
256 
257 	/* the performance counters */
258 	zbx_hashset_t			counters;
259 
260 	/* list of entities to monitor with performance counters */
261 	zbx_hashset_t			entities;
262 
263 	/* the service data object that is swapped with a new one during service update */
264 	zbx_vmware_data_t		*data;
265 
266 	/* lastlogsize when vmware.eventlog[] item was polled last time and skip old flag*/
267 	zbx_vmware_eventlog_state_t	eventlog;
268 }
269 zbx_vmware_service_t;
270 
271 #define ZBX_VMWARE_PERF_INTERVAL_UNKNOWN	0
272 #define ZBX_VMWARE_PERF_INTERVAL_NONE		-1
273 
274 /* the vmware collector data */
275 typedef struct
276 {
277 	zbx_vector_ptr_t	services;
278 	zbx_hashset_t		strpool;
279 	zbx_uint64_t		strpool_sz;
280 }
281 zbx_vmware_t;
282 
283 /* the vmware collector statistics */
284 typedef struct
285 {
286 	zbx_uint64_t	memory_used;
287 	zbx_uint64_t	memory_total;
288 }
289 zbx_vmware_stats_t;
290 
291 ZBX_THREAD_ENTRY(vmware_thread, args);
292 
293 int	zbx_vmware_init(char **error);
294 void	zbx_vmware_destroy(void);
295 
296 void	zbx_vmware_lock(void);
297 void	zbx_vmware_unlock(void);
298 
299 int	zbx_vmware_get_statistics(zbx_vmware_stats_t *stats);
300 
301 #if defined(HAVE_LIBXML2) && defined(HAVE_LIBCURL)
302 
303 zbx_vmware_service_t	*zbx_vmware_get_service(const char* url, const char* username, const char* password);
304 
305 int	zbx_vmware_service_get_counterid(zbx_vmware_service_t *service, const char *path, zbx_uint64_t *counterid);
306 int	zbx_vmware_service_add_perf_counter(zbx_vmware_service_t *service, const char *type, const char *id,
307 		zbx_uint64_t counterid, const char *instance);
308 zbx_vmware_perf_entity_t	*zbx_vmware_service_get_perf_entity(zbx_vmware_service_t *service, const char *type,
309 		const char *id);
310 
311 /* hypervisor properties */
312 #define ZBX_VMWARE_HVPROP_OVERALL_CPU_USAGE		0
313 #define ZBX_VMWARE_HVPROP_FULL_NAME			1
314 #define ZBX_VMWARE_HVPROP_HW_NUM_CPU_CORES		2
315 #define ZBX_VMWARE_HVPROP_HW_CPU_MHZ			3
316 #define ZBX_VMWARE_HVPROP_HW_CPU_MODEL			4
317 #define ZBX_VMWARE_HVPROP_HW_NUM_CPU_THREADS		5
318 #define ZBX_VMWARE_HVPROP_HW_MEMORY_SIZE		6
319 #define ZBX_VMWARE_HVPROP_HW_MODEL			7
320 #define ZBX_VMWARE_HVPROP_HW_UUID			8
321 #define ZBX_VMWARE_HVPROP_HW_VENDOR			9
322 #define ZBX_VMWARE_HVPROP_MEMORY_USED			10
323 #define ZBX_VMWARE_HVPROP_HEALTH_STATE			11
324 #define ZBX_VMWARE_HVPROP_UPTIME			12
325 #define ZBX_VMWARE_HVPROP_VERSION			13
326 #define ZBX_VMWARE_HVPROP_NAME				14
327 #define ZBX_VMWARE_HVPROP_STATUS			15
328 
329 #define ZBX_VMWARE_HVPROPS_NUM				16
330 
331 /* virtual machine properties */
332 #define ZBX_VMWARE_VMPROP_CPU_NUM			0
333 #define ZBX_VMWARE_VMPROP_CPU_USAGE			1
334 #define ZBX_VMWARE_VMPROP_NAME				2
335 #define ZBX_VMWARE_VMPROP_MEMORY_SIZE			3
336 #define ZBX_VMWARE_VMPROP_MEMORY_SIZE_BALLOONED		4
337 #define ZBX_VMWARE_VMPROP_MEMORY_SIZE_COMPRESSED	5
338 #define ZBX_VMWARE_VMPROP_MEMORY_SIZE_SWAPPED		6
339 #define ZBX_VMWARE_VMPROP_MEMORY_SIZE_USAGE_GUEST	7
340 #define ZBX_VMWARE_VMPROP_MEMORY_SIZE_USAGE_HOST	8
341 #define ZBX_VMWARE_VMPROP_MEMORY_SIZE_PRIVATE		9
342 #define ZBX_VMWARE_VMPROP_MEMORY_SIZE_SHARED		10
343 #define ZBX_VMWARE_VMPROP_POWER_STATE			11
344 #define ZBX_VMWARE_VMPROP_STORAGE_COMMITED		12
345 #define ZBX_VMWARE_VMPROP_STORAGE_UNSHARED		13
346 #define ZBX_VMWARE_VMPROP_STORAGE_UNCOMMITTED		14
347 #define ZBX_VMWARE_VMPROP_UPTIME			15
348 
349 #define ZBX_VMWARE_VMPROPS_NUM				16
350 
351 /* vmware service types */
352 #define ZBX_VMWARE_TYPE_UNKNOWN	0
353 #define ZBX_VMWARE_TYPE_VSPHERE	1
354 #define ZBX_VMWARE_TYPE_VCENTER	2
355 
356 #define ZBX_VMWARE_SOAP_DATACENTER	"Datacenter"
357 #define ZBX_VMWARE_SOAP_FOLDER		"Folder"
358 #define ZBX_VMWARE_SOAP_CLUSTER		"ClusterComputeResource"
359 #define ZBX_VMWARE_SOAP_DEFAULT		"VMware"
360 
361 #endif	/* defined(HAVE_LIBXML2) && defined(HAVE_LIBCURL) */
362 
363 #endif	/* ZABBIX_VMWARE_H */
364