MYSQL_RES *db_query(MYSQL *mysql, char *query) { MYSQL_RES *mysql_res; int return_code; int retries; int error; thread_mutex_lock(LOCK_MYSQL); retries = 0; error = FALSE; while (retries < 3) { return_code = mysql_query(mysql, query); if (return_code) { cacti_log("MYSQL: ERROR encountered while attempting to retrieve records from query\n"); error = TRUE; } else { mysql_res = mysql_store_result(mysql); error = FALSE; break; } usleep(1000); retries++; } thread_mutex_unlock(LOCK_MYSQL); if (error) { cacti_log("MYSQL: ERROR could not obtain results from database, exiting\n"); exit_cactid(); } return mysql_res; }
int db_connect(char *database, MYSQL *mysql) { char logmessage[LOGSIZE]; int tries; int result; char *hostname; char *socket; if ((hostname = strdup(set.dbhost)) == NULL) { snprintf(logmessage, LOGSIZE-1, "ERROR: malloc(): strdup() failed\n"); cacti_log(logmessage); return (FALSE); } if ((socket = strstr(hostname,":"))) *socket++ = 0x0; /* initialalize my variables */ tries = 10; result = 0; if (set.verbose == POLLER_VERBOSITY_DEBUG) { snprintf(logmessage, LOGSIZE-1, "MYSQL: Connecting to MySQL database '%s' on '%s'...\n", database, set.dbhost); cacti_log(logmessage); } thread_mutex_lock(LOCK_MYSQL); mysql_init(mysql); while (tries > 0){ tries--; if (!mysql_real_connect(mysql, hostname, set.dbuser, set.dbpass, database, set.dbport, socket, 0)) { if (set.verbose == POLLER_VERBOSITY_DEBUG) { snprintf(logmessage, LOGSIZE-1, "MYSQL: Connection Failed: %s\n", mysql_error(mysql)); cacti_log(logmessage); } result = 1; }else{ tries = 0; result = 0; if (set.verbose == POLLER_VERBOSITY_DEBUG) { snprintf(logmessage, LOGSIZE-1, "MYSQL: Connected to MySQL database '%s' on '%s'...\n", database, set.dbhost); cacti_log(logmessage); } } } free(hostname); if (result == 1){ snprintf(logmessage, LOGSIZE-1, "MYSQL: Connection Failed: %s\n", mysql_error(mysql)); cacti_log(logmessage); thread_mutex_unlock(LOCK_MYSQL); exit_cactid(); }else{ thread_mutex_unlock(LOCK_MYSQL); return (0); } }
MYSQL_RES *db_query(MYSQL *mysql, char *query) { MYSQL_RES *mysql_res; int return_code; thread_mutex_lock(LOCK_MYSQL); return_code = mysql_query(mysql, query); if (return_code) { cacti_log("MYSQL: ERROR encountered while attempting to retrieve records from query\n"); thread_mutex_unlock(LOCK_MYSQL); exit_cactid(); }else{ mysql_res = mysql_store_result(mysql); thread_mutex_unlock(LOCK_MYSQL); } return mysql_res; }