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 
35 #define ZBX_DATASTORE_TOTAL			""
36 #define ZBX_DATASTORE_COUNTER_CAPACITY		0x01
37 #define ZBX_DATASTORE_COUNTER_USED		0x02
38 #define ZBX_DATASTORE_COUNTER_PROVISIONED	0x04
39 
vmware_set_powerstate_result(AGENT_RESULT * result)40 static int	vmware_set_powerstate_result(AGENT_RESULT *result)
41 {
42 	int	ret = SYSINFO_RET_OK;
43 
44 	if (NULL != GET_STR_RESULT(result))
45 	{
46 		if (0 == strcmp(result->str, "poweredOff"))
47 			SET_UI64_RESULT(result, 0);
48 		else if (0 == strcmp(result->str, "poweredOn"))
49 			SET_UI64_RESULT(result, 1);
50 		else if (0 == strcmp(result->str, "suspended"))
51 			SET_UI64_RESULT(result, 2);
52 		else
53 			ret = SYSINFO_RET_FAIL;
54 
55 		UNSET_STR_RESULT(result);
56 	}
57 
58 	return ret;
59 }
60 
61 /******************************************************************************
62  *                                                                            *
63  * Function: hv_get                                                           *
64  *                                                                            *
65  * Purpose: return pointer to Hypervisor data from hashset with uuid          *
66  *                                                                            *
67  * Parameters: hvs  - [IN] the hashset with all Hypervisors                   *
68  *             uuid - [IN] the uuid of Hypervisor                             *
69  *                                                                            *
70  * Return value: zbx_vmware_hv_t* - the operation has completed successfully  *
71  *               NULL             - the operation has failed                  *
72  *                                                                            *
73  ******************************************************************************/
hv_get(zbx_hashset_t * hvs,const char * uuid)74 static zbx_vmware_hv_t	*hv_get(zbx_hashset_t *hvs, const char *uuid)
75 {
76 	zbx_vmware_hv_t	*hv, hv_local = {.uuid = (char *)uuid};
77 
78 	zabbix_log(LOG_LEVEL_DEBUG, "In %s() uuid:'%s'", __func__, uuid);
79 
80 	hv = (zbx_vmware_hv_t *)zbx_hashset_search(hvs, &hv_local);
81 
82 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%p", __func__, (void *)hv);
83 
84 	return hv;
85 }
86 
87 /******************************************************************************
88  *                                                                            *
89  * Function: ds_get                                                           *
90  *                                                                            *
91  * Purpose: return pointer to Datastore data from vector with id              *
92  *                                                                            *
93  * Parameters: dss - [IN] the vector with all Datastores                      *
94  *             id  - [IN] the id of Datastore                                 *
95  *                                                                            *
96  * Return value:                                                              *
97  *        zbx_vmware_datastore_t* - the operation has completed successfully  *
98  *        NULL                    - the operation has failed                  *
99  *                                                                            *
100  ******************************************************************************/
ds_get(const zbx_vector_vmware_datastore_t * dss,const char * name)101 static zbx_vmware_datastore_t	*ds_get(const zbx_vector_vmware_datastore_t *dss, const char *name)
102 {
103 	int			i;
104 	zbx_vmware_datastore_t	ds_cmp;
105 
106 	ds_cmp.name = (char *)name;
107 
108 	if (FAIL == (i = zbx_vector_vmware_datastore_bsearch(dss, &ds_cmp, vmware_ds_name_compare)))
109 		return NULL;
110 
111 	return dss->values[i];
112 }
113 
service_hv_get_by_vm_uuid(zbx_vmware_service_t * service,const char * uuid)114 static zbx_vmware_hv_t	*service_hv_get_by_vm_uuid(zbx_vmware_service_t *service, const char *uuid)
115 {
116 	zbx_vmware_vm_t		vm_local = {.uuid = (char *)uuid};
117 	zbx_vmware_vm_index_t	vmi_local = {&vm_local, NULL}, *vmi;
118 	zbx_vmware_hv_t		*hv = NULL;
119 
120 	zabbix_log(LOG_LEVEL_DEBUG, "In %s() uuid:'%s'", __func__, uuid);
121 
122 	if (NULL != (vmi = (zbx_vmware_vm_index_t *)zbx_hashset_search(&service->data->vms_index, &vmi_local)))
123 		hv = vmi->hv;
124 	else
125 		hv = NULL;
126 
127 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%p", __func__, (void *)hv);
128 
129 	return hv;
130 
131 }
132 
service_vm_get(zbx_vmware_service_t * service,const char * uuid)133 static zbx_vmware_vm_t	*service_vm_get(zbx_vmware_service_t *service, const char *uuid)
134 {
135 	zbx_vmware_vm_t		vm_local = {.uuid = (char *)uuid}, *vm;
136 	zbx_vmware_vm_index_t	vmi_local = {&vm_local, NULL}, *vmi;
137 
138 	zabbix_log(LOG_LEVEL_DEBUG, "In %s() uuid:'%s'", __func__, uuid);
139 
140 	if (NULL != (vmi = (zbx_vmware_vm_index_t *)zbx_hashset_search(&service->data->vms_index, &vmi_local)))
141 		vm = vmi->vm;
142 	else
143 		vm = NULL;
144 
145 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%p", __func__, (void *)vm);
146 
147 	return vm;
148 }
149 
cluster_get(zbx_vector_ptr_t * clusters,const char * clusterid)150 static zbx_vmware_cluster_t	*cluster_get(zbx_vector_ptr_t *clusters, const char *clusterid)
151 {
152 	int			i;
153 	zbx_vmware_cluster_t	*cluster;
154 
155 	zabbix_log(LOG_LEVEL_DEBUG, "In %s() uuid:'%s'", __func__, clusterid);
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->id, clusterid))
162 			goto out;
163 	}
164 
165 	cluster = NULL;
166 out:
167 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%p", __func__, (void *)cluster);
168 
169 	return cluster;
170 }
171 
cluster_get_by_name(zbx_vector_ptr_t * clusters,const char * name)172 static zbx_vmware_cluster_t	*cluster_get_by_name(zbx_vector_ptr_t *clusters, const char *name)
173 {
174 	int			i;
175 	zbx_vmware_cluster_t	*cluster;
176 
177 	zabbix_log(LOG_LEVEL_DEBUG, "In %s() name:'%s'", __func__, name);
178 
179 	for (i = 0; i < clusters->values_num; i++)
180 	{
181 		cluster = (zbx_vmware_cluster_t *)clusters->values[i];
182 
183 		if (0 == strcmp(cluster->name, name))
184 			goto out;
185 	}
186 
187 	cluster = NULL;
188 out:
189 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%p", __func__, (void *)cluster);
190 
191 	return cluster;
192 }
193 
194 /******************************************************************************
195  *                                                                            *
196  * Function: vmware_service_get_counter_value_by_id                           *
197  *                                                                            *
198  * Purpose: gets vmware performance counter value by its identifier           *
199  *                                                                            *
200  * Parameters: service   - [IN] the vmware service                            *
201  *             type      - [IN] the performance entity type (HostSystem,      *
202  *                              VirtualMachine, Datastore)                    *
203  *             id        - [IN] the performance entity identifier             *
204  *             counterid - [IN] the performance counter identifier            *
205  *             instance  - [IN] the performance counter instance or "" for    *
206  *                              aggregate data                                *
207  *             coeff     - [IN] the coefficient to apply to the value         *
208  *             unit      - [IN] the counter unit info (kilo, mega, % etc)     *
209  *             result    - [OUT] the output result                            *
210  *                                                                            *
211  * Return value: SYSINFO_RET_OK, result has value - performance counter value *
212  *                               was successfully retrieved                   *
213  *               SYSINFO_RET_OK, result has no value - performance counter    *
214  *                               was found without a value                    *
215  *               SYSINFO_RET_FAIL - otherwise, error message is set in result *
216  *                                                                            *
217  * Comments: There can be situation when performance counter is configured    *
218  *           to be read but the collector has not yet processed it. In this   *
219  *           case return SYSINFO_RET_OK with empty result so that it is       *
220  *           ignored by server rather than generating error.                  *
221  *                                                                            *
222  ******************************************************************************/
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,unsigned int coeff,int unit,AGENT_RESULT * result)223 static int	vmware_service_get_counter_value_by_id(zbx_vmware_service_t *service, const char *type, const char *id,
224 		zbx_uint64_t counterid, const char *instance, unsigned int coeff, int unit, AGENT_RESULT *result)
225 {
226 	zbx_vmware_perf_entity_t	*entity;
227 	zbx_vmware_perf_counter_t	*perfcounter;
228 	zbx_str_uint64_pair_t		*perfvalue;
229 	int				i, ret = SYSINFO_RET_FAIL;
230 
231 	zabbix_log(LOG_LEVEL_DEBUG, "In %s() type:%s id:%s counterid:" ZBX_FS_UI64 " instance:%s", __func__,
232 			type, id, counterid, instance);
233 
234 	if (NULL == (entity = zbx_vmware_service_get_perf_entity(service, type, id)))
235 	{
236 		/* the requested counter has not been queried yet */
237 		zabbix_log(LOG_LEVEL_DEBUG, "performance data is not yet ready, ignoring request");
238 		ret = SYSINFO_RET_OK;
239 		goto out;
240 	}
241 
242 	if (NULL != entity->error)
243 	{
244 		SET_MSG_RESULT(result, zbx_strdup(NULL, entity->error));
245 		goto out;
246 	}
247 
248 	if (FAIL == (i = zbx_vector_ptr_bsearch(&entity->counters, &counterid, ZBX_DEFAULT_UINT64_PTR_COMPARE_FUNC)))
249 	{
250 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Performance counter data was not found."));
251 		goto out;
252 	}
253 
254 	perfcounter = (zbx_vmware_perf_counter_t *)entity->counters.values[i];
255 
256 	if (0 == (perfcounter->state & ZBX_VMWARE_COUNTER_READY))
257 	{
258 		ret = SYSINFO_RET_OK;
259 		goto out;
260 	}
261 
262 	if (0 == perfcounter->values.values_num)
263 	{
264 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Performance counter data is not available."));
265 		goto out;
266 	}
267 
268 	for (i = 0; i < perfcounter->values.values_num; i++)
269 	{
270 		perfvalue = &perfcounter->values.values[i];
271 
272 		if (0 == strcmp(perfvalue->name, instance))
273 			break;
274 	}
275 
276 	if (i == perfcounter->values.values_num)
277 	{
278 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Performance counter instance was not found."));
279 		goto out;
280 	}
281 
282 	/* VMware returns -1 value if the performance data for the specified period is not ready - ignore it */
283 	if (ZBX_MAX_UINT64 == perfvalue->value)
284 	{
285 		ret = SYSINFO_RET_OK;
286 		goto out;
287 	}
288 
289 	if (0 != coeff)
290 	{
291 		SET_UI64_RESULT(result, perfvalue->value * coeff);
292 	}
293 	else
294 	{
295 		switch (unit)
296 		{
297 		case ZBX_VMWARE_UNIT_KILOBYTES:
298 		case ZBX_VMWARE_UNIT_KILOBYTESPERSECOND:
299 			SET_UI64_RESULT(result, perfvalue->value * ZBX_KIBIBYTE);
300 			break;
301 		case ZBX_VMWARE_UNIT_MEGABYTES:
302 		case ZBX_VMWARE_UNIT_MEGABYTESPERSECOND:
303 			SET_UI64_RESULT(result, perfvalue->value * ZBX_MEBIBYTE);
304 			break;
305 		case ZBX_VMWARE_UNIT_TERABYTES:
306 			SET_UI64_RESULT(result, perfvalue->value * ZBX_TEBIBYTE);
307 			break;
308 		case ZBX_VMWARE_UNIT_PERCENT:
309 			SET_DBL_RESULT(result, (double)perfvalue->value / 100.0);
310 			break;
311 		case ZBX_VMWARE_UNIT_JOULE:
312 		case ZBX_VMWARE_UNIT_MEGAHERTZ:
313 		case ZBX_VMWARE_UNIT_MICROSECOND:
314 		case ZBX_VMWARE_UNIT_MILLISECOND:
315 		case ZBX_VMWARE_UNIT_NUMBER:
316 		case ZBX_VMWARE_UNIT_SECOND:
317 		case ZBX_VMWARE_UNIT_WATT:
318 		case ZBX_VMWARE_UNIT_CELSIUS:
319 			SET_UI64_RESULT(result, perfvalue->value);
320 			break;
321 		default:
322 			SET_MSG_RESULT(result, zbx_dsprintf(NULL, "Performance counter type of unitInfo is unknown. "
323 					"Counter id:" ZBX_FS_UI64, counterid));
324 			goto out;
325 		}
326 	}
327 
328 	ret = SYSINFO_RET_OK;
329 out:
330 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __func__, zbx_sysinfo_ret_string(ret));
331 
332 	return ret;
333 }
334 
335 /******************************************************************************
336  *                                                                            *
337  * Function: vmware_service_get_counter_value_by_path                         *
338  *                                                                            *
339  * Purpose: gets vmware performance counter value by the path                 *
340  *                                                                            *
341  * Parameters: service  - [IN] the vmware service                             *
342  *             type     - [IN] the performance entity type (HostSystem,       *
343  *                             VirtualMachine, Datastore)                     *
344  *             id       - [IN] the performance entity identifier              *
345  *             path     - [IN] the performance counter path                   *
346  *                             (<group>/<key>[<rollup type>])                 *
347  *             instance - [IN] the performance counter instance or "" for     *
348  *                             aggregate data                                 *
349  *             coeff    - [IN] the coefficient to apply to the value          *
350  *             result   - [OUT] the output result                             *
351  *                                                                            *
352  * Return value: SYSINFO_RET_OK, result has value - performance counter value *
353  *                               was successfully retrieved                   *
354  *               SYSINFO_RET_OK, result has no value - performance counter    *
355  *                               was found without a value                    *
356  *               SYSINFO_RET_FAIL - otherwise, error message is set in result *
357  *                                                                            *
358  * Comments: There can be situation when performance counter is configured    *
359  *           to be read but the collector has not yet processed it. In this   *
360  *           case return SYSINFO_RET_OK with empty result so that it is       *
361  *           ignored by server rather than generating error.                  *
362  *                                                                            *
363  ******************************************************************************/
vmware_service_get_counter_value_by_path(zbx_vmware_service_t * service,const char * type,const char * id,const char * path,const char * instance,unsigned int coeff,AGENT_RESULT * result)364 static int	vmware_service_get_counter_value_by_path(zbx_vmware_service_t *service, const char *type,
365 		const char *id, const char *path, const char *instance, unsigned int coeff, AGENT_RESULT *result)
366 {
367 	zbx_uint64_t	counterid;
368 	int		unit;
369 
370 	if (FAIL == zbx_vmware_service_get_counterid(service, path, &counterid, &unit))
371 	{
372 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Performance counter is not available."));
373 		return SYSINFO_RET_FAIL;
374 	}
375 
376 	return vmware_service_get_counter_value_by_id(service, type, id, counterid, instance, coeff, unit, result);
377 }
378 
vmware_service_get_vm_counter(zbx_vmware_service_t * service,const char * uuid,const char * instance,const char * path,unsigned int coeff,AGENT_RESULT * result)379 static int	vmware_service_get_vm_counter(zbx_vmware_service_t *service, const char *uuid, const char *instance,
380 		const char *path, unsigned int coeff, AGENT_RESULT *result)
381 {
382 	zbx_vmware_vm_t	*vm;
383 	int		ret = SYSINFO_RET_FAIL;
384 
385 	zabbix_log(LOG_LEVEL_DEBUG, "In %s() uuid:%s instance:%s path:%s", __func__, uuid, instance, path);
386 
387 	if (NULL == (vm = service_vm_get(service, uuid)))
388 	{
389 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Unknown virtual machine uuid."));
390 		goto out;
391 	}
392 
393 	ret = vmware_service_get_counter_value_by_path(service, "VirtualMachine", vm->id, path, instance, coeff,
394 			result);
395 out:
396 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __func__, zbx_sysinfo_ret_string(ret));
397 
398 	return ret;
399 }
400 
401 /******************************************************************************
402  *                                                                            *
403  * Function: get_vmware_service                                               *
404  *                                                                            *
405  * Purpose: gets vmware service object                                        *
406  *                                                                            *
407  * Parameters: url       - [IN] the vmware service URL                        *
408  *             username  - [IN] the vmware service username                   *
409  *             password  - [IN] the vmware service password                   *
410  *             ret       - [OUT] the operation result code                    *
411  *                                                                            *
412  * Return value: The vmware service object or NULL if the service was not     *
413  *               found, did not have data or any error occurred. In the last  *
414  *               case the error message will be stored in agent result.       *
415  *                                                                            *
416  * Comments: There are three possible cases:                                  *
417  *             1) the vmware service is not ready. This can happen when       *
418  *                service was added, but not yet processed by collector.      *
419  *                In this case NULL is returned and result code is set to     *
420  *                SYSINFO_RET_OK.                                             *
421  *             2) the vmware service update failed. This can happen if there  *
422  *                was a network problem, authentication failure or any error  *
423  *                that prevented from obtaining and parsing vmware data.      *
424  *                In this case NULL is returned and result code is set to     *
425  *                SYSINFO_RET_FAIL.                                           *
426  *             3) the vmware service has been updated successfully.           *
427  *                In this case the service object is returned and result code *
428  *                is not set.                                                 *
429  *                                                                            *
430  ******************************************************************************/
get_vmware_service(const char * url,const char * username,const char * password,AGENT_RESULT * result,int * ret)431 static zbx_vmware_service_t	*get_vmware_service(const char *url, const char *username, const char *password,
432 		AGENT_RESULT *result, int *ret)
433 {
434 	zbx_vmware_service_t	*service;
435 
436 	zabbix_log(LOG_LEVEL_DEBUG, "In %s() '%s'@'%s'", __func__, username, url);
437 
438 	if (NULL == (service = zbx_vmware_get_service(url, username, password)))
439 	{
440 		*ret = SYSINFO_RET_OK;
441 		goto out;
442 	}
443 
444 	if (0 != (service->state & ZBX_VMWARE_STATE_FAILED))
445 	{
446 		SET_MSG_RESULT(result, zbx_strdup(NULL, NULL != service->data->error ? service->data->error :
447 				"Unknown VMware service error."));
448 
449 		zabbix_log(LOG_LEVEL_DEBUG, "failed to query VMware service: %s",
450 				NULL != service->data->error ? service->data->error : "unknown error");
451 
452 		*ret = SYSINFO_RET_FAIL;
453 		service = NULL;
454 	}
455 out:
456 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%p", __func__, (void *)service);
457 
458 	return service;
459 }
460 
461 /******************************************************************************
462  *                                                                            *
463  * Function: get_vcenter_vmprop                                               *
464  *                                                                            *
465  * Purpose: retrieves data from virtual machine details                       *
466  *                                                                            *
467  * Parameters: request   - [IN] the original request. The first parameter is  *
468  *                              vmware service URL and the second parameter   *
469  *                              is virtual machine uuid.                      *
470  *             username  - [IN] the vmware service user name                  *
471  *             password  - [IN] the vmware service password                   *
472  *             xpath     - [IN] the xpath describing data to retrieve         *
473  *             result    - [OUT] the request result                           *
474  *                                                                            *
475  ******************************************************************************/
get_vcenter_vmprop(AGENT_REQUEST * request,const char * username,const char * password,int propid,AGENT_RESULT * result)476 static int	get_vcenter_vmprop(AGENT_REQUEST *request, const char *username, const char *password,
477 		int propid, AGENT_RESULT *result)
478 {
479 	zbx_vmware_service_t	*service;
480 	zbx_vmware_vm_t		*vm = NULL;
481 	const char		*url, *uuid, *value;
482 	int			ret = SYSINFO_RET_FAIL;
483 
484 	zabbix_log(LOG_LEVEL_DEBUG, "In %s() propid:%d", __func__, propid);
485 
486 	if (2 != request->nparam)
487 	{
488 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid number of parameters."));
489 		goto out;
490 	}
491 
492 	url = get_rparam(request, 0);
493 	uuid = get_rparam(request, 1);
494 
495 	if ('\0' == *uuid)
496 	{
497 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid second parameter."));
498 		goto out;
499 	}
500 
501 	zbx_vmware_lock();
502 
503 	if (NULL == (service = get_vmware_service(url, username, password, result, &ret)))
504 		goto unlock;
505 
506 	if (NULL == (vm = service_vm_get(service, uuid)))
507 	{
508 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Unknown virtual machine uuid."));
509 		goto unlock;
510 	}
511 
512 	if (NULL == (value = vm->props[propid]))
513 	{
514 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Value is not available."));
515 		goto unlock;
516 	}
517 
518 	SET_STR_RESULT(result, zbx_strdup(NULL, value));
519 
520 	ret = SYSINFO_RET_OK;
521 unlock:
522 	zbx_vmware_unlock();
523 out:
524 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __func__, zbx_sysinfo_ret_string(ret));
525 
526 	return ret;
527 }
528 
529 /******************************************************************************
530  *                                                                            *
531  * Function: get_vcenter_hvprop                                               *
532  *                                                                            *
533  * Purpose: retrieves hypervisor property                                     *
534  *                                                                            *
535  * Parameters: request   - [IN] the original request. The first parameter is  *
536  *                              vmware service URL and the second parameter   *
537  *                              is hypervisor uuid.                           *
538  *             username  - [IN] the vmware service user name                  *
539  *             password  - [IN] the vmware service password                   *
540  *             propid    - [IN] the property id                               *
541  *             result    - [OUT] the request result                           *
542  *                                                                            *
543  ******************************************************************************/
get_vcenter_hvprop(AGENT_REQUEST * request,const char * username,const char * password,int propid,AGENT_RESULT * result)544 static int	get_vcenter_hvprop(AGENT_REQUEST *request, const char *username, const char *password, int propid,
545 		AGENT_RESULT *result)
546 {
547 	zbx_vmware_service_t	*service;
548 	const char		*uuid, *url, *value;
549 	zbx_vmware_hv_t		*hv;
550 	int			ret = SYSINFO_RET_FAIL;
551 
552 	zabbix_log(LOG_LEVEL_DEBUG, "In %s() propid:%d", __func__, propid);
553 
554 	if (2 != request->nparam)
555 	{
556 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid number of parameters."));
557 		goto out;
558 	}
559 
560 	url = get_rparam(request, 0);
561 	uuid = get_rparam(request, 1);
562 
563 	if ('\0' == *uuid)
564 	{
565 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid second parameter."));
566 		goto out;
567 	}
568 
569 	zbx_vmware_lock();
570 
571 	if (NULL == (service = get_vmware_service(url, username, password, result, &ret)))
572 		goto unlock;
573 
574 	if (NULL == (hv = hv_get(&service->data->hvs, uuid)))
575 	{
576 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Unknown hypervisor uuid."));
577 		goto unlock;
578 	}
579 
580 	if (NULL == (value = hv->props[propid]))
581 	{
582 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Value is not available."));
583 		goto unlock;
584 	}
585 
586 	SET_STR_RESULT(result, zbx_strdup(NULL, value));
587 	ret = SYSINFO_RET_OK;
588 unlock:
589 	zbx_vmware_unlock();
590 out:
591 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __func__, zbx_sysinfo_ret_string(ret));
592 
593 	return ret;
594 }
595 
check_vcenter_cluster_discovery(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)596 int	check_vcenter_cluster_discovery(AGENT_REQUEST *request, const char *username, const char *password,
597 		AGENT_RESULT *result)
598 {
599 	struct zbx_json		json_data;
600 	const char		*url;
601 	zbx_vmware_service_t	*service;
602 	int			i, ret = SYSINFO_RET_FAIL;
603 
604 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __func__);
605 
606 	if (1 != request->nparam)
607 	{
608 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid number of parameters."));
609 		goto out;
610 	}
611 
612 	url = get_rparam(request, 0);
613 
614 	zbx_vmware_lock();
615 
616 	if (NULL == (service = get_vmware_service(url, username, password, result, &ret)))
617 		goto unlock;
618 
619 	zbx_json_initarray(&json_data, ZBX_JSON_STAT_BUF_LEN);
620 
621 	for (i = 0; i < service->data->clusters.values_num; i++)
622 	{
623 		zbx_vmware_cluster_t	*cluster = (zbx_vmware_cluster_t *)service->data->clusters.values[i];
624 
625 		zbx_json_addobject(&json_data, NULL);
626 		zbx_json_addstring(&json_data, "{#CLUSTER.ID}", cluster->id, ZBX_JSON_TYPE_STRING);
627 		zbx_json_addstring(&json_data, "{#CLUSTER.NAME}", cluster->name, ZBX_JSON_TYPE_STRING);
628 		zbx_json_close(&json_data);
629 	}
630 
631 	zbx_json_close(&json_data);
632 
633 	SET_STR_RESULT(result, zbx_strdup(NULL, json_data.buffer));
634 
635 	zbx_json_free(&json_data);
636 
637 	ret = SYSINFO_RET_OK;
638 unlock:
639 	zbx_vmware_unlock();
640 out:
641 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __func__, zbx_sysinfo_ret_string(ret));
642 
643 	return ret;
644 }
645 
check_vcenter_cluster_status(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)646 int	check_vcenter_cluster_status(AGENT_REQUEST *request, const char *username, const char *password,
647 		AGENT_RESULT *result)
648 {
649 	const char		*url, *name;
650 	zbx_vmware_service_t	*service;
651 	zbx_vmware_cluster_t	*cluster;
652 	int			ret = SYSINFO_RET_FAIL;
653 
654 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __func__);
655 
656 	if (2 != request->nparam)
657 	{
658 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid number of parameters."));
659 		goto out;
660 	}
661 
662 	url = get_rparam(request, 0);
663 	name = get_rparam(request, 1);
664 
665 	if ('\0' == *name)
666 		goto out;
667 
668 	zbx_vmware_lock();
669 
670 	if (NULL == (service = get_vmware_service(url, username, password, result, &ret)))
671 		goto unlock;
672 
673 	if (NULL == (cluster = cluster_get_by_name(&service->data->clusters, name)))
674 	{
675 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Unknown cluster name."));
676 		goto unlock;
677 	}
678 
679 	if (NULL == cluster->status)
680 		goto unlock;
681 
682 	ret = SYSINFO_RET_OK;
683 
684 	if (0 == strcmp(cluster->status, "gray"))
685 		SET_UI64_RESULT(result, 0);
686 	else if (0 == strcmp(cluster->status, "green"))
687 		SET_UI64_RESULT(result, 1);
688 	else if (0 == strcmp(cluster->status, "yellow"))
689 		SET_UI64_RESULT(result, 2);
690 	else if (0 == strcmp(cluster->status, "red"))
691 		SET_UI64_RESULT(result, 3);
692 	else
693 		ret = SYSINFO_RET_FAIL;
694 
695 unlock:
696 	zbx_vmware_unlock();
697 out:
698 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __func__, zbx_sysinfo_ret_string(ret));
699 
700 	return ret;
701 }
702 
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)703 static void	vmware_get_events(const zbx_vector_ptr_t *events, zbx_uint64_t eventlog_last_key, const DC_ITEM *item,
704 		zbx_vector_ptr_t *add_results)
705 {
706 	int	i;
707 
708 	zabbix_log(LOG_LEVEL_DEBUG, "In %s() eventlog_last_key:" ZBX_FS_UI64, __func__, eventlog_last_key);
709 
710 	/* events were retrieved in reverse chronological order */
711 	for (i = events->values_num - 1; i >= 0; i--)
712 	{
713 		const zbx_vmware_event_t	*event = (zbx_vmware_event_t *)events->values[i];
714 		AGENT_RESULT			*add_result = NULL;
715 
716 		if (event->key <= eventlog_last_key)
717 			continue;
718 
719 		add_result = (AGENT_RESULT *)zbx_malloc(add_result, sizeof(AGENT_RESULT));
720 		init_result(add_result);
721 
722 		if (SUCCEED == set_result_type(add_result, item->value_type, event->message))
723 		{
724 			set_result_meta(add_result, event->key, 0);
725 
726 			if (ITEM_VALUE_TYPE_LOG == item->value_type)
727 			{
728 				add_result->log->logeventid = event->key;
729 				add_result->log->timestamp = event->timestamp;
730 			}
731 
732 			zbx_vector_ptr_append(add_results, add_result);
733 		}
734 		else
735 			zbx_free(add_result);
736 	}
737 
738 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s(): events:%d", __func__, add_results->values_num);
739 }
740 
check_vcenter_eventlog(AGENT_REQUEST * request,const DC_ITEM * item,AGENT_RESULT * result,zbx_vector_ptr_t * add_results)741 int	check_vcenter_eventlog(AGENT_REQUEST *request, const DC_ITEM *item, AGENT_RESULT *result,
742 		zbx_vector_ptr_t *add_results)
743 {
744 	const char		*url, *skip;
745 	unsigned char		skip_old;
746 	zbx_vmware_service_t	*service;
747 	int			ret = SYSINFO_RET_FAIL;
748 
749 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __func__);
750 
751 	if (2 < request->nparam || 0 == request->nparam)
752 	{
753 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid number of parameters."));
754 		goto out;
755 	}
756 
757 	url = get_rparam(request, 0);
758 
759 	if (NULL == (skip = get_rparam(request, 1)) || '\0' == *skip || 0 == strcmp(skip, "all"))
760 	{
761 		skip_old = 0;
762 	}
763 	else if (0 == strcmp(skip, "skip"))
764 	{
765 		skip_old = (0 == request->lastlogsize ? 1 : 0);
766 	}
767 	else
768 	{
769 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid second parameter."));
770 		goto out;
771 	}
772 
773 	zbx_vmware_lock();
774 
775 	if (NULL == (service = get_vmware_service(url, item->username, item->password, result, &ret)))
776 		goto unlock;
777 
778 	if (ZBX_VMWARE_EVENT_KEY_UNINITIALIZED == service->eventlog.last_key ||
779 			(0 != skip_old && 0 != service->eventlog.last_key ))
780 	{
781 		/* this may happen if recreate item vmware.eventlog for the same service URL */
782 		service->eventlog.last_key = request->lastlogsize;
783 		service->eventlog.skip_old = skip_old;
784 	}
785 	else if (0 != service->eventlog.oom)
786 	{
787 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Not enough shared memory to store VMware events."));
788 		goto unlock;
789 	}
790 	else if (request->lastlogsize < service->eventlog.last_key && 0 != request->lastlogsize)
791 	{
792 		/* this may happen if there are multiple vmware.eventlog items for the same service URL or item has  */
793 		/* been polled, but values got stuck in history cache and item's lastlogsize hasn't been updated yet */
794 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Too old events requested."));
795 		goto unlock;
796 	}
797 	else if (0 < service->data->events.values_num)
798 	{
799 		vmware_get_events(&service->data->events, request->lastlogsize, item, add_results);
800 		service->eventlog.last_key = ((const zbx_vmware_event_t *)service->data->events.values[0])->key;
801 	}
802 
803 	ret = SYSINFO_RET_OK;
804 unlock:
805 	zbx_vmware_unlock();
806 out:
807 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __func__, zbx_sysinfo_ret_string(ret));
808 
809 	return ret;
810 }
811 
check_vcenter_version(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)812 int	check_vcenter_version(AGENT_REQUEST *request, const char *username, const char *password,
813 		AGENT_RESULT *result)
814 {
815 	const char		*url;
816 	zbx_vmware_service_t	*service;
817 	int			ret = SYSINFO_RET_FAIL;
818 
819 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __func__);
820 
821 	if (1 != request->nparam)
822 	{
823 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid number of parameters."));
824 		goto out;
825 	}
826 
827 	url = get_rparam(request, 0);
828 
829 	zbx_vmware_lock();
830 
831 	if (NULL == (service = get_vmware_service(url, username, password, result, &ret)))
832 		goto unlock;
833 
834 	if (NULL == service->version)
835 		goto unlock;
836 
837 	SET_STR_RESULT(result, zbx_strdup(NULL, service->version));
838 
839 	ret = SYSINFO_RET_OK;
840 unlock:
841 	zbx_vmware_unlock();
842 out:
843 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __func__, zbx_sysinfo_ret_string(ret));
844 
845 	return ret;
846 }
847 
check_vcenter_fullname(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)848 int	check_vcenter_fullname(AGENT_REQUEST *request, const char *username, const char *password,
849 		AGENT_RESULT *result)
850 {
851 	char			*url;
852 	zbx_vmware_service_t	*service;
853 	int			ret = SYSINFO_RET_FAIL;
854 
855 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __func__);
856 
857 	if (1 != request->nparam)
858 	{
859 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid number of parameters."));
860 		goto out;
861 	}
862 
863 	url = get_rparam(request, 0);
864 
865 	zbx_vmware_lock();
866 
867 	if (NULL == (service = get_vmware_service(url, username, password, result, &ret)))
868 		goto unlock;
869 
870 	if (NULL == service->fullname)
871 		goto unlock;
872 
873 	SET_STR_RESULT(result, zbx_strdup(NULL, service->fullname));
874 
875 	ret = SYSINFO_RET_OK;
876 unlock:
877 	zbx_vmware_unlock();
878 out:
879 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __func__, zbx_sysinfo_ret_string(ret));
880 
881 	return ret;
882 }
883 
check_vcenter_hv_cluster_name(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)884 int	check_vcenter_hv_cluster_name(AGENT_REQUEST *request, const char *username, const char *password,
885 		AGENT_RESULT *result)
886 {
887 	const char		*url, *uuid;
888 	zbx_vmware_hv_t		*hv;
889 	zbx_vmware_service_t	*service;
890 	zbx_vmware_cluster_t	*cluster = NULL;
891 	int			ret = SYSINFO_RET_FAIL;
892 
893 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __func__);
894 
895 	if (2 != request->nparam)
896 	{
897 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid number of parameters."));
898 		goto out;
899 	}
900 
901 	url = get_rparam(request, 0);
902 	uuid = get_rparam(request, 1);
903 
904 	if ('\0' == *uuid)
905 	{
906 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid second parameter."));
907 		goto out;
908 	}
909 
910 	zbx_vmware_lock();
911 
912 	if (NULL == (service = get_vmware_service(url, username, password, result, &ret)))
913 		goto unlock;
914 
915 	if (NULL == (hv = hv_get(&service->data->hvs, uuid)))
916 	{
917 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Unknown hypervisor uuid."));
918 		goto unlock;
919 	}
920 
921 	if (NULL != hv->clusterid)
922 		cluster = cluster_get(&service->data->clusters, hv->clusterid);
923 
924 	SET_STR_RESULT(result, zbx_strdup(NULL, NULL != cluster ? cluster->name : ""));
925 
926 	ret = SYSINFO_RET_OK;
927 unlock:
928 	zbx_vmware_unlock();
929 out:
930 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __func__, zbx_sysinfo_ret_string(ret));
931 
932 	return ret;
933 }
934 
check_vcenter_hv_cpu_usage(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)935 int	check_vcenter_hv_cpu_usage(AGENT_REQUEST *request, const char *username, const char *password,
936 		AGENT_RESULT *result)
937 {
938 	int	ret;
939 
940 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __func__);
941 
942 	ret = get_vcenter_hvprop(request, username, password, ZBX_VMWARE_HVPROP_OVERALL_CPU_USAGE, result);
943 
944 	if (SYSINFO_RET_OK == ret && NULL != GET_UI64_RESULT(result))
945 		result->ui64 = result->ui64 * 1000000;
946 
947 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __func__, zbx_sysinfo_ret_string(ret));
948 
949 	return ret;
950 }
951 
check_vcenter_hv_cpu_usage_perf(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)952 int	check_vcenter_hv_cpu_usage_perf(AGENT_REQUEST *request, const char *username, const char *password,
953 		AGENT_RESULT *result)
954 {
955 	const char		*url, *uuid;
956 	zbx_vmware_service_t	*service;
957 	zbx_vmware_hv_t		*hv;
958 	int			ret = SYSINFO_RET_FAIL;
959 
960 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __func__);
961 
962 	if (2 != request->nparam)
963 	{
964 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid number of parameters."));
965 		goto out;
966 	}
967 
968 	url = get_rparam(request, 0);
969 	uuid = get_rparam(request, 1);
970 
971 	if ('\0' == *uuid)
972 	{
973 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid second parameter."));
974 		goto out;
975 	}
976 
977 	zbx_vmware_lock();
978 
979 	if (NULL == (service = get_vmware_service(url, username, password, result, &ret)))
980 		goto unlock;
981 
982 	if (NULL == (hv = hv_get(&service->data->hvs, uuid)))
983 	{
984 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Unknown hypervisor uuid."));
985 		goto unlock;
986 	}
987 
988 	ret = vmware_service_get_counter_value_by_path(service, "HostSystem", hv->id, "cpu/usage[average]", "", 0,
989 			result);
990 unlock:
991 	zbx_vmware_unlock();
992 out:
993 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __func__, zbx_sysinfo_ret_string(ret));
994 
995 	return ret;
996 }
997 
check_vcenter_hv_cpu_utilization(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)998 int	check_vcenter_hv_cpu_utilization(AGENT_REQUEST *request, const char *username, const char *password,
999 		AGENT_RESULT *result)
1000 {
1001 	const char		*url, *uuid;
1002 	zbx_vmware_service_t	*service;
1003 	zbx_vmware_hv_t		*hv;
1004 	int			ret = SYSINFO_RET_FAIL;
1005 
1006 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __func__);
1007 
1008 	if (2 != request->nparam)
1009 	{
1010 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid number of parameters."));
1011 		goto out;
1012 	}
1013 
1014 	url = get_rparam(request, 0);
1015 	uuid = get_rparam(request, 1);
1016 
1017 	zbx_vmware_lock();
1018 
1019 	if (NULL == (service = get_vmware_service(url, username, password, result, &ret)))
1020 		goto unlock;
1021 
1022 	if (NULL == (hv = hv_get(&service->data->hvs, uuid)))
1023 	{
1024 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Unknown hypervisor uuid."));
1025 		goto unlock;
1026 	}
1027 
1028 	ret = vmware_service_get_counter_value_by_path(service, "HostSystem", hv->id, "cpu/utilization[average]", "",
1029 			0, result);
1030 unlock:
1031 	zbx_vmware_unlock();
1032 out:
1033 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __func__, zbx_sysinfo_ret_string(ret));
1034 
1035 	return ret;
1036 }
1037 
check_vcenter_hv_power(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)1038 int	check_vcenter_hv_power(AGENT_REQUEST *request, const char *username, const char *password,
1039 		AGENT_RESULT *result)
1040 {
1041 	const char		*path, *url, *uuid, *max;
1042 	zbx_vmware_service_t	*service;
1043 	zbx_vmware_hv_t		*hv;
1044 	int 			ret = SYSINFO_RET_FAIL;
1045 
1046 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __func__);
1047 
1048 	if (2 > request->nparam || request->nparam > 3)
1049 	{
1050 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid number of parameters."));
1051 		goto out;
1052 	}
1053 
1054 	url = get_rparam(request, 0);
1055 	uuid = get_rparam(request, 1);
1056 	max = get_rparam(request, 2);
1057 
1058 	if ('\0' == *uuid)
1059 	{
1060 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid second parameter."));
1061 		goto out;
1062 	}
1063 
1064 	if (NULL != max && '\0' != *max)
1065 	{
1066 		if (0 != strcmp(max, "max"))
1067 		{
1068 			SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid third parameter."));
1069 			goto out;
1070 		}
1071 
1072 		path = "power/powerCap[average]";
1073 	}
1074 	else
1075 		path = "power/power[average]";
1076 
1077 	zbx_vmware_lock();
1078 
1079 	if (NULL == (service = get_vmware_service(url, username, password, result, &ret)))
1080 		goto unlock;
1081 
1082 	if (NULL == (hv = hv_get(&service->data->hvs, uuid)))
1083 	{
1084 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Unknown hypervisor uuid."));
1085 		goto unlock;
1086 	}
1087 
1088 	ret = vmware_service_get_counter_value_by_path(service, "HostSystem", hv->id, path, "", 1, result);
1089 unlock:
1090 	zbx_vmware_unlock();
1091 out:
1092 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __func__, zbx_sysinfo_ret_string(ret));
1093 
1094 	return ret;
1095 }
1096 
check_vcenter_hv_discovery(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)1097 int	check_vcenter_hv_discovery(AGENT_REQUEST *request, const char *username, const char *password,
1098 		AGENT_RESULT *result)
1099 {
1100 	struct zbx_json		json_data;
1101 	const char		*url, *name;
1102 	zbx_vmware_service_t	*service;
1103 	int			ret = SYSINFO_RET_FAIL;
1104 	zbx_vmware_hv_t		*hv;
1105 	zbx_hashset_iter_t	iter;
1106 
1107 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __func__);
1108 
1109 	if (1 != request->nparam)
1110 	{
1111 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid number of parameters."));
1112 		goto out;
1113 	}
1114 
1115 	url = get_rparam(request, 0);
1116 
1117 	zbx_vmware_lock();
1118 
1119 	if (NULL == (service = get_vmware_service(url, username, password, result, &ret)))
1120 		goto unlock;
1121 
1122 	zbx_json_initarray(&json_data, ZBX_JSON_STAT_BUF_LEN);
1123 
1124 	zbx_hashset_iter_reset(&service->data->hvs, &iter);
1125 	while (NULL != (hv = (zbx_vmware_hv_t *)zbx_hashset_iter_next(&iter)))
1126 	{
1127 		zbx_vmware_cluster_t	*cluster = NULL;
1128 
1129 		if (NULL == (name = hv->props[ZBX_VMWARE_HVPROP_NAME]))
1130 			continue;
1131 
1132 		if (NULL != hv->clusterid)
1133 			cluster = cluster_get(&service->data->clusters, hv->clusterid);
1134 
1135 		zbx_json_addobject(&json_data, NULL);
1136 		zbx_json_addstring(&json_data, "{#HV.UUID}", hv->uuid, ZBX_JSON_TYPE_STRING);
1137 		zbx_json_addstring(&json_data, "{#HV.ID}", hv->id, ZBX_JSON_TYPE_STRING);
1138 		zbx_json_addstring(&json_data, "{#HV.NAME}", name, ZBX_JSON_TYPE_STRING);
1139 		zbx_json_addstring(&json_data, "{#HV.IP}", ZBX_NULL2EMPTY_STR(hv->ip), ZBX_JSON_TYPE_STRING);
1140 		zbx_json_addstring(&json_data, "{#DATACENTER.NAME}", hv->datacenter_name, ZBX_JSON_TYPE_STRING);
1141 		zbx_json_addstring(&json_data, "{#CLUSTER.NAME}",
1142 				NULL != cluster ? cluster->name : "", ZBX_JSON_TYPE_STRING);
1143 		zbx_json_addstring(&json_data, "{#PARENT.NAME}", hv->parent_name, ZBX_JSON_TYPE_STRING);
1144 		zbx_json_addstring(&json_data, "{#PARENT.TYPE}", hv->parent_type, ZBX_JSON_TYPE_STRING);
1145 		zbx_json_addstring(&json_data, "{#HV.NETNAME}",
1146 				ZBX_NULL2EMPTY_STR(hv->props[ZBX_VMWARE_HVPROP_NET_NAME]), ZBX_JSON_TYPE_STRING);
1147 		zbx_json_close(&json_data);
1148 	}
1149 
1150 	zbx_json_close(&json_data);
1151 
1152 	SET_STR_RESULT(result, zbx_strdup(NULL, json_data.buffer));
1153 
1154 	zbx_json_free(&json_data);
1155 
1156 	ret = SYSINFO_RET_OK;
1157 unlock:
1158 	zbx_vmware_unlock();
1159 out:
1160 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __func__, zbx_sysinfo_ret_string(ret));
1161 
1162 	return ret;
1163 }
1164 
check_vcenter_hv_fullname(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)1165 int	check_vcenter_hv_fullname(AGENT_REQUEST *request, const char *username, const char *password,
1166 		AGENT_RESULT *result)
1167 {
1168 	int	ret;
1169 
1170 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __func__);
1171 
1172 	ret = get_vcenter_hvprop(request, username, password, ZBX_VMWARE_HVPROP_FULL_NAME, result);
1173 
1174 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __func__, zbx_sysinfo_ret_string(ret));
1175 
1176 	return ret;
1177 }
1178 
check_vcenter_hv_hw_cpu_num(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)1179 int	check_vcenter_hv_hw_cpu_num(AGENT_REQUEST *request, const char *username, const char *password,
1180 		AGENT_RESULT *result)
1181 {
1182 	int	ret;
1183 
1184 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __func__);
1185 
1186 	ret = get_vcenter_hvprop(request, username, password, ZBX_VMWARE_HVPROP_HW_NUM_CPU_CORES, result);
1187 
1188 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __func__, zbx_sysinfo_ret_string(ret));
1189 
1190 	return ret;
1191 }
1192 
check_vcenter_hv_hw_cpu_freq(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)1193 int	check_vcenter_hv_hw_cpu_freq(AGENT_REQUEST *request, const char *username, const char *password,
1194 		AGENT_RESULT *result)
1195 {
1196 	int	ret;
1197 
1198 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __func__);
1199 
1200 	ret = get_vcenter_hvprop(request, username, password, ZBX_VMWARE_HVPROP_HW_CPU_MHZ, result);
1201 
1202 	if (SYSINFO_RET_OK == ret && NULL != GET_UI64_RESULT(result))
1203 		result->ui64 = result->ui64 * 1000000;
1204 
1205 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __func__, zbx_sysinfo_ret_string(ret));
1206 
1207 	return ret;
1208 }
1209 
check_vcenter_hv_hw_cpu_model(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)1210 int	check_vcenter_hv_hw_cpu_model(AGENT_REQUEST *request, const char *username, const char *password,
1211 		AGENT_RESULT *result)
1212 {
1213 	int	ret;
1214 
1215 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __func__);
1216 
1217 	ret = get_vcenter_hvprop(request, username, password, ZBX_VMWARE_HVPROP_HW_CPU_MODEL, result);
1218 
1219 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __func__, zbx_sysinfo_ret_string(ret));
1220 
1221 	return ret;
1222 }
1223 
check_vcenter_hv_hw_cpu_threads(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)1224 int	check_vcenter_hv_hw_cpu_threads(AGENT_REQUEST *request, const char *username, const char *password,
1225 		AGENT_RESULT *result)
1226 {
1227 	int	ret;
1228 
1229 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __func__);
1230 
1231 	ret = get_vcenter_hvprop(request, username, password, ZBX_VMWARE_HVPROP_HW_NUM_CPU_THREADS, result);
1232 
1233 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __func__, zbx_sysinfo_ret_string(ret));
1234 
1235 	return ret;
1236 }
1237 
check_vcenter_hv_hw_memory(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)1238 int	check_vcenter_hv_hw_memory(AGENT_REQUEST *request, const char *username, const char *password,
1239 		AGENT_RESULT *result)
1240 {
1241 	int	ret;
1242 
1243 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __func__);
1244 
1245 	ret = get_vcenter_hvprop(request, username, password, ZBX_VMWARE_HVPROP_HW_MEMORY_SIZE, result);
1246 
1247 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __func__, zbx_sysinfo_ret_string(ret));
1248 
1249 	return ret;
1250 }
1251 
check_vcenter_hv_hw_model(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)1252 int	check_vcenter_hv_hw_model(AGENT_REQUEST *request, const char *username, const char *password,
1253 		AGENT_RESULT *result)
1254 {
1255 	int	ret;
1256 
1257 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __func__);
1258 
1259 	ret = get_vcenter_hvprop(request, username, password, ZBX_VMWARE_HVPROP_HW_MODEL, result);
1260 
1261 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __func__, zbx_sysinfo_ret_string(ret));
1262 
1263 	return ret;
1264 }
1265 
check_vcenter_hv_hw_uuid(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)1266 int	check_vcenter_hv_hw_uuid(AGENT_REQUEST *request, const char *username, const char *password,
1267 		AGENT_RESULT *result)
1268 {
1269 	int	ret;
1270 
1271 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __func__);
1272 
1273 	ret = get_vcenter_hvprop(request, username, password, ZBX_VMWARE_HVPROP_HW_UUID, result);
1274 
1275 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __func__, zbx_sysinfo_ret_string(ret));
1276 
1277 	return ret;
1278 }
1279 
check_vcenter_hv_hw_vendor(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)1280 int	check_vcenter_hv_hw_vendor(AGENT_REQUEST *request, const char *username, const char *password,
1281 		AGENT_RESULT *result)
1282 {
1283 	int	ret;
1284 
1285 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __func__);
1286 
1287 	ret = get_vcenter_hvprop(request, username, password, ZBX_VMWARE_HVPROP_HW_VENDOR, result);
1288 
1289 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __func__, zbx_sysinfo_ret_string(ret));
1290 
1291 	return ret;
1292 }
1293 
check_vcenter_hv_memory_size_ballooned(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)1294 int	check_vcenter_hv_memory_size_ballooned(AGENT_REQUEST *request, const char *username, const char *password,
1295 		AGENT_RESULT *result)
1296 {
1297 	int			i, ret = SYSINFO_RET_FAIL;
1298 	zbx_vmware_service_t	*service;
1299 	const char		*uuid, *url;
1300 	zbx_vmware_hv_t		*hv;
1301 	zbx_uint64_t		value = 0;
1302 
1303 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __func__);
1304 
1305 	if (2 != request->nparam)
1306 	{
1307 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid number of parameters."));
1308 		goto out;
1309 	}
1310 
1311 	url = get_rparam(request, 0);
1312 	uuid = get_rparam(request, 1);
1313 
1314 	if ('\0' == *uuid)
1315 	{
1316 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid second parameter."));
1317 		goto out;
1318 	}
1319 
1320 	zbx_vmware_lock();
1321 
1322 	if (NULL == (service = get_vmware_service(url, username, password, result, &ret)))
1323 		goto unlock;
1324 
1325 	if (NULL == (hv = hv_get(&service->data->hvs, uuid)))
1326 	{
1327 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Unknown hypervisor uuid."));
1328 		goto unlock;
1329 	}
1330 
1331 	for (i = 0; i < hv->vms.values_num; i++)
1332 	{
1333 		zbx_uint64_t	mem;
1334 		const char	*value_str;
1335 		zbx_vmware_vm_t	*vm = (zbx_vmware_vm_t *)hv->vms.values[i];
1336 
1337 		if (NULL == (value_str = vm->props[ZBX_VMWARE_VMPROP_MEMORY_SIZE_BALLOONED]))
1338 			continue;
1339 
1340 		if (SUCCEED != is_uint64(value_str, &mem))
1341 			continue;
1342 
1343 		value += mem;
1344 	}
1345 
1346 	value *= ZBX_MEBIBYTE;
1347 	SET_UI64_RESULT(result, value);
1348 
1349 	ret = SYSINFO_RET_OK;
1350 unlock:
1351 	zbx_vmware_unlock();
1352 out:
1353 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __func__, zbx_sysinfo_ret_string(ret));
1354 
1355 	return ret;
1356 }
1357 
check_vcenter_hv_memory_used(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)1358 int	check_vcenter_hv_memory_used(AGENT_REQUEST *request, const char *username, const char *password,
1359 		AGENT_RESULT *result)
1360 {
1361 	int	ret;
1362 
1363 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __func__);
1364 
1365 	ret = get_vcenter_hvprop(request, username, password, ZBX_VMWARE_HVPROP_MEMORY_USED, result);
1366 
1367 	if (SYSINFO_RET_OK == ret && NULL != GET_UI64_RESULT(result))
1368 		result->ui64 = result->ui64 * ZBX_MEBIBYTE;
1369 
1370 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __func__, zbx_sysinfo_ret_string(ret));
1371 
1372 	return ret;
1373 }
1374 
check_vcenter_hv_sensor_health_state(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)1375 int	check_vcenter_hv_sensor_health_state(AGENT_REQUEST *request, const char *username, const char *password,
1376 		AGENT_RESULT *result)
1377 {
1378 	int	ret;
1379 
1380 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __func__);
1381 
1382 	ret = get_vcenter_hvprop(request, username, password, ZBX_VMWARE_HVPROP_HEALTH_STATE, result);
1383 
1384 	if (SYSINFO_RET_OK == ret && NULL != GET_STR_RESULT(result))
1385 	{
1386 		if (0 == strcmp(result->str, "gray") || 0 == strcmp(result->str, "unknown"))
1387 			SET_UI64_RESULT(result, 0);
1388 		else if (0 == strcmp(result->str, "green"))
1389 			SET_UI64_RESULT(result, 1);
1390 		else if (0 == strcmp(result->str, "yellow"))
1391 			SET_UI64_RESULT(result, 2);
1392 		else if (0 == strcmp(result->str, "red"))
1393 			SET_UI64_RESULT(result, 3);
1394 		else
1395 			ret = SYSINFO_RET_FAIL;
1396 
1397 		UNSET_STR_RESULT(result);
1398 	}
1399 
1400 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __func__, zbx_sysinfo_ret_string(ret));
1401 
1402 	return ret;
1403 }
1404 
check_vcenter_hv_status(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)1405 int	check_vcenter_hv_status(AGENT_REQUEST *request, const char *username, const char *password,
1406 		AGENT_RESULT *result)
1407 {
1408 	int	ret;
1409 
1410 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __func__);
1411 
1412 	ret = get_vcenter_hvprop(request, username, password, ZBX_VMWARE_HVPROP_STATUS, result);
1413 
1414 	if (SYSINFO_RET_OK == ret && NULL != GET_STR_RESULT(result))
1415 	{
1416 		if (0 == strcmp(result->str, "gray") || 0 == strcmp(result->str, "unknown"))
1417 			SET_UI64_RESULT(result, 0);
1418 		else if (0 == strcmp(result->str, "green"))
1419 			SET_UI64_RESULT(result, 1);
1420 		else if (0 == strcmp(result->str, "yellow"))
1421 			SET_UI64_RESULT(result, 2);
1422 		else if (0 == strcmp(result->str, "red"))
1423 			SET_UI64_RESULT(result, 3);
1424 		else
1425 			ret = SYSINFO_RET_FAIL;
1426 
1427 		UNSET_STR_RESULT(result);
1428 	}
1429 
1430 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __func__, zbx_sysinfo_ret_string(ret));
1431 
1432 	return ret;
1433 }
1434 
check_vcenter_hv_maintenance(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)1435 int	check_vcenter_hv_maintenance(AGENT_REQUEST *request, const char *username, const char *password,
1436 		AGENT_RESULT *result)
1437 {
1438 	int	ret;
1439 
1440 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __func__);
1441 
1442 	ret = get_vcenter_hvprop(request, username, password, ZBX_VMWARE_HVPROP_MAINTENANCE, result);
1443 
1444 	if (SYSINFO_RET_OK == ret && NULL != GET_STR_RESULT(result))
1445 	{
1446 		if (0 == strcmp(result->str, "false"))
1447 			SET_UI64_RESULT(result, 0);
1448 		else
1449 			SET_UI64_RESULT(result, 1);
1450 
1451 		UNSET_STR_RESULT(result);
1452 	}
1453 
1454 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __func__, zbx_sysinfo_ret_string(ret));
1455 
1456 	return ret;
1457 }
1458 
check_vcenter_hv_uptime(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)1459 int	check_vcenter_hv_uptime(AGENT_REQUEST *request, const char *username, const char *password,
1460 		AGENT_RESULT *result)
1461 {
1462 	int	ret;
1463 
1464 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __func__);
1465 
1466 	ret = get_vcenter_hvprop(request, username, password, ZBX_VMWARE_HVPROP_UPTIME, result);
1467 
1468 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __func__, zbx_sysinfo_ret_string(ret));
1469 
1470 	return ret;
1471 }
1472 
check_vcenter_hv_version(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)1473 int	check_vcenter_hv_version(AGENT_REQUEST *request, const char *username, const char *password,
1474 		AGENT_RESULT *result)
1475 {
1476 	int	ret;
1477 
1478 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __func__);
1479 
1480 	ret = get_vcenter_hvprop(request, username, password, ZBX_VMWARE_HVPROP_VERSION, result);
1481 
1482 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __func__, zbx_sysinfo_ret_string(ret));
1483 
1484 	return ret;
1485 }
1486 
check_vcenter_hv_sensors_get(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)1487 int	check_vcenter_hv_sensors_get(AGENT_REQUEST *request, const char *username, const char *password,
1488 		AGENT_RESULT *result)
1489 {
1490 	int	ret;
1491 
1492 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __func__);
1493 
1494 	ret = get_vcenter_hvprop(request, username, password, ZBX_VMWARE_HVPROP_SENSOR, result);
1495 
1496 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __func__, zbx_sysinfo_ret_string(ret));
1497 
1498 	return ret;
1499 }
1500 
check_vcenter_hv_vm_num(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)1501 int	check_vcenter_hv_vm_num(AGENT_REQUEST *request, const char *username, const char *password,
1502 		AGENT_RESULT *result)
1503 {
1504 	int			ret = SYSINFO_RET_FAIL;
1505 	zbx_vmware_service_t	*service;
1506 	const char		*uuid, *url;
1507 	zbx_vmware_hv_t		*hv;
1508 
1509 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __func__);
1510 
1511 	if (2 != request->nparam)
1512 	{
1513 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid number of parameters."));
1514 		goto out;
1515 	}
1516 
1517 	url = get_rparam(request, 0);
1518 	uuid = get_rparam(request, 1);
1519 
1520 	if ('\0' == *uuid)
1521 	{
1522 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid second 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 	SET_UI64_RESULT(result, hv->vms.values_num);
1538 	ret = SYSINFO_RET_OK;
1539 unlock:
1540 	zbx_vmware_unlock();
1541 out:
1542 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __func__, zbx_sysinfo_ret_string(ret));
1543 
1544 	return ret;
1545 }
1546 
check_vcenter_hv_network_in(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)1547 int	check_vcenter_hv_network_in(AGENT_REQUEST *request, const char *username, const char *password,
1548 		AGENT_RESULT *result)
1549 {
1550 	const char		*url, *mode, *uuid;
1551 	zbx_vmware_service_t	*service;
1552 	zbx_vmware_hv_t		*hv;
1553 	int			ret = SYSINFO_RET_FAIL;
1554 
1555 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __func__);
1556 
1557 	if (2 > request->nparam || request->nparam > 3)
1558 	{
1559 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid number of parameters."));
1560 		goto out;
1561 	}
1562 
1563 	url = get_rparam(request, 0);
1564 	uuid = get_rparam(request, 1);
1565 	mode = get_rparam(request, 2);
1566 
1567 	if (NULL != mode && '\0' != *mode && 0 != strcmp(mode, "bps"))
1568 	{
1569 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid third parameter."));
1570 		goto out;
1571 	}
1572 
1573 	zbx_vmware_lock();
1574 
1575 	if (NULL == (service = get_vmware_service(url, username, password, result, &ret)))
1576 		goto unlock;
1577 
1578 	if (NULL == (hv = hv_get(&service->data->hvs, uuid)))
1579 	{
1580 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Unknown hypervisor uuid."));
1581 		goto unlock;
1582 	}
1583 
1584 	ret = vmware_service_get_counter_value_by_path(service, "HostSystem", hv->id, "net/received[average]", "",
1585 			ZBX_KIBIBYTE, result);
1586 unlock:
1587 	zbx_vmware_unlock();
1588 out:
1589 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __func__, zbx_sysinfo_ret_string(ret));
1590 
1591 	return ret;
1592 }
1593 
check_vcenter_hv_network_out(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)1594 int	check_vcenter_hv_network_out(AGENT_REQUEST *request, const char *username, const char *password,
1595 		AGENT_RESULT *result)
1596 {
1597 	const char		*url, *mode, *uuid;
1598 	zbx_vmware_service_t	*service;
1599 	zbx_vmware_hv_t		*hv;
1600 	int			ret = SYSINFO_RET_FAIL;
1601 
1602 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __func__);
1603 
1604 	if (2 > request->nparam || request->nparam > 3)
1605 	{
1606 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid number of parameters."));
1607 		goto out;
1608 	}
1609 
1610 	url = get_rparam(request, 0);
1611 	uuid = get_rparam(request, 1);
1612 	mode = get_rparam(request, 2);
1613 
1614 	if (NULL != mode && '\0' != *mode && 0 != strcmp(mode, "bps"))
1615 	{
1616 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid third parameter."));
1617 		goto out;
1618 	}
1619 
1620 	zbx_vmware_lock();
1621 
1622 	if (NULL == (service = get_vmware_service(url, username, password, result, &ret)))
1623 		goto unlock;
1624 
1625 	if (NULL == (hv = hv_get(&service->data->hvs, uuid)))
1626 	{
1627 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Unknown hypervisor uuid."));
1628 		goto unlock;
1629 	}
1630 
1631 	ret = vmware_service_get_counter_value_by_path(service, "HostSystem", hv->id, "net/transmitted[average]", "",
1632 			ZBX_KIBIBYTE, result);
1633 unlock:
1634 	zbx_vmware_unlock();
1635 out:
1636 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __func__, zbx_sysinfo_ret_string(ret));
1637 
1638 	return ret;
1639 }
1640 
check_vcenter_hv_datacenter_name(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)1641 int	check_vcenter_hv_datacenter_name(AGENT_REQUEST *request, const char *username, const char *password,
1642 		AGENT_RESULT *result)
1643 {
1644 	const char		*url, *uuid;
1645 	zbx_vmware_service_t	*service;
1646 	zbx_vmware_hv_t		*hv;
1647 	int			ret = SYSINFO_RET_FAIL;
1648 
1649 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __func__);
1650 
1651 	if (2 != request->nparam)
1652 	{
1653 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid number of parameters."));
1654 		goto out;
1655 	}
1656 
1657 	url = get_rparam(request, 0);
1658 	uuid = get_rparam(request, 1);
1659 
1660 	zbx_vmware_lock();
1661 
1662 	if (NULL == (service = get_vmware_service(url, username, password, result, &ret)))
1663 		goto unlock;
1664 
1665 	if (NULL == (hv = hv_get(&service->data->hvs, uuid)))
1666 	{
1667 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Unknown hypervisor uuid."));
1668 		goto unlock;
1669 	}
1670 
1671 	SET_STR_RESULT(result, zbx_strdup(NULL, hv->datacenter_name));
1672 
1673 	ret = SYSINFO_RET_OK;
1674 unlock:
1675 	zbx_vmware_unlock();
1676 out:
1677 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __func__, zbx_sysinfo_ret_string(ret));
1678 
1679 	return ret;
1680 }
1681 
check_vcenter_hv_datastore_discovery(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)1682 int	check_vcenter_hv_datastore_discovery(AGENT_REQUEST *request, const char *username, const char *password,
1683 		AGENT_RESULT *result)
1684 {
1685 	const char		*url, *uuid;
1686 	zbx_vmware_service_t	*service;
1687 	zbx_vmware_hv_t		*hv;
1688 	struct zbx_json		json_data;
1689 	int			i, ret = SYSINFO_RET_FAIL;
1690 
1691 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __func__);
1692 
1693 	if (2 != request->nparam)
1694 	{
1695 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid number of parameters."));
1696 		goto out;
1697 	}
1698 
1699 	url = get_rparam(request, 0);
1700 	uuid = get_rparam(request, 1);
1701 
1702 	zbx_vmware_lock();
1703 
1704 	if (NULL == (service = get_vmware_service(url, username, password, result, &ret)))
1705 		goto unlock;
1706 
1707 	if (NULL == (hv = hv_get(&service->data->hvs, uuid)))
1708 	{
1709 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Unknown hypervisor uuid."));
1710 		goto unlock;
1711 	}
1712 
1713 	zbx_json_initarray(&json_data, ZBX_JSON_STAT_BUF_LEN);
1714 
1715 	for (i = 0; i < hv->dsnames.values_num; i++)
1716 	{
1717 		zbx_vmware_dsname_t	*dsname = hv->dsnames.values[i];
1718 		int			j, total = 0;
1719 
1720 
1721 		for (j = 0; j < dsname->hvdisks.values_num; j++)
1722 			total += dsname->hvdisks.values[j].multipath_total;
1723 
1724 		zbx_json_addobject(&json_data, NULL);
1725 		zbx_json_addstring(&json_data, "{#DATASTORE}", dsname->name, ZBX_JSON_TYPE_STRING);
1726 		zbx_json_adduint64(&json_data, "{#MULTIPATH.COUNT}", (unsigned int)total);
1727 		zbx_json_adduint64(&json_data, "{#MULTIPATH.PARTITION.COUNT}",
1728 				(unsigned int)dsname->hvdisks.values_num);
1729 
1730 		zbx_json_close(&json_data);
1731 	}
1732 
1733 	zbx_json_close(&json_data);
1734 
1735 	SET_STR_RESULT(result, zbx_strdup(NULL, json_data.buffer));
1736 
1737 	zbx_json_free(&json_data);
1738 
1739 	ret = SYSINFO_RET_OK;
1740 unlock:
1741 	zbx_vmware_unlock();
1742 out:
1743 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __func__, zbx_sysinfo_ret_string(ret));
1744 
1745 	return ret;
1746 }
1747 
check_vcenter_hv_datastore_latency(AGENT_REQUEST * request,const char * username,const char * password,const char * perfcounter,zbx_uint64_t access_filter,AGENT_RESULT * result)1748 static int	check_vcenter_hv_datastore_latency(AGENT_REQUEST *request, const char *username, const char *password,
1749 		const char *perfcounter, zbx_uint64_t access_filter, AGENT_RESULT *result)
1750 {
1751 	const char		*url, *mode, *uuid, *name;
1752 	zbx_vmware_service_t	*service;
1753 	zbx_vmware_hv_t		*hv;
1754 	zbx_vmware_datastore_t	*datastore;
1755 	zbx_vmware_dsname_t	dsnames_cmp;
1756 	int			i, ret = SYSINFO_RET_FAIL;
1757 	zbx_str_uint64_pair_t	uuid_cmp = {.value = 0};
1758 
1759 	zabbix_log(LOG_LEVEL_DEBUG, "In %s() perfcounter:%s", __func__, perfcounter);
1760 
1761 	if (3 > request->nparam || request->nparam > 4)
1762 	{
1763 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid number of parameters."));
1764 		goto out;
1765 	}
1766 
1767 	url = get_rparam(request, 0);
1768 	uuid = get_rparam(request, 1);
1769 	name = get_rparam(request, 2);
1770 	mode = get_rparam(request, 3);
1771 
1772 	if (NULL != mode && '\0' != *mode && 0 != strcmp(mode, "latency"))
1773 	{
1774 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid fourth parameter."));
1775 		goto out;
1776 	}
1777 
1778 	zbx_vmware_lock();
1779 
1780 	if (NULL == (service = get_vmware_service(url, username, password, result, &ret)))
1781 		goto unlock;
1782 
1783 	if (NULL == (hv = hv_get(&service->data->hvs, uuid)))
1784 	{
1785 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Unknown hypervisor uuid."));
1786 		goto unlock;
1787 	}
1788 
1789 	datastore = ds_get(&service->data->datastores, name);
1790 
1791 	if (NULL == datastore)
1792 	{
1793 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Unknown datastore name."));
1794 		goto unlock;
1795 	}
1796 
1797 	dsnames_cmp.name = datastore->name;
1798 
1799 	if (FAIL == zbx_vector_vmware_dsname_bsearch(&hv->dsnames, &dsnames_cmp, vmware_dsname_compare))
1800 	{
1801 		SET_MSG_RESULT(result, zbx_dsprintf(NULL, "Datastore \"%s\" not found on this hypervisor.",
1802 				datastore->name));
1803 		goto unlock;
1804 	}
1805 
1806 	if (NULL == datastore->uuid)
1807 	{
1808 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Unknown datastore uuid."));
1809 		goto unlock;
1810 	}
1811 
1812 	uuid_cmp.name = hv->uuid;
1813 
1814 	if (FAIL == (i = zbx_vector_str_uint64_pair_bsearch(&datastore->hv_uuids_access, uuid_cmp,
1815 			zbx_str_uint64_pair_name_compare)))
1816 	{
1817 		SET_MSG_RESULT(result, zbx_dsprintf(NULL, "Unknown hypervisor \"%s\" for datastore \"%s\".",
1818 				hv->props[ZBX_VMWARE_HVPROP_NAME], datastore->name));
1819 		goto unlock;
1820 	}
1821 
1822 	if (access_filter != (datastore->hv_uuids_access.values[i].value & access_filter))
1823 	{
1824 		zbx_uint64_t	mi = datastore->hv_uuids_access.values[i].value;
1825 
1826 		SET_MSG_RESULT(result, zbx_dsprintf(NULL, "Datastore is not available for hypervisor: %s",
1827 				0 == (ZBX_VMWARE_DS_MOUNTED & mi) ? "unmounted" : (
1828 				0 == (ZBX_VMWARE_DS_ACCESSIBLE & mi) ? "inaccessible" : (
1829 				ZBX_VMWARE_DS_READ == (ZBX_VMWARE_DS_READWRITE & mi)? "readOnly" :
1830 				"unknown"))));
1831 		goto unlock;
1832 	}
1833 
1834 	ret = vmware_service_get_counter_value_by_path(service, "HostSystem", hv->id, perfcounter, datastore->uuid, 1,
1835 			result);
1836 unlock:
1837 	zbx_vmware_unlock();
1838 out:
1839 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __func__, zbx_sysinfo_ret_string(ret));
1840 
1841 	return ret;
1842 }
1843 
check_vcenter_hv_datastore_read(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)1844 int	check_vcenter_hv_datastore_read(AGENT_REQUEST *request, const char *username, const char *password,
1845 		AGENT_RESULT *result)
1846 {
1847 	return check_vcenter_hv_datastore_latency(request, username, password, "datastore/totalReadLatency[average]",
1848 			ZBX_VMWARE_DS_READ_FILTER, result);
1849 }
1850 
check_vcenter_hv_datastore_write(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)1851 int	check_vcenter_hv_datastore_write(AGENT_REQUEST *request, const char *username, const char *password,
1852 		AGENT_RESULT *result)
1853 {
1854 	return check_vcenter_hv_datastore_latency(request, username, password, "datastore/totalWriteLatency[average]",
1855 			ZBX_VMWARE_DS_WRITE_FILTER, result);
1856 }
1857 
check_vcenter_hv_datastore_size_vsphere(int mode,const zbx_vmware_datastore_t * datastore,AGENT_RESULT * result)1858 static int	check_vcenter_hv_datastore_size_vsphere(int mode, const zbx_vmware_datastore_t *datastore,
1859 		AGENT_RESULT *result)
1860 {
1861 	switch (mode)
1862 	{
1863 		case ZBX_VMWARE_DATASTORE_SIZE_TOTAL:
1864 			if (ZBX_MAX_UINT64 == datastore->capacity)
1865 			{
1866 				SET_MSG_RESULT(result, zbx_strdup(NULL, "Datastore \"capacity\" is not available."));
1867 				return SYSINFO_RET_FAIL;
1868 			}
1869 			SET_UI64_RESULT(result, datastore->capacity);
1870 			break;
1871 		case ZBX_VMWARE_DATASTORE_SIZE_FREE:
1872 			if (ZBX_MAX_UINT64 == datastore->free_space)
1873 			{
1874 				SET_MSG_RESULT(result, zbx_strdup(NULL, "Datastore \"free space\" is not available."));
1875 				return SYSINFO_RET_FAIL;
1876 			}
1877 			SET_UI64_RESULT(result, datastore->free_space);
1878 			break;
1879 		case ZBX_VMWARE_DATASTORE_SIZE_UNCOMMITTED:
1880 			if (ZBX_MAX_UINT64 == datastore->uncommitted)
1881 			{
1882 				SET_MSG_RESULT(result, zbx_strdup(NULL, "Datastore \"uncommitted\" is not available."));
1883 				return SYSINFO_RET_FAIL;
1884 			}
1885 			SET_UI64_RESULT(result, datastore->uncommitted);
1886 			break;
1887 		case ZBX_VMWARE_DATASTORE_SIZE_PFREE:
1888 			if (ZBX_MAX_UINT64 == datastore->capacity)
1889 			{
1890 				SET_MSG_RESULT(result, zbx_strdup(NULL, "Datastore \"capacity\" is not available."));
1891 				return SYSINFO_RET_FAIL;
1892 			}
1893 			if (ZBX_MAX_UINT64 == datastore->free_space)
1894 			{
1895 				SET_MSG_RESULT(result, zbx_strdup(NULL, "Datastore \"free space\" is not available."));
1896 				return SYSINFO_RET_FAIL;
1897 			}
1898 			if (0 == datastore->capacity)
1899 			{
1900 				SET_MSG_RESULT(result, zbx_strdup(NULL, "Datastore \"capacity\" is zero."));
1901 				return SYSINFO_RET_FAIL;
1902 			}
1903 			SET_DBL_RESULT(result, (double)datastore->free_space / datastore->capacity * 100);
1904 			break;
1905 	}
1906 
1907 	return SYSINFO_RET_OK;
1908 }
1909 
check_vcenter_ds_param(const char * param,int * mode)1910 static int	check_vcenter_ds_param(const char *param, int *mode)
1911 {
1912 
1913 	if (NULL == param || '\0' == *param || 0 == strcmp(param, "total"))
1914 	{
1915 		*mode = ZBX_VMWARE_DATASTORE_SIZE_TOTAL;
1916 	}
1917 	else if (0 == strcmp(param, "free"))
1918 	{
1919 		*mode = ZBX_VMWARE_DATASTORE_SIZE_FREE;
1920 	}
1921 	else if (0 == strcmp(param, "pfree"))
1922 	{
1923 		*mode = ZBX_VMWARE_DATASTORE_SIZE_PFREE;
1924 	}
1925 	else if (0 == strcmp(param, "uncommitted"))
1926 	{
1927 		*mode = ZBX_VMWARE_DATASTORE_SIZE_UNCOMMITTED;
1928 	}
1929 	else
1930 		return FAIL;
1931 
1932 	return SUCCEED;
1933 }
1934 
check_vcenter_ds_size(const char * url,const char * hv_uuid,const char * name,const int mode,const char * username,const char * password,AGENT_RESULT * result)1935 static int	check_vcenter_ds_size(const char *url, const char *hv_uuid, const char *name, const int mode,
1936 		const char *username, const char *password, AGENT_RESULT *result)
1937 {
1938 	zbx_vmware_service_t	*service;
1939 	int			ret = SYSINFO_RET_FAIL;
1940 	zbx_vmware_datastore_t	*datastore = NULL;
1941 	zbx_uint64_t		disk_used, disk_provisioned, disk_capacity;
1942 	unsigned int		flags;
1943 	zbx_str_uint64_pair_t	uuid_cmp = {.name = (char *)hv_uuid, .value = 0};
1944 
1945 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __func__);
1946 
1947 	zbx_vmware_lock();
1948 
1949 	if (NULL == (service = get_vmware_service(url, username, password, result, &ret)))
1950 		goto unlock;
1951 
1952 	datastore = ds_get(&service->data->datastores, name);
1953 
1954 	if (NULL == datastore)
1955 	{
1956 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Unknown datastore name."));
1957 		goto unlock;
1958 	}
1959 
1960 	if (NULL != hv_uuid &&
1961 			FAIL == zbx_vector_str_uint64_pair_bsearch(&datastore->hv_uuids_access, uuid_cmp,
1962 			zbx_str_uint64_pair_name_compare))
1963 	{
1964 		SET_MSG_RESULT(result, zbx_dsprintf(NULL, "Hypervisor '%s' not found on this datastore.", hv_uuid));
1965 		goto unlock;
1966 	}
1967 
1968 	if (ZBX_VMWARE_TYPE_VSPHERE == service->type)
1969 	{
1970 		ret = check_vcenter_hv_datastore_size_vsphere(mode, datastore, result);
1971 		goto unlock;
1972 	}
1973 
1974 	switch (mode)
1975 	{
1976 		case ZBX_VMWARE_DATASTORE_SIZE_TOTAL:
1977 			flags = ZBX_DATASTORE_COUNTER_CAPACITY;
1978 			break;
1979 		case ZBX_VMWARE_DATASTORE_SIZE_FREE:
1980 			flags = ZBX_DATASTORE_COUNTER_CAPACITY | ZBX_DATASTORE_COUNTER_USED;
1981 			break;
1982 		case ZBX_VMWARE_DATASTORE_SIZE_PFREE:
1983 			flags = ZBX_DATASTORE_COUNTER_CAPACITY | ZBX_DATASTORE_COUNTER_USED;
1984 			break;
1985 		case ZBX_VMWARE_DATASTORE_SIZE_UNCOMMITTED:
1986 			flags = ZBX_DATASTORE_COUNTER_PROVISIONED | ZBX_DATASTORE_COUNTER_USED;
1987 			break;
1988 	}
1989 
1990 	if (0 != (flags & ZBX_DATASTORE_COUNTER_PROVISIONED))
1991 	{
1992 		ret = vmware_service_get_counter_value_by_path(service, "Datastore", datastore->id,
1993 				"disk/provisioned[latest]", ZBX_DATASTORE_TOTAL, ZBX_KIBIBYTE, result);
1994 
1995 		if (SYSINFO_RET_OK != ret || NULL == GET_UI64_RESULT(result))
1996 			goto unlock;
1997 
1998 		disk_provisioned = *GET_UI64_RESULT(result);
1999 		UNSET_UI64_RESULT(result);
2000 	}
2001 
2002 	if (0 != (flags & ZBX_DATASTORE_COUNTER_USED))
2003 	{
2004 		ret = vmware_service_get_counter_value_by_path(service, "Datastore", datastore->id,
2005 				"disk/used[latest]", ZBX_DATASTORE_TOTAL, ZBX_KIBIBYTE, result);
2006 
2007 		if (SYSINFO_RET_OK != ret || NULL == GET_UI64_RESULT(result))
2008 			goto unlock;
2009 
2010 		disk_used = *GET_UI64_RESULT(result);
2011 		UNSET_UI64_RESULT(result);
2012 	}
2013 
2014 	if (0 != (flags & ZBX_DATASTORE_COUNTER_CAPACITY))
2015 	{
2016 		ret = vmware_service_get_counter_value_by_path(service, "Datastore", datastore->id,
2017 				"disk/capacity[latest]", ZBX_DATASTORE_TOTAL, ZBX_KIBIBYTE, result);
2018 
2019 		if (SYSINFO_RET_OK != ret || NULL == GET_UI64_RESULT(result))
2020 			goto unlock;
2021 
2022 		disk_capacity = *GET_UI64_RESULT(result);
2023 		UNSET_UI64_RESULT(result);
2024 	}
2025 
2026 	switch (mode)
2027 	{
2028 		case ZBX_VMWARE_DATASTORE_SIZE_TOTAL:
2029 			SET_UI64_RESULT(result, disk_capacity);
2030 			break;
2031 		case ZBX_VMWARE_DATASTORE_SIZE_FREE:
2032 			SET_UI64_RESULT(result, disk_capacity - disk_used);
2033 			break;
2034 		case ZBX_VMWARE_DATASTORE_SIZE_UNCOMMITTED:
2035 			SET_UI64_RESULT(result, disk_provisioned - disk_used);
2036 			break;
2037 		case ZBX_VMWARE_DATASTORE_SIZE_PFREE:
2038 			SET_DBL_RESULT(result, 0 != disk_capacity ?
2039 					(double) (disk_capacity - disk_used) / disk_capacity * 100 : 0);
2040 			break;
2041 	}
2042 
2043 	ret = SYSINFO_RET_OK;
2044 unlock:
2045 	zbx_vmware_unlock();
2046 
2047 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __func__, zbx_sysinfo_ret_string(ret));
2048 
2049 	return ret;
2050 }
2051 
check_vcenter_hv_datastore_size(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)2052 int	check_vcenter_hv_datastore_size(AGENT_REQUEST *request, const char *username, const char *password,
2053 		AGENT_RESULT *result)
2054 {
2055 	const char	*url, *uuid, *name, *param;
2056 	int		ret = SYSINFO_RET_FAIL, mode;
2057 
2058 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __func__);
2059 
2060 	if (3 > request->nparam || request->nparam > 4)
2061 	{
2062 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid number of parameters."));
2063 		goto out;
2064 	}
2065 
2066 	url = get_rparam(request, 0);
2067 	uuid = get_rparam(request, 1);
2068 	name = get_rparam(request, 2);
2069 	param = get_rparam(request, 3);
2070 
2071 	if (SUCCEED == check_vcenter_ds_param(param, &mode))
2072 		ret = check_vcenter_ds_size(url, uuid, name, mode, username, password, result);
2073 	else
2074 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid fourth parameter."));
2075 out:
2076 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __func__, zbx_sysinfo_ret_string(ret));
2077 
2078 	return ret;
2079 }
2080 
check_vcenter_cl_perfcounter(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)2081 int	check_vcenter_cl_perfcounter(AGENT_REQUEST *request, const char *username, const char *password,
2082 		AGENT_RESULT *result)
2083 {
2084 	char			*url, *path, *clusterid;
2085 	const char 		*instance;
2086 	zbx_vmware_service_t	*service;
2087 	zbx_vmware_cluster_t	*cluster;
2088 	zbx_uint64_t		counterid;
2089 	int			unit, ret = SYSINFO_RET_FAIL;
2090 
2091 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __func__);
2092 
2093 	if (3 > request->nparam || request->nparam > 4)
2094 	{
2095 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid number of parameters."));
2096 		goto out;
2097 	}
2098 
2099 	url = get_rparam(request, 0);
2100 	clusterid = get_rparam(request, 1);
2101 	path = get_rparam(request, 2);
2102 	instance = get_rparam(request, 3);
2103 
2104 	if (NULL == instance)
2105 		instance = "";
2106 
2107 	zbx_vmware_lock();
2108 
2109 	if (NULL == (service = get_vmware_service(url, username, password, result, &ret)))
2110 		goto unlock;
2111 
2112 	if (FAIL == zbx_vmware_service_get_counterid(service, path, &counterid, &unit))
2113 	{
2114 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Performance counter is not available."));
2115 		goto unlock;
2116 	}
2117 
2118 	if (NULL == (cluster = cluster_get(&service->data->clusters, clusterid)))
2119 	{
2120 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid cluster id."));
2121 		goto unlock;
2122 	}
2123 
2124 	/* FAIL is returned if counter already exists */
2125 	if (SUCCEED == zbx_vmware_service_add_perf_counter(service, "ClusterComputeResource", cluster->id,
2126 			counterid, "*"))
2127 	{
2128 		ret = SYSINFO_RET_OK;
2129 		goto unlock;
2130 	}
2131 
2132 	/* the performance counter is already being monitored, try to get the results from statistics */
2133 	ret = vmware_service_get_counter_value_by_id(service, "ClusterComputeResource", cluster->id, counterid,
2134 			instance, 1, unit, result);
2135 unlock:
2136 	zbx_vmware_unlock();
2137 out:
2138 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __func__, zbx_sysinfo_ret_string(ret));
2139 
2140 	return ret;
2141 }
2142 
check_vcenter_hv_perfcounter(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)2143 int	check_vcenter_hv_perfcounter(AGENT_REQUEST *request, const char *username, const char *password,
2144 		AGENT_RESULT *result)
2145 {
2146 	const char		*instance, *url, *uuid, *path;
2147 	zbx_vmware_service_t	*service;
2148 	zbx_vmware_hv_t		*hv;
2149 	zbx_uint64_t		counterid;
2150 	int			unit, ret = SYSINFO_RET_FAIL;
2151 
2152 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __func__);
2153 
2154 	if (3 > request->nparam || request->nparam > 4)
2155 	{
2156 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid number of parameters."));
2157 		goto out;
2158 	}
2159 
2160 	url = get_rparam(request, 0);
2161 	uuid = get_rparam(request, 1);
2162 	path = get_rparam(request, 2);
2163 	instance = get_rparam(request, 3);
2164 
2165 	if (NULL == instance)
2166 		instance = "";
2167 
2168 	zbx_vmware_lock();
2169 
2170 	if (NULL == (service = get_vmware_service(url, username, password, result, &ret)))
2171 		goto unlock;
2172 
2173 	if (NULL == (hv = hv_get(&service->data->hvs, uuid)))
2174 	{
2175 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Unknown hypervisor uuid."));
2176 		goto unlock;
2177 	}
2178 
2179 	if (FAIL == zbx_vmware_service_get_counterid(service, path, &counterid, &unit))
2180 	{
2181 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Performance counter is not available."));
2182 		goto unlock;
2183 	}
2184 
2185 	/* FAIL is returned if counter already exists */
2186 	if (SUCCEED == zbx_vmware_service_add_perf_counter(service, "HostSystem", hv->id, counterid, "*"))
2187 	{
2188 		ret = SYSINFO_RET_OK;
2189 		goto unlock;
2190 	}
2191 
2192 	/* the performance counter is already being monitored, try to get the results from statistics */
2193 	ret = vmware_service_get_counter_value_by_id(service, "HostSystem", hv->id, counterid, instance, 1, unit,
2194 			result);
2195 unlock:
2196 	zbx_vmware_unlock();
2197 out:
2198 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __func__, zbx_sysinfo_ret_string(ret));
2199 
2200 	return ret;
2201 }
2202 
check_vcenter_hv_datastore_list(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)2203 int	check_vcenter_hv_datastore_list(AGENT_REQUEST *request, const char *username, const char *password,
2204 		AGENT_RESULT *result)
2205 {
2206 	const char		*url, *hv_uuid;
2207 	char			*ds_list = NULL;
2208 	zbx_vmware_service_t	*service;
2209 	zbx_vmware_hv_t		*hv;
2210 	int			i, ret = SYSINFO_RET_FAIL;
2211 
2212 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __func__);
2213 
2214 	if (2 != request->nparam )
2215 	{
2216 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid number of parameters."));
2217 		goto out;
2218 	}
2219 
2220 	url = get_rparam(request, 0);
2221 	hv_uuid = get_rparam(request, 1);
2222 	zbx_vmware_lock();
2223 
2224 	if (NULL == (service = get_vmware_service(url, username, password, result, &ret)))
2225 		goto unlock;
2226 
2227 	if (NULL == (hv = hv_get(&service->data->hvs, hv_uuid)))
2228 	{
2229 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Unknown hypervisor uuid."));
2230 		goto unlock;
2231 	}
2232 
2233 	for (i = 0; i < hv->dsnames.values_num; i++)
2234 	{
2235 		zbx_vmware_dsname_t	*dsname = hv->dsnames.values[i];
2236 
2237 		ds_list = zbx_strdcatf(ds_list, "%s\n", dsname->name);
2238 	}
2239 
2240 	if (NULL != ds_list)
2241 		ds_list[strlen(ds_list)-1] = '\0';
2242 	else
2243 		ds_list = zbx_strdup(NULL, "");
2244 
2245 	SET_TEXT_RESULT(result, ds_list);
2246 
2247 	ret = SYSINFO_RET_OK;
2248 unlock:
2249 	zbx_vmware_unlock();
2250 out:
2251 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __func__, zbx_sysinfo_ret_string(ret));
2252 
2253 	return ret;
2254 }
2255 
check_vcenter_hv_datastore_multipath(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)2256 int	check_vcenter_hv_datastore_multipath(AGENT_REQUEST *request, const char *username, const char *password,
2257 		AGENT_RESULT *result)
2258 {
2259 	const char		*url, *hv_uuid, *ds_name, *partition;
2260 	zbx_vmware_service_t	*service;
2261 	zbx_vmware_hv_t		*hv;
2262 	zbx_vmware_dsname_t	*dsname;
2263 	int			ret = SYSINFO_RET_FAIL, i, j, multipath_count = 0;
2264 	zbx_uint64_t		partitionid = 0;
2265 
2266 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __func__);
2267 
2268 	if (2 > request->nparam || request->nparam > 4)
2269 	{
2270 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid number of parameters."));
2271 		goto out;
2272 	}
2273 
2274 	url = get_rparam(request, 0);
2275 	hv_uuid = get_rparam(request, 1);
2276 	ds_name = get_rparam(request, 2);
2277 	partition = get_rparam(request, 3);
2278 
2279 	if ('\0' == *hv_uuid)
2280 	{
2281 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid second parameter."));
2282 		goto out;
2283 	}
2284 
2285 	if (NULL != partition && '\0' != *partition)
2286 	{
2287 		if (NULL == ds_name || '\0' == *ds_name)
2288 		{
2289 			SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid fourth parameter."));
2290 			goto out;
2291 		}
2292 
2293 		partitionid = (unsigned int) atoi(partition);
2294 	}
2295 
2296 	zbx_vmware_lock();
2297 
2298 	if (NULL == (service = get_vmware_service(url, username, password, result, &ret)))
2299 		goto unlock;
2300 
2301 	if (NULL == (hv = hv_get(&service->data->hvs, hv_uuid)))
2302 	{
2303 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Unknown hypervisor uuid."));
2304 		goto unlock;
2305 	}
2306 
2307 	if (NULL != ds_name && '\0' != *ds_name)
2308 	{
2309 		zbx_vmware_datastore_t	*datastore;
2310 		zbx_vmware_dsname_t	dsnames_cmp;
2311 		zbx_vmware_hvdisk_t	hvdisk_cmp;
2312 
2313 		datastore = ds_get(&service->data->datastores, ds_name);
2314 
2315 		if (NULL == datastore)
2316 		{
2317 			SET_MSG_RESULT(result, zbx_strdup(NULL, "Unknown datastore name."));
2318 			goto unlock;
2319 		}
2320 
2321 		dsnames_cmp.name = datastore->name;
2322 
2323 		if (FAIL == (i = zbx_vector_vmware_dsname_bsearch(&hv->dsnames, &dsnames_cmp, vmware_dsname_compare)))
2324 		{
2325 			SET_MSG_RESULT(result, zbx_dsprintf(NULL, "Datastore \"%s\" not found on this hypervisor.",
2326 					datastore->name));
2327 			goto unlock;
2328 		}
2329 
2330 		if (NULL == datastore->uuid)
2331 		{
2332 			SET_MSG_RESULT(result, zbx_strdup(NULL, "Unknown datastore uuid."));
2333 			goto unlock;
2334 		}
2335 
2336 		dsname = hv->dsnames.values[i];
2337 
2338 		if (NULL != partition)
2339 		{
2340 			hvdisk_cmp.partitionid = partitionid;
2341 
2342 			if (FAIL == (i = zbx_vector_vmware_hvdisk_bsearch(&dsname->hvdisks, hvdisk_cmp,
2343 					ZBX_DEFAULT_UINT64_COMPARE_FUNC)))
2344 			{
2345 				SET_MSG_RESULT(result, zbx_dsprintf(NULL, "Unknown partition id:" ZBX_FS_UI64,
2346 						partitionid));
2347 				goto unlock;
2348 			}
2349 
2350 			multipath_count = dsname->hvdisks.values[i].multipath_active;
2351 		}
2352 		else
2353 		{
2354 			for (j = 0; j < dsname->hvdisks.values_num; j++)
2355 				multipath_count += dsname->hvdisks.values[j].multipath_active;
2356 		}
2357 	}
2358 	else
2359 	{
2360 		for (i = 0; i < hv->dsnames.values_num; i++)
2361 		{
2362 			dsname = hv->dsnames.values[i];
2363 
2364 			for (j = 0; j < dsname->hvdisks.values_num; j++)
2365 				multipath_count += dsname->hvdisks.values[j].multipath_active;
2366 		}
2367 	}
2368 
2369 	SET_UI64_RESULT(result, (unsigned int)multipath_count);
2370 	ret = SYSINFO_RET_OK;
2371 unlock:
2372 	zbx_vmware_unlock();
2373 out:
2374 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __func__, zbx_sysinfo_ret_string(ret));
2375 
2376 	return ret;
2377 }
2378 
check_vcenter_datastore_hv_list(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)2379 int	check_vcenter_datastore_hv_list(AGENT_REQUEST *request, const char *username, const char *password,
2380 		AGENT_RESULT *result)
2381 {
2382 	const char		*url, *ds_name, *hv_name;
2383 	char			*hv_list = NULL;
2384 	zbx_vmware_service_t	*service;
2385 	int			i, ret = SYSINFO_RET_FAIL;
2386 	zbx_vmware_datastore_t	*datastore = NULL;
2387 	zbx_vmware_hv_t		*hv;
2388 
2389 
2390 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __func__);
2391 
2392 	if (2 != request->nparam )
2393 	{
2394 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid number of parameters."));
2395 		goto out;
2396 	}
2397 
2398 	url = get_rparam(request, 0);
2399 	ds_name = get_rparam(request, 1);
2400 	zbx_vmware_lock();
2401 
2402 	if (NULL == (service = get_vmware_service(url, username, password, result, &ret)))
2403 		goto unlock;
2404 
2405 	for (i = 0; i < service->data->datastores.values_num; i++)
2406 	{
2407 		if (0 != strcmp(ds_name, service->data->datastores.values[i]->name))
2408 			continue;
2409 
2410 		datastore = service->data->datastores.values[i];
2411 		break;
2412 	}
2413 
2414 	if (NULL == datastore)
2415 	{
2416 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Unknown datastore name."));
2417 		goto unlock;
2418 	}
2419 
2420 	for (i=0; i < datastore->hv_uuids_access.values_num; i++)
2421 	{
2422 		if (NULL == (hv = hv_get(&service->data->hvs, datastore->hv_uuids_access.values[i].name)))
2423 		{
2424 			SET_MSG_RESULT(result, zbx_strdup(NULL, "Unknown hypervisor uuid."));
2425 			zbx_free(hv_list);
2426 			goto unlock;
2427 		}
2428 
2429 		if (NULL == (hv_name = hv->props[ZBX_VMWARE_HVPROP_NAME]))
2430 			hv_name = datastore->hv_uuids_access.values[i].name;
2431 
2432 		hv_list = zbx_strdcatf(hv_list, "%s\n", hv_name);
2433 	}
2434 
2435 	if (NULL != hv_list)
2436 		hv_list[strlen(hv_list)-1] = '\0';
2437 	else
2438 		hv_list = zbx_strdup(NULL, "");
2439 
2440 	SET_TEXT_RESULT(result, hv_list);
2441 
2442 	ret = SYSINFO_RET_OK;
2443 unlock:
2444 	zbx_vmware_unlock();
2445 out:
2446 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __func__, zbx_sysinfo_ret_string(ret));
2447 
2448 	return ret;
2449 }
2450 
check_vcenter_datastore_size(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)2451 int	check_vcenter_datastore_size(AGENT_REQUEST *request, const char *username, const char *password,
2452 		AGENT_RESULT *result)
2453 {
2454 	const char	*url, *name, *param;
2455 	int		ret = SYSINFO_RET_FAIL, mode;
2456 
2457 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __func__);
2458 
2459 	if (2 > request->nparam || request->nparam > 3)
2460 	{
2461 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid number of parameters."));
2462 		goto out;
2463 	}
2464 
2465 	url = get_rparam(request, 0);
2466 	name = get_rparam(request, 1);
2467 	param = get_rparam(request, 2);
2468 
2469 	if (SUCCEED == check_vcenter_ds_param(param, &mode))
2470 		ret = check_vcenter_ds_size(url, NULL, name, mode, username, password, result);
2471 	else
2472 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid third parameter."));
2473 out:
2474 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __func__, zbx_sysinfo_ret_string(ret));
2475 
2476 	return ret;
2477 }
2478 
check_vcenter_datastore_discovery(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)2479 int	check_vcenter_datastore_discovery(AGENT_REQUEST *request, const char *username, const char *password,
2480 		AGENT_RESULT *result)
2481 {
2482 	const char		*url;
2483 	zbx_vmware_service_t	*service;
2484 	struct zbx_json		json_data;
2485 	int			i, j, ret = SYSINFO_RET_FAIL;
2486 
2487 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __func__);
2488 
2489 	if (1 != request->nparam)
2490 	{
2491 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid number of parameters."));
2492 		goto out;
2493 	}
2494 
2495 	url = get_rparam(request, 0);
2496 
2497 	zbx_vmware_lock();
2498 
2499 	if (NULL == (service = get_vmware_service(url, username, password, result, &ret)))
2500 		goto unlock;
2501 
2502 	zbx_json_initarray(&json_data, ZBX_JSON_STAT_BUF_LEN);
2503 
2504 	for (i = 0; i < service->data->datastores.values_num; i++)
2505 	{
2506 		zbx_vmware_datastore_t	*datastore = service->data->datastores.values[i];
2507 		zbx_json_addobject(&json_data, NULL);
2508 		zbx_json_addstring(&json_data, "{#DATASTORE}", datastore->name, ZBX_JSON_TYPE_STRING);
2509 		zbx_json_addobject(&json_data, "{#DATASTORE.EXTENT}");
2510 
2511 		for (j = 0; j < datastore->diskextents.values_num; j++)
2512 		{
2513 			char			buffer[MAX_ID_LEN];
2514 			zbx_vmware_diskextent_t	*extent = datastore->diskextents.values[j];
2515 
2516 			zbx_snprintf(buffer, sizeof(buffer), ZBX_FS_UI64, extent->partitionid);
2517 			zbx_json_addstring(&json_data, extent->diskname, buffer, ZBX_JSON_TYPE_INT);
2518 		}
2519 
2520 		zbx_json_close(&json_data);
2521 		zbx_json_close(&json_data);
2522 	}
2523 
2524 	zbx_json_close(&json_data);
2525 
2526 	SET_STR_RESULT(result, zbx_strdup(NULL, json_data.buffer));
2527 
2528 	zbx_json_free(&json_data);
2529 
2530 	ret = SYSINFO_RET_OK;
2531 unlock:
2532 	zbx_vmware_unlock();
2533 out:
2534 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __func__, zbx_sysinfo_ret_string(ret));
2535 
2536 	return ret;
2537 }
2538 
check_vcenter_datastore_latency(AGENT_REQUEST * request,const char * username,const char * password,const char * perfcounter,zbx_uint64_t access_filter,AGENT_RESULT * result)2539 static int	check_vcenter_datastore_latency(AGENT_REQUEST *request, const char *username, const char *password,
2540 		const char *perfcounter, zbx_uint64_t access_filter, AGENT_RESULT *result)
2541 {
2542 	const char		*url, *mode, *name;
2543 	zbx_vmware_service_t	*service;
2544 	zbx_vmware_hv_t		*hv;
2545 	zbx_vmware_datastore_t	*datastore;
2546 	int			i, ret = SYSINFO_RET_FAIL, count = 0, ds_count = 0, unit;
2547 	zbx_uint64_t		latency = 0, counterid;
2548 	unsigned char		is_maxlatency = 0;
2549 
2550 	zabbix_log(LOG_LEVEL_DEBUG, "In %s() perfcounter:%s", __func__, perfcounter);
2551 
2552 	if (2 > request->nparam || request->nparam > 3)
2553 	{
2554 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid number of parameters."));
2555 		goto out;
2556 	}
2557 
2558 	url = get_rparam(request, 0);
2559 	name = get_rparam(request, 1);
2560 	mode = get_rparam(request, 2);
2561 
2562 	if (NULL != mode && '\0' != *mode && 0 != strcmp(mode, "latency") && 0 != strcmp(mode, "maxlatency"))
2563 	{
2564 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid third parameter."));
2565 		goto out;
2566 	}
2567 
2568 	if (NULL != mode && 0 == strcmp(mode, "maxlatency"))
2569 		is_maxlatency = 1;
2570 
2571 	zbx_vmware_lock();
2572 
2573 	if (NULL == (service = get_vmware_service(url, username, password, result, &ret)))
2574 		goto unlock;
2575 
2576 	datastore = ds_get(&service->data->datastores, name);
2577 
2578 	if (NULL == datastore)
2579 	{
2580 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Unknown datastore name."));
2581 		goto unlock;
2582 	}
2583 
2584 	if (NULL == datastore->uuid)
2585 	{
2586 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Unknown datastore uuid."));
2587 		goto unlock;
2588 	}
2589 
2590 	if (FAIL == zbx_vmware_service_get_counterid(service, perfcounter, &counterid, &unit))
2591 	{
2592 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Performance counter is not available."));
2593 		goto unlock;
2594 	}
2595 
2596 	for (i = 0; i < datastore->hv_uuids_access.values_num; i++)
2597 	{
2598 		if (access_filter != (datastore->hv_uuids_access.values[i].value & access_filter))
2599 		{
2600 			zbx_uint64_t	mi = datastore->hv_uuids_access.values[i].value;
2601 
2602 			zabbix_log(LOG_LEVEL_DEBUG, "Datastore %s is not available for hypervisor %s: %s",
2603 					datastore->name, datastore->hv_uuids_access.values[i].name,
2604 					0 == (ZBX_VMWARE_DS_MOUNTED & mi) ? "unmounted" : (
2605 					0 == (ZBX_VMWARE_DS_ACCESSIBLE & mi) ? "inaccessible" : (
2606 					ZBX_VMWARE_DS_READ == (ZBX_VMWARE_DS_READWRITE & mi)? "readOnly" :
2607 					"unknown")));
2608 			continue;
2609 		}
2610 
2611 		if (NULL == (hv = hv_get(&service->data->hvs, datastore->hv_uuids_access.values[i].name)))
2612 		{
2613 			SET_MSG_RESULT(result, zbx_strdup(NULL, "Unknown hypervisor uuid."));
2614 			goto unlock;
2615 		}
2616 
2617 		if (SYSINFO_RET_OK != (ret = vmware_service_get_counter_value_by_id(service, "HostSystem", hv->id,
2618 				counterid, datastore->uuid, 1, unit, result)))
2619 		{
2620 			char	*err, *msg = *GET_MSG_RESULT(result);
2621 
2622 			*msg = (char)tolower(*msg);
2623 			err = zbx_dsprintf(NULL, "Counter %s for datastore %s is not available for hypervisor %s: %s",
2624 					perfcounter, datastore->name,
2625 					ZBX_NULL2EMPTY_STR(hv->props[ZBX_VMWARE_HVPROP_NAME]), msg);
2626 			UNSET_MSG_RESULT(result);
2627 			SET_MSG_RESULT(result, err);
2628 			goto unlock;
2629 		}
2630 
2631 		ds_count++;
2632 
2633 		if (0 == ISSET_VALUE(result))
2634 			continue;
2635 
2636 		if (0 == is_maxlatency)
2637 		{
2638 			latency += *GET_UI64_RESULT(result);
2639 			count++;
2640 		}
2641 		else if (latency < *GET_UI64_RESULT(result))
2642 			latency = *GET_UI64_RESULT(result);
2643 
2644 		UNSET_UI64_RESULT(result);
2645 	}
2646 
2647 	if (0 == ds_count)
2648 	{
2649 		SET_MSG_RESULT(result, zbx_strdup(NULL, "No datastores available."));
2650 		goto unlock;
2651 	}
2652 
2653 	if (0 == is_maxlatency && 0 != count)
2654 		latency = latency / count;
2655 
2656 	SET_UI64_RESULT(result, latency);
2657 unlock:
2658 	zbx_vmware_unlock();
2659 out:
2660 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __func__, zbx_sysinfo_ret_string(ret));
2661 
2662 	return ret;
2663 }
2664 
check_vcenter_datastore_read(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)2665 int	check_vcenter_datastore_read(AGENT_REQUEST *request, const char *username, const char *password,
2666 		AGENT_RESULT *result)
2667 {
2668 	return check_vcenter_datastore_latency(request, username, password, "datastore/totalReadLatency[average]",
2669 			ZBX_VMWARE_DS_READ_FILTER, result);
2670 }
2671 
check_vcenter_datastore_write(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)2672 int	check_vcenter_datastore_write(AGENT_REQUEST *request, const char *username, const char *password,
2673 		AGENT_RESULT *result)
2674 {
2675 	return check_vcenter_datastore_latency(request, username, password, "datastore/totalWriteLatency[average]",
2676 			ZBX_VMWARE_DS_WRITE_FILTER, result);
2677 }
2678 
check_vcenter_vm_cpu_num(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)2679 int	check_vcenter_vm_cpu_num(AGENT_REQUEST *request, const char *username, const char *password,
2680 		AGENT_RESULT *result)
2681 {
2682 	int	ret;
2683 
2684 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __func__);
2685 
2686 	ret = get_vcenter_vmprop(request, username, password, ZBX_VMWARE_VMPROP_CPU_NUM, result);
2687 
2688 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __func__, zbx_sysinfo_ret_string(ret));
2689 
2690 	return ret;
2691 }
2692 
check_vcenter_vm_cluster_name(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)2693 int	check_vcenter_vm_cluster_name(AGENT_REQUEST *request, const char *username, const char *password,
2694 		AGENT_RESULT *result)
2695 {
2696 	const char		*url, *uuid;
2697 	zbx_vmware_service_t	*service;
2698 	zbx_vmware_cluster_t	*cluster = NULL;
2699 	int			ret = SYSINFO_RET_FAIL;
2700 	zbx_vmware_hv_t		*hv;
2701 
2702 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __func__);
2703 
2704 	if (2 != request->nparam)
2705 	{
2706 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid number of parameters."));
2707 		goto out;
2708 	}
2709 
2710 	url = get_rparam(request, 0);
2711 	uuid = get_rparam(request, 1);
2712 
2713 	if ('\0' == *uuid)
2714 	{
2715 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid second parameter."));
2716 		goto out;
2717 	}
2718 
2719 	zbx_vmware_lock();
2720 
2721 	if (NULL == (service = get_vmware_service(url, username, password, result, &ret)))
2722 		goto unlock;
2723 
2724 	if (NULL == (hv = service_hv_get_by_vm_uuid(service, uuid)))
2725 	{
2726 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Unknown virtual machine uuid."));
2727 		goto unlock;
2728 	}
2729 	if (NULL != hv->clusterid)
2730 		cluster = cluster_get(&service->data->clusters, hv->clusterid);
2731 
2732 	SET_STR_RESULT(result, zbx_strdup(NULL, NULL != cluster ? cluster->name : ""));
2733 
2734 	ret = SYSINFO_RET_OK;
2735 unlock:
2736 	zbx_vmware_unlock();
2737 out:
2738 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __func__, zbx_sysinfo_ret_string(ret));
2739 
2740 	return ret;
2741 }
2742 
check_vcenter_vm_cpu_ready(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)2743 int	check_vcenter_vm_cpu_ready(AGENT_REQUEST *request, const char *username, const char *password,
2744 		AGENT_RESULT *result)
2745 {
2746 	zbx_vmware_service_t	*service;
2747 	int			ret = SYSINFO_RET_FAIL;
2748 	const char		*url, *uuid;
2749 
2750 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __func__);
2751 
2752 	if (2 != request->nparam)
2753 	{
2754 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid number of parameters."));
2755 		goto out;
2756 	}
2757 
2758 	url = get_rparam(request, 0);
2759 	uuid = get_rparam(request, 1);
2760 
2761 	if ('\0' == *uuid)
2762 	{
2763 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid second parameter."));
2764 		goto out;
2765 	}
2766 
2767 	zbx_vmware_lock();
2768 
2769 	if (NULL == (service = get_vmware_service(url, username, password, result, &ret)))
2770 		goto unlock;
2771 
2772 	ret = vmware_service_get_vm_counter(service, uuid, "", "cpu/ready[summation]", 1, result);
2773 unlock:
2774 	zbx_vmware_unlock();
2775 out:
2776 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __func__, zbx_sysinfo_ret_string(ret));
2777 
2778 	return ret;
2779 }
2780 
check_vcenter_vm_cpu_usage(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)2781 int	check_vcenter_vm_cpu_usage(AGENT_REQUEST *request, const char *username, const char *password,
2782 		AGENT_RESULT *result)
2783 {
2784 	int	ret;
2785 
2786 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __func__);
2787 
2788 	ret = get_vcenter_vmprop(request, username, password, ZBX_VMWARE_VMPROP_CPU_USAGE, result);
2789 
2790 	if (SYSINFO_RET_OK == ret && NULL != GET_UI64_RESULT(result))
2791 		result->ui64 = result->ui64 * 1000000;
2792 
2793 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __func__, zbx_sysinfo_ret_string(ret));
2794 
2795 	return ret;
2796 }
2797 
check_vcenter_vm_datacenter_name(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)2798 int	check_vcenter_vm_datacenter_name(AGENT_REQUEST *request, const char *username, const char *password,
2799 		AGENT_RESULT *result)
2800 {
2801 	zbx_vmware_service_t	*service;
2802 	zbx_vmware_hv_t		*hv;
2803 	const char		*url, *uuid;
2804 	int			ret = SYSINFO_RET_FAIL;
2805 
2806 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __func__);
2807 
2808 	if (2 != request->nparam)
2809 	{
2810 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid number of parameters."));
2811 		goto out;
2812 	}
2813 
2814 	url = get_rparam(request, 0);
2815 	uuid = get_rparam(request, 1);
2816 
2817 	if ('\0' == *uuid)
2818 	{
2819 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid second parameter."));
2820 		goto out;
2821 	}
2822 
2823 	zbx_vmware_lock();
2824 
2825 	if (NULL == (service = get_vmware_service(url, username, password, result, &ret)))
2826 		goto unlock;
2827 
2828 	if (NULL == (hv = service_hv_get_by_vm_uuid(service, uuid)))
2829 	{
2830 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Unknown virtual machine uuid."));
2831 		goto unlock;
2832 	}
2833 
2834 	SET_STR_RESULT(result, zbx_strdup(NULL, hv->datacenter_name));
2835 	ret = SYSINFO_RET_OK;
2836 unlock:
2837 	zbx_vmware_unlock();
2838 out:
2839 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __func__, zbx_sysinfo_ret_string(ret));
2840 
2841 	return ret;
2842 }
2843 
check_vcenter_vm_discovery(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)2844 int	check_vcenter_vm_discovery(AGENT_REQUEST *request, const char *username, const char *password,
2845 		AGENT_RESULT *result)
2846 {
2847 	struct zbx_json		json_data;
2848 	const char		*url, *vm_name, *hv_name;
2849 	zbx_vmware_service_t	*service;
2850 	zbx_vmware_hv_t		*hv;
2851 	zbx_vmware_vm_t		*vm;
2852 	zbx_hashset_iter_t	iter;
2853 	int			i, ret = SYSINFO_RET_FAIL;
2854 
2855 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __func__);
2856 
2857 	if (1 != request->nparam)
2858 	{
2859 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid number of parameters."));
2860 		goto out;
2861 	}
2862 
2863 	url = get_rparam(request, 0);
2864 
2865 	zbx_vmware_lock();
2866 
2867 	if (NULL == (service = get_vmware_service(url, username, password, result, &ret)))
2868 		goto unlock;
2869 
2870 	zbx_json_initarray(&json_data, ZBX_JSON_STAT_BUF_LEN);
2871 
2872 	zbx_hashset_iter_reset(&service->data->hvs, &iter);
2873 	while (NULL != (hv = (zbx_vmware_hv_t *)zbx_hashset_iter_next(&iter)))
2874 	{
2875 		zbx_vmware_cluster_t	*cluster = NULL;
2876 
2877 		if (NULL != hv->clusterid)
2878 			cluster = cluster_get(&service->data->clusters, hv->clusterid);
2879 
2880 		for (i = 0; i < hv->vms.values_num; i++)
2881 		{
2882 			vm = (zbx_vmware_vm_t *)hv->vms.values[i];
2883 
2884 			if (NULL == (vm_name = vm->props[ZBX_VMWARE_VMPROP_NAME]))
2885 				continue;
2886 
2887 			if (NULL == (hv_name = hv->props[ZBX_VMWARE_HVPROP_NAME]))
2888 				continue;
2889 
2890 			zbx_json_addobject(&json_data, NULL);
2891 			zbx_json_addstring(&json_data, "{#VM.UUID}", vm->uuid, ZBX_JSON_TYPE_STRING);
2892 			zbx_json_addstring(&json_data, "{#VM.ID}", vm->id, ZBX_JSON_TYPE_STRING);
2893 			zbx_json_addstring(&json_data, "{#VM.NAME}", vm_name, ZBX_JSON_TYPE_STRING);
2894 			zbx_json_addstring(&json_data, "{#HV.NAME}", hv_name, ZBX_JSON_TYPE_STRING);
2895 			zbx_json_addstring(&json_data, "{#DATACENTER.NAME}", hv->datacenter_name, ZBX_JSON_TYPE_STRING);
2896 			zbx_json_addstring(&json_data, "{#CLUSTER.NAME}",
2897 					NULL != cluster ? cluster->name : "", ZBX_JSON_TYPE_STRING);
2898 			zbx_json_addstring(&json_data, "{#VM.IP}",
2899 					ZBX_NULL2EMPTY_STR(vm->props[ZBX_VMWARE_VMPROP_IPADDRESS]),
2900 					ZBX_JSON_TYPE_STRING);
2901 			zbx_json_addstring(&json_data, "{#VM.DNS}",
2902 					ZBX_NULL2EMPTY_STR(vm->props[ZBX_VMWARE_VMPROP_GUESTHOSTNAME]),
2903 					ZBX_JSON_TYPE_STRING);
2904 			zbx_json_addstring(&json_data, "{#VM.GUESTFAMILY}",
2905 					ZBX_NULL2EMPTY_STR(vm->props[ZBX_VMWARE_VMPROP_GUESTFAMILY]),
2906 					ZBX_JSON_TYPE_STRING);
2907 			zbx_json_addstring(&json_data, "{#VM.GUESTFULLNAME}",
2908 					ZBX_NULL2EMPTY_STR(vm->props[ZBX_VMWARE_VMPROP_GUESTFULLNAME]),
2909 					ZBX_JSON_TYPE_STRING);
2910 			zbx_json_addstring(&json_data, "{#VM.FOLDER}",
2911 					ZBX_NULL2EMPTY_STR(vm->props[ZBX_VMWARE_VMPROP_FOLDER]), ZBX_JSON_TYPE_STRING);
2912 
2913 			zbx_json_close(&json_data);
2914 		}
2915 	}
2916 
2917 	zbx_json_close(&json_data);
2918 
2919 	SET_STR_RESULT(result, zbx_strdup(NULL, json_data.buffer));
2920 
2921 	zbx_json_free(&json_data);
2922 
2923 	ret = SYSINFO_RET_OK;
2924 unlock:
2925 	zbx_vmware_unlock();
2926 out:
2927 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __func__, zbx_sysinfo_ret_string(ret));
2928 
2929 	return ret;
2930 }
2931 
check_vcenter_vm_hv_name(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)2932 int	check_vcenter_vm_hv_name(AGENT_REQUEST *request, const char *username, const char *password,
2933 		AGENT_RESULT *result)
2934 {
2935 	zbx_vmware_service_t	*service;
2936 	zbx_vmware_hv_t		*hv;
2937 	const char		*url, *uuid, *name;
2938 	int			ret = SYSINFO_RET_FAIL;
2939 
2940 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __func__);
2941 
2942 	if (2 != request->nparam)
2943 	{
2944 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid number of parameters."));
2945 		goto out;
2946 	}
2947 
2948 	url = get_rparam(request, 0);
2949 	uuid = get_rparam(request, 1);
2950 
2951 	if ('\0' == *uuid)
2952 	{
2953 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid second parameter."));
2954 		goto out;
2955 	}
2956 
2957 	zbx_vmware_lock();
2958 
2959 	if (NULL == (service = get_vmware_service(url, username, password, result, &ret)))
2960 		goto unlock;
2961 
2962 
2963 	if (NULL == (hv = service_hv_get_by_vm_uuid(service, uuid)))
2964 	{
2965 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Unknown virtual machine uuid."));
2966 		goto unlock;
2967 	}
2968 
2969 	if (NULL == (name = hv->props[ZBX_VMWARE_HVPROP_NAME]))
2970 	{
2971 		SET_MSG_RESULT(result, zbx_strdup(NULL, "No hypervisor name found."));
2972 		goto unlock;
2973 	}
2974 
2975 	SET_STR_RESULT(result, zbx_strdup(NULL, name));
2976 	ret = SYSINFO_RET_OK;
2977 unlock:
2978 	zbx_vmware_unlock();
2979 out:
2980 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __func__, zbx_sysinfo_ret_string(ret));
2981 
2982 	return ret;
2983 }
2984 
check_vcenter_vm_memory_size(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)2985 int	check_vcenter_vm_memory_size(AGENT_REQUEST *request, const char *username, const char *password,
2986 		AGENT_RESULT *result)
2987 {
2988 	int	ret;
2989 
2990 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __func__);
2991 
2992 	ret = get_vcenter_vmprop(request, username, password, ZBX_VMWARE_VMPROP_MEMORY_SIZE, result);
2993 
2994 	if (SYSINFO_RET_OK == ret && NULL != GET_UI64_RESULT(result))
2995 		result->ui64 = result->ui64 * ZBX_MEBIBYTE;
2996 
2997 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __func__, zbx_sysinfo_ret_string(ret));
2998 
2999 	return ret;
3000 }
3001 
check_vcenter_vm_memory_size_ballooned(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)3002 int	check_vcenter_vm_memory_size_ballooned(AGENT_REQUEST *request, const char *username, const char *password,
3003 		AGENT_RESULT *result)
3004 {
3005 	int	ret;
3006 
3007 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __func__);
3008 
3009 	ret = get_vcenter_vmprop(request, username, password, ZBX_VMWARE_VMPROP_MEMORY_SIZE_BALLOONED, result);
3010 
3011 	if (SYSINFO_RET_OK == ret && NULL != GET_UI64_RESULT(result))
3012 		result->ui64 = result->ui64 * ZBX_MEBIBYTE;
3013 
3014 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __func__, zbx_sysinfo_ret_string(ret));
3015 
3016 	return ret;
3017 }
3018 
check_vcenter_vm_memory_size_compressed(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)3019 int	check_vcenter_vm_memory_size_compressed(AGENT_REQUEST *request, const char *username, const char *password,
3020 		AGENT_RESULT *result)
3021 {
3022 	int	ret;
3023 
3024 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __func__);
3025 
3026 	ret = get_vcenter_vmprop(request, username, password, ZBX_VMWARE_VMPROP_MEMORY_SIZE_COMPRESSED, result);
3027 
3028 	if (SYSINFO_RET_OK == ret && NULL != GET_UI64_RESULT(result))
3029 		result->ui64 = result->ui64 * ZBX_MEBIBYTE;
3030 
3031 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __func__, zbx_sysinfo_ret_string(ret));
3032 
3033 	return ret;
3034 }
3035 
check_vcenter_vm_memory_size_swapped(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)3036 int	check_vcenter_vm_memory_size_swapped(AGENT_REQUEST *request, const char *username, const char *password,
3037 		AGENT_RESULT *result)
3038 {
3039 	int	ret;
3040 
3041 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __func__);
3042 
3043 	ret = get_vcenter_vmprop(request, username, password, ZBX_VMWARE_VMPROP_MEMORY_SIZE_SWAPPED, result);
3044 
3045 	if (SYSINFO_RET_OK == ret && NULL != GET_UI64_RESULT(result))
3046 		result->ui64 = result->ui64 * ZBX_MEBIBYTE;
3047 
3048 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __func__, zbx_sysinfo_ret_string(ret));
3049 
3050 	return ret;
3051 }
3052 
check_vcenter_vm_memory_size_usage_guest(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)3053 int	check_vcenter_vm_memory_size_usage_guest(AGENT_REQUEST *request, const char *username, const char *password,
3054 		AGENT_RESULT *result)
3055 {
3056 	int	ret;
3057 
3058 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __func__);
3059 
3060 	ret = get_vcenter_vmprop(request, username, password, ZBX_VMWARE_VMPROP_MEMORY_SIZE_USAGE_GUEST, result);
3061 
3062 	if (SYSINFO_RET_OK == ret && NULL != GET_UI64_RESULT(result))
3063 		result->ui64 = result->ui64 * ZBX_MEBIBYTE;
3064 
3065 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __func__, zbx_sysinfo_ret_string(ret));
3066 
3067 	return ret;
3068 }
3069 
check_vcenter_vm_memory_size_usage_host(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)3070 int	check_vcenter_vm_memory_size_usage_host(AGENT_REQUEST *request, const char *username, const char *password,
3071 		AGENT_RESULT *result)
3072 {
3073 	int	ret;
3074 
3075 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __func__);
3076 
3077 	ret = get_vcenter_vmprop(request, username, password, ZBX_VMWARE_VMPROP_MEMORY_SIZE_USAGE_HOST, result);
3078 
3079 	if (SYSINFO_RET_OK == ret && NULL != GET_UI64_RESULT(result))
3080 		result->ui64 = result->ui64 * ZBX_MEBIBYTE;
3081 
3082 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __func__, zbx_sysinfo_ret_string(ret));
3083 
3084 	return ret;
3085 }
3086 
check_vcenter_vm_memory_size_private(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)3087 int	check_vcenter_vm_memory_size_private(AGENT_REQUEST *request, const char *username, const char *password,
3088 		AGENT_RESULT *result)
3089 {
3090 	int	ret;
3091 
3092 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __func__);
3093 
3094 	ret = get_vcenter_vmprop(request, username, password, ZBX_VMWARE_VMPROP_MEMORY_SIZE_PRIVATE, result);
3095 
3096 	if (SYSINFO_RET_OK == ret && NULL != GET_UI64_RESULT(result))
3097 		result->ui64 = result->ui64 * ZBX_MEBIBYTE;
3098 
3099 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __func__, zbx_sysinfo_ret_string(ret));
3100 
3101 	return ret;
3102 }
3103 
check_vcenter_vm_memory_size_shared(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)3104 int	check_vcenter_vm_memory_size_shared(AGENT_REQUEST *request, const char *username, const char *password,
3105 		AGENT_RESULT *result)
3106 {
3107 	int	ret;
3108 
3109 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __func__);
3110 
3111 	ret = get_vcenter_vmprop(request, username, password, ZBX_VMWARE_VMPROP_MEMORY_SIZE_SHARED, result);
3112 
3113 	if (SYSINFO_RET_OK == ret && NULL != GET_UI64_RESULT(result))
3114 		result->ui64 = result->ui64 * ZBX_MEBIBYTE;
3115 
3116 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __func__, zbx_sysinfo_ret_string(ret));
3117 
3118 	return ret;
3119 }
3120 
check_vcenter_vm_powerstate(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)3121 int	check_vcenter_vm_powerstate(AGENT_REQUEST *request, const char *username, const char *password,
3122 		AGENT_RESULT *result)
3123 {
3124 	int	ret;
3125 
3126 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __func__);
3127 
3128 	ret = get_vcenter_vmprop(request, username, password, ZBX_VMWARE_VMPROP_POWER_STATE, result);
3129 
3130 	if (SYSINFO_RET_OK == ret)
3131 		ret = vmware_set_powerstate_result(result);
3132 
3133 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __func__, zbx_sysinfo_ret_string(ret));
3134 
3135 	return ret;
3136 }
3137 
check_vcenter_vm_net_if_discovery(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)3138 int	check_vcenter_vm_net_if_discovery(AGENT_REQUEST *request, const char *username, const char *password,
3139 		AGENT_RESULT *result)
3140 {
3141 	struct zbx_json		json_data;
3142 	zbx_vmware_service_t	*service;
3143 	zbx_vmware_vm_t		*vm = NULL;
3144 	zbx_vmware_dev_t	*dev;
3145 	const char		*url, *uuid;
3146 	int			i, ret = SYSINFO_RET_FAIL;
3147 
3148 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __func__);
3149 
3150 	if (2 != request->nparam)
3151 	{
3152 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid number of parameters."));
3153 		goto out;
3154 	}
3155 
3156 	url = get_rparam(request, 0);
3157 	uuid = get_rparam(request, 1);
3158 
3159 	if ('\0' == *uuid)
3160 	{
3161 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid second parameter."));
3162 		goto out;
3163 	}
3164 
3165 	zbx_vmware_lock();
3166 
3167 	if (NULL == (service = get_vmware_service(url, username, password, result, &ret)))
3168 		goto unlock;
3169 
3170 	if (NULL == (vm = service_vm_get(service, uuid)))
3171 	{
3172 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Unknown virtual machine uuid."));
3173 		goto unlock;
3174 	}
3175 
3176 	zbx_json_initarray(&json_data, ZBX_JSON_STAT_BUF_LEN);
3177 
3178 	for (i = 0; i < vm->devs.values_num; i++)
3179 	{
3180 		dev = (zbx_vmware_dev_t *)vm->devs.values[i];
3181 
3182 		if (ZBX_VMWARE_DEV_TYPE_NIC != dev->type)
3183 			continue;
3184 
3185 		zbx_json_addobject(&json_data, NULL);
3186 		zbx_json_addstring(&json_data, "{#IFNAME}", dev->instance, ZBX_JSON_TYPE_STRING);
3187 		if (NULL != dev->label)
3188 			zbx_json_addstring(&json_data, "{#IFDESC}", dev->label, ZBX_JSON_TYPE_STRING);
3189 
3190 		zbx_json_close(&json_data);
3191 	}
3192 
3193 	zbx_json_close(&json_data);
3194 
3195 	SET_STR_RESULT(result, zbx_strdup(NULL, json_data.buffer));
3196 
3197 	zbx_json_free(&json_data);
3198 
3199 	ret = SYSINFO_RET_OK;
3200 unlock:
3201 	zbx_vmware_unlock();
3202 out:
3203 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __func__, zbx_sysinfo_ret_string(ret));
3204 
3205 	return ret;
3206 }
3207 
check_vcenter_vm_net_if_in(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)3208 int	check_vcenter_vm_net_if_in(AGENT_REQUEST *request, const char *username, const char *password,
3209 		AGENT_RESULT *result)
3210 {
3211 	zbx_vmware_service_t	*service;
3212 	const char		*path, *url, *uuid, *instance, *mode;
3213 	unsigned int 		coeff;
3214 	int 			ret = SYSINFO_RET_FAIL;
3215 
3216 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __func__);
3217 
3218 	if (3 > request->nparam || request->nparam > 4)
3219 	{
3220 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid number of parameters."));
3221 		goto out;
3222 	}
3223 
3224 	url = get_rparam(request, 0);
3225 	uuid = get_rparam(request, 1);
3226 	instance = get_rparam(request, 2);
3227 	mode = get_rparam(request, 3);
3228 
3229 	if ('\0' == *uuid)
3230 	{
3231 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid second parameter."));
3232 		goto out;
3233 	}
3234 
3235 	if ('\0' == *instance)
3236 	{
3237 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid third parameter."));
3238 		goto out;
3239 	}
3240 
3241 	zbx_vmware_lock();
3242 
3243 	if (NULL == (service = get_vmware_service(url, username, password, result, &ret)))
3244 		goto unlock;
3245 
3246 	if (NULL == mode || '\0' == *mode || 0 == strcmp(mode, "bps"))
3247 	{
3248 		path = "net/received[average]";
3249 		coeff = ZBX_KIBIBYTE;
3250 	}
3251 	else if (0 == strcmp(mode, "pps"))
3252 	{
3253 		path = "net/packetsRx[summation]";
3254 		coeff = 1;
3255 	}
3256 	else
3257 	{
3258 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid fourth parameter."));
3259 		goto unlock;
3260 	}
3261 
3262 	ret = vmware_service_get_vm_counter(service, uuid, instance, path, coeff, result);
3263 unlock:
3264 	zbx_vmware_unlock();
3265 out:
3266 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __func__, zbx_sysinfo_ret_string(ret));
3267 
3268 	return ret;
3269 }
3270 
check_vcenter_vm_net_if_out(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)3271 int	check_vcenter_vm_net_if_out(AGENT_REQUEST *request, const char *username, const char *password,
3272 		AGENT_RESULT *result)
3273 {
3274 	zbx_vmware_service_t	*service;
3275 	const char		*path, *url, *uuid, *instance, *mode;
3276 	unsigned int 		coeff;
3277 	int 			ret = SYSINFO_RET_FAIL;
3278 
3279 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __func__);
3280 
3281 	if (3 > request->nparam || request->nparam > 4)
3282 	{
3283 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid number of parameters."));
3284 		goto out;
3285 	}
3286 
3287 	url = get_rparam(request, 0);
3288 	uuid = get_rparam(request, 1);
3289 	instance = get_rparam(request, 2);
3290 	mode = get_rparam(request, 3);
3291 
3292 	if ('\0' == *uuid)
3293 	{
3294 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid second parameter."));
3295 		goto out;
3296 	}
3297 
3298 	if ('\0' == *instance)
3299 	{
3300 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid third parameter."));
3301 		goto out;
3302 	}
3303 
3304 	zbx_vmware_lock();
3305 
3306 	if (NULL == (service = get_vmware_service(url, username, password, result, &ret)))
3307 		goto unlock;
3308 
3309 	if (NULL == mode || '\0' == *mode || 0 == strcmp(mode, "bps"))
3310 	{
3311 		path = "net/transmitted[average]";
3312 		coeff = ZBX_KIBIBYTE;
3313 	}
3314 	else if (0 == strcmp(mode, "pps"))
3315 	{
3316 		path = "net/packetsTx[summation]";
3317 		coeff = 1;
3318 	}
3319 	else
3320 	{
3321 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid fourth parameter."));
3322 		goto unlock;
3323 	}
3324 
3325 	ret = vmware_service_get_vm_counter(service, uuid, instance, path, coeff, result);
3326 unlock:
3327 	zbx_vmware_unlock();
3328 out:
3329 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __func__, zbx_sysinfo_ret_string(ret));
3330 
3331 	return ret;
3332 }
3333 
check_vcenter_vm_storage_committed(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)3334 int	check_vcenter_vm_storage_committed(AGENT_REQUEST *request, const char *username, const char *password,
3335 		AGENT_RESULT *result)
3336 {
3337 	int	ret;
3338 
3339 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __func__);
3340 
3341 	ret = get_vcenter_vmprop(request, username, password, ZBX_VMWARE_VMPROP_STORAGE_COMMITED, result);
3342 
3343 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __func__, zbx_sysinfo_ret_string(ret));
3344 
3345 	return ret;
3346 }
3347 
check_vcenter_vm_storage_unshared(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)3348 int	check_vcenter_vm_storage_unshared(AGENT_REQUEST *request, const char *username, const char *password,
3349 		AGENT_RESULT *result)
3350 {
3351 	int	ret;
3352 
3353 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __func__);
3354 
3355 	ret = get_vcenter_vmprop(request, username, password, ZBX_VMWARE_VMPROP_STORAGE_UNSHARED, result);
3356 
3357 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __func__, zbx_sysinfo_ret_string(ret));
3358 
3359 	return ret;
3360 }
3361 
check_vcenter_vm_storage_uncommitted(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)3362 int	check_vcenter_vm_storage_uncommitted(AGENT_REQUEST *request, const char *username, const char *password,
3363 		AGENT_RESULT *result)
3364 {
3365 	int	ret;
3366 
3367 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __func__);
3368 
3369 	ret = get_vcenter_vmprop(request, username, password, ZBX_VMWARE_VMPROP_STORAGE_UNCOMMITTED, result);
3370 
3371 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __func__, zbx_sysinfo_ret_string(ret));
3372 
3373 	return ret;
3374 }
3375 
check_vcenter_vm_uptime(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)3376 int	check_vcenter_vm_uptime(AGENT_REQUEST *request, const char *username, const char *password,
3377 		AGENT_RESULT *result)
3378 {
3379 	int	ret;
3380 
3381 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __func__);
3382 
3383 	ret = get_vcenter_vmprop(request, username, password, ZBX_VMWARE_VMPROP_UPTIME, result);
3384 
3385 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __func__, zbx_sysinfo_ret_string(ret));
3386 
3387 	return ret;
3388 }
3389 
check_vcenter_vm_vfs_dev_discovery(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)3390 int	check_vcenter_vm_vfs_dev_discovery(AGENT_REQUEST *request, const char *username, const char *password,
3391 		AGENT_RESULT *result)
3392 {
3393 	struct zbx_json		json_data;
3394 	zbx_vmware_service_t	*service;
3395 	zbx_vmware_vm_t		*vm = NULL;
3396 	zbx_vmware_dev_t	*dev;
3397 	const char		*url, *uuid;
3398 	int			i, ret = SYSINFO_RET_FAIL;
3399 
3400 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __func__);
3401 
3402 	if (2 != request->nparam)
3403 	{
3404 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid number of parameters."));
3405 		goto out;
3406 	}
3407 
3408 	url = get_rparam(request, 0);
3409 	uuid = get_rparam(request, 1);
3410 
3411 	if ('\0' == *uuid)
3412 	{
3413 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid second parameter."));
3414 		goto out;
3415 	}
3416 
3417 	zbx_vmware_lock();
3418 
3419 	if (NULL == (service = get_vmware_service(url, username, password, result, &ret)))
3420 		goto unlock;
3421 
3422 	if (NULL == (vm = service_vm_get(service, uuid)))
3423 	{
3424 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Unknown virtual machine uuid."));
3425 		goto unlock;
3426 	}
3427 
3428 	zbx_json_initarray(&json_data, ZBX_JSON_STAT_BUF_LEN);
3429 
3430 	for (i = 0; i < vm->devs.values_num; i++)
3431 	{
3432 		dev = (zbx_vmware_dev_t *)vm->devs.values[i];
3433 
3434 		if (ZBX_VMWARE_DEV_TYPE_DISK != dev->type)
3435 			continue;
3436 
3437 		zbx_json_addobject(&json_data, NULL);
3438 		zbx_json_addstring(&json_data, "{#DISKNAME}", dev->instance, ZBX_JSON_TYPE_STRING);
3439 		if (NULL != dev->label)
3440 			zbx_json_addstring(&json_data, "{#DISKDESC}", dev->label, ZBX_JSON_TYPE_STRING);
3441 		zbx_json_close(&json_data);
3442 	}
3443 
3444 	zbx_json_close(&json_data);
3445 
3446 	SET_STR_RESULT(result, zbx_strdup(NULL, json_data.buffer));
3447 
3448 	zbx_json_free(&json_data);
3449 
3450 	ret = SYSINFO_RET_OK;
3451 unlock:
3452 	zbx_vmware_unlock();
3453 out:
3454 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __func__, zbx_sysinfo_ret_string(ret));
3455 
3456 	return ret;
3457 }
3458 
check_vcenter_vm_vfs_dev_read(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)3459 int	check_vcenter_vm_vfs_dev_read(AGENT_REQUEST *request, const char *username, const char *password,
3460 		AGENT_RESULT *result)
3461 {
3462 	zbx_vmware_service_t	*service;
3463 	const char		*path, *url, *uuid, *instance, *mode;
3464 	unsigned int 		coeff;
3465 	int 			ret = SYSINFO_RET_FAIL;
3466 
3467 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __func__);
3468 
3469 	if (3 > request->nparam || request->nparam > 4)
3470 	{
3471 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid number of parameters."));
3472 		goto out;
3473 	}
3474 
3475 	url = get_rparam(request, 0);
3476 	uuid = get_rparam(request, 1);
3477 	instance = get_rparam(request, 2);
3478 	mode = get_rparam(request, 3);
3479 
3480 	if ('\0' == *uuid)
3481 	{
3482 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid second parameter."));
3483 		goto out;
3484 	}
3485 
3486 	if ('\0' == *instance)
3487 	{
3488 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid third parameter."));
3489 		goto out;
3490 	}
3491 
3492 	zbx_vmware_lock();
3493 
3494 	if (NULL == (service = get_vmware_service(url, username, password, result, &ret)))
3495 		goto unlock;
3496 
3497 	if (NULL == mode || '\0' == *mode || 0 == strcmp(mode, "bps"))
3498 	{
3499 		path = "virtualDisk/read[average]";
3500 		coeff = ZBX_KIBIBYTE;
3501 	}
3502 	else if (0 == strcmp(mode, "ops"))
3503 	{
3504 		path = "virtualDisk/numberReadAveraged[average]";
3505 		coeff = 1;
3506 	}
3507 	else
3508 	{
3509 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid fourth parameter."));
3510 		goto unlock;
3511 	}
3512 
3513 	ret =  vmware_service_get_vm_counter(service, uuid, instance, path, coeff, result);
3514 unlock:
3515 	zbx_vmware_unlock();
3516 out:
3517 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __func__, zbx_sysinfo_ret_string(ret));
3518 
3519 	return ret;
3520 }
3521 
check_vcenter_vm_vfs_dev_write(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)3522 int	check_vcenter_vm_vfs_dev_write(AGENT_REQUEST *request, const char *username, const char *password,
3523 		AGENT_RESULT *result)
3524 {
3525 	zbx_vmware_service_t	*service;
3526 	const char		*path, *url, *uuid, *instance, *mode;
3527 	unsigned int 		coeff;
3528 	int 			ret = SYSINFO_RET_FAIL;
3529 
3530 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __func__);
3531 
3532 	if (3 > request->nparam || request->nparam > 4)
3533 	{
3534 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid number of parameters."));
3535 		goto out;
3536 	}
3537 
3538 	url = get_rparam(request, 0);
3539 	uuid = get_rparam(request, 1);
3540 	instance = get_rparam(request, 2);
3541 	mode = get_rparam(request, 3);
3542 
3543 	if ('\0' == *uuid)
3544 	{
3545 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid second parameter."));
3546 		goto out;
3547 	}
3548 
3549 	if ('\0' == *instance)
3550 	{
3551 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid third parameter."));
3552 		goto out;
3553 	}
3554 
3555 	zbx_vmware_lock();
3556 
3557 	if (NULL == (service = get_vmware_service(url, username, password, result, &ret)))
3558 		goto unlock;
3559 
3560 	if (NULL == mode || '\0' == *mode || 0 == strcmp(mode, "bps"))
3561 	{
3562 		path = "virtualDisk/write[average]";
3563 		coeff = ZBX_KIBIBYTE;
3564 	}
3565 	else if (0 == strcmp(mode, "ops"))
3566 	{
3567 		path = "virtualDisk/numberWriteAveraged[average]";
3568 		coeff = 1;
3569 	}
3570 	else
3571 	{
3572 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid fourth parameter."));
3573 		goto unlock;
3574 	}
3575 
3576 	ret =  vmware_service_get_vm_counter(service, uuid, instance, path, coeff, result);
3577 unlock:
3578 	zbx_vmware_unlock();
3579 out:
3580 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __func__, zbx_sysinfo_ret_string(ret));
3581 
3582 	return ret;
3583 }
3584 
check_vcenter_vm_vfs_fs_discovery(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)3585 int	check_vcenter_vm_vfs_fs_discovery(AGENT_REQUEST *request, const char *username, const char *password,
3586 		AGENT_RESULT *result)
3587 {
3588 	struct zbx_json		json_data;
3589 	zbx_vmware_service_t	*service;
3590 	zbx_vmware_vm_t		*vm = NULL;
3591 	const char		*url, *uuid;
3592 	int			i, ret = SYSINFO_RET_FAIL;
3593 
3594 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __func__);
3595 
3596 	if (2 != request->nparam)
3597 	{
3598 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid number of parameters."));
3599 		goto out;
3600 	}
3601 
3602 	url = get_rparam(request, 0);
3603 	uuid = get_rparam(request, 1);
3604 
3605 	if ('\0' == *uuid)
3606 	{
3607 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid second parameter."));
3608 		goto out;
3609 	}
3610 
3611 	zbx_vmware_lock();
3612 
3613 	if (NULL == (service = get_vmware_service(url, username, password, result, &ret)))
3614 		goto unlock;
3615 
3616 	if (NULL == (vm = service_vm_get(service, uuid)))
3617 	{
3618 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Unknown virtual machine uuid."));
3619 		goto unlock;
3620 	}
3621 
3622 	zbx_json_initarray(&json_data, ZBX_JSON_STAT_BUF_LEN);
3623 
3624 	for (i = 0; i < vm->file_systems.values_num; i++)
3625 	{
3626 		zbx_vmware_fs_t	*fs = (zbx_vmware_fs_t *)vm->file_systems.values[i];
3627 
3628 		zbx_json_addobject(&json_data, NULL);
3629 		zbx_json_addstring(&json_data, "{#FSNAME}", fs->path, ZBX_JSON_TYPE_STRING);
3630 		zbx_json_close(&json_data);
3631 	}
3632 
3633 	zbx_json_close(&json_data);
3634 
3635 	SET_STR_RESULT(result, zbx_strdup(NULL, json_data.buffer));
3636 
3637 	zbx_json_free(&json_data);
3638 
3639 	ret = SYSINFO_RET_OK;
3640 unlock:
3641 	zbx_vmware_unlock();
3642 out:
3643 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __func__, zbx_sysinfo_ret_string(ret));
3644 
3645 	return ret;
3646 }
3647 
check_vcenter_vm_vfs_fs_size(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)3648 int	check_vcenter_vm_vfs_fs_size(AGENT_REQUEST *request, const char *username, const char *password,
3649 		AGENT_RESULT *result)
3650 {
3651 	zbx_vmware_service_t	*service;
3652 	zbx_vmware_vm_t		*vm;
3653 	const char		*url, *uuid, *fsname, *mode;
3654 	int			i, ret = SYSINFO_RET_FAIL;
3655 	zbx_vmware_fs_t		*fs = NULL;
3656 
3657 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __func__);
3658 
3659 	if (3 > request->nparam || request->nparam > 4)
3660 	{
3661 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid number of parameters."));
3662 		goto out;
3663 	}
3664 
3665 	url = get_rparam(request, 0);
3666 	uuid = get_rparam(request, 1);
3667 	fsname = get_rparam(request, 2);
3668 	mode = get_rparam(request, 3);
3669 
3670 	if ('\0' == *uuid)
3671 	{
3672 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid second parameter."));
3673 		goto out;
3674 	}
3675 
3676 	zbx_vmware_lock();
3677 
3678 	if (NULL == (service = get_vmware_service(url, username, password, result, &ret)))
3679 		goto unlock;
3680 
3681 	if (NULL == (vm = service_vm_get(service, uuid)))
3682 	{
3683 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Unknown virtual machine uuid."));
3684 		goto unlock;
3685 	}
3686 
3687 	for (i = 0; i < vm->file_systems.values_num; i++)
3688 	{
3689 		fs = (zbx_vmware_fs_t *)vm->file_systems.values[i];
3690 
3691 		if (0 == strcmp(fs->path, fsname))
3692 			break;
3693 	}
3694 
3695 	if (NULL == fs)
3696 	{
3697 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Unknown file system path."));
3698 		goto unlock;
3699 	}
3700 
3701 	ret = SYSINFO_RET_OK;
3702 
3703 	if (NULL == mode || '\0' == *mode || 0 == strcmp(mode, "total"))
3704 		SET_UI64_RESULT(result, fs->capacity);
3705 	else if (0 == strcmp(mode, "free"))
3706 		SET_UI64_RESULT(result, fs->free_space);
3707 	else if (0 == strcmp(mode, "used"))
3708 		SET_UI64_RESULT(result, fs->capacity - fs->free_space);
3709 	else if (0 == strcmp(mode, "pfree"))
3710 		SET_DBL_RESULT(result, 0 != fs->capacity ? (double)(100.0 * fs->free_space) / fs->capacity : 0);
3711 	else if (0 == strcmp(mode, "pused"))
3712 		SET_DBL_RESULT(result, 100.0 - (0 != fs->capacity ? 100.0 * fs->free_space / fs->capacity : 0));
3713 	else
3714 	{
3715 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid fourth parameter."));
3716 		ret = SYSINFO_RET_FAIL;
3717 	}
3718 unlock:
3719 	zbx_vmware_unlock();
3720 out:
3721 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __func__, zbx_sysinfo_ret_string(ret));
3722 
3723 	return ret;
3724 }
3725 
check_vcenter_vm_perfcounter(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)3726 int	check_vcenter_vm_perfcounter(AGENT_REQUEST *request, const char *username, const char *password,
3727 		AGENT_RESULT *result)
3728 {
3729 	const char 		*instance, *url, *uuid, *path;
3730 	zbx_vmware_service_t	*service;
3731 	zbx_vmware_vm_t		*vm;
3732 	zbx_uint64_t		counterid;
3733 	int			unit, ret = SYSINFO_RET_FAIL;
3734 
3735 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __func__);
3736 
3737 	if (3 > request->nparam || request->nparam > 4)
3738 	{
3739 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid number of parameters."));
3740 		goto out;
3741 	}
3742 
3743 	url = get_rparam(request, 0);
3744 	uuid = get_rparam(request, 1);
3745 	path = get_rparam(request, 2);
3746 	instance = get_rparam(request, 3);
3747 
3748 	if (NULL == instance)
3749 		instance = "";
3750 
3751 	zbx_vmware_lock();
3752 
3753 	if (NULL == (service = get_vmware_service(url, username, password, result, &ret)))
3754 		goto unlock;
3755 
3756 	if (NULL == (vm = service_vm_get(service, uuid)))
3757 	{
3758 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Unknown virtual machine uuid."));
3759 		goto unlock;
3760 	}
3761 
3762 	if (FAIL == zbx_vmware_service_get_counterid(service, path, &counterid, &unit))
3763 	{
3764 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Performance counter is not available."));
3765 		goto unlock;
3766 	}
3767 
3768 	/* FAIL is returned if counter already exists */
3769 	if (SUCCEED == zbx_vmware_service_add_perf_counter(service, "VirtualMachine", vm->id, counterid, "*"))
3770 	{
3771 		ret = SYSINFO_RET_OK;
3772 		goto unlock;
3773 	}
3774 
3775 	/* the performance counter is already being monitored, try to get the results from statistics */
3776 	ret = vmware_service_get_counter_value_by_id(service, "VirtualMachine", vm->id, counterid, instance, 1, unit,
3777 			result);
3778 unlock:
3779 	zbx_vmware_unlock();
3780 out:
3781 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __func__, zbx_sysinfo_ret_string(ret));
3782 
3783 	return ret;
3784 }
3785 
check_vcenter_dc_discovery(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)3786 int	check_vcenter_dc_discovery(AGENT_REQUEST *request, const char *username, const char *password,
3787 		AGENT_RESULT *result)
3788 {
3789 	const char		*url;
3790 	zbx_vmware_service_t	*service;
3791 	struct zbx_json		json_data;
3792 	int			i, ret = SYSINFO_RET_FAIL;
3793 
3794 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __func__);
3795 
3796 	if (1 != request->nparam)
3797 	{
3798 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid number of parameters."));
3799 		goto out;
3800 	}
3801 
3802 	url = get_rparam(request, 0);
3803 
3804 	zbx_vmware_lock();
3805 
3806 	if (NULL == (service = get_vmware_service(url, username, password, result, &ret)))
3807 		goto unlock;
3808 
3809 	zbx_json_initarray(&json_data, ZBX_JSON_STAT_BUF_LEN);
3810 
3811 	for (i = 0; i < service->data->datacenters.values_num; i++)
3812 	{
3813 		zbx_vmware_datacenter_t	*datacenter = service->data->datacenters.values[i];
3814 
3815 		zbx_json_addobject(&json_data, NULL);
3816 		zbx_json_addstring(&json_data, "{#DATACENTER}", datacenter->name, ZBX_JSON_TYPE_STRING);
3817 		zbx_json_addstring(&json_data, "{#DATACENTERID}", datacenter->id, ZBX_JSON_TYPE_STRING);
3818 		zbx_json_close(&json_data);
3819 	}
3820 
3821 	zbx_json_close(&json_data);
3822 
3823 	SET_STR_RESULT(result, zbx_strdup(NULL, json_data.buffer));
3824 
3825 	zbx_json_free(&json_data);
3826 
3827 	ret = SYSINFO_RET_OK;
3828 unlock:
3829 	zbx_vmware_unlock();
3830 out:
3831 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __func__, zbx_sysinfo_ret_string(ret));
3832 
3833 	return ret;
3834 }
3835 
check_vcenter_vm_net_if_usage(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)3836 int	check_vcenter_vm_net_if_usage(AGENT_REQUEST *request, const char *username, const char *password,
3837 		AGENT_RESULT *result)
3838 {
3839 	zbx_vmware_service_t	*service;
3840 	int			ret = SYSINFO_RET_FAIL;
3841 	const char		*url, *uuid, *instance;
3842 
3843 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __func__);
3844 
3845 	if (2 > request->nparam || request->nparam > 3)
3846 	{
3847 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid number of parameters."));
3848 		goto out;
3849 	}
3850 
3851 	url = get_rparam(request, 0);
3852 	uuid = get_rparam(request, 1);
3853 	instance = get_rparam(request, 2);
3854 
3855 	if (NULL == instance)
3856 		instance = "";
3857 
3858 	if ('\0' == *uuid)
3859 	{
3860 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid second parameter."));
3861 		goto out;
3862 	}
3863 
3864 	zbx_vmware_lock();
3865 
3866 	if (NULL == (service = get_vmware_service(url, username, password, result, &ret)))
3867 		goto unlock;
3868 
3869 	ret = vmware_service_get_vm_counter(service, uuid, instance, "net/usage[average]", ZBX_KIBIBYTE, result);
3870 unlock:
3871 	zbx_vmware_unlock();
3872 out:
3873 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __func__, zbx_sysinfo_ret_string(ret));
3874 
3875 	return ret;
3876 }
3877 
check_vcenter_vm_guest_memory_size_swapped(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)3878 int	check_vcenter_vm_guest_memory_size_swapped(AGENT_REQUEST *request, const char *username, const char *password,
3879 		AGENT_RESULT *result)
3880 {
3881 	zbx_vmware_service_t	*service;
3882 	int			ret = SYSINFO_RET_FAIL;
3883 	const char		*url, *uuid;
3884 
3885 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __func__);
3886 
3887 	if (2 != request->nparam)
3888 	{
3889 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid number of parameters."));
3890 		goto out;
3891 	}
3892 
3893 	url = get_rparam(request, 0);
3894 	uuid = get_rparam(request, 1);
3895 
3896 	if ('\0' == *uuid)
3897 	{
3898 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid second parameter."));
3899 		goto out;
3900 	}
3901 
3902 	zbx_vmware_lock();
3903 
3904 	if (NULL == (service = get_vmware_service(url, username, password, result, &ret)))
3905 		goto unlock;
3906 
3907 	ret = vmware_service_get_vm_counter(service, uuid, "", "mem/swapped[average]", ZBX_KIBIBYTE, result);
3908 unlock:
3909 	zbx_vmware_unlock();
3910 out:
3911 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __func__, zbx_sysinfo_ret_string(ret));
3912 
3913 	return ret;
3914 }
3915 
check_vcenter_vm_memory_size_consumed(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)3916 int	check_vcenter_vm_memory_size_consumed(AGENT_REQUEST *request, const char *username, const char *password,
3917 		AGENT_RESULT *result)
3918 {
3919 	zbx_vmware_service_t	*service;
3920 	int			ret = SYSINFO_RET_FAIL;
3921 	const char		*url, *uuid;
3922 
3923 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __func__);
3924 
3925 	if (2 != request->nparam)
3926 	{
3927 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid number of parameters."));
3928 		goto out;
3929 	}
3930 
3931 	url = get_rparam(request, 0);
3932 	uuid = get_rparam(request, 1);
3933 
3934 	if ('\0' == *uuid)
3935 	{
3936 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid second parameter."));
3937 		goto out;
3938 	}
3939 
3940 	zbx_vmware_lock();
3941 
3942 	if (NULL == (service = get_vmware_service(url, username, password, result, &ret)))
3943 		goto unlock;
3944 
3945 	ret = vmware_service_get_vm_counter(service, uuid, "", "mem/consumed[average]", ZBX_KIBIBYTE, result);
3946 unlock:
3947 	zbx_vmware_unlock();
3948 out:
3949 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __func__, zbx_sysinfo_ret_string(ret));
3950 
3951 	return ret;
3952 }
3953 
check_vcenter_vm_memory_usage(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)3954 int	check_vcenter_vm_memory_usage(AGENT_REQUEST *request, const char *username, const char *password,
3955 		AGENT_RESULT *result)
3956 {
3957 	zbx_vmware_service_t	*service;
3958 	int			ret = SYSINFO_RET_FAIL;
3959 	const char		*url, *uuid;
3960 
3961 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __func__);
3962 
3963 	if (2 != request->nparam)
3964 	{
3965 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid number of parameters."));
3966 		goto out;
3967 	}
3968 
3969 	url = get_rparam(request, 0);
3970 	uuid = get_rparam(request, 1);
3971 
3972 	if ('\0' == *uuid)
3973 	{
3974 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid second parameter."));
3975 		goto out;
3976 	}
3977 
3978 	zbx_vmware_lock();
3979 
3980 	if (NULL == (service = get_vmware_service(url, username, password, result, &ret)))
3981 		goto unlock;
3982 
3983 	ret = vmware_service_get_vm_counter(service, uuid, "", "mem/usage[average]", 0, result);
3984 unlock:
3985 	zbx_vmware_unlock();
3986 out:
3987 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __func__, zbx_sysinfo_ret_string(ret));
3988 
3989 	return ret;
3990 }
3991 
check_vcenter_vm_cpu_latency(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)3992 int	check_vcenter_vm_cpu_latency(AGENT_REQUEST *request, const char *username, const char *password,
3993 		AGENT_RESULT *result)
3994 {
3995 	zbx_vmware_service_t	*service;
3996 	int			ret = SYSINFO_RET_FAIL;
3997 	const char		*url, *uuid;
3998 
3999 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __func__);
4000 
4001 	if (2 != request->nparam)
4002 	{
4003 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid number of parameters."));
4004 		goto out;
4005 	}
4006 
4007 	url = get_rparam(request, 0);
4008 	uuid = get_rparam(request, 1);
4009 
4010 	if ('\0' == *uuid)
4011 	{
4012 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid second parameter."));
4013 		goto out;
4014 	}
4015 
4016 	zbx_vmware_lock();
4017 
4018 	if (NULL == (service = get_vmware_service(url, username, password, result, &ret)))
4019 		goto unlock;
4020 
4021 	ret = vmware_service_get_vm_counter(service, uuid, "", "cpu/latency[average]", 0, result);
4022 unlock:
4023 	zbx_vmware_unlock();
4024 out:
4025 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __func__, zbx_sysinfo_ret_string(ret));
4026 
4027 	return ret;
4028 }
4029 
check_vcenter_vm_cpu_readiness(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)4030 int	check_vcenter_vm_cpu_readiness(AGENT_REQUEST *request, const char *username, const char *password,
4031 		AGENT_RESULT *result)
4032 {
4033 	zbx_vmware_service_t	*service;
4034 	int			ret = SYSINFO_RET_FAIL;
4035 	const char		*url, *uuid, *instance;
4036 
4037 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __func__);
4038 
4039 	if (2 > request->nparam || request->nparam > 3)
4040 	{
4041 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid number of parameters."));
4042 		goto out;
4043 	}
4044 
4045 	url = get_rparam(request, 0);
4046 	uuid = get_rparam(request, 1);
4047 	instance = get_rparam(request, 2);
4048 
4049 	if (NULL == instance)
4050 		instance = "";
4051 
4052 	if ('\0' == *uuid)
4053 	{
4054 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid second parameter."));
4055 		goto out;
4056 	}
4057 
4058 	zbx_vmware_lock();
4059 
4060 	if (NULL == (service = get_vmware_service(url, username, password, result, &ret)))
4061 		goto unlock;
4062 
4063 	ret = vmware_service_get_vm_counter(service, uuid, instance, "cpu/readiness[average]", 0, result);
4064 unlock:
4065 	zbx_vmware_unlock();
4066 out:
4067 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __func__, zbx_sysinfo_ret_string(ret));
4068 
4069 	return ret;
4070 }
4071 
check_vcenter_vm_cpu_swapwait(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)4072 int	check_vcenter_vm_cpu_swapwait(AGENT_REQUEST *request, const char *username, const char *password,
4073 		AGENT_RESULT *result)
4074 {
4075 	zbx_vmware_service_t	*service;
4076 	int			ret = SYSINFO_RET_FAIL;
4077 	const char		*url, *uuid, *instance;
4078 
4079 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __func__);
4080 
4081 	if (2 > request->nparam || request->nparam > 3)
4082 	{
4083 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid number of parameters."));
4084 		goto out;
4085 	}
4086 
4087 	url = get_rparam(request, 0);
4088 	uuid = get_rparam(request, 1);
4089 	instance = get_rparam(request, 2);
4090 
4091 	if (NULL == instance)
4092 		instance = "";
4093 
4094 	if ('\0' == *uuid)
4095 	{
4096 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid second parameter."));
4097 		goto out;
4098 	}
4099 
4100 	zbx_vmware_lock();
4101 
4102 	if (NULL == (service = get_vmware_service(url, username, password, result, &ret)))
4103 		goto unlock;
4104 
4105 	ret = vmware_service_get_vm_counter(service, uuid, instance, "cpu/swapwait[summation]", 0, result);
4106 unlock:
4107 	zbx_vmware_unlock();
4108 out:
4109 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __func__, zbx_sysinfo_ret_string(ret));
4110 
4111 	return ret;
4112 }
4113 
check_vcenter_vm_cpu_usage_perf(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)4114 int	check_vcenter_vm_cpu_usage_perf(AGENT_REQUEST *request, const char *username, const char *password,
4115 		AGENT_RESULT *result)
4116 {
4117 	zbx_vmware_service_t	*service;
4118 	int			ret = SYSINFO_RET_FAIL;
4119 	const char		*url, *uuid;
4120 
4121 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __func__);
4122 
4123 	if (2 != request->nparam)
4124 	{
4125 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid number of parameters."));
4126 		goto out;
4127 	}
4128 
4129 	url = get_rparam(request, 0);
4130 	uuid = get_rparam(request, 1);
4131 
4132 	if ('\0' == *uuid)
4133 	{
4134 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid second parameter."));
4135 		goto out;
4136 	}
4137 
4138 	zbx_vmware_lock();
4139 
4140 	if (NULL == (service = get_vmware_service(url, username, password, result, &ret)))
4141 		goto unlock;
4142 
4143 	ret = vmware_service_get_vm_counter(service, uuid, "", "cpu/usage[average]", 0, result);
4144 unlock:
4145 	zbx_vmware_unlock();
4146 out:
4147 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __func__, zbx_sysinfo_ret_string(ret));
4148 
4149 	return ret;
4150 }
4151 
check_vcenter_vm_storage_readoio(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)4152 int	check_vcenter_vm_storage_readoio(AGENT_REQUEST *request, const char *username, const char *password,
4153 		AGENT_RESULT *result)
4154 {
4155 	zbx_vmware_service_t	*service;
4156 	int			ret = SYSINFO_RET_FAIL;
4157 	const char		*url, *uuid, *instance;
4158 
4159 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __func__);
4160 
4161 	if (3 != request->nparam)
4162 	{
4163 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid number of parameters."));
4164 		goto out;
4165 	}
4166 
4167 	url = get_rparam(request, 0);
4168 	uuid = get_rparam(request, 1);
4169 	instance = get_rparam(request, 2);
4170 
4171 	if ('\0' == *uuid)
4172 	{
4173 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid second parameter."));
4174 		goto out;
4175 	}
4176 
4177 	if ('\0' == *instance)
4178 	{
4179 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid third parameter."));
4180 		goto out;
4181 	}
4182 
4183 	zbx_vmware_lock();
4184 
4185 	if (NULL == (service = get_vmware_service(url, username, password, result, &ret)))
4186 		goto unlock;
4187 
4188 	ret = vmware_service_get_vm_counter(service, uuid, instance, "virtualDisk/readOIO[latest]", 1, result);
4189 unlock:
4190 	zbx_vmware_unlock();
4191 out:
4192 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __func__, zbx_sysinfo_ret_string(ret));
4193 
4194 	return ret;
4195 }
4196 
check_vcenter_vm_storage_writeoio(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)4197 int	check_vcenter_vm_storage_writeoio(AGENT_REQUEST *request, const char *username, const char *password,
4198 		AGENT_RESULT *result)
4199 {
4200 	zbx_vmware_service_t	*service;
4201 	int			ret = SYSINFO_RET_FAIL;
4202 	const char		*url, *uuid, *instance;
4203 
4204 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __func__);
4205 
4206 	if (3 != request->nparam)
4207 	{
4208 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid number of parameters."));
4209 		goto out;
4210 	}
4211 
4212 	url = get_rparam(request, 0);
4213 	uuid = get_rparam(request, 1);
4214 	instance = get_rparam(request, 2);
4215 
4216 	if ('\0' == *uuid)
4217 	{
4218 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid second parameter."));
4219 		goto out;
4220 	}
4221 
4222 	if ('\0' == *instance)
4223 	{
4224 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid third parameter."));
4225 		goto out;
4226 	}
4227 
4228 	zbx_vmware_lock();
4229 
4230 	if (NULL == (service = get_vmware_service(url, username, password, result, &ret)))
4231 		goto unlock;
4232 
4233 	ret = vmware_service_get_vm_counter(service, uuid, instance, "virtualDisk/writeOIO[latest]", 1, result);
4234 unlock:
4235 	zbx_vmware_unlock();
4236 out:
4237 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __func__, zbx_sysinfo_ret_string(ret));
4238 
4239 	return ret;
4240 }
4241 
check_vcenter_vm_storage_totalwritelatency(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)4242 int	check_vcenter_vm_storage_totalwritelatency(AGENT_REQUEST *request, const char *username, const char *password,
4243 		AGENT_RESULT *result)
4244 {
4245 	zbx_vmware_service_t	*service;
4246 	int			ret = SYSINFO_RET_FAIL;
4247 	const char		*url, *uuid, *instance;
4248 
4249 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __func__);
4250 
4251 	if (3 != request->nparam)
4252 	{
4253 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid number of parameters."));
4254 		goto out;
4255 	}
4256 
4257 	url = get_rparam(request, 0);
4258 	uuid = get_rparam(request, 1);
4259 	instance = get_rparam(request, 2);
4260 
4261 	if ('\0' == *uuid)
4262 	{
4263 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid second parameter."));
4264 		goto out;
4265 	}
4266 
4267 	if ('\0' == *instance)
4268 	{
4269 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid third parameter."));
4270 		goto out;
4271 	}
4272 
4273 	zbx_vmware_lock();
4274 
4275 	if (NULL == (service = get_vmware_service(url, username, password, result, &ret)))
4276 		goto unlock;
4277 
4278 	ret = vmware_service_get_vm_counter(service, uuid, instance,
4279 			"virtualDisk/totalWriteLatency[average]", 1, result);
4280 unlock:
4281 	zbx_vmware_unlock();
4282 out:
4283 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __func__, zbx_sysinfo_ret_string(ret));
4284 
4285 	return ret;
4286 }
4287 
check_vcenter_vm_storage_totalreadlatency(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)4288 int	check_vcenter_vm_storage_totalreadlatency(AGENT_REQUEST *request, const char *username, const char *password,
4289 		AGENT_RESULT *result)
4290 {
4291 	zbx_vmware_service_t	*service;
4292 	int			ret = SYSINFO_RET_FAIL;
4293 	const char		*url, *uuid, *instance;
4294 
4295 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __func__);
4296 
4297 	if (3 != request->nparam)
4298 	{
4299 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid number of parameters."));
4300 		goto out;
4301 	}
4302 
4303 	url = get_rparam(request, 0);
4304 	uuid = get_rparam(request, 1);
4305 	instance = get_rparam(request, 2);
4306 
4307 	if ('\0' == *uuid)
4308 	{
4309 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid second parameter."));
4310 		goto out;
4311 	}
4312 
4313 	if ('\0' == *instance)
4314 	{
4315 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid third parameter."));
4316 		goto out;
4317 	}
4318 
4319 	zbx_vmware_lock();
4320 
4321 	if (NULL == (service = get_vmware_service(url, username, password, result, &ret)))
4322 		goto unlock;
4323 
4324 	ret = vmware_service_get_vm_counter(service, uuid, instance,
4325 			"virtualDisk/totalReadLatency[average]", 1, result);
4326 unlock:
4327 	zbx_vmware_unlock();
4328 out:
4329 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __func__, zbx_sysinfo_ret_string(ret));
4330 
4331 	return ret;
4332 }
4333 
check_vcenter_vm_guest_uptime(AGENT_REQUEST * request,const char * username,const char * password,AGENT_RESULT * result)4334 int	check_vcenter_vm_guest_uptime(AGENT_REQUEST *request, const char *username, const char *password,
4335 		AGENT_RESULT *result)
4336 {
4337 	zbx_vmware_service_t	*service;
4338 	int			ret = SYSINFO_RET_FAIL;
4339 	const char		*url, *uuid;
4340 
4341 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __func__);
4342 
4343 	if (2 != request->nparam)
4344 	{
4345 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid number of parameters."));
4346 		goto out;
4347 	}
4348 
4349 	url = get_rparam(request, 0);
4350 	uuid = get_rparam(request, 1);
4351 
4352 	if ('\0' == *uuid)
4353 	{
4354 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid second parameter."));
4355 		goto out;
4356 	}
4357 
4358 	zbx_vmware_lock();
4359 
4360 	if (NULL == (service = get_vmware_service(url, username, password, result, &ret)))
4361 		goto unlock;
4362 
4363 	ret = vmware_service_get_vm_counter(service, uuid, "", "sys/osUptime[latest]", 1, result);
4364 unlock:
4365 	zbx_vmware_unlock();
4366 out:
4367 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __func__, zbx_sysinfo_ret_string(ret));
4368 
4369 	return ret;
4370 }
4371 
4372 
4373 #endif	/* defined(HAVE_LIBXML2) && defined(HAVE_LIBCURL) */
4374