/****************************************************************************** * * * Function: history_sender * * * * Purpose: * * * * Parameters: * * * * Return value: * * * * Author: Alexander Vladishev * * * * Comments: * * * ******************************************************************************/ static void history_sender(struct zbx_json *j, int *records, const char *tag, int (*f_get_data)(), void (*f_set_lastid)()) { const char *__function_name = "history_sender"; zbx_sock_t sock; zbx_uint64_t lastid; zbx_timespec_t ts; int ret = SUCCEED; zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name); zbx_json_clean(j); zbx_json_addstring(j, ZBX_PROTO_TAG_REQUEST, tag, ZBX_JSON_TYPE_STRING); zbx_json_addstring(j, ZBX_PROTO_TAG_HOST, CONFIG_HOSTNAME, ZBX_JSON_TYPE_STRING); zbx_json_addarray(j, ZBX_PROTO_TAG_DATA); *records = f_get_data(j, &lastid); zbx_json_close(j); if (*records > 0) { char *error = NULL; connect_to_server(&sock, 600, CONFIG_PROXYDATA_FREQUENCY); /* retry till have a connection */ zbx_timespec(&ts); zbx_json_adduint64(j, ZBX_PROTO_TAG_CLOCK, ts.sec); zbx_json_adduint64(j, ZBX_PROTO_TAG_NS, ts.ns); if (SUCCEED != (ret = put_data_to_server(&sock, j, &error))) { *records = 0; zabbix_log(LOG_LEVEL_WARNING, "sending data to server failed: %s", error); } zbx_free(error); disconnect_server(&sock); } if (SUCCEED == ret && 0 != lastid) { DBbegin(); f_set_lastid(lastid); DBcommit(); } zabbix_log(LOG_LEVEL_DEBUG, "End of %s()", __function_name); }
/****************************************************************************** * * * Function: send_proxyhistory * * * * Purpose: send history data to a Zabbix server * * * ******************************************************************************/ static void send_proxyhistory(zbx_socket_t *sock, zbx_timespec_t *ts) { const char *__function_name = "send_proxyhistory"; struct zbx_json j; zbx_uint64_t lastid; int records, ret = FAIL; char *error = NULL; zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name); if (SUCCEED != check_access_passive_proxy(sock, ZBX_DO_NOT_SEND_RESPONSE, "history data request")) { /* do not send any reply to server in this case as the server expects history data */ goto out1; } 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, ts->sec); zbx_json_adduint64(&j, ZBX_PROTO_TAG_NS, ts->ns); if (SUCCEED != zbx_tcp_send_to(sock, j.buffer, CONFIG_TIMEOUT)) { error = zbx_strdup(error, zbx_socket_strerror()); goto out; } if (SUCCEED != zbx_recv_response(sock, CONFIG_TIMEOUT, &error)) goto out; if (0 != records) proxy_set_hist_lastid(lastid); ret = SUCCEED; out: if (SUCCEED != ret) zabbix_log(LOG_LEVEL_WARNING, "cannot send history data to server at \"%s\": %s", sock->peer, error); zbx_json_free(&j); zbx_free(error); out1: zabbix_log(LOG_LEVEL_DEBUG, "End of %s()", __function_name); }
/****************************************************************************** * * * 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: * * * * 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); }
/****************************************************************************** * * * Function: * * * * Purpose: * * * * Parameters: * * * * Return value: * * * * Comments: * * * ******************************************************************************/ void reply_jobnetrun_response(zbx_sock_t * sock, int ret, 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_JOBNETRUN_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); 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))) { ja_log("JATRAPPER200038", 0, NULL, 0, zbx_tcp_strerror()); ret = NETWORK_ERROR; } zbx_json_free(&json); }
/****************************************************************************** * * * 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); }
int SYSTEM_CPU_DISCOVERY(AGENT_REQUEST *request, AGENT_RESULT *result) { int i; zbx_vector_uint64_t cpus; struct zbx_json json; zbx_vector_uint64_create(&cpus); if (SUCCEED != get_cpu_statuses(&cpus)) { SET_MSG_RESULT(result, zbx_strdup(NULL, "Collector in not started.")); zbx_vector_uint64_destroy(&cpus); return SYSINFO_RET_FAIL; } zbx_json_init(&json, ZBX_JSON_STAT_BUF_LEN); zbx_json_addarray(&json, ZBX_PROTO_TAG_DATA); for (i = 0; i < cpus.values_num; i++) { zbx_json_addobject(&json, NULL); zbx_json_adduint64(&json, "{#CPU.NUMBER}", i); zbx_json_addstring(&json, "{#CPU.STATUS}", (PERF_COUNTER_ACTIVE == cpus.values[i]) ? "online" : (PERF_COUNTER_INITIALIZED == cpus.values[i]) ? "unknown" : "offline", ZBX_JSON_TYPE_STRING); zbx_json_close(&json); } zbx_json_close(&json); SET_STR_RESULT(result, zbx_strdup(result->str, json.buffer)); zbx_json_free(&json); zbx_vector_uint64_destroy(&cpus); return SYSINFO_RET_OK; }
/****************************************************************************** * * * Function: queue_stats_export * * * * Purpose: export queue stats to JSON format * * * * Parameters: queue_stats - [IN] a hashset containing item stats * * id_name - [IN] the name of stats id field * * json - [OUT] the output JSON * * * ******************************************************************************/ static void queue_stats_export(zbx_hashset_t *queue_stats, const char *id_name, struct zbx_json *json) { zbx_hashset_iter_t iter; zbx_queue_stats_t *stats; zbx_json_addarray(json, ZBX_PROTO_TAG_DATA); zbx_hashset_iter_reset(queue_stats, &iter); while (NULL != (stats = zbx_hashset_iter_next(&iter))) { zbx_json_addobject(json, NULL); zbx_json_adduint64(json, id_name, stats->id); zbx_json_adduint64(json, "delay5", stats->delay5); zbx_json_adduint64(json, "delay10", stats->delay10); zbx_json_adduint64(json, "delay30", stats->delay30); zbx_json_adduint64(json, "delay60", stats->delay60); zbx_json_adduint64(json, "delay300", stats->delay300); zbx_json_adduint64(json, "delay600", stats->delay600); zbx_json_close(json); } zbx_json_close(json); }
/****************************************************************************** * * * Function: send_buffer * * * * Purpose: Send value stgored in the buffer to ZABBIX server * * * * Parameters: host - IP or Hostname of ZABBIX server * * port - port number * * * * Return value: returns SUCCEED on succesfull parsing, * * FAIL on other cases * * * * Author: Alexei Vladishev * * * * Comments: * * * ******************************************************************************/ static int send_buffer( const char *host, unsigned short port ) { zbx_sock_t s; char *buf = NULL; int ret = SUCCEED; struct zbx_json json; int i; static int lastsent = 0; int now; zabbix_log( LOG_LEVEL_DEBUG, "In send_buffer('%s','%d')", host, port); zabbix_log( LOG_LEVEL_DEBUG, "Values in the buffer %d Max %d", buffer.count, CONFIG_BUFFER_SIZE); now = (int)time(NULL); if(buffer.count < CONFIG_BUFFER_SIZE && now-lastsent < CONFIG_BUFFER_SEND) { zabbix_log( LOG_LEVEL_DEBUG, "Will not send now. Now %d lastsent %d < %d", now, lastsent, CONFIG_BUFFER_SEND); return ret; } if(buffer.count < 1) { return ret; } zbx_json_init(&json, ZBX_JSON_STAT_BUF_LEN); zbx_json_addstring(&json, ZBX_PROTO_TAG_REQUEST, ZBX_PROTO_VALUE_AGENT_DATA, ZBX_JSON_TYPE_STRING); zbx_json_addarray(&json, ZBX_PROTO_TAG_DATA); for(i=0;i<buffer.count;i++) { zbx_json_addobject(&json, NULL); zbx_json_addstring(&json, ZBX_PROTO_TAG_HOST, buffer.data[i].host, ZBX_JSON_TYPE_STRING); zbx_json_addstring(&json, ZBX_PROTO_TAG_KEY, buffer.data[i].key, ZBX_JSON_TYPE_STRING); zbx_json_addstring(&json, ZBX_PROTO_TAG_VALUE, buffer.data[i].value, ZBX_JSON_TYPE_STRING); if (buffer.data[i].lastlogsize) zbx_json_adduint64(&json, ZBX_PROTO_TAG_LOGLASTSIZE, buffer.data[i].lastlogsize); if (buffer.data[i].timestamp) zbx_json_adduint64(&json, ZBX_PROTO_TAG_LOGTIMESTAMP, buffer.data[i].timestamp); if (buffer.data[i].source) zbx_json_addstring(&json, ZBX_PROTO_TAG_LOGSOURCE, buffer.data[i].source, ZBX_JSON_TYPE_STRING); if (buffer.data[i].severity) zbx_json_adduint64(&json, ZBX_PROTO_TAG_LOGSEVERITY, buffer.data[i].severity); zbx_json_adduint64(&json, ZBX_PROTO_TAG_CLOCK, buffer.data[i].clock); zbx_json_close(&json); } zbx_json_close(&json); zbx_json_adduint64(&json, ZBX_PROTO_TAG_CLOCK, (int)time(NULL)); if (SUCCEED == (ret = zbx_tcp_connect(&s, CONFIG_SOURCE_IP, host, port, MIN(buffer.count*CONFIG_TIMEOUT, 60)))) { zabbix_log(LOG_LEVEL_DEBUG, "JSON before sending [%s]", json.buffer); ret = zbx_tcp_send(&s, json.buffer); if( SUCCEED == ret ) { if( SUCCEED == (ret = zbx_tcp_recv(&s, &buf)) ) { zabbix_log(LOG_LEVEL_DEBUG, "JSON back [%s]", buf); if( !buf || check_response(buf) != SUCCEED ) { zabbix_log(LOG_LEVEL_DEBUG, "NOT OK"); } else { zabbix_log(LOG_LEVEL_DEBUG, "OK"); } } else zabbix_log(LOG_LEVEL_DEBUG, "Send value error: [recv] %s", zbx_tcp_strerror()); } else zabbix_log(LOG_LEVEL_DEBUG, "Send value error: [send] %s", zbx_tcp_strerror()); zbx_tcp_close(&s); } else zabbix_log(LOG_LEVEL_DEBUG, "Send value error: [connect] %s", zbx_tcp_strerror()); zbx_json_free(&json); if(SUCCEED == ret) { /* free buffer */ for(i=0;i<buffer.count;i++) { if(buffer.data[i].host != NULL) zbx_free(buffer.data[i].host); if(buffer.data[i].key != NULL) zbx_free(buffer.data[i].key); if(buffer.data[i].value != NULL) zbx_free(buffer.data[i].value); if(buffer.data[i].source != NULL) zbx_free(buffer.data[i].source); } buffer.count = 0; } if(SUCCEED == ret) lastsent = now; return ret; }
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; }
void get_values_java(unsigned char request, DC_ITEM *items, AGENT_RESULT *results, int *errcodes, int num) { const char *__function_name = "get_values_java"; zbx_sock_t s; struct zbx_json json; char error[MAX_STRING_LEN]; char *buffer = NULL; int i, j, err = SUCCEED; zabbix_log(LOG_LEVEL_DEBUG, "In %s() host:'%s' addr:'%s' num:%d", __function_name, items[0].host.host, items[0].interface.addr, num); for (j = 0; j < num; j++) /* locate first supported item */ { if (SUCCEED == errcodes[j]) break; } if (j == num) /* all items already NOTSUPPORTED (with invalid key or port) */ goto out; zbx_json_init(&json, ZBX_JSON_STAT_BUF_LEN); if (NULL == CONFIG_JAVA_GATEWAY || '\0' == *CONFIG_JAVA_GATEWAY) { err = GATEWAY_ERROR; strscpy(error, "JavaGateway configuration parameter not set or empty"); goto exit; } if (ZBX_JAVA_GATEWAY_REQUEST_INTERNAL == request) { zbx_json_addstring(&json, ZBX_PROTO_TAG_REQUEST, ZBX_PROTO_VALUE_JAVA_GATEWAY_INTERNAL, ZBX_JSON_TYPE_STRING); } else if (ZBX_JAVA_GATEWAY_REQUEST_JMX == request) { for (i = j + 1; i < num; i++) { if (SUCCEED != errcodes[i]) continue; if (0 != strcmp(items[j].interface.addr, items[i].interface.addr) || items[j].interface.port != items[i].interface.port || 0 != strcmp(items[j].username, items[i].username) || 0 != strcmp(items[j].password, items[i].password)) { err = GATEWAY_ERROR; strscpy(error, "Java poller received items with different connection parameters"); goto exit; } } zbx_json_addstring(&json, ZBX_PROTO_TAG_REQUEST, ZBX_PROTO_VALUE_JAVA_GATEWAY_JMX, ZBX_JSON_TYPE_STRING); zbx_json_addstring(&json, ZBX_PROTO_TAG_CONN, items[j].interface.addr, ZBX_JSON_TYPE_STRING); zbx_json_adduint64(&json, ZBX_PROTO_TAG_PORT, items[j].interface.port); if ('\0' != *items[j].username) zbx_json_addstring(&json, ZBX_PROTO_TAG_USERNAME, items[j].username, ZBX_JSON_TYPE_STRING); if ('\0' != *items[j].password) zbx_json_addstring(&json, ZBX_PROTO_TAG_PASSWORD, items[j].password, ZBX_JSON_TYPE_STRING); } else assert(0); zbx_json_addarray(&json, ZBX_PROTO_TAG_KEYS); for (i = 0; i < num; i++) { if (SUCCEED != errcodes[i]) continue; zbx_json_addstring(&json, NULL, items[i].key, ZBX_JSON_TYPE_STRING); } zbx_json_close(&json); if (SUCCEED == (err = zbx_tcp_connect(&s, CONFIG_SOURCE_IP, CONFIG_JAVA_GATEWAY, CONFIG_JAVA_GATEWAY_PORT, CONFIG_TIMEOUT))) { zabbix_log(LOG_LEVEL_DEBUG, "JSON before sending [%s]", json.buffer); if (SUCCEED == (err = zbx_tcp_send(&s, json.buffer))) { if (SUCCEED == (err = zbx_tcp_recv(&s, &buffer))) { zabbix_log(LOG_LEVEL_DEBUG, "JSON back [%s]", buffer); err = parse_response(items, results, errcodes, num, buffer, error, sizeof(error)); } } zbx_tcp_close(&s); } zbx_json_free(&json); if (FAIL == err) { strscpy(error, zbx_tcp_strerror()); err = GATEWAY_ERROR; } exit: if (NETWORK_ERROR == err || GATEWAY_ERROR == err) { zabbix_log(LOG_LEVEL_DEBUG, "Getting Java values failed: %s", error); for (i = 0; i < num; i++) { if (SUCCEED != errcodes[i]) continue; if (!ISSET_MSG(&results[i])) { SET_MSG_RESULT(&results[i], zbx_strdup(NULL, error)); errcodes[i] = err; } } } out: zabbix_log(LOG_LEVEL_DEBUG, "End of %s()", __function_name); }
int SERVICE_DISCOVERY(AGENT_REQUEST *request, AGENT_RESULT *result) { ENUM_SERVICE_STATUS_PROCESS *ssp = NULL; QUERY_SERVICE_CONFIG *qsc = NULL; SERVICE_DESCRIPTION *scd = NULL; SC_HANDLE h_mgr; DWORD sz = 0, szn, i, k, services, resume_handle = 0; char *utf8; struct zbx_json j; if (NULL == (h_mgr = OpenSCManager(NULL, NULL, GENERIC_READ))) { SET_MSG_RESULT(result, zbx_strdup(NULL, "Cannot obtain system information.")); return SYSINFO_RET_FAIL; } zbx_json_init(&j, ZBX_JSON_STAT_BUF_LEN); zbx_json_addarray(&j, ZBX_PROTO_TAG_DATA); while (0 != EnumServicesStatusEx(h_mgr, SC_ENUM_PROCESS_INFO, SERVICE_WIN32, SERVICE_STATE_ALL, (LPBYTE)ssp, sz, &szn, &services, &resume_handle, NULL) || ERROR_MORE_DATA == GetLastError()) { for (i = 0; i < services; i++) { SC_HANDLE h_srv; DWORD current_state; if (NULL == (h_srv = OpenService(h_mgr, ssp[i].lpServiceName, SERVICE_QUERY_CONFIG))) continue; QueryServiceConfig(h_srv, NULL, 0, &sz); if (ERROR_INSUFFICIENT_BUFFER != GetLastError()) { zabbix_log(LOG_LEVEL_DEBUG, "cannot obtain configuration of service \"%s\": %s", ssp[i].lpServiceName, strerror_from_system(GetLastError())); goto next; } qsc = (QUERY_SERVICE_CONFIG *)zbx_malloc(qsc, sz); if (0 == QueryServiceConfig(h_srv, qsc, sz, &sz)) { zabbix_log(LOG_LEVEL_DEBUG, "cannot obtain configuration of service \"%s\": %s", ssp[i].lpServiceName, strerror_from_system(GetLastError())); goto next; } QueryServiceConfig2(h_srv, SERVICE_CONFIG_DESCRIPTION, NULL, 0, &sz); if (ERROR_INSUFFICIENT_BUFFER != GetLastError()) { zabbix_log(LOG_LEVEL_DEBUG, "cannot obtain description of service \"%s\": %s", ssp[i].lpServiceName, strerror_from_system(GetLastError())); goto next; } scd = (SERVICE_DESCRIPTION *)zbx_malloc(scd, sz); if (0 == QueryServiceConfig2(h_srv, SERVICE_CONFIG_DESCRIPTION, (LPBYTE)scd, sz, &sz)) { zabbix_log(LOG_LEVEL_DEBUG, "cannot obtain description of service \"%s\": %s", ssp[i].lpServiceName, strerror_from_system(GetLastError())); goto next; } zbx_json_addobject(&j, NULL); utf8 = zbx_unicode_to_utf8(ssp[i].lpServiceName); zbx_json_addstring(&j, "{#SERVICE.NAME}", utf8, ZBX_JSON_TYPE_STRING); zbx_free(utf8); utf8 = zbx_unicode_to_utf8(ssp[i].lpDisplayName); zbx_json_addstring(&j, "{#SERVICE.DISPLAYNAME}", utf8, ZBX_JSON_TYPE_STRING); zbx_free(utf8); if (NULL != scd->lpDescription) { utf8 = zbx_unicode_to_utf8(scd->lpDescription); zbx_json_addstring(&j, "{#SERVICE.DESCRIPTION}", utf8, ZBX_JSON_TYPE_STRING); zbx_free(utf8); } else zbx_json_addstring(&j, "{#SERVICE.DESCRIPTION}", "", ZBX_JSON_TYPE_STRING); current_state = ssp[i].ServiceStatusProcess.dwCurrentState; for (k = 0; k < ARRSIZE(service_states) && current_state != service_states[k]; k++) ; zbx_json_adduint64(&j, "{#SERVICE.STATE}", k); zbx_json_addstring(&j, "{#SERVICE.STATENAME}", get_state_string(current_state), ZBX_JSON_TYPE_STRING); utf8 = zbx_unicode_to_utf8(qsc->lpBinaryPathName); zbx_json_addstring(&j, "{#SERVICE.PATH}", utf8, ZBX_JSON_TYPE_STRING); zbx_free(utf8); utf8 = zbx_unicode_to_utf8(qsc->lpServiceStartName); zbx_json_addstring(&j, "{#SERVICE.USER}", utf8, ZBX_JSON_TYPE_STRING); zbx_free(utf8); if (SERVICE_AUTO_START == qsc->dwStartType) { if (SUCCEED == check_delayed_start(h_srv)) { zbx_json_adduint64(&j, "{#SERVICE.STARTUP}", 1); zbx_json_addstring(&j, "{#SERVICE.STARTUPNAME}", "automatic delayed", ZBX_JSON_TYPE_STRING); } else { zbx_json_adduint64(&j, "{#SERVICE.STARTUP}", 0); zbx_json_addstring(&j, "{#SERVICE.STARTUPNAME}", "automatic", ZBX_JSON_TYPE_STRING); } } else { for (k = 2; k < ARRSIZE(start_types) && qsc->dwStartType != start_types[k]; k++) ; zbx_json_adduint64(&j, "{#SERVICE.STARTUP}", k); zbx_json_addstring(&j, "{#SERVICE.STARTUPNAME}", get_startup_string(qsc->dwStartType), ZBX_JSON_TYPE_STRING); } zbx_json_close(&j); next: zbx_free(scd); zbx_free(qsc); CloseServiceHandle(h_srv); } if (0 == szn) break; if (NULL == ssp) { sz = szn; ssp = (ENUM_SERVICE_STATUS_PROCESS *)zbx_malloc(ssp, sz); } } zbx_free(ssp); CloseServiceHandle(h_mgr); zbx_json_close(&j); SET_STR_RESULT(result, zbx_strdup(NULL, j.buffer)); zbx_json_free(&j); return SYSINFO_RET_OK; }
/****************************************************************************** * * * Function: send_list_of_active_checks_json * * * * Purpose: send list of active checks to the host * * * * Parameters: sock - open socket of server-agent connection * * json - request buffer * * * * Return value: SUCCEED - list of active checks sent successfully * * FAIL - an error occurred * * * * Author: Alexander Vladishev * * * * Comments: * * * ******************************************************************************/ int send_list_of_active_checks_json(zbx_socket_t *sock, struct zbx_json_parse *jp) { const char *__function_name = "send_list_of_active_checks_json"; char host[HOST_HOST_LEN_MAX], tmp[MAX_STRING_LEN], ip[INTERFACE_IP_LEN_MAX], error[MAX_STRING_LEN], *host_metadata = NULL; struct zbx_json json; int ret = FAIL, i; zbx_uint64_t hostid; size_t host_metadata_alloc = 1; /* for at least NUL-termination char */ unsigned short port; zbx_vector_uint64_t itemids; zbx_vector_ptr_t regexps; zbx_vector_str_t names; zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name); zbx_vector_ptr_create(®exps); zbx_vector_str_create(&names); if (FAIL == zbx_json_value_by_name(jp, ZBX_PROTO_TAG_HOST, host, sizeof(host))) { zbx_snprintf(error, MAX_STRING_LEN, "%s", zbx_json_strerror()); goto error; } host_metadata = zbx_malloc(host_metadata, host_metadata_alloc); if (FAIL == zbx_json_value_by_name_dyn(jp, ZBX_PROTO_TAG_HOST_METADATA, &host_metadata, &host_metadata_alloc)) { *host_metadata = '\0'; } if (FAIL == zbx_json_value_by_name(jp, ZBX_PROTO_TAG_IP, ip, sizeof(ip))) strscpy(ip, sock->peer); if (FAIL == zbx_json_value_by_name(jp, ZBX_PROTO_TAG_PORT, tmp, sizeof(tmp))) *tmp = '\0'; if (FAIL == is_ushort(tmp, &port)) port = ZBX_DEFAULT_AGENT_PORT; if (FAIL == get_hostid_by_host(sock, host, ip, port, host_metadata, &hostid, error)) goto error; zbx_vector_uint64_create(&itemids); get_list_of_active_checks(hostid, &itemids); zbx_json_init(&json, ZBX_JSON_STAT_BUF_LEN); zbx_json_addstring(&json, ZBX_PROTO_TAG_RESPONSE, ZBX_PROTO_VALUE_SUCCESS, ZBX_JSON_TYPE_STRING); zbx_json_addarray(&json, ZBX_PROTO_TAG_DATA); if (0 != itemids.values_num) { DC_ITEM *dc_items; int *errcodes, now; zbx_config_t cfg; dc_items = zbx_malloc(NULL, sizeof(DC_ITEM) * itemids.values_num); errcodes = zbx_malloc(NULL, sizeof(int) * itemids.values_num); DCconfig_get_items_by_itemids(dc_items, itemids.values, errcodes, itemids.values_num); zbx_config_get(&cfg, ZBX_CONFIG_FLAGS_REFRESH_UNSUPPORTED); now = time(NULL); for (i = 0; i < itemids.values_num; i++) { if (SUCCEED != errcodes[i]) { zabbix_log(LOG_LEVEL_DEBUG, "%s() Item [" ZBX_FS_UI64 "] was not found in the" " server cache. Not sending now.", __function_name, itemids.values[i]); continue; } if (ITEM_STATUS_ACTIVE != dc_items[i].status) continue; if (HOST_STATUS_MONITORED != dc_items[i].host.status) continue; if (ITEM_STATE_NOTSUPPORTED == dc_items[i].state) { if (0 == cfg.refresh_unsupported) continue; if (dc_items[i].lastclock + cfg.refresh_unsupported > now) continue; } dc_items[i].key = zbx_strdup(dc_items[i].key, dc_items[i].key_orig); substitute_key_macros(&dc_items[i].key, NULL, &dc_items[i], NULL, MACRO_TYPE_ITEM_KEY, NULL, 0); zbx_json_addobject(&json, NULL); zbx_json_addstring(&json, ZBX_PROTO_TAG_KEY, dc_items[i].key, ZBX_JSON_TYPE_STRING); if (0 != strcmp(dc_items[i].key, dc_items[i].key_orig)) { zbx_json_addstring(&json, ZBX_PROTO_TAG_KEY_ORIG, dc_items[i].key_orig, ZBX_JSON_TYPE_STRING); } zbx_json_adduint64(&json, ZBX_PROTO_TAG_DELAY, dc_items[i].delay); /* The agent expects ALWAYS to have lastlogsize and mtime tags. */ /* Removing those would cause older agents to fail. */ zbx_json_adduint64(&json, ZBX_PROTO_TAG_LASTLOGSIZE, dc_items[i].lastlogsize); zbx_json_adduint64(&json, ZBX_PROTO_TAG_MTIME, dc_items[i].mtime); zbx_json_close(&json); zbx_itemkey_extract_global_regexps(dc_items[i].key, &names); zbx_free(dc_items[i].key); } zbx_config_clean(&cfg); DCconfig_clean_items(dc_items, errcodes, itemids.values_num); zbx_free(errcodes); zbx_free(dc_items); } zbx_vector_uint64_destroy(&itemids); zbx_json_close(&json); DCget_expressions_by_names(®exps, (const char * const *)names.values, names.values_num); if (0 < regexps.values_num) { char buffer[32]; zbx_json_addarray(&json, ZBX_PROTO_TAG_REGEXP); for (i = 0; i < regexps.values_num; i++) { zbx_expression_t *regexp = regexps.values[i]; zbx_json_addobject(&json, NULL); zbx_json_addstring(&json, "name", regexp->name, ZBX_JSON_TYPE_STRING); zbx_json_addstring(&json, "expression", regexp->expression, ZBX_JSON_TYPE_STRING); zbx_snprintf(buffer, sizeof(buffer), "%d", regexp->expression_type); zbx_json_addstring(&json, "expression_type", buffer, ZBX_JSON_TYPE_INT); zbx_snprintf(buffer, sizeof(buffer), "%c", regexp->exp_delimiter); zbx_json_addstring(&json, "exp_delimiter", buffer, ZBX_JSON_TYPE_STRING); zbx_snprintf(buffer, sizeof(buffer), "%d", regexp->case_sensitive); zbx_json_addstring(&json, "case_sensitive", buffer, ZBX_JSON_TYPE_INT); zbx_json_close(&json); } zbx_json_close(&json); } zabbix_log(LOG_LEVEL_DEBUG, "%s() sending [%s]", __function_name, json.buffer); zbx_alarm_on(CONFIG_TIMEOUT); if (SUCCEED != zbx_tcp_send(sock, json.buffer)) strscpy(error, zbx_socket_strerror()); else ret = SUCCEED; zbx_alarm_off(); zbx_json_free(&json); goto out; error: zabbix_log(LOG_LEVEL_WARNING, "cannot send list of active checks to \"%s\": %s", sock->peer, error); zbx_json_init(&json, ZBX_JSON_STAT_BUF_LEN); zbx_json_addstring(&json, ZBX_PROTO_TAG_RESPONSE, ZBX_PROTO_VALUE_FAILED, ZBX_JSON_TYPE_STRING); zbx_json_addstring(&json, ZBX_PROTO_TAG_INFO, error, ZBX_JSON_TYPE_STRING); zabbix_log(LOG_LEVEL_DEBUG, "%s() sending [%s]", __function_name, json.buffer); ret = zbx_tcp_send(sock, json.buffer); zbx_json_free(&json); out: for (i = 0; i < names.values_num; i++) zbx_free(names.values[i]); zbx_vector_str_destroy(&names); zbx_regexp_clean_expressions(®exps); zbx_vector_ptr_destroy(®exps); zbx_free(host_metadata); zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __function_name, zbx_result_string(ret)); return ret; }
/****************************************************************************** * * * Function: send_buffer * * * * Purpose: Send value stored in the buffer to Zabbix server * * * * Parameters: host - IP or Hostname of Zabbix server * * port - port number * * * * Return value: returns SUCCEED on successful parsing, * * FAIL on other cases * * * * Author: Alexei Vladishev * * * * Comments: * * * ******************************************************************************/ static int send_buffer(const char *host, unsigned short port) { const char *__function_name = "send_buffer"; struct zbx_json json; ZBX_ACTIVE_BUFFER_ELEMENT *el; zbx_sock_t s; char *buf = NULL; int ret = SUCCEED, i, now; zbx_timespec_t ts; const char *err_send_step = ""; zabbix_log(LOG_LEVEL_DEBUG, "In %s() host:'%s' port:%d values:%d/%d", __function_name, host, port, buffer.count, CONFIG_BUFFER_SIZE); if (0 == buffer.count) goto ret; now = (int)time(NULL); if (CONFIG_BUFFER_SIZE / 2 > buffer.pcount && CONFIG_BUFFER_SIZE > buffer.count && CONFIG_BUFFER_SEND > now - buffer.lastsent) { zabbix_log(LOG_LEVEL_DEBUG, "Will not send now. Now %d lastsent %d < %d", now, buffer.lastsent, CONFIG_BUFFER_SEND); goto ret; } zbx_json_init(&json, ZBX_JSON_STAT_BUF_LEN); zbx_json_addstring(&json, ZBX_PROTO_TAG_REQUEST, ZBX_PROTO_VALUE_AGENT_DATA, ZBX_JSON_TYPE_STRING); zbx_json_addarray(&json, ZBX_PROTO_TAG_DATA); for (i = 0; i < buffer.count; i++) { el = &buffer.data[i]; zbx_json_addobject(&json, NULL); zbx_json_addstring(&json, ZBX_PROTO_TAG_HOST, el->host, ZBX_JSON_TYPE_STRING); zbx_json_addstring(&json, ZBX_PROTO_TAG_KEY, el->key, ZBX_JSON_TYPE_STRING); zbx_json_addstring(&json, ZBX_PROTO_TAG_VALUE, el->value, ZBX_JSON_TYPE_STRING); if (0 != el->lastlogsize) zbx_json_adduint64(&json, ZBX_PROTO_TAG_LOGLASTSIZE, el->lastlogsize); if (el->mtime) zbx_json_adduint64(&json, ZBX_PROTO_TAG_MTIME, el->mtime); if (el->timestamp) zbx_json_adduint64(&json, ZBX_PROTO_TAG_LOGTIMESTAMP, el->timestamp); if (el->source) zbx_json_addstring(&json, ZBX_PROTO_TAG_LOGSOURCE, el->source, ZBX_JSON_TYPE_STRING); if (el->severity) zbx_json_adduint64(&json, ZBX_PROTO_TAG_LOGSEVERITY, el->severity); if (el->logeventid) zbx_json_adduint64(&json, ZBX_PROTO_TAG_LOGEVENTID, el->logeventid); zbx_json_adduint64(&json, ZBX_PROTO_TAG_CLOCK, el->ts.sec); zbx_json_adduint64(&json, ZBX_PROTO_TAG_NS, el->ts.ns); zbx_json_close(&json); } zbx_json_close(&json); zbx_timespec(&ts); zbx_json_adduint64(&json, ZBX_PROTO_TAG_CLOCK, ts.sec); zbx_json_adduint64(&json, ZBX_PROTO_TAG_NS, ts.ns); if (SUCCEED == (ret = zbx_tcp_connect(&s, CONFIG_SOURCE_IP, host, port, MIN(buffer.count * CONFIG_TIMEOUT, 60)))) { zabbix_log(LOG_LEVEL_DEBUG, "JSON before sending [%s]", json.buffer); if (SUCCEED == (ret = zbx_tcp_send(&s, json.buffer))) { if (SUCCEED == zbx_tcp_recv(&s, &buf)) { zabbix_log(LOG_LEVEL_DEBUG, "JSON back [%s]", buf); if (NULL == buf || SUCCEED != check_response(buf)) zabbix_log(LOG_LEVEL_DEBUG, "NOT OK"); else zabbix_log(LOG_LEVEL_DEBUG, "OK"); } else err_send_step = "[recv] "; } else err_send_step = "[send] "; zbx_tcp_close(&s); } else err_send_step = "[connect] "; zbx_json_free(&json); if (SUCCEED == ret) { /* free buffer */ for (i = 0; i < buffer.count; i++) { el = &buffer.data[i]; zbx_free(el->host); zbx_free(el->key); zbx_free(el->value); zbx_free(el->source); } buffer.count = 0; buffer.pcount = 0; buffer.lastsent = now; if (0 != buffer.first_error) { zabbix_log(LOG_LEVEL_WARNING, "active check data upload to [%s:%hu] is working again", host, port); buffer.first_error = 0; } } else { if (0 == buffer.first_error) { zabbix_log(LOG_LEVEL_WARNING, "active check data upload to [%s:%hu] started to fail (%s%s)", host, port, err_send_step, zbx_tcp_strerror()); buffer.first_error = now; } zabbix_log(LOG_LEVEL_DEBUG, "send value error: %s %s", err_send_step, zbx_tcp_strerror()); } ret: zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __function_name, zbx_result_string(ret)); return ret; }
/****************************************************************************** * * * Function: refresh_active_checks * * * * Purpose: Retrieve from Zabbix server list of active checks * * * * Parameters: host - IP or Hostname of Zabbix server * * port - port of Zabbix server * * * * Return value: returns SUCCEED on successful parsing, * * FAIL on other cases * * * * Author: Eugene Grigorjev, Alexei Vladishev (new json protocol) * * * * Comments: * * * ******************************************************************************/ static int refresh_active_checks(const char *host, unsigned short port) { const char *__function_name = "refresh_active_checks"; zbx_sock_t s; char *buf; int ret; struct zbx_json json; static int last_ret = SUCCEED; zabbix_log(LOG_LEVEL_DEBUG, "In %s() host:'%s' port:%hu", __function_name, host, port); zbx_json_init(&json, ZBX_JSON_STAT_BUF_LEN); zbx_json_addstring(&json, ZBX_PROTO_TAG_REQUEST, ZBX_PROTO_VALUE_GET_ACTIVE_CHECKS, ZBX_JSON_TYPE_STRING); zbx_json_addstring(&json, ZBX_PROTO_TAG_HOST, CONFIG_HOSTNAME, ZBX_JSON_TYPE_STRING); if (NULL != CONFIG_HOST_METADATA) { zbx_json_addstring(&json, ZBX_PROTO_TAG_HOST_METADATA, CONFIG_HOST_METADATA, ZBX_JSON_TYPE_STRING); } else if (NULL != CONFIG_HOST_METADATA_ITEM) { char **value; AGENT_RESULT result; init_result(&result); if (SUCCEED == process(CONFIG_HOST_METADATA_ITEM, PROCESS_LOCAL_COMMAND, &result) && NULL != (value = GET_STR_RESULT(&result)) && NULL != *value) { if (SUCCEED != zbx_is_utf8(*value)) { zabbix_log(LOG_LEVEL_WARNING, "cannot get host metadata using \"%s\" item specified by" " \"HostMetadataItem\" configuration parameter: returned value is not" " an UTF-8 string", CONFIG_HOST_METADATA_ITEM); } else { if (HOST_METADATA_LEN < zbx_strlen_utf8(*value)) { size_t bytes; zabbix_log(LOG_LEVEL_WARNING, "the returned value of \"%s\" item specified by" " \"HostMetadataItem\" configuration parameter is too long," " using first %d characters", CONFIG_HOST_METADATA_ITEM, HOST_METADATA_LEN); bytes = zbx_strlen_utf8_n(*value, HOST_METADATA_LEN); (*value)[bytes] = '\0'; } zbx_json_addstring(&json, ZBX_PROTO_TAG_HOST_METADATA, *value, ZBX_JSON_TYPE_STRING); } } else zabbix_log(LOG_LEVEL_WARNING, "cannot get host metadata using \"%s\" item specified by" " \"HostMetadataItem\" configuration parameter", CONFIG_HOST_METADATA_ITEM); free_result(&result); } if (NULL != CONFIG_LISTEN_IP) { char *p; if (NULL != (p = strchr(CONFIG_LISTEN_IP, ','))) *p = '\0'; zbx_json_addstring(&json, ZBX_PROTO_TAG_IP, CONFIG_LISTEN_IP, ZBX_JSON_TYPE_STRING); if (NULL != p) *p = ','; } if (ZBX_DEFAULT_AGENT_PORT != CONFIG_LISTEN_PORT) zbx_json_adduint64(&json, ZBX_PROTO_TAG_PORT, CONFIG_LISTEN_PORT); if (SUCCEED == (ret = zbx_tcp_connect(&s, CONFIG_SOURCE_IP, host, port, CONFIG_TIMEOUT))) { zabbix_log(LOG_LEVEL_DEBUG, "sending [%s]", json.buffer); if (SUCCEED == (ret = zbx_tcp_send(&s, json.buffer))) { zabbix_log(LOG_LEVEL_DEBUG, "before read"); if (SUCCEED == (ret = SUCCEED_OR_FAIL(zbx_tcp_recv_ext(&s, &buf, ZBX_TCP_READ_UNTIL_CLOSE, 0)))) { zabbix_log(LOG_LEVEL_DEBUG, "got [%s]", buf); if (SUCCEED != last_ret) { zabbix_log(LOG_LEVEL_WARNING, "active check configuration update from [%s:%hu]" " is working again", host, port); } parse_list_of_checks(buf, host, port); } } zbx_tcp_close(&s); } if (SUCCEED != ret && SUCCEED == last_ret) { zabbix_log(LOG_LEVEL_WARNING, "active check configuration update from [%s:%hu] started to fail (%s)", host, port, zbx_tcp_strerror()); } last_ret = ret; zbx_json_free(&json); zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __function_name, zbx_result_string(ret)); return ret; }
/****************************************************************************** * * * Function: recv_getqueue * * * * Purpose: process queue request * * * * Parameters: sock - [IN] the request socket * * jp - [IN] the request data * * * * Return value: SUCCEED - processed successfully * * FAIL - an error occurred * * * ******************************************************************************/ static int recv_getqueue(zbx_socket_t *sock, struct zbx_json_parse *jp) { const char *__function_name = "recv_getqueue"; int ret = FAIL, request_type = -1, now, i; char type[MAX_STRING_LEN], sessionid[MAX_STRING_LEN]; zbx_vector_ptr_t queue; struct zbx_json json; zbx_hashset_t queue_stats; zbx_queue_stats_t *stats; zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name); if (FAIL == zbx_json_value_by_name(jp, ZBX_PROTO_TAG_SID, sessionid, sizeof(sessionid)) || FAIL == zbx_session_validate(sessionid, USER_TYPE_SUPER_ADMIN)) { zbx_send_response_raw(sock, ret, "Permission denied.", CONFIG_TIMEOUT); goto out; } if (FAIL != zbx_json_value_by_name(jp, ZBX_PROTO_TAG_TYPE, type, sizeof(type))) { if (0 == strcmp(type, ZBX_PROTO_VALUE_GET_QUEUE_OVERVIEW)) request_type = ZBX_GET_QUEUE_OVERVIEW; else if (0 == strcmp(type, ZBX_PROTO_VALUE_GET_QUEUE_PROXY)) request_type = ZBX_GET_QUEUE_PROXY; else if (0 == strcmp(type, ZBX_PROTO_VALUE_GET_QUEUE_DETAILS)) request_type = ZBX_GET_QUEUE_DETAILS; } if (-1 == request_type) { zbx_send_response_raw(sock, ret, "Unsupported request type.", CONFIG_TIMEOUT); goto out; } now = time(NULL); zbx_vector_ptr_create(&queue); DCget_item_queue(&queue, 6, -1); zbx_json_init(&json, ZBX_JSON_STAT_BUF_LEN); switch (request_type) { case ZBX_GET_QUEUE_OVERVIEW: zbx_hashset_create(&queue_stats, 32, ZBX_DEFAULT_UINT64_HASH_FUNC, ZBX_DEFAULT_UINT64_COMPARE_FUNC); /* gather queue stats by item type */ for (i = 0; i < queue.values_num; i++) { zbx_queue_item_t *item = queue.values[i]; zbx_uint64_t id = item->type; if (NULL == (stats = zbx_hashset_search(&queue_stats, &id))) { zbx_queue_stats_t data = {id}; stats = zbx_hashset_insert(&queue_stats, &data, sizeof(data)); } queue_stats_update(stats, now - item->nextcheck); } zbx_json_addstring(&json, ZBX_PROTO_TAG_RESPONSE, ZBX_PROTO_VALUE_SUCCESS, ZBX_JSON_TYPE_STRING); queue_stats_export(&queue_stats, "itemtype", &json); zbx_hashset_destroy(&queue_stats); break; case ZBX_GET_QUEUE_PROXY: zbx_hashset_create(&queue_stats, 32, ZBX_DEFAULT_UINT64_HASH_FUNC, ZBX_DEFAULT_UINT64_COMPARE_FUNC); /* gather queue stats by proxy hostid */ for (i = 0; i < queue.values_num; i++) { zbx_queue_item_t *item = queue.values[i]; zbx_uint64_t id = item->proxy_hostid; if (NULL == (stats = zbx_hashset_search(&queue_stats, &id))) { zbx_queue_stats_t data = {id}; stats = zbx_hashset_insert(&queue_stats, &data, sizeof(data)); } queue_stats_update(stats, now - item->nextcheck); } zbx_json_addstring(&json, ZBX_PROTO_TAG_RESPONSE, ZBX_PROTO_VALUE_SUCCESS, ZBX_JSON_TYPE_STRING); queue_stats_export(&queue_stats, "proxyid", &json); zbx_hashset_destroy(&queue_stats); break; case ZBX_GET_QUEUE_DETAILS: zbx_vector_ptr_sort(&queue, (zbx_compare_func_t)queue_compare_by_nextcheck_asc); zbx_json_addstring(&json, ZBX_PROTO_TAG_RESPONSE, ZBX_PROTO_VALUE_SUCCESS, ZBX_JSON_TYPE_STRING); zbx_json_addarray(&json, ZBX_PROTO_TAG_DATA); for (i = 0; i < queue.values_num && i <= 500; i++) { zbx_queue_item_t *item = queue.values[i]; zbx_json_addobject(&json, NULL); zbx_json_adduint64(&json, "itemid", item->itemid); zbx_json_adduint64(&json, "nextcheck", item->nextcheck); zbx_json_close(&json); } zbx_json_close(&json); break; } zabbix_log(LOG_LEVEL_DEBUG, "%s() json.buffer:'%s'", __function_name, json.buffer); zbx_tcp_send_raw(sock, json.buffer); DCfree_item_queue(&queue); zbx_vector_ptr_destroy(&queue); zbx_json_free(&json); ret = SUCCEED; out: zabbix_log(LOG_LEVEL_DEBUG, "End of %s()", __function_name); return ret; }