From 4a2d1ae381304fe972eca90f268835819375734d Mon Sep 17 00:00:00 2001 From: cigamit Date: Sun, 25 Aug 2019 15:23:43 -0500 Subject: [PATCH] Correct segfault and other cleanup * Correct segfault due to use of wrong variable * Remove some whitespace * More declarations to headers * Add 'Device[]' to some log messages * PID[] instead of Process[] * Some block formatting * Remove an automake warning * Use stdbool.h --- Makefile.am | 1 + common.h | 2 +- poller.c | 51 ++++++++++++++---------- snmp.c | 10 ++--- spine.c | 112 +++++++++++++++++++++++++--------------------------- sql.c | 3 +- util.c | 10 +++-- 7 files changed, 100 insertions(+), 89 deletions(-) diff --git a/Makefile.am b/Makefile.am index dae0f134..fc99646e 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1,6 +1,7 @@ ## Makefile.am -- Process this file with automake to produce Makefile.in AUTOMAKE_OPTIONS = foreign +ACLOCAL_AMFLAGS = -I m4 spine_SOURCES = sql.c spine.c util.c snmp.c locks.c poller.c nft_popen.c php.c ping.c keywords.c error.c diff --git a/common.h b/common.h index 983c32ff..2df82ebd 100644 --- a/common.h +++ b/common.h @@ -92,7 +92,7 @@ #include #include #include - +#include #if HAVE_STDINT_H # include diff --git a/poller.c b/poller.c index 780b1ad0..cf6380a8 100644 --- a/poller.c +++ b/poller.c @@ -1685,9 +1685,9 @@ void poll_host(int host_id, int host_thread, int last_host_thread, int host_data /* record the polling time for the device */ poll_time = get_time_as_double() - poll_time; if (is_debug_device(host_id)) { - SPINE_LOG(("Device[%i] HT[%i] Total Time: %5.2g Seconds", host_id, host_thread, poll_time)); + SPINE_LOG(("Device[%i] HT[%i] Total Time: %0.2g Seconds", host_id, host_thread, poll_time)); } else { - SPINE_LOG_MEDIUM(("Device[%i] HT[%i] Total Time: %5.2g Seconds", host_id, host_thread, poll_time)); + SPINE_LOG_MEDIUM(("Device[%i] HT[%i] Total Time: %0.2g Seconds", host_id, host_thread, poll_time)); } /* record the total time for the host */ @@ -1768,70 +1768,79 @@ int is_multipart_output(char *result) { void get_system_information(host_t *host, MYSQL *mysql, int system) { char *poll_result; + SPINE_LOG_MEDIUM(("NOTE: Device[%d] Checking for System Information Update", host->id)); + if (set.mibs || system) { if (is_debug_device(host->id)) { - SPINE_LOG(("Updating Full System Information Table")); + SPINE_LOG(("NOTE: Device[%d] Updating Full System Information Table", host->id)); } else { - SPINE_LOG_MEDIUM(("Updating Full System Information Table")); + SPINE_LOG_MEDIUM(("NOTE: Device[%d] Updating Full System Information Table", host->id)); } - SPINE_LOG_DEVDBG(("poll_result = snmp_get(host, '.1.3.6.1.2.1.1.1.0');")); + SPINE_LOG_DEVDBG(("DEVDBG: Device[%d] poll_result = snmp_get(host, '.1.3.6.1.2.1.1.1.0');", host->id)); poll_result = snmp_get(host, ".1.3.6.1.2.1.1.1.0"); - SPINE_LOG_DEVDBG(("poll_result = snmp_get(host, '.1.3.6.1.2.1.1.1.0'); [complete]")); + SPINE_LOG_DEVDBG(("DEVDBG: Device[%d] poll_result = snmp_get(host, '.1.3.6.1.2.1.1.1.0'); [complete]", host->id)); + if (poll_result) { db_escape(mysql, host->snmp_sysDescr, sizeof(host->snmp_sysDescr), poll_result); + free(poll_result); } - free(poll_result); - SPINE_LOG_DEVDBG(("poll_result = snmp_get(host, '.1.3.6.1.2.1.1.2.0');")); + SPINE_LOG_DEVDBG(("DEVDBG: Device[%d] poll_result = snmp_get(host, '.1.3.6.1.2.1.1.2.0');", host->id)); poll_result = snmp_get(host, ".1.3.6.1.2.1.1.2.0"); - SPINE_LOG_DEVDBG(("poll_result = snmp_get(host, '.1.3.6.1.2.1.1.2.0'); [complete]")); + SPINE_LOG_DEVDBG(("DEVDBG: Device[%d] poll_result = snmp_get(host, '.1.3.6.1.2.1.1.2.0'); [complete]", host->id)); + if (poll_result) { db_escape(mysql, host->snmp_sysObjectID, sizeof(host->snmp_sysObjectID), poll_result); free(poll_result); } - SPINE_LOG_DEVDBG(("poll_result = snmp_get(host, '.1.3.6.1.2.1.1.3.0');")); + SPINE_LOG_DEVDBG(("DEVDBG: Device[%d] poll_result = snmp_get(host, '.1.3.6.1.2.1.1.3.0');", host->id)); poll_result = snmp_get(host, ".1.3.6.1.2.1.1.3.0"); - SPINE_LOG_DEVDBG(("poll_result = snmp_get(host, '.1.3.6.1.2.1.1.3.0'); [complete]")); + SPINE_LOG_DEVDBG(("DEVDGB: Device[%d] poll_result = snmp_get(host, '.1.3.6.1.2.1.1.3.0'); [complete]", host->id)); + if (poll_result) { host->snmp_sysUpTimeInstance = atoi(poll_result); free(poll_result); } - SPINE_LOG_DEVDBG(("poll_result = snmp_get(host, '.1.3.6.1.2.1.1.4.0');")); + SPINE_LOG_DEVDBG(("DEVDBG: Device [%d] poll_result = snmp_get(host, '.1.3.6.1.2.1.1.4.0');", host->id)); poll_result = snmp_get(host, ".1.3.6.1.2.1.1.4.0"); - SPINE_LOG_DEVDBG(("poll_result = snmp_get(host, '.1.3.6.1.2.1.1.4.0'); [complete]")); + SPINE_LOG_DEVDBG(("DEVDBG: Device [%d] poll_result = snmp_get(host, '.1.3.6.1.2.1.1.4.0'); [complete]", host->id)); + if (poll_result) { db_escape(mysql, host->snmp_sysContact, sizeof(host->snmp_sysContact), poll_result); free(poll_result); } - SPINE_LOG_DEVDBG(("poll_result = snmp_get(host, '.1.3.6.1.2.1.1.5.0');")); + SPINE_LOG_DEVDBG(("DEVDBG: Device [%d] poll_result = snmp_get(host, '.1.3.6.1.2.1.1.5.0');", host->id)); poll_result = snmp_get(host, ".1.3.6.1.2.1.1.5.0"); - SPINE_LOG_DEVDBG(("poll_result = snmp_get(host, '.1.3.6.1.2.1.1.5.0'); [complete]")); + SPINE_LOG_DEVDBG(("DEVDBG: Device [%d] poll_result = snmp_get(host, '.1.3.6.1.2.1.1.5.0'); [complete]", host->id)); + if (poll_result) { db_escape(mysql, host->snmp_sysName, sizeof(host->snmp_sysName), poll_result); free(poll_result); } - SPINE_LOG_DEVDBG(("poll_result = snmp_get(host, '.1.3.6.1.2.1.1.6.0');")); + SPINE_LOG_DEVDBG(("DEVDBG: Device [%d] poll_result = snmp_get(host, '.1.3.6.1.2.1.1.6.0');", host->id)); poll_result = snmp_get(host, ".1.3.6.1.2.1.1.6.0"); - SPINE_LOG_DEVDBG(("poll_result = snmp_get(host, '.1.3.6.1.2.1.1.6.0'); [complete]")); + SPINE_LOG_DEVDBG(("DEVDBG: Device [%d] poll_result = snmp_get(host, '.1.3.6.1.2.1.1.6.0'); [complete]", host->id)); + if (poll_result) { db_escape(mysql, host->snmp_sysLocation, sizeof(host->snmp_sysLocation), poll_result); free(poll_result); } } else { if (is_debug_device(host->id)) { - SPINE_LOG(("Updating Short System Information Table")); + SPINE_LOG(("NOTE: Device[%d] Updating Short System Information Table", host->id)); } else { - SPINE_LOG_MEDIUM(("Updating Short System Information Table")); + SPINE_LOG_MEDIUM(("NOTE: Device[%d] Updating Short System Information Table", host->id)); } - SPINE_LOG_DEVDBG(("poll_result = snmp_get(host, '.1.3.6.1.2.1.1.3.0');")); + SPINE_LOG_DEVDBG(("DEVDBG: Device [%d] poll_result = snmp_get(host, '.1.3.6.1.2.1.1.3.0');", host->id)); poll_result = snmp_get(host, ".1.3.6.1.2.1.1.3.0"); - SPINE_LOG_DEVDBG(("poll_result = snmp_get(host, '.1.3.6.1.2.1.1.3.0'); [complete]")); + SPINE_LOG_DEVDBG(("DEVDBG: Device [%d] poll_result = snmp_get(host, '.1.3.6.1.2.1.1.3.0'); [complete]", host->id)); + if (poll_result) { host->snmp_sysUpTimeInstance = atoi(poll_result); free(poll_result); diff --git a/snmp.c b/snmp.c index 0f1afa36..a62a63d8 100644 --- a/snmp.c +++ b/snmp.c @@ -115,9 +115,9 @@ void snmp_spine_close(void) { * */ void *snmp_host_init(int host_id, char *hostname, int snmp_version, char *snmp_community, - char *snmp_username, char *snmp_password, char *snmp_auth_protocol, - char *snmp_priv_passphrase, char *snmp_priv_protocol, - char *snmp_context, char *snmp_engine_id, int snmp_port, int snmp_timeout) { + char *snmp_username, char *snmp_password, char *snmp_auth_protocol, + char *snmp_priv_passphrase, char *snmp_priv_protocol, + char *snmp_context, char *snmp_engine_id, int snmp_port, int snmp_timeout) { void *sessp = NULL; struct snmp_session session; @@ -187,8 +187,8 @@ void *snmp_host_init(int host_id, char *hostname, int snmp_version, char *snmp_c session.peername = hostnameport; session.retries = set.snmp_retries; session.remote_port = snmp_port; - //session.timeout = (snmp_timeout * 1000); /* net-snmp likes microseconds */ session.timeout = (snmp_timeout * 1000); /* net-snmp likes microseconds */ + SPINE_LOG_MEDIUM(("Device[%i] INFO: SNMP Device '%s' has timeout %ld (%d), retries %d", host_id, hostname, session.timeout, snmp_timeout, session.retries)); if ((snmp_version == 2) || (snmp_version == 1)) { @@ -346,7 +346,7 @@ char *snmp_get(host_t *current_host, char *snmp_oid) { anOID_len = MAX_OID_LEN; SPINE_LOG_DEVDBG(("Device[%i] WARNING: snmp_pdu_create(%s)", current_host->id, snmp_oid)); - pdu = snmp_pdu_create(SNMP_MSG_GET); + pdu = snmp_pdu_create(SNMP_MSG_GET); SPINE_LOG_DEVDBG(("Device[%i] WARNING: snmp_pdu_create(%s) [complete]", current_host->id, snmp_oid)); SPINE_LOG_DEVDBG(("Device[%i] WARNING: snmp_parse_oid(%s)", current_host->id, snmp_oid)); diff --git a/spine.c b/spine.c index 007ab581..966d292a 100644 --- a/spine.c +++ b/spine.c @@ -225,6 +225,9 @@ int main(int argc, char *argv[]) { int total_items = 0; int change_host = TRUE; int current_thread; + int threads_final = 0; + int threads_missing = -1; + int threads_count; UNUSED_PARAMETER(argc); /* we operate strictly with argv */ @@ -316,8 +319,7 @@ int main(int argc, char *argv[]) { if (opt) *opt++ = '\0'; - if (STRMATCH(arg, "-f") || - STRMATCH(arg, "--first")) { + if (STRMATCH(arg, "-f") || STRMATCH(arg, "--first")) { if (HOSTID_DEFINED(set.start_host_id)) { die("ERROR: %s can only be used once", arg); } @@ -329,8 +331,7 @@ int main(int argc, char *argv[]) { } } - else if (STRMATCH(arg, "-l") || - STRIMATCH(arg, "--last")) { + else if (STRMATCH(arg, "-l") || STRIMATCH(arg, "--last")) { if (HOSTID_DEFINED(set.end_host_id)) { die("ERROR: %s can only be used once", arg); } @@ -342,38 +343,27 @@ int main(int argc, char *argv[]) { } } - else if (STRMATCH(arg, "-p") || - STRIMATCH(arg, "--poller")) { + else if (STRMATCH(arg, "-p") || STRIMATCH(arg, "--poller")) { set.poller_id = atoi(getarg(opt, &argv)); } - else if (STRMATCH(arg, "-N") || - STRIMATCH(arg, "--mode")) { - + else if (STRMATCH(arg, "-N") || STRIMATCH(arg, "--mode")) { if (STRIMATCH(getarg(opt, &argv), "online")) { set.mode = REMOTE_ONLINE; - } - - else if (STRIMATCH(getarg(opt, &argv), "offline")) { + } else if (STRIMATCH(getarg(opt, &argv), "offline")) { set.mode = REMOTE_OFFLINE; - } - - else if (STRIMATCH(getarg(opt, &argv), "recovery")) { + } else if (STRIMATCH(getarg(opt, &argv), "recovery")) { set.mode = REMOTE_RECOVERY; - } - - else { + } else { die("ERROR: invalid polling mode '%s' specified", opt); } } - else if (STRMATCH(arg, "-H") || - STRIMATCH(arg, "--hostlist")) { + else if (STRMATCH(arg, "-H") || STRIMATCH(arg, "--hostlist")) { snprintf(set.host_id_list, BIG_BUFSIZE, "%s", getarg(opt, &argv)); } - else if (STRMATCH(arg, "-M") || - STRMATCH(arg, "--mibs")) { + else if (STRMATCH(arg, "-M") || STRMATCH(arg, "--mibs")) { set.mibs = 1; } @@ -389,10 +379,9 @@ int main(int argc, char *argv[]) { exit(EXIT_SUCCESS); } - else if (STRMATCH(arg, "-O") || - STRIMATCH(arg, "--option")) { - char *setting = getarg(opt, &argv); - char *value = strchr(setting, ':'); + else if (STRMATCH(arg, "-O") || STRIMATCH(arg, "--option")) { + char *setting = getarg(opt, &argv); + char *value = strchr(setting, ':'); if (*value) { *value++ = '\0'; @@ -403,34 +392,27 @@ int main(int argc, char *argv[]) { set_option(setting, value); } - else if (STRMATCH(arg, "-R") || - STRMATCH(arg, "--readonly") || - STRMATCH(arg, "--read-only")) { + else if (STRMATCH(arg, "-R") || STRMATCH(arg, "--readonly") || STRMATCH(arg, "--read-only")) { set.SQL_readonly = TRUE; } - else if (STRMATCH(arg, "-C") || - STRMATCH(arg, "--conf")) { + else if (STRMATCH(arg, "-C") || STRMATCH(arg, "--conf")) { conf_file = strdup(getarg(opt, &argv)); } - else if (STRMATCH(arg, "-S") || - STRMATCH(arg, "--stdout")) { + else if (STRMATCH(arg, "-S") || STRMATCH(arg, "--stdout")) { set_option("log_destination", "STDOUT"); } - else if (STRMATCH(arg, "-L") || - STRMATCH(arg, "--log")) { + else if (STRMATCH(arg, "-L") || STRMATCH(arg, "--log")) { set_option("log_destination", getarg(opt, &argv)); } - else if (STRMATCH(arg, "-V") || - STRMATCH(arg, "--verbosity")) { + else if (STRMATCH(arg, "-V") || STRMATCH(arg, "--verbosity")) { set_option("log_verbosity", getarg(opt, &argv)); } - else if (STRMATCH(arg, "--snmponly") || - STRMATCH(arg, "--snmp-only")) { + else if (STRMATCH(arg, "--snmponly") || STRMATCH(arg, "--snmp-only")) { set.snmponly = TRUE; } @@ -623,11 +605,7 @@ int main(int argc, char *argv[]) { qp += sprintf(qp, " ORDER BY polling_time DESC"); result = db_query(&mysql, LOCAL, querybuf); - if (set.poller_id == 0) { - num_rows = mysql_num_rows(result) + 1; /* add 1 for host = 0 */ - } else { - num_rows = mysql_num_rows(result); /* pollerid 0 takes care of not host based data sources */ - } + num_rows = mysql_num_rows(result); /* pollerid 0 takes care of not host based data sources */ if (!(threads = (pthread_t *)malloc(num_rows * sizeof(pthread_t)))) { die("ERROR: Fatal malloc error: spine.c threads!"); @@ -726,12 +704,15 @@ int main(int argc, char *argv[]) { db_free_result(tresult); if (host_time) free(host_time); + host_time = get_host_poll_time(); host_time_double = get_time_as_double(); } } else { - itemsPT = 0; + itemsPT = 0; + if (host_time) free(host_time); + host_time = get_host_poll_time(); host_time_double = get_time_as_double(); } @@ -749,6 +730,7 @@ int main(int argc, char *argv[]) { poller_details->host_time_double = host_time_double; poller_details->thread_init_sem = &thread_init_sem; poller_details->complete = FALSE; + details[device_counter] = poller_details; retry1: @@ -787,8 +769,14 @@ int main(int argc, char *argv[]) { thread_mutex_unlock(LOCK_PEND); sem_post(&thread_init_sem); - SPINE_LOG_DEVDBG(("INFO: DTS: device = %d, host_id = %d, host_thread = %d, last_host_thread = %d, host_data_ids = %d, complete = %d", - device_counter-1, poller_details->host_id, poller_details->host_thread, poller_details->last_host_thread, poller_details->host_data_ids, poller_details->complete)); + SPINE_LOG_DEVDBG(("INFO: DTS: device = %d, host_id = %d, host_thread = %d," + " last_host_thread = %d, host_data_ids = %d, complete = %d", + device_counter-1, + poller_details->host_id, + poller_details->host_thread, + poller_details->last_host_thread, + poller_details->host_data_ids, + poller_details->complete)); break; case EAGAIN: @@ -805,8 +793,9 @@ int main(int argc, char *argv[]) { } /* Restore thread initialization semaphore if thread creation failed */ - if (thread_status) + if (thread_status) { sem_post(&thread_init_sem); + } } /* wait for all threads to 'complete' @@ -826,18 +815,19 @@ int main(int argc, char *argv[]) { thread_mutex_unlock(LOCK_PEND); - sleep(1); + usleep(100000); } sem_getvalue(&active_threads, &a_threads_value); - int threads_final = set.threads - a_threads_value; + + threads_final = set.threads - a_threads_value; if (threads_final) { SPINE_LOG_HIGH(("SPINE: The final count of Threads is %i", threads_final)); - int threads_missing = -1; - int threads_count; + for (threads_count = 0; threads_count < num_rows; threads_count++) { poller_thread_t* det = details[threads_count]; + if (threads_missing == -1 && det == NULL) { threads_missing = threads_count; } @@ -848,8 +838,15 @@ int main(int argc, char *argv[]) { det->host_id, det->host_data_ids * (det->host_thread - 1), det->host_data_ids * (det->host_thread))); - SPINE_LOG_DEVDBG(("INFO: DTF: device = %d, host_id = %d, host_thread = %d, last_host_thread = %d, host_data_ids = %d, complete = %d", - threads_count, det->host_id, det->host_thread, det->last_host_thread, det->host_data_ids, det->complete)); + + SPINE_LOG_DEVDBG(("INFO: DTF: device = %d, host_id = %d, host_thread = %d," + " last_host_thread = %d, host_data_ids = %d, complete = %d", + threads_count, + det->host_id, + det->host_thread, + det->last_host_thread, + det->host_data_ids, + det->complete)); } } @@ -896,10 +893,9 @@ int main(int argc, char *argv[]) { SPINE_LOG_DEBUG(("DEBUG: PHP Script Server Pipes Closed")); /* free malloc'd variables */ - int mrow; - for (mrow = 0; i < num_rows; mrow++) { - if (details[mrow] != NULL) { - free(details[mrow]); + for (i = 0; i < num_rows; i++) { + if (details[i] != NULL) { + free(details[i]); } } diff --git a/sql.c b/sql.c index 2738077c..4584c791 100644 --- a/sql.c +++ b/sql.c @@ -187,6 +187,7 @@ void db_connect(int type, MYSQL *mysql) { int options_error; int success; int error; + bool reconnect; MYSQL *connect_error; char *hostname; char *socket = NULL; @@ -229,7 +230,7 @@ void db_connect(int type, MYSQL *mysql) { timeout = 5; rtimeout = 10; wtimeout = 20; - _Bool reconnect = 1; + reconnect = 1; mysql_init(mysql); diff --git a/util.c b/util.c index 1d511762..68a546b2 100644 --- a/util.c +++ b/util.c @@ -446,18 +446,22 @@ void read_config_options() { SPINE_LOG_DEBUG(("DEBUG: The ping_retries variable is %i", set.ping_retries)); /* set ping_timeout */ - if ( (res = getsetting(&mysql, LOCAL, "ping_timeout")) != 0) { + if ((res = getsetting(&mysql, LOCAL, "ping_timeout")) != 0) { set.ping_timeout = atoi(res); free((char *)res); + } else { + set.ping_timeout = 400; } /* log the ping_timeout variable */ SPINE_LOG_DEBUG(("DEBUG: The ping_timeout variable is %i", set.ping_timeout)); /* set snmp_retries */ - if ( (res = getsetting(&mysql, LOCAL, "snmp_retries")) != 0) { + if ((res = getsetting(&mysql, LOCAL, "snmp_retries")) != 0) { set.snmp_retries = atoi(res); free((char *)res); + } else { + set.snmp_retries = 3; } /* log the snmp_retries variable */ @@ -1098,7 +1102,7 @@ int spine_log(const char *format, ...) { } /* log message prefix */ - snprintf(logprefix, SMALL_BUFSIZE, "SPINE: Poller[%i] Process[%i] ", set.poller_id, getpid()); + snprintf(logprefix, SMALL_BUFSIZE, "SPINE: Poller[%i] PID[%i] ", set.poller_id, getpid()); /* get time for poller_output table */ nowbin = time(&nowbin);