1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
|
/*
** Zabbix
** Copyright (C) 2001-2019 Zabbix SIA
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
**/
#include "common.h"
#include "log.h"
#include "zbxexec.h"
#include "checks_external.h"
extern char *CONFIG_EXTERNALSCRIPTS;
/******************************************************************************
* *
* Function: get_value_external *
* *
* Purpose: retrieve data from script executed on Zabbix server *
* *
* Parameters: item - item we are interested in *
* *
* Return value: SUCCEED - data successfully retrieved and stored in result *
* and result_str (as string) *
* NOTSUPPORTED - requested item is not supported *
* *
* Author: Mike Nestor, rewritten by Alexander Vladishev *
* *
******************************************************************************/
int get_value_external(DC_ITEM *item, AGENT_RESULT *result)
{
const char *__function_name = "get_value_external";
char error[ITEM_ERROR_LEN_MAX], *cmd = NULL, *buf = NULL;
size_t cmd_alloc = ZBX_KIBIBYTE, cmd_offset = 0;
int i, ret = NOTSUPPORTED;
AGENT_REQUEST request;
zabbix_log(LOG_LEVEL_DEBUG, "In %s() key:'%s'", __function_name, item->key);
init_request(&request);
if (SUCCEED != parse_item_key(item->key, &request))
{
SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid item key format."));
goto out;
}
cmd = (char *)zbx_malloc(cmd, cmd_alloc);
zbx_snprintf_alloc(&cmd, &cmd_alloc, &cmd_offset, "%s/%s", CONFIG_EXTERNALSCRIPTS, get_rkey(&request));
if (-1 == access(cmd, X_OK))
{
SET_MSG_RESULT(result, zbx_dsprintf(NULL, "%s: %s", cmd, zbx_strerror(errno)));
goto out;
}
for (i = 0; i < get_rparams_num(&request); i++)
{
const char *param;
char *param_esc;
param = get_rparam(&request, i);
param_esc = zbx_dyn_escape_shell_single_quote(param);
zbx_snprintf_alloc(&cmd, &cmd_alloc, &cmd_offset, " '%s'", param_esc);
zbx_free(param_esc);
}
if (SUCCEED == zbx_execute(cmd, &buf, error, sizeof(error), CONFIG_TIMEOUT, ZBX_EXIT_CODE_CHECKS_DISABLED))
{
zbx_rtrim(buf, ZBX_WHITESPACE);
set_result_type(result, ITEM_VALUE_TYPE_TEXT, buf);
zbx_free(buf);
ret = SUCCEED;
}
else
SET_MSG_RESULT(result, zbx_strdup(NULL, error));
out:
zbx_free(cmd);
free_request(&request);
zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __function_name, zbx_result_string(ret));
return ret;
}
|