* DB.C - Datatabase routines for NDO2DB daemon
*
* Copyright (c) 2009 Nagios Core Development Team and Community Contributors
* Copyright (c) 2005-2009 Ethan Galstad
*
* Last Modified: 07-11-2009
*
**************************************************************/
/* include our project‘s header files */
#include "../include/config.h"
#include "../include/common.h"
#include "../include/io.h"
#include "../include/utils.h"
#include "../include/protoapi.h"
#include "../include/ndo2db.h"
#include "../include/dbhandlers.h"
#include "../include/db.h"
extern int errno;
extern ndo2db_dbconfig ndo2db_db_settings;
extern time_t ndo2db_db_last_checkin_time;
char *ndo2db_db_rawtablenames[NDO2DB_MAX_DBTABLES]={
<span style="white-space:pre"> </span>"instances",
<span style="white-space:pre"> </span>"conninfo",
<span style="white-space:pre"> </span>"objects",
<span style="white-space:pre"> </span>"objecttypes",
<span style="white-space:pre"> </span>"logentries",
<span style="white-space:pre"> </span>"systemcommands",
<span style="white-space:pre"> </span>"eventhandlers",
<span style="white-space:pre"> </span>"servicechecks",
<span style="white-space:pre"> </span>"hostchecks",
<span style="white-space:pre"> </span>"programstatus",
<span style="white-space:pre"> </span>"externalcommands",
<span style="white-space:pre"> </span>"servicestatus",
<span style="white-space:pre"> </span>"hoststatus",
<span style="white-space:pre"> </span>"processevents",
<span style="white-space:pre"> </span>"timedevents",
<span style="white-space:pre"> </span>"timedeventqueue",
<span style="white-space:pre"> </span>"flappinghistory",
<span style="white-space:pre"> </span>"commenthistory",
<span style="white-space:pre"> </span>"comments",
<span style="white-space:pre"> </span>"notifications",
<span style="white-space:pre"> </span>"contactnotifications",
<span style="white-space:pre"> </span>"contactnotificationmethods",
<span style="white-space:pre"> </span>"acknowledgements",
<span style="white-space:pre"> </span>"statehistory",
<span style="white-space:pre"> </span>"downtimehistory",
<span style="white-space:pre"> </span>"scheduleddowntime",
<span style="white-space:pre"> </span>"configfiles",
<span style="white-space:pre"> </span>"configfilevariables",
<span style="white-space:pre"> </span>"runtimevariables",
<span style="white-space:pre"> </span>"contactstatus",
<span style="white-space:pre"> </span>"customvariablestatus",
<span style="white-space:pre"> </span>"",
<span style="white-space:pre"> </span>"",
<span style="white-space:pre"> </span>"",
<span style="white-space:pre"> </span>"",
<span style="white-space:pre"> </span>"",
<span style="white-space:pre"> </span>"",
<span style="white-space:pre"> </span>"",
<span style="white-space:pre"> </span>"",
<span style="white-space:pre"> </span>"",
<span style="white-space:pre"> </span>"commands",
<span style="white-space:pre"> </span>"timeperiods",
<span style="white-space:pre"> </span>"timeperiod_timeranges",
<span style="white-space:pre"> </span>"contactgroups",
<span style="white-space:pre"> </span>"contactgroup_members",
<span style="white-space:pre"> </span>"hostgroups",
<span style="white-space:pre"> </span>"hostgroup_members",
<span style="white-space:pre"> </span>"servicegroups",
<span style="white-space:pre"> </span>"servicegroup_members",
<span style="white-space:pre"> </span>"hostescalations",
<span style="white-space:pre"> </span>"hostescalation_contacts",
<span style="white-space:pre"> </span>"serviceescalations",
<span style="white-space:pre"> </span>"serviceescalation_contacts",
<span style="white-space:pre"> </span>"hostdependencies",
<span style="white-space:pre"> </span>"servicedependencies",
<span style="white-space:pre"> </span>"contacts",
<span style="white-space:pre"> </span>"contact_addresses",
<span style="white-space:pre"> </span>"contact_notificationcommands",
<span style="white-space:pre"> </span>"hosts",
<span style="white-space:pre"> </span>"host_parenthosts",
<span style="white-space:pre"> </span>"host_contacts",
<span style="white-space:pre"> </span>"services",
<span style="white-space:pre"> </span>"service_contacts",
<span style="white-space:pre"> </span>"customvariables",
<span style="white-space:pre"> </span>"host_contactgroups",
<span style="white-space:pre"> </span>"service_contactgroups",
<span style="white-space:pre"> </span>"hostescalation_contactgroups",
<span style="white-space:pre"> </span>"serviceescalation_contactgroups",
<span style="white-space:pre"> </span>"service_parentservices",
};
char *ndo2db_db_tablenames[NDO2DB_MAX_DBTABLES];
/*
#define DEBUG_NDO2DB_QUERIES 1
*/
/****************************************************************************/
/* CONNECTION FUNCTIONS */
/****************************************************************************/
/* initialize database structures */
int ndo2db_db_init(ndo2db_idi *idi){
<span style="white-space:pre"> </span>register int x;
<span style="white-space:pre"> </span>if(idi==NULL)
<span style="white-space:pre"> </span>return NDO_ERROR;
<span style="white-space:pre"> </span>/* initialize db server type */
<span style="white-space:pre"> </span>idi->dbinfo.server_type=ndo2db_db_settings.server_type;
<span style="white-space:pre"> </span>/* initialize table names */
<span style="white-space:pre"> </span>for(x=0;x<NDO2DB_MAX_DBTABLES;x++){
<span style="white-space:pre"> </span>if((ndo2db_db_tablenames[x]=(char *)malloc(strlen(ndo2db_db_rawtablenames[x])+((ndo2db_db_settings.dbprefix==NULL)?0:strlen(ndo2db_db_settings.dbprefix))+1))==NULL)
<span style="white-space:pre"> </span>return NDO_ERROR;
<span style="white-space:pre"> </span>sprintf(ndo2db_db_tablenames[x],"%s%s",(ndo2db_db_settings.dbprefix==NULL)?"":ndo2db_db_settings.dbprefix,ndo2db_db_rawtablenames[x]);
<span style="white-space:pre"> </span> }
<span style="white-space:pre"> </span>/* initialize other variables */
<span style="white-space:pre"> </span>idi->dbinfo.connected=NDO_FALSE;
<span style="white-space:pre"> </span>idi->dbinfo.error=NDO_FALSE;
<span style="white-space:pre"> </span>idi->dbinfo.instance_id=0L;
<span style="white-space:pre"> </span>idi->dbinfo.conninfo_id=0L;
<span style="white-space:pre"> </span>idi->dbinfo.latest_program_status_time=(time_t)0L;
<span style="white-space:pre"> </span>idi->dbinfo.latest_host_status_time=(time_t)0L;
<span style="white-space:pre"> </span>idi->dbinfo.latest_service_status_time=(time_t)0L;
<span style="white-space:pre"> </span>idi->dbinfo.latest_queued_event_time=(time_t)0L;
<span style="white-space:pre"> </span>idi->dbinfo.latest_realtime_data_time=(time_t)0L;
<span style="white-space:pre"> </span>idi->dbinfo.latest_comment_time=(time_t)0L;
<span style="white-space:pre"> </span>idi->dbinfo.clean_event_queue=NDO_FALSE;
<span style="white-space:pre"> </span>idi->dbinfo.last_notification_id=0L;
<span style="white-space:pre"> </span>idi->dbinfo.last_contact_notification_id=0L;
<span style="white-space:pre"> </span>idi->dbinfo.max_timedevents_age=ndo2db_db_settings.max_timedevents_age;
<span style="white-space:pre"> </span>idi->dbinfo.max_systemcommands_age=ndo2db_db_settings.max_systemcommands_age;
<span style="white-space:pre"> </span>idi->dbinfo.max_servicechecks_age=ndo2db_db_settings.max_servicechecks_age;
<span style="white-space:pre"> </span>idi->dbinfo.max_hostchecks_age=ndo2db_db_settings.max_hostchecks_age;
<span style="white-space:pre"> </span>idi->dbinfo.max_eventhandlers_age=ndo2db_db_settings.max_eventhandlers_age;
<span style="white-space:pre"> </span>idi->dbinfo.max_externalcommands_age=ndo2db_db_settings.max_externalcommands_age;
<span style="white-space:pre"> </span>idi->dbinfo.max_notifications_age=ndo2db_db_settings.max_notifications_age;
<span style="white-space:pre"> </span>idi->dbinfo.max_contactnotifications_age=ndo2db_db_settings.max_contactnotifications_age;
<span style="white-space:pre"> </span>idi->dbinfo.max_contactnotificationmethods_age=ndo2db_db_settings.max_contactnotificationmethods_age;
<span style="white-space:pre"> </span>idi->dbinfo.max_logentries_age=ndo2db_db_settings.max_logentries_age;
<span style="white-space:pre"> </span>idi->dbinfo.max_acknowledgements_age=ndo2db_db_settings.max_acknowledgements_age;<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>idi->dbinfo.last_table_trim_time=(time_t)0L;
<span style="white-space:pre"> </span>idi->dbinfo.last_logentry_time=(time_t)0L;
<span style="white-space:pre"> </span>idi->dbinfo.last_logentry_data=NULL;
<span style="white-space:pre"> </span>idi->dbinfo.object_hashlist=NULL;
<span style="white-space:pre"> </span>/* initialize db structures, etc. */
<span style="white-space:pre"> </span>switch(idi->dbinfo.server_type){
<span style="white-space:pre"> </span>case NDO2DB_DBSERVER_MYSQL:
#ifdef USE_MYSQL
<span style="white-space:pre"> </span>if(!mysql_init(&idi->dbinfo.mysql_conn)){
<span style="white-space:pre"> </span>syslog(LOG_USER|LOG_INFO,"Error: mysql_init() failed\n");
<span style="white-space:pre"> </span>return NDO_ERROR;
<span style="white-space:pre"> </span> }
#endif
<span style="white-space:pre"> </span>break;
<span style="white-space:pre"> </span>case NDO2DB_DBSERVER_PGSQL:
#ifdef USE_PGSQL
<span style="white-space:pre"> </span>idi->dbinfo.pgsql_conn=NULL;
<span style="white-space:pre"> </span>idi->dbinfo.pgsql_result=NULL;
#endif
<span style="white-space:pre"> </span>break;
<span style="white-space:pre"> </span>default:
<span style="white-space:pre"> </span>break;
<span style="white-space:pre"> </span> }
<span style="white-space:pre"> </span>return NDO_OK;
}
/* clean up database structures */
int ndo2db_db_deinit(ndo2db_idi *idi){
<span style="white-space:pre"> </span>register int x;
<span style="white-space:pre"> </span>if(idi==NULL)
<span style="white-space:pre"> </span>return NDO_ERROR;
<span style="white-space:pre"> </span>/* free table names */
<span style="white-space:pre"> </span>for(x=0;x<NDO2DB_MAX_DBTABLES;x++){
<span style="white-space:pre"> </span>if(ndo2db_db_tablenames[x])
<span style="white-space:pre"> </span>free(ndo2db_db_tablenames[x]);
<span style="white-space:pre"> </span>ndo2db_db_tablenames[x]=NULL;
<span style="white-space:pre"> </span> }
<span style="white-space:pre"> </span>/* free cached object ids */
<span style="white-space:pre"> </span>ndo2db_free_cached_object_ids(idi);
<span style="white-space:pre"> </span>return NDO_OK;
}
/* connects to the database server */
int ndo2db_db_connect(ndo2db_idi *idi){
<span style="white-space:pre"> </span>int result=NDO_OK;
<span style="white-space:pre"> </span>if(idi==NULL)
<span style="white-space:pre"> </span>return NDO_ERROR;
<span style="white-space:pre"> </span>/* we‘re already connected... */
<span style="white-space:pre"> </span>if(idi->dbinfo.connected==NDO_TRUE)
<span style="white-space:pre"> </span>return NDO_OK;
<span style="white-space:pre"> </span>switch(idi->dbinfo.server_type){
<span style="white-space:pre"> </span>case NDO2DB_DBSERVER_MYSQL:
#ifdef USE_MYSQL
<span style="white-space:pre"> </span>if(!mysql_real_connect(&idi->dbinfo.mysql_conn,ndo2db_db_settings.host,ndo2db_db_settings.username,ndo2db_db_settings.password,ndo2db_db_settings.dbname,ndo2db_db_settings.port,NULL,0)){
<span style="white-space:pre"> </span>mysql_close(&idi->dbinfo.mysql_conn);
<span style="white-space:pre"> </span>syslog(LOG_USER|LOG_INFO,"Error: Could not connect to MySQL database: %s",mysql_error(&idi->dbinfo.mysql_conn));
<span style="white-space:pre"> </span>result=NDO_ERROR;
<span style="white-space:pre"> </span>idi->disconnect_client=NDO_TRUE;
<span style="white-space:pre"> </span> }
<span style="white-space:pre"> </span>else{
<span style="white-space:pre"> </span>/*rubydrew@qq.com*/
<span style="white-space:pre"> </span>mysql_set_character_set(&idi->dbinfo.mysql_conn, "utf8");
<span style="white-space:pre"> </span>idi->dbinfo.connected=NDO_TRUE;
<span style="white-space:pre"> </span>syslog(LOG_USER|LOG_DEBUG,"Successfully connected to MySQL database");
<span style="white-space:pre"> </span> }
#endif
<span style="white-space:pre"> </span>break;
<span style="white-space:pre"> </span>case NDO2DB_DBSERVER_PGSQL:
#ifdef USE_PGSQL
<span style="white-space:pre"> </span>snprintf(connect_string,sizeof(connect_string)-1,"host=‘%s‘ port=%d dbname=‘%s‘ user=‘%s‘ password=‘%s‘",ndo2db_db_settings.host,ndo2db_db_settings.port,ndo2db_db_settings.dbname,ndo2db_db_settings.username,ndo2db_db_settings.password);
<span style="white-space:pre"> </span>connect_string[sizeof(connect_string)-1]=‘\x0‘;
<span style="white-space:pre"> </span>idi->dbinfo.pgsql_conn=PQconnectdb(connect_string);
<span style="white-space:pre"> </span>if(PQstatus(idi->dbinfo.pgsql_conn)==CONNECTION_BAD){
<span style="white-space:pre"> </span>PQfinish(idi->dbinfo.pgsql_conn);
<span style="white-space:pre"> </span>syslog(LOG_USER|LOG_INFO,"Error: Could not connect to PostgreSQL database: %s",PQerrorMessage(idi->dbinfo.pgsql_conn));
<span style="white-space:pre"> </span>result=NDO_ERROR;
<span style="white-space:pre"> </span>idi->disconnect_client=NDO_TRUE;
<span style="white-space:pre"> </span> }
<span style="white-space:pre"> </span>else{
<span style="white-space:pre"> </span>idi->dbinfo.connected=NDO_TRUE;
<span style="white-space:pre"> </span>syslog(LOG_USER|LOG_DEBUG,"Successfully connected to PostgreSQL database");
<span style="white-space:pre"> </span> }
#endif
<span style="white-space:pre"> </span>break;
<span style="white-space:pre"> </span>default:
<span style="white-space:pre"> </span>break;
<span style="white-space:pre"> </span> }
<span style="white-space:pre"> </span>return result;
}
/* disconnects from the database server */
int ndo2db_db_disconnect(ndo2db_idi *idi){
<span style="white-space:pre"> </span>if(idi==NULL)
<span style="white-space:pre"> </span>return NDO_ERROR;
<span style="white-space:pre"> </span>/* we‘re not connected... */
<span style="white-space:pre"> </span>if(idi->dbinfo.connected==NDO_FALSE)
<span style="white-space:pre"> </span>return NDO_OK;
<span style="white-space:pre"> </span>switch(idi->dbinfo.server_type){
<span style="white-space:pre"> </span>case NDO2DB_DBSERVER_MYSQL:
#ifdef USE_MYSQL
<span style="white-space:pre"> </span>/* close the connection to the database server */<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>mysql_close(&idi->dbinfo.mysql_conn);
<span style="white-space:pre"> </span>idi->dbinfo.connected=NDO_FALSE;
<span style="white-space:pre"> </span>syslog(LOG_USER|LOG_DEBUG,"Successfully disconnected from MySQL database");
#endif
<span style="white-space:pre"> </span>break;
<span style="white-space:pre"> </span>case NDO2DB_DBSERVER_PGSQL:
#ifdef USE_PGSQL
<span style="white-space:pre"> </span>/* close database connection and cleanup */
<span style="white-space:pre"> </span>if(PQstatus(idi->dbinfo.pgsql_conn)!=CONNECTION_BAD)
<span style="white-space:pre"> </span>PQfinish(idi->dbinfo.pgsql_conn);
<span style="white-space:pre"> </span>idi->dbinfo.connected=NDO_FALSE;
<span style="white-space:pre"> </span>syslog(LOG_USER|LOG_DEBUG,"Successfully disconnected from PostgreSQL database");
#endif
<span style="white-space:pre"> </span>break;
<span style="white-space:pre"> </span>default:
<span style="white-space:pre"> </span>break;
<span style="white-space:pre"> </span> }
<span style="white-space:pre"> </span>return NDO_OK;
}
/* post-connect routines */
int ndo2db_db_hello(ndo2db_idi *idi){
<span style="white-space:pre"> </span>char *buf=NULL;
<span style="white-space:pre"> </span>char *ts=NULL;
<span style="white-space:pre"> </span>int result=NDO_OK;
<span style="white-space:pre"> </span>int have_instance=NDO_FALSE;
<span style="white-space:pre"> </span>time_t current_time;
<span style="white-space:pre"> </span>/* make sure we have an instance name */
<span style="white-space:pre"> </span>if(idi->instance_name==NULL)
<span style="white-space:pre"> </span>idi->instance_name=strdup("default");
<span style="white-space:pre"> </span>/* get existing instance */
<span style="white-space:pre"> </span>if(asprintf(&buf,"SELECT instance_id FROM %s WHERE instance_name=‘%s‘",ndo2db_db_tablenames[NDO2DB_DBTABLE_INSTANCES],idi->instance_name)==-1)
<span style="white-space:pre"> </span>buf=NULL;
<span style="white-space:pre"> </span>if((result=ndo2db_db_query(idi,buf))==NDO_OK){
<span style="white-space:pre"> </span>switch(idi->dbinfo.server_type){
<span style="white-space:pre"> </span>case NDO2DB_DBSERVER_MYSQL:
#ifdef USE_MYSQL
<span style="white-space:pre"> </span>idi->dbinfo.mysql_result=mysql_store_result(&idi->dbinfo.mysql_conn);
<span style="white-space:pre"> </span>if((idi->dbinfo.mysql_row=mysql_fetch_row(idi->dbinfo.mysql_result))!=NULL){
<span style="white-space:pre"> </span>ndo2db_convert_string_to_unsignedlong(idi->dbinfo.mysql_row[0],&idi->dbinfo.instance_id);
<span style="white-space:pre"> </span>have_instance=NDO_TRUE;
<span style="white-space:pre"> </span> }
<span style="white-space:pre"> </span>mysql_free_result(idi->dbinfo.mysql_result);
<span style="white-space:pre"> </span>idi->dbinfo.mysql_result=NULL;
#endif
<span style="white-space:pre"> </span>break;
<span style="white-space:pre"> </span>default:
<span style="white-space:pre"> </span>break;
<span style="white-space:pre"> </span> }
<span style="white-space:pre"> </span> }
<span style="white-space:pre"> </span>free(buf);
<span style="white-space:pre"> </span>/* insert new instance if necessary */
<span style="white-space:pre"> </span>if(have_instance==NDO_FALSE){
<span style="white-space:pre"> </span>if(asprintf(&buf,"INSERT INTO %s SET instance_name=‘%s‘",ndo2db_db_tablenames[NDO2DB_DBTABLE_INSTANCES],idi->instance_name)==-1)
<span style="white-space:pre"> </span>buf=NULL;
<span style="white-space:pre"> </span>if((result=ndo2db_db_query(idi,buf))==NDO_OK){
<span style="white-space:pre"> </span>switch(idi->dbinfo.server_type){
<span style="white-space:pre"> </span>case NDO2DB_DBSERVER_MYSQL:
#ifdef USE_MYSQL
<span style="white-space:pre"> </span>idi->dbinfo.instance_id=mysql_insert_id(&idi->dbinfo.mysql_conn);
#endif
<span style="white-space:pre"> </span>break;
<span style="white-space:pre"> </span>default:
<span style="white-space:pre"> </span>break;
<span style="white-space:pre"> </span> }
<span style="white-space:pre"> </span> }
<span style="white-space:pre"> </span>free(buf);
<span style="white-space:pre"> </span> }
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>ts=ndo2db_db_timet_to_sql(idi,idi->data_start_time);
<span style="white-space:pre"> </span>/* record initial connection information */
<span style="white-space:pre"> </span>if(asprintf(&buf,"INSERT INTO %s SET instance_id=‘%lu‘, connect_time=NOW(), last_checkin_time=NOW(), bytes_processed=‘0‘, lines_processed=‘0‘, entries_processed=‘0‘, agent_name=‘%s‘, agent_version=‘%s‘, disposition=‘%s‘, connect_source=‘%s‘, connect_type=‘%s‘, data_start_time=%s"
<span style="white-space:pre"> </span> ,ndo2db_db_tablenames[NDO2DB_DBTABLE_CONNINFO]
<span style="white-space:pre"> </span> ,idi->dbinfo.instance_id
<span style="white-space:pre"> </span> ,idi->agent_name
<span style="white-space:pre"> </span> ,idi->agent_version
<span style="white-space:pre"> </span> ,idi->disposition
<span style="white-space:pre"> </span> ,idi->connect_source
<span style="white-space:pre"> </span> ,idi->connect_type
<span style="white-space:pre"> </span> ,ts
<span style="white-space:pre"> </span> )==-1)
<span style="white-space:pre"> </span>buf=NULL;
<span style="white-space:pre"> </span>if((result=ndo2db_db_query(idi,buf))==NDO_OK){
<span style="white-space:pre"> </span>switch(idi->dbinfo.server_type){
<span style="white-space:pre"> </span>case NDO2DB_DBSERVER_MYSQL:
#ifdef USE_MYSQL
<span style="white-space:pre"> </span>idi->dbinfo.conninfo_id=mysql_insert_id(&idi->dbinfo.mysql_conn);
#endif
<span style="white-space:pre"> </span>break;
<span style="white-space:pre"> </span>default:
<span style="white-space:pre"> </span>break;
<span style="white-space:pre"> </span> }
<span style="white-space:pre"> </span> }
<span style="white-space:pre"> </span>free(buf);
<span style="white-space:pre"> </span>free(ts);
<span style="white-space:pre"> </span>/* get cached object ids... */
<span style="white-space:pre"> </span>ndo2db_get_cached_object_ids(idi);
<span style="white-space:pre"> </span>/* get latest times from various tables... */
<span style="white-space:pre"> </span>ndo2db_db_get_latest_data_time(idi,ndo2db_db_tablenames[NDO2DB_DBTABLE_PROGRAMSTATUS],"status_update_time",(unsigned long *)&idi->dbinfo.latest_program_status_time);
<span style="white-space:pre"> </span>ndo2db_db_get_latest_data_time(idi,ndo2db_db_tablenames[NDO2DB_DBTABLE_HOSTSTATUS],"status_update_time",(unsigned long *)&idi->dbinfo.latest_host_status_time);
<span style="white-space:pre"> </span>ndo2db_db_get_latest_data_time(idi,ndo2db_db_tablenames[NDO2DB_DBTABLE_SERVICESTATUS],"status_update_time",(unsigned long *)&idi->dbinfo.latest_service_status_time);
<span style="white-space:pre"> </span>ndo2db_db_get_latest_data_time(idi,ndo2db_db_tablenames[NDO2DB_DBTABLE_CONTACTSTATUS],"status_update_time",(unsigned long *)&idi->dbinfo.latest_contact_status_time);
<span style="white-space:pre"> </span>ndo2db_db_get_latest_data_time(idi,ndo2db_db_tablenames[NDO2DB_DBTABLE_TIMEDEVENTQUEUE],"queued_time",(unsigned long *)&idi->dbinfo.latest_queued_event_time);
<span style="white-space:pre"> </span>ndo2db_db_get_latest_data_time(idi,ndo2db_db_tablenames[NDO2DB_DBTABLE_COMMENTS],"entry_time",(unsigned long *)&idi->dbinfo.latest_comment_time);
<span style="white-space:pre"> </span>/* calculate time of latest realtime data */
<span style="white-space:pre"> </span>idi->dbinfo.latest_realtime_data_time=(time_t)0L;
<span style="white-space:pre"> </span>if(idi->dbinfo.latest_program_status_time>idi->dbinfo.latest_realtime_data_time)
<span style="white-space:pre"> </span>idi->dbinfo.latest_realtime_data_time=idi->dbinfo.latest_program_status_time;
<span style="white-space:pre"> </span>if(idi->dbinfo.latest_host_status_time>idi->dbinfo.latest_realtime_data_time)
<span style="white-space:pre"> </span>idi->dbinfo.latest_realtime_data_time=idi->dbinfo.latest_host_status_time;
<span style="white-space:pre"> </span>if(idi->dbinfo.latest_service_status_time>idi->dbinfo.latest_realtime_data_time)
<span style="white-space:pre"> </span>idi->dbinfo.latest_realtime_data_time=idi->dbinfo.latest_service_status_time;
<span style="white-space:pre"> </span>if(idi->dbinfo.latest_contact_status_time>idi->dbinfo.latest_realtime_data_time)
<span style="white-space:pre"> </span>idi->dbinfo.latest_realtime_data_time=idi->dbinfo.latest_contact_status_time;
<span style="white-space:pre"> </span>if(idi->dbinfo.latest_queued_event_time>idi->dbinfo.latest_realtime_data_time)
<span style="white-space:pre"> </span>idi->dbinfo.latest_realtime_data_time=idi->dbinfo.latest_queued_event_time;
<span style="white-space:pre"> </span>/* get current time */
<span style="white-space:pre"> </span>/* make sure latest time stamp isn‘t in the future - this will cause problems if a backwards system time change occurs */
<span style="white-space:pre"> </span>time(¤t_time);
<span style="white-space:pre"> </span>if(idi->dbinfo.latest_realtime_data_time>current_time)
<span style="white-space:pre"> </span>idi->dbinfo.latest_realtime_data_time=current_time;
<span style="white-space:pre"> </span>/* set flags to clean event queue, etc. */
<span style="white-space:pre"> </span>idi->dbinfo.clean_event_queue=NDO_TRUE;
<span style="white-space:pre"> </span>/* set misc data */
<span style="white-space:pre"> </span>idi->dbinfo.last_notification_id=0L;
<span style="white-space:pre"> </span>idi->dbinfo.last_contact_notification_id=0L;
<span style="white-space:pre"> </span>return result;
}
/* pre-disconnect routines */
int ndo2db_db_goodbye(ndo2db_idi *idi){
<span style="white-space:pre"> </span>int result=NDO_OK;
<span style="white-space:pre"> </span>char *buf=NULL;
<span style="white-space:pre"> </span>char *ts=NULL;
<span style="white-space:pre"> </span>ts=ndo2db_db_timet_to_sql(idi,idi->data_end_time);
<span style="white-space:pre"> </span>/* record last connection information */
<span style="white-space:pre"> </span>if(asprintf(&buf,"UPDATE %s SET disconnect_time=NOW(), last_checkin_time=NOW(), data_end_time=%s, bytes_processed=‘%lu‘, lines_processed=‘%lu‘, entries_processed=‘%lu‘ WHERE conninfo_id=‘%lu‘"
<span style="white-space:pre"> </span> ,ndo2db_db_tablenames[NDO2DB_DBTABLE_CONNINFO]
<span style="white-space:pre"> </span> ,ts
<span style="white-space:pre"> </span> ,idi->bytes_processed
<span style="white-space:pre"> </span> ,idi->lines_processed
<span style="white-space:pre"> </span> ,idi->entries_processed
<span style="white-space:pre"> </span> ,idi->dbinfo.conninfo_id
<span style="white-space:pre"> </span> )==-1)
<span style="white-space:pre"> </span>buf=NULL;
<span style="white-space:pre"> </span>result=ndo2db_db_query(idi,buf);
<span style="white-space:pre"> </span>free(buf);
<span style="white-space:pre"> </span>free(ts);
<span style="white-space:pre"> </span>return result;
}
/* checking routines */
int ndo2db_db_checkin(ndo2db_idi *idi){
<span style="white-space:pre"> </span>int result=NDO_OK;
<span style="white-space:pre"> </span>char *buf=NULL;
<span style="white-space:pre"> </span>/* record last connection information */
<span style="white-space:pre"> </span>if(asprintf(&buf,"UPDATE %s SET last_checkin_time=NOW(), bytes_processed=‘%lu‘, lines_processed=‘%lu‘, entries_processed=‘%lu‘ WHERE conninfo_id=‘%lu‘"
<span style="white-space:pre"> </span> ,ndo2db_db_tablenames[NDO2DB_DBTABLE_CONNINFO]
<span style="white-space:pre"> </span> ,idi->bytes_processed
<span style="white-space:pre"> </span> ,idi->lines_processed
<span style="white-space:pre"> </span> ,idi->entries_processed
<span style="white-space:pre"> </span> ,idi->dbinfo.conninfo_id
<span style="white-space:pre"> </span> )==-1)
<span style="white-space:pre"> </span>buf=NULL;
<span style="white-space:pre"> </span>result=ndo2db_db_query(idi,buf);
<span style="white-space:pre"> </span>free(buf);
<span style="white-space:pre"> </span>time(&ndo2db_db_last_checkin_time);
<span style="white-space:pre"> </span>return result;
}
/****************************************************************************/
/* MISC FUNCTIONS */
/****************************************************************************/
/* escape a string for a SQL statement */
char *ndo2db_db_escape_string(ndo2db_idi *idi, char *buf){
<span style="white-space:pre"> </span>register int x,y,z;
<span style="white-space:pre"> </span>char *newbuf=NULL;
<span style="white-space:pre"> </span>if(idi==NULL || buf==NULL)
<span style="white-space:pre"> </span>return NULL;
<span style="white-space:pre"> </span>z=strlen(buf);
<span style="white-space:pre"> </span>/* allocate space for the new string */
<span style="white-space:pre"> </span>if((newbuf=(char *)malloc((z*2)+1))==NULL)
<span style="white-space:pre"> </span>return NULL;
<span style="white-space:pre"> </span>/* escape characters */
<span style="white-space:pre"> </span>for(x=0,y=0;x<z;x++){
<span style="white-space:pre"> </span>if(idi->dbinfo.server_type==NDO2DB_DBSERVER_MYSQL){
<span style="white-space:pre"> </span>if(buf[x]==‘\‘‘ || buf[x]==‘\"‘ || buf[x]==‘*‘ || buf[x]==‘\\‘ || buf[x]==‘$‘ || buf[x]==‘?‘ || buf[x]==‘.‘ || buf[x]==‘^‘ || buf[x]==‘+‘ || buf[x]==‘[‘ || buf[x]==‘]‘ || buf[x]==‘(‘ || buf[x]==‘)‘)
<span style="white-space:pre"> </span>newbuf[y++]=‘\\‘;
<span style="white-space:pre"> </span> }
<span style="white-space:pre"> </span>else if(idi->dbinfo.server_type==NDO2DB_DBSERVER_PGSQL){
<span style="white-space:pre"> </span>if(! (isspace(buf[x]) || isalnum(buf[x]) || (buf[x]==‘_‘)) )
<span style="white-space:pre"> </span>newbuf[y++]=‘\\‘;
<span style="white-space:pre"> </span> }
<span style="white-space:pre"> </span>newbuf[y++]=buf[x];
<span style="white-space:pre"> </span> }
<span style="white-space:pre"> </span>/* terminate escape string */
<span style="white-space:pre"> </span>newbuf [y]=‘\0‘;
<span style="white-space:pre"> </span>return newbuf;
}
/* SQL query conversion of time_t format to date/time format */
char *ndo2db_db_timet_to_sql(ndo2db_idi *idi, time_t t){
<span style="white-space:pre"> </span>char *buf=NULL;
<span style="white-space:pre"> </span>switch(idi->dbinfo.server_type){
<span style="white-space:pre"> </span>case NDO2DB_DBSERVER_MYSQL:
#ifdef USE_MYSQL
<span style="white-space:pre"> </span>asprintf(&buf,"FROM_UNIXTIME(%lu)",(unsigned long)t);
#endif
<span style="white-space:pre"> </span>break;
<span style="white-space:pre"> </span>case NDO2DB_DBSERVER_PGSQL:
#ifdef USE_PGSQL
<span style="white-space:pre"> </span>asprintf(&buf,"FROM_UNIXTIME(%lu)",(unsigned long)t);
#endif
<span style="white-space:pre"> </span>break;
<span style="white-space:pre"> </span>default:
<span style="white-space:pre"> </span>break;
<span style="white-space:pre"> </span> }
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>return buf;
}
/* SQL query conversion of date/time format to time_t format */
char *ndo2db_db_sql_to_timet(ndo2db_idi *idi, char *field){
<span style="white-space:pre"> </span>char *buf=NULL;
<span style="white-space:pre"> </span>switch(idi->dbinfo.server_type){
<span style="white-space:pre"> </span>case NDO2DB_DBSERVER_MYSQL:
#ifdef USE_MYSQL
<span style="white-space:pre"> </span>asprintf(&buf,"UNIX_TIMESTAMP(%s)",(field==NULL)?"":field);
#endif
<span style="white-space:pre"> </span>break;
<span style="white-space:pre"> </span>case NDO2DB_DBSERVER_PGSQL:
#ifdef USE_PGSQL
<span style="white-space:pre"> </span>asprintf(&buf,"UNIX_TIMESTAMP(%s",(field==NULL)?"":field);
#endif
<span style="white-space:pre"> </span>break;
<span style="white-space:pre"> </span>default:
<span style="white-space:pre"> </span>break;
<span style="white-space:pre"> </span> }
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>return buf;
}
/* executes a SQL statement */
int ndo2db_db_query(ndo2db_idi *idi, char *buf){
<span style="white-space:pre"> </span>int result=NDO_OK;
<span style="white-space:pre"> </span>int query_result=0;
<span style="white-space:pre"> </span>if(idi==NULL || buf==NULL)
<span style="white-space:pre"> </span>return NDO_ERROR;
<span style="white-space:pre"> </span>/* if we‘re not connected, try and reconnect... */
<span style="white-space:pre"> </span>if(idi->dbinfo.connected==NDO_FALSE){
<span style="white-space:pre"> </span>if(ndo2db_db_connect(idi)==NDO_ERROR)
<span style="white-space:pre"> </span>return NDO_ERROR;
<span style="white-space:pre"> </span>ndo2db_db_hello(idi);
<span style="white-space:pre"> </span> }
#ifdef DEBUG_NDO2DB_QUERIES
<span style="white-space:pre"> </span>printf("%s\n\n",buf);
#endif
<span style="white-space:pre"> </span>ndo2db_log_debug_info(NDO2DB_DEBUGL_SQL,0,"%s\n",buf);
<span style="white-space:pre"> </span>switch(idi->dbinfo.server_type){
<span style="white-space:pre"> </span>case NDO2DB_DBSERVER_MYSQL:
#ifdef USE_MYSQL
<span style="white-space:pre"> </span>if((query_result=mysql_query(&idi->dbinfo.mysql_conn,buf))){
<span style="white-space:pre"> </span>syslog(LOG_USER|LOG_INFO,"Error: mysql_query() failed for ‘%s‘\n",buf);
<span style="white-space:pre"> </span>syslog(LOG_USER|LOG_INFO,"mysql_error: ‘%s‘\n", mysql_error(&idi->dbinfo.mysql_conn));
<span style="white-space:pre"> </span>result=NDO_ERROR;
<span style="white-space:pre"> </span> }
#endif
<span style="white-space:pre"> </span>break;
<span style="white-space:pre"> </span>case NDO2DB_DBSERVER_PGSQL:
#ifdef USE_PGSQL
<span style="white-space:pre"> </span>idi->dbinfo.pgsql_result==PQexec(idi->dbinfo.pgsql_conn,buf);
<span style="white-space:pre"> </span>if((query_result=PQresultStatus(idi->dbinfo.pgsql_result))!=PGRES_COMMAND_OK){
<span style="white-space:pre"> </span>syslog(LOG_USER|LOG_INFO,"Error: PQexec() failed for ‘%s‘\n",buf);
<span style="white-space:pre"> </span>PQclear(idi->dbinfo.pgsql_result);
<span style="white-space:pre"> </span>result=NDO_ERROR;
<span style="white-space:pre"> </span> }
#endif
<span style="white-space:pre"> </span>break;
<span style="white-space:pre"> </span>default:
<span style="white-space:pre"> </span>break;
<span style="white-space:pre"> </span> }
<span style="white-space:pre"> </span>/* handle errors */
<span style="white-space:pre"> </span>if(result==NDO_ERROR)
<span style="white-space:pre"> </span>ndo2db_handle_db_error(idi,query_result);
<span style="white-space:pre"> </span>return result;
}
/* frees memory associated with a query */
int ndo2db_db_free_query(ndo2db_idi *idi){
<span style="white-space:pre"> </span>if(idi==NULL)
<span style="white-space:pre"> </span>return NDO_ERROR;
<span style="white-space:pre"> </span>switch(idi->dbinfo.server_type){
<span style="white-space:pre"> </span>case NDO2DB_DBSERVER_MYSQL:
#ifdef USE_MYSQL
#endif
<span style="white-space:pre"> </span>break;
<span style="white-space:pre"> </span>case NDO2DB_DBSERVER_PGSQL:
#ifdef USE_PGSQL
<span style="white-space:pre"> </span>PQclear(idi->dbinfo.pgsql_result);
#endif
<span style="white-space:pre"> </span>break;
<span style="white-space:pre"> </span>default:
<span style="white-space:pre"> </span>break;
<span style="white-space:pre"> </span> }
<span style="white-space:pre"> </span>return NDO_OK;
}
/* handles SQL query errors */
int ndo2db_handle_db_error(ndo2db_idi *idi, int query_result){
<span style="white-space:pre"> </span>int result=0;
<span style="white-space:pre"> </span>if(idi==NULL)
<span style="white-space:pre"> </span>return NDO_ERROR;
<span style="white-space:pre"> </span>/* we‘re not currently connected... */
<span style="white-space:pre"> </span>if(idi->dbinfo.connected==NDO_FALSE)
<span style="white-space:pre"> </span>return NDO_OK;
<span style="white-space:pre"> </span>switch(idi->dbinfo.server_type){
<span style="white-space:pre"> </span>case NDO2DB_DBSERVER_MYSQL:
#ifdef USE_MYSQL
<span style="white-space:pre"> </span>result=mysql_errno(&idi->dbinfo.mysql_conn);
<span style="white-space:pre"> </span>if(result==CR_SERVER_LOST || result==CR_SERVER_GONE_ERROR){
<span style="white-space:pre"> </span>syslog(LOG_USER|LOG_INFO,"Error: Connection to MySQL database has been lost!\n");
<span style="white-space:pre"> </span>ndo2db_db_disconnect(idi);
<span style="white-space:pre"> </span>idi->disconnect_client=NDO_TRUE;
<span style="white-space:pre"> </span> }
#endif
<span style="white-space:pre"> </span>break;
<span style="white-space:pre"> </span>case NDO2DB_DBSERVER_PGSQL:
#ifdef USE_PGSQL
<span style="white-space:pre"> </span>result=PQstatus(idi->dbinfo.pgsql_conn);
<span style="white-space:pre"> </span>if(result!=CONNECTION_OK){
<span style="white-space:pre"> </span>syslog(LOG_USER|LOG_INFO,"Error: Connection to PostgreSQL database has been lost!\n");
<span style="white-space:pre"> </span>ndo2db_db_disconnect(idi);
<span style="white-space:pre"> </span>idi->disconnect_client=NDO_TRUE;
<span style="white-space:pre"> </span> }
#endif
<span style="white-space:pre"> </span>break;
<span style="white-space:pre"> </span>default:
<span style="white-space:pre"> </span>break;
<span style="white-space:pre"> </span> }
<span style="white-space:pre"> </span>return NDO_OK;
}
/* clears data from a given table (current instance only) */
int ndo2db_db_clear_table(ndo2db_idi *idi, char *table_name){
<span style="white-space:pre"> </span>char *buf=NULL;
<span style="white-space:pre"> </span>int result=NDO_OK;
<span style="white-space:pre"> </span>if(idi==NULL || table_name==NULL)
<span style="white-space:pre"> </span>return NDO_ERROR;
<span style="white-space:pre"> </span>if(asprintf(&buf,"DELETE FROM %s WHERE instance_id=‘%lu‘"
<span style="white-space:pre"> </span> ,table_name
<span style="white-space:pre"> </span> ,idi->dbinfo.instance_id
<span style="white-space:pre"> </span> )==-1)
<span style="white-space:pre"> </span>buf=NULL;
<span style="white-space:pre"> </span>result=ndo2db_db_query(idi,buf);
<span style="white-space:pre"> </span>free(buf);
<span style="white-space:pre"> </span>return result;
}
<span style="white-space:pre"> </span>
/* gets latest data time value from a given table */
int ndo2db_db_get_latest_data_time(ndo2db_idi *idi, char *table_name, char *field_name, unsigned long *t){
<span style="white-space:pre"> </span>char *buf=NULL;
<span style="white-space:pre"> </span>char *ts[1];
<span style="white-space:pre"> </span>int result=NDO_OK;
<span style="white-space:pre"> </span>if(idi==NULL || table_name==NULL || field_name==NULL || t==NULL)
<span style="white-space:pre"> </span>return NDO_ERROR;
<span style="white-space:pre"> </span>*t=(time_t)0L;
<span style="white-space:pre"> </span>ts[0]=ndo2db_db_sql_to_timet(idi,field_name);
<span style="white-space:pre"> </span>if(asprintf(&buf,"SELECT %s AS latest_time FROM %s WHERE instance_id=‘%lu‘ ORDER BY %s DESC LIMIT 0,1"
<span style="white-space:pre"> </span> ,ts[0]
<span style="white-space:pre"> </span> ,table_name
<span style="white-space:pre"> </span> ,idi->dbinfo.instance_id
<span style="white-space:pre"> </span> ,field_name
<span style="white-space:pre"> </span> )==-1)
<span style="white-space:pre"> </span>buf=NULL;
<span style="white-space:pre"> </span>if((result=ndo2db_db_query(idi,buf))==NDO_OK){
<span style="white-space:pre"> </span>switch(idi->dbinfo.server_type){
<span style="white-space:pre"> </span>case NDO2DB_DBSERVER_MYSQL:
#ifdef USE_MYSQL
<span style="white-space:pre"> </span>idi->dbinfo.mysql_result=mysql_store_result(&idi->dbinfo.mysql_conn);
<span style="white-space:pre"> </span>if((idi->dbinfo.mysql_row=mysql_fetch_row(idi->dbinfo.mysql_result))!=NULL){
<span style="white-space:pre"> </span>ndo2db_convert_string_to_unsignedlong(idi->dbinfo.mysql_row[0],t);
<span style="white-space:pre"> </span> }
<span style="white-space:pre"> </span>mysql_free_result(idi->dbinfo.mysql_result);
<span style="white-space:pre"> </span>idi->dbinfo.mysql_result=NULL;
#endif
<span style="white-space:pre"> </span>break;
<span style="white-space:pre"> </span>default:
<span style="white-space:pre"> </span>break;
<span style="white-space:pre"> </span> }
<span style="white-space:pre"> </span> }
<span style="white-space:pre"> </span>free(buf);
<span style="white-space:pre"> </span>free(ts[0]);
<span style="white-space:pre"> </span>return result;
}
<span style="white-space:pre"> </span>
/* trim/delete old data from a given table */
int ndo2db_db_trim_data_table(ndo2db_idi *idi, char *table_name, char *field_name, unsigned long t){
<span style="white-space:pre"> </span>char *buf=NULL;
<span style="white-space:pre"> </span>char *ts[1];
<span style="white-space:pre"> </span>int result=NDO_OK;
<span style="white-space:pre"> </span>if(idi==NULL || table_name==NULL || field_name==NULL)
<span style="white-space:pre"> </span>return NDO_ERROR;
<span style="white-space:pre"> </span>ts[0]=ndo2db_db_timet_to_sql(idi,(time_t)t);
<span style="white-space:pre"> </span>if(asprintf(&buf,"DELETE FROM %s WHERE instance_id=‘%lu‘ AND %s<%s"
<span style="white-space:pre"> </span> ,table_name
<span style="white-space:pre"> </span> ,idi->dbinfo.instance_id
<span style="white-space:pre"> </span> ,field_name
<span style="white-space:pre"> </span> ,ts[0]
<span style="white-space:pre"> </span> )==-1)
<span style="white-space:pre"> </span>buf=NULL;
<span style="white-space:pre"> </span>result=ndo2db_db_query(idi,buf);
<span style="white-space:pre"> </span>free(buf);
<span style="white-space:pre"> </span>free(ts[0]);
<span style="white-space:pre"> </span>return result;
}
<span style="white-space:pre"> </span>
/* performs some periodic table maintenance... */
int ndo2db_db_perform_maintenance(ndo2db_idi *idi){
<span style="white-space:pre"> </span>time_t current_time;
<span style="white-space:pre"> </span>/* get the current time */
<span style="white-space:pre"> </span>time(¤t_time);
<span style="white-space:pre"> </span>/* trim tables */
<span style="white-space:pre"> </span>if(((unsigned long)current_time-60)>(unsigned long)idi->dbinfo.last_table_trim_time){
<span style="white-space:pre"> </span>if(idi->dbinfo.max_timedevents_age>0L)
<span style="white-space:pre"> </span>ndo2db_db_trim_data_table(idi,ndo2db_db_tablenames[NDO2DB_DBTABLE_TIMEDEVENTS],"scheduled_time",(time_t)((unsigned long)current_time-idi->dbinfo.max_timedevents_age));
<span style="white-space:pre"> </span>if(idi->dbinfo.max_systemcommands_age>0L)
<span style="white-space:pre"> </span>ndo2db_db_trim_data_table(idi,ndo2db_db_tablenames[NDO2DB_DBTABLE_SYSTEMCOMMANDS],"start_time",(time_t)((unsigned long)current_time-idi->dbinfo.max_systemcommands_age));
<span style="white-space:pre"> </span>if(idi->dbinfo.max_servicechecks_age>0L)
<span style="white-space:pre"> </span>ndo2db_db_trim_data_table(idi,ndo2db_db_tablenames[NDO2DB_DBTABLE_SERVICECHECKS],"start_time",(time_t)((unsigned long)current_time-idi->dbinfo.max_servicechecks_age));
<span style="white-space:pre"> </span>if(idi->dbinfo.max_hostchecks_age>0L)
<span style="white-space:pre"> </span>ndo2db_db_trim_data_table(idi,ndo2db_db_tablenames[NDO2DB_DBTABLE_HOSTCHECKS],"start_time",(time_t)((unsigned long)current_time-idi->dbinfo.max_hostchecks_age));
<span style="white-space:pre"> </span>if(idi->dbinfo.max_eventhandlers_age>0L)
<span style="white-space:pre"> </span>ndo2db_db_trim_data_table(idi,ndo2db_db_tablenames[NDO2DB_DBTABLE_EVENTHANDLERS],"start_time",(time_t)((unsigned long)current_time-idi->dbinfo.max_eventhandlers_age));
<span style="white-space:pre"> </span>if(idi->dbinfo.max_externalcommands_age>0L)
<span style="white-space:pre"> </span>ndo2db_db_trim_data_table(idi,ndo2db_db_tablenames[NDO2DB_DBTABLE_EXTERNALCOMMANDS],"entry_time",(time_t)((unsigned long)current_time-idi->dbinfo.max_externalcommands_age));
<span style="white-space:pre"> </span>if(idi->dbinfo.max_notifications_age>0L)
<span style="white-space:pre"> </span>ndo2db_db_trim_data_table(idi,ndo2db_db_tablenames[NDO2DB_DBTABLE_NOTIFICATIONS],"entry_time",(time_t)((unsigned long)current_time-idi->dbinfo.max_notifications_age));
<span style="white-space:pre"> </span>if(idi->dbinfo.max_contactnotifications_age>0L)
<span style="white-space:pre"> </span>ndo2db_db_trim_data_table(idi,ndo2db_db_tablenames[NDO2DB_DBTABLE_CONTACTNOTIFICATIONS],"entry_time",(time_t)((unsigned long)current_time-idi->dbinfo.max_contactnotifications_age));
<span style="white-space:pre"> </span>if(idi->dbinfo.max_contactnotificationmethods_age>0L)
<span style="white-space:pre"> </span>ndo2db_db_trim_data_table(idi,ndo2db_db_tablenames[NDO2DB_DBTABLE_CONTACTNOTIFICATIONMETHODS],"entry_time",(time_t)((unsigned long)current_time-idi->dbinfo.max_contactnotificationmethods_age));
<span style="white-space:pre"> </span>if(idi->dbinfo.max_logentries_age>0L)
<span style="white-space:pre"> </span>ndo2db_db_trim_data_table(idi,ndo2db_db_tablenames[NDO2DB_DBTABLE_LOGENTRIES],"entry_time",(time_t)((unsigned long)current_time-idi->dbinfo.max_logentries_age));
<span style="white-space:pre"> </span>if(idi->dbinfo.max_acknowledgements_age>0L)
<span style="white-space:pre"> </span>ndo2db_db_trim_data_table(idi,ndo2db_db_tablenames[NDO2DB_DBTABLE_ACKNOWLEDGEMENTS],"entry_time",(time_t)((unsigned long)current_time-idi->dbinfo.max_acknowledgements_age));<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>idi->dbinfo.last_table_trim_time=current_time;
<span style="white-space:pre"> </span> }
<span style="white-space:pre"> </span>return NDO_OK;
}
ndoutils-2.0.1-utf8(完美解决中文乱码问题)
软件下载地址:http://download.csdn.net/detail/drew27/8475389
参考资料:http://blog.sina.com.cn/s/blog_620f4cf70100pvlv.html