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 #include "common.h"
21 
22 #if defined(HAVE_LIBXML2) && defined(HAVE_LIBCURL)
23 
24 #include "log.h"
25 #include "zbxjson.h"
26 #include "zbxalgo.h"
27 #include "checks_simple_vmware.h"
28 #include"../vmware/vmware.h"
29 
30 #define ZBX_VMWARE_DATASTORE_SIZE_TOTAL		0
31 #define ZBX_VMWARE_DATASTORE_SIZE_FREE		1
32 #define ZBX_VMWARE_DATASTORE_SIZE_PFREE		2
33 #define ZBX_VMWARE_DATASTORE_SIZE_UNCOMMITTED	3
34 
sysinfo_ret_string(int ret)35 static const char	*sysinfo_ret_string(int ret)
36 {
37 	switch (ret)
38 	{
39 		case SYSINFO_RET_OK:
40 			return "SYSINFO_SUCCEED";
41 		case SYSINFO_RET_FAIL:
42 			return "SYSINFO_FAIL";
43 		default:
44 			return "SYSINFO_UNKNOWN";
45 	}
46 }
47 
vmware_set_powerstate_result(AGENT_RESULT * result)48 static int	vmware_set_powerstate_result(AGENT_RESULT *result)
49 {
50 	int	ret = SYSINFO_RET_OK;
51 
52 	if (NULL != GET_STR_RESULT(result))
53 	{
54 		if (0 == strcmp(result->str, "poweredOff"))
55 			SET_UI64_RESULT(result, 0);
56 		else if (0 == strcmp(result->str, "poweredOn"))
57 			SET_UI64_RESULT(result, 1);
58 		else if (0 == strcmp(result->str, "suspended"))
59 			SET_UI64_RESULT(result, 2);
60 		else
61 			ret = SYSINFO_RET_FAIL;
62 
63 		UNSET_STR_RESULT(result);
64 	}
65 
66 	return ret;
67 }
68 
hv_get(zbx_hashset_t * hvs,const char * uuid)69 static zbx_vmware_hv_t	*hv_get(zbx_hashset_t *hvs, const char *uuid)
70 {
71 	const char	*__function_name = "hv_get";
72 
73 	zbx_vmware_hv_t	*hv, hv_local = {(char *)uuid};
74 
75 	zabbix_log(LOG_LEVEL_DEBUG, "In %s() uuid:'%s'", __function_name, uuid);
76 
77 	hv = zbx_hashset_search(hvs, &hv_local);
78 
79 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%p", __function_name, hv);
80 
81 	return hv;
82 }
83 
service_hv_get_by_vm_uuid(zbx_vmware_service_t * service,const char * uuid)84 static zbx_vmware_hv_t	*service_hv_get_by_vm_uuid(zbx_vmware_service_t *service, const char *uuid)
85 {
86 	const char	*__function_name = "service_hv_get_by_vm_uuid";
87 
88 	zbx_vmware_vm_t		vm_local = {(char *)uuid};
89 	zbx_vmware_vm_index_t	vmi_local = {&vm_local}, *vmi;
90 	zbx_vmware_hv_t		*hv = NULL;
91 
92 	zabbix_log(LOG_LEVEL_DEBUG, "In %s() uuid:'%s'", __function_name, uuid);
93 
94 	if (NULL != (vmi = zbx_hashset_search(&service->data->vms_index, &vmi_local)))
95 		hv = vmi->hv;
96 	else
97 		hv = NULL;
98 
99 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%p", __function_name, hv);
100 
101 	return hv;
102 
103 }
104 
service_vm_get(zbx_vmware_service_t * service,const char * uuid)105 static zbx_vmware_vm_t	*service_vm_get(zbx_vmware_service_t *service, const char *uuid)
106 {
107 	const char	*__function_name = "service_vm_get";
108 
109 	zbx_vmware_vm_t		vm_local = {(char *)uuid}, *vm;
110 	zbx_vmware_vm_index_t	vmi_local = {&vm_local}, *vmi;
111 
112 	zabbix_log(LOG_LEVEL_DEBUG, "In %s() uuid:'%s'", __function_name, uuid);
113 
114 	if (NULL != (vmi = zbx_hashset_search(&service->data->vms_index, &vmi_local)))
115 		vm = vmi->vm;
116 	else
117 		vm = NULL;
118 
119 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%p", __function_name, vm);
120 
121 	return vm;
122 }
123 
cluster_get(zbx_vector_ptr_t * clusters,const char * clusterid)124 static zbx_vmware_cluster_t	*cluster_get(zbx_vector_ptr_t *clusters, const char *clusterid)
125 {
126 	const char		*__function_name = "cluster_get";
127 
128 	int			i;
129 	zbx_vmware_cluster_t	*cluster;
130 
131 	zabbix_log(LOG_LEVEL_DEBUG, "In %s() uuid:'%s'", __function_name, clusterid);
132 
133 	for (i = 0; i < clusters->values_num; i++)
134 	{
135 		cluster = (zbx_vmware_cluster_t *)clusters->values[i];
136 
137 		if (0 == strcmp(cluster->id, clusterid))
138 			goto out;
139 	}
140 
141 	cluster = NULL;
142 out:
143 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%p", __function_name, cluster);
144 
145 	return cluster;
146 }
147 
cluster_get_by_name(zbx_vector_ptr_t * clusters,const char * name)148 static zbx_vmware_cluster_t	*cluster_get_by_name(zbx_vector_ptr_t *clusters, const char *name)
149 {
150 	const char		*__function_name = "cluster_get_by_name";
151 
152 	int			i;
153 	zbx_vmware_cluster_t	*cluster;
154 
155 	zabbix_log(LOG_LEVEL_DEBUG, "In %s() name:'%s'", __function_name, name);
156 
157 	for (i = 0; i < clusters->values_num; i++)
158 	{
159 		cluster = (zbx_vmware_cluster_t *)clusters->values[i];
160 
161 		if (0 == strcmp(cluster->name, name))
162 			goto out;
163 	}
164 
165 	cluster = NULL;
166 out:
167 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%p", __function_name, cluster);
168 
169 	return cluster;
170 }
171 
172 /******************************************************************************
173  *                                                                            *
174  * Function: vmware_service_get_counter_value_by_id                           *
175  *                                                                            *
176  * Purpose: gets vmware performance counter value by its identifier           *
177  *                                                                            *
178  * Parameters: service   - [IN] the vmware service                            *
179  *             type      - [IN] the performance entity type (HostSystem,      *
180  *                              VirtualMachine, Datastore)                    *
181  *             id        - [IN] the performance entity identifier             *
182  *             counterid - [IN] the performance counter identifier            *
183  *             instance  - [IN] the performance counter instance or "" for    *
184  *                              aggregate data                                *
185  *             coeff     - [IN] the coefficient to apply to the value         *
186  *             result    - [OUT] the output result                            *
187  *                                                                            *
188  * Return value: SYSINFO_RET_OK, result has value - performance counter value *
189  *                               was successfully retrieved                   *
190  *               SYSINFO_RET_OK, result has no value - performance counter    *
191  *                               was found without a value                    *
192  *               SYSINFO_RET_FAIL - otherwise, error message is set in result *
193  *                                                                            *
194  * Comments: There can be situation when performance counter is configured    *
195  *           to be read but the collector has not yet processed it. In this   *
196  *           case return SYSINFO_RET_OK with empty result so that it is       *
197  *           ignored by server rather than generating error.                  *
198  *                                                                            *
199  ******************************************************************************/
vmware_service_get_counter_value_by_id(zbx_vmware_service_t * service,const char * type,const char * id,zbx_uint64_t counterid,const char * instance,int coeff,AGENT_RESULT * result)200 static int	vmware_service_get_counter_value_by_id(zbx_vmware_service_t *service, const char *type, const char *id,
201 		zbx_uint64_t counterid, const char *instance, int coeff, AGENT_RESULT *result)
202 {
203 	const char			*__function_name = "vmware_service_get_counter_value_by_id";
204 
205 	zbx_vmware_perf_entity_t	*entity;
206 	zbx_vmware_perf_counter_t	*perfcounter;
207 	zbx_ptr_pair_t			*perfvalue;
208 	int				i, ret = SYSINFO_RET_FAIL;
209 
210 	zabbix_log(LOG_LEVEL_DEBUG, "In %s() type:%s id:%s counterid:" ZBX_FS_UI64 " instance:%s", __function_name,
211 			type, id, counterid, instance);
212 
213 	if (NULL == (entity = zbx_vmware_service_get_perf_entity(service, type, id)))
214 	{
215 		/* the requested counter has not been queried yet */
216 		zabbix_log(LOG_LEVEL_DEBUG, "performance data is not yet ready, ignoring request");
217 		ret = SYSINFO_RET_OK;
218 		goto out;
219 	}
220 
221 	if (NULL != entity->error)
222 	{
223 		SET_MSG_RESULT(result, zbx_strdup(NULL, entity->error));
224 		goto out;
225 	}
226 
227 	if (FAIL == (i = zbx_vector_ptr_bsearch(&entity->counters, &counterid, ZBX_DEFAULT_UINT64_PTR_COMPARE_FUNC)))
228 	{
229 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Performance counter data was not found."));
230 		goto out;
231 	}
232 
233 	perfcounter = (zbx_vmware_perf_counter_t *)entity->counters.values[i];
234 
235 	if (0 == (perfcounter->state & ZBX_VMWARE_COUNTER_READY))
236 	{
237 		ret = SYSINFO_RET_OK;
238 		goto out;
239 	}
240 
241 	if (0 == perfcounter->values.values_num)
242 	{
243 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Performance counter data is not available."));
244 		goto out;
245 	}
246 
247 	for (i = 0; i < perfcounter->values.values_num; i++)
248 	{
249 		perfvalue = (zbx_ptr_pair_t *)&perfcounter->values.values[i];
250 
251 		if (0 == strcmp(perfvalue->first, instance))
252 			break;
253 	}
254 
255 	if (i == perfcounter->values.values_num)
256 	{
257 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Performance counter instance was not found."));
258 		goto out;
259 	}
260 
261 	/* VMware returns -1 value if the performance data for the specified period is not ready - ignore it */
262 	if (0 == strcmp(perfvalue->second, "-1"))
263 	{
264 		ret = SYSINFO_RET_OK;
265 		goto out;
266 	}
267 
268 	if (SUCCEED == set_result_type(result, ITEM_VALUE_TYPE_UINT64, ITEM_DATA_TYPE_DECIMAL, perfvalue->second))
269 	{
270 		result->ui64 *= coeff;
271 		ret = SYSINFO_RET_OK;
272 	}
273 out:
274 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __function_name, sysinfo_ret_string(ret));
275 
276 	return ret;
277 }
278 
279 /******************************************************************************
280  *                                                                            *
281  * Function: vmware_service_get_counter_value_by_path                         *
282  *                                                                            *
283  * Purpose: gets vmware performance counter value by the path                 *
284  *                                                                            *
285  * Parameters: service  - [IN] the vmware service                             *
286  *             type     - [IN] the performance entity type (HostSystem,       *
287  *                             VirtualMachine, Datastore)                     *
288  *             id       - [IN] the performance entity identifier              *
289  *             path     - [IN] the performance counter path                   *
290  *                             (<group>/<key>[<rollup type>])                 *
291  *             instance - [IN] the performance counter instance or "" for     *
292  *                             aggregate data                                 *
293  *             coeff    - [IN] the coefficient to apply to the value          *
294  *             result   - [OUT] the output result                             *
295  *                                                                            *
296  * Return value: SYSINFO_RET_OK, result has value - performance counter value *
297  *                               was successfully retrieved                   *
298  *               SYSINFO_RET_OK, result has no value - performance counter    *
299  *                               was found without a value                    *
300  *               SYSINFO_RET_FAIL - otherwise, error message is set in result *
301  *                                                                            *
302  * Comments: There can be situation when performance counter is configured    *
303  *           to be read but the collector has not yet processed it. In this   *
304  *           case return SYSINFO_RET_OK with empty result so that it is       *
305  *           ignored by server rather than generating error.                  *
306  *                                                                            *
307  ******************************************************************************/
vmware_service_get_counter_value_by_path(zbx_vmware_service_t * service,const char * type,const char * id,const char * path,const char * instance,int coeff,AGENT_RESULT * result)308 static int	vmware_service_get_counter_value_by_path(zbx_vmware_service_t *service, const char *type,
309 		const char *id, const char *path, const char *instance, int coeff, AGENT_RESULT *result)
310 {
311 	zbx_uint64_t	counterid;
312 
313 	if (FAIL == zbx_vmware_service_get_counterid(service, path, &counterid))
314 	{
315 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Performance counter is not available."));
316 		return SYSINFO_RET_FAIL;
317 	}
318 
319 	return vmware_service_get_counter_value_by_id(service, type, id, counterid, instance, coeff, result);
320 }
321 
vmware_service_get_vm_counter(zbx_vmware_service_t * service,const char * uuid,const char * instance,const char * path,int coeff,AGENT_RESULT * result)322 static int	vmware_service_get_vm_counter(zbx_vmware_service_t *service, const char *uuid, const char *instance,
323 		const char *path, int coeff, AGENT_RESULT *result)
324 {
325 	const char	*__function_name = "vmware_service_get_vm_counter";
326 
327 	zbx_vmware_vm_t	*vm;
328 	int		ret = SYSINFO_RET_FAIL;
329 
330 	zabbix_log(LOG_LEVEL_DEBUG, "In %s() uuid:%s instance:%s path:%s", __function_name, uuid, instance, path);
331 
332 	if (NULL == (vm = service_vm_get(service, uuid)))
333 	{
334 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Unknown virtual machine uuid."));
335 		goto out;
336 	}
337 
338 	ret = vmware_service_get_counter_value_by_path(service, "VirtualMachine", vm->id, path, instance, coeff,
339 			result);
340 out:
341 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __function_name, sysinfo_ret_string(ret));
342 
343 	return ret;
344 }
345 
346 /******************************************************************************
347  *                                                                            *
348  * Function: get_vmware_service                                               *
349  *                                                                            *
350  * Purpose: gets vmware service object                                        *
351  *                                                                            *
352  * Parameters: url       - [IN] the vmware service URL                        *
353  *             username  - [IN] the vmware service username                   *
354  *             password  - [IN] the vmware service password                   *
355  *             ret       - [OUT] the operation result code                    *
356  *                                                                            *
357  * Return value: The vmware service object or NULL if the service was not     *
358  *               found, did not have data or any error occurred. In the last  *
359  *               case the error message will be stored in agent result.       *
360  *                                                                            *
361  * Comments: There are three possible cases:                                  *
362  *             1) the vmware service is not ready. This can happen when       *
363  *                service was added, but not yet processed by collector.      *
364  *                In this case NULL is returned and result code is set to     *
365  *                SYSINFO_RET_OK.                                             *
366  *             2) the vmware service update failed. This can happen if there  *
367  *                was a network problem, authentication failure or any error  *
368  *                that prevented from obtaining and parsing vmware data.      *
369  *                In this case NULL is returned and result code is set to     *
370  *                SYSINFO_RET_FAIL.                                           *
371  *             3) the vmware service has been updated successfully.           *
372  *                In this case the service object is returned and result code *
373  *                is not set.                                                 *
374  *                                                                            *
375  ******************************************************************************/
get_vmware_service(const char * url,const char * username,const char * password,AGENT_RESULT * result,int * ret)376 static zbx_vmware_service_t	*get_vmware_service(const char *url, const char *username, const char *password,
377 		AGENT_RESULT *result, int *ret)
378 {
379 	const char		*__function_name = "get_vmware_service";
380 
381 	zbx_vmware_service_t	*service;
382 
383 	zabbix_log(LOG_LEVEL_DEBUG, "In %s() '%s'@'%s'", __function_name, username, url);
384 
385 	if (NULL == (service = zbx_vmware_get_service(url, username, password)))
386 	{
387 		*ret = SYSINFO_RET_OK;
388 		goto out;
389 	}
390 
391 	if (0 != (service->state & ZBX_VMWARE_STATE_FAILED))
392 	{
393 		SET_MSG_RESULT(result, zbx_strdup(NULL, NULL != service->data->error ? service->data->error :
394 				"Unknown VMware service error."));
395 
396 		zabbix_log(LOG_LEVEL_DEBUG, "failed to query VMware service: %s",
397 				NULL != service->data->error ? service->data->error : "unknown error");
398 
399 		*ret = SYSINFO_RET_FAIL;
400 		service = NULL;
401 	}
402 out:
403 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%p", __function_name, service);
404 
405 	return service;
406 }
407 
408 /******************************************************************************
409  *                                                                            *
410  * Function: get_vcenter_vmprop                                               *
411  *                                                                            *
412  * Purpose: retrieves data from virtual machine details                       *
413  *                                                                            *
414  * Parameters: request   - [IN] the original request. The first parameter is  *
415  *                              vmware service URL and the second parameter   *
416  *                              is virtual machine uuid.                      *
417  *             username  - [IN] the vmware service user name                  *
418  *             password  - [IN] the vmware service password                   *
419  *             xpath     - [IN] the xpath describing data to retrieve         *
420  *             result    - [OUT] the request result                           *
421  *                                                                            *
422  ******************************************************************************/
get_vcenter_vmprop(AGENT_REQUEST * request,const char * username,const char * password,int propid,AGENT_RESULT * result)423 static int	get_vcenter_vmprop(AGENT_REQUEST *request, const char *username, const char *password,
424 		int propid, AGENT_RESULT *result)
425 {
426 	const char		*__function_name = "get_vcenter_vmprop";
427 
428 	zbx_vmware_service_t	*service;
429 	zbx_vmware_vm_t		*vm = NULL;
430 	char			*url, *uuid, *value;
431 	int			ret = SYSINFO_RET_FAIL;
432 
433 	zabbix_log(LOG_LEVEL_DEBUG, "In %s() propid:%d", __function_name, propid);
434 
435 	if (2 != request->nparam)
436 	{
437 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid number of parameters."));
438 		goto out;
439 	}
440 
441 	url = get_rparam(request, 0);
442 	uuid = get_rparam(request, 1);
443 
444 	if ('\0' == *uuid)
445 	{
446 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid second parameter."));
447 		goto out;
448 	}
449 
450 	zbx_vmware_lock();
451 
452 	if (NULL == (service = get_vmware_service(url, username, password, result, &ret)))
453 		goto unlock;
454 
455 	if (NULL == (vm = service_vm_get(service, uuid)))
456 	{
457 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Unknown virtual machine uuid."));
458 		goto unlock;
459 	}
460 
461 	if (NULL == (value = vm->props[propid]))
462 	{
463 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Value is not available."));
464 		goto unlock;
465 	}
466 
467 	SET_STR_RESULT(result, zbx_strdup(NULL, value));
468 
469 	ret = SYSINFO_RET_OK;
470 unlock:
471 	zbx_vmware_unlock();
472 out:
473 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __function_name, sysinfo_ret_string(ret));
474 
475 	return ret;
476 }
477 
478 /******************************************************************************
479  *                                                                            *
480  * Function: get_vcenter_hvprop                                               *
481  *                                                                            *
482  * Purpose: retrieves hypervisor property                                     *
483  *                                                                            *
484  * Parameters: request   - [IN] the original request. The first parameter is  *
485  *                              vmware service URL and the second parameter   *
486  *                              is hypervisor uuid.                           *
487  *             username  - [IN] the vmware service user name                  *
488  *             password  - [IN] the vmware service password                   *
489  *             propid    - [IN] the property id                               *
490  *             result    - [OUT] the request result                           *
491  *                                                                            *
492  ******************************************************************************/
get_vcenter_hvprop(AGENT_REQUEST * request,const char * username,const char * password,int propid,AGENT_RESULT * result)493 static int	get_vcenter_hvprop(AGENT_REQUEST *request, const char *username, const char *password, int propid,
494 		AGENT_RESULT *result)
495 {
496 	const char		*__function_name = "get_vcenter_hvprop";
497 
498 	zbx_vmware_service_t	*service;
499 	const char		*uuid, *url, *value;
500 	zbx_vmware_hv_t		*hv;
501 	int			ret = SYSINFO_RET_FAIL;
502 
503 	zabbix_log(LOG_LEVEL_DEBUG, "In %s() propid:%d", __function_name, propid);
504 
505 	if (2 != request->nparam)
506 	{
507 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid number of parameters."));
508 		goto out;
509 	}
510 
511 	url = get_rparam(request, 0);
512 	uuid = get_rparam(request, 1);
513 
514 	if ('\0' == *uuid)
515 	{
516 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid second parameter."));
517 		goto out;
518 	}
519 
520 	zbx_vmware_lock();
521 
522 	if (NULL == (service = get_vmware_service(url, username, password, result, &ret)))
523 		goto unlock;
524 
525 	if (NULL == (hv = hv_get(&service->data->hvs, uuid)))
526 	{
527 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Unknown hypervisor uuid."));
528 		goto unlock;
529 	}
530 
531 	if (NULL == (value = hv->props[propid]))
532 	{
533 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Value is not available."));
534 		goto unlock;
535 	}
536 
537 	SET_STR_RESULT(result, zbx_strdup(NULL, value));
538 	ret = SYSINFO_RET_OK;
539 unlock:
540 	zbx_vmware_unlock();
541 out:
542 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __function_name, sysinfo_ret_string(ret));
543 
544 	return ret;
545 }
546 
547 
check_vcenter_cluster_discovery(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)548 int	check_vcenter_cluster_discovery(AGENT_REQUEST *request, const char *username, const char *password,
549 		AGENT_RESULT *result)
550 {
551 	const char		*__function_name = "check_vcenter_cluster_discovery";
552 
553 	struct zbx_json		json_data;
554 	char			*url;
555 	zbx_vmware_service_t	*service;
556 	int			i, ret = SYSINFO_RET_FAIL;
557 
558 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name);
559 
560 	if (1 != request->nparam)
561 	{
562 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid number of parameters."));
563 		goto out;
564 	}
565 
566 	url = get_rparam(request, 0);
567 
568 	zbx_vmware_lock();
569 
570 	if (NULL == (service = get_vmware_service(url, username, password, result, &ret)))
571 		goto unlock;
572 
573 	zbx_json_init(&json_data, ZBX_JSON_STAT_BUF_LEN);
574 	zbx_json_addarray(&json_data, ZBX_PROTO_TAG_DATA);
575 
576 	for (i = 0; i < service->data->clusters.values_num; i++)
577 	{
578 		zbx_vmware_cluster_t	*cluster = (zbx_vmware_cluster_t *)service->data->clusters.values[i];
579 
580 		zbx_json_addobject(&json_data, NULL);
581 		zbx_json_addstring(&json_data, "{#CLUSTER.ID}", cluster->id, ZBX_JSON_TYPE_STRING);
582 		zbx_json_addstring(&json_data, "{#CLUSTER.NAME}", cluster->name, ZBX_JSON_TYPE_STRING);
583 		zbx_json_close(&json_data);
584 	}
585 
586 	zbx_json_close(&json_data);
587 
588 	SET_STR_RESULT(result, zbx_strdup(NULL, json_data.buffer));
589 
590 	zbx_json_free(&json_data);
591 
592 	ret = SYSINFO_RET_OK;
593 unlock:
594 	zbx_vmware_unlock();
595 out:
596 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __function_name, sysinfo_ret_string(ret));
597 
598 	return ret;
599 }
600 
check_vcenter_cluster_status(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)601 int	check_vcenter_cluster_status(AGENT_REQUEST *request, const char *username, const char *password,
602 		AGENT_RESULT *result)
603 {
604 	const char		*__function_name = "check_vcenter_cluster_status";
605 
606 	char			*url, *name, *status;
607 	zbx_vmware_service_t	*service;
608 	zbx_vmware_cluster_t	*cluster;
609 	int			ret = SYSINFO_RET_FAIL;
610 
611 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name);
612 
613 	if (2 != request->nparam)
614 	{
615 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid number of parameters."));
616 		goto out;
617 	}
618 
619 	url = get_rparam(request, 0);
620 	name = get_rparam(request, 1);
621 
622 	if ('\0' == *name)
623 		goto out;
624 
625 	zbx_vmware_lock();
626 
627 	if (NULL == (service = get_vmware_service(url, username, password, result, &ret)))
628 		goto unlock;
629 
630 	if (NULL == (cluster = cluster_get_by_name(&service->data->clusters, name)))
631 	{
632 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Unknown cluster name."));
633 		goto unlock;
634 	}
635 
636 	if (NULL == (status = zbx_xml_read_value(cluster->status, ZBX_XPATH_LN2("val", "overallStatus"))))
637 		goto unlock;
638 
639 	ret = SYSINFO_RET_OK;
640 
641 	if (0 == strcmp(status, "gray"))
642 		SET_UI64_RESULT(result, 0);
643 	else if (0 == strcmp(status, "green"))
644 		SET_UI64_RESULT(result, 1);
645 	else if (0 == strcmp(status, "yellow"))
646 		SET_UI64_RESULT(result, 2);
647 	else if (0 == strcmp(status, "red"))
648 		SET_UI64_RESULT(result, 3);
649 	else
650 		ret = SYSINFO_RET_FAIL;
651 
652 	zbx_free(status);
653 unlock:
654 	zbx_vmware_unlock();
655 out:
656 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __function_name, sysinfo_ret_string(ret));
657 
658 	return ret;
659 }
660 
vmware_get_events(const zbx_vector_ptr_t * events,zbx_uint64_t eventlog_last_key,const DC_ITEM * item,zbx_vector_ptr_t * add_results)661 static void	vmware_get_events(const zbx_vector_ptr_t *events, zbx_uint64_t eventlog_last_key, const DC_ITEM *item,
662 		zbx_vector_ptr_t *add_results)
663 {
664 	const char	*__function_name = "vmware_get_events";
665 
666 	int		i;
667 
668 	zabbix_log(LOG_LEVEL_DEBUG, "In %s() eventlog_last_key:" ZBX_FS_UI64, __function_name, eventlog_last_key);
669 
670 	/* events were retrieved in reverse chronological order */
671 	for (i = events->values_num - 1; i >= 0; i--)
672 	{
673 		const zbx_vmware_event_t	*event = events->values[i];
674 		AGENT_RESULT			*add_result = NULL;
675 
676 		if (event->key <= eventlog_last_key)
677 			continue;
678 
679 		add_result = zbx_malloc(add_result, sizeof(AGENT_RESULT));
680 		init_result(add_result);
681 
682 		if (SUCCEED == set_result_type(add_result, item->value_type, item->flags, event->message))
683 		{
684 			set_result_meta(add_result, event->key, 0);
685 
686 			if (ITEM_VALUE_TYPE_LOG == item->value_type)
687 			{
688 				add_result->log->logeventid = event->key;
689 				add_result->log->timestamp = event->timestamp;
690 			}
691 
692 			zbx_vector_ptr_append(add_results, add_result);
693 		}
694 	}
695 
696 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s(): events:%d", __function_name, add_results->values_num);
697 }
698 
check_vcenter_eventlog(AGENT_REQUEST * request,const DC_ITEM * item,AGENT_RESULT * result,zbx_vector_ptr_t * add_results)699 int	check_vcenter_eventlog(AGENT_REQUEST *request, const DC_ITEM *item, AGENT_RESULT *result,
700 		zbx_vector_ptr_t *add_results)
701 {
702 	const char		*__function_name = "check_vcenter_eventlog";
703 
704 	char			*url;
705 	zbx_vmware_service_t	*service;
706 	int			ret = SYSINFO_RET_FAIL;
707 
708 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name);
709 
710 	if (1 != request->nparam)
711 	{
712 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid number of parameters."));
713 		goto out;
714 	}
715 
716 	url = get_rparam(request, 0);
717 
718 	zbx_vmware_lock();
719 
720 	if (NULL == (service = get_vmware_service(url, item->username, item->password, result, &ret)))
721 		goto unlock;
722 
723 	if (ZBX_VMWARE_EVENT_KEY_UNINITIALIZED == service->eventlog_last_key)
724 	{
725 		service->eventlog_last_key = request->lastlogsize;
726 	}
727 	else if (request->lastlogsize < service->eventlog_last_key)
728 	{
729 		/* this may happen if there are multiple vmware.eventlog items for the same service URL or item has  */
730 		/* been polled, but values got stuck in history cache and item's lastlogsize hasn't been updated yet */
731 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Too old events requested."));
732 		goto unlock;
733 	}
734 	else if (0 < service->data->events.values_num)
735 	{
736 		vmware_get_events(&service->data->events, request->lastlogsize, item, add_results);
737 		service->eventlog_last_key = ((const zbx_vmware_event_t *)service->data->events.values[0])->key;
738 	}
739 
740 	ret = SYSINFO_RET_OK;
741 unlock:
742 	zbx_vmware_unlock();
743 out:
744 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __function_name, sysinfo_ret_string(ret));
745 
746 	return ret;
747 }
748 
check_vcenter_version(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)749 int	check_vcenter_version(AGENT_REQUEST *request, const char *username, const char *password,
750 		AGENT_RESULT *result)
751 {
752 	const char		*__function_name = "check_vcenter_version";
753 
754 	char			*url, *version;
755 	zbx_vmware_service_t	*service;
756 	int			ret = SYSINFO_RET_FAIL;
757 
758 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name);
759 
760 	if (1 != request->nparam)
761 	{
762 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid number of parameters."));
763 		goto out;
764 	}
765 
766 	url = get_rparam(request, 0);
767 
768 	zbx_vmware_lock();
769 
770 	if (NULL == (service = get_vmware_service(url, username, password, result, &ret)))
771 		goto unlock;
772 
773 	if (NULL == (version = zbx_xml_read_value(service->contents, ZBX_XPATH_VMWARE_ABOUT("version"))))
774 		goto unlock;
775 
776 	SET_STR_RESULT(result, version);
777 
778 	ret = SYSINFO_RET_OK;
779 unlock:
780 	zbx_vmware_unlock();
781 out:
782 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __function_name, sysinfo_ret_string(ret));
783 
784 	return ret;
785 }
786 
check_vcenter_fullname(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)787 int	check_vcenter_fullname(AGENT_REQUEST *request, const char *username, const char *password,
788 		AGENT_RESULT *result)
789 {
790 	const char		*__function_name = "check_vcenter_fullname";
791 
792 	char			*url, *fullname = NULL;
793 	zbx_vmware_service_t	*service;
794 	int			ret = SYSINFO_RET_FAIL;
795 
796 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name);
797 
798 	if (1 != request->nparam)
799 	{
800 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid number of parameters."));
801 		goto out;
802 	}
803 
804 	url = get_rparam(request, 0);
805 
806 	zbx_vmware_lock();
807 
808 	if (NULL == (service = get_vmware_service(url, username, password, result, &ret)))
809 		goto unlock;
810 
811 	if (NULL == (fullname = zbx_xml_read_value(service->contents, ZBX_XPATH_VMWARE_ABOUT("fullName"))))
812 		goto unlock;
813 
814 	SET_STR_RESULT(result, fullname);
815 
816 	ret = SYSINFO_RET_OK;
817 unlock:
818 	zbx_vmware_unlock();
819 out:
820 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s fullname:[%s]", __function_name, sysinfo_ret_string(ret),
821 			ZBX_NULL2STR(fullname));
822 
823 	return ret;
824 }
825 
check_vcenter_hv_cluster_name(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)826 int	check_vcenter_hv_cluster_name(AGENT_REQUEST *request, const char *username, const char *password,
827 		AGENT_RESULT *result)
828 {
829 	const char		*__function_name = "check_vcenter_hv_cluster_name";
830 
831 	char			*url, *uuid;
832 	zbx_vmware_hv_t		*hv;
833 	zbx_vmware_service_t	*service;
834 	zbx_vmware_cluster_t	*cluster = NULL;
835 	int			ret = SYSINFO_RET_FAIL;
836 
837 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name);
838 
839 	if (2 != request->nparam)
840 	{
841 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid number of parameters."));
842 		goto out;
843 	}
844 
845 	url = get_rparam(request, 0);
846 	uuid = get_rparam(request, 1);
847 
848 	if ('\0' == *uuid)
849 	{
850 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid second parameter."));
851 		goto out;
852 	}
853 
854 	zbx_vmware_lock();
855 
856 	if (NULL == (service = get_vmware_service(url, username, password, result, &ret)))
857 		goto unlock;
858 
859 	if (NULL == (hv = hv_get(&service->data->hvs, uuid)))
860 	{
861 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Unknown hypervisor uuid."));
862 		goto unlock;
863 	}
864 
865 	if (NULL != hv->clusterid)
866 		cluster = cluster_get(&service->data->clusters, hv->clusterid);
867 
868 	SET_STR_RESULT(result, zbx_strdup(NULL, NULL != cluster ? cluster->name : ""));
869 
870 	ret = SYSINFO_RET_OK;
871 unlock:
872 	zbx_vmware_unlock();
873 out:
874 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __function_name, sysinfo_ret_string(ret));
875 
876 	return ret;
877 }
878 
check_vcenter_hv_cpu_usage(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)879 int	check_vcenter_hv_cpu_usage(AGENT_REQUEST *request, const char *username, const char *password,
880 		AGENT_RESULT *result)
881 {
882 	const char	*__function_name = "check_vcenter_hv_cpu_usage";
883 
884 	int		ret;
885 
886 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name);
887 
888 	ret = get_vcenter_hvprop(request, username, password, ZBX_VMWARE_HVPROP_OVERALL_CPU_USAGE, result);
889 
890 	if (SYSINFO_RET_OK == ret && NULL != GET_UI64_RESULT(result))
891 		result->ui64 = result->ui64 * 1000000;
892 
893 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __function_name, sysinfo_ret_string(ret));
894 
895 	return ret;
896 }
897 
check_vcenter_hv_discovery(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)898 int	check_vcenter_hv_discovery(AGENT_REQUEST *request, const char *username, const char *password,
899 		AGENT_RESULT *result)
900 {
901 	const char		*__function_name = "check_vcenter_hv_discovery";
902 
903 	struct zbx_json		json_data;
904 	char			*url, *name;
905 	zbx_vmware_service_t	*service;
906 	int			ret = SYSINFO_RET_FAIL;
907 	zbx_vmware_hv_t		*hv;
908 	zbx_hashset_iter_t	iter;
909 
910 
911 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name);
912 
913 	if (1 != request->nparam)
914 	{
915 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid number of parameters."));
916 		goto out;
917 	}
918 
919 	url = get_rparam(request, 0);
920 
921 	zbx_vmware_lock();
922 
923 	if (NULL == (service = get_vmware_service(url, username, password, result, &ret)))
924 		goto unlock;
925 
926 	zbx_json_init(&json_data, ZBX_JSON_STAT_BUF_LEN);
927 	zbx_json_addarray(&json_data, ZBX_PROTO_TAG_DATA);
928 
929 	zbx_hashset_iter_reset(&service->data->hvs, &iter);
930 	while (NULL != (hv = zbx_hashset_iter_next(&iter)))
931 	{
932 		zbx_vmware_cluster_t	*cluster = NULL;
933 
934 		if (NULL == (name = hv->props[ZBX_VMWARE_HVPROP_NAME]))
935 			continue;
936 
937 		if (NULL != hv->clusterid)
938 			cluster = cluster_get(&service->data->clusters, hv->clusterid);
939 
940 		zbx_json_addobject(&json_data, NULL);
941 		zbx_json_addstring(&json_data, "{#HV.UUID}", hv->uuid, ZBX_JSON_TYPE_STRING);
942 		zbx_json_addstring(&json_data, "{#HV.ID}", hv->id, ZBX_JSON_TYPE_STRING);
943 		zbx_json_addstring(&json_data, "{#HV.NAME}", name, ZBX_JSON_TYPE_STRING);
944 		zbx_json_addstring(&json_data, "{#CLUSTER.NAME}",
945 				NULL != cluster ? cluster->name : "", ZBX_JSON_TYPE_STRING);
946 		zbx_json_close(&json_data);
947 	}
948 
949 	zbx_json_close(&json_data);
950 
951 	SET_STR_RESULT(result, zbx_strdup(NULL, json_data.buffer));
952 
953 	zbx_json_free(&json_data);
954 
955 	ret = SYSINFO_RET_OK;
956 unlock:
957 	zbx_vmware_unlock();
958 out:
959 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __function_name, sysinfo_ret_string(ret));
960 
961 	return ret;
962 }
963 
check_vcenter_hv_fullname(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)964 int	check_vcenter_hv_fullname(AGENT_REQUEST *request, const char *username, const char *password,
965 		AGENT_RESULT *result)
966 {
967 	const char	*__function_name = "check_vcenter_hv_fullname";
968 
969 	int		ret;
970 
971 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name);
972 
973 	ret = get_vcenter_hvprop(request, username, password, ZBX_VMWARE_HVPROP_FULL_NAME, result);
974 
975 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __function_name, sysinfo_ret_string(ret));
976 
977 	return ret;
978 }
979 
check_vcenter_hv_hw_cpu_num(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)980 int	check_vcenter_hv_hw_cpu_num(AGENT_REQUEST *request, const char *username, const char *password,
981 		AGENT_RESULT *result)
982 {
983 	const char	*__function_name = "check_vcenter_hv_hw_cpu_num";
984 
985 	int		ret;
986 
987 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name);
988 
989 	ret = get_vcenter_hvprop(request, username, password, ZBX_VMWARE_HVPROP_HW_NUM_CPU_CORES, result);
990 
991 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __function_name, sysinfo_ret_string(ret));
992 
993 	return ret;
994 }
995 
check_vcenter_hv_hw_cpu_freq(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)996 int	check_vcenter_hv_hw_cpu_freq(AGENT_REQUEST *request, const char *username, const char *password,
997 		AGENT_RESULT *result)
998 {
999 	const char	*__function_name = "check_vcenter_hv_hw_cpu_freq";
1000 
1001 	int		ret;
1002 
1003 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name);
1004 
1005 	ret = get_vcenter_hvprop(request, username, password, ZBX_VMWARE_HVPROP_HW_CPU_MHZ, result);
1006 
1007 	if (SYSINFO_RET_OK == ret && NULL != GET_UI64_RESULT(result))
1008 		result->ui64 = result->ui64 * 1000000;
1009 
1010 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __function_name, sysinfo_ret_string(ret));
1011 
1012 	return ret;
1013 }
1014 
check_vcenter_hv_hw_cpu_model(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)1015 int	check_vcenter_hv_hw_cpu_model(AGENT_REQUEST *request, const char *username, const char *password,
1016 		AGENT_RESULT *result)
1017 {
1018 	const char	*__function_name = "check_vcenter_hv_hw_cpu_model";
1019 
1020 	int		ret;
1021 
1022 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name);
1023 
1024 	ret = get_vcenter_hvprop(request, username, password, ZBX_VMWARE_HVPROP_HW_CPU_MODEL, result);
1025 
1026 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __function_name, sysinfo_ret_string(ret));
1027 
1028 	return ret;
1029 }
1030 
check_vcenter_hv_hw_cpu_threads(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)1031 int	check_vcenter_hv_hw_cpu_threads(AGENT_REQUEST *request, const char *username, const char *password,
1032 		AGENT_RESULT *result)
1033 {
1034 	const char	*__function_name = "check_vcenter_hv_hw_cpu_threads";
1035 
1036 	int		ret;
1037 
1038 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name);
1039 
1040 	ret = get_vcenter_hvprop(request, username, password, ZBX_VMWARE_HVPROP_HW_NUM_CPU_THREADS, result);
1041 
1042 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __function_name, sysinfo_ret_string(ret));
1043 
1044 	return ret;
1045 }
1046 
check_vcenter_hv_hw_memory(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)1047 int	check_vcenter_hv_hw_memory(AGENT_REQUEST *request, const char *username, const char *password,
1048 		AGENT_RESULT *result)
1049 {
1050 	const char	*__function_name = "check_vcenter_hv_hw_memory";
1051 
1052 	int		ret;
1053 
1054 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name);
1055 
1056 	ret = get_vcenter_hvprop(request, username, password, ZBX_VMWARE_HVPROP_HW_MEMORY_SIZE, result);
1057 
1058 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __function_name, sysinfo_ret_string(ret));
1059 
1060 	return ret;
1061 }
1062 
check_vcenter_hv_hw_model(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)1063 int	check_vcenter_hv_hw_model(AGENT_REQUEST *request, const char *username, const char *password,
1064 		AGENT_RESULT *result)
1065 {
1066 	const char	*__function_name = "check_vcenter_hv_hw_model";
1067 
1068 	int		ret;
1069 
1070 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name);
1071 
1072 	ret = get_vcenter_hvprop(request, username, password, ZBX_VMWARE_HVPROP_HW_MODEL, result);
1073 
1074 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __function_name, sysinfo_ret_string(ret));
1075 
1076 	return ret;
1077 }
1078 
check_vcenter_hv_hw_uuid(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)1079 int	check_vcenter_hv_hw_uuid(AGENT_REQUEST *request, const char *username, const char *password,
1080 		AGENT_RESULT *result)
1081 {
1082 	const char	*__function_name = "check_vcenter_hv_hw_uuid";
1083 
1084 	int		ret;
1085 
1086 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name);
1087 
1088 	ret = get_vcenter_hvprop(request, username, password, ZBX_VMWARE_HVPROP_HW_UUID, result);
1089 
1090 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __function_name, sysinfo_ret_string(ret));
1091 
1092 	return ret;
1093 }
1094 
check_vcenter_hv_hw_vendor(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)1095 int	check_vcenter_hv_hw_vendor(AGENT_REQUEST *request, const char *username, const char *password,
1096 		AGENT_RESULT *result)
1097 {
1098 	const char	*__function_name = "check_vcenter_hv_hw_vendor";
1099 
1100 	int		ret;
1101 
1102 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name);
1103 
1104 	ret = get_vcenter_hvprop(request, username, password, ZBX_VMWARE_HVPROP_HW_VENDOR, result);
1105 
1106 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __function_name, sysinfo_ret_string(ret));
1107 
1108 	return ret;
1109 }
1110 
check_vcenter_hv_memory_size_ballooned(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)1111 int	check_vcenter_hv_memory_size_ballooned(AGENT_REQUEST *request, const char *username, const char *password,
1112 		AGENT_RESULT *result)
1113 {
1114 	const char	*__function_name = "check_vcenter_hv_memory_size_ballooned";
1115 
1116 	int			i, ret = SYSINFO_RET_FAIL;
1117 	zbx_vmware_service_t	*service;
1118 	const char		*uuid, *url;
1119 	zbx_vmware_hv_t		*hv;
1120 	zbx_uint64_t		value = 0;
1121 
1122 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name);
1123 
1124 	if (2 != request->nparam)
1125 	{
1126 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid number of parameters."));
1127 		goto out;
1128 	}
1129 
1130 	url = get_rparam(request, 0);
1131 	uuid = get_rparam(request, 1);
1132 
1133 	if ('\0' == *uuid)
1134 	{
1135 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid second parameter."));
1136 		goto out;
1137 	}
1138 
1139 	zbx_vmware_lock();
1140 
1141 	if (NULL == (service = get_vmware_service(url, username, password, result, &ret)))
1142 		goto unlock;
1143 
1144 	if (NULL == (hv = hv_get(&service->data->hvs, uuid)))
1145 	{
1146 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Unknown hypervisor uuid."));
1147 		goto unlock;
1148 	}
1149 
1150 	for (i = 0; i < hv->vms.values_num; i++)
1151 	{
1152 		zbx_uint64_t	mem;
1153 		const char	*value_str;
1154 		zbx_vmware_vm_t	*vm = (zbx_vmware_vm_t *)hv->vms.values[i];
1155 
1156 		if (NULL == (value_str = vm->props[ZBX_VMWARE_VMPROP_MEMORY_SIZE_BALLOONED]))
1157 			continue;
1158 
1159 		if (SUCCEED != is_uint64(value_str, &mem))
1160 			continue;
1161 
1162 		value += mem;
1163 	}
1164 
1165 	value *= ZBX_MEBIBYTE;
1166 	SET_UI64_RESULT(result, value);
1167 
1168 	ret = SYSINFO_RET_OK;
1169 unlock:
1170 	zbx_vmware_unlock();
1171 out:
1172 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __function_name, sysinfo_ret_string(ret));
1173 
1174 	return ret;
1175 }
1176 
check_vcenter_hv_memory_used(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)1177 int	check_vcenter_hv_memory_used(AGENT_REQUEST *request, const char *username, const char *password,
1178 		AGENT_RESULT *result)
1179 {
1180 	const char	*__function_name = "check_vcenter_hv_memory_used";
1181 
1182 	int		ret;
1183 
1184 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name);
1185 
1186 	ret = get_vcenter_hvprop(request, username, password, ZBX_VMWARE_HVPROP_MEMORY_USED, result);
1187 
1188 	if (SYSINFO_RET_OK == ret && NULL != GET_UI64_RESULT(result))
1189 		result->ui64 = result->ui64 * ZBX_MEBIBYTE;
1190 
1191 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __function_name, sysinfo_ret_string(ret));
1192 
1193 	return ret;
1194 }
1195 
check_vcenter_hv_sensor_health_state(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)1196 int	check_vcenter_hv_sensor_health_state(AGENT_REQUEST *request, const char *username, const char *password,
1197 		AGENT_RESULT *result)
1198 {
1199 	const char	*__function_name = "check_vcenter_hv_sensor_health_state";
1200 
1201 	int		ret;
1202 
1203 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name);
1204 
1205 	ret = get_vcenter_hvprop(request, username, password, ZBX_VMWARE_HVPROP_HEALTH_STATE, result);
1206 
1207 	if (SYSINFO_RET_OK == ret && NULL != GET_STR_RESULT(result))
1208 	{
1209 		if (0 == strcmp(result->str, "gray") || 0 == strcmp(result->str, "unknown"))
1210 			SET_UI64_RESULT(result, 0);
1211 		else if (0 == strcmp(result->str, "green"))
1212 			SET_UI64_RESULT(result, 1);
1213 		else if (0 == strcmp(result->str, "yellow"))
1214 			SET_UI64_RESULT(result, 2);
1215 		else if (0 == strcmp(result->str, "red"))
1216 			SET_UI64_RESULT(result, 3);
1217 		else
1218 			ret = SYSINFO_RET_FAIL;
1219 
1220 		UNSET_STR_RESULT(result);
1221 	}
1222 
1223 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __function_name, sysinfo_ret_string(ret));
1224 
1225 	return ret;
1226 }
1227 
check_vcenter_hv_status(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)1228 int	check_vcenter_hv_status(AGENT_REQUEST *request, const char *username, const char *password,
1229 		AGENT_RESULT *result)
1230 {
1231 	const char	*__function_name = "check_vcenter_hv_status";
1232 
1233 	int		ret;
1234 
1235 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name);
1236 
1237 	ret = get_vcenter_hvprop(request, username, password, ZBX_VMWARE_HVPROP_STATUS, result);
1238 
1239 	if (SYSINFO_RET_OK == ret && NULL != GET_STR_RESULT(result))
1240 	{
1241 		if (0 == strcmp(result->str, "gray") || 0 == strcmp(result->str, "unknown"))
1242 			SET_UI64_RESULT(result, 0);
1243 		else if (0 == strcmp(result->str, "green"))
1244 			SET_UI64_RESULT(result, 1);
1245 		else if (0 == strcmp(result->str, "yellow"))
1246 			SET_UI64_RESULT(result, 2);
1247 		else if (0 == strcmp(result->str, "red"))
1248 			SET_UI64_RESULT(result, 3);
1249 		else
1250 			ret = SYSINFO_RET_FAIL;
1251 
1252 		UNSET_STR_RESULT(result);
1253 	}
1254 
1255 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __function_name, sysinfo_ret_string(ret));
1256 
1257 	return ret;
1258 }
check_vcenter_hv_uptime(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)1259 int	check_vcenter_hv_uptime(AGENT_REQUEST *request, const char *username, const char *password,
1260 		AGENT_RESULT *result)
1261 {
1262 	const char	*__function_name = "check_vcenter_hv_uptime";
1263 
1264 	int		ret;
1265 
1266 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name);
1267 
1268 	ret = get_vcenter_hvprop(request, username, password, ZBX_VMWARE_HVPROP_UPTIME, result);
1269 
1270 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __function_name, sysinfo_ret_string(ret));
1271 
1272 	return ret;
1273 }
1274 
check_vcenter_hv_version(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)1275 int	check_vcenter_hv_version(AGENT_REQUEST *request, const char *username, const char *password,
1276 		AGENT_RESULT *result)
1277 {
1278 	const char	*__function_name = "check_vcenter_hv_version";
1279 
1280 	int		ret;
1281 
1282 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name);
1283 
1284 	ret = get_vcenter_hvprop(request, username, password, ZBX_VMWARE_HVPROP_VERSION, result);
1285 
1286 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __function_name, sysinfo_ret_string(ret));
1287 
1288 	return ret;
1289 }
1290 
check_vcenter_hv_vm_num(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)1291 int	check_vcenter_hv_vm_num(AGENT_REQUEST *request, const char *username, const char *password,
1292 		AGENT_RESULT *result)
1293 {
1294 	const char	*__function_name = "check_vcenter_hv_vm_num";
1295 
1296 	int			ret = SYSINFO_RET_FAIL;
1297 	zbx_vmware_service_t	*service;
1298 	const char		*uuid, *url;
1299 	zbx_vmware_hv_t		*hv;
1300 
1301 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name);
1302 
1303 	if (2 != request->nparam)
1304 	{
1305 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid number of parameters."));
1306 		goto out;
1307 	}
1308 
1309 	url = get_rparam(request, 0);
1310 	uuid = get_rparam(request, 1);
1311 
1312 	if ('\0' == *uuid)
1313 	{
1314 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid second parameter."));
1315 		goto out;
1316 	}
1317 
1318 	zbx_vmware_lock();
1319 
1320 	if (NULL == (service = get_vmware_service(url, username, password, result, &ret)))
1321 		goto unlock;
1322 
1323 	if (NULL == (hv = hv_get(&service->data->hvs, uuid)))
1324 	{
1325 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Unknown hypervisor uuid."));
1326 		goto unlock;
1327 	}
1328 
1329 	SET_UI64_RESULT(result, hv->vms.values_num);
1330 	ret = SYSINFO_RET_OK;
1331 unlock:
1332 	zbx_vmware_unlock();
1333 out:
1334 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __function_name, sysinfo_ret_string(ret));
1335 
1336 	return ret;
1337 }
1338 
check_vcenter_hv_network_in(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)1339 int	check_vcenter_hv_network_in(AGENT_REQUEST *request, const char *username, const char *password,
1340 		AGENT_RESULT *result)
1341 {
1342 	const char		*__function_name = "check_vcenter_hv_network_in";
1343 
1344 	char			*url, *mode, *uuid;
1345 	zbx_vmware_service_t	*service;
1346 	zbx_vmware_hv_t		*hv;
1347 	int			ret = SYSINFO_RET_FAIL;
1348 
1349 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name);
1350 
1351 	if (2 > request->nparam || request->nparam > 3)
1352 	{
1353 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid number of parameters."));
1354 		goto out;
1355 	}
1356 
1357 	url = get_rparam(request, 0);
1358 	uuid = get_rparam(request, 1);
1359 	mode = get_rparam(request, 2);
1360 
1361 	if (NULL != mode && '\0' != *mode && 0 != strcmp(mode, "bps"))
1362 	{
1363 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid third parameter."));
1364 		goto out;
1365 	}
1366 
1367 	zbx_vmware_lock();
1368 
1369 	if (NULL == (service = get_vmware_service(url, username, password, result, &ret)))
1370 		goto unlock;
1371 
1372 	if (NULL == (hv = hv_get(&service->data->hvs, uuid)))
1373 	{
1374 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Unknown hypervisor uuid."));
1375 		goto unlock;
1376 	}
1377 
1378 	ret = vmware_service_get_counter_value_by_path(service, "HostSystem", hv->id, "net/received[average]", "",
1379 			ZBX_KIBIBYTE, result);
1380 unlock:
1381 	zbx_vmware_unlock();
1382 out:
1383 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __function_name, sysinfo_ret_string(ret));
1384 
1385 	return ret;
1386 }
1387 
check_vcenter_hv_network_out(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)1388 int	check_vcenter_hv_network_out(AGENT_REQUEST *request, const char *username, const char *password,
1389 		AGENT_RESULT *result)
1390 {
1391 	const char		*__function_name = "check_vcenter_hv_network_out";
1392 
1393 	char			*url, *mode, *uuid;
1394 	zbx_vmware_service_t	*service;
1395 	zbx_vmware_hv_t		*hv;
1396 	int			ret = SYSINFO_RET_FAIL;
1397 
1398 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name);
1399 
1400 	if (2 > request->nparam || request->nparam > 3)
1401 	{
1402 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid number of parameters."));
1403 		goto out;
1404 	}
1405 
1406 	url = get_rparam(request, 0);
1407 	uuid = get_rparam(request, 1);
1408 	mode = get_rparam(request, 2);
1409 
1410 	if (NULL != mode && '\0' != *mode && 0 != strcmp(mode, "bps"))
1411 	{
1412 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid third parameter."));
1413 		goto out;
1414 	}
1415 
1416 	zbx_vmware_lock();
1417 
1418 	if (NULL == (service = get_vmware_service(url, username, password, result, &ret)))
1419 		goto unlock;
1420 
1421 	if (NULL == (hv = hv_get(&service->data->hvs, uuid)))
1422 	{
1423 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Unknown hypervisor uuid."));
1424 		goto unlock;
1425 	}
1426 
1427 	ret = vmware_service_get_counter_value_by_path(service, "HostSystem", hv->id, "net/transmitted[average]", "",
1428 			ZBX_KIBIBYTE, result);
1429 unlock:
1430 	zbx_vmware_unlock();
1431 out:
1432 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __function_name, sysinfo_ret_string(ret));
1433 
1434 	return ret;
1435 }
1436 
check_vcenter_hv_datastore_discovery(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)1437 int	check_vcenter_hv_datastore_discovery(AGENT_REQUEST *request, const char *username, const char *password,
1438 		AGENT_RESULT *result)
1439 {
1440 	const char		*__function_name = "check_vcenter_hv_datastore_discovery";
1441 
1442 	char			*url, *uuid;
1443 	zbx_vmware_service_t	*service;
1444 	zbx_vmware_hv_t		*hv;
1445 	struct zbx_json		json_data;
1446 	int			i, ret = SYSINFO_RET_FAIL;
1447 
1448 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name);
1449 
1450 	if (2 != request->nparam)
1451 	{
1452 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid number of parameters."));
1453 		goto out;
1454 	}
1455 
1456 	url = get_rparam(request, 0);
1457 	uuid = get_rparam(request, 1);
1458 
1459 	zbx_vmware_lock();
1460 
1461 	if (NULL == (service = get_vmware_service(url, username, password, result, &ret)))
1462 		goto unlock;
1463 
1464 	if (NULL == (hv = hv_get(&service->data->hvs, uuid)))
1465 	{
1466 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Unknown hypervisor uuid."));
1467 		goto unlock;
1468 	}
1469 
1470 	zbx_json_init(&json_data, ZBX_JSON_STAT_BUF_LEN);
1471 	zbx_json_addarray(&json_data, ZBX_PROTO_TAG_DATA);
1472 
1473 	for (i = 0; i < hv->datastores.values_num; i++)
1474 	{
1475 		zbx_vmware_datastore_t	*datastore = hv->datastores.values[i];
1476 
1477 		zbx_json_addobject(&json_data, NULL);
1478 		zbx_json_addstring(&json_data, "{#DATASTORE}", datastore->name, ZBX_JSON_TYPE_STRING);
1479 		zbx_json_close(&json_data);
1480 	}
1481 
1482 	zbx_json_close(&json_data);
1483 
1484 	SET_STR_RESULT(result, zbx_strdup(NULL, json_data.buffer));
1485 
1486 	zbx_json_free(&json_data);
1487 
1488 	ret = SYSINFO_RET_OK;
1489 unlock:
1490 	zbx_vmware_unlock();
1491 out:
1492 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __function_name, sysinfo_ret_string(ret));
1493 
1494 	return ret;
1495 }
1496 
check_vcenter_hv_datastore_read(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)1497 int	check_vcenter_hv_datastore_read(AGENT_REQUEST *request, const char *username, const char *password,
1498 		AGENT_RESULT *result)
1499 {
1500 	const char		*__function_name = "check_vcenter_hv_datastore_read";
1501 
1502 	char			*url, *mode, *uuid, *name;
1503 	zbx_vmware_service_t	*service;
1504 	zbx_vmware_hv_t		*hv;
1505 	int			i, ret = SYSINFO_RET_FAIL;
1506 
1507 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name);
1508 
1509 	if (3 > request->nparam || request->nparam > 4)
1510 	{
1511 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid number of parameters."));
1512 		goto out;
1513 	}
1514 
1515 	url = get_rparam(request, 0);
1516 	uuid = get_rparam(request, 1);
1517 	name = get_rparam(request, 2);
1518 	mode = get_rparam(request, 3);
1519 
1520 	if (NULL != mode && '\0' != *mode && 0 != strcmp(mode, "latency"))
1521 	{
1522 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid fourth parameter."));
1523 		goto out;
1524 	}
1525 
1526 	zbx_vmware_lock();
1527 
1528 	if (NULL == (service = get_vmware_service(url, username, password, result, &ret)))
1529 		goto unlock;
1530 
1531 	if (NULL == (hv = hv_get(&service->data->hvs, uuid)))
1532 	{
1533 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Unknown hypervisor uuid."));
1534 		goto unlock;
1535 	}
1536 
1537 	for (i = 0; i < hv->datastores.values_num; i++)
1538 	{
1539 		zbx_vmware_datastore_t	*datastore = hv->datastores.values[i];
1540 
1541 		if (0 == strcmp(name, datastore->name))
1542 		{
1543 			if (NULL == datastore->uuid)
1544 			{
1545 				SET_MSG_RESULT(result, zbx_strdup(NULL, "Unknown datastore uuid."));
1546 				goto unlock;
1547 			}
1548 
1549 			ret = vmware_service_get_counter_value_by_path(service, "HostSystem", hv->id,
1550 					"datastore/totalReadLatency[average]", datastore->uuid, 1, result);
1551 			goto unlock;
1552 		}
1553 	}
1554 
1555 	ret = SYSINFO_RET_OK;
1556 unlock:
1557 	zbx_vmware_unlock();
1558 out:
1559 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __function_name, sysinfo_ret_string(ret));
1560 
1561 	return ret;
1562 }
1563 
check_vcenter_hv_datastore_write(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)1564 int	check_vcenter_hv_datastore_write(AGENT_REQUEST *request, const char *username, const char *password,
1565 		AGENT_RESULT *result)
1566 {
1567 	const char		*__function_name = "check_vcenter_hv_datastore_write";
1568 
1569 	char			*url, *mode, *uuid, *name;
1570 	zbx_vmware_service_t	*service;
1571 	zbx_vmware_hv_t		*hv;
1572 	int			i, ret = SYSINFO_RET_FAIL;
1573 
1574 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name);
1575 
1576 	if (3 > request->nparam || request->nparam > 4)
1577 	{
1578 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid number of parameters."));
1579 		goto out;
1580 	}
1581 
1582 	url = get_rparam(request, 0);
1583 	uuid = get_rparam(request, 1);
1584 	name = get_rparam(request, 2);
1585 	mode = get_rparam(request, 3);
1586 
1587 	if (NULL != mode && '\0' != *mode && 0 != strcmp(mode, "latency"))
1588 	{
1589 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid fourth parameter."));
1590 		goto out;
1591 	}
1592 
1593 	zbx_vmware_lock();
1594 
1595 	if (NULL == (service = get_vmware_service(url, username, password, result, &ret)))
1596 		goto unlock;
1597 
1598 	if (NULL == (hv = hv_get(&service->data->hvs, uuid)))
1599 	{
1600 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Unknown hypervisor uuid."));
1601 		goto unlock;
1602 	}
1603 
1604 	for (i = 0; i < hv->datastores.values_num; i++)
1605 	{
1606 		zbx_vmware_datastore_t	*datastore = hv->datastores.values[i];
1607 
1608 		if (0 == strcmp(name, datastore->name))
1609 		{
1610 			if (NULL == datastore->uuid)
1611 				break;
1612 
1613 			ret = vmware_service_get_counter_value_by_path(service, "HostSystem", hv->id,
1614 					"datastore/totalWriteLatency[average]", datastore->uuid, 1, result);
1615 			goto unlock;
1616 		}
1617 	}
1618 
1619 	ret = SYSINFO_RET_OK;
1620 unlock:
1621 	zbx_vmware_unlock();
1622 out:
1623 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __function_name, sysinfo_ret_string(ret));
1624 
1625 	return ret;
1626 }
1627 
check_vcenter_hv_datastore_size_vsphere(int mode,const zbx_vmware_datastore_t * datastore,AGENT_RESULT * result)1628 static int	check_vcenter_hv_datastore_size_vsphere(int mode, const zbx_vmware_datastore_t *datastore,
1629 		AGENT_RESULT *result)
1630 {
1631 	switch (mode)
1632 	{
1633 		case ZBX_VMWARE_DATASTORE_SIZE_TOTAL:
1634 			if (ZBX_MAX_UINT64 == datastore->capacity)
1635 			{
1636 				SET_MSG_RESULT(result, zbx_strdup(NULL, "Datastore \"capacity\" is not available."));
1637 				return SYSINFO_RET_FAIL;
1638 			}
1639 			SET_UI64_RESULT(result, datastore->capacity);
1640 			break;
1641 		case ZBX_VMWARE_DATASTORE_SIZE_FREE:
1642 			if (ZBX_MAX_UINT64 == datastore->free_space)
1643 			{
1644 				SET_MSG_RESULT(result, zbx_strdup(NULL, "Datastore \"free space\" is not available."));
1645 				return SYSINFO_RET_FAIL;
1646 			}
1647 			SET_UI64_RESULT(result, datastore->free_space);
1648 			break;
1649 		case ZBX_VMWARE_DATASTORE_SIZE_UNCOMMITTED:
1650 			if (ZBX_MAX_UINT64 == datastore->uncommitted)
1651 			{
1652 				SET_MSG_RESULT(result, zbx_strdup(NULL, "Datastore \"uncommitted\" is not available."));
1653 				return SYSINFO_RET_FAIL;
1654 			}
1655 			SET_UI64_RESULT(result, datastore->uncommitted);
1656 			break;
1657 		case ZBX_VMWARE_DATASTORE_SIZE_PFREE:
1658 			if (ZBX_MAX_UINT64 == datastore->capacity)
1659 			{
1660 				SET_MSG_RESULT(result, zbx_strdup(NULL, "Datastore \"capacity\" is not available."));
1661 				return SYSINFO_RET_FAIL;
1662 			}
1663 			if (ZBX_MAX_UINT64 == datastore->free_space)
1664 			{
1665 				SET_MSG_RESULT(result, zbx_strdup(NULL, "Datastore \"free space\" is not available."));
1666 				return SYSINFO_RET_FAIL;
1667 			}
1668 			if (0 == datastore->capacity)
1669 			{
1670 				SET_MSG_RESULT(result, zbx_strdup(NULL, "Datastore \"capacity\" is zero."));
1671 				return SYSINFO_RET_FAIL;
1672 			}
1673 			SET_DBL_RESULT(result, (double)datastore->free_space / datastore->capacity * 100);
1674 			break;
1675 	}
1676 
1677 	return SYSINFO_RET_OK;
1678 }
1679 
check_vcenter_hv_datastore_size(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)1680 int	check_vcenter_hv_datastore_size(AGENT_REQUEST *request, const char *username, const char *password,
1681 		AGENT_RESULT *result)
1682 {
1683 #define		ZBX_DATASTORE_TOTAL			""
1684 #define		ZBX_DATASTORE_COUNTER_CAPACITY		0x01
1685 #define		ZBX_DATASTORE_COUNTER_USED		0x02
1686 #define		ZBX_DATASTORE_COUNTER_PROVISIONED	0x04
1687 
1688 	const char		*__function_name = "check_vcenter_hv_datastore_size";
1689 
1690 	char			*url, *param, *uuid, *name;
1691 	zbx_vmware_service_t	*service;
1692 	zbx_vmware_hv_t		*hv;
1693 	int			i, ret = SYSINFO_RET_FAIL, mode;
1694 	zbx_vmware_datastore_t	*datastore;
1695 	zbx_uint64_t		disk_used, disk_provisioned, disk_capacity;
1696 	unsigned int		flags;
1697 
1698 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name);
1699 
1700 	if (3 > request->nparam || request->nparam > 4)
1701 	{
1702 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid number of parameters."));
1703 		goto out;
1704 	}
1705 
1706 	url = get_rparam(request, 0);
1707 	uuid = get_rparam(request, 1);
1708 	name = get_rparam(request, 2);
1709 	param = get_rparam(request, 3);
1710 
1711 	if (NULL == param || '\0' == *param || 0 == strcmp(param, "total"))
1712 	{
1713 		mode = ZBX_VMWARE_DATASTORE_SIZE_TOTAL;
1714 		flags = ZBX_DATASTORE_COUNTER_CAPACITY;
1715 	}
1716 	else if (0 == strcmp(param, "free"))
1717 	{
1718 		mode = ZBX_VMWARE_DATASTORE_SIZE_FREE;
1719 		flags = ZBX_DATASTORE_COUNTER_CAPACITY | ZBX_DATASTORE_COUNTER_USED;
1720 	}
1721 	else if (0 == strcmp(param, "pfree"))
1722 	{
1723 		mode = ZBX_VMWARE_DATASTORE_SIZE_PFREE;
1724 		flags = ZBX_DATASTORE_COUNTER_CAPACITY | ZBX_DATASTORE_COUNTER_USED;
1725 	}
1726 	else if (0 == strcmp(param, "uncommitted"))
1727 	{
1728 		mode = ZBX_VMWARE_DATASTORE_SIZE_UNCOMMITTED;
1729 		flags = ZBX_DATASTORE_COUNTER_PROVISIONED | ZBX_DATASTORE_COUNTER_USED;
1730 	}
1731 	else
1732 	{
1733 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid fourth parameter."));
1734 		goto out;
1735 	}
1736 
1737 	zbx_vmware_lock();
1738 
1739 	if (NULL == (service = get_vmware_service(url, username, password, result, &ret)))
1740 		goto unlock;
1741 
1742 	if (NULL == (hv = hv_get(&service->data->hvs, uuid)))
1743 	{
1744 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Unknown hypervisor uuid."));
1745 		goto unlock;
1746 	}
1747 
1748 	for (i = 0; i < hv->datastores.values_num; i++)
1749 	{
1750 		datastore = hv->datastores.values[i];
1751 		if (0 == strcmp(name, datastore->name))
1752 			break;
1753 	}
1754 
1755 	if (NULL == datastore)
1756 	{
1757 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Unknown datastore name."));
1758 		goto unlock;
1759 	}
1760 
1761 	if (ZBX_VMWARE_TYPE_VSPHERE == service->type)
1762 	{
1763 		ret = check_vcenter_hv_datastore_size_vsphere(mode, datastore, result);
1764 		goto unlock;
1765 	}
1766 
1767 	if (0 != (flags & ZBX_DATASTORE_COUNTER_PROVISIONED))
1768 	{
1769 		ret = vmware_service_get_counter_value_by_path(service, "Datastore", datastore->id,
1770 				"disk/provisioned[latest]", ZBX_DATASTORE_TOTAL, ZBX_KIBIBYTE, result);
1771 
1772 		if (SYSINFO_RET_OK != ret || NULL == GET_UI64_RESULT(result))
1773 			goto unlock;
1774 
1775 		disk_provisioned = *GET_UI64_RESULT(result);
1776 		UNSET_UI64_RESULT(result);
1777 	}
1778 
1779 	if (0 != (flags & ZBX_DATASTORE_COUNTER_USED))
1780 	{
1781 		ret = vmware_service_get_counter_value_by_path(service, "Datastore", datastore->id,
1782 				"disk/used[latest]", ZBX_DATASTORE_TOTAL, ZBX_KIBIBYTE, result);
1783 
1784 		if (SYSINFO_RET_OK != ret || NULL == GET_UI64_RESULT(result))
1785 			goto unlock;
1786 
1787 		disk_used = *GET_UI64_RESULT(result);
1788 		UNSET_UI64_RESULT(result);
1789 	}
1790 
1791 	if (0 != (flags & ZBX_DATASTORE_COUNTER_CAPACITY))
1792 	{
1793 		ret = vmware_service_get_counter_value_by_path(service, "Datastore", datastore->id,
1794 				"disk/capacity[latest]", ZBX_DATASTORE_TOTAL, ZBX_KIBIBYTE, result);
1795 
1796 		if (SYSINFO_RET_OK != ret || NULL == GET_UI64_RESULT(result))
1797 			goto unlock;
1798 
1799 		disk_capacity = *GET_UI64_RESULT(result);
1800 		UNSET_UI64_RESULT(result);
1801 	}
1802 
1803 	switch (mode)
1804 	{
1805 		case ZBX_VMWARE_DATASTORE_SIZE_TOTAL:
1806 			SET_UI64_RESULT(result, disk_capacity);
1807 			break;
1808 		case ZBX_VMWARE_DATASTORE_SIZE_FREE:
1809 			SET_UI64_RESULT(result, disk_capacity - disk_used);
1810 			break;
1811 		case ZBX_VMWARE_DATASTORE_SIZE_UNCOMMITTED:
1812 			SET_UI64_RESULT(result, disk_provisioned - disk_used);
1813 			break;
1814 		case ZBX_VMWARE_DATASTORE_SIZE_PFREE:
1815 			SET_DBL_RESULT(result, (double) (disk_capacity - disk_used) / disk_capacity * 100);
1816 			break;
1817 	}
1818 
1819 	ret = SYSINFO_RET_OK;
1820 unlock:
1821 	zbx_vmware_unlock();
1822 out:
1823 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __function_name, sysinfo_ret_string(ret));
1824 
1825 	return ret;
1826 
1827 #undef	ZBX_DATASTORE_COUNTER_PROVISIONED
1828 #undef	ZBX_DATASTORE_COUNTER_USED
1829 #undef	ZBX_DATASTORE_COUNTER_CAPACITY
1830 #undef	ZBX_DATASTORE_TOTAL
1831 }
1832 
check_vcenter_hv_perfcounter(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)1833 int	check_vcenter_hv_perfcounter(AGENT_REQUEST *request, const char *username, const char *password,
1834 		AGENT_RESULT *result)
1835 {
1836 	const char		*__function_name = "check_vcenter_hv_perfcounter";
1837 
1838 	char			*url, *uuid, *path, *instance;
1839 	zbx_vmware_service_t	*service;
1840 	zbx_vmware_hv_t		*hv;
1841 	zbx_uint64_t		counterid;
1842 	int			ret = SYSINFO_RET_FAIL;
1843 
1844 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name);
1845 
1846 	if (3 > request->nparam || request->nparam > 4)
1847 	{
1848 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid number of parameters."));
1849 		goto out;
1850 	}
1851 
1852 	url = get_rparam(request, 0);
1853 	uuid = get_rparam(request, 1);
1854 	path = get_rparam(request, 2);
1855 	instance = get_rparam(request, 3);
1856 
1857 	if (NULL == instance)
1858 		instance = "";
1859 
1860 	zbx_vmware_lock();
1861 
1862 	if (NULL == (service = get_vmware_service(url, username, password, result, &ret)))
1863 		goto unlock;
1864 
1865 	if (NULL == (hv = hv_get(&service->data->hvs, uuid)))
1866 	{
1867 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Unknown hypervisor uuid."));
1868 		goto unlock;
1869 	}
1870 
1871 	if (FAIL == zbx_vmware_service_get_counterid(service, path, &counterid))
1872 	{
1873 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Performance counter is not available."));
1874 		goto unlock;
1875 	}
1876 
1877 	/* FAIL is returned if counter already exists */
1878 	if (SUCCEED == zbx_vmware_service_add_perf_counter(service, "HostSystem", hv->id, counterid, "*"))
1879 	{
1880 		ret = SYSINFO_RET_OK;
1881 		goto unlock;
1882 	}
1883 
1884 	/* the performance counter is already being monitored, try to get the results from statistics */
1885 	ret = vmware_service_get_counter_value_by_id(service, "HostSystem", hv->id, counterid, instance, 1, result);
1886 unlock:
1887 	zbx_vmware_unlock();
1888 out:
1889 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __function_name, sysinfo_ret_string(ret));
1890 
1891 	return ret;
1892 }
1893 
1894 
check_vcenter_vm_cpu_num(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)1895 int	check_vcenter_vm_cpu_num(AGENT_REQUEST *request, const char *username, const char *password,
1896 		AGENT_RESULT *result)
1897 {
1898 	const char	*__function_name = "check_vcenter_vm_cpu_num";
1899 
1900 	int		ret;
1901 
1902 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name);
1903 
1904 	ret = get_vcenter_vmprop(request, username, password, ZBX_VMWARE_VMPROP_CPU_NUM, result);
1905 
1906 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __function_name, sysinfo_ret_string(ret));
1907 
1908 	return ret;
1909 }
1910 
check_vcenter_vm_cluster_name(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)1911 int	check_vcenter_vm_cluster_name(AGENT_REQUEST *request, const char *username, const char *password,
1912 		AGENT_RESULT *result)
1913 {
1914 	const char		*__function_name = "check_vcenter_vm_cluster_name";
1915 
1916 	char			*url, *uuid;
1917 	zbx_vmware_service_t	*service;
1918 	zbx_vmware_cluster_t	*cluster = NULL;
1919 	int			ret = SYSINFO_RET_FAIL;
1920 	zbx_vmware_hv_t		*hv;
1921 
1922 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name);
1923 
1924 	if (2 != request->nparam)
1925 	{
1926 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid number of parameters."));
1927 		goto out;
1928 	}
1929 
1930 	url = get_rparam(request, 0);
1931 	uuid = get_rparam(request, 1);
1932 
1933 	if ('\0' == *uuid)
1934 	{
1935 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid second parameter."));
1936 		goto out;
1937 	}
1938 
1939 	zbx_vmware_lock();
1940 
1941 	if (NULL == (service = get_vmware_service(url, username, password, result, &ret)))
1942 		goto unlock;
1943 
1944 	if (NULL == (hv = service_hv_get_by_vm_uuid(service, uuid)))
1945 	{
1946 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Unknown virtual machine uuid."));
1947 		goto unlock;
1948 	}
1949 	if (NULL != hv->clusterid)
1950 		cluster = cluster_get(&service->data->clusters, hv->clusterid);
1951 
1952 	SET_STR_RESULT(result, zbx_strdup(NULL, NULL != cluster ? cluster->name : ""));
1953 
1954 	ret = SYSINFO_RET_OK;
1955 unlock:
1956 	zbx_vmware_unlock();
1957 out:
1958 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __function_name, sysinfo_ret_string(ret));
1959 
1960 	return ret;
1961 }
1962 
check_vcenter_vm_cpu_ready(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)1963 int	check_vcenter_vm_cpu_ready(AGENT_REQUEST *request, const char *username, const char *password,
1964 		AGENT_RESULT *result)
1965 {
1966 	const char		*__function_name = "check_vcenter_vm_cpu_ready";
1967 
1968 	zbx_vmware_service_t	*service;
1969 	int			ret = SYSINFO_RET_FAIL;
1970 	const char		*url, *uuid;
1971 
1972 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name);
1973 
1974 	if (2 != request->nparam)
1975 	{
1976 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid number of parameters."));
1977 		goto out;
1978 	}
1979 
1980 	url = get_rparam(request, 0);
1981 	uuid = get_rparam(request, 1);
1982 
1983 	if ('\0' == *uuid)
1984 	{
1985 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid second parameter."));
1986 		goto out;
1987 	}
1988 
1989 	zbx_vmware_lock();
1990 
1991 	if (NULL == (service = get_vmware_service(url, username, password, result, &ret)))
1992 		goto unlock;
1993 
1994 	ret = vmware_service_get_vm_counter(service, uuid, "", "cpu/ready[summation]", 1, result);
1995 unlock:
1996 	zbx_vmware_unlock();
1997 out:
1998 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __function_name, sysinfo_ret_string(ret));
1999 
2000 	return ret;
2001 }
2002 
check_vcenter_vm_cpu_usage(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)2003 int	check_vcenter_vm_cpu_usage(AGENT_REQUEST *request, const char *username, const char *password,
2004 		AGENT_RESULT *result)
2005 {
2006 	const char	*__function_name = "check_vcenter_vm_cpu_usage";
2007 
2008 	int		ret;
2009 
2010 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name);
2011 
2012 	ret = get_vcenter_vmprop(request, username, password, ZBX_VMWARE_VMPROP_CPU_USAGE, result);
2013 
2014 	if (SYSINFO_RET_OK == ret && NULL != GET_UI64_RESULT(result))
2015 		result->ui64 = result->ui64 * 1000000;
2016 
2017 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __function_name, sysinfo_ret_string(ret));
2018 
2019 	return ret;
2020 }
2021 
check_vcenter_vm_discovery(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)2022 int	check_vcenter_vm_discovery(AGENT_REQUEST *request, const char *username, const char *password,
2023 		AGENT_RESULT *result)
2024 {
2025 	const char		*__function_name = "check_vcenter_vm_discovery";
2026 
2027 	struct zbx_json		json_data;
2028 	char			*url, *vm_name, *hv_name;
2029 	zbx_vmware_service_t	*service;
2030 	zbx_vmware_hv_t		*hv;
2031 	zbx_vmware_vm_t		*vm;
2032 	zbx_hashset_iter_t	iter;
2033 	int			i, ret = SYSINFO_RET_FAIL;
2034 
2035 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name);
2036 
2037 	if (1 != request->nparam)
2038 	{
2039 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid number of parameters."));
2040 		goto out;
2041 	}
2042 
2043 	url = get_rparam(request, 0);
2044 
2045 	zbx_vmware_lock();
2046 
2047 	if (NULL == (service = get_vmware_service(url, username, password, result, &ret)))
2048 		goto unlock;
2049 
2050 	zbx_json_init(&json_data, ZBX_JSON_STAT_BUF_LEN);
2051 	zbx_json_addarray(&json_data, ZBX_PROTO_TAG_DATA);
2052 
2053 	zbx_hashset_iter_reset(&service->data->hvs, &iter);
2054 	while (NULL != (hv = zbx_hashset_iter_next(&iter)))
2055 	{
2056 		zbx_vmware_cluster_t	*cluster = NULL;
2057 
2058 		if (NULL != hv->clusterid)
2059 			cluster = cluster_get(&service->data->clusters, hv->clusterid);
2060 
2061 		for (i = 0; i < hv->vms.values_num; i++)
2062 		{
2063 			vm = (zbx_vmware_vm_t *)hv->vms.values[i];
2064 
2065 			if (NULL == (vm_name = vm->props[ZBX_VMWARE_VMPROP_NAME]))
2066 				continue;
2067 
2068 			if (NULL == (hv_name = hv->props[ZBX_VMWARE_HVPROP_NAME]))
2069 				continue;
2070 
2071 			zbx_json_addobject(&json_data, NULL);
2072 			zbx_json_addstring(&json_data, "{#VM.UUID}", vm->uuid, ZBX_JSON_TYPE_STRING);
2073 			zbx_json_addstring(&json_data, "{#VM.ID}", vm->id, ZBX_JSON_TYPE_STRING);
2074 			zbx_json_addstring(&json_data, "{#VM.NAME}", vm_name, ZBX_JSON_TYPE_STRING);
2075 			zbx_json_addstring(&json_data, "{#HV.NAME}", hv_name, ZBX_JSON_TYPE_STRING);
2076 			zbx_json_addstring(&json_data, "{#CLUSTER.NAME}",
2077 					NULL != cluster ? cluster->name : "", ZBX_JSON_TYPE_STRING);
2078 			zbx_json_close(&json_data);
2079 		}
2080 	}
2081 
2082 	zbx_json_close(&json_data);
2083 
2084 	SET_STR_RESULT(result, zbx_strdup(NULL, json_data.buffer));
2085 
2086 	zbx_json_free(&json_data);
2087 
2088 	ret = SYSINFO_RET_OK;
2089 unlock:
2090 	zbx_vmware_unlock();
2091 out:
2092 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __function_name, sysinfo_ret_string(ret));
2093 
2094 	return ret;
2095 }
2096 
check_vcenter_vm_hv_name(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)2097 int	check_vcenter_vm_hv_name(AGENT_REQUEST *request, const char *username, const char *password,
2098 		AGENT_RESULT *result)
2099 {
2100 	const char		*__function_name = "check_vcenter_vm_hv_name";
2101 
2102 	zbx_vmware_service_t	*service;
2103 	zbx_vmware_hv_t		*hv;
2104 	char			*url, *uuid, *name;
2105 	int			ret = SYSINFO_RET_FAIL;
2106 
2107 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name);
2108 
2109 	if (2 != request->nparam)
2110 	{
2111 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid number of parameters."));
2112 		goto out;
2113 	}
2114 
2115 	url = get_rparam(request, 0);
2116 	uuid = get_rparam(request, 1);
2117 
2118 	if ('\0' == *uuid)
2119 	{
2120 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid second parameter."));
2121 		goto out;
2122 	}
2123 
2124 	zbx_vmware_lock();
2125 
2126 	if (NULL == (service = get_vmware_service(url, username, password, result, &ret)))
2127 		goto unlock;
2128 
2129 
2130 	if (NULL == (hv = service_hv_get_by_vm_uuid(service, uuid)))
2131 	{
2132 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Unknown virtual machine uuid."));
2133 		goto unlock;
2134 	}
2135 
2136 	if (NULL == (name = hv->props[ZBX_VMWARE_HVPROP_NAME]))
2137 	{
2138 		SET_MSG_RESULT(result, zbx_strdup(NULL, "No hypervisor name found."));
2139 		goto unlock;
2140 	}
2141 
2142 	SET_STR_RESULT(result, zbx_strdup(NULL, name));
2143 	ret = SYSINFO_RET_OK;
2144 unlock:
2145 	zbx_vmware_unlock();
2146 out:
2147 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __function_name, sysinfo_ret_string(ret));
2148 
2149 	return ret;
2150 }
2151 
check_vcenter_vm_memory_size(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)2152 int	check_vcenter_vm_memory_size(AGENT_REQUEST *request, const char *username, const char *password,
2153 		AGENT_RESULT *result)
2154 {
2155 	const char	*__function_name = "check_vcenter_vm_memory_size";
2156 
2157 	int		ret;
2158 
2159 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name);
2160 
2161 	ret = get_vcenter_vmprop(request, username, password, ZBX_VMWARE_VMPROP_MEMORY_SIZE, result);
2162 
2163 	if (SYSINFO_RET_OK == ret && NULL != GET_UI64_RESULT(result))
2164 		result->ui64 = result->ui64 * ZBX_MEBIBYTE;
2165 
2166 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __function_name, sysinfo_ret_string(ret));
2167 
2168 	return ret;
2169 }
2170 
check_vcenter_vm_memory_size_ballooned(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)2171 int	check_vcenter_vm_memory_size_ballooned(AGENT_REQUEST *request, const char *username, const char *password,
2172 		AGENT_RESULT *result)
2173 {
2174 	const char	*__function_name = "check_vcenter_vm_memory_size_ballooned";
2175 
2176 	int		ret;
2177 
2178 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name);
2179 
2180 	ret = get_vcenter_vmprop(request, username, password, ZBX_VMWARE_VMPROP_MEMORY_SIZE_BALLOONED, result);
2181 
2182 	if (SYSINFO_RET_OK == ret && NULL != GET_UI64_RESULT(result))
2183 		result->ui64 = result->ui64 * ZBX_MEBIBYTE;
2184 
2185 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __function_name, sysinfo_ret_string(ret));
2186 
2187 	return ret;
2188 }
2189 
check_vcenter_vm_memory_size_compressed(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)2190 int	check_vcenter_vm_memory_size_compressed(AGENT_REQUEST *request, const char *username, const char *password,
2191 		AGENT_RESULT *result)
2192 {
2193 	const char	*__function_name = "check_vcenter_vm_memory_size_compressed";
2194 
2195 	int		ret;
2196 
2197 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name);
2198 
2199 	ret = get_vcenter_vmprop(request, username, password, ZBX_VMWARE_VMPROP_MEMORY_SIZE_COMPRESSED, result);
2200 
2201 	if (SYSINFO_RET_OK == ret && NULL != GET_UI64_RESULT(result))
2202 		result->ui64 = result->ui64 * ZBX_MEBIBYTE;
2203 
2204 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __function_name, sysinfo_ret_string(ret));
2205 
2206 	return ret;
2207 }
2208 
check_vcenter_vm_memory_size_swapped(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)2209 int	check_vcenter_vm_memory_size_swapped(AGENT_REQUEST *request, const char *username, const char *password,
2210 		AGENT_RESULT *result)
2211 {
2212 	const char	*__function_name = "check_vcenter_vm_memory_size_swapped";
2213 
2214 	int		ret;
2215 
2216 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name);
2217 
2218 	ret = get_vcenter_vmprop(request, username, password, ZBX_VMWARE_VMPROP_MEMORY_SIZE_SWAPPED, result);
2219 
2220 	if (SYSINFO_RET_OK == ret && NULL != GET_UI64_RESULT(result))
2221 		result->ui64 = result->ui64 * ZBX_MEBIBYTE;
2222 
2223 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __function_name, sysinfo_ret_string(ret));
2224 
2225 	return ret;
2226 }
2227 
check_vcenter_vm_memory_size_usage_guest(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)2228 int	check_vcenter_vm_memory_size_usage_guest(AGENT_REQUEST *request, const char *username, const char *password,
2229 		AGENT_RESULT *result)
2230 {
2231 	const char	*__function_name = "check_vcenter_vm_memory_size_usage_guest";
2232 
2233 	int		ret;
2234 
2235 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name);
2236 
2237 	ret = get_vcenter_vmprop(request, username, password, ZBX_VMWARE_VMPROP_MEMORY_SIZE_USAGE_GUEST, result);
2238 
2239 	if (SYSINFO_RET_OK == ret && NULL != GET_UI64_RESULT(result))
2240 		result->ui64 = result->ui64 * ZBX_MEBIBYTE;
2241 
2242 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __function_name, sysinfo_ret_string(ret));
2243 
2244 	return ret;
2245 }
2246 
check_vcenter_vm_memory_size_usage_host(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)2247 int	check_vcenter_vm_memory_size_usage_host(AGENT_REQUEST *request, const char *username, const char *password,
2248 		AGENT_RESULT *result)
2249 {
2250 	const char	*__function_name = "check_vcenter_vm_memory_size_usage_host";
2251 
2252 	int		ret;
2253 
2254 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name);
2255 
2256 	ret = get_vcenter_vmprop(request, username, password, ZBX_VMWARE_VMPROP_MEMORY_SIZE_USAGE_HOST, result);
2257 
2258 	if (SYSINFO_RET_OK == ret && NULL != GET_UI64_RESULT(result))
2259 		result->ui64 = result->ui64 * ZBX_MEBIBYTE;
2260 
2261 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __function_name, sysinfo_ret_string(ret));
2262 
2263 	return ret;
2264 }
2265 
check_vcenter_vm_memory_size_private(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)2266 int	check_vcenter_vm_memory_size_private(AGENT_REQUEST *request, const char *username, const char *password,
2267 		AGENT_RESULT *result)
2268 {
2269 	const char	*__function_name = "check_vcenter_vm_memory_size_private";
2270 
2271 	int		ret;
2272 
2273 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name);
2274 
2275 	ret = get_vcenter_vmprop(request, username, password, ZBX_VMWARE_VMPROP_MEMORY_SIZE_PRIVATE, result);
2276 
2277 	if (SYSINFO_RET_OK == ret && NULL != GET_UI64_RESULT(result))
2278 		result->ui64 = result->ui64 * ZBX_MEBIBYTE;
2279 
2280 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __function_name, sysinfo_ret_string(ret));
2281 
2282 	return ret;
2283 }
2284 
check_vcenter_vm_memory_size_shared(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)2285 int	check_vcenter_vm_memory_size_shared(AGENT_REQUEST *request, const char *username, const char *password,
2286 		AGENT_RESULT *result)
2287 {
2288 	const char	*__function_name = "check_vcenter_vm_memory_size_shared";
2289 
2290 	int		ret;
2291 
2292 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name);
2293 
2294 	ret = get_vcenter_vmprop(request, username, password, ZBX_VMWARE_VMPROP_MEMORY_SIZE_SHARED, result);
2295 
2296 	if (SYSINFO_RET_OK == ret && NULL != GET_UI64_RESULT(result))
2297 		result->ui64 = result->ui64 * ZBX_MEBIBYTE;
2298 
2299 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __function_name, sysinfo_ret_string(ret));
2300 
2301 	return ret;
2302 }
2303 
check_vcenter_vm_powerstate(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)2304 int	check_vcenter_vm_powerstate(AGENT_REQUEST *request, const char *username, const char *password,
2305 		AGENT_RESULT *result)
2306 {
2307 	const char	*__function_name = "check_vcenter_vm_powerstate";
2308 
2309 	int		ret;
2310 
2311 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name);
2312 
2313 	ret = get_vcenter_vmprop(request, username, password, ZBX_VMWARE_VMPROP_POWER_STATE, result);
2314 
2315 	if (SYSINFO_RET_OK == ret)
2316 		ret = vmware_set_powerstate_result(result);
2317 
2318 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __function_name, sysinfo_ret_string(ret));
2319 
2320 	return ret;
2321 }
2322 
check_vcenter_vm_net_if_discovery(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)2323 int	check_vcenter_vm_net_if_discovery(AGENT_REQUEST *request, const char *username, const char *password,
2324 		AGENT_RESULT *result)
2325 {
2326 	const char		*__function_name = "check_vcenter_vm_net_if_discovery";
2327 
2328 	struct zbx_json		json_data;
2329 	zbx_vmware_service_t	*service;
2330 	zbx_vmware_vm_t		*vm = NULL;
2331 	zbx_vmware_dev_t	*dev;
2332 	char			*url, *uuid;
2333 	int			i, ret = SYSINFO_RET_FAIL;
2334 
2335 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name);
2336 
2337 	if (2 != request->nparam)
2338 	{
2339 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid number of parameters."));
2340 		goto out;
2341 	}
2342 
2343 	url = get_rparam(request, 0);
2344 	uuid = get_rparam(request, 1);
2345 
2346 	if ('\0' == *uuid)
2347 	{
2348 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid second parameter."));
2349 		goto out;
2350 	}
2351 
2352 	zbx_vmware_lock();
2353 
2354 	if (NULL == (service = get_vmware_service(url, username, password, result, &ret)))
2355 		goto unlock;
2356 
2357 	if (NULL == (vm = service_vm_get(service, uuid)))
2358 	{
2359 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Unknown virtual machine uuid."));
2360 		goto unlock;
2361 	}
2362 
2363 	zbx_json_init(&json_data, ZBX_JSON_STAT_BUF_LEN);
2364 	zbx_json_addarray(&json_data, ZBX_PROTO_TAG_DATA);
2365 
2366 	for (i = 0; i < vm->devs.values_num; i++)
2367 	{
2368 		dev = (zbx_vmware_dev_t *)vm->devs.values[i];
2369 
2370 		if (ZBX_VMWARE_DEV_TYPE_NIC != dev->type)
2371 			continue;
2372 
2373 		zbx_json_addobject(&json_data, NULL);
2374 		zbx_json_addstring(&json_data, "{#IFNAME}", dev->instance, ZBX_JSON_TYPE_STRING);
2375 		if (NULL != dev->label)
2376 			zbx_json_addstring(&json_data, "{#IFDESC}", dev->label, ZBX_JSON_TYPE_STRING);
2377 
2378 		zbx_json_close(&json_data);
2379 	}
2380 
2381 	zbx_json_close(&json_data);
2382 
2383 	SET_STR_RESULT(result, zbx_strdup(NULL, json_data.buffer));
2384 
2385 	zbx_json_free(&json_data);
2386 
2387 	ret = SYSINFO_RET_OK;
2388 unlock:
2389 	zbx_vmware_unlock();
2390 out:
2391 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __function_name, sysinfo_ret_string(ret));
2392 
2393 	return ret;
2394 }
2395 
check_vcenter_vm_net_if_in(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)2396 int	check_vcenter_vm_net_if_in(AGENT_REQUEST *request, const char *username, const char *password,
2397 		AGENT_RESULT *result)
2398 {
2399 	const char		*__function_name = "check_vcenter_vm_net_if_in";
2400 
2401 	char			*url, *uuid, *instance, *mode;
2402 	zbx_vmware_service_t	*service;
2403 	const char		*path;
2404 	int 			coeff, ret = SYSINFO_RET_FAIL;
2405 
2406 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name);
2407 
2408 	if (3 > request->nparam || request->nparam > 4)
2409 	{
2410 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid number of parameters."));
2411 		goto out;
2412 	}
2413 
2414 	url = get_rparam(request, 0);
2415 	uuid = get_rparam(request, 1);
2416 	instance = get_rparam(request, 2);
2417 	mode = get_rparam(request, 3);
2418 
2419 	if ('\0' == *uuid)
2420 	{
2421 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid second parameter."));
2422 		goto out;
2423 	}
2424 
2425 	if ('\0' == *instance)
2426 	{
2427 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid third parameter."));
2428 		goto out;
2429 	}
2430 
2431 	zbx_vmware_lock();
2432 
2433 	if (NULL == (service = get_vmware_service(url, username, password, result, &ret)))
2434 		goto unlock;
2435 
2436 	if (NULL == mode || '\0' == *mode || 0 == strcmp(mode, "bps"))
2437 	{
2438 		path = "net/received[average]";
2439 		coeff = ZBX_KIBIBYTE;
2440 	}
2441 	else if (0 == strcmp(mode, "pps"))
2442 	{
2443 		path = "net/packetsRx[summation]";
2444 		coeff = 1;
2445 	}
2446 	else
2447 	{
2448 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid fourth parameter."));
2449 		goto unlock;
2450 	}
2451 
2452 	ret = vmware_service_get_vm_counter(service, uuid, instance, path, coeff, result);
2453 unlock:
2454 	zbx_vmware_unlock();
2455 out:
2456 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __function_name, sysinfo_ret_string(ret));
2457 
2458 	return ret;
2459 }
2460 
check_vcenter_vm_net_if_out(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)2461 int	check_vcenter_vm_net_if_out(AGENT_REQUEST *request, const char *username, const char *password,
2462 		AGENT_RESULT *result)
2463 {
2464 	const char		*__function_name = "check_vcenter_vm_net_if_out";
2465 
2466 	char			*url, *uuid, *instance, *mode;
2467 	zbx_vmware_service_t	*service;
2468 	const char		*path;
2469 	int 			coeff, ret = SYSINFO_RET_FAIL;
2470 
2471 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name);
2472 
2473 	if (3 > request->nparam || request->nparam > 4)
2474 	{
2475 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid number of parameters."));
2476 		goto out;
2477 	}
2478 
2479 	url = get_rparam(request, 0);
2480 	uuid = get_rparam(request, 1);
2481 	instance = get_rparam(request, 2);
2482 	mode = get_rparam(request, 3);
2483 
2484 	if ('\0' == *uuid)
2485 	{
2486 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid second parameter."));
2487 		goto out;
2488 	}
2489 
2490 	if ('\0' == *instance)
2491 	{
2492 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid third parameter."));
2493 		goto out;
2494 	}
2495 
2496 	zbx_vmware_lock();
2497 
2498 	if (NULL == (service = get_vmware_service(url, username, password, result, &ret)))
2499 		goto unlock;
2500 
2501 	if (NULL == mode || '\0' == *mode || 0 == strcmp(mode, "bps"))
2502 	{
2503 		path = "net/transmitted[average]";
2504 		coeff = ZBX_KIBIBYTE;
2505 	}
2506 	else if (0 == strcmp(mode, "pps"))
2507 	{
2508 		path = "net/packetsTx[summation]";
2509 		coeff = 1;
2510 	}
2511 	else
2512 	{
2513 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid fourth parameter."));
2514 		goto unlock;
2515 	}
2516 
2517 	ret = vmware_service_get_vm_counter(service, uuid, instance, path, coeff, result);
2518 unlock:
2519 	zbx_vmware_unlock();
2520 out:
2521 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __function_name, sysinfo_ret_string(ret));
2522 
2523 	return ret;
2524 }
2525 
check_vcenter_vm_storage_committed(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)2526 int	check_vcenter_vm_storage_committed(AGENT_REQUEST *request, const char *username, const char *password,
2527 		AGENT_RESULT *result)
2528 {
2529 	const char	*__function_name = "check_vcenter_vm_storage_committed";
2530 
2531 	int		ret;
2532 
2533 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name);
2534 
2535 	ret = get_vcenter_vmprop(request, username, password, ZBX_VMWARE_VMPROP_STORAGE_COMMITED, result);
2536 
2537 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __function_name, sysinfo_ret_string(ret));
2538 
2539 	return ret;
2540 }
2541 
check_vcenter_vm_storage_unshared(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)2542 int	check_vcenter_vm_storage_unshared(AGENT_REQUEST *request, const char *username, const char *password,
2543 		AGENT_RESULT *result)
2544 {
2545 	const char	*__function_name = "check_vcenter_vm_storage_unshared";
2546 
2547 	int		ret;
2548 
2549 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name);
2550 
2551 	ret = get_vcenter_vmprop(request, username, password, ZBX_VMWARE_VMPROP_STORAGE_UNSHARED, result);
2552 
2553 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __function_name, sysinfo_ret_string(ret));
2554 
2555 	return ret;
2556 }
2557 
check_vcenter_vm_storage_uncommitted(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)2558 int	check_vcenter_vm_storage_uncommitted(AGENT_REQUEST *request, const char *username, const char *password,
2559 		AGENT_RESULT *result)
2560 {
2561 	const char	*__function_name = "check_vcenter_vm_storage_uncommitted";
2562 
2563 	int		ret;
2564 
2565 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name);
2566 
2567 	ret = get_vcenter_vmprop(request, username, password, ZBX_VMWARE_VMPROP_STORAGE_UNCOMMITTED, result);
2568 
2569 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __function_name, sysinfo_ret_string(ret));
2570 
2571 	return ret;
2572 }
2573 
check_vcenter_vm_uptime(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)2574 int	check_vcenter_vm_uptime(AGENT_REQUEST *request, const char *username, const char *password,
2575 		AGENT_RESULT *result)
2576 {
2577 	const char	*__function_name = "check_vcenter_vm_uptime";
2578 
2579 	int		ret;
2580 
2581 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name);
2582 
2583 	ret = get_vcenter_vmprop(request, username, password, ZBX_VMWARE_VMPROP_UPTIME, result);
2584 
2585 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __function_name, sysinfo_ret_string(ret));
2586 
2587 	return ret;
2588 }
2589 
check_vcenter_vm_vfs_dev_discovery(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)2590 int	check_vcenter_vm_vfs_dev_discovery(AGENT_REQUEST *request, const char *username, const char *password,
2591 		AGENT_RESULT *result)
2592 {
2593 	const char		*__function_name = "check_vcenter_vm_vfs_dev_discovery";
2594 
2595 	struct zbx_json		json_data;
2596 	zbx_vmware_service_t	*service;
2597 	zbx_vmware_vm_t		*vm = NULL;
2598 	zbx_vmware_dev_t	*dev;
2599 	char			*url, *uuid;
2600 	int			i, ret = SYSINFO_RET_FAIL;
2601 
2602 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name);
2603 
2604 	if (2 != request->nparam)
2605 	{
2606 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid number of parameters."));
2607 		goto out;
2608 	}
2609 
2610 	url = get_rparam(request, 0);
2611 	uuid = get_rparam(request, 1);
2612 
2613 	if ('\0' == *uuid)
2614 	{
2615 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid second parameter."));
2616 		goto out;
2617 	}
2618 
2619 	zbx_vmware_lock();
2620 
2621 	if (NULL == (service = get_vmware_service(url, username, password, result, &ret)))
2622 		goto unlock;
2623 
2624 	if (NULL == (vm = service_vm_get(service, uuid)))
2625 	{
2626 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Unknown virtual machine uuid."));
2627 		goto unlock;
2628 	}
2629 
2630 	zbx_json_init(&json_data, ZBX_JSON_STAT_BUF_LEN);
2631 	zbx_json_addarray(&json_data, ZBX_PROTO_TAG_DATA);
2632 
2633 	for (i = 0; i < vm->devs.values_num; i++)
2634 	{
2635 		dev = (zbx_vmware_dev_t *)vm->devs.values[i];
2636 
2637 		if (ZBX_VMWARE_DEV_TYPE_DISK != dev->type)
2638 			continue;
2639 
2640 		zbx_json_addobject(&json_data, NULL);
2641 		zbx_json_addstring(&json_data, "{#DISKNAME}", dev->instance, ZBX_JSON_TYPE_STRING);
2642 		if (NULL != dev->label)
2643 			zbx_json_addstring(&json_data, "{#DISKDESC}", dev->label, ZBX_JSON_TYPE_STRING);
2644 		zbx_json_close(&json_data);
2645 	}
2646 
2647 	zbx_json_close(&json_data);
2648 
2649 	SET_STR_RESULT(result, zbx_strdup(NULL, json_data.buffer));
2650 
2651 	zbx_json_free(&json_data);
2652 
2653 	ret = SYSINFO_RET_OK;
2654 unlock:
2655 	zbx_vmware_unlock();
2656 out:
2657 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __function_name, sysinfo_ret_string(ret));
2658 
2659 	return ret;
2660 }
2661 
check_vcenter_vm_vfs_dev_read(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)2662 int	check_vcenter_vm_vfs_dev_read(AGENT_REQUEST *request, const char *username, const char *password,
2663 		AGENT_RESULT *result)
2664 {
2665 	const char		*__function_name = "check_vcenter_vm_vfs_dev_read";
2666 
2667 	char			*url, *uuid, *instance, *mode;
2668 	zbx_vmware_service_t	*service;
2669 	const char		*path;
2670 	int			coeff, ret = SYSINFO_RET_FAIL;
2671 
2672 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name);
2673 
2674 	if (3 > request->nparam || request->nparam > 4)
2675 	{
2676 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid number of parameters."));
2677 		goto out;
2678 	}
2679 
2680 	url = get_rparam(request, 0);
2681 	uuid = get_rparam(request, 1);
2682 	instance = get_rparam(request, 2);
2683 	mode = get_rparam(request, 3);
2684 
2685 	if ('\0' == *uuid)
2686 	{
2687 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid second parameter."));
2688 		goto out;
2689 	}
2690 
2691 	if ('\0' == *instance)
2692 	{
2693 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid third parameter."));
2694 		goto out;
2695 	}
2696 
2697 	zbx_vmware_lock();
2698 
2699 	if (NULL == (service = get_vmware_service(url, username, password, result, &ret)))
2700 		goto unlock;
2701 
2702 	if (NULL == mode || '\0' == *mode || 0 == strcmp(mode, "bps"))
2703 	{
2704 		path = "virtualDisk/read[average]";
2705 		coeff = ZBX_KIBIBYTE;
2706 	}
2707 	else if (0 == strcmp(mode, "ops"))
2708 	{
2709 		path = "virtualDisk/numberReadAveraged[average]";
2710 		coeff = 1;
2711 	}
2712 	else
2713 	{
2714 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid fourth parameter."));
2715 		goto unlock;
2716 	}
2717 
2718 	ret =  vmware_service_get_vm_counter(service, uuid, instance, path, coeff, result);
2719 unlock:
2720 	zbx_vmware_unlock();
2721 out:
2722 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __function_name, sysinfo_ret_string(ret));
2723 
2724 	return ret;
2725 }
2726 
check_vcenter_vm_vfs_dev_write(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)2727 int	check_vcenter_vm_vfs_dev_write(AGENT_REQUEST *request, const char *username, const char *password,
2728 		AGENT_RESULT *result)
2729 {
2730 	const char		*__function_name = "check_vcenter_vm_vfs_dev_write";
2731 
2732 	char			*url, *uuid, *instance, *mode;
2733 	zbx_vmware_service_t	*service;
2734 	const char		*path;
2735 	int			coeff, ret = SYSINFO_RET_FAIL;
2736 
2737 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name);
2738 
2739 	if (3 > request->nparam || request->nparam > 4)
2740 	{
2741 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid number of parameters."));
2742 		goto out;
2743 	}
2744 
2745 	url = get_rparam(request, 0);
2746 	uuid = get_rparam(request, 1);
2747 	instance = get_rparam(request, 2);
2748 	mode = get_rparam(request, 3);
2749 
2750 	if ('\0' == *uuid)
2751 	{
2752 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid second parameter."));
2753 		goto out;
2754 	}
2755 
2756 	if ('\0' == *instance)
2757 	{
2758 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid third parameter."));
2759 		goto out;
2760 	}
2761 
2762 	zbx_vmware_lock();
2763 
2764 	if (NULL == (service = get_vmware_service(url, username, password, result, &ret)))
2765 		goto unlock;
2766 
2767 	if (NULL == mode || '\0' == *mode || 0 == strcmp(mode, "bps"))
2768 	{
2769 		path = "virtualDisk/write[average]";
2770 		coeff = ZBX_KIBIBYTE;
2771 	}
2772 	else if (0 == strcmp(mode, "ops"))
2773 	{
2774 		path = "virtualDisk/numberWriteAveraged[average]";
2775 		coeff = 1;
2776 	}
2777 	else
2778 	{
2779 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid fourth parameter."));
2780 		goto unlock;
2781 	}
2782 
2783 	ret =  vmware_service_get_vm_counter(service, uuid, instance, path, coeff, result);
2784 unlock:
2785 	zbx_vmware_unlock();
2786 out:
2787 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __function_name, sysinfo_ret_string(ret));
2788 
2789 	return ret;
2790 }
2791 
check_vcenter_vm_vfs_fs_discovery(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)2792 int	check_vcenter_vm_vfs_fs_discovery(AGENT_REQUEST *request, const char *username, const char *password,
2793 		AGENT_RESULT *result)
2794 {
2795 	const char		*__function_name = "check_vcenter_vm_vfs_fs_discovery";
2796 
2797 	struct zbx_json		json_data;
2798 	zbx_vmware_service_t	*service;
2799 	zbx_vmware_vm_t		*vm = NULL;
2800 	char			*url, *uuid;
2801 	int			i, ret = SYSINFO_RET_FAIL;
2802 
2803 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name);
2804 
2805 	if (2 != request->nparam)
2806 	{
2807 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid number of parameters."));
2808 		goto out;
2809 	}
2810 
2811 	url = get_rparam(request, 0);
2812 	uuid = get_rparam(request, 1);
2813 
2814 	if ('\0' == *uuid)
2815 	{
2816 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid second parameter."));
2817 		goto out;
2818 	}
2819 
2820 	zbx_vmware_lock();
2821 
2822 	if (NULL == (service = get_vmware_service(url, username, password, result, &ret)))
2823 		goto unlock;
2824 
2825 	if (NULL == (vm = service_vm_get(service, uuid)))
2826 	{
2827 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Unknown virtual machine uuid."));
2828 		goto unlock;
2829 	}
2830 
2831 	zbx_json_init(&json_data, ZBX_JSON_STAT_BUF_LEN);
2832 	zbx_json_addarray(&json_data, ZBX_PROTO_TAG_DATA);
2833 
2834 	for (i = 0; i < vm->file_systems.values_num; i++)
2835 	{
2836 		zbx_vmware_fs_t	*fs = (zbx_vmware_fs_t *)vm->file_systems.values[i];
2837 
2838 		zbx_json_addobject(&json_data, NULL);
2839 		zbx_json_addstring(&json_data, "{#FSNAME}", fs->path, ZBX_JSON_TYPE_STRING);
2840 		zbx_json_close(&json_data);
2841 	}
2842 
2843 	zbx_json_close(&json_data);
2844 
2845 	SET_STR_RESULT(result, zbx_strdup(NULL, json_data.buffer));
2846 
2847 	zbx_json_free(&json_data);
2848 
2849 	ret = SYSINFO_RET_OK;
2850 unlock:
2851 	zbx_vmware_unlock();
2852 out:
2853 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __function_name, sysinfo_ret_string(ret));
2854 
2855 	return ret;
2856 }
2857 
check_vcenter_vm_vfs_fs_size(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)2858 int	check_vcenter_vm_vfs_fs_size(AGENT_REQUEST *request, const char *username, const char *password,
2859 		AGENT_RESULT *result)
2860 {
2861 	const char		*__function_name = "check_vcenter_vm_vfs_fs_size";
2862 
2863 	zbx_vmware_service_t	*service;
2864 	zbx_vmware_vm_t		*vm;
2865 	char			*url, *uuid, *fsname, *mode;
2866 	int			i, ret = SYSINFO_RET_FAIL;
2867 	zbx_vmware_fs_t		*fs = NULL;
2868 
2869 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name);
2870 
2871 	if (3 > request->nparam || request->nparam > 4)
2872 	{
2873 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid number of parameters."));
2874 		goto out;
2875 	}
2876 
2877 	url = get_rparam(request, 0);
2878 	uuid = get_rparam(request, 1);
2879 	fsname = get_rparam(request, 2);
2880 	mode = get_rparam(request, 3);
2881 
2882 	if ('\0' == *uuid)
2883 	{
2884 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid second parameter."));
2885 		goto out;
2886 	}
2887 
2888 	zbx_vmware_lock();
2889 
2890 	if (NULL == (service = get_vmware_service(url, username, password, result, &ret)))
2891 		goto unlock;
2892 
2893 	if (NULL == (vm = service_vm_get(service, uuid)))
2894 	{
2895 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Unknown virtual machine uuid."));
2896 		goto unlock;
2897 	}
2898 
2899 	for (i = 0; i < vm->file_systems.values_num; i++)
2900 	{
2901 		fs = (zbx_vmware_fs_t *)vm->file_systems.values[i];
2902 
2903 		if (0 == strcmp(fs->path, fsname))
2904 			break;
2905 	}
2906 
2907 	if (NULL == fs)
2908 	{
2909 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Unknown file system path."));
2910 		goto unlock;
2911 	}
2912 
2913 	ret = SYSINFO_RET_OK;
2914 
2915 	if (NULL == mode || '\0' == *mode || 0 == strcmp(mode, "total"))
2916 		SET_UI64_RESULT(result, fs->capacity);
2917 	else if (0 == strcmp(mode, "free"))
2918 		SET_UI64_RESULT(result, fs->free_space);
2919 	else if (0 == strcmp(mode, "used"))
2920 		SET_UI64_RESULT(result, fs->capacity - fs->free_space);
2921 	else if (0 == strcmp(mode, "pfree"))
2922 		SET_DBL_RESULT(result, 0 != fs->capacity ? (double)(100.0 * fs->free_space) / fs->capacity : 0);
2923 	else if (0 == strcmp(mode, "pused"))
2924 		SET_DBL_RESULT(result, 100.0 - (0 != fs->capacity ? 100.0 * fs->free_space / fs->capacity : 0));
2925 	else
2926 	{
2927 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid fourth parameter."));
2928 		ret = SYSINFO_RET_FAIL;
2929 	}
2930 unlock:
2931 	zbx_vmware_unlock();
2932 out:
2933 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __function_name, sysinfo_ret_string(ret));
2934 
2935 	return ret;
2936 }
2937 
check_vcenter_vm_perfcounter(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)2938 int	check_vcenter_vm_perfcounter(AGENT_REQUEST *request, const char *username, const char *password,
2939 		AGENT_RESULT *result)
2940 {
2941 	const char		*__function_name = "check_vcenter_vm_perfcounter";
2942 
2943 	char			*url, *uuid, *path, *instance;
2944 	zbx_vmware_service_t	*service;
2945 	zbx_vmware_vm_t		*vm;
2946 	zbx_uint64_t		counterid;
2947 	int			ret = SYSINFO_RET_FAIL;
2948 
2949 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name);
2950 
2951 	if (3 > request->nparam || request->nparam > 4)
2952 	{
2953 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid number of parameters."));
2954 		goto out;
2955 	}
2956 
2957 	url = get_rparam(request, 0);
2958 	uuid = get_rparam(request, 1);
2959 	path = get_rparam(request, 2);
2960 	instance = get_rparam(request, 3);
2961 
2962 	if (NULL == instance)
2963 		instance = "";
2964 
2965 	zbx_vmware_lock();
2966 
2967 	if (NULL == (service = get_vmware_service(url, username, password, result, &ret)))
2968 		goto unlock;
2969 
2970 	if (NULL == (vm = service_vm_get(service, uuid)))
2971 	{
2972 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Unknown virtual machine uuid."));
2973 		goto unlock;
2974 	}
2975 
2976 	if (FAIL == zbx_vmware_service_get_counterid(service, path, &counterid))
2977 	{
2978 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Performance counter is not available."));
2979 		goto unlock;
2980 	}
2981 
2982 	/* FAIL is returned if counter already exists */
2983 	if (SUCCEED == zbx_vmware_service_add_perf_counter(service, "VirtualMachine", vm->id, counterid, "*"))
2984 	{
2985 		ret = SYSINFO_RET_OK;
2986 		goto unlock;
2987 	}
2988 
2989 	/* the performance counter is already being monitored, try to get the results from statistics */
2990 	ret = vmware_service_get_counter_value_by_id(service, "VirtualMachine", vm->id, counterid, instance, 1, result);
2991 unlock:
2992 	zbx_vmware_unlock();
2993 out:
2994 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __function_name, sysinfo_ret_string(ret));
2995 
2996 	return ret;
2997 }
2998 
2999 #endif	/* defined(HAVE_LIBXML2) && defined(HAVE_LIBCURL) */
3000