浏览代码

modules/db_mysql Added statistics (via counter framework) for mysql driver error

These errors are caused by lost connectivity to the server.
Marius Zbihlei 15 年之前
父节点
当前提交
7ca3941c12
共有 4 个文件被更改,包括 29 次插入1 次删除
  1. 5 0
      modules/db_mysql/km_dbase.c
  2. 3 1
      modules/db_mysql/km_my_con.c
  3. 10 0
      modules/db_mysql/mysql_mod.c
  4. 11 0
      modules/db_mysql/mysql_mod.h

+ 5 - 0
modules/db_mysql/km_dbase.c

@@ -81,6 +81,7 @@ static int db_mysql_submit_query(const db1_con_t* _h, const str* _s)
 		if ((t - CON_TIMESTAMP(_h)) > my_ping_interval) {
 		if ((t - CON_TIMESTAMP(_h)) > my_ping_interval) {
 			if (mysql_ping(CON_CONNECTION(_h))) {
 			if (mysql_ping(CON_CONNECTION(_h))) {
 				LM_WARN("driver error on ping: %s\n", mysql_error(CON_CONNECTION(_h)));
 				LM_WARN("driver error on ping: %s\n", mysql_error(CON_CONNECTION(_h)));
+				counter_inc(mysql_cnts_h.driver_err);
 			}
 			}
 		}
 		}
 		/*
 		/*
@@ -115,6 +116,10 @@ static int db_mysql_submit_query(const db1_con_t* _h, const str* _s)
 		}
 		}
 	}
 	}
 	LM_ERR("driver error on query: %s\n", mysql_error(CON_CONNECTION(_h)));
 	LM_ERR("driver error on query: %s\n", mysql_error(CON_CONNECTION(_h)));
+	/* Bad queries don't count */
+	if(code == CR_SERVER_GONE_ERROR || code == CR_SERVER_LOST) {
+		counter_inc(mysql_cnts_h.driver_err);
+	}
 	return -2;
 	return -2;
 }
 }
 
 

+ 3 - 1
modules/db_mysql/km_my_con.c

@@ -34,7 +34,7 @@
 #include "../../mem/mem.h"
 #include "../../mem/mem.h"
 #include "../../dprint.h"
 #include "../../dprint.h"
 #include "../../ut.h"
 #include "../../ut.h"
-
+#include "mysql_mod.h"
 
 
 /*! \brief
 /*! \brief
  * Create a new connection structure,
  * Create a new connection structure,
@@ -107,6 +107,8 @@ struct my_con* db_mysql_new_connection(const struct db_id* id)
 				id->database, id->port, 0, 0)) {
 				id->database, id->port, 0, 0)) {
 #endif
 #endif
 		LM_ERR("driver error: %s\n", mysql_error(ptr->con));
 		LM_ERR("driver error: %s\n", mysql_error(ptr->con));
+		/* increase error counter */
+		counter_inc(mysql_cnts_h.driver_err);
 		mysql_close(ptr->con);
 		mysql_close(ptr->con);
 		goto err;
 		goto err;
 	}
 	}

+ 10 - 0
modules/db_mysql/mysql_mod.c

@@ -57,6 +57,12 @@ unsigned int my_retries = 1;    /* Number of retries when command fails */
 
 
 unsigned long my_client_ver = 0;
 unsigned long my_client_ver = 0;
 
 
+struct mysql_counters_h mysql_cnts_h;
+counter_def_t mysql_cnt_defs[] =  {
+	{&mysql_cnts_h.driver_err, "Mysql driver erros", 0, 0, 0,
+		"incremented each time a Mysql error happened because the server/connection has failed."},
+	{0, 0, 0, 0, 0, 0 }
+};
 #define DEFAULT_MY_SEND_TO  2   /* in seconds */
 #define DEFAULT_MY_SEND_TO  2   /* in seconds */
 #define DEFAULT_MY_RECV_TO  4   /* in seconds */
 #define DEFAULT_MY_RECV_TO  4   /* in seconds */
 
 
@@ -145,8 +151,12 @@ static int mysql_mod_init(void)
 			" compiled against %ld)\n", MYSQL_VERSION_ID);
 			" compiled against %ld)\n", MYSQL_VERSION_ID);
 	}
 	}
 #endif
 #endif
+	if (counter_register_array("mysql", mysql_cnt_defs) < 0)
+		goto error;
 
 
 	return kam_mysql_mod_init();
 	return kam_mysql_mod_init();
+error:
+	return -1;
 }
 }
 
 
 /** @} */
 /** @} */

+ 11 - 0
modules/db_mysql/mysql_mod.h

@@ -36,10 +36,21 @@
 #ifndef _MYSQL_MOD_H
 #ifndef _MYSQL_MOD_H
 #define _MYSQL_MOD_H
 #define _MYSQL_MOD_H
 
 
+#include "../../counters.h"
+
+/* counter struct
+*/
+struct mysql_counters_h {
+    counter_handle_t driver_err;
+};
+/* defined in km_dbase.c */
+extern struct mysql_counters_h mysql_cnts_h;
+
 /** @defgroup mysql MySQL db driver
 /** @defgroup mysql MySQL db driver
  *  @ingroup DB_API
  *  @ingroup DB_API
  */
  */
 /** @{ */
 /** @{ */
+
 extern int my_ping_interval;
 extern int my_ping_interval;
 extern unsigned int my_connect_to;
 extern unsigned int my_connect_to;
 extern unsigned int my_send_to;
 extern unsigned int my_send_to;