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 "comms.h"
22 #include "log.h"
23 #include "../../libs/zbxcrypto/tls_tcp_active.h"
24
25 #include "checks_agent.h"
26
27 #if !(defined(HAVE_GNUTLS) || defined(HAVE_OPENSSL))
28 extern unsigned char program_type;
29 #endif
30
31 /******************************************************************************
32 * *
33 * Function: get_value_agent *
34 * *
35 * Purpose: retrieve data from Zabbix agent *
36 * *
37 * Parameters: item - item we are interested in *
38 * *
39 * Return value: SUCCEED - data successfully retrieved and stored in result *
40 * and result_str (as string) *
41 * NETWORK_ERROR - network related error occurred *
42 * NOTSUPPORTED - item not supported by the agent *
43 * AGENT_ERROR - uncritical error on agent side occurred *
44 * FAIL - otherwise *
45 * *
46 * Author: Alexei Vladishev *
47 * *
48 * Comments: error will contain error message *
49 * *
50 ******************************************************************************/
get_value_agent(const DC_ITEM * item,AGENT_RESULT * result)51 int get_value_agent(const DC_ITEM *item, AGENT_RESULT *result)
52 {
53 zbx_socket_t s;
54 const char *tls_arg1, *tls_arg2;
55 int ret = SUCCEED;
56 ssize_t received_len;
57
58 zabbix_log(LOG_LEVEL_DEBUG, "In %s() host:'%s' addr:'%s' key:'%s' conn:'%s'", __func__, item->host.host,
59 item->interface.addr, item->key, zbx_tcp_connection_type_name(item->host.tls_connect));
60
61 switch (item->host.tls_connect)
62 {
63 case ZBX_TCP_SEC_UNENCRYPTED:
64 tls_arg1 = NULL;
65 tls_arg2 = NULL;
66 break;
67 #if defined(HAVE_GNUTLS) || defined(HAVE_OPENSSL)
68 case ZBX_TCP_SEC_TLS_CERT:
69 tls_arg1 = item->host.tls_issuer;
70 tls_arg2 = item->host.tls_subject;
71 break;
72 case ZBX_TCP_SEC_TLS_PSK:
73 tls_arg1 = item->host.tls_psk_identity;
74 tls_arg2 = item->host.tls_psk;
75 break;
76 #else
77 case ZBX_TCP_SEC_TLS_CERT:
78 case ZBX_TCP_SEC_TLS_PSK:
79 SET_MSG_RESULT(result, zbx_dsprintf(NULL, "A TLS connection is configured to be used with agent"
80 " but support for TLS was not compiled into %s.",
81 get_program_type_string(program_type)));
82 ret = CONFIG_ERROR;
83 goto out;
84 #endif
85 default:
86 THIS_SHOULD_NEVER_HAPPEN;
87 SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid TLS connection parameters."));
88 ret = CONFIG_ERROR;
89 goto out;
90 }
91
92 if (SUCCEED == zbx_tcp_connect(&s, CONFIG_SOURCE_IP, item->interface.addr, item->interface.port, 0,
93 item->host.tls_connect, tls_arg1, tls_arg2))
94 {
95 zabbix_log(LOG_LEVEL_DEBUG, "Sending [%s]", item->key);
96
97 if (SUCCEED != zbx_tcp_send(&s, item->key))
98 ret = NETWORK_ERROR;
99 else if (FAIL != (received_len = zbx_tcp_recv_ext(&s, 0, 0)))
100 ret = SUCCEED;
101 else if (SUCCEED == zbx_alarm_timed_out())
102 ret = TIMEOUT_ERROR;
103 else
104 ret = NETWORK_ERROR;
105 }
106 else
107 ret = NETWORK_ERROR;
108
109 if (SUCCEED == ret)
110 {
111 zabbix_log(LOG_LEVEL_DEBUG, "get value from agent result: '%s'", s.buffer);
112
113 if (0 == strcmp(s.buffer, ZBX_NOTSUPPORTED))
114 {
115 /* 'ZBX_NOTSUPPORTED\0<error message>' */
116 if (sizeof(ZBX_NOTSUPPORTED) < s.read_bytes)
117 SET_MSG_RESULT(result, zbx_dsprintf(NULL, "%s", s.buffer + sizeof(ZBX_NOTSUPPORTED)));
118 else
119 SET_MSG_RESULT(result, zbx_strdup(NULL, "Not supported by Zabbix Agent"));
120
121 ret = NOTSUPPORTED;
122 }
123 else if (0 == strcmp(s.buffer, ZBX_ERROR))
124 {
125 SET_MSG_RESULT(result, zbx_strdup(NULL, "Zabbix Agent non-critical error"));
126 ret = AGENT_ERROR;
127 }
128 else if (0 == received_len)
129 {
130 SET_MSG_RESULT(result, zbx_dsprintf(NULL, "Received empty response from Zabbix Agent at [%s]."
131 " Assuming that agent dropped connection because of access permissions.",
132 item->interface.addr));
133 ret = NETWORK_ERROR;
134 }
135 else
136 set_result_type(result, ITEM_VALUE_TYPE_TEXT, s.buffer);
137 }
138 else
139 SET_MSG_RESULT(result, zbx_dsprintf(NULL, "Get value from agent failed: %s", zbx_socket_strerror()));
140
141 zbx_tcp_close(&s);
142 out:
143 zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __func__, zbx_result_string(ret));
144
145 return ret;
146 }
147