/****************************************************************************** * * * Function: zbx_free_service_resources * * * * Purpose: free service resources allocated by main thread * * * ******************************************************************************/ void zbx_free_service_resources(void) { if (NULL != threads) { int i; #if !defined(_WINDOWS) sigset_t set; /* ignore SIGCHLD signals in order for zbx_sleep() to work */ sigemptyset(&set); sigaddset(&set, SIGCHLD); sigprocmask(SIG_BLOCK, &set, NULL); #else /* wait for threads to finish first. although listener threads will never end */ WaitForMultipleObjectsEx(threads_num, threads, TRUE, 1000, FALSE); #endif for (i = 0; i < threads_num; i++) { if (threads[i]) zbx_thread_kill(threads[i]); } for (i = 0; i < threads_num; i++) { if (threads[i]) zbx_thread_wait(threads[i]); threads[i] = ZBX_THREAD_HANDLE_NULL; } zbx_free(threads); } zabbix_log(LOG_LEVEL_INFORMATION, "Zabbix Agent stopped. Zabbix %s (revision %s).", ZABBIX_VERSION, ZABBIX_REVISION); #ifndef _WINDOWS unload_modules(); #endif zabbix_close_log(); free_metrics(); alias_list_free(); free_collector_data(); #ifdef _WINDOWS free_perf_collector(); zbx_co_uninitialize(); #endif }
int main(int argc, char **argv) { FILE *in; char in_line[MAX_BUFFER_LEN], hostname[MAX_STRING_LEN], key[MAX_STRING_LEN], key_value[MAX_BUFFER_LEN], clock[32]; int total_count = 0, succeed_count = 0, buffer_count = 0, read_more = 0, ret = SUCCEED; double last_send = 0; const char *p; zbx_thread_args_t thread_args; ZBX_THREAD_SENDVAL_ARGS sentdval_args; progname = get_program_name(argv[0]); parse_commandline(argc, argv); zbx_load_config(CONFIG_FILE); zabbix_open_log(LOG_TYPE_UNDEFINED, CONFIG_LOG_LEVEL, NULL); if (NULL == ZABBIX_SERVER) { zabbix_log(LOG_LEVEL_WARNING, "'Server' parameter required"); goto exit; } if (0 == ZABBIX_SERVER_PORT) ZABBIX_SERVER_PORT = ZBX_DEFAULT_SERVER_PORT; if (MIN_ZABBIX_PORT > ZABBIX_SERVER_PORT) { zabbix_log(LOG_LEVEL_WARNING, "Incorrect port number [%d]. Allowed [%d:%d]", (int)ZABBIX_SERVER_PORT, (int)MIN_ZABBIX_PORT, (int)MAX_ZABBIX_PORT); goto exit; } thread_args.thread_num = 0; thread_args.args = &sentdval_args; sentdval_args.server = ZABBIX_SERVER; sentdval_args.port = ZABBIX_SERVER_PORT; zbx_json_init(&sentdval_args.json, ZBX_JSON_STAT_BUF_LEN); zbx_json_addstring(&sentdval_args.json, ZBX_PROTO_TAG_REQUEST, ZBX_PROTO_VALUE_SENDER_DATA, ZBX_JSON_TYPE_STRING); zbx_json_addarray(&sentdval_args.json, ZBX_PROTO_TAG_DATA); if (INPUT_FILE) { if (0 == strcmp(INPUT_FILE, "-")) { in = stdin; if (1 == REAL_TIME) { /* set line buffering on stdin */ setvbuf(stdin, (char *)NULL, _IOLBF, 0); } } else if (NULL == (in = fopen(INPUT_FILE, "r")) ) { zabbix_log(LOG_LEVEL_WARNING, "cannot open [%s]: %s", INPUT_FILE, zbx_strerror(errno)); ret = FAIL; goto exit; } while (NULL != fgets(in_line, sizeof(in_line), in) && SUCCEED == ret) /* <hostname> <key> [<timestamp>] <value> */ { total_count++; /* also used as inputline */ zbx_rtrim(in_line, "\r\n"); p = in_line; if ('\0' == *p || NULL == (p = get_string(p, hostname, sizeof(hostname))) || '\0' == *hostname) { zabbix_log(LOG_LEVEL_WARNING, "[line %d] 'Hostname' required", total_count); continue; } if (0 == strcmp(hostname, "-")) { if (NULL == ZABBIX_HOSTNAME) { zabbix_log(LOG_LEVEL_WARNING, "[line %d] '-' encountered as 'Hostname', " "but no default hostname was specified", total_count); continue; } else zbx_strlcpy(hostname, ZABBIX_HOSTNAME, sizeof(hostname)); } if ('\0' == *p || NULL == (p = get_string(p, key, sizeof(key))) || '\0' == *key) { zabbix_log(LOG_LEVEL_WARNING, "[line %d] 'Key' required", total_count); continue; } if (1 == WITH_TIMESTAMPS) { if ('\0' == *p || NULL == (p = get_string(p, clock, sizeof(clock))) || '\0' == *clock) { zabbix_log(LOG_LEVEL_WARNING, "[line %d] 'Timestamp' required", total_count); continue; } } if ('\0' != *p && '"' != *p) zbx_strlcpy(key_value, p, sizeof(key_value)); else if ('\0' == *p || NULL == (p = get_string(p, key_value, sizeof(key_value))) || '\0' == *key_value) { zabbix_log(LOG_LEVEL_WARNING, "[line %d] 'Key value' required", total_count); continue; } zbx_rtrim(key_value, "\r\n"); zbx_json_addobject(&sentdval_args.json, NULL); zbx_json_addstring(&sentdval_args.json, ZBX_PROTO_TAG_HOST, hostname, ZBX_JSON_TYPE_STRING); zbx_json_addstring(&sentdval_args.json, ZBX_PROTO_TAG_KEY, key, ZBX_JSON_TYPE_STRING); zbx_json_addstring(&sentdval_args.json, ZBX_PROTO_TAG_VALUE, key_value, ZBX_JSON_TYPE_STRING); if (1 == WITH_TIMESTAMPS) zbx_json_adduint64(&sentdval_args.json, ZBX_PROTO_TAG_CLOCK, atoi(clock)); zbx_json_close(&sentdval_args.json); succeed_count++; buffer_count++; if (stdin == in && 1 == REAL_TIME) { /* if there is nothing on standard input after 1/5 seconds, we send what we have */ /* otherwise, we keep reading, but we should send data at least once per second */ struct timeval tv; fd_set read_set; tv.tv_sec = 0; tv.tv_usec = 200000; FD_ZERO(&read_set); FD_SET(0, &read_set); /* stdin is file descriptor 0 */ if (-1 == (read_more = select(1, &read_set, NULL, NULL, &tv))) { zabbix_log(LOG_LEVEL_WARNING, "select() failed: %s", zbx_strerror(errno)); } else if (1 <= read_more) { if (0 == last_send) last_send = zbx_time(); else if (zbx_time() - last_send >= 1) read_more = 0; } } if (VALUES_MAX == buffer_count || (stdin == in && 1 == REAL_TIME && 0 >= read_more)) { zbx_json_close(&sentdval_args.json); if (1 == WITH_TIMESTAMPS) zbx_json_adduint64(&sentdval_args.json, ZBX_PROTO_TAG_CLOCK, (int)time(NULL)); last_send = zbx_time(); ret = zbx_thread_wait(zbx_thread_start(send_value, &thread_args)); buffer_count = 0; zbx_json_clean(&sentdval_args.json); zbx_json_addstring(&sentdval_args.json, ZBX_PROTO_TAG_REQUEST, ZBX_PROTO_VALUE_SENDER_DATA, ZBX_JSON_TYPE_STRING); zbx_json_addarray(&sentdval_args.json, ZBX_PROTO_TAG_DATA); } } zbx_json_close(&sentdval_args.json); if (0 != buffer_count) { if (1 == WITH_TIMESTAMPS) zbx_json_adduint64(&sentdval_args.json, ZBX_PROTO_TAG_CLOCK, (int)time(NULL)); ret = zbx_thread_wait(zbx_thread_start(send_value, &thread_args)); } if (in != stdin) fclose(in); } else { total_count++; do /* try block simulation */ { if (NULL == ZABBIX_HOSTNAME) { zabbix_log(LOG_LEVEL_WARNING, "'Hostname' parameter required"); break; } if (NULL == ZABBIX_KEY) { zabbix_log(LOG_LEVEL_WARNING, "Key required"); break; } if (NULL == ZABBIX_KEY_VALUE) { zabbix_log(LOG_LEVEL_WARNING, "Key value required"); break; } zbx_json_addobject(&sentdval_args.json, NULL); zbx_json_addstring(&sentdval_args.json, ZBX_PROTO_TAG_HOST, ZABBIX_HOSTNAME, ZBX_JSON_TYPE_STRING); zbx_json_addstring(&sentdval_args.json, ZBX_PROTO_TAG_KEY, ZABBIX_KEY, ZBX_JSON_TYPE_STRING); zbx_json_addstring(&sentdval_args.json, ZBX_PROTO_TAG_VALUE, ZABBIX_KEY_VALUE, ZBX_JSON_TYPE_STRING); zbx_json_close(&sentdval_args.json); succeed_count++; ret = zbx_thread_wait(zbx_thread_start(send_value, &thread_args)); } while(0); /* try block simulation */ } zbx_json_free(&sentdval_args.json); if (SUCCEED == ret) printf("sent: %d; skipped: %d; total: %d\n", succeed_count, (total_count - succeed_count), total_count); else printf("Sending failed. Use option -vv for more detailed output.\n"); exit: zabbix_close_log(); return ret; }