[icinga-checkins] icinga.org: icinga-core/master: idoutils: add dbversion check in ido2db and print error to syslog in case of non-matching #633

git at icinga.org git at icinga.org
Mon Jan 10 16:19:18 CET 2011


Module: icinga-core
Branch: master
Commit: aac76ccd58bfce8e3ca5f7515c87c39b4bc31335
URL:    https://git.icinga.org/?p=icinga-core.git;a=commit;h=aac76ccd58bfce8e3ca5f7515c87c39b4bc31335

Author: Michael Friedrich <michael.friedrich at univie.ac.at>
Date:   Mon Jan 10 16:10:51 2011 +0100

idoutils: add dbversion check in ido2db and print error to syslog in case of non-matching #633

check the issue at https://dev.icinga.org/issues/633
for tests and explainations.

fixes #633

---

 Changelog                        |    1 +
 module/idoutils/include/db.h     |    3 +-
 module/idoutils/include/ido2db.h |    3 +
 module/idoutils/src/db.c         |  127 ++++++++++++++++++++++++++++++++++++--
 4 files changed, 128 insertions(+), 6 deletions(-)

diff --git a/Changelog b/Changelog
index 2127cd9..38df0ee 100644
--- a/Changelog
+++ b/Changelog
@@ -33,6 +33,7 @@ ENHANCEMENTS
 * idoutils: add check for configuration file availability for idomod and ido2db (idea from Ricardo Maraschini) #1087
 * idoutils: add cfg option to enable/disable oci errors to syslog through error handler #1060
 * idoutils: add dbversion upgrade scripts for 1.3 #633
+* idoutils: add dbversion check in ido2db and print error to syslog in case of non-matching #633
 
 * contrib: bring daemonchk.cgi building into shape (Elan Ruusamäe) #1051
 
diff --git a/module/idoutils/include/db.h b/module/idoutils/include/db.h
index 65cb3c3..231d1ec 100644
--- a/module/idoutils/include/db.h
+++ b/module/idoutils/include/db.h
@@ -107,8 +107,9 @@ typedef struct ido2db_dbconfig_struct{
 #define IDO2DB_DBTABLE_SERVICECONTACTGROUPS           65
 #define IDO2DB_DBTABLE_HOSTESCALATIONCONTACTGROUPS    66
 #define IDO2DB_DBTABLE_SERVICEESCALATIONCONTACTGROUPS 67
+#define IDO2DB_DBTABLE_DBVERSION                      68
 
-#define IDO2DB_MAX_DBTABLES                           68
+#define IDO2DB_MAX_DBTABLES                           69
 
 
 /**************** Object types *****************/
diff --git a/module/idoutils/include/ido2db.h b/module/idoutils/include/ido2db.h
index bf83ae0..c2a73d8 100644
--- a/module/idoutils/include/ido2db.h
+++ b/module/idoutils/include/ido2db.h
@@ -189,6 +189,9 @@ typedef struct ido2db_dbconninfo_struct{
 	OCI_Statement* oci_statement_instances_delete;
 	OCI_Statement* oci_statement_instances_delete_time;
 
+	/* dbversion */
+	OCI_Statement* oci_statement_dbversion_select;
+
 #endif /* Oracle ocilib specific */
 	unsigned long instance_id;
 	unsigned long conninfo_id;
diff --git a/module/idoutils/src/db.c b/module/idoutils/src/db.c
index 7c8d9e2..11155ba 100644
--- a/module/idoutils/src/db.c
+++ b/module/idoutils/src/db.c
@@ -111,6 +111,9 @@ int ido2db_oci_prepared_statement_downtime_delete(ido2db_idi *idi);
 int ido2db_oci_prepared_statement_instances_delete(ido2db_idi *idi);
 int ido2db_oci_prepared_statement_instances_delete_time(ido2db_idi *idi);
 
+/* db version stuff */
+int ido2db_oci_prepared_statement_dbversion_select(ido2db_idi *idi);
+
 #endif
 
 extern ido2db_dbconfig ido2db_db_settings;
@@ -185,10 +188,11 @@ char *ido2db_db_rawtablenames[IDO2DB_MAX_DBTABLES]={
 	"service_contactgroups",
 	"hostescalation_contactgroups",
 #ifdef USE_ORACLE /* Oracle ocilib specific */
-	"serviceescalationcontactgroups"
+	"serviceescalationcontactgroups",
 #else /* everything else will be libdbi */
-	"serviceescalation_contactgroups"
+	"serviceescalation_contactgroups",
 #endif /* Oracle ocilib specific */
+	"dbversion"
 	};
 
 char *ido2db_db_tablenames[IDO2DB_MAX_DBTABLES];
@@ -999,6 +1003,11 @@ int ido2db_db_connect(ido2db_idi *idi) {
                 return IDO_ERROR;
         }
 
+        /* dbversion */
+        if(ido2db_oci_prepared_statement_dbversion_select(idi) == IDO_ERROR) {
+                ido2db_log_debug_info(IDO2DB_DEBUGL_PROCESSINFO, 2, "ido2db_oci_prepared_statement_dbversion_select() failed\n");
+                return IDO_ERROR;
+        }
 
 	ido2db_log_debug_info(IDO2DB_DEBUGL_PROCESSINFO, 2, "ido2db_db_connect() prepare statements end\n");
 
@@ -1120,6 +1129,8 @@ int ido2db_db_disconnect(ido2db_idi *idi) {
 	OCI_StatementFree(idi->dbinfo.oci_statement_instances_delete);
 	OCI_StatementFree(idi->dbinfo.oci_statement_instances_delete_time);
 
+	OCI_StatementFree(idi->dbinfo.oci_statement_dbversion_select);
+
 	syslog(LOG_USER | LOG_INFO, "Successfully freed prepared statements");
 
 	/* close db connection */
@@ -1139,8 +1150,80 @@ int ido2db_db_disconnect(ido2db_idi *idi) {
 /************************************/
 
 int ido2db_db_version_check(ido2db_idi *idi) {
+        char *buf=NULL;
+	char *dbversion=NULL;
+	char *name=NULL;
+	void *data[1];
+
+	ido2db_log_debug_info(IDO2DB_DEBUGL_PROCESSINFO, 2, "ido2db_db_version_check () start \n");
+
+	name=strdup("idoutils");
+        data[0] = (void *) &name;
+
+#ifdef USE_LIBDBI
+
+        if (asprintf(&buf, "SELECT version FROM %s WHERE name='%s'", ido2db_db_tablenames[IDO2DB_DBTABLE_DBVERSION], name) == -1)
+                buf = NULL;
+
+        if ((ido2db_db_query(idi, buf))==IDO_OK) {
+
+                if (idi->dbinfo.dbi_result!=NULL) {
+                        if (dbi_result_next_row(idi->dbinfo.dbi_result)) {
+                                idi->dbinfo.dbversion = dbi_result_get_string(idi->dbinfo.dbi_result, "version");
+                        }
+                }
+        }
+
+	free(buf);
+#endif
+
+#ifdef USE_ORACLE
+
+
+        if(!OCI_BindString(idi->dbinfo.oci_statement_dbversion_select, MT(":X1"), *(char **) data[0], 0)) {
+                return IDO_ERROR;
+        }
+
+        /* execute statement */
+        if(!OCI_Execute(idi->dbinfo.oci_statement_dbversion_select)) {
+                ido2db_log_debug_info(IDO2DB_DEBUGL_PROCESSINFO, 2, "ido2db_db_version_check () \n");
+        }
+
+        /* commit statement */
+        OCI_Commit(idi->dbinfo.oci_connection);
+
+        /* do not free statement yet! */
+
+        idi->dbinfo.oci_resultset = OCI_GetResultset(idi->dbinfo.oci_statement_dbversion_select);
+
+        ido2db_log_debug_info(IDO2DB_DEBUGL_PROCESSINFO, 2, "ido2db_db_version_check() query ok\n");
+
+        if(OCI_FetchNext(idi->dbinfo.oci_resultset)) {
+                ido2db_log_debug_info(IDO2DB_DEBUGL_PROCESSINFO, 2, "ido2db_db_version_check() fetchnext ok\n");
+                idi->dbinfo.dbversion=OCI_GetString(idi->dbinfo.oci_resultset, 1);
+        } else {
+                ido2db_log_debug_info(IDO2DB_DEBUGL_PROCESSINFO, 2, "ido2db_db_version_check() fetchnext not ok\n");
+        }
+        ido2db_log_debug_info(IDO2DB_DEBUGL_PROCESSINFO, 2, "ido2db_db_hello(version=%s)\n", dbversion);
+
+
+#endif
+
+#ifdef USE_PGSQL
 
 	//FIXME
+#endif
+
+	free(name);
+
+	/* check dbversion against program version */
+	if(strcmp(idi->dbinfo.dbversion, IDO2DB_VERSION)!=0){
+		syslog(LOG_ERR, "Error: DB Version %s does not match program version %s. Please check the upgrade docs!", idi->dbinfo.dbversion, IDO2DB_VERSION);
+		return IDO_ERROR;
+	}
+
+	ido2db_log_debug_info(IDO2DB_DEBUGL_PROCESSINFO, 2, "ido2db_db_version_check () end\n");
+
 	return IDO_OK;
 
 }
@@ -1166,9 +1249,7 @@ int ido2db_db_hello(ido2db_idi *idi) {
 		idi->instance_name = strdup("default");
 
 
-	if(ido2db_db_version_check(idi)==IDO_ERROR){
-		syslog(LOG_USER | LOG_INFO, "Error: DB Version %s does not match %s. Please read the upgrade docs!", idi->dbinfo.dbversion, IDO2DB_VERSION);
-	}
+	ido2db_db_version_check(idi);
 
 #ifdef USE_LIBDBI /* everything else will be libdbi */
 
@@ -2910,6 +2991,42 @@ int ido2db_oci_prepared_statement_instances_select(ido2db_idi *idi) {
 }
 
 
+int ido2db_oci_prepared_statement_dbversion_select(ido2db_idi *idi) {
+
+        char *buf = NULL;
+
+        //ido2db_log_debug_info(IDO2DB_DEBUGL_PROCESSINFO, 2, "ido2db_oci_prepared_statement_dbversion_select() start\n");
+
+        if(asprintf(&buf, "SELECT version FROM %s WHERE name=:X1",
+                ido2db_db_tablenames[IDO2DB_DBTABLE_DBVERSION]) == -1) {
+                        buf = NULL;
+        }
+
+        //ido2db_log_debug_info(IDO2DB_DEBUGL_PROCESSINFO, 2, "ido2db_oci_prepared_statement_dbversion_select() query: %s\n", buf);
+
+        if(idi->dbinfo.oci_connection) {
+
+                idi->dbinfo.oci_statement_dbversion_select = OCI_StatementCreate(idi->dbinfo.oci_connection);
+
+                /* allow rebinding values */
+                OCI_AllowRebinding(idi->dbinfo.oci_statement_dbversion_select, 1);
+
+                if(!OCI_Prepare(idi->dbinfo.oci_statement_dbversion_select, MT(buf))) {
+                        free(buf);
+                        return IDO_ERROR;
+                }
+        } else {
+                free(buf);
+                return IDO_ERROR;
+        }
+        free(buf);
+
+        //ido2db_log_debug_info(IDO2DB_DEBUGL_PROCESSINFO, 2, "ido2db_oci_prepared_statement_dbversion_select() end\n");
+
+        return IDO_OK;
+}
+
+
 /************************************/
 /* CONNINFO                         */
 /************************************/





More information about the icinga-checkins mailing list