From: Florian G. <re...@us...> - 2007-04-19 12:38:29
|
Update of /cvsroot/perfparse/_perfparse/db_tools In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv19564/db_tools Modified Files: convert.c Log Message: Fixed mem leaks Index: convert.c =================================================================== RCS file: /cvsroot/perfparse/_perfparse/db_tools/convert.c,v retrieving revision 1.32 retrieving revision 1.33 diff -C2 -d -r1.32 -r1.33 *** convert.c 15 Apr 2007 23:39:52 -0000 1.32 --- convert.c 19 Apr 2007 12:38:26 -0000 1.33 *************** *** 52,55 **** --- 52,57 ---- void pp_mysql_convert_sql_failure(char*f,int l, const char*str, const char *error); MYSQL_RES *dbtool_mysql_query(const char *sql, MYSQL *p_tmp_db_mysql, MYSQL_RES *p_tmp_query_result); + MYSQL_RES *dbtool_mysql_query_cursor(const char *sql, MYSQL *p_tmp_db_mysql, MYSQL_RES *p_tmp_query_result); + MYSQL_RES *dbtool_mysql_query_internal(const char *sql, MYSQL *p_tmp_db_mysql, MYSQL_RES *p_tmp_query_result, int cursor); void dbtool_freeResult(MYSQL_RES *p_tmp_query_result); int dbtool_rows(MYSQL_RES *p_tmp_query_result); *************** *** 80,83 **** --- 82,96 ---- } + void g_hash_table_insert_copy(GHashTable *hashCache,char *tmp_string,int tmp_int) { + // mallocs space for string and value (int) and inserts it into the table given + int *pInt=malloc(sizeof(int)); + char *pStr=malloc(sizeof(char)*(strlen(tmp_string)+1)); + *pInt=tmp_int; + strncpy(pStr,tmp_string,strlen(tmp_string)+1); + g_hash_table_insert(hashCache,(gpointer) pStr,pInt); + } + + + int get_policyId_from_name(const char *policy_name, MYSQL *p_db_mysql_new) { int *pPid; *************** *** 106,112 **** dbtool_freeResult(query_result_new); // save result in cache ! pPid=malloc(sizeof(int)); ! *pPid=Pid; ! g_hash_table_insert(policiesCache,(gpointer) policy_name,pPid); #ifdef DEBUG printw("\nPolicyId Cache Miss: %s -> %d\n",policy_name,Pid);refresh(); --- 119,123 ---- dbtool_freeResult(query_result_new); // save result in cache ! g_hash_table_insert_copy(policiesCache,policy_name,Pid); #ifdef DEBUG printw("\nPolicyId Cache Miss: %s -> %d\n",policy_name,Pid);refresh(); *************** *** 142,148 **** dbtool_freeResult(query_result_new); // save result in cache ! pGid=malloc(sizeof(int)); ! *pGid=Gid; ! g_hash_table_insert(groupsCache,(gpointer) group_name,pGid); #ifdef DEBUG printw("\nGroupId Cache Miss: %s -> %d\n",group_name,Gid);refresh(); --- 153,157 ---- dbtool_freeResult(query_result_new); // save result in cache ! g_hash_table_insert_copy(groupsCache,group_name,Gid); #ifdef DEBUG printw("\nGroupId Cache Miss: %s -> %d\n",group_name,Gid);refresh(); *************** *** 178,184 **** dbtool_freeResult(query_result_new); // save result in cache ! pHid=malloc(sizeof(int)); ! *pHid=Hid; ! g_hash_table_insert(hostsCache,(gpointer) host_name,pHid); #ifdef DEBUG printw("\nHostId Cache Miss: %s -> %d\n",host_name,Hid);refresh(); --- 187,191 ---- dbtool_freeResult(query_result_new); // save result in cache ! g_hash_table_insert_copy(hostsCache,host_name,Hid); #ifdef DEBUG printw("\nHostId Cache Miss: %s -> %d\n",host_name,Hid);refresh(); *************** *** 215,218 **** --- 222,228 ---- "host_id=%d and service_description='%s'", Hid,service_desc); + #ifdef DEBUG + printw("debug: query: %s\n",gsNDBSql->str); + #endif query_result_new=dbtool_mysql_query(gsNDBSql->str, p_db_mysql_new, query_result_new); if((result_row_new=mysql_fetch_row(query_result_new))) { *************** *** 225,231 **** dbtool_freeResult(query_result_new); // save result in cache ! pSid=malloc(sizeof(int)); ! *pSid=Sid; ! g_hash_table_insert(servicesCache,(gpointer) gsCombined->str,pSid); #ifdef DEBUG printw("\nServiceId Cache Miss: %s -> %d\n",gsCombined->str,Sid);refresh(); --- 235,239 ---- dbtool_freeResult(query_result_new); // save result in cache ! g_hash_table_insert_copy(servicesCache,gsCombined->str,Sid); #ifdef DEBUG printw("\nServiceId Cache Miss: %s -> %d\n",gsCombined->str,Sid);refresh(); *************** *** 274,280 **** dbtool_freeResult(query_result_new); // save result in cache ! pMid=malloc(sizeof(int)); ! *pMid=Mid; ! g_hash_table_insert(metricCache,(gpointer) gsCombined->str,pMid); #ifdef DEBUG printw("\nMetricId Cache Miss: %s -> %d\n",gsCombined->str,Mid);refresh(); --- 282,286 ---- dbtool_freeResult(query_result_new); // save result in cache ! g_hash_table_insert_copy(metricCache,gsCombined->str,Mid); #ifdef DEBUG printw("\nMetricId Cache Miss: %s -> %d\n",gsCombined->str,Mid);refresh(); *************** *** 757,784 **** printw(_("Copying entry %d/%d"),i,iODBnumrows); refresh(); Sid=get_serviceId_from_desc(result_row_old[0],result_row_old[1],&db_mysql_new); ! g_string_printf(gsNDBSql, "insert ignore into perfdata_service_metric" ! "(service_id,metric,unit,notes,value_max," ! "value_min,is_deleted"); ! g_string_printf(gsNDBSql2," values(%d,'%s','%s','%s',%s,%s,%s", ! Sid,result_row_old[2],result_row_old[3],result_row_old[4], ! result_row_old[5],result_row_old[6],result_row_old[11]); ! // get bin delete policy id if template ! if(strncmp(result_row_old[10],"template",4)==0) { ! iBinPolicyId=get_policyId_from_name(result_row_old[9],&db_mysql_new); ! g_string_append_printf(gsNDBSql,",bin_delete_policy_id"); ! g_string_append_printf(gsNDBSql2,",%d",iBinPolicyId); ! } else { ! if(result_row_old[8]!=NULL) { ! iBinPolicyInd=atoi(result_row_old[8])*86400; ! g_string_append_printf(gsNDBSql,",bin_delete_policy_individual"); ! g_string_append_printf(gsNDBSql2,",%d",iBinPolicyInd); ! } ! } ! g_string_append_printf(gsNDBSql2,")"); ! g_string_append_printf(gsNDBSql,")%s",gsNDBSql2->str); ! // printw("debug: Sql Statement: %s\n",gsNDBSql->str);refresh(); ! dbtool_mysql_query(gsNDBSql->str, &db_mysql_new, query_result_new); } printw(_("\ndone\n")); refresh(); --- 763,792 ---- printw(_("Copying entry %d/%d"),i,iODBnumrows); refresh(); Sid=get_serviceId_from_desc(result_row_old[0],result_row_old[1],&db_mysql_new); ! if(Sid!=0) { // stale entry ! g_string_printf(gsNDBSql, "insert ignore into perfdata_service_metric" ! "(service_id,metric,unit,notes,value_max," ! "value_min,is_deleted"); ! g_string_printf(gsNDBSql2," values(%d,'%s','%s','%s',%s,%s,%s", ! Sid,result_row_old[2],result_row_old[3],result_row_old[4], ! result_row_old[5],result_row_old[6],result_row_old[11]); ! // get bin delete policy id if template ! if(strncmp(result_row_old[10],"template",4)==0) { ! iBinPolicyId=get_policyId_from_name(result_row_old[9],&db_mysql_new); ! g_string_append_printf(gsNDBSql,",bin_delete_policy_id"); ! g_string_append_printf(gsNDBSql2,",%d",iBinPolicyId); ! } else { ! if(result_row_old[8]!=NULL) { ! iBinPolicyInd=atoi(result_row_old[8])*86400; ! g_string_append_printf(gsNDBSql,",bin_delete_policy_individual"); ! g_string_append_printf(gsNDBSql2,",%d",iBinPolicyInd); ! } ! } ! g_string_append_printf(gsNDBSql2,")"); ! g_string_append_printf(gsNDBSql,")%s",gsNDBSql2->str); ! // printw("debug: Sql Statement: %s\n",gsNDBSql->str);refresh(); ! dbtool_mysql_query(gsNDBSql->str, &db_mysql_new, query_result_new); ! } } printw(_("\ndone\n")); refresh(); *************** *** 790,853 **** printw(_("(Step %d of %d)\n"),iStep,iNumSteps); printw(_("Copying data of perfdata_service_bin table.\n")); ! printw(_("Since this table can be really huge i will copy the data in chunks to avoid table locks.\n")); ! printw(_("Every chunk will consist of the data collected on one day starting with the 1st entry.\n")); ! printw(_("Everytime when data of 30 days are inserted i will make a 15 sec. break to give the database a chance to finish outstanding transactions.\n")); refresh(); ! // get date of oldest entry ! g_string_printf(gsODBSql, "select UNIX_TIMESTAMP(MIN(ctime)) from perfdata_service_bin"); - int iOldestEntry; // oldest Entry not yet copied query_result_old=dbtool_mysql_query(gsODBSql->str, &db_mysql_old, query_result_old); - if((result_row_old=mysql_fetch_row(query_result_old))) { - iOldestEntry=atoi(result_row_old[0]); - } else { - iOldestEntry=time(NULL); - } ! int donedays=0; ! while (iOldestEntry < time(NULL)) { ! g_string_printf(gsODBSql, "select host_name, service_description,metric," ! "UNIX_TIMESTAMP(ctime),value,warn,critical," ! "state from perfdata_service_bin where ctime " ! "between FROM_UNIXTIME(%d) and FROM_UNIXTIME(%d)", ! iOldestEntry,iOldestEntry+24*60*60); ! query_result_old=dbtool_mysql_query(gsODBSql->str, &db_mysql_old,query_result_old); ! iODBnumrows=dbtool_rows(query_result_old); ! printw(_("Copying data from date: %s Affected rows: %d\n"), ! ctime(&iOldestEntry),iODBnumrows); ! getyx(stdscr,y,x); ! i=0; ! while ((result_row_old=mysql_fetch_row(query_result_old))) { ! i++; ! int Mid; ! move(y,x); ! piODBlengths=mysql_fetch_lengths(query_result_old); ! printw(_("Copying entry %d/%d"),i,iODBnumrows); refresh(); ! Mid=get_metricId_from_text(result_row_old[0],result_row_old[1],result_row_old[2],&db_mysql_new); ! if(Mid!=FALSE) { // stale data ! // TODO: check if ctime is changed everywhere from DATETIME to int (unix timestamp) ! g_string_printf(gsNDBSql, "insert ignore into perfdata_service_bin" ! "(metric_id,ctime,value,warn,critical,state) " ! "values (%d,%s,%s,%s,%s,%s)", ! Mid,result_row_old[3],result_row_old[4], ! result_row_old[5],result_row_old[6], ! result_row_old[7]); ! ! // printw("debug: Sql Statement: %s\n",gsNDBSql->str);refresh(); ! dbtool_mysql_query(gsNDBSql->str, &db_mysql_new, query_result_new); } } ! iOldestEntry+=3600*24; ! donedays++; ! if(donedays%30==0) { printw(_("Sleeping 15 Sec.")); sleep(15); } - move(x,y-2); } move(x,y); --- 798,879 ---- printw(_("(Step %d of %d)\n"),iStep,iNumSteps); printw(_("Copying data of perfdata_service_bin table.\n")); ! printw(_("Since this table can be really huge i will copy the data in chunks.\n")); ! printw(_("Every 5 minutes i will make a 15 sec. break to give the database a chance to finish outstanding transactions.\n")); refresh(); ! // Set transaction level for dirty reads ! g_string_printf(gsODBSql, "SET SESSION TRANSACTION ISOLATION LEVEL READ UNCOMMITTED"); query_result_old=dbtool_mysql_query(gsODBSql->str, &db_mysql_old, query_result_old); ! // get num of rows in extra query since mysql_num_rows does not work with cursors ! printw(_("Calculating number of records to copy ... ")); refresh(); ! g_string_printf(gsODBSql, "select count(*) from perfdata_service_bin"); ! query_result_old=dbtool_mysql_query(gsODBSql->str, &db_mysql_old, query_result_old); ! result_row_old=mysql_fetch_row(query_result_old); ! iODBnumrows=atoi(result_row_old[0]); ! printw(_("%d records.\n\n"),iODBnumrows); ! ! int starttime=time(NULL); ! int acttime=time(NULL); ! g_string_printf(gsODBSql, "select host_name, service_description,metric," ! "UNIX_TIMESTAMP(ctime),value,warn,critical," ! "state from perfdata_service_bin"); ! query_result_old=dbtool_mysql_query_cursor(gsODBSql->str, &db_mysql_old,query_result_old); ! int Mid; ! i=0; ! getyx(stdscr,y,x); ! while ((result_row_old=mysql_fetch_row(query_result_old))) { ! if(acttime != time(NULL)) { // only update screen every second ! acttime = time(NULL); ! move(y-1,x); ! int h=0, min=0, sec=0; ! int eh=0, emin=0, esec=0; ! esec=acttime-starttime; ! sec=round(iODBnumrows/i*esec); ! if(sec>60) { ! min=floor(sec/60); ! sec=sec%60; ! } ! if(esec>60) { ! emin=floor(esec/60); ! esec=esec%60; ! } ! if(min>60) { ! h=floor(min/60); ! min=min%60; } + if(emin>60) { + eh=floor(emin/60); + emin=emin%60; + } + + printw(_("Copying entry: %d/%d (%.2f %%) Elapsed: %d h, %d min, %d sec Est. remaining: %d h, %d min, %d sec\n"), i,iODBnumrows,(float) i/iODBnumrows,eh,emin,esec,h,min,sec); + refresh(); } ! i++; ! Mid=get_metricId_from_text(result_row_old[0],result_row_old[1],result_row_old[2],&db_mysql_new); ! if(Mid!=FALSE) { // stale data ! // TODO: check if ctime is changed everywhere from DATETIME to int (unix timestamp) ! g_string_printf(gsNDBSql, "insert ignore into perfdata_service_bin" ! "(metric_id,ctime,value,warn,critical,state) " ! "values (%d,%s,%s,%s,%s,%s)", ! Mid,result_row_old[3],result_row_old[4], ! result_row_old[5],result_row_old[6], ! result_row_old[7]); ! ! #ifdef DEBUG ! printw("debug: Sql Statement: %s\n",gsNDBSql->str);refresh(); ! #endif ! dbtool_mysql_query(gsNDBSql->str, &db_mysql_new, query_result_new); ! } ! if((starttime-time(NULL))%600==0) { printw(_("Sleeping 15 Sec.")); sleep(15); } } move(x,y); *************** *** 946,951 **** --- 972,989 ---- } + MYSQL_RES *dbtool_mysql_query(const char *sql, MYSQL *p_tmp_db_mysql, MYSQL_RES *p_tmp_query_result) { + return(dbtool_mysql_query_internal(sql,p_tmp_db_mysql,p_tmp_query_result,FALSE)); + } + + MYSQL_RES *dbtool_mysql_query_cursor(const char *sql, MYSQL *p_tmp_db_mysql, MYSQL_RES *p_tmp_query_result) + { + return(dbtool_mysql_query_internal(sql,p_tmp_db_mysql,p_tmp_query_result,TRUE)); + } + + + MYSQL_RES *dbtool_mysql_query_internal(const char *sql, MYSQL *p_tmp_db_mysql, MYSQL_RES *p_tmp_query_result, int cursor) + { #ifdef SHOW_SQL printf("SQL: \"%s\"\n", sql); *************** *** 963,967 **** /*iRowsAffected = mysql_affected_rows(&tmp_db_mysql);*/ ! p_tmp_query_result = mysql_store_result(p_tmp_db_mysql); #ifdef SHOW_SQL --- 1001,1009 ---- /*iRowsAffected = mysql_affected_rows(&tmp_db_mysql);*/ ! if(cursor) { ! p_tmp_query_result = mysql_use_result(p_tmp_db_mysql); ! } else { ! p_tmp_query_result = mysql_store_result(p_tmp_db_mysql); ! } #ifdef SHOW_SQL |