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