[icinga-checkins] icinga.org: icinga-core/tdressler/oci_rewrite: manual merge db.c with core

git at icinga.org git at icinga.org
Sun Jun 5 18:12:15 CEST 2011


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

Author: Thomas Dressler <tdressler at tdressler.net>
Date:   Sun Apr  3 17:04:16 2011 +0200

manual merge db.c with core

---

 module/idoutils/src/db.c |   87 +++++++++++++++++++++++++++++++++++++++-------
 1 files changed, 74 insertions(+), 13 deletions(-)

diff --git a/module/idoutils/src/db.c b/module/idoutils/src/db.c
old mode 100644
new mode 100755
index 3c58fbc..42c5a6a
--- a/module/idoutils/src/db.c
+++ b/module/idoutils/src/db.c
@@ -15,7 +15,14 @@
 #include "../include/ido2db.h"
 #include "../include/dbhandlers.h"
 #include "../include/db.h"
-/*oracle specific headers*/
+
+extern int errno;
+
+extern int ido2db_log_debug_info(int , int , const char *, ...);
+
+int dummy;	/* reduce compiler warnings */
+
+/* point to prepared statements after db initialize */
 #ifdef USE_ORACLE
 #include "../include/ido2db_oci.h"
 /*
@@ -25,8 +32,6 @@ extern int ido2db_oci_prepared_statement_bind_null_param(OCI_Statement *, char *
 */
 #endif
 
-extern int errno;
-extern int ido2db_log_debug_info(int , int , const char *, ...);
 extern ido2db_dbconfig ido2db_db_settings;
 extern time_t ido2db_db_last_checkin_time;
 
@@ -269,6 +274,47 @@ int ido2db_db_deinit(ido2db_idi *idi) {
 /************************************/
 /* connects to the database server  */
 /************************************/
+
+int ido2db_db_is_connected(ido2db_idi *idi) {
+
+#ifdef USE_LIBDBI
+	if(!dbi_conn_ping(idi->dbinfo.dbi_conn))
+		return IDO_FALSE;
+#endif
+
+#ifdef USE_PGSQL
+	if(PQstatus(idi->dbinfo.pg_conn)!=CONNECTION_OK)
+		return IDO_FALSE;
+#endif
+
+#ifdef USE_ORACLE
+	if(!OCI_IsConnected(idi->dbinfo.oci_connection))
+		return IDO_FALSE;
+#endif
+
+	return IDO_TRUE;
+}
+
+int ido2db_db_reconnect(ido2db_idi *idi) {
+
+	/* check connection */
+	if(ido2db_db_is_connected(idi)==IDO_FALSE)
+		idi->dbinfo.connected=IDO_FALSE;
+
+        /* try to reconnect... */
+        if(idi->dbinfo.connected==IDO_FALSE) {
+                if(ido2db_db_connect(idi)==IDO_ERROR){
+			ido2db_log_debug_info(IDO2DB_DEBUGL_PROCESSINFO, 2, "ido2db_db_reconnect(): failed.\n");
+			syslog(LOG_USER | LOG_INFO, "Error: Could not reconnect to database!");
+                        return IDO_ERROR;
+		}
+                ido2db_db_hello(idi);
+        }
+
+	return IDO_OK;
+}
+
+
 int ido2db_db_connect(ido2db_idi *idi) {
 	int result = IDO_OK;
 #ifdef USE_PGSQL /* pgsql */
@@ -282,8 +328,8 @@ int ido2db_db_connect(ido2db_idi *idi) {
 	if (idi == NULL)
 		return IDO_ERROR;
 
-	/* we're already connected... */
-	if (idi->dbinfo.connected == IDO_TRUE){
+	/* we're already connected... (and we don't wanna double check with ido2db_db_is_connected) */
+	if(idi->dbinfo.connected==IDO_TRUE){
 		ido2db_log_debug_info(IDO2DB_DEBUGL_PROCESSINFO, 2, "\tido2db_db_connect(): already connected. Dropping out.\n");
 		return IDO_OK;
 	}
@@ -353,6 +399,23 @@ int ido2db_db_connect(ido2db_idi *idi) {
 	dbi_conn_set_option(idi->dbinfo.dbi_conn, "dbname", ido2db_db_settings.dbname);
 	dbi_conn_set_option(idi->dbinfo.dbi_conn, "encoding", "auto");
 
+	if(ido2db_db_settings.dbsocket!=NULL){
+		/* a local db socket was desired, drop db_port settings in case */
+		dbi_conn_clear_option(idi->dbinfo.dbi_conn, "port");
+
+	        switch (idi->dbinfo.server_type) {
+	        case IDO2DB_DBSERVER_MYSQL:
+	                dbi_conn_set_option(idi->dbinfo.dbi_conn, "mysql_unix_socket", ido2db_db_settings.dbsocket);
+			break;
+		case IDO2DB_DBSERVER_PGSQL:
+			/* override the port as stated in libdbi-driver docs */
+	                dbi_conn_set_option(idi->dbinfo.dbi_conn, "port", ido2db_db_settings.dbsocket);
+			break;
+		default:
+			break;
+		}
+	}
+
 	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 %s database: %s", ido2db_db_settings.dbserver, dbi_error);
@@ -366,7 +429,7 @@ int ido2db_db_connect(ido2db_idi *idi) {
 
 #ifdef USE_PGSQL /* pgsql */
 
-	asprintf(&temp_port, "%d", ido2db_db_settings.port);
+	dummy=asprintf(&temp_port, "%d", ido2db_db_settings.port);
 	ido2db_log_debug_info(IDO2DB_DEBUGL_PROCESSINFO, 2, "ido2db_db_connect() pgsql start\n");
 
         /* check if config matches */
@@ -440,7 +503,7 @@ int ido2db_db_disconnect(ido2db_idi *idi) {
 		return IDO_ERROR;
 
 	/* we're not connected... */
-	if (idi->dbinfo.connected == IDO_FALSE)
+	if(ido2db_db_is_connected(idi)==IDO_FALSE)
 		return IDO_OK;
 
 #ifdef USE_LIBDBI /* everything else will be libdbi */
@@ -1686,11 +1749,9 @@ int ido2db_db_query(ido2db_idi *idi, char *buf) {
 		return IDO_ERROR;
 
 	/* if we're not connected, try and reconnect... */
-	if (idi->dbinfo.connected == IDO_FALSE) {
-		if (ido2db_db_connect(idi) == IDO_ERROR)
-			return IDO_ERROR;
-		ido2db_db_hello(idi);
-	}
+        if(ido2db_db_reconnect(idi)==IDO_ERROR)
+                return IDO_ERROR;
+
 
 #ifdef DEBUG_IDO2DB_QUERIES
 	printf("%s\n\n",buf);
@@ -1787,7 +1848,7 @@ int ido2db_handle_db_error(ido2db_idi *idi) {
 		return IDO_ERROR;
 
 	/* we're not currently connected... */
-	if (idi->dbinfo.connected == IDO_FALSE)
+	if(ido2db_db_is_connected(idi)==IDO_TRUE)
 		return IDO_OK;
 
 	ido2db_db_disconnect(idi);





More information about the icinga-checkins mailing list