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 "zbxjson.h"
22 #include "comms.h"
23 
24 #include "zabbix_sender.h"
25 
26 const char	*progname = NULL;
27 const char	title_message[] = "";
28 const char	*usage_message[] = {NULL};
29 
30 const char	*help_message[] = {NULL};
31 
32 unsigned char	program_type	= ZBX_PROGRAM_TYPE_SENDER;
33 
zabbix_sender_send_values(const char * address,unsigned short port,const char * source,const zabbix_sender_value_t * values,int count,char ** result)34 int	zabbix_sender_send_values(const char *address, unsigned short port, const char *source,
35 		const zabbix_sender_value_t *values, int count, char **result)
36 {
37 	zbx_socket_t	sock;
38 	int		ret, i;
39 	struct zbx_json	json;
40 
41 	if (1 > count)
42 	{
43 		if (NULL != result)
44 			*result = zbx_strdup(NULL, "values array must have at least one item");
45 
46 		return FAIL;
47 	}
48 
49 	zbx_json_init(&json, ZBX_JSON_STAT_BUF_LEN);
50 	zbx_json_addstring(&json, ZBX_PROTO_TAG_REQUEST, ZBX_PROTO_VALUE_SENDER_DATA, ZBX_JSON_TYPE_STRING);
51 	zbx_json_addarray(&json, ZBX_PROTO_TAG_DATA);
52 
53 	for (i = 0; i < count; i++)
54 	{
55 		zbx_json_addobject(&json, NULL);
56 		zbx_json_addstring(&json, ZBX_PROTO_TAG_HOST, values[i].host, ZBX_JSON_TYPE_STRING);
57 		zbx_json_addstring(&json, ZBX_PROTO_TAG_KEY, values[i].key, ZBX_JSON_TYPE_STRING);
58 		zbx_json_addstring(&json, ZBX_PROTO_TAG_VALUE, values[i].value, ZBX_JSON_TYPE_STRING);
59 		zbx_json_close(&json);
60 	}
61 	zbx_json_close(&json);
62 
63 	if (SUCCEED == (ret = zbx_tcp_connect(&sock, source, address, port, GET_SENDER_TIMEOUT,
64 			ZBX_TCP_SEC_UNENCRYPTED, NULL, NULL)))
65 	{
66 		if (SUCCEED == (ret = zbx_tcp_send(&sock, json.buffer)))
67 		{
68 			if (SUCCEED == (ret = zbx_tcp_recv(&sock)))
69 			{
70 				if (NULL != result)
71 					*result = zbx_strdup(NULL, sock.buffer);
72 			}
73 		}
74 
75 		zbx_tcp_close(&sock);
76 	}
77 
78 	if (FAIL == ret && NULL != result)
79 		*result = zbx_strdup(NULL, zbx_socket_strerror());
80 
81 	zbx_json_free(&json);
82 
83 	return ret;
84 }
85 
zabbix_sender_parse_result(const char * result,int * response,zabbix_sender_info_t * info)86 int	zabbix_sender_parse_result(const char *result, int *response, zabbix_sender_info_t *info)
87 {
88 	int			ret;
89 	struct zbx_json_parse	jp;
90 	char			value[MAX_STRING_LEN];
91 
92 	if (SUCCEED != (ret = zbx_json_open(result, &jp)))
93 		goto out;
94 
95 	if (SUCCEED != (ret = zbx_json_value_by_name(&jp, ZBX_PROTO_TAG_RESPONSE, value, sizeof(value))))
96 		goto out;
97 
98 	*response = (0 == strcmp(value, ZBX_PROTO_VALUE_SUCCESS)) ? 0 : -1;
99 
100 	if (NULL == info)
101 		goto out;
102 
103 	if (SUCCEED != zbx_json_value_by_name(&jp, ZBX_PROTO_TAG_INFO, value, sizeof(value)) ||
104 			3 != sscanf(value, "processed: %*d; failed: %d; total: %d; seconds spent: %lf",
105 				&info->failed, &info->total, &info->time_spent))
106 	{
107 		info->total = -1;
108 	}
109 out:
110 	return ret;
111 }
112 
zabbix_sender_free_result(void * ptr)113 void	zabbix_sender_free_result(void *ptr)
114 {
115 	if (NULL != ptr)
116 		free(ptr);
117 }
118