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(DC_ITEM * item,AGENT_RESULT * result)43 int get_value_external(DC_ITEM *item, AGENT_RESULT *result)
44 {
45 const char *__function_name = "get_value_external";
46
47 char error[ITEM_ERROR_LEN_MAX], *cmd = NULL, *buf = NULL;
48 size_t cmd_alloc = ZBX_KIBIBYTE, cmd_offset = 0;
49 int i, ret = NOTSUPPORTED;
50 AGENT_REQUEST request;
51
52 zabbix_log(LOG_LEVEL_DEBUG, "In %s() key:'%s'", __function_name, item->key);
53
54 init_request(&request);
55
56 if (SUCCEED != parse_item_key(item->key, &request))
57 {
58 SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid item key format."));
59 goto out;
60 }
61
62 cmd = (char *)zbx_malloc(cmd, cmd_alloc);
63 zbx_snprintf_alloc(&cmd, &cmd_alloc, &cmd_offset, "%s/%s", CONFIG_EXTERNALSCRIPTS, get_rkey(&request));
64
65 if (-1 == access(cmd, X_OK))
66 {
67 SET_MSG_RESULT(result, zbx_dsprintf(NULL, "%s: %s", cmd, zbx_strerror(errno)));
68 goto out;
69 }
70
71 for (i = 0; i < get_rparams_num(&request); i++)
72 {
73 const char *param;
74 char *param_esc;
75
76 param = get_rparam(&request, i);
77
78 param_esc = zbx_dyn_escape_shell_single_quote(param);
79 zbx_snprintf_alloc(&cmd, &cmd_alloc, &cmd_offset, " '%s'", param_esc);
80 zbx_free(param_esc);
81 }
82
83 if (SUCCEED == zbx_execute(cmd, &buf, error, sizeof(error), CONFIG_TIMEOUT, ZBX_EXIT_CODE_CHECKS_DISABLED))
84 {
85 zbx_rtrim(buf, ZBX_WHITESPACE);
86
87 set_result_type(result, ITEM_VALUE_TYPE_TEXT, buf);
88 zbx_free(buf);
89
90 ret = SUCCEED;
91 }
92 else
93 SET_MSG_RESULT(result, zbx_strdup(NULL, error));
94 out:
95 zbx_free(cmd);
96
97 free_request(&request);
98
99 zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __function_name, zbx_result_string(ret));
100
101 return ret;
102 }
103