/****************************************************************************** * * * Function: recv_proxyhistory * * * * Purpose: processes the received values from active proxies * * * ******************************************************************************/ static void recv_proxyhistory(zbx_socket_t *sock, struct zbx_json_parse *jp, zbx_timespec_t *ts) { const char *__function_name = "recv_proxyhistory"; zbx_uint64_t proxy_hostid; char host[HOST_HOST_LEN_MAX], *error = NULL; int ret; zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name); if (SUCCEED != (ret = get_active_proxy_id(jp, &proxy_hostid, host, sock, &error))) { zabbix_log(LOG_LEVEL_WARNING, "cannot parse history data from active proxy at \"%s\": %s", sock->peer, error); goto out; } update_proxy_lastaccess(proxy_hostid); ret = process_hist_data(sock, jp, proxy_hostid, ts, &error); out: zbx_send_response(sock, ret, error, CONFIG_TIMEOUT); zbx_free(error); zabbix_log(LOG_LEVEL_DEBUG, "End of %s()", __function_name); }
/****************************************************************************** * * * Function: send_proxyconfig * * * * Purpose: send configuration tables to the proxy * * * * Parameters: * * * * Return value: SUCCEED - processed successfully * * FAIL - an error occurred * * * * Author: Aleksander Vladishev * * * * Comments: * * * ******************************************************************************/ int send_proxyconfig(zbx_sock_t *sock, struct zbx_json_parse *jp) { zbx_uint64_t proxy_hostid; struct zbx_json j; int res = FAIL; zabbix_log(LOG_LEVEL_DEBUG, "In send_proxyconfig()"); if (FAIL == get_proxy_id(jp, &proxy_hostid)) goto exit; update_proxy_lastaccess(proxy_hostid); zbx_json_init(&j, 512*1024); if (SUCCEED == (res = get_proxyconfig_data(proxy_hostid, &j))) { zabbix_log(LOG_LEVEL_WARNING, "Sending configuration data to proxy. Datalen %d", (int)j.buffer_size); zabbix_log(LOG_LEVEL_DEBUG, "%s", j.buffer); if (FAIL == (res = zbx_tcp_send(sock, j.buffer))) zabbix_log(LOG_LEVEL_WARNING, "Error while sending configuration. %s", zbx_tcp_strerror()); } zbx_json_free(&j); exit: return res; }
/****************************************************************************** * * * Function: process_proxy_values * * * * Purpose: process values sent by proxy servers * * * * Parameters: * * * * Return value: SUCCEED - processed successfully * * FAIL - an error occured * * * * Author: Aleksander Vladishev * * * * Comments: * * * ******************************************************************************/ static int process_proxy_values(zbx_sock_t *sock, struct zbx_json_parse *jp) { zbx_uint64_t proxy_hostid; zabbix_log(LOG_LEVEL_DEBUG, "In process_proxy_values()"); if (FAIL == get_proxy_id(jp, &proxy_hostid)) return FAIL; update_proxy_lastaccess(proxy_hostid); return process_new_values(sock, jp, proxy_hostid); }
/****************************************************************************** * * * Function: send_proxyconfig * * * * Purpose: send configuration tables to the proxy from server * * (for active proxies) * * * * Author: Alexander Vladishev * * * ******************************************************************************/ void send_proxyconfig(zbx_socket_t *sock, struct zbx_json_parse *jp) { const char *__function_name = "send_proxyconfig"; zbx_uint64_t proxy_hostid; char host[HOST_HOST_LEN_MAX], *error = NULL; struct zbx_json j; zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name); if (SUCCEED != get_active_proxy_id(jp, &proxy_hostid, host, sock, &error)) { zbx_send_response(sock, FAIL, error, CONFIG_TIMEOUT); zabbix_log(LOG_LEVEL_WARNING, "cannot parse proxy configuration data request from active proxy at" " \"%s\": %s", sock->peer, error); goto out; } update_proxy_lastaccess(proxy_hostid); zbx_json_init(&j, ZBX_JSON_STAT_BUF_LEN); if (SUCCEED != get_proxyconfig_data(proxy_hostid, &j, &error)) { zbx_send_response(sock, FAIL, error, CONFIG_TIMEOUT); zabbix_log(LOG_LEVEL_WARNING, "cannot collect configuration data for proxy \"%s\" at \"%s\": %s", host, sock->peer, error); goto clean; } zabbix_log(LOG_LEVEL_WARNING, "sending configuration data to proxy \"%s\" at \"%s\", datalen " ZBX_FS_SIZE_T, host, sock->peer, (zbx_fs_size_t)j.buffer_size); zabbix_log(LOG_LEVEL_DEBUG, "%s", j.buffer); if (SUCCEED != zbx_tcp_send_to(sock, j.buffer, CONFIG_TRAPPER_TIMEOUT)) { zabbix_log(LOG_LEVEL_WARNING, "cannot send configuration data to proxy \"%s\" at \"%s\": %s", host, sock->peer, zbx_socket_strerror()); } clean: zbx_json_free(&j); out: zbx_free(error); zabbix_log(LOG_LEVEL_DEBUG, "End of %s()", __function_name); }
/****************************************************************************** * * * Function: process_proxy_heartbeat * * * * Purpose: process heartbeat sent by proxy servers * * * * Parameters: * * * * Return value: SUCCEED - processed successfully * * FAIL - an error occured * * * * Author: Aleksander Vladishev * * * * Comments: * * * ******************************************************************************/ static int process_proxy_heartbeat(zbx_sock_t *sock, struct zbx_json_parse *jp) { zbx_uint64_t proxy_hostid; zabbix_log(LOG_LEVEL_DEBUG, "In process_proxy_heartbeat()"); if (FAIL == get_proxy_id(jp, &proxy_hostid)) return FAIL; update_proxy_lastaccess(proxy_hostid); if (send_result(sock, SUCCEED, NULL) != SUCCEED) { zabbix_log( LOG_LEVEL_WARNING, "Error sending result back"); zabbix_syslog("Trapper: error sending result back"); } return SUCCEED; }
/****************************************************************************** * * * Function: recv_proxy_heartbeat * * * * Purpose: process heartbeat sent by proxy servers * * * * Parameters: * * * * Return value: SUCCEED - processed successfully * * FAIL - an error occurred * * * * Author: Alexander Vladishev * * * * Comments: * * * ******************************************************************************/ static void recv_proxy_heartbeat(zbx_sock_t *sock, struct zbx_json_parse *jp) { const char *__function_name = "recv_proxy_heartbeat"; zbx_uint64_t proxy_hostid; char host[HOST_HOST_LEN_MAX], error[256]; zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name); if (FAIL == get_proxy_id(jp, &proxy_hostid, host, error, sizeof(error))) { zabbix_log(LOG_LEVEL_WARNING, "Heartbeat from active proxy on [%s] failed: %s", get_ip_by_socket(sock), error); return; } update_proxy_lastaccess(proxy_hostid); zbx_send_response(sock, SUCCEED, NULL, CONFIG_TIMEOUT); zabbix_log(LOG_LEVEL_DEBUG, "End of %s()", __function_name); }
/****************************************************************************** * * * Function: send_proxyconfig * * * * Purpose: send configuration tables to the proxy from server * * (for active proxies) * * * * Parameters: * * * * Return value: * * * * Author: Alexander Vladishev * * * * Comments: * * * ******************************************************************************/ void send_proxyconfig(zbx_sock_t *sock, struct zbx_json_parse *jp) { const char *__function_name = "send_proxyconfig"; zbx_uint64_t proxy_hostid; char host[HOST_HOST_LEN_MAX], error[256]; struct zbx_json j; zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name); if (FAIL == get_proxy_id(jp, &proxy_hostid, host, error, sizeof(error))) { zabbix_log(LOG_LEVEL_WARNING, "Proxy config request from active proxy on [%s] failed: %s", get_ip_by_socket(sock), error); return; } update_proxy_lastaccess(proxy_hostid); zbx_json_init(&j, ZBX_JSON_STAT_BUF_LEN); get_proxyconfig_data(proxy_hostid, &j); zabbix_log(LOG_LEVEL_WARNING, "Sending configuration data to proxy '%s'. Datalen " ZBX_FS_SIZE_T, host, (zbx_fs_size_t)j.buffer_size); zabbix_log(LOG_LEVEL_DEBUG, "%s", j.buffer); alarm(CONFIG_TIMEOUT); if (FAIL == zbx_tcp_send(sock, j.buffer)) zabbix_log(LOG_LEVEL_WARNING, "Error while sending configuration. %s", zbx_tcp_strerror()); alarm(0); zbx_json_free(&j); zabbix_log(LOG_LEVEL_DEBUG, "End of %s()", __function_name); }
/****************************************************************************** * * * Function: recv_proxyhistory * * * * Purpose: process values sent by proxy servers * * * * Parameters: * * * * Return value: * * * * Author: Alexander Vladishev * * * * Comments: * * * ******************************************************************************/ static void recv_proxyhistory(zbx_sock_t *sock, struct zbx_json_parse *jp) { const char *__function_name = "recv_proxyhistory"; zbx_uint64_t proxy_hostid; char host[HOST_HOST_LEN_MAX], info[128], error[256]; int ret; zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name); if (FAIL == (ret = get_proxy_id(jp, &proxy_hostid, host, error, sizeof(error)))) { zabbix_log(LOG_LEVEL_WARNING, "History data from active proxy on [%s] failed: %s", get_ip_by_socket(sock), error); goto exit; } update_proxy_lastaccess(proxy_hostid); ret = process_hist_data(sock, jp, proxy_hostid, info, sizeof(info)); exit: zbx_send_response(sock, ret, info, CONFIG_TIMEOUT); zabbix_log(LOG_LEVEL_DEBUG, "End of %s()", __function_name); }
/****************************************************************************** * * * Function: process_proxy * * * * Purpose: retrieve values of metrics from monitored hosts * * * * Parameters: * * * * Return value: * * * * Author: Alexei Vladishev * * * * Comments: * * * ******************************************************************************/ static int process_proxy(void) { const char *__function_name = "process_proxy"; DC_PROXY proxy; int num, i, ret; struct zbx_json j; struct zbx_json_parse jp, jp_data; zbx_socket_t s; char *answer = NULL, *port = NULL; time_t now; unsigned char update_nextcheck; zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name); if (0 == (num = DCconfig_get_proxypoller_hosts(&proxy, 1))) goto exit; now = time(NULL); zbx_json_init(&j, 512 * 1024); for (i = 0; i < num; i++) { update_nextcheck = 0; if (proxy.proxy_config_nextcheck <= now) update_nextcheck |= 0x01; if (proxy.proxy_data_nextcheck <= now) update_nextcheck |= 0x02; proxy.addr = proxy.addr_orig; port = zbx_strdup(port, proxy.port_orig); substitute_simple_macros(NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, &port, MACRO_TYPE_COMMON, NULL, 0); if (FAIL == is_ushort(port, &proxy.port)) { zabbix_log(LOG_LEVEL_ERR, "invalid proxy \"%s\" port: \"%s\"", proxy.host, port); goto network_error; } if (proxy.proxy_config_nextcheck <= now) { char *error = NULL; zbx_json_clean(&j); zbx_json_addstring(&j, ZBX_PROTO_TAG_REQUEST, ZBX_PROTO_VALUE_PROXY_CONFIG, ZBX_JSON_TYPE_STRING); zbx_json_addobject(&j, ZBX_PROTO_TAG_DATA); if (SUCCEED != (ret = get_proxyconfig_data(proxy.hostid, &j, &error))) { zabbix_log(LOG_LEVEL_ERR, "cannot collect configuration data for proxy \"%s\": %s", proxy.host, error); zbx_free(error); goto network_error; } if (SUCCEED == (ret = connect_to_proxy(&proxy, &s, CONFIG_TRAPPER_TIMEOUT))) { zabbix_log(LOG_LEVEL_WARNING, "sending configuration data to proxy \"%s\" at \"%s\"," " datalen " ZBX_FS_SIZE_T, proxy.host, get_ip_by_socket(&s), (zbx_fs_size_t)j.buffer_size); if (SUCCEED == (ret = send_data_to_proxy(&proxy, &s, j.buffer))) { char *error = NULL; if (SUCCEED != (ret = zbx_recv_response(&s, 0, &error))) { zabbix_log(LOG_LEVEL_WARNING, "cannot send configuration data to proxy" " \"%s\" at \"%s\": %s", proxy.host, get_ip_by_socket(&s), error); } zbx_free(error); } disconnect_proxy(&s); } if (SUCCEED != ret) goto network_error; } if (proxy.proxy_data_nextcheck <= now) { if (SUCCEED == get_data_from_proxy(&proxy, ZBX_PROTO_VALUE_HOST_AVAILABILITY, &answer)) { if (SUCCEED == zbx_json_open(answer, &jp)) process_host_availability(&jp); zbx_free(answer); } else goto network_error; retry_history: if (SUCCEED == get_data_from_proxy(&proxy, ZBX_PROTO_VALUE_HISTORY_DATA, &answer)) { if (SUCCEED == zbx_json_open(answer, &jp)) { process_hist_data(NULL, &jp, proxy.hostid, NULL, 0); if (SUCCEED == zbx_json_brackets_by_name(&jp, ZBX_PROTO_TAG_DATA, &jp_data)) { if (ZBX_MAX_HRECORDS <= zbx_json_count(&jp_data)) { zbx_free(answer); goto retry_history; } } } zbx_free(answer); } else goto network_error; retry_dhistory: if (SUCCEED == get_data_from_proxy(&proxy, ZBX_PROTO_VALUE_DISCOVERY_DATA, &answer)) { if (SUCCEED == zbx_json_open(answer, &jp)) { process_dhis_data(&jp); if (SUCCEED == zbx_json_brackets_by_name(&jp, ZBX_PROTO_TAG_DATA, &jp_data)) { if (ZBX_MAX_HRECORDS <= zbx_json_count(&jp_data)) { zbx_free(answer); goto retry_dhistory; } } } zbx_free(answer); } else goto network_error; retry_autoreg_host: if (SUCCEED == get_data_from_proxy(&proxy, ZBX_PROTO_VALUE_AUTO_REGISTRATION_DATA, &answer)) { if (SUCCEED == zbx_json_open(answer, &jp)) { process_areg_data(&jp, proxy.hostid); if (SUCCEED == zbx_json_brackets_by_name(&jp, ZBX_PROTO_TAG_DATA, &jp_data)) { if (ZBX_MAX_HRECORDS <= zbx_json_count(&jp_data)) { zbx_free(answer); goto retry_autoreg_host; } } } zbx_free(answer); } else goto network_error; } DBbegin(); update_proxy_lastaccess(proxy.hostid); DBcommit(); network_error: DCrequeue_proxy(proxy.hostid, update_nextcheck); } zbx_free(port); zbx_json_free(&j); exit: zabbix_log(LOG_LEVEL_DEBUG, "End of %s()", __function_name); return num; }