void main_poller_loop(unsigned char poller_type) { int nextcheck, sleeptime, processed; double sec; zabbix_log(LOG_LEVEL_DEBUG, "In main_poller_loop() process_type:'%s' process_num:%d", get_process_type_string(process_type), process_num); zbx_setproctitle("%s [connecting to the database]", get_process_type_string(process_type)); DBconnect(ZBX_DB_CONNECT_NORMAL); for (;;) { zbx_setproctitle("%s [getting values]", get_process_type_string(process_type)); sec = zbx_time(); processed = get_values(poller_type); sec = zbx_time() - sec; zabbix_log(LOG_LEVEL_DEBUG, "%s #%d spent " ZBX_FS_DBL " seconds while updating %d values", get_process_type_string(process_type), process_num, sec, processed); nextcheck = DCconfig_get_poller_nextcheck(poller_type); sleeptime = calculate_sleeptime(nextcheck, POLLER_DELAY); zbx_sleep_loop(sleeptime); } }
/****************************************************************************** * * * Function: main_escalator_loop * * * * Purpose: periodically check table escalations and generate alerts * * * * Parameters: * * * * Return value: * * * * Author: Aleksander Vladishev * * * * Comments: never returns * * * ******************************************************************************/ int main_escalator_loop() { int now/*, nextcheck, sleeptime*/; double sec; struct sigaction phan; zabbix_log(LOG_LEVEL_DEBUG, "In main_escalator_loop()"); /* phan.sa_handler = child_signal_handler;*/ phan.sa_sigaction = child_signal_handler; sigemptyset(&phan.sa_mask); phan.sa_flags = SA_SIGINFO; sigaction(SIGALRM, &phan, NULL); zbx_setproctitle("escalator [connecting to the database]"); DBconnect(ZBX_DB_CONNECT_NORMAL); for (;;) { now = time(NULL); sec = zbx_time(); zbx_setproctitle("escalator [processing escalations]"); process_escalations(now); sec = zbx_time() - sec; /* nextcheck = get_minnextcheck(); if (FAIL == nextcheck) sleeptime = CONFIG_ESCALATOR_FREQUENCY; else { sleeptime = nextcheck - time(NULL); if (sleeptime < 0) sleeptime = 0; else if (sleeptime > CONFIG_ESCALATOR_FREQUENCY) sleeptime = CONFIG_ESCALATOR_FREQUENCY; }*/ zabbix_log(LOG_LEVEL_DEBUG, "Escalator spent " ZBX_FS_DBL " seconds while processing escalation items." " Nextcheck after %d sec.", sec, CONFIG_ESCALATOR_FREQUENCY); zbx_setproctitle("escalator [sleeping for %d seconds]", CONFIG_ESCALATOR_FREQUENCY); sleep(CONFIG_ESCALATOR_FREQUENCY); /* if (sleeptime > 0) { zbx_setproctitle("escalator [sleeping for %d seconds]", sleeptime); sleep(sleeptime); }*/ } /* Never reached */ DBclose(); }
/****************************************************************************** * * * Function: main_snmptrapper_loop * * * * Purpose: SNMP trap reader's entry point * * * * Author: Rudolfs Kreicbergs * * * ******************************************************************************/ void main_snmptrapper_loop() { const char *__function_name = "main_snmptrapper_loop"; zabbix_log(LOG_LEVEL_DEBUG, "In %s() trapfile:'%s'", __function_name, CONFIG_SNMPTRAP_FILE); zbx_setproctitle("%s [connecting to the database]", get_process_type_string(process_type)); DBconnect(ZBX_DB_CONNECT_NORMAL); DBget_lastsize(); for (;;) { zbx_setproctitle("%s [processing data]", get_process_type_string(process_type)); while (SUCCEED == get_latest_data()) read_traps(); zbx_sleep_loop(1); } if (-1 != trap_fd) close(trap_fd); }
/****************************************************************************** * * * Function: main_dbconfig_loop * * * * Purpose: periodically synchronises database data with memory cache * * * * Parameters: * * * * Return value: * * * * Author: Alexander Vladishev * * * * Comments: never returns * * * ******************************************************************************/ void main_dbconfig_loop(void) { double sec = 0.0; zbx_setproctitle("%s [waiting %d sec for processes]", get_process_type_string(process_type), CONFIG_CONFSYNCER_FREQUENCY); /* the initial configuration sync is done by server before worker processes are forked */ zbx_sleep_loop(CONFIG_CONFSYNCER_FREQUENCY); zbx_setproctitle("%s [connecting to the database]", get_process_type_string(process_type)); DBconnect(ZBX_DB_CONNECT_NORMAL); for (;;) { zbx_setproctitle("%s [synced configuration in " ZBX_FS_DBL " sec, syncing configuration]", get_process_type_string(process_type), sec); sec = zbx_time(); DCsync_configuration(); sec = zbx_time() - sec; zbx_setproctitle("%s [synced configuration in " ZBX_FS_DBL " sec, idle %d sec]", get_process_type_string(process_type), sec, CONFIG_CONFSYNCER_FREQUENCY); zbx_sleep_loop(CONFIG_CONFSYNCER_FREQUENCY); } }
void child_trapper_main(zbx_process_t p, zbx_sock_t *s) { struct sigaction phan; zabbix_log( LOG_LEVEL_DEBUG, "In child_trapper_main()"); phan.sa_handler = child_signal_handler; sigemptyset(&phan.sa_mask); phan.sa_flags = 0; sigaction(SIGALRM, &phan, NULL); zbx_process = p; DBconnect(ZBX_DB_CONNECT_NORMAL); for (;;) { zbx_setproctitle("trapper [waiting for connection]"); if (SUCCEED == zbx_tcp_accept(s)) { zbx_setproctitle("trapper [processing data]"); process_trapper_child(s); zbx_tcp_unaccept(s); } else zabbix_log(LOG_LEVEL_WARNING, "Trapper failed to accept connection"); } DBclose(); }
/****************************************************************************** * * * Function: main_nodewatcher_loop * * * * Purpose: periodically calculates checksum of config data * * * * Parameters: * * * * Return value: * * * * Author: Alexei Vladishev * * * * Comments: never returns * * * ******************************************************************************/ void main_nodewatcher_loop() { int start, end; int lastrun = 0; zabbix_log(LOG_LEVEL_DEBUG, "In main_nodewatcher_loop()"); zbx_setproctitle("%s [connecting to the database]", get_process_type_string(process_type)); DBconnect(ZBX_DB_CONNECT_NORMAL); for (;;) { zbx_setproctitle("%s [exchanging data]", get_process_type_string(process_type)); start = time(NULL); zabbix_log(LOG_LEVEL_DEBUG, "Starting sync with nodes"); if (lastrun + 120 < start) { process_nodes(); lastrun = start; } /* send new history data to master node */ main_historysender(); end = time(NULL); zbx_sleep_loop(10 - (end - start)); } }
/****************************************************************************** * * * Function: main_watchdog_loop * * * * Purpose: check database availability every DB_PING_FREQUENCY seconds and * * alert admins if it is down * * * * Author: Alexei Vladishev, Rudolfs Kreicbergs * * * ******************************************************************************/ void main_watchdog_loop() { int now, nextsync = 0; zabbix_log(LOG_LEVEL_DEBUG, "In main_watchdog_loop()"); /* disable writing to database in zabbix_syslog() */ CONFIG_ENABLE_LOG = 0; zbx_vector_ptr_create(&recipients); for (;;) { zbx_setproctitle("%s [pinging database]", get_process_type_string(process_type)); if (ZBX_DB_OK != DBconnect(ZBX_DB_CONNECT_ONCE)) { zabbix_log(LOG_LEVEL_WARNING, "watchdog: database is down"); send_alerts(); } else if (nextsync <= (now = (int)time(NULL))) { zbx_setproctitle("%s [syncing configuration]", get_process_type_string(process_type)); sync_config(); nextsync = now + CONFIG_CONFSYNCER_FREQUENCY; } DBclose(); zbx_sleep_loop(DB_PING_FREQUENCY); } }
void main_trapper_loop(zbx_sock_t *s) { const char *__function_name = "main_trapper_loop"; zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name); zbx_setproctitle("%s [connecting to the database]", get_process_type_string(process_type)); DBconnect(ZBX_DB_CONNECT_NORMAL); for (;;) { zbx_setproctitle("%s [waiting for connection]", get_process_type_string(process_type)); update_selfmon_counter(ZBX_PROCESS_STATE_IDLE); if (SUCCEED == zbx_tcp_accept(s)) { update_selfmon_counter(ZBX_PROCESS_STATE_BUSY); zbx_setproctitle("%s [processing data]", get_process_type_string(process_type)); process_trapper_child(s); zbx_tcp_unaccept(s); } else zabbix_log(LOG_LEVEL_WARNING, "Trapper failed to accept connection"); } }
/****************************************************************************** * * * Function: main_discoverer_loop * * * * Purpose: periodically try to find new hosts and services * * * * Author: Alexei Vladishev * * * * Comments: executes once per 30 seconds (hardcoded) * * * ******************************************************************************/ void main_discoverer_loop() { int now, nextcheck, sleeptime; double sec; zabbix_log(LOG_LEVEL_DEBUG, "In main_discoverer_loop() process_num:%d", process_num); zbx_setproctitle("%s [connecting to the database]", get_process_type_string(process_type)); DBconnect(ZBX_DB_CONNECT_NORMAL); for (;;) { zbx_setproctitle("%s [performing discovery]", get_process_type_string(process_type)); now = time(NULL); sec = zbx_time(); process_discovery(now); sec = zbx_time() - sec; zabbix_log(LOG_LEVEL_DEBUG, "%s #%d spent " ZBX_FS_DBL " seconds while processing rules", get_process_type_string(process_type), process_num, sec); nextcheck = get_minnextcheck(now); sleeptime = calculate_sleeptime(nextcheck, DISCOVERER_DELAY); zbx_sleep_loop(sleeptime); } }
/****************************************************************************** * * * Function: main_httppoller_loop * * * * Purpose: main loop of processing of httptests * * * * Parameters: * * * * Return value: * * * * Author: Alexei Vladishev * * * * Comments: never returns * * * ******************************************************************************/ void main_httppoller_loop() { int now, nextcheck, sleeptime; double sec; zabbix_log(LOG_LEVEL_DEBUG, "In main_httppoller_loop() process_num:%d", process_num); zbx_setproctitle("%s [connecting to the database]", get_process_type_string(process_type)); DBconnect(ZBX_DB_CONNECT_NORMAL); for (;;) { zbx_setproctitle("%s [getting values]", get_process_type_string(process_type)); now = time(NULL); sec = zbx_time(); process_httptests(process_num, now); sec = zbx_time() - sec; zabbix_log(LOG_LEVEL_DEBUG, "%s #%d spent " ZBX_FS_DBL " seconds while updating HTTP tests", get_process_type_string(process_type), process_num, sec); nextcheck = get_minnextcheck(now); sleeptime = calculate_sleeptime(nextcheck, POLLER_DELAY); zbx_sleep_loop(sleeptime); } }
void main_housekeeper_loop(void) { int records, start, sleeptime; double sec; for (;;) { start = time(NULL); zabbix_log(LOG_LEVEL_WARNING, "executing housekeeper"); zbx_setproctitle("%s [connecting to the database]", get_process_type_string(process_type)); DBconnect(ZBX_DB_CONNECT_NORMAL); zbx_setproctitle("%s [removing old history]", get_process_type_string(process_type)); sec = zbx_time(); records = housekeeping_history(start); sec = zbx_time() - sec; DBclose(); sleeptime = CONFIG_HOUSEKEEPING_FREQUENCY * SEC_PER_HOUR - (time(NULL) - start); zabbix_log(LOG_LEVEL_WARNING, "%s [deleted %d records in " ZBX_FS_DBL " sec, idle %d sec]", get_process_type_string(process_type), records, sec, sleeptime); zbx_setproctitle("%s [deleted %d records in " ZBX_FS_DBL " sec, idle %d sec]", get_process_type_string(process_type), records, sec, sleeptime); zbx_sleep_loop(sleeptime); } }
/****************************************************************************** * * * Function: main_escalator_loop * * * * Purpose: periodically check table escalations and generate alerts * * * * Parameters: * * * * Return value: * * * * Author: Alexander Vladishev * * * * Comments: never returns * * * ******************************************************************************/ void main_escalator_loop() { int now; double sec; zabbix_log(LOG_LEVEL_DEBUG, "In main_escalator_loop()"); zbx_setproctitle("%s [connecting to the database]", get_process_type_string(process_type)); DBconnect(ZBX_DB_CONNECT_NORMAL); for (;;) { zbx_setproctitle("%s [processing escalations]", get_process_type_string(process_type)); now = time(NULL); sec = zbx_time(); process_escalations(now); sec = zbx_time() - sec; zabbix_log(LOG_LEVEL_DEBUG, "%s #%d spent " ZBX_FS_DBL " seconds while processing escalations", get_process_type_string(process_type), process_num, sec); zbx_sleep_loop(CONFIG_ESCALATOR_FREQUENCY); } }
ZBX_THREAD_ENTRY(trapper_thread, args) { double sec = 0.0; zbx_socket_t s; 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_program_type_string(program_type), server_num, get_process_type_string(process_type), process_num); memcpy(&s, (zbx_socket_t *)((zbx_thread_args_t *)args)->args, sizeof(zbx_socket_t)); #if defined(HAVE_POLARSSL) || defined(HAVE_GNUTLS) || defined(HAVE_OPENSSL) zbx_tls_init_child(); find_psk_in_cache = DCget_psk_by_identity; #endif zbx_setproctitle("%s #%d [connecting to the database]", get_process_type_string(process_type), process_num); DBconnect(ZBX_DB_CONNECT_NORMAL); for (;;) { zbx_handle_log(); zbx_setproctitle("%s #%d [processed data in " ZBX_FS_DBL " sec, waiting for connection]", get_process_type_string(process_type), process_num, sec); update_selfmon_counter(ZBX_PROCESS_STATE_IDLE); /* Trapper has to accept all types of connections it can accept with the specified configuration. */ /* Only after receiving data it is known who has sent them and one can decide to accept or discard */ /* the data. */ if (SUCCEED == zbx_tcp_accept(&s, ZBX_TCP_SEC_TLS_CERT | ZBX_TCP_SEC_TLS_PSK | ZBX_TCP_SEC_UNENCRYPTED)) { zbx_timespec_t ts; /* get connection timestamp */ zbx_timespec(&ts); update_selfmon_counter(ZBX_PROCESS_STATE_BUSY); zbx_setproctitle("%s #%d [processing data]", get_process_type_string(process_type), process_num); sec = zbx_time(); process_trapper_child(&s, &ts); sec = zbx_time() - sec; zbx_tcp_unaccept(&s); } else if (EINTR != zbx_socket_last_error()) { zabbix_log(LOG_LEVEL_WARNING, "failed to accept an incoming connection: %s", zbx_socket_strerror()); } } }
/****************************************************************************** * * * Function: main_timer_loop * * * * Purpose: periodically updates time-related triggers * * * * Parameters: * * * * Return value: * * * * Author: Alexei Vladishev * * * * Comments: does update once per 30 seconds (hardcoded) * * * ******************************************************************************/ void main_timer_loop() { int now, cur; /* int itemid,functionid; char *function; char *parameter;*/ DB_ITEM item; DB_RESULT result; DB_ROW row; for(;;) { zbx_setproctitle("Timer: updating nodata() functions"); DBconnect(ZBX_DB_CONNECT_NORMAL); now=time(NULL); /* #ifdef HAVE_POSTGRESQL zbx_snprintf(sql,sizeof(sql),"select distinct f.itemid,f.functionid,f.parameter from functions f, items i,hosts h where h.hostid=i.hostid and h.status=%d and i.itemid=f.itemid and f.function in ('nodata','date','dayofweek','time','now') and i.lastclock+f.parameter::text::integer<=%d and i.status=%d", HOST_STATUS_MONITORED, now, ITEM_STATUS_ACTIVE); #else zbx_snprintf(sql,sizeof(sql),"select distinct f.itemid,f.functionid,f.parameter,f.function from functions f, items i,hosts h where h.hostid=i.hostid and h.status=%d and i.itemid=f.itemid and f.function in ('nodata','date','dayofweek','time','now') and i.lastclock+f.parameter<=%d and i.status=%d", HOST_STATUS_MONITORED, now, ITEM_STATUS_ACTIVE); #endif */ result = DBselect("select %s, functions f where h.hostid=i.hostid and h.status=%d and i.status=%d and f.function in ('nodata','date','dayofweek','time','now','count') and i.itemid=f.itemid and " ZBX_COND_SITE, ZBX_SQL_ITEM_SELECT, HOST_STATUS_MONITORED, ITEM_STATUS_ACTIVE, getSiteCondition ()); cur = 1; while((row=DBfetch(result))) { #ifdef HAVE_MYSQL zbx_setproctitle("Timer: processing %d item of %d", cur++, result->row_count); #endif DBget_item_from_db(&item,row); DBbegin(); update_functions(&item); update_triggers(item.itemid); DBcommit(); DBfree_item(&item); } DBfree_result(result); DBclose(); zbx_setproctitle("Timer: sleeping for 10 sec"); sleep(10); } }
/****************************************************************************** * * * Function: main_datasender_loop * * * * Purpose: periodically sends history and events to the server * * * * Parameters: * * * * Return value: * * * * Author: Alexander Vladishev * * * * Comments: never returns * * * ******************************************************************************/ ZBX_THREAD_ENTRY(datasender_thread, args) { int records = 0, r; double sec = 0.0; struct zbx_json j; 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); zbx_setproctitle("%s [connecting to the database]", get_process_type_string(process_type)); DBconnect(ZBX_DB_CONNECT_NORMAL); zbx_json_init(&j, 16 * ZBX_KIBIBYTE); for (;;) { zbx_setproctitle("%s [sent %d values in " ZBX_FS_DBL " sec, sending data]", get_process_type_string(process_type), records, sec); sec = zbx_time(); host_availability_sender(&j); records = 0; retry_history: history_sender(&j, &r, ZBX_PROTO_VALUE_HISTORY_DATA, proxy_get_hist_data, proxy_set_hist_lastid); records += r; if (ZBX_MAX_HRECORDS == r) goto retry_history; retry_dhistory: history_sender(&j, &r, ZBX_PROTO_VALUE_DISCOVERY_DATA, proxy_get_dhis_data, proxy_set_dhis_lastid); records += r; if (ZBX_MAX_HRECORDS == r) goto retry_dhistory; retry_autoreg_host: history_sender(&j, &r, ZBX_PROTO_VALUE_AUTO_REGISTRATION_DATA, proxy_get_areg_data, proxy_set_areg_lastid); records += r; if (ZBX_MAX_HRECORDS == r) goto retry_autoreg_host; sec = zbx_time() - sec; zbx_setproctitle("%s [sent %d values in " ZBX_FS_DBL " sec, idle %d sec]", get_process_type_string(process_type), records, sec, CONFIG_PROXYDATA_FREQUENCY); zbx_sleep_loop(CONFIG_PROXYDATA_FREQUENCY); } }
/****************************************************************************** * * * Function: main_discoverer_loop * * * * Purpose: periodically try to find new hosts and services * * * * Author: Alexei Vladishev * * * * Comments: executes once per 30 seconds (hardcoded) * * * ******************************************************************************/ void main_discoverer_loop(void) { int now, nextcheck, sleeptime = -1, rule_count = 0, old_rule_count = 0; double sec, total_sec = 0.0, old_total_sec = 0.0; time_t last_stat_time; #define STAT_INTERVAL 5 /* if a process is busy and does not sleep then update status not faster than */ /* once in STAT_INTERVAL seconds */ zbx_setproctitle("%s #%d [connecting to the database]", get_process_type_string(process_type), process_num); last_stat_time = time(NULL); DBconnect(ZBX_DB_CONNECT_NORMAL); for (;;) { if (0 != sleeptime) { zbx_setproctitle("%s #%d [processed %d rules in " ZBX_FS_DBL " sec, performing discovery]", get_process_type_string(process_type), process_num, old_rule_count, old_total_sec); } now = time(NULL); sec = zbx_time(); rule_count += process_discovery(now); total_sec += zbx_time() - sec; nextcheck = get_minnextcheck(now); sleeptime = calculate_sleeptime(nextcheck, DISCOVERER_DELAY); if (0 != sleeptime || STAT_INTERVAL <= time(NULL) - last_stat_time) { if (0 == sleeptime) { zbx_setproctitle("%s #%d [processed %d rules in " ZBX_FS_DBL " sec, performing " "discovery]", get_process_type_string(process_type), process_num, rule_count, total_sec); } else { zbx_setproctitle("%s #%d [processed %d rules in " ZBX_FS_DBL " sec, idle %d sec]", get_process_type_string(process_type), process_num, rule_count, total_sec, sleeptime); old_rule_count = rule_count; old_total_sec = total_sec; } rule_count = 0; total_sec = 0.0; last_stat_time = time(NULL); } zbx_sleep_loop(sleeptime); } #undef STAT_INTERVAL }
void main_poller_loop(zbx_process_t p, int type, int num) { struct sigaction phan; int now; int nextcheck, sleeptime; int items; double sec; zabbix_log( LOG_LEVEL_DEBUG, "In main_poller_loop(type:%d,num:%d)", type, num); /* phan.sa_handler = child_signal_handler;*/ phan.sa_sigaction = child_signal_handler; sigemptyset(&phan.sa_mask); phan.sa_flags = SA_SIGINFO; sigaction(SIGALRM, &phan, NULL); zbx_process = p; poller_type = type; poller_num = num; DBconnect(ZBX_DB_CONNECT_NORMAL); for (;;) { zbx_setproctitle("poller [getting values]"); now = time(NULL); sec = zbx_time(); items = get_values(now, &nextcheck); sec = zbx_time() - sec; if (FAIL == nextcheck) sleeptime = POLLER_DELAY; else { sleeptime = nextcheck - time(NULL); if (sleeptime < 0) sleeptime = 0; if (sleeptime > POLLER_DELAY) sleeptime = POLLER_DELAY; } zabbix_log(LOG_LEVEL_DEBUG, "Poller spent " ZBX_FS_DBL " seconds while updating %3d values." " Sleeping for %d seconds", sec, items, sleeptime); if (sleeptime > 0) { zbx_setproctitle("poller [sleeping for %d seconds]", sleeptime); sleep(sleeptime); } } }
/****************************************************************************** * * * Function: main_watchdog_loop * * * * Purpose: check database availability every DB_PING_FREQUENCY seconds and * * alert admins if it is down * * * * Author: Alexei Vladishev, Rudolfs Kreicbergs * * * ******************************************************************************/ ZBX_THREAD_ENTRY(watchdog_thread, args) { int now, nextsync = 0, action; double sec; 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); zbx_vector_ptr_create(&recipients); for (;;) { zbx_setproctitle("%s [pinging database]", get_process_type_string(process_type)); sec = zbx_time(); action = 0; if (ZBX_DB_OK != DBconnect(ZBX_DB_CONNECT_ONCE)) { zabbix_log(LOG_LEVEL_WARNING, "watchdog: database is down"); send_alerts(); action = 1; } else if (nextsync <= (now = (int)time(NULL))) { zbx_setproctitle("%s [syncing configuration]", get_process_type_string(process_type)); sync_config(); nextsync = now + CONFIG_CONFSYNCER_FREQUENCY; action = 2; } DBclose(); sec = zbx_time() - sec; if (1 == action) { zbx_setproctitle("%s [database is down, checking took " ZBX_FS_DBL " sec, idle %d sec]", get_process_type_string(process_type), sec, (int)DB_PING_FREQUENCY); } else if (2 == action) { zbx_setproctitle("%s [synced alerts config in " ZBX_FS_DBL " sec, idle %d sec]", get_process_type_string(process_type), sec, (int)DB_PING_FREQUENCY); } zbx_sleep_loop(DB_PING_FREQUENCY); } }
int main (int argc, char **argv) { DBconnect(); initialize(); printf("---------- asynchronous -----------\n"); asynchronous(); return 0; }
void zbx_on_exit() { zabbix_log(LOG_LEVEL_INFORMATION, "zbx_on_exit()"); #if !defined(_WINDOWS) int i = 0; if (threads != NULL) { for ( i = 1; i <= CONFIG_CONFSYNCER_FORKS + CONFIG_DATASENDER_FORKS + CONFIG_POLLER_FORKS + CONFIG_TRAPPERD_FORKS + CONFIG_PINGER_FORKS + CONFIG_HOUSEKEEPER_FORKS + CONFIG_UNREACHABLE_POLLER_FORKS + CONFIG_HTTPPOLLER_FORKS + CONFIG_DISCOVERER_FORKS + CONFIG_DBSYNCER_FORKS + CONFIG_IPMIPOLLER_FORKS; i ++) { if (threads[i]) { kill(threads[i], SIGTERM); threads[i] = (ZBX_THREAD_HANDLE)NULL; } } zbx_free(threads); } #endif /* not _WINDOWS */ #ifdef USE_PID_FILE daemon_stop(); #endif /* USE_PID_FILE */ free_metrics(); zbx_sleep(2); /* wait for all threads closing */ DBconnect(ZBX_DB_CONNECT_EXIT); free_database_cache(); DBclose(); /* zbx_mutex_destroy(&node_sync_access);*/ #ifdef HAVE_OPENIPMI free_ipmi_handler(); #endif zabbix_close_log(); #ifdef HAVE_SQLITE3 php_sem_remove(&sqlite_access); #endif /* HAVE_SQLITE3 */ zabbix_log(LOG_LEVEL_INFORMATION, "ZABBIX Proxy stopped. ZABBIX %s (revision %s).", ZABBIX_VERSION, ZABBIX_REVISION); exit(SUCCEED); }
ZBX_THREAD_ENTRY(taskmanager_thread, args) { static int cleanup_time = 0; double sec1, sec2; int tasks_num, sleeptime, nextcheck; 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_program_type_string(program_type), server_num, get_process_type_string(process_type), process_num); #if defined(HAVE_POLARSSL) || defined(HAVE_GNUTLS) || defined(HAVE_OPENSSL) zbx_tls_init_child(); #endif zbx_setproctitle("%s [connecting to the database]", get_process_type_string(process_type)); DBconnect(ZBX_DB_CONNECT_NORMAL); sec1 = zbx_time(); sleeptime = ZBX_TM_PROCESS_PERIOD - (int)sec1 % ZBX_TM_PROCESS_PERIOD; zbx_setproctitle("%s [started, idle %d sec]", get_process_type_string(process_type), sleeptime); for (;;) { zbx_sleep_loop(sleeptime); sec1 = zbx_time(); zbx_update_env(sec1); zbx_setproctitle("%s [processing tasks]", get_process_type_string(process_type)); tasks_num = tm_process_tasks((int)sec1); if (ZBX_TM_CLEANUP_PERIOD <= sec1 - cleanup_time) { tm_remove_old_tasks((int)sec1); cleanup_time = sec1; } sec2 = zbx_time(); nextcheck = (int)sec1 - (int)sec1 % ZBX_TM_PROCESS_PERIOD + ZBX_TM_PROCESS_PERIOD; if (0 > (sleeptime = nextcheck - (int)sec2)) sleeptime = 0; zbx_setproctitle("%s [processed %d task(s) in " ZBX_FS_DBL " sec, idle %d sec]", get_process_type_string(process_type), tasks_num, sec2 - sec1, sleeptime); } }
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); }
void zbx_on_exit() { zabbix_log(LOG_LEVEL_DEBUG, "zbx_on_exit() called"); 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(); free_configuration_cache(); DBclose(); #ifdef HAVE_OPENIPMI free_ipmi_handler(); #endif #ifdef HAVE_SQLITE3 zbx_remove_sqlite3_mutex(); #endif free_selfmon_collector(); zabbix_log(LOG_LEVEL_INFORMATION, "Zabbix Proxy stopped. Zabbix %s (revision %s).", ZABBIX_VERSION, ZABBIX_REVISION); zabbix_close_log(); exit(SUCCEED); }
/****************************************************************************** * * * Function: main_nodewatcher_loop * * * * Purpose: periodically calculates checks sum of config data * * * * Parameters: * * * * Return value: * * * * Author: Alexei Vladishev * * * * Comments: never returns * * * ******************************************************************************/ int main_nodewatcher_loop() { int start, end; int lastrun = 0; zabbix_log( LOG_LEVEL_DEBUG, "In main_nodeupdater_loop()"); for(;;) { start = time(NULL); zbx_setproctitle("connecting to the database"); zabbix_log( LOG_LEVEL_DEBUG, "Starting sync with nodes"); DBconnect(ZBX_DB_CONNECT_NORMAL); #if 0 if(lastrun + 120 < start) { DBbegin(); calculate_checksums(); compare_checksums(); update_checksums(); /* Send configuration changes to required nodes */ main_nodesender(); DBcommit(); lastrun = start; } #endif /* Send new events to master node */ main_eventsender(); /* Send new history data to master node */ main_historysender(); DBclose(); end = time(NULL); if(end-start<10) { zbx_setproctitle("sender [sleeping for %d seconds]", 10-(end-start)); zabbix_log( LOG_LEVEL_DEBUG, "Sleeping %d seconds", 10-(end-start)); sleep(10-(end-start)); } } }
void main_housekeeper_loop() { int d, now; set_child_signal_handler(); for (;;) { zabbix_log(LOG_LEVEL_WARNING, "Executing housekeeper"); now = time(NULL); zbx_setproctitle("%s [connecting to the database]", get_process_type_string(process_type)); DBconnect(ZBX_DB_CONNECT_NORMAL); /* Transaction is not required here. It causes timeouts under MySQL. */ /* DBbegin();*/ zbx_setproctitle("%s [removing old history]", get_process_type_string(process_type)); d = housekeeping_history_and_trends(now); zabbix_log(LOG_LEVEL_WARNING, "Deleted %d records from history and trends", d); zbx_setproctitle("%s [removing old history]", get_process_type_string(process_type)); housekeeping_process_log(now); zbx_setproctitle("%s [removing old events]", get_process_type_string(process_type)); housekeeping_events(now); zbx_setproctitle("%s [removing old alerts]", get_process_type_string(process_type)); housekeeping_alerts(now); zbx_setproctitle("%s [removing old sessions]", get_process_type_string(process_type)); housekeeping_sessions(now); /* Transaction is not required here. It causes timeouts under MySQL. */ /* DBcommit();*/ /* zbx_setproctitle("housekeeper [vacuuming database]");*/ /* DBvacuum();*/ DBclose(); zbx_sleep_loop(SEC_PER_HOUR * CONFIG_HOUSEKEEPING_FREQUENCY); } }
void main_housekeeper_loop(void) { int now, d_history_and_trends, d_cleanup, d_events, d_sessions, d_services, d_audit; double sec; for (;;) { zabbix_log(LOG_LEVEL_WARNING, "executing housekeeper"); now = time(NULL); zbx_setproctitle("%s [connecting to the database]", get_process_type_string(process_type)); DBconnect(ZBX_DB_CONNECT_NORMAL); DCconfig_get_config_hk(&hk_config); zbx_setproctitle("%s [removing old history and trends]", get_process_type_string(process_type)); sec = zbx_time(); d_history_and_trends = housekeeping_history_and_trends(now); zbx_setproctitle("%s [removing deleted items data]", get_process_type_string(process_type)); d_cleanup = housekeeping_cleanup(); zbx_setproctitle("%s [removing old events]", get_process_type_string(process_type)); d_events = housekeeping_events(now); zbx_setproctitle("%s [removing old sessions]", get_process_type_string(process_type)); d_sessions = housekeeping_sessions(now); zbx_setproctitle("%s [removing old service alarms]", get_process_type_string(process_type)); d_services = housekeeping_services(now); zbx_setproctitle("%s [removing old audit log items]", get_process_type_string(process_type)); d_audit = housekeeping_audit(now); sec = zbx_time() - sec; zabbix_log(LOG_LEVEL_WARNING, "%s [deleted %d hist/trends, %d items, %d events, %d sessions, %d alarms," " %d audit items in " ZBX_FS_DBL " sec, idle %d hour(s)]", get_process_type_string(process_type), d_history_and_trends, d_cleanup, d_events, d_sessions, d_services, d_audit, sec, CONFIG_HOUSEKEEPING_FREQUENCY); DBclose(); zbx_setproctitle("%s [deleted %d hist/trends, %d items, %d events, %d sessions, %d alarms, %d audit " "items in " ZBX_FS_DBL " sec, idle %d hour(s)]", get_process_type_string(process_type), d_history_and_trends, d_cleanup, d_events, d_sessions, d_services, d_audit, sec, CONFIG_HOUSEKEEPING_FREQUENCY); zbx_sleep_loop(CONFIG_HOUSEKEEPING_FREQUENCY * SEC_PER_HOUR); } }
int main (int argc, char** argv) { #ifdef HAVE_MYSQL DB_RESULT result; DB_ROW row; zbx_uint64_t id; char* value; hfs_function_value_t fun_val; int index = 0; static struct cfg_line cfg[] = { {"DBHost",&CONFIG_DBHOST,0,TYPE_STRING,PARM_OPT,0,0}, {"DBName",&CONFIG_DBNAME,0,TYPE_STRING,PARM_MAND,0,0}, {"DBUser",&CONFIG_DBUSER,0,TYPE_STRING,PARM_OPT,0,0}, {"DBPassword",&CONFIG_DBPASSWORD,0,TYPE_STRING,PARM_OPT,0,0}, {"DBSocket",&CONFIG_DBSOCKET,0,TYPE_STRING,PARM_OPT,0,0}, {"DBPort",&CONFIG_DBPORT,0,TYPE_INT,PARM_OPT,1024,65535}, {"ServerHistoryFSPath",&CONFIG_HFS_PATH,0,TYPE_STRING,PARM_OPT,0,0}, {"ServerSite",&CONFIG_SERVER_SITE,0,TYPE_STRING,PARM_OPT,0,0}, {0} }; /* read config file */ parse_cfg_file ("/etc/zabbix/zabbix_server.conf", cfg); /* connect to database */ DBconnect (ZBX_DB_CONNECT_EXIT); /* select all lastvalues from functions */ result = DBselect ("select functionid, lastvalue from functions"); /* update them in HFS */ while (row = DBfetch (result)) { ZBX_STR2UINT64 (id, row[0]); value = row[1]; if (HFS_convert_function_str2val (value, &fun_val)) if (HFS_save_function_value (CONFIG_HFS_PATH, CONFIG_SERVER_SITE, id, &fun_val)) printf ("Converted item %d of %lld. ID=%lld, val='%s', type=%d\n", index++, result->row_count, id, value, fun_val.type); else printf ("Item %d save failed. ID=%lld, val=%s\n", index++, id, value); else printf ("Item %d convert failed. ID=%lld, val=%s\n", index++, id, value); } DBclose (); #else printf ("This utility have no meaning on ztops\n"); #endif return 0; }
/****************************************************************************** * * * Function: main_discoverer_loop * * * * Purpose: periodically try to find new hosts and services * * * * Parameters: * * * * Return value: * * * * Author: Alexei Vladishev * * * * Comments: executes once per 30 seconds (hardcoded) * * * ******************************************************************************/ void main_discoverer_loop(int num) { int now; DB_RESULT result; DB_ROW row; DB_DRULE rule; zabbix_log( LOG_LEVEL_DEBUG, "In main_discoverer_loop(num:%d)", num); discoverer_num = num; DBconnect(ZBX_DB_CONNECT_NORMAL); for(;;) { zbx_setproctitle("Discoverer finding new hosts and services"); now=time(NULL); result = DBselect("select druleid,iprange,delay,nextcheck,name,status,siteid from drules where status=%d and nextcheck<=%d and " ZBX_SQL_MOD(druleid,%d) "=%d", DRULE_STATUS_MONITORED, now, CONFIG_DISCOVERER_FORKS, discoverer_num-1); while((row=DBfetch(result))) { memset(&rule, 0, sizeof(DB_DRULE)); ZBX_STR2UINT64(rule.druleid,row[0]); rule.iprange = row[1]; rule.delay = atoi(row[2]); rule.nextcheck = atoi(row[3]); rule.name = row[4]; rule.status = atoi(row[5]); ZBX_STR2UINT64(rule.siteid,row[6]); process_rule(&rule); } DBfree_result(result); zbx_setproctitle("Discoverer [sleeping for 30 sec]"); sleep(30); } DBclose(); }
/****************************************************************************** * * * Function: main_proxyconfig_loop * * * * Purpose: periodically request config data * * * * Parameters: * * * * Return value: * * * * Author: Alexander Vladishev * * * * Comments: never returns * * * ******************************************************************************/ void main_proxyconfig_loop() { zabbix_log(LOG_LEVEL_DEBUG, "In main_proxyconfig_loop()"); zbx_setproctitle("%s [connecting to the database]", get_process_type_string(process_type)); DBconnect(ZBX_DB_CONNECT_NORMAL); for (;;) { zbx_setproctitle("%s [loading configuration]", get_process_type_string(process_type)); process_configuration_sync(); zbx_sleep_loop(CONFIG_PROXYCONFIG_FREQUENCY); } }
/****************************************************************************** * * * Function: * * * * Purpose: * * * * Parameters: * * * * Return value: * * * * Comments: * * * ******************************************************************************/ ZBX_THREAD_ENTRY(monitor_thread, args) { assert(args); zabbix_log(LOG_LEVEL_INFORMATION, "jobarg_monitor #%d started [monitor]", ((zbx_thread_args_t *) args)->thread_num); zbx_free(args); DBconnect(ZBX_DB_CONNECT_NORMAL); CONFIG_SPAN_TIME = ja_schedule_load_span(); while (ZBX_IS_RUNNING()) { zbx_setproctitle("process monitor"); process_monitor(); zbx_sleep(CONFIG_JAMONITOR_INTERVAL); } zabbix_log(LOG_LEVEL_INFORMATION, "jobarg_monitor stopped"); zbx_thread_exit(0); }