void zbx_on_exit(void) { zabbix_log(LOG_LEVEL_DEBUG, "zbx_on_exit() called"); if (SUCCEED == DBtxn_ongoing()) DBrollback(); if (NULL != threads) { zbx_threads_wait(threads, threads_num); /* wait for all child processes to exit */ zbx_free(threads); } #ifdef HAVE_PTHREAD_PROCESS_SHARED zbx_locks_disable(); #endif free_metrics(); zbx_ipc_service_free_env(); DBconnect(ZBX_DB_CONNECT_EXIT); free_database_cache(); DBclose(); free_configuration_cache(); /* free history value cache */ zbx_vc_destroy(); zbx_destroy_itservices_lock(); /* free vmware support */ if (0 != CONFIG_VMWARE_FORKS) zbx_vmware_destroy(); free_selfmon_collector(); zbx_uninitialize_events(); zbx_unload_modules(); zabbix_log(LOG_LEVEL_INFORMATION, "Zabbix Server stopped. Zabbix %s (revision %s).", ZABBIX_VERSION, ZABBIX_REVISION); zabbix_close_log(); #if defined(PS_OVERWRITE_ARGV) setproctitle_free_env(); #endif exit(EXIT_SUCCESS); }
/****************************************************************************** * * * Function: * * * * Purpose: * * * * Parameters: * * * * Return value: * * * * Comments: * * * ******************************************************************************/ void zbx_on_exit() { int rc; zabbix_log(LOG_LEVEL_DEBUG, "zbx_on_exit() called"); if (SUCCEED == DBtxn_ongoing()) DBrollback(); if (NULL != threads) { int i; sigset_t set; /* ignore SIGCHLD signals in order for zbx_sleep() to work */ sigemptyset(&set); sigaddset(&set, SIGCHLD); sigprocmask(SIG_BLOCK, &set, NULL); for (i = 0; i < threads_num; i++) { if (threads[i]) { kill(threads[i], SIGTERM); threads[i] = ZBX_THREAD_HANDLE_NULL; } } zbx_free(threads); } zbx_sleep(2); /* wait for all child processes to exit */ ja_free_selfmon_collector(); /* kill the external command */ rc = system("pkill -SIGTERM jobarg_session"); rc = system("pkill -SIGTERM jobarg_command"); ja_log("JASERVER000002", 0, NULL, 0, JOBARG_VERSION, JOBARG_REVISION); zabbix_close_log(); exit(SUCCEED); }
/****************************************************************************** * * * Function: node_history * * * * Purpose: process new history received from a slave node * * * * Parameters: * * * * Return value: SUCCEED - processed successfully * * FAIL - an error occurred * * * * Author: Alexei Vladishev * * * * Comments: * * * ******************************************************************************/ int node_history(char *data, size_t datalen) { const char *r; char *newline = NULL; char *pos; int sender_nodeid = 0, nodeid = 0, firstline = 1, events = 0, history = 0, acknowledges = 0; const ZBX_TABLE *table_sync = NULL, *table = NULL; int res = SUCCEED; char *sql1 = NULL, *sql2 = NULL, *sql3 = NULL; size_t sql1_alloc, sql2_alloc, sql3_alloc; size_t sql1_offset, sql2_offset, sql3_offset; zbx_vector_uint64_t ack_eventids; assert(data); zabbix_log(LOG_LEVEL_DEBUG, "In node_history()"); buffer_alloc = 4 * ZBX_KIBIBYTE; sql1_alloc = 32 * ZBX_KIBIBYTE; sql2_alloc = 32 * ZBX_KIBIBYTE; sql3_alloc = 32 * ZBX_KIBIBYTE; tmp_alloc = 4 * ZBX_KIBIBYTE; buffer = zbx_malloc(buffer, buffer_alloc); sql1 = zbx_malloc(sql1, sql1_alloc); sql2 = zbx_malloc(sql2, sql2_alloc); sql3 = zbx_malloc(sql3, sql3_alloc); tmp = zbx_malloc(tmp, tmp_alloc); zbx_vector_uint64_create(&ack_eventids); DBbegin(); for (r = data; *r != '\0' && res == SUCCEED;) { if (NULL != (newline = strchr(r, '\n'))) *newline = '\0'; if (1 == firstline) { zbx_get_next_field(&r, &buffer, &buffer_alloc, ZBX_DM_DELIMITER); /* constant 'History' */ zbx_get_next_field(&r, &buffer, &buffer_alloc, ZBX_DM_DELIMITER); /* sender_nodeid */ sender_nodeid=atoi(buffer); zbx_get_next_field(&r, &buffer, &buffer_alloc, ZBX_DM_DELIMITER); /* nodeid */ nodeid=atoi(buffer); zbx_get_next_field(&r, &buffer, &buffer_alloc, ZBX_DM_DELIMITER); /* tablename */ if (FAIL == is_direct_slave_node(sender_nodeid)) { zabbix_log(LOG_LEVEL_ERR, "NODE %d: Received data from node %d" " that is not a direct slave node", CONFIG_NODEID, sender_nodeid); res = FAIL; } if (FAIL == is_slave_node(CONFIG_NODEID, nodeid)) { zabbix_log(LOG_LEVEL_ERR, "NODE %d: Received history for unknown slave node %d", CONFIG_NODEID, nodeid); res = FAIL; } table = DBget_table(buffer); if (NULL != table && 0 == (table->flags & (ZBX_HISTORY | ZBX_HISTORY_SYNC))) table = NULL; if (NULL != table && 0 != (table->flags & ZBX_HISTORY_SYNC)) { table_sync = table; if (NULL != (pos = strstr(buffer, "_sync"))) { *pos = '\0'; table = DBget_table(buffer); } } if (NULL == table) { zabbix_log(LOG_LEVEL_ERR, "NODE %d: Invalid received data: unknown tablename \"%s\"", CONFIG_NODEID, buffer); res = FAIL; } else { if (0 == strcmp(table->table, "events")) events = 1; if (0 == strncmp(table->table, "history", 7)) history = 1; if (0 == strcmp(table->table, "acknowledges")) acknowledges = 1; } if (NULL != newline) { zabbix_log(LOG_LEVEL_WARNING, "NODE %d: Received %s from node %d for node %d datalen " ZBX_FS_SIZE_T, CONFIG_NODEID, buffer, sender_nodeid, nodeid, (zbx_fs_size_t)datalen); } firstline = 0; sql1_offset = 0; sql2_offset = 0; sql3_offset = 0; } else if (NULL != table) { if (events) { res = process_record_event(sender_nodeid, nodeid, table, r); } else { res = process_record(&sql1, &sql1_alloc, &sql1_offset, sender_nodeid, nodeid, table, r, newline ? 0 : 1, acknowledges, &ack_eventids); if (SUCCEED == res && 0 != history) { res = process_items(&sql2, &sql2_alloc, &sql2_offset, sender_nodeid, nodeid, table, r, newline ? 0 : 1); } if (SUCCEED == res && NULL != table_sync && 0 != CONFIG_MASTER_NODEID) { res = process_record(&sql3, &sql3_alloc, &sql3_offset, sender_nodeid, nodeid, table_sync, r, newline ? 0 : 1, 0, NULL); } } } if (newline != NULL) { *newline = '\n'; r = newline + 1; } else break; } if (SUCCEED == res) DBcommit(); else DBrollback(); zbx_vector_uint64_destroy(&ack_eventids); zbx_free(tmp); zbx_free(sql1); zbx_free(sql2); zbx_free(sql3); zbx_free(buffer); return res; }
/****************************************************************************** * * * Function: delete_history * * * * Purpose: remove outdated information from historical table * * * * Parameters: now - current timestamp * * * * Return value: number of rows records * * * * Author: Alexei Vladishev * * * * Comments: * * * ******************************************************************************/ static int delete_history(const char *table, const char *fieldname, int now) { const char *__function_name = "delete_history"; DB_RESULT result; DB_ROW row; int minclock, records = 0; zbx_uint64_t lastid, maxid; zabbix_log(LOG_LEVEL_DEBUG, "In %s() table:'%s' now:%d", __function_name, table, now); DBbegin(); result = DBselect( "select nextid" " from ids" " where table_name='%s'" " and field_name='%s'", table, fieldname); if (NULL == (row = DBfetch(result))) goto rollback; ZBX_STR2UINT64(lastid, row[0]); DBfree_result(result); result = DBselect("select min(clock) from %s", table); if (NULL == (row = DBfetch(result)) || SUCCEED == DBis_null(row[0])) goto rollback; minclock = atoi(row[0]); DBfree_result(result); result = DBselect("select max(id) from %s", table); if (NULL == (row = DBfetch(result)) || SUCCEED == DBis_null(row[0])) goto rollback; ZBX_STR2UINT64(maxid, row[0]); DBfree_result(result); records = DBexecute( "delete from %s" " where id<" ZBX_FS_UI64 " and (clock<%d" " or (id<=" ZBX_FS_UI64 " and clock<%d))", table, maxid, now - CONFIG_PROXY_OFFLINE_BUFFER * SEC_PER_HOUR, lastid, MIN(now - CONFIG_PROXY_LOCAL_BUFFER * SEC_PER_HOUR, minclock + HK_MAX_DELETE_PERIODS * hk_period)); DBcommit(); return records; rollback: DBfree_result(result); DBrollback(); return 0; }
/****************************************************************************** * * * Function: * * * * Purpose: * * * * Parameters: * * * * Return value: * * * * Comments: * * * ******************************************************************************/ char *evaluate_jobnetrun(zbx_sock_t * sock, struct zbx_json_parse *jp, int *ret) { DB_RESULT result; DB_ROW row; struct zbx_json_parse jp_row; struct zbx_json_parse jp_row2; char *message = NULL; char value[MAX_STRING_LEN]; const char *p; const char *p2; const char *p3 = NULL; int version; int res; int i; int count; zbx_uint64_t inner_jobnet_id; static JOBARG_EXEC_REQUEST er; init_exec_request(&er); if (SUCCEED == zbx_json_value_by_name(jp, JA_PROTO_TAG_VERSION, value, sizeof(value))) { version = atoi(value); if (version != JA_PROTO_VALUE_VERSION_1) { ja_log("JATRAPPER200027", 0, NULL, 0, JA_PROTO_VALUE_VERSION_1); *ret = FAIL; return zbx_dsprintf(message, "Received message error: [version] is not correct."); } } else { ja_log("JATRAPPER200028", 0, NULL, 0); *ret = FAIL; return zbx_dsprintf(message, "Received message error: [version] not found"); } if (NULL == (p = zbx_json_pair_by_name(jp, JA_PROTO_TAG_DATA))) { ja_log("JATRAPPER200021", 0, NULL, 0); *ret = FAIL; return zbx_dsprintf(message, "Received message error: [data] not found"); } else { if (FAIL == (*ret = zbx_json_brackets_open(p, &jp_row))) { ja_log("JATRAPPER200022", 0, NULL, 0); *ret = FAIL; return zbx_dsprintf(message, "Received message error: Cannot open [data] object"); } else { if (SUCCEED == zbx_json_value_by_name(&jp_row, JA_PROTO_TAG_USERNAME, value, sizeof(value))) { er.username = strdup(value); } else { ja_log("JATRAPPER200032", 0, NULL, 0); *ret = FAIL; return zbx_dsprintf(message, "Received message error: [username] not found"); } if (SUCCEED == zbx_json_value_by_name(&jp_row, JA_PROTO_TAG_PASSWORD, value, sizeof(value))) { er.password = strdup(value); } else { ja_log("JATRAPPER200033", 0, NULL, 0); zbx_free(er.username); *ret = FAIL; return zbx_dsprintf(message, "Received message error: [password] not found"); } if (SUCCEED == zbx_json_value_by_name(&jp_row, JA_PROTO_TAG_JOBNETID, value, sizeof(value))) { er.jobnetid = strdup(value); } else { ja_log("JATRAPPER200034", 0, NULL, 0); zbx_free(er.username); zbx_free(er.password); *ret = FAIL; return zbx_dsprintf(message, "Received message error: [jobnetid] not found"); } if (SUCCEED == zbx_json_value_by_name(&jp_row, JA_PROTO_TAG_STARTTIME, value, sizeof(value))) { er.starttime = strdup(value); res = time_passed_check(er.starttime); if (res == FAIL) { zbx_free(er.username); zbx_free(er.password); zbx_free(er.jobnetid); zbx_free(er.starttime); *ret = FAIL; return zbx_dsprintf(message, "Received message error: [start_time] already passed."); } } i = 0; if (NULL != (p2 = zbx_json_pair_by_name(&jp_row, JA_PROTO_TAG_ENV))) { if (FAIL == (*ret = zbx_json_brackets_open(p2, &jp_row2))) { ja_log("JATRAPPER200051", 0, er.jobnetid, 0); zbx_free(er.username); zbx_free(er.password); zbx_free(er.jobnetid); zbx_free(er.starttime); *ret = FAIL; return zbx_dsprintf(message, "Received message error: Cannot open [env] object"); } else { while (NULL != (p3 = zbx_json_pair_next(&jp_row2, p3, value, sizeof(value)))) { er.env[i] = strdup(value); zbx_json_value_by_name(&jp_row2, er.env[i], value, sizeof(value)); er.value[i] = strdup(value); i++; } er.env[i] = '\0'; } } if (SUCCEED == zbx_json_value_by_name(&jp_row, JA_PROTO_TAG_DETERRENCE, value, sizeof(value))) { er.deterrence = atoi(value); } if (SUCCEED == (job_exec_auth(er))) { DBbegin(); /* double check start-up suppression time specified */ if (er.starttime != NULL && er.deterrence == 1) { result = DBselect("select count(*) from ja_run_jobnet_table" " where scheduled_time = %s and jobnet_id = '%s' and run_type = %d", er.starttime, er.jobnetid, JA_JOBNET_RUN_TYPE_SCHEDULED); if (NULL == (row = DBfetch(result))) { zbx_snprintf(msgwork, sizeof(msgwork), "%s %s %d", er.starttime, er.jobnetid, JA_JOBNET_RUN_TYPE_SCHEDULED); ja_log("JATRAPPER200057", 0, er.jobnetid, 0, "ja_run_jobnet_table", msgwork); DBfree_result(result); DBrollback(); clean_exec_request(&er, i); *ret = FAIL; return zbx_dsprintf(message, "ja_run_jobnet_table select error."); } count = atoi(row[0]); DBfree_result(result); if (count > 0) { DBrollback(); clean_exec_request(&er, i); *ret = FAIL; return zbx_dsprintf(message, "Received message error: Double registration detection of time starting jobnet."); } } if (SUCCEED == (register_db_table(er, &inner_jobnet_id, i))) { DBcommit(); clean_exec_request(&er, i); return zbx_dsprintf(message, "Registry number : [" ZBX_FS_UI64 "]", inner_jobnet_id); } else { DBrollback(); clean_exec_request(&er, i); *ret = FAIL; return zbx_dsprintf(message, "ja_run_jobnet_table insert error."); } } else { clean_exec_request(&er, i); *ret = FAIL; return zbx_dsprintf(message, "Authentication failure."); } } } }
/****************************************************************************** * * * Function: ja_host_getip * * * * Purpose: get the ip address and port of the host * * * * Parameters: host (in) - host name * * host_ip (out) - ip address * * inner_job_id (in) - inner job id * * port (out) - port * * txn (in) - transaction instruction * * * * Return value: return the host id. failure is zero * * * * Comments: value is not set when host_ip or port is NULL * * * ******************************************************************************/ zbx_uint64_t ja_host_getip(const char *host, char *host_ip, const zbx_uint64_t inner_job_id, int *port, int txn) { DB_RESULT result; DB_RESULT result2; DB_ROW row; DB_ROW row2; char *host_esc; zbx_uint64_t hostid; const char *__function_name = "ja_host_getip"; zabbix_log(LOG_LEVEL_DEBUG, "In %s() host: %s", __function_name, host); hostid = 0; result = NULL; host_esc = DBdyn_escape_string(host); switch (CONFIG_ZABBIX_VERSION) { case 1: // for zabbix 1.8 result = DBselect("select hostid, useip, dns, ip, status, port from hosts where host = '%s'", host_esc); break; case 2: case 3: // for zabbix 2.0 or 2.2 result = DBselect(" select i.hostid, i.useip, i.dns, i.ip, h.status, i.port from hosts h, interface i" " where h.hostid = i.hostid and i.main = 1 and h.host = '%s'", host_esc); break; default: ja_log("JAHOST200001", 0, NULL, inner_job_id, __function_name, CONFIG_ZABBIX_VERSION); goto error; } if (result == NULL) { if (txn == JA_TXN_ON) { DBrollback(); } ja_log("JAHOST200007", 0, NULL, inner_job_id, __function_name, CONFIG_ZABBIX_VERSION); goto error; } row = DBfetch(result); if (row == NULL) { ja_log("JAHOST200002", 0, NULL, inner_job_id, __function_name, host, inner_job_id); goto error; } /* get the force flag and job id */ result2 = DBselect("select force_flag, job_id from ja_run_job_table where inner_job_id = " ZBX_FS_UI64, inner_job_id); if (NULL == (row2 = DBfetch(result2))) { ja_log("JAHOST200004", 0, NULL, inner_job_id, __function_name, inner_job_id); DBfree_result(result2); goto error; } if (atoi(row[4]) != HOST_STATUS_MONITORED && atoi(row2[0]) == JA_JOB_FORCE_FLAG_OFF) { ja_log("JAHOST200003", 0, NULL, inner_job_id, __function_name, host, inner_job_id, row2[1]); DBfree_result(result2); goto error; } DBfree_result(result2); ZBX_STR2UINT64(hostid, row[0]); if (host_ip != NULL) { if (atoi(row[1]) == 0) { // use dns zbx_snprintf(host_ip, strlen(row[2]) + 1, "%s", row[2]); } else { // use ip zbx_snprintf(host_ip, strlen(row[3]) + 1, "%s", row[3]); } } /* get port */ if (port != NULL) { *port = atoi(row[5]); } DBfree_result(result); result = NULL; error: zbx_free(host_esc); if (result != NULL) DBfree_result(result); return hostid; }
void zbx_on_exit() { zabbix_log(LOG_LEVEL_DEBUG, "zbx_on_exit() called"); if (SUCCEED == DBtxn_ongoing()) DBrollback(); if (NULL != threads) { int i; sigset_t set; /* ignore SIGCHLD signals in order for zbx_sleep() to work */ sigemptyset(&set); sigaddset(&set, SIGCHLD); sigprocmask(SIG_BLOCK, &set, NULL); for (i = 0; i < threads_num; i++) { if (threads[i]) { kill(threads[i], SIGTERM); threads[i] = ZBX_THREAD_HANDLE_NULL; } } zbx_free(threads); } free_metrics(); zbx_sleep(2); /* wait for all child processes to exit */ DBconnect(ZBX_DB_CONNECT_EXIT); free_database_cache(); DBclose(); free_configuration_cache(); /* free history value cache */ zbx_vc_destroy(); zbx_destroy_itservices_lock(); zbx_mutex_destroy(&node_sync_access); #ifdef HAVE_OPENIPMI free_ipmi_handler(); #endif #ifdef HAVE_SQLITE3 zbx_remove_sqlite3_mutex(); #endif /* free vmware support */ if (0 != CONFIG_VMWARE_FORKS) zbx_vmware_destroy(); free_selfmon_collector(); unload_modules(); zabbix_log(LOG_LEVEL_INFORMATION, "Zabbix Server stopped. Zabbix %s (revision %s).", ZABBIX_VERSION, ZABBIX_REVISION); zabbix_close_log(); #if defined(PS_OVERWRITE_ARGV) setproctitle_free_env(); #endif exit(SUCCEED); }