int connect_to_server(zbx_sock_t *sock, int timeout, int retry_interval) { int res, lastlogtime, now; zabbix_log(LOG_LEVEL_DEBUG, "In connect_to_server() [%s]:%d [timeout:%d]", CONFIG_SERVER, CONFIG_SERVER_PORT, timeout); if (FAIL == (res = zbx_tcp_connect(sock, CONFIG_SOURCE_IP, CONFIG_SERVER, CONFIG_SERVER_PORT, timeout))) { if (0 == retry_interval) { zabbix_log(LOG_LEVEL_WARNING, "Unable to connect to the server [%s]:%d [%s]", CONFIG_SERVER, CONFIG_SERVER_PORT, zbx_tcp_strerror()); } else { zabbix_log(LOG_LEVEL_WARNING, "Unable to connect to the server [%s]:%d [%s]. Will retry every %d second(s)", CONFIG_SERVER, CONFIG_SERVER_PORT, zbx_tcp_strerror(), retry_interval); lastlogtime = (int)time(NULL); while (FAIL == (res = zbx_tcp_connect(sock, CONFIG_SOURCE_IP, CONFIG_SERVER, CONFIG_SERVER_PORT, timeout))) { now = (int)time(NULL); if (60 <= now - lastlogtime) { zabbix_log(LOG_LEVEL_WARNING, "Still unable to connect..."); lastlogtime = now; } sleep(retry_interval); } zabbix_log(LOG_LEVEL_WARNING, "Connection restored."); } } return res; }
int zbx_module_unifi_proxy(AGENT_REQUEST *request, AGENT_RESULT *result) { int ret; int i, p, np; zbx_sock_t s; char send_buf[MAX_STRING_LEN]; *send_buf='\0'; np = request->nparam; if (9 < request->nparam) { /* set optional error message */ SET_MSG_RESULT(result, strdup("So much parameters given.")); return SYSINFO_RET_FAIL; } // make request string by concatenate all params for (i=0; i < np; i++) { strcat(send_buf, get_rparam(request, i)); p=strlen(send_buf); send_buf[p]=(i < (np-1)) ? ',' : '\n'; send_buf[p+1]='\0'; } // Connect to UniFi Proxy // item_timeout or (item_timeout-1) ? if (SUCCEED == (ret = zbx_tcp_connect(&s, CONFIG_SOURCE_IP, UNIFI_PROXY_SERVER, UNIFI_PROXY_PORT, CONFIG_TIMEOUT))) { // Send request if (SUCCEED == (ret = zbx_tcp_send_raw(&s, send_buf))) { // Recive answer from UniFi Proxy if (SUCCEED == (ret = zbx_tcp_recv(&s))) { zbx_rtrim(s.buffer, "\r\n"); SET_STR_RESULT(result, strdup(s.buffer)); } } zbx_tcp_close(&s); } if (FAIL == ret) { zabbix_log(LOG_LEVEL_DEBUG, "%s: communication error: %s", ZBX_MODULE_NAME, zbx_tcp_strerror()); SET_MSG_RESULT(result, strdup(zbx_tcp_strerror())); return SYSINFO_RET_FAIL; } return SYSINFO_RET_OK; }
/****************************************************************************** * * * Function: send_script * * * * Purpose: sending command to slave node * * * * Return value: SUCCEED - processed successfully * * FAIL - an error occurred * * * ******************************************************************************/ static int send_script(int nodeid, const char *data, char **result) { DB_RESULT db_result; DB_ROW db_row; int ret = FAIL; zbx_sock_t sock; char *answer; zabbix_log(LOG_LEVEL_DEBUG, "In send_script(nodeid:%d)", nodeid); db_result = DBselect( "select ip,port" " from nodes" " where nodeid=%d", nodeid); if (NULL != (db_row = DBfetch(db_result))) { if (SUCCEED == (ret = zbx_tcp_connect(&sock, CONFIG_SOURCE_IP, db_row[0], atoi(db_row[1]), CONFIG_TRAPPER_TIMEOUT))) { if (FAIL == (ret = zbx_tcp_send(&sock, data))) { *result = zbx_dsprintf(*result, "NODE %d: Error while sending data to Node [%d]: %s.", CONFIG_NODEID, nodeid, zbx_tcp_strerror()); goto exit_sock; } if (SUCCEED == (ret = zbx_tcp_recv(&sock, &answer))) *result = zbx_dsprintf(*result, "%s", answer); else *result = zbx_dsprintf(*result, "NODE %d: Error while receiving data from Node [%d]: %s.", CONFIG_NODEID, nodeid, zbx_tcp_strerror()); exit_sock: zbx_tcp_close(&sock); } else *result = zbx_dsprintf(*result, "NODE %d: Unable to connect to Node [%d]: %s.", CONFIG_NODEID, nodeid, zbx_tcp_strerror()); } else *result = zbx_dsprintf(*result, "NODE %d: Unknown Node ID [%d].", CONFIG_NODEID, nodeid); DBfree_result(db_result); return ret; }
static int check_telnet(const char *host, unsigned short port, int timeout, int *value_int) { const char *__function_name = "check_telnet"; zbx_sock_t s; #ifdef _WINDOWS u_long argp = 1; #else int flags; #endif *value_int = 0; if (SUCCEED == zbx_tcp_connect(&s, CONFIG_SOURCE_IP, host, port, timeout)) { #ifdef _WINDOWS ioctlsocket(s.socket, FIONBIO, &argp); /* non-zero value sets the socket to non-blocking */ #else flags = fcntl(s.socket, F_GETFL); if (0 == (flags & O_NONBLOCK)) fcntl(s.socket, F_SETFL, flags | O_NONBLOCK); #endif if (SUCCEED == telnet_test_login(s.socket)) *value_int = 1; else zabbix_log(LOG_LEVEL_DEBUG, "Telnet check error: no login prompt"); zbx_tcp_close(&s); } else zabbix_log(LOG_LEVEL_DEBUG, "%s error: %s", __function_name, zbx_tcp_strerror()); return SYSINFO_RET_OK; }
/* * Example: telnet.run["ls /"] */ static int telnet_run(DC_ITEM *item, AGENT_RESULT *result, const char *encoding) { const char *__function_name = "telnet_run"; zbx_sock_t s; int ret = NOTSUPPORTED, flags; zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name); if (FAIL == zbx_tcp_connect(&s, CONFIG_SOURCE_IP, item->interface.addr, item->interface.port, 0)) { SET_MSG_RESULT(result, zbx_dsprintf(NULL, "cannot connect to TELNET server: %s", zbx_tcp_strerror())); goto close; } flags = fcntl(s.socket, F_GETFL); if (0 == (flags & O_NONBLOCK)) fcntl(s.socket, F_SETFL, flags | O_NONBLOCK); if (FAIL == telnet_login(s.socket, item->username, item->password, result)) goto tcp_close; if (FAIL == telnet_execute(s.socket, item->params, result, encoding)) goto tcp_close; ret = SUCCEED; tcp_close: zbx_tcp_close(&s); close: zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __function_name, zbx_result_string(ret)); return ret; }
/****************************************************************************** * * * Function: ja_connect_to_port * * * * Purpose: connection to the specified port on the host * * * * Parameters: s (in) - socket identification information * * host (in) - host name * * inner_job_id (in) - inner job id * * txn (in) - transaction instruction * * * * Return value: SUCCEED - normal end * * FAIL - an error occurred * * * * Comments: * * * ******************************************************************************/ int ja_connect_to_port(zbx_sock_t * s, const char *host, const zbx_uint64_t inner_job_id, int txn) { int ret; char host_ip[128]; zbx_uint64_t hostid; int port; const char *__function_name = "ja_connect_to_port"; zabbix_log(LOG_LEVEL_DEBUG, "In %s() host: %s", __function_name, host); hostid = ja_host_getip(host, host_ip, inner_job_id, &port, txn); if (hostid == 0) { return FAIL; } port = ja_host_getport(hostid, 1); zabbix_log(LOG_LEVEL_DEBUG, "In %s() connect the host. source_ip: %s, host_ip: %s, port: %d, timeout: %d", __function_name, CONFIG_SOURCE_IP, host_ip, port, CONFIG_TIMEOUT); ret = zbx_tcp_connect(s, CONFIG_SOURCE_IP, host_ip, port, CONFIG_TIMEOUT); if (ret == FAIL) { ja_log("JACONNECT300001", 0, NULL, inner_job_id, __function_name, zbx_tcp_strerror(), host_ip, port, CONFIG_SOURCE_IP, CONFIG_TIMEOUT); } return ret; }
/****************************************************************************** * * * Function: send_proxyhistory * * * * Purpose: send history data to a Zabbix server * * * * Parameters: * * * * Return value: * * * * Author: Alexander Vladishev * * * * Comments: * * * ******************************************************************************/ static void send_proxyhistory(zbx_sock_t *sock) { const char *__function_name = "send_proxyhistory"; struct zbx_json j; zbx_uint64_t lastid; int records; zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name); zbx_json_init(&j, ZBX_JSON_STAT_BUF_LEN); zbx_json_addarray(&j, ZBX_PROTO_TAG_DATA); records = proxy_get_hist_data(&j, &lastid); zbx_json_close(&j); zbx_json_adduint64(&j, ZBX_PROTO_TAG_CLOCK, (int)time(NULL)); if (FAIL == zbx_tcp_send_to(sock, j.buffer, CONFIG_TIMEOUT)) zabbix_log(LOG_LEVEL_WARNING, "Error while sending availability of hosts. %s", zbx_tcp_strerror()); else if (SUCCEED == zbx_recv_response(sock, NULL, 0, CONFIG_TIMEOUT) && 0 != records) proxy_set_hist_lastid(lastid); zbx_json_free(&j); 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; }
int check_ntp(char *host, unsigned short port, int timeout, int *value_int) { zbx_sock_t s; int ret; char *buf = NULL, packet[NTP_PACKET_MIN]; ntp_data data; *value_int = 0; if (SUCCEED == (ret = zbx_tcp_connect(&s, CONFIG_SOURCE_IP, host, port, timeout))) { make_packet(&data); pack_ntp((unsigned char *)packet, sizeof(packet), &data); if (SUCCEED == (ret = zbx_tcp_send_raw(&s, packet))) { if (SUCCEED == (ret = zbx_tcp_recv(&s, &buf))) { unpack_ntp(&data, (unsigned char *)buf, (int)strlen(buf)); *value_int = (0 < data.receive ? (int)(data.receive - ZBX_JAN_1970_IN_SEC) : 0); } } zbx_tcp_close(&s); } if (FAIL == ret) zabbix_log(LOG_LEVEL_DEBUG, "NTP check error: %s", zbx_tcp_strerror()); return SYSINFO_RET_OK; }
int connect_to_node(int nodeid, zbx_sock_t *sock) { DB_RESULT result; DB_ROW row; unsigned short port; int res = FAIL; zabbix_log(LOG_LEVEL_DEBUG, "In connect_to_node(nodeid:%d)", nodeid); result = DBselect("select ip,port from nodes where nodeid=%d", nodeid); if (NULL != (row = DBfetch(result))) { port = (unsigned short)atoi(row[1]); if (SUCCEED == zbx_tcp_connect(sock, CONFIG_SOURCE_IP, row[0], port, 0)) res = SUCCEED; else zabbix_log(LOG_LEVEL_ERR, "NODE %d: Unable to connect to Node [%d] error: %s", CONFIG_NODEID, nodeid, zbx_tcp_strerror()); } else zabbix_log(LOG_LEVEL_ERR, "NODE %d: Node [%d] is unknown", CONFIG_NODEID, nodeid); DBfree_result(result); return res; }
static void process_listener(zbx_sock_t *s) { AGENT_RESULT result; char *command; char **value = NULL; int ret; if (SUCCEED == (ret = zbx_tcp_recv_to(s, &command, CONFIG_TIMEOUT))) { zbx_rtrim(command, "\r\n"); zabbix_log(LOG_LEVEL_DEBUG, "Requested [%s]", command); init_result(&result); process(command, 0, &result); if (NULL == (value = GET_TEXT_RESULT(&result))) value = GET_MSG_RESULT(&result); if (NULL != value) { zabbix_log(LOG_LEVEL_DEBUG, "Sending back [%s]", *value); ret = zbx_tcp_send_to(s, *value, CONFIG_TIMEOUT); } free_result(&result); } if (FAIL == ret) zabbix_log(LOG_LEVEL_DEBUG, "Process listener error: %s", zbx_tcp_strerror()); }
static int check_ssh(const char *host, unsigned short port, int timeout, int *value_int) { int ret; zbx_sock_t s; char send_buf[MAX_STRING_LEN], *recv_buf, *ssh_server, *ssh_proto; *value_int = 0; if (SUCCEED == (ret = zbx_tcp_connect(&s, CONFIG_SOURCE_IP, host, port, timeout))) { if (SUCCEED == (ret = zbx_tcp_recv(&s, &recv_buf))) { if (0 == strncmp(recv_buf, "SSH", 3)) { ssh_server = ssh_proto = recv_buf + 4; ssh_server += strspn(ssh_proto, "0123456789-. "); ssh_server[-1] = '\0'; zbx_snprintf(send_buf, sizeof(send_buf), "SSH-%s-%s\n", ssh_proto, "zabbix_agent"); *value_int = 1; } else zbx_snprintf(send_buf, sizeof(send_buf), "0\n"); ret = zbx_tcp_send_raw(&s, send_buf); } zbx_tcp_close(&s); } if (FAIL == ret) zabbix_log(LOG_LEVEL_DEBUG, "SSH check error: %s", zbx_tcp_strerror()); return SYSINFO_RET_OK; }
ZBX_THREAD_ENTRY(listener_thread, args) { int ret, local_request_failed = 0; zbx_sock_t s; assert(args); assert(((zbx_thread_args_t *)args)->args); process_type = ((zbx_thread_args_t *)args)->process_type; server_num = ((zbx_thread_args_t *)args)->server_num; process_num = ((zbx_thread_args_t *)args)->process_num; zabbix_log(LOG_LEVEL_INFORMATION, "%s #%d started [%s #%d]", get_daemon_type_string(daemon_type), server_num, get_process_type_string(process_type), process_num); memcpy(&s, (zbx_sock_t *)((zbx_thread_args_t *)args)->args, sizeof(zbx_sock_t)); zbx_free(args); while (ZBX_IS_RUNNING()) { zbx_setproctitle("listener #%d [waiting for connection]", process_num); if (SUCCEED == (ret = zbx_tcp_accept(&s))) { local_request_failed = 0; /* reset consecutive errors counter */ zbx_setproctitle("listener #%d [processing request]", process_num); if (SUCCEED == (ret = zbx_tcp_check_security(&s, CONFIG_HOSTS_ALLOWED, 0))) process_listener(&s); zbx_tcp_unaccept(&s); } if (SUCCEED == ret || EINTR == zbx_sock_last_error()) continue; zabbix_log(LOG_LEVEL_DEBUG, "failed to accept an incoming connection: %s", zbx_tcp_strerror()); if (local_request_failed++ > 1000) { zabbix_log(LOG_LEVEL_WARNING, "too many failures to accept an incoming connection"); local_request_failed = 0; } if (ZBX_IS_RUNNING()) zbx_sleep(1); } #ifdef _WINDOWS ZBX_DO_EXIT(); zbx_thread_exit(EXIT_SUCCESS); #endif }
ZBX_THREAD_ENTRY(listener_thread, args) { int ret, local_request_failed = 0; zbx_sock_t s; assert(args); assert(((zbx_thread_args_t *)args)->args); zabbix_log(LOG_LEVEL_WARNING, "agent #%d started [listener]", ((zbx_thread_args_t *)args)->thread_num); memcpy(&s, (zbx_sock_t *)((zbx_thread_args_t *)args)->args, sizeof(zbx_sock_t)); zbx_free(args); while (ZBX_IS_RUNNING()) { zbx_setproctitle("listener [waiting for connection]"); if (SUCCEED == (ret = zbx_tcp_accept(&s))) { local_request_failed = 0; /* reset consecutive errors counter */ zbx_setproctitle("listener [processing request]"); zabbix_log(LOG_LEVEL_DEBUG, "Processing request."); if (SUCCEED == (ret = zbx_tcp_check_security(&s, CONFIG_HOSTS_ALLOWED, 0))) process_listener(&s); zbx_tcp_unaccept(&s); } if (SUCCEED == ret) continue; zabbix_log(LOG_LEVEL_DEBUG, "Listener error: %s", zbx_tcp_strerror()); if (local_request_failed++ > 1000) { zabbix_log(LOG_LEVEL_WARNING, "Too many consecutive errors on accept() call."); local_request_failed = 0; } if (ZBX_IS_RUNNING()) zbx_sleep(1); } #ifdef _WINDOWS zabbix_log(LOG_LEVEL_INFORMATION, "zabbix_agentd listener stopped"); ZBX_DO_EXIT(); zbx_thread_exit(0); #endif }
static int send_data_to_server(zbx_sock_t *sock, const char *data) { int res; zabbix_log(LOG_LEVEL_DEBUG, "In send_data_to_server() [%s]", data); if (FAIL == (res = zbx_tcp_send(sock, data))) zabbix_log(LOG_LEVEL_ERR, "Error while sending data to the server [%s]", zbx_tcp_strerror()); return res; }
ZBX_THREAD_ENTRY(listener_thread, pSock) { int ret, local_request_failed = 0; zbx_sock_t s; assert(pSock); zbx_setproctitle("listener waiting for connection"); zabbix_log( LOG_LEVEL_INFORMATION, "zabbix_agentd listener started"); memcpy(&s, ((zbx_sock_t *)pSock), sizeof(zbx_sock_t)); while(ZBX_IS_RUNNING) { if( SUCCEED == (ret = zbx_tcp_accept(&s)) ) { local_request_failed = 0; /* Reset consecutive errors counter */ zbx_setproctitle("processing request"); zabbix_log(LOG_LEVEL_DEBUG, "Processing request."); if( SUCCEED == (ret = zbx_tcp_check_security(&s, CONFIG_HOSTS_ALLOWED, 0)) ) { process_listener(&s); } zbx_tcp_unaccept(&s); } zbx_setproctitle("listener waiting for connection"); if( SUCCEED == ret ) continue; zabbix_log(LOG_LEVEL_DEBUG, "Listener error: %s", zbx_tcp_strerror()); if (local_request_failed++ > 1000) { zabbix_log( LOG_LEVEL_WARNING, "Too many consecutive errors on accept() call."); local_request_failed = 0; } if(ZBX_IS_RUNNING) zbx_sleep(1); } zabbix_log( LOG_LEVEL_INFORMATION, "zabbix_agentd listener stopped"); ZBX_DO_EXIT(); zbx_tread_exit(0); }
static void process_listener(zbx_sock_t *s) { AGENT_RESULT result; char **value = NULL; int ret; if (SUCCEED == (ret = zbx_tcp_recv_to(s, CONFIG_TIMEOUT))) { zbx_rtrim(s->buffer, "\r\n"); zabbix_log(LOG_LEVEL_DEBUG, "Requested [%s]", s->buffer); init_result(&result); if (SUCCEED == process(s->buffer, PROCESS_WITH_ALIAS, &result)) { if (NULL != (value = GET_TEXT_RESULT(&result))) { zabbix_log(LOG_LEVEL_DEBUG, "Sending back [%s]", *value); ret = zbx_tcp_send_to(s, *value, CONFIG_TIMEOUT); } } else { value = GET_MSG_RESULT(&result); if (NULL != value) { static char *buffer = NULL; static size_t buffer_alloc = 256; size_t buffer_offset = 0; if (NULL == buffer) buffer = zbx_malloc(buffer, buffer_alloc); zbx_strncpy_alloc(&buffer, &buffer_alloc, &buffer_offset, ZBX_NOTSUPPORTED, ZBX_CONST_STRLEN(ZBX_NOTSUPPORTED)); buffer_offset++; zbx_strcpy_alloc(&buffer, &buffer_alloc, &buffer_offset, *value); ret = zbx_tcp_send_bytes_to(s, buffer, buffer_offset, CONFIG_TIMEOUT); } else ret = zbx_tcp_send_to(s, ZBX_NOTSUPPORTED, CONFIG_TIMEOUT); } free_result(&result); } if (FAIL == ret) zabbix_log(LOG_LEVEL_DEBUG, "Process listener error: %s", zbx_tcp_strerror()); }
/****************************************************************************** * * * Function: * * * * Purpose: * * * * Parameters: * * * * Return value: * * * * Comments: * * * ******************************************************************************/ void reply_jobnetstatusrq_response(zbx_sock_t * sock, int ret, JOBARG_JOBNET_INFO * ji, char *message) { struct zbx_json json; /*JSON Data */ zbx_json_init(&json, ZBX_JSON_STAT_BUF_LEN); zbx_json_addstring(&json, JA_PROTO_TAG_KIND, JA_PROTO_VALUE_JOBNETSTATUSRQ_RES, ZBX_JSON_TYPE_STRING); zbx_json_adduint64(&json, JA_PROTO_TAG_VERSION, JA_PROTO_VALUE_VERSION_1); /*data version */ zbx_json_addstring(&json, JA_PROTO_TAG_SERVERID, serverid, ZBX_JSON_TYPE_STRING); zbx_json_addobject(&json, JA_PROTO_TAG_DATA); zbx_json_adduint64(&json, JA_PROTO_TAG_RESULT, FAIL == ret ? 1 : 0); if (ret != FAIL) { zbx_json_addstring(&json, JA_PROTO_TAG_JOBNETID, ji->jobnetid, ZBX_JSON_TYPE_STRING); zbx_json_addstring(&json, JA_PROTO_TAG_JOBNETNAME, ji->jobnetname, ZBX_JSON_TYPE_STRING); zbx_json_adduint64(&json, JA_PROTO_TAG_SCHEDULEDTIME, ji->scheduled_time); zbx_json_adduint64(&json, JA_PROTO_TAG_STARTTIME, ji->start_time); zbx_json_adduint64(&json, JA_PROTO_TAG_ENDTIME, ji->end_time); zbx_json_adduint64(&json, JA_PROTO_TAG_JOBNETRUNTYPE, ji->jobnetruntype); zbx_json_adduint64(&json, JA_PROTO_TAG_JOBNETSTATUS, ji->jobnetstatus); zbx_json_adduint64(&json, JA_PROTO_TAG_JOBSTATUS, ji->jobstatus); zbx_json_addstring(&json, JA_PROTO_TAG_LASTEXITCD, ji->lastexitcd, ZBX_JSON_TYPE_STRING); zbx_json_addstring(&json, JA_PROTO_TAG_LASTSTDOUT, ji->laststdout, ZBX_JSON_TYPE_STRING); zbx_json_addstring(&json, JA_PROTO_TAG_LASTSTDERR, ji->laststderr, ZBX_JSON_TYPE_STRING); } else { zbx_json_addstring(&json, JA_PROTO_TAG_MESSAGE, message, ZBX_JSON_TYPE_STRING); } zbx_json_close(&json); zbx_json_close(&json); zabbix_log(LOG_LEVEL_DEBUG, "JSON before sending [%s]", json.buffer); if (FAIL == (ret = zbx_tcp_send_to(sock, json.buffer, CONFIG_TIMEOUT))) { zbx_free(ji->jobnetid); zbx_free(ji->jobnetname); ja_log("JATRAPPER200038", 0, NULL, 0, zbx_tcp_strerror()); ret = NETWORK_ERROR; } zbx_free(ji->jobnetid); zbx_free(ji->jobnetname); zbx_json_free(&json); }
static int recv_data_from_server(zbx_sock_t *sock) { int res; zabbix_log(LOG_LEVEL_DEBUG, "In recv_data_from_server()"); if (FAIL == (res = zbx_tcp_recv(sock))) zabbix_log(LOG_LEVEL_ERR, "Error while receiving answer from server [%s]", zbx_tcp_strerror()); else zabbix_log(LOG_LEVEL_DEBUG, "Received [%s] from server", sock->buffer); return res; }
/* * 0 - NOT OK * 1 - OK * */ int tcp_expect( const char *host, unsigned short port, const char *request, const char *expect, const char *sendtoclose, int *value_int ) { zbx_sock_t s; char *buf; int ret; assert(value_int); *value_int = 0; if (SUCCEED == (ret = zbx_tcp_connect(&s, CONFIG_SOURCE_IP, host, port, 3/*alarm!!!*/))) { if( NULL == request ) { *value_int = 1; } else if( SUCCEED == (ret = zbx_tcp_send_raw(&s, request)) ) { if( NULL == expect ) { *value_int = 1; } else if( SUCCEED == (ret = zbx_tcp_recv(&s, &buf)) ) { if( 0 == strncmp(buf, expect, strlen(expect)) ) { *value_int = 1; } } if(SUCCEED == ret && NULL != sendtoclose) { /* ret = (skip errors) */ zbx_tcp_send_raw(&s, sendtoclose); } } } zbx_tcp_close(&s); if( FAIL == ret ) { zabbix_log(LOG_LEVEL_DEBUG, "TCP expect error: %s", zbx_tcp_strerror()); } return SYSINFO_RET_OK; }
int recv_data_from_node(int nodeid, zbx_sock_t *sock, char **data) { int res; if (FAIL == (res = zbx_tcp_recv(sock, data))) { zabbix_log(LOG_LEVEL_ERR, "NODE %d: Error while receiving answer from Node [%d] error: %s", CONFIG_NODEID, nodeid, zbx_tcp_strerror()); } else zabbix_log(LOG_LEVEL_DEBUG, "NODE %d: Receiving [%s] from Node [%d]", CONFIG_NODEID, *data, nodeid); return res; }
int send_data_to_node(int nodeid, zbx_sock_t *sock, const char *data) { int res; if (FAIL == (res = zbx_tcp_send(sock, data))) { zabbix_log(LOG_LEVEL_ERR, "NODE %d: Error while sending data to Node [%d] error: %s", CONFIG_NODEID, nodeid, zbx_tcp_strerror()); } else zabbix_log(LOG_LEVEL_DEBUG, "NODE %d: Sending [%s] to Node [%d]", CONFIG_NODEID, data, nodeid); return res; }
/****************************************************************************** * * * Function: * * * * Purpose: * * * * Parameters: * * * * Return value: * * * * Comments: * * * ******************************************************************************/ int ja_tcp_recv_to(zbx_sock_t * s, ja_job_object * job, int timeout) { int ret, cnt=0; char *data; const char *__function_name = "ja_tcp_recv_to"; zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name); // 2016/01/07 Park.iggy ADD /* ORG ret = zbx_tcp_recv_to(s, &data, timeout); if (ret == FAIL) { zbx_snprintf(job->message, sizeof(job->message), "%s", zbx_tcp_strerror()); goto error; } */ while((ret = zbx_tcp_recv_to(s, &data, timeout+15)) == FAIL){ if(cnt >= 1) break; cnt++; } if (ret == FAIL) { zbx_snprintf(job->message, sizeof(job->message), "%s", zbx_tcp_strerror()); goto error; } //Park.iggy END zabbix_log(LOG_LEVEL_DEBUG, "In %s() %s", __function_name, data); if (strlen(data) == 0) { zbx_snprintf(job->message, sizeof(job->message), "received data is null"); goto error; } zbx_rtrim(data, "\r\n"); ret = ja_telegram_from(data, job); error: if (ret == FAIL) { zabbix_log(LOG_LEVEL_ERR, "In %s() message: %s", __function_name, job->message); } return ret; }
int tcp_expect(const char *host, unsigned short port, int timeout, const char *request, int (*validate_func)(const char *), const char *sendtoclose, int *value_int) { zbx_sock_t s; const char *buf; int net, val = ZBX_TCP_EXPECT_OK; *value_int = 0; if (SUCCEED != (net = zbx_tcp_connect(&s, CONFIG_SOURCE_IP, host, port, timeout))) goto out; if (NULL != request) net = zbx_tcp_send_raw(&s, request); if (NULL != validate_func && SUCCEED == net) { val = ZBX_TCP_EXPECT_FAIL; while (NULL != (buf = zbx_tcp_recv_line(&s))) { val = validate_func(buf); if (ZBX_TCP_EXPECT_OK == val) break; if (ZBX_TCP_EXPECT_FAIL == val) { zabbix_log(LOG_LEVEL_DEBUG, "TCP expect content error, received [%s]", buf); break; } } } if (NULL != sendtoclose && SUCCEED == net && ZBX_TCP_EXPECT_OK == val) zbx_tcp_send_raw(&s, sendtoclose); if (SUCCEED == net && ZBX_TCP_EXPECT_OK == val) *value_int = 1; zbx_tcp_close(&s); out: if (SUCCEED != net) zabbix_log(LOG_LEVEL_DEBUG, "TCP expect network error: %s", zbx_tcp_strerror()); return SYSINFO_RET_OK; }
int connect_to_server(zbx_sock_t *sock, int timeout) { int res; zabbix_log(LOG_LEVEL_DEBUG, "In connect_to_server() [%s]:%d [timeout:%d]", CONFIG_SERVER, CONFIG_SERVER_PORT, timeout); if (FAIL == (res = zbx_tcp_connect(sock, CONFIG_SOURCE_IP, CONFIG_SERVER, CONFIG_SERVER_PORT, timeout))) zabbix_log(LOG_LEVEL_ERR, "Unable connect to the server [%s]:%d [%s]", CONFIG_SERVER, CONFIG_SERVER_PORT, zbx_tcp_strerror()); return res; }
/****************************************************************************** * * * Function: send_proxyconfig * * * * Purpose: send configuration tables to the proxy from server * * (for active proxies) * * * * Author: Alexander Vladishev * * * ******************************************************************************/ 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 = NULL; struct zbx_json j; zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name); if (SUCCEED != get_active_proxy_id(jp, &proxy_hostid, host, &error)) { zbx_send_response(sock, FAIL, error, CONFIG_TIMEOUT); zabbix_log(LOG_LEVEL_WARNING, "proxy configuration request from active proxy on \"%s\" failed: %s", get_ip_by_socket(sock), 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 proxy configuration: %s", error); goto clean; } 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 (SUCCEED != zbx_tcp_send(sock, j.buffer)) zabbix_log(LOG_LEVEL_WARNING, "cannot send configuration: %s", zbx_tcp_strerror()); alarm(0); clean: zbx_json_free(&j); out: zbx_free(error); zabbix_log(LOG_LEVEL_DEBUG, "End of %s()", __function_name); }
int check_ntp(char *host, unsigned short port, int *value_int) { zbx_sock_t s; int ret; char *buf = NULL; ntp_data data; char packet[NTP_PACKET_MIN]; assert(value_int); *value_int = 0; if (SUCCEED == (ret = zbx_tcp_connect(&s, CONFIG_SOURCE_IP, host, port, 0))) { make_packet(&data); pack_ntp((unsigned char*)packet, sizeof(packet), &data); if( SUCCEED == (ret = zbx_tcp_send_raw(&s, packet)) ) { if( SUCCEED == (ret = zbx_tcp_recv(&s, &buf)) ) { unpack_ntp(&data, (unsigned char *)buf, (int)strlen(buf)); #if OFF /* local time */ *value_int = time(NULL); #else /* server time */ *value_int = (data.receive > 0) ? (int)(data.receive - ZBX_JAN_1970_IN_SEC) : 0; #endif } } } zbx_tcp_close(&s); if( FAIL == ret ) { zabbix_log(LOG_LEVEL_DEBUG, "NTP check error: %s", zbx_tcp_strerror()); } return SYSINFO_RET_OK; }
/****************************************************************************** * * * Function: send_areg_data * * * * Purpose: send auto-registration data from proxy to a server * * * * Author: Alexander Vladishev * * * ******************************************************************************/ void send_areg_data(zbx_sock_t *sock) { const char *__function_name = "send_areg_data"; struct zbx_json j; zbx_uint64_t lastid; int records; char *info = NULL, *error = NULL; zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name); zbx_json_init(&j, ZBX_JSON_STAT_BUF_LEN); zbx_json_addarray(&j, ZBX_PROTO_TAG_DATA); records = proxy_get_areg_data(&j, &lastid); zbx_json_close(&j); zbx_json_adduint64(&j, ZBX_PROTO_TAG_CLOCK, (int)time(NULL)); if (SUCCEED != zbx_tcp_send_to(sock, j.buffer, CONFIG_TIMEOUT)) { zabbix_log(LOG_LEVEL_WARNING, "error while sending auto-registration data to server: %s", zbx_tcp_strerror()); goto out; } if (SUCCEED != zbx_recv_response(sock, &info, CONFIG_TIMEOUT, &error)) { zabbix_log(LOG_LEVEL_WARNING, "sending auto-registration data to server: error:\"%s\", info:\"%s\"", ZBX_NULL2EMPTY_STR(error), ZBX_NULL2EMPTY_STR(info)); goto out; } if (0 != records) proxy_set_areg_lastid(lastid); out: zbx_json_free(&j); zbx_free(info); zbx_free(error); zabbix_log(LOG_LEVEL_DEBUG, "End of %s()", __function_name); }
/****************************************************************************** * * * Function: * * * * Purpose: * * * * Parameters: * * * * Return value: * * * * Comments: * * * ******************************************************************************/ int ja_tcp_send(zbx_sock_t * s, int timeout, json_object * json) { int ret; char *data; const char *__function_name = "ja_tcp_send"; if (json == NULL) return FAIL; data = (char *) json_object_to_json_string(json); zabbix_log(LOG_LEVEL_DEBUG, "In %s() data: %s", __function_name, data); ret = zbx_tcp_send_to(s, data, timeout); if (ret == FAIL) { zabbix_log(LOG_LEVEL_ERR, "In %s() error: %s", __function_name, zbx_tcp_strerror()); } return ret; }
/****************************************************************************** * * * Function: * * * * Purpose: * * * * Parameters: * * * * Return value: * * * * Comments: * * * ******************************************************************************/ static void jatraper_reply(zbx_sock_t * sock, ja_telegram_object * obj) { char *data; const char *__function_name = "jatraper_reply"; zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name); json_object_object_add(obj->response, JA_PROTO_TAG_KIND, json_object_new_string("response")); json_object_object_add(obj->response, JA_PROTO_TAG_VERSION, json_object_new_int(JA_PROTO_TELE_VERSION)); json_object_object_add(obj->response, JA_PROTO_TAG_SERVERID, json_object_new_string(serverid)); data = (char *) json_object_to_json_string(obj->response); zabbix_log(LOG_LEVEL_DEBUG, "In %s() %s", __function_name, data); if (zbx_tcp_send_to(sock, data, CONFIG_TIMEOUT) == FAIL) { ja_log("JATRAPPER200038", 0, NULL, 0, zbx_tcp_strerror()); } }