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 "checks_simple_vmware.h"
21 #include "checks_simple.h"
22 #include "simple.h"
23 #include "log.h"
24 
25 #include "zbxself.h"
26 
27 typedef int	(*vmfunc_t)(AGENT_REQUEST *, const char *, const char *, AGENT_RESULT *);
28 
29 #define ZBX_VMWARE_PREFIX	"vmware."
30 
31 typedef struct
32 {
33 	const char	*key;
34 	vmfunc_t	func;
35 }
36 zbx_vmcheck_t;
37 
38 #if defined(HAVE_LIBXML2) && defined(HAVE_LIBCURL)
39 #	define VMCHECK_FUNC(func)	func
40 #else
41 #	define VMCHECK_FUNC(func)	NULL
42 #endif
43 
44 static zbx_vmcheck_t	vmchecks[] =
45 {
46 	{"cluster.discovery", VMCHECK_FUNC(check_vcenter_cluster_discovery)},
47 	{"cluster.status", VMCHECK_FUNC(check_vcenter_cluster_status)},
48 	{"version", VMCHECK_FUNC(check_vcenter_version)},
49 	{"fullname", VMCHECK_FUNC(check_vcenter_fullname)},
50 	{"datastore.discovery", VMCHECK_FUNC(check_vcenter_datastore_discovery)},
51 	{"datastore.read", VMCHECK_FUNC(check_vcenter_datastore_read)},
52 	{"datastore.size", VMCHECK_FUNC(check_vcenter_datastore_size)},
53 	{"datastore.write", VMCHECK_FUNC(check_vcenter_datastore_write)},
54 	{"datastore.hv.list", VMCHECK_FUNC(check_vcenter_datastore_hv_list)},
55 
56 	{"hv.cluster.name", VMCHECK_FUNC(check_vcenter_hv_cluster_name)},
57 	{"hv.cpu.usage", VMCHECK_FUNC(check_vcenter_hv_cpu_usage)},
58 	{"hv.datacenter.name", VMCHECK_FUNC(check_vcenter_hv_datacenter_name)},
59 	{"hv.datastore.discovery", VMCHECK_FUNC(check_vcenter_hv_datastore_discovery)},
60 	{"hv.datastore.read", VMCHECK_FUNC(check_vcenter_hv_datastore_read)},
61 	{"hv.datastore.size", VMCHECK_FUNC(check_vcenter_hv_datastore_size)},
62 	{"hv.datastore.write", VMCHECK_FUNC(check_vcenter_hv_datastore_write)},
63 	{"hv.datastore.list", VMCHECK_FUNC(check_vcenter_hv_datastore_list)},
64 	{"hv.discovery", VMCHECK_FUNC(check_vcenter_hv_discovery)},
65 	{"hv.fullname", VMCHECK_FUNC(check_vcenter_hv_fullname)},
66 	{"hv.hw.cpu.num", VMCHECK_FUNC(check_vcenter_hv_hw_cpu_num)},
67 	{"hv.hw.cpu.freq", VMCHECK_FUNC(check_vcenter_hv_hw_cpu_freq)},
68 	{"hv.hw.cpu.model", VMCHECK_FUNC(check_vcenter_hv_hw_cpu_model)},
69 	{"hv.hw.cpu.threads", VMCHECK_FUNC(check_vcenter_hv_hw_cpu_threads)},
70 	{"hv.hw.memory", VMCHECK_FUNC(check_vcenter_hv_hw_memory)},
71 	{"hv.hw.model", VMCHECK_FUNC(check_vcenter_hv_hw_model)},
72 	{"hv.hw.uuid", VMCHECK_FUNC(check_vcenter_hv_hw_uuid)},
73 	{"hv.hw.vendor", VMCHECK_FUNC(check_vcenter_hv_hw_vendor)},
74 	{"hv.memory.size.ballooned", VMCHECK_FUNC(check_vcenter_hv_memory_size_ballooned)},
75 	{"hv.memory.used", VMCHECK_FUNC(check_vcenter_hv_memory_used)},
76 	{"hv.network.in", VMCHECK_FUNC(check_vcenter_hv_network_in)},
77 	{"hv.network.out", VMCHECK_FUNC(check_vcenter_hv_network_out)},
78 	{"hv.perfcounter", VMCHECK_FUNC(check_vcenter_hv_perfcounter)},
79 	{"hv.sensor.health.state", VMCHECK_FUNC(check_vcenter_hv_sensor_health_state)},
80 	{"hv.status", VMCHECK_FUNC(check_vcenter_hv_status)},
81 	{"hv.maintenance", VMCHECK_FUNC(check_vcenter_hv_maintenance)},
82 	{"hv.uptime", VMCHECK_FUNC(check_vcenter_hv_uptime)},
83 	{"hv.version", VMCHECK_FUNC(check_vcenter_hv_version)},
84 	{"hv.sensors.get", VMCHECK_FUNC(check_vcenter_hv_sensors_get)},
85 	{"hv.vm.num", VMCHECK_FUNC(check_vcenter_hv_vm_num)},
86 
87 	{"vm.cluster.name", VMCHECK_FUNC(check_vcenter_vm_cluster_name)},
88 	{"vm.cpu.num", VMCHECK_FUNC(check_vcenter_vm_cpu_num)},
89 	{"vm.cpu.ready", VMCHECK_FUNC(check_vcenter_vm_cpu_ready)},
90 	{"vm.cpu.usage", VMCHECK_FUNC(check_vcenter_vm_cpu_usage)},
91 	{"vm.datacenter.name", VMCHECK_FUNC(check_vcenter_vm_datacenter_name)},
92 	{"vm.discovery", VMCHECK_FUNC(check_vcenter_vm_discovery)},
93 	{"vm.hv.name", VMCHECK_FUNC(check_vcenter_vm_hv_name)},
94 	{"vm.memory.size", VMCHECK_FUNC(check_vcenter_vm_memory_size)},
95 	{"vm.memory.size.ballooned", VMCHECK_FUNC(check_vcenter_vm_memory_size_ballooned)},
96 	{"vm.memory.size.compressed", VMCHECK_FUNC(check_vcenter_vm_memory_size_compressed)},
97 	{"vm.memory.size.swapped", VMCHECK_FUNC(check_vcenter_vm_memory_size_swapped)},
98 	{"vm.memory.size.usage.guest", VMCHECK_FUNC(check_vcenter_vm_memory_size_usage_guest)},
99 	{"vm.memory.size.usage.host", VMCHECK_FUNC(check_vcenter_vm_memory_size_usage_host)},
100 	{"vm.memory.size.private", VMCHECK_FUNC(check_vcenter_vm_memory_size_private)},
101 	{"vm.memory.size.shared", VMCHECK_FUNC(check_vcenter_vm_memory_size_shared)},
102 	{"vm.net.if.discovery", VMCHECK_FUNC(check_vcenter_vm_net_if_discovery)},
103 	{"vm.net.if.in", VMCHECK_FUNC(check_vcenter_vm_net_if_in)},
104 	{"vm.net.if.out", VMCHECK_FUNC(check_vcenter_vm_net_if_out)},
105 	{"vm.perfcounter", VMCHECK_FUNC(check_vcenter_vm_perfcounter)},
106 	{"vm.powerstate", VMCHECK_FUNC(check_vcenter_vm_powerstate)},
107 	{"vm.storage.committed", VMCHECK_FUNC(check_vcenter_vm_storage_committed)},
108 	{"vm.storage.unshared", VMCHECK_FUNC(check_vcenter_vm_storage_unshared)},
109 	{"vm.storage.uncommitted", VMCHECK_FUNC(check_vcenter_vm_storage_uncommitted)},
110 	{"vm.uptime", VMCHECK_FUNC(check_vcenter_vm_uptime)},
111 	{"vm.vfs.dev.discovery", VMCHECK_FUNC(check_vcenter_vm_vfs_dev_discovery)},
112 	{"vm.vfs.dev.read", VMCHECK_FUNC(check_vcenter_vm_vfs_dev_read)},
113 	{"vm.vfs.dev.write", VMCHECK_FUNC(check_vcenter_vm_vfs_dev_write)},
114 	{"vm.vfs.fs.discovery", VMCHECK_FUNC(check_vcenter_vm_vfs_fs_discovery)},
115 	{"vm.vfs.fs.size", VMCHECK_FUNC(check_vcenter_vm_vfs_fs_size)},
116 
117 	{"dc.discovery", VMCHECK_FUNC(check_vcenter_dc_discovery)},
118 
119 	{NULL, NULL}
120 };
121 
122 /******************************************************************************
123  *                                                                            *
124  * Function: get_vmware_function                                              *
125  *                                                                            *
126  * Purpose: Retrieves a handler of the item key                               *
127  *                                                                            *
128  * Parameters: key    - [IN] an item key (without parameters)                 *
129  *             vmfunc - [OUT] a handler of the item key; can be NULL if       *
130  *                            libxml2 or libcurl is not compiled in           *
131  *                                                                            *
132  * Return value: SUCCEED if key is a valid VMware key, FAIL - otherwise       *
133  *                                                                            *
134  ******************************************************************************/
get_vmware_function(const char * key,vmfunc_t * vmfunc)135 static int	get_vmware_function(const char *key, vmfunc_t *vmfunc)
136 {
137 	zbx_vmcheck_t	*check;
138 
139 	if (0 != strncmp(key, ZBX_VMWARE_PREFIX, ZBX_CONST_STRLEN(ZBX_VMWARE_PREFIX)))
140 		return FAIL;
141 
142 	for (check = vmchecks; NULL != check->key; check++)
143 	{
144 		if (0 == strcmp(key + ZBX_CONST_STRLEN(ZBX_VMWARE_PREFIX), check->key))
145 		{
146 			*vmfunc = check->func;
147 			return SUCCEED;
148 		}
149 	}
150 
151 	return FAIL;
152 }
153 
get_value_simple(const DC_ITEM * item,AGENT_RESULT * result,zbx_vector_ptr_t * add_results)154 int	get_value_simple(const DC_ITEM *item, AGENT_RESULT *result, zbx_vector_ptr_t *add_results)
155 {
156 	AGENT_REQUEST	request;
157 	vmfunc_t	vmfunc;
158 	int		ret = NOTSUPPORTED;
159 
160 	zabbix_log(LOG_LEVEL_DEBUG, "In %s() key_orig:'%s' addr:'%s'", __func__, item->key_orig, item->interface.addr);
161 
162 	init_request(&request);
163 
164 	if (SUCCEED != parse_item_key(item->key, &request))
165 	{
166 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid item key format."));
167 		goto out;
168 	}
169 
170 	request.lastlogsize = item->lastlogsize;
171 
172 	if (0 == strcmp(request.key, "net.tcp.service") || 0 == strcmp(request.key, "net.udp.service"))
173 	{
174 		if (SYSINFO_RET_OK == check_service(&request, item->interface.addr, result, 0))
175 			ret = SUCCEED;
176 	}
177 	else if (0 == strcmp(request.key, "net.tcp.service.perf") || 0 == strcmp(request.key, "net.udp.service.perf"))
178 	{
179 		if (SYSINFO_RET_OK == check_service(&request, item->interface.addr, result, 1))
180 			ret = SUCCEED;
181 	}
182 	else if (SUCCEED == get_vmware_function(request.key, &vmfunc))
183 	{
184 		if (NULL != vmfunc)
185 		{
186 			if (0 == get_process_type_forks(ZBX_PROCESS_TYPE_VMWARE))
187 			{
188 				SET_MSG_RESULT(result, zbx_strdup(NULL, "No \"vmware collector\" processes started."));
189 				goto out;
190 			}
191 
192 			if (SYSINFO_RET_OK == vmfunc(&request, item->username, item->password, result))
193 				ret = SUCCEED;
194 		}
195 		else
196 			SET_MSG_RESULT(result, zbx_strdup(NULL, "Support for VMware checks was not compiled in."));
197 	}
198 	else if (0 == strcmp(request.key, ZBX_VMWARE_PREFIX "eventlog"))
199 	{
200 #if defined(HAVE_LIBXML2) && defined(HAVE_LIBCURL)
201 		if (SYSINFO_RET_OK == check_vcenter_eventlog(&request, item, result, add_results))
202 			ret = SUCCEED;
203 #else
204 		ZBX_UNUSED(add_results);
205 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Support for VMware checks was not compiled in."));
206 #endif
207 	}
208 	else
209 	{
210 		/* it will execute item from a loadable module if any */
211 		if (SUCCEED == process(item->key, PROCESS_MODULE_COMMAND, result))
212 			ret = SUCCEED;
213 	}
214 
215 	if (NOTSUPPORTED == ret && !ISSET_MSG(result))
216 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Simple check is not supported."));
217 
218 out:
219 	free_request(&request);
220 
221 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __func__, zbx_result_string(ret));
222 
223 	return ret;
224 }
225