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 #include "log.h"
22 #include "zbxexec.h"
23 
24 #include "checks_external.h"
25 
26 extern char	*CONFIG_EXTERNALSCRIPTS;
27 
28 /******************************************************************************
29  *                                                                            *
30  * Function: get_value_external                                               *
31  *                                                                            *
32  * Purpose: retrieve data from script executed on Zabbix server               *
33  *                                                                            *
34  * Parameters: item - item we are interested in                               *
35  *                                                                            *
36  * Return value: SUCCEED - data successfully retrieved and stored in result   *
37  *                         and result_str (as string)                         *
38  *               NOTSUPPORTED - requested item is not supported               *
39  *                                                                            *
40  * Author: Mike Nestor, rewritten by Alexander Vladishev                      *
41  *                                                                            *
42  ******************************************************************************/
get_value_external(const DC_ITEM * item,AGENT_RESULT * result)43 int	get_value_external(const DC_ITEM *item, AGENT_RESULT *result)
44 {
45 	char		error[ITEM_ERROR_LEN_MAX], *cmd = NULL, *buf = NULL;
46 	size_t		cmd_alloc = ZBX_KIBIBYTE, cmd_offset = 0;
47 	int		i, ret = NOTSUPPORTED;
48 	AGENT_REQUEST	request;
49 
50 	zabbix_log(LOG_LEVEL_DEBUG, "In %s() key:'%s'", __func__, item->key);
51 
52 	init_request(&request);
53 
54 	if (SUCCEED != parse_item_key(item->key, &request))
55 	{
56 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid item key format."));
57 		goto out;
58 	}
59 
60 	cmd = (char *)zbx_malloc(cmd, cmd_alloc);
61 	zbx_snprintf_alloc(&cmd, &cmd_alloc, &cmd_offset, "%s/%s", CONFIG_EXTERNALSCRIPTS, get_rkey(&request));
62 
63 	if (-1 == access(cmd, X_OK))
64 	{
65 		SET_MSG_RESULT(result, zbx_dsprintf(NULL, "%s: %s", cmd, zbx_strerror(errno)));
66 		goto out;
67 	}
68 
69 	for (i = 0; i < get_rparams_num(&request); i++)
70 	{
71 		const char	*param;
72 		char		*param_esc;
73 
74 		param = get_rparam(&request, i);
75 
76 		param_esc = zbx_dyn_escape_shell_single_quote(param);
77 		zbx_snprintf_alloc(&cmd, &cmd_alloc, &cmd_offset, " '%s'", param_esc);
78 		zbx_free(param_esc);
79 	}
80 
81 	if (SUCCEED == zbx_execute(cmd, &buf, error, sizeof(error), CONFIG_TIMEOUT, ZBX_EXIT_CODE_CHECKS_DISABLED))
82 	{
83 		zbx_rtrim(buf, ZBX_WHITESPACE);
84 
85 		set_result_type(result, ITEM_VALUE_TYPE_TEXT, buf);
86 		zbx_free(buf);
87 
88 		ret = SUCCEED;
89 	}
90 	else
91 		SET_MSG_RESULT(result, zbx_strdup(NULL, error));
92 out:
93 	zbx_free(cmd);
94 
95 	free_request(&request);
96 
97 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __func__, zbx_result_string(ret));
98 
99 	return ret;
100 }
101