Skip to content
This repository has been archived by the owner on Jan 15, 2019. It is now read-only.

Commit

Permalink
fix oracle rewrites for other rdbms, add several fixes/features
Browse files Browse the repository at this point in the history
* add database name in connection msg to syslog
* fix comment commentdata/downtimedata copypaste failures
* fix object insert rewrite for mysql/pgsql
* fix customvariablestatus timestamp

fixes #252,#272
  • Loading branch information
Michael Friedrich committed Feb 8, 2010
1 parent 0bd2982 commit c9985f5
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 37 deletions.
1 change: 1 addition & 0 deletions module/idoutils/include/db.h
Expand Up @@ -20,6 +20,7 @@ typedef struct ndo2db_dbconfig_struct{
char *password;
char *dbname;
char *dbprefix;
char *dbserver;
unsigned long max_timedevents_age;
unsigned long max_systemcommands_age;
unsigned long max_servicechecks_age;
Expand Down
2 changes: 1 addition & 1 deletion module/idoutils/include/dbhandlers.h
Expand Up @@ -71,7 +71,7 @@ int ndo2db_handle_commanddefinition(ndo2db_idi *);
int ndo2db_handle_timeperiodefinition(ndo2db_idi *);
int ndo2db_handle_contactdefinition(ndo2db_idi *);
int ndo2db_handle_contactgroupdefinition(ndo2db_idi *);
int ndo2db_save_custom_variables(ndo2db_idi *,int, int, char *, unsigned long);
int ndo2db_save_custom_variables(ndo2db_idi *,int, unsigned long, char *, unsigned long);

int ndo2db_log_debug_info(int , int , const char *, ...);
#endif
10 changes: 5 additions & 5 deletions module/idoutils/src/db.c
Expand Up @@ -415,12 +415,12 @@ int ndo2db_db_connect(ndo2db_idi *idi) {

if (dbi_conn_connect(idi->dbinfo.dbi_conn) != 0) {
dbi_conn_error(idi->dbinfo.dbi_conn, &dbi_error);
syslog(LOG_USER | LOG_INFO, "Error: Could not connect to database: %s", dbi_error);
syslog(LOG_USER | LOG_INFO, "Error: Could not connect to %s database: %s", ndo2db_db_settings.dbserver, dbi_error);
result = NDO_ERROR;
idi->disconnect_client = NDO_TRUE;
} else {
idi->dbinfo.connected = NDO_TRUE;
syslog(LOG_USER | LOG_INFO, "Successfully connected to database");
syslog(LOG_USER | LOG_INFO, "Successfully connected to %s database", ndo2db_db_settings.dbserver);
}
#else /* Oracle ocilib specific */

Expand All @@ -435,13 +435,13 @@ int ndo2db_db_connect(ndo2db_idi *idi) {

if(idi->dbinfo.oci_connection == NULL) {
syslog(LOG_USER | LOG_INFO, "Error: Could not connect to oracle database: %s", OCI_ErrorGetString(OCI_GetLastError()));
ndo2db_log_debug_info(NDO2DB_DEBUGL_PROCESSINFO, 2, "Error: Could not connect to oracle database\n");
ndo2db_log_debug_info(NDO2DB_DEBUGL_PROCESSINFO, 2, "Error: Could not connect to %s database\n", ndo2db_db_settings.dbserver);
result = NDO_ERROR;
idi->disconnect_client = NDO_TRUE;
} else {
idi->dbinfo.connected = NDO_TRUE;
syslog(LOG_USER | LOG_INFO, "Successfully connected to oracle database");
ndo2db_log_debug_info(NDO2DB_DEBUGL_PROCESSINFO, 2, "Successfully connected to oracle database\n");
ndo2db_log_debug_info(NDO2DB_DEBUGL_PROCESSINFO, 2, "Successfully connected to %s database\n", ndo2db_db_settings.dbserver);
}

/* initialize prepared statements */
Expand Down Expand Up @@ -4943,7 +4943,7 @@ int ido2db_oci_prepared_statement_save_custom_variables_customvariablestatus(ndo

ndo2db_log_debug_info(NDO2DB_DEBUGL_PROCESSINFO, 2, "ido2db_oci_prepared_statement_() start\n");

if(asprintf(&buf, "MERGE INTO %s USING DUAL ON (object_id=:X2 AND varname=:X5) WHEN MATCHED THEN UPDATE SET instance_id=:X1, status_update_time=(SELECT unixts2date(:X3) FROM DUAL), has_been_modified=:X4, varvalue=:X6 WHEN NOT MATCHED THEN INSERT (id, instance_id, object_id, status_update_time, has_been_modified, varname, varvalue) VALUES (seq_customvariablestatus.nextval, :X1, :X2, :X3, :X4, :X5, :X6)",
if(asprintf(&buf, "MERGE INTO %s USING DUAL ON (object_id=:X2 AND varname=:X5) WHEN MATCHED THEN UPDATE SET instance_id=:X1, status_update_time=(SELECT unixts2date(:X3) FROM DUAL), has_been_modified=:X4, varvalue=:X6 WHEN NOT MATCHED THEN INSERT (id, instance_id, object_id, status_update_time, has_been_modified, varname, varvalue) VALUES (seq_customvariablestatus.nextval, :X1, :X2, (SELECT unixts2date(:X3) FROM DUAL), :X4, :X5, :X6)",
ndo2db_db_tablenames[NDO2DB_DBTABLE_CUSTOMVARIABLESTATUS]) == -1) {
buf = NULL;
}
Expand Down
8 changes: 5 additions & 3 deletions module/idoutils/src/dbhandlers.c
Expand Up @@ -411,7 +411,7 @@ int ndo2db_get_object_id_with_insert(ndo2db_idi *idi, int object_type, char *n1,

#ifndef USE_ORACLE /* everything else will be libdbi */
if (asprintf(&buf,
"INSERT INTO %s (instance_id, objecttype_id, name1, name2) VALUES (%lu, %d, %s, %s)",
"INSERT INTO %s (instance_id, objecttype_id, name1, name2) VALUES (%lu, %d, '%s', '%s')",
ndo2db_db_tablenames[NDO2DB_DBTABLE_OBJECTS],
idi->dbinfo.instance_id, object_type, es[0],
es[1]) == -1)
Expand Down Expand Up @@ -6852,7 +6852,7 @@ int ndo2db_handle_contactdefinition(ndo2db_idi *idi) {
return NDO_OK;
}

int ndo2db_save_custom_variables(ndo2db_idi *idi,int table_idx, int o_id, char *ts, unsigned long tstamp){
int ndo2db_save_custom_variables(ndo2db_idi *idi,int table_idx, unsigned long o_id, char *ts, unsigned long tstamp){
char *buf=NULL;
char *buf1=NULL;
char *buf2=NULL;
Expand Down Expand Up @@ -6909,7 +6909,8 @@ int ndo2db_save_custom_variables(ndo2db_idi *idi,int table_idx, int o_id, char *
data[3] = (void *) &has_been_modified;
data[4] = (void *) &es[0];
data[5] = (void *) &es[1];


ndo2db_log_debug_info(NDO2DB_DEBUGL_PROCESSINFO, 2, "ndo2db_save_custom_variables() instance_id=%lu, object_id=%lu, config_type=%d, modified=%d, varname=%s, varvalue=%s\n", idi->dbinfo.instance_id, o_id, idi->current_object_config_type, has_been_modified, es[0], es[1]);
result = ido2db_query_insert_or_update_save_custom_variables_customvariables_add(idi, data);

#ifndef USE_ORACLE /* everything else will be libdbi */
Expand All @@ -6933,6 +6934,7 @@ int ndo2db_save_custom_variables(ndo2db_idi *idi,int table_idx, int o_id, char *
/* wtf is ts doing here? */
data[6] = (void *) &tstamp;

ndo2db_log_debug_info(NDO2DB_DEBUGL_PROCESSINFO, 2, "ndo2db_save_custom_variablestatus() instance_id=%lu, object_id=%lu, ts=%s, modified=%d, varname=%s, varvalue=%s\n", idi->dbinfo.instance_id, o_id, ts, has_been_modified, es[0], es[1]);
result = ido2db_query_insert_or_update_save_custom_variables_customvariablestatus_add(idi, data);

#ifndef USE_ORACLE /* everything else will be libdbi */
Expand Down
20 changes: 6 additions & 14 deletions module/idoutils/src/dbqueries.c
Expand Up @@ -1906,9 +1906,7 @@ int ido2db_query_insert_or_update_commentdata_history_add(ndo2db_idi *idi, void
#ifndef USE_ORACLE /* everything else will be libdbi */
switch (idi->dbinfo.server_type) {
case NDO2DB_DBSERVER_MYSQL:
asprintf(&query1, "INSERT INTO %s (entry_time, entry_time_usec, instance_id, comment_type, entry_type, object_id, comment_time, internal_comment_id,
author_name, comment_data, is_persistent, comment_source, expires, expiration_time) VALUES (%s, %lu, %lu, %d, %d, %lu, %s, %lu, '%s', '%s', %d, %d, %d, %s) ON DUPLICATE KE
Y UPDATE comment_type=%d, entry_type=%d, object_id=%lu, author_name='%s', comment_data='%s', is_persistent=%d, comment_source=%d, expires=%d, expiration_time=%s",
asprintf(&query1, "INSERT INTO %s (entry_time, entry_time_usec, instance_id, comment_type, entry_type, object_id, comment_time, internal_comment_id, author_name, comment_data, is_persistent, comment_source, expires, expiration_time) VALUES (%s, %lu, %lu, %d, %d, %lu, %s, %lu, '%s', '%s', %d, %d, %d, %s) ON DUPLICATE KEY UPDATE comment_type=%d, entry_type=%d, object_id=%lu, author_name='%s', comment_data='%s', is_persistent=%d, comment_source=%d, expires=%d, expiration_time=%s",
ndo2db_db_tablenames[NDO2DB_DBTABLE_COMMENTHISTORY],
*(char **) data[0], /* insert start */
*(unsigned long *) data[1],
Expand Down Expand Up @@ -1939,8 +1937,7 @@ Y UPDATE comment_type=%d, entry_type=%d, object_id=%lu, author_name='%s', commen
free(query1);
break;
case NDO2DB_DBSERVER_PGSQL:
asprintf(&query1, "UPDATE %s SET comment_type=%d, entry_type=%d, object_id=%lu, author_name='%s', comment_data='%s', is_persistent=%d, comment_sourc
e=%d, expires=%d, expiration_time=%s WHERE instance_id=%lu AND comment_time=%s AND internal_comment_id=%lu",
asprintf(&query1, "UPDATE %s SET comment_type=%d, entry_type=%d, object_id=%lu, author_name='%s', comment_data='%s', is_persistent=%d, comment_source=%d, expires=%d, expiration_time=%s WHERE instance_id=%lu AND comment_time=%s AND internal_comment_id=%lu",
ndo2db_db_tablenames[NDO2DB_DBTABLE_COMMENTHISTORY],
*(int *) data[3], /* update start */
*(int *) data[4],
Expand All @@ -1962,8 +1959,7 @@ e=%d, expires=%d, expiration_time=%s WHERE instance_id=%lu AND comment_time=%s A
/* check result if update was ok */
if(dbi_result_get_numrows_affected(idi->dbinfo.dbi_result) == 0) {
/* try insert instead */
asprintf(&query2, "INSERT INTO %s (entry_time, entry_time_usec, instance_id, comment_type, entry_type, object_id, comment_time, internal_com
ment_id, author_name, comment_data, is_persistent, comment_source, expires, expiration_time) VALUES (%s, %lu, %lu, %d, %d, %lu, %s, %lu, '%s', '%s', %d, %d, %d, %s)",
asprintf(&query2, "INSERT INTO %s (entry_time, entry_time_usec, instance_id, comment_type, entry_type, object_id, comment_time, internal_comment_id, author_name, comment_data, is_persistent, comment_source, expires, expiration_time) VALUES (%s, %lu, %lu, %d, %d, %lu, %s, %lu, '%s', '%s', %d, %d, %d, %s)",
ndo2db_db_tablenames[NDO2DB_DBTABLE_COMMENTHISTORY],
*(char **) data[0], /* insert start */
*(unsigned long *) data[1],
Expand Down Expand Up @@ -2248,9 +2244,7 @@ int ido2db_query_insert_or_update_downtimedata_downtime_history_add(ndo2db_idi *
#ifndef USE_ORACLE /* everything else will be libdbi */
switch (idi->dbinfo.server_type) {
case NDO2DB_DBSERVER_MYSQL:
asprintf(&query1, "INSERT INTO %s (instance_id, downtime_type, object_id, entry_time, author_name, comment_data, internal_downtime_id, triggered_by_
id, is_fixed, duration, scheduled_start_time, scheduled_end_time) VALUES (%lu, %d, %lu, %s, '%s', '%s', %lu, %lu, %d, %lu, %s, %s) ON DUPLICATE KEY UPDATE downtime_type=%d,
author_name='%s', comment_data='%s', triggered_by_id=%lu, is_fixed=%d, duration=%lu, scheduled_start_time=%s, scheduled_end_time=%s ",
asprintf(&query1, "INSERT INTO %s (instance_id, downtime_type, object_id, entry_time, author_name, comment_data, internal_downtime_id, triggered_by_id, is_fixed, duration, scheduled_start_time, scheduled_end_time) VALUES (%lu, %d, %lu, %s, '%s', '%s', %lu, %lu, %d, %lu, %s, %s) ON DUPLICATE KEY UPDATE downtime_type=%d, author_name='%s', comment_data='%s', triggered_by_id=%lu, is_fixed=%d, duration=%lu, scheduled_start_time=%s, scheduled_end_time=%s ",
ndo2db_db_tablenames[NDO2DB_DBTABLE_DOWNTIMEHISTORY],
*(unsigned long *) data[0], /* insert start */
*(int *) data[1],
Expand Down Expand Up @@ -2278,8 +2272,7 @@ id, is_fixed, duration, scheduled_start_time, scheduled_end_time) VALUES (%lu, %
free(query1);
break;
case NDO2DB_DBSERVER_PGSQL:
asprintf(&query1, "UPDATE %s SET downtime_type=%d, author_name='%s', comment_data='%s', triggered_by_id=%lu, is_fixed=%d, duration=%lu, scheduled_st
art_time=%s, scheduled_end_time=%s WHERE instance_id=%lu AND object_id=%lu AND entry_time=%s AND internal_downtime_id=%lu",
asprintf(&query1, "UPDATE %s SET downtime_type=%d, author_name='%s', comment_data='%s', triggered_by_id=%lu, is_fixed=%d, duration=%lu, scheduled_start_time=%s, scheduled_end_time=%s WHERE instance_id=%lu AND object_id=%lu AND entry_time=%s AND internal_downtime_id=%lu",
ndo2db_db_tablenames[NDO2DB_DBTABLE_DOWNTIMEHISTORY],
*(int *) data[1], /* update start */
*(char **) data[4],
Expand All @@ -2302,8 +2295,7 @@ art_time=%s, scheduled_end_time=%s WHERE instance_id=%lu AND object_id=%lu AND e
/* check result if update was ok */
if(dbi_result_get_numrows_affected(idi->dbinfo.dbi_result) == 0) {
/* try insert instead */
asprintf(&query2, "INSERT INTO %s (instance_id, downtime_type, object_id, entry_time, author_name, comment_data, internal_downtime_id, trigg
ered_by_id, is_fixed, duration, scheduled_start_time, scheduled_end_time) VALUES (%lu, %d, %lu, %s, '%s', '%s', %lu, %lu, %d, %lu, %s, %s)",
asprintf(&query2, "INSERT INTO %s (instance_id, downtime_type, object_id, entry_time, author_name, comment_data, internal_downtime_id, triggered_by_id, is_fixed, duration, scheduled_start_time, scheduled_end_time) VALUES (%lu, %d, %lu, %s, '%s', '%s', %lu, %lu, %d, %lu, %s, %s)",
ndo2db_db_tablenames[NDO2DB_DBTABLE_DOWNTIMEHISTORY],
*(unsigned long *) data[0], /* insert start */
*(int *) data[1],
Expand Down
38 changes: 24 additions & 14 deletions module/idoutils/src/ido2db.c
Expand Up @@ -111,16 +111,16 @@ int main(int argc, char **argv){
printf("\n");
printf("%s %s\n",NDO2DB_NAME,NDO2DB_VERSION);
printf("Copyright(c) 2005-2008 Ethan Galstad (nagios@nagios.org)\n");
printf("Copyright(c) 2009 Icinga Development Team (http://www.icinga.org)\n");
printf("Copyright(c) 2009-2010 Icinga Development Team (http://www.icinga.org)\n");
printf("Last Modified: %s\n",NDO2DB_DATE);
printf("License: GPL v2\n");
#ifdef HAVE_SSL
printf("SSL/TLS Available: Anonymous DH Mode, OpenSSL 0.9.6 or higher required\n");
#endif
printf("\n");
printf("Stores Icinga event and configuration data to a database for later retrieval\n");
printf("and processing. Clients that are capable of sending data to the NDO2DB daemon\n");
printf("include the LOG2NDO utility and NDOMOD event broker module.\n");
printf("and processing. Clients that are capable of sending data to the IDO2DB daemon\n");
printf("include the LOG2NDO utility and IDOMOD event broker module.\n");
printf("\n");
printf("Usage: %s -c <config_file> [-i] [-f]\n",argv[0]);
printf("\n");
Expand Down Expand Up @@ -442,27 +442,37 @@ int ndo2db_process_config_var(char *arg){
ndo2db_tcp_port=atoi(val);
}
else if(!strcmp(var,"db_servertype")){
if(!strcmp(val,"mysql"))
if(!strcmp(val,"mysql")) {
ndo2db_db_settings.server_type=NDO2DB_DBSERVER_MYSQL;
else if(!strcmp(val,"pgsql"))
ndo2db_db_settings.dbserver=strdup(val);
} else if(!strcmp(val,"pgsql")) {
ndo2db_db_settings.server_type=NDO2DB_DBSERVER_PGSQL;
else if(!strcmp(val,"db2"))
ndo2db_db_settings.dbserver=strdup(val);
} else if(!strcmp(val,"db2")) {
ndo2db_db_settings.server_type=NDO2DB_DBSERVER_DB2;
else if(!strcmp(val,"firebird"))
ndo2db_db_settings.dbserver=strdup(val);
} else if(!strcmp(val,"firebird")) {
ndo2db_db_settings.server_type=NDO2DB_DBSERVER_FIREBIRD;
else if(!strcmp(val,"freetds"))
ndo2db_db_settings.dbserver=strdup(val);
} else if(!strcmp(val,"freetds")) {
ndo2db_db_settings.server_type=NDO2DB_DBSERVER_FREETDS;
else if(!strcmp(val,"ingres"))
ndo2db_db_settings.dbserver=strdup(val);
} else if(!strcmp(val,"ingres")) {
ndo2db_db_settings.server_type=NDO2DB_DBSERVER_INGRES;
else if(!strcmp(val,"msql"))
ndo2db_db_settings.dbserver=strdup(val);
} else if(!strcmp(val,"msql")) {
ndo2db_db_settings.server_type=NDO2DB_DBSERVER_MSQL;
else if(!strcmp(val,"oracle"))
ndo2db_db_settings.dbserver=strdup(val);
} else if(!strcmp(val,"oracle")) {
ndo2db_db_settings.server_type=NDO2DB_DBSERVER_ORACLE;
else if(!strcmp(val,"sqlite"))
ndo2db_db_settings.dbserver=strdup(val);
} else if(!strcmp(val,"sqlite")) {
ndo2db_db_settings.server_type=NDO2DB_DBSERVER_SQLITE;
else if(!strcmp(val,"sqlite3"))
ndo2db_db_settings.dbserver=strdup(val);
} else if(!strcmp(val,"sqlite3")) {
ndo2db_db_settings.server_type=NDO2DB_DBSERVER_SQLITE3;
else
ndo2db_db_settings.dbserver=strdup(val);
} else
return NDO_ERROR;
}
else if(!strcmp(var,"db_host")){
Expand Down

0 comments on commit c9985f5

Please sign in to comment.