diff --git a/configure.in b/configure.in index c152a378c0..3561dc8f84 100644 --- a/configure.in +++ b/configure.in @@ -2282,7 +2282,7 @@ then fi if test "x$with_libmysql" = "xyes" then - with_mysql_libs=`$with_mysql_config --libs 2>/dev/null` + with_mysql_libs=`$with_mysql_config --libs_r 2>/dev/null` mysql_config_status=$? if test $mysql_config_status -ne 0 diff --git a/contrib/write_mysql.sql b/contrib/write_mysql.sql index 0d1f84c78c..684740af36 100644 --- a/contrib/write_mysql.sql +++ b/contrib/write_mysql.sql @@ -1,9 +1,3 @@ --- MySQL dump 10.11 --- --- Host: localhost Database: collectd --- ------------------------------------------------------ --- Server version 5.0.77 - /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; @@ -28,30 +22,45 @@ USE `collectd`; -- DROP TABLE IF EXISTS `data`; -SET @saved_cs_client = @@character_set_client; -SET character_set_client = utf8; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; CREATE TABLE `data` ( - `id` bigint(20) NOT NULL auto_increment, - `timestamp` double NOT NULL, + `id` bigint(20) NOT NULL AUTO_INCREMENT, + `date` datetime NOT NULL, `host_id` int(11) NOT NULL, `plugin_id` int(11) NOT NULL, - `plugin_instance` varchar(255) default NULL, + `plugin_instance` varchar(255) DEFAULT NULL, `type_id` int(11) NOT NULL, - `typeinstance` varchar(255) default NULL, + `type_instance` varchar(255) DEFAULT NULL, `dataset_id` int(11) NOT NULL, `value` double NOT NULL, - PRIMARY KEY (`id`), - KEY `timestamp` (`timestamp`), + PRIMARY KEY (`id`), + UNIQUE KEY `host_id_3` (`host_id`,`plugin_id`,`plugin_instance`,`type_id`,`type_instance`,`dataset_id`), KEY `host_id` (`host_id`), KEY `plugin_id` (`plugin_id`), KEY `type_id` (`type_id`), - KEY `typeinstance_id` (`typeinstance`), - KEY `dataset_id` (`dataset_id`), - CONSTRAINT `data_ibfk_1` FOREIGN KEY (`host_id`) REFERENCES `host` (`id`) ON DELETE CASCADE ON UPDATE CASCADE, - CONSTRAINT `data_ibfk_2` FOREIGN KEY (`plugin_id`) REFERENCES `plugin` (`id`) ON DELETE CASCADE ON UPDATE CASCADE, - CONSTRAINT `data_ibfk_3` FOREIGN KEY (`type_id`) REFERENCES `type` (`id`) ON DELETE CASCADE ON UPDATE CASCADE, - CONSTRAINT `data_ibfk_4` FOREIGN KEY (`dataset_id`) REFERENCES `dataset` (`id`) ON DELETE CASCADE ON UPDATE CASCADE -) ENGINE=InnoDB AUTO_INCREMENT=51 DEFAULT CHARSET=utf8; + KEY `dataset_id` (`dataset_id`) +) ENGINE=MEMORY AUTO_INCREMENT=0 DEFAULT CHARSET=utf8; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Temporary table structure for view `data_view` +-- + +DROP TABLE IF EXISTS `data_view`; +/*!50001 DROP VIEW IF EXISTS `data_view`*/; +SET @saved_cs_client = @@character_set_client; +SET character_set_client = utf8; +/*!50001 CREATE TABLE `data_view` ( + `date` datetime, + `host` varchar(255), + `plugin` varchar(255), + `plugin_instance` varchar(255), + `type` varchar(255), + `type_instance` varchar(255), + `dataset_name` varchar(255), + `value` double +) ENGINE=MyISAM */; SET character_set_client = @saved_cs_client; -- @@ -59,36 +68,99 @@ SET character_set_client = @saved_cs_client; -- DROP TABLE IF EXISTS `dataset`; -SET @saved_cs_client = @@character_set_client; -SET character_set_client = utf8; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; CREATE TABLE `dataset` ( - `id` int(11) NOT NULL auto_increment, + `id` int(11) NOT NULL AUTO_INCREMENT, `type_id` int(11) NOT NULL, `name` varchar(255) NOT NULL, `type` enum('COUNTER','GAUGE','DERIVE','ABSOLUTE') NOT NULL, `min` double NOT NULL, `max` double NOT NULL, - PRIMARY KEY (`id`), + PRIMARY KEY (`id`), UNIQUE KEY `id` (`id`,`type_id`), KEY `name` (`name`), KEY `type_id` (`type_id`), CONSTRAINT `dataset_ibfk_1` FOREIGN KEY (`type_id`) REFERENCES `type` (`id`) ON DELETE CASCADE ON UPDATE CASCADE -) ENGINE=InnoDB AUTO_INCREMENT=13 DEFAULT CHARSET=utf8; -SET character_set_client = @saved_cs_client; +) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Table structure for table `grouped_type` +-- + +DROP TABLE IF EXISTS `grouped_type`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `grouped_type` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `name` varchar(255) CHARACTER SET latin1 NOT NULL, + PRIMARY KEY (`id`), + UNIQUE KEY `name` (`name`) +) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8; +/*!40101 SET character_set_client = @saved_cs_client */; -- -- Table structure for table `host` -- DROP TABLE IF EXISTS `host`; -SET @saved_cs_client = @@character_set_client; -SET character_set_client = utf8; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; CREATE TABLE `host` ( - `id` int(11) NOT NULL auto_increment, + `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(255) NOT NULL, - PRIMARY KEY (`id`), - UNIQUE KEY `name` (`name`) -) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8; + `nbcpu` int(11) NOT NULL DEFAULT '0', + `load` int(11) NOT NULL DEFAULT '0', + UNIQUE KEY `id` (`id`,`name`) +) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Table structure for table `notification` +-- + +DROP TABLE IF EXISTS `notification`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `notification` ( + `id` bigint(20) NOT NULL AUTO_INCREMENT, + `date` datetime NOT NULL, + `host_id` int(11) NOT NULL, + `plugin_id` int(11) NOT NULL, + `plugin_instance` varchar(255) DEFAULT NULL, + `type_id` int(11) NOT NULL, + `type_instance` varchar(255) DEFAULT NULL, + `severity` enum('FAILURE','WARNING','OKAY','UNKNOW') NOT NULL, + `message` mediumtext, + PRIMARY KEY (`id`,`date`), + KEY `type_instance` (`type_instance`), + KEY `plugin_instance` (`plugin_instance`), + KEY `severity` (`severity`), + KEY `host_id_2` (`host_id`,`plugin_id`,`type_id`) +) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8 +/*!50100 PARTITION BY HASH (MONTH(`date`)) +PARTITIONS 12 */; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Temporary table structure for view `notification_view` +-- + +DROP TABLE IF EXISTS `notification_view`; +/*!50001 DROP VIEW IF EXISTS `notification_view`*/; +SET @saved_cs_client = @@character_set_client; +SET character_set_client = utf8; +/*!50001 CREATE TABLE `notification_view` ( + `date` datetime, + `hostname` varchar(255), + `plugin` varchar(255), + `plugin_instance` varchar(255), + `type` varchar(255), + `type_instance` varchar(255), + `severity` enum('FAILURE','WARNING','OKAY','UNKNOW'), + `message` mediumtext +) ENGINE=MyISAM */; SET character_set_client = @saved_cs_client; -- @@ -96,41 +168,139 @@ SET character_set_client = @saved_cs_client; -- DROP TABLE IF EXISTS `plugin`; -SET @saved_cs_client = @@character_set_client; -SET character_set_client = utf8; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; CREATE TABLE `plugin` ( - `id` int(11) NOT NULL auto_increment, + `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(255) NOT NULL, - PRIMARY KEY (`id`), + PRIMARY KEY (`id`), UNIQUE KEY `name` (`name`), UNIQUE KEY `id` (`id`,`name`) -) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8; +) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Temporary table structure for view `plugin_view` +-- + +DROP TABLE IF EXISTS `plugin_view`; +/*!50001 DROP VIEW IF EXISTS `plugin_view`*/; +SET @saved_cs_client = @@character_set_client; +SET character_set_client = utf8; +/*!50001 CREATE TABLE `plugin_view` ( + `host` varchar(255), + `plugin` varchar(255), + `plugin_instance` varchar(255), + `type` varchar(255), + `type_instance` varchar(255) +) ENGINE=MyISAM */; SET character_set_client = @saved_cs_client; + -- -- Table structure for table `type` -- DROP TABLE IF EXISTS `type`; -SET @saved_cs_client = @@character_set_client; -SET character_set_client = utf8; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; CREATE TABLE `type` ( - `id` int(11) NOT NULL auto_increment, + `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(255) NOT NULL, - PRIMARY KEY (`id`), + PRIMARY KEY (`id`), UNIQUE KEY `name` (`name`), UNIQUE KEY `id` (`id`,`name`) -) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8; +) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Temporary table structure for view `types_db` +-- + +DROP TABLE IF EXISTS `types_db`; +/*!50001 DROP VIEW IF EXISTS `types_db`*/; +SET @saved_cs_client = @@character_set_client; +SET character_set_client = utf8; +/*!50001 CREATE TABLE `types_db` ( + `type` varchar(255), + `ds` varchar(255) +) ENGINE=MyISAM */; SET character_set_client = @saved_cs_client; -/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */; -/*!40101 SET SQL_MODE=@OLD_SQL_MODE */; -/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */; -/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */; -/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; -/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; -/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; -/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */; +-- +-- Final view structure for view `data_view` +-- + +/*!50001 DROP TABLE IF EXISTS `data_view`*/; +/*!50001 DROP VIEW IF EXISTS `data_view`*/; +/*!50001 SET @saved_cs_client = @@character_set_client */; +/*!50001 SET @saved_cs_results = @@character_set_results */; +/*!50001 SET @saved_col_connection = @@collation_connection */; +/*!50001 SET character_set_client = utf8 */; +/*!50001 SET character_set_results = utf8 */; +/*!50001 SET collation_connection = utf8_general_ci */; +/*!50001 CREATE ALGORITHM=UNDEFINED */ +/*!50013 DEFINER=`root`@`localhost` SQL SECURITY DEFINER */ +/*!50001 VIEW `data_view` AS select `data`.`date` AS `date`,`host`.`name` AS `host`,`plugin`.`name` AS `plugin`,`data`.`plugin_instance` AS `plugin_instance`,`type`.`name` AS `type`,`data`.`type_instance` AS `type_instance`,`dataset`.`name` AS `dataset_name`,`data`.`value` AS `value` from ((((`data` join `host`) join `plugin`) join `type`) join `dataset`) where ((`host`.`id` = `data`.`host_id`) and (`plugin`.`id` = `data`.`plugin_id`) and (`type`.`id` = `data`.`type_id`) and (`data`.`dataset_id` = `dataset`.`id`)) */; +/*!50001 SET character_set_client = @saved_cs_client */; +/*!50001 SET character_set_results = @saved_cs_results */; +/*!50001 SET collation_connection = @saved_col_connection */; + +-- +-- Final view structure for view `notification_view` +-- + +/*!50001 DROP TABLE IF EXISTS `notification_view`*/; +/*!50001 DROP VIEW IF EXISTS `notification_view`*/; +/*!50001 SET @saved_cs_client = @@character_set_client */; +/*!50001 SET @saved_cs_results = @@character_set_results */; +/*!50001 SET @saved_col_connection = @@collation_connection */; +/*!50001 SET character_set_client = utf8 */; +/*!50001 SET character_set_results = utf8 */; +/*!50001 SET collation_connection = utf8_general_ci */; +/*!50001 CREATE ALGORITHM=UNDEFINED */ +/*!50013 DEFINER=`root`@`localhost` SQL SECURITY DEFINER */ +/*!50001 VIEW `notification_view` AS select `n`.`date` AS `date`,`h`.`name` AS `hostname`,`p`.`name` AS `plugin`,`n`.`plugin_instance` AS `plugin_instance`,`t`.`name` AS `type`,`n`.`type_instance` AS `type_instance`,`n`.`severity` AS `severity`,`n`.`message` AS `message` from (((`notification` `n` join `host` `h`) join `plugin` `p`) join `type` `t`) where ((`n`.`host_id` = `h`.`id`) and (`n`.`plugin_id` = `p`.`id`) and (`n`.`type_id` = `t`.`id`)) */; +/*!50001 SET character_set_client = @saved_cs_client */; +/*!50001 SET character_set_results = @saved_cs_results */; +/*!50001 SET collation_connection = @saved_col_connection */; + +-- +-- Final view structure for view `plugin_view` +-- + +/*!50001 DROP TABLE IF EXISTS `plugin_view`*/; +/*!50001 DROP VIEW IF EXISTS `plugin_view`*/; +/*!50001 SET @saved_cs_client = @@character_set_client */; +/*!50001 SET @saved_cs_results = @@character_set_results */; +/*!50001 SET @saved_col_connection = @@collation_connection */; +/*!50001 SET character_set_client = utf8 */; +/*!50001 SET character_set_results = utf8 */; +/*!50001 SET collation_connection = utf8_general_ci */; +/*!50001 CREATE ALGORITHM=UNDEFINED */ +/*!50013 DEFINER=`root`@`localhost` SQL SECURITY DEFINER */ +/*!50001 VIEW `plugin_view` AS select `host`.`name` AS `host`,`plugin`.`name` AS `plugin`,`data`.`plugin_instance` AS `plugin_instance`,`type`.`name` AS `type`,`data`.`type_instance` AS `type_instance` from (((`data` join `host`) join `plugin`) join `type`) where ((`host`.`id` = `data`.`host_id`) and (`plugin`.`id` = `data`.`plugin_id`) and (`type`.`id` = `data`.`type_id`)) */; +/*!50001 SET character_set_client = @saved_cs_client */; +/*!50001 SET character_set_results = @saved_cs_results */; +/*!50001 SET collation_connection = @saved_col_connection */; + + +-- +-- Final view structure for view `types_db` +-- --- Dump completed on 2011-11-07 12:57:15 +/*!50001 DROP TABLE IF EXISTS `types_db`*/; +/*!50001 DROP VIEW IF EXISTS `types_db`*/; +/*!50001 SET @saved_cs_client = @@character_set_client */; +/*!50001 SET @saved_cs_results = @@character_set_results */; +/*!50001 SET @saved_col_connection = @@collation_connection */; +/*!50001 SET character_set_client = utf8 */; +/*!50001 SET character_set_results = utf8 */; +/*!50001 SET collation_connection = utf8_general_ci */; +/*!50001 CREATE ALGORITHM=UNDEFINED */ +/*!50013 DEFINER=`root`@`localhost` SQL SECURITY DEFINER */ +/*!50001 VIEW `types_db` AS select `type`.`name` AS `type`,`dataset`.`name` AS `ds` from (`type` join `dataset`) where (`dataset`.`type_id` = `type`.`id`) order by `type`.`name`,`dataset`.`id` */; +/*!50001 SET character_set_client = @saved_cs_client */; +/*!50001 SET character_set_results = @saved_cs_results */; +/*!50001 SET collation_connection = @saved_col_connection */; diff --git a/src/collectd.conf.pod b/src/collectd.conf.pod index 7274b1fe60..23efe87424 100644 --- a/src/collectd.conf.pod +++ b/src/collectd.conf.pod @@ -4888,13 +4888,20 @@ F. Synopsis of the configuration is: - Host "localhost" - User "root" - Passwd "" - Database "collectd" - Port "3306" + + Host "localhost" + User "root" + Passwd "" + Database "collectd" + Port "3306" + Replace true + +A B block defines one connection to a MySQL database. It accepts a +single argument which specifies the name of the database. Repeat this option +to specify multiple database. + =over 4 =item B I diff --git a/src/write_mysql.c b/src/write_mysql.c index 95229220a1..097dd82be7 100644 --- a/src/write_mysql.c +++ b/src/write_mysql.c @@ -36,8 +36,8 @@ #include -static c_avl_tree_t *host_tree, *plugin_tree, *type_tree, *dataset_tree = - NULL; +//static c_avl_tree_t *host_tree, *plugin_tree, *type_tree, *dataset_tree = +// NULL; typedef struct dataset_s dataset_t; struct dataset_s @@ -47,6 +47,32 @@ struct dataset_s int type_id; }; +typedef struct mysql_database_s mysql_database_t; +struct mysql_database_s +{ + char *instance; + char *host; + char *user; + char *passwd; + char *database; + int port; + _Bool replace; + c_avl_tree_t *host_tree, *plugin_tree, *type_tree, *dataset_tree; + MYSQL *conn; + MYSQL_BIND data_bind[8], notif_bind[8]; + MYSQL_STMT *data_stmt, *notif_stmt; + pthread_mutex_t mutexdb; + pthread_mutex_t mutexhost_tree, mutexplugin_tree; + pthread_mutex_t mutextype_tree, mutexdataset_tree; + char data_query[1024]; +}; + +static int write_mysql_write (const data_set_t * ds, const value_list_t * vl, + user_data_t * ud); + +static int write_mysql_init (user_data_t * ud); +static int notify_write_mysql (const notification_t * n, user_data_t * ud); +/* static const char *config_keys[] = { "Host", "User", @@ -63,25 +89,121 @@ static char *passwd = ""; static char *database = "collectd"; static int port = 0; static _Bool replace = 1; +*/ #define HOST_ITEM 0 #define PLUGIN_ITEM 1 #define TYPE_ITEM 2 -static MYSQL *conn; -static MYSQL_BIND data_bind[8], notif_bind[8]; -static MYSQL_STMT *data_stmt, *notif_stmt; - -static pthread_mutex_t mutexdb; -static pthread_mutex_t mutexhost_tree, mutexplugin_tree, mutextype_tree, - mutexdataset_tree; - -static char data_query[1024]; static const char *notif_query = "INSERT INTO notification (date,host_id,plugin_id," "plugin_instance,type_id,type_instance,severity,message) VALUES " "(?,?,?,?,?,?,?,?)"; +static int +write_mysql_config_database (oconfig_item_t *ci) +{ + mysql_database_t *db; + int status = 0; + int i; + + if ((ci->values_num != 1) + || (ci->values[0].type != OCONFIG_TYPE_STRING)) + { + WARNING ("write_mysql plugin: The `Database' block " + "needs exactly one string argument."); + return (-1); + } + + db = (mysql_database_t *) malloc (sizeof (*db)); + if (db == NULL) + { + ERROR ("mysql plugin: malloc failed."); + return (-1); + } + memset (db, '\0', sizeof (*db)); + db->host = NULL; + db->user = NULL; + db->passwd = NULL; + db->database = NULL; + db->port = NULL; + db->replace = NULL; + + status = cf_util_get_string (ci, &db->instance); + if (status != 0) + { + sfree (db); + return (status); + } + assert (db->instance != NULL); + for (i = 0; i < ci->children_num; i++) + { + oconfig_item_t *child = ci->children + i; + if (strcasecmp ("Host", child->key) == 0) + status = cf_util_get_string (child, &db->host); + else if (strcasecmp ("User", child->key) == 0) + status = cf_util_get_string (child, &db->user); + else if (strcasecmp ("Passwd", child->key) == 0) + status = cf_util_get_string (child, &db->passwd); + else if (strcasecmp ("Port", child->key) == 0) + { + status = cf_util_get_port_number (child); + if (status > 0) + { + db->port = status; + status = 0; + } + } + else if (strcasecmp ("Database", child->key) == 0) + status = cf_util_get_string (child, &db->database); + else if (strcasecmp ("Replace", child->key) == 0) + status = cf_util_get_boolean (child, &db->replace); + else + { + WARNING ("write_mysql plugin: Option `%s' not allowed here.", child->key); + status = -1; + } + + if (status != 0) + break; + } + + if (status == 0) + { + user_data_t ud; + char cb_name[DATA_MAX_NAME_LEN]; + DEBUG ("write_mysql plugin: Registering new write callback: %s", + db->instance); + ssnprintf (cb_name, sizeof (cb_name), "write_mysql/%s", db->instance); + memset (&ud, 0, sizeof (ud)); + ud.data = (void *) db; + //ud.free_func = write_mysql_database_free; + write_mysql_init (&ud); + plugin_register_write(cb_name, write_mysql_write, &ud); + plugin_register_notification (cb_name, notify_write_mysql, &ud); + } +} + +static int +write_mysql_config (oconfig_item_t *ci) +{ + int i; + if (ci == NULL) + return (EINVAL); + for (i = 0; i < ci->children_num; i++) + { + oconfig_item_t *child = ci->children + i; + + if (strcasecmp ("Database", child->key) == 0) { + write_mysql_config_database (child); + } else + WARNING ("mysql plugin: Option \"%s\" not allowed here.", + child->key); + } + return (0); +} + +/* static int write_mysql_config (const char *key, const char *value) { @@ -110,12 +232,20 @@ write_mysql_config (const char *key, const char *value) replace = IS_TRUE (value); } } +*/ static int -write_mysql_init (void) +write_mysql_init (user_data_t * ud) { + mysql_database_t *db; my_bool my_true = 1; - conn = mysql_init (NULL); + if ((ud == NULL) || (ud->data == NULL)) + { + ERROR ("write_mysql plugin: get_item_id: Invalid user data."); + return (-1); + } + db = (mysql_database_t *) ud->data; + db->conn = mysql_init (NULL); if (!mysql_thread_safe ()) { ERROR ("write_mysql plugin: mysqlclient Thread Safe OFF"); @@ -125,32 +255,32 @@ write_mysql_init (void) { DEBUG ("write_mysql plugin: mysqlclient Thread Safe ON"); } - if (mysql_real_connect (conn, host, user, passwd, database, port, NULL, 0) + if (mysql_real_connect (db->conn, db->host, db->user, db->passwd, db->database, db->port, NULL, 0) == NULL) { ERROR ("write_mysql plugin: Failed to connect to database %s " - " at server %s with user %s : %s", database, host, user, - mysql_error (conn)); + " at server %s with user %s : %s", db->database, db->host, db->user, + mysql_error (db->conn)); } char tmpquery[1024] = "%s INTO data " - "(timestamp,host_id,plugin_id,plugin_instance,type_id,type_instance,dataset_id,value)" - "VALUES (?,?,?,?,?,?,?,?)"; - ssnprintf (data_query, sizeof (tmpquery), tmpquery, replace ? "REPLACE" : "INSERT"); - mysql_options (conn, MYSQL_OPT_RECONNECT, &my_true); - data_stmt = mysql_stmt_init (conn); - notif_stmt = mysql_stmt_init (conn); - mysql_stmt_prepare (data_stmt, data_query, strlen (data_query)); - mysql_stmt_prepare (notif_stmt, notif_query, strlen (notif_query)); - host_tree = c_avl_create ((void *) strcmp); - plugin_tree = c_avl_create ((void *) strcmp); - type_tree = c_avl_create ((void *) strcmp); - dataset_tree = c_avl_create ((void *) strcmp); + "(date,host_id,plugin_id,plugin_instance,type_id,type_instance,dataset_id,value)" + " VALUES (?,?,?,?,?,?,?,?)"; + ssnprintf (db->data_query, sizeof (tmpquery), tmpquery, db->replace ? "REPLACE" : "INSERT"); + mysql_options (db->conn, MYSQL_OPT_RECONNECT, &my_true); + db->data_stmt = mysql_stmt_init (db->conn); + db->notif_stmt = mysql_stmt_init (db->conn); + mysql_stmt_prepare (db->data_stmt, db->data_query, strlen (db->data_query)); + mysql_stmt_prepare (db->notif_stmt, notif_query, strlen (notif_query)); + db->host_tree = c_avl_create ((void *) strcmp); + db->plugin_tree = c_avl_create ((void *) strcmp); + db->type_tree = c_avl_create ((void *) strcmp); + db->dataset_tree = c_avl_create ((void *) strcmp); return (0); } static int -add_item_id (const char *name, const int item) +add_item_id (const char *name, const int item, user_data_t * ud) { int *id = malloc (sizeof (int)); char query[1024]; @@ -158,6 +288,13 @@ add_item_id (const char *name, const int item) c_avl_tree_t *tree; MYSQL_BIND param_bind[1], result_bind[1]; MYSQL_STMT *stmt; + mysql_database_t *db; + if ((ud == NULL) || (ud->data == NULL)) + { + ERROR ("write_mysql plugin: get_item_id: Invalid user data."); + return (-1); + } + db = (mysql_database_t *) ud->data; ssnprintf (query, sizeof (query), "SELECT id FROM %s WHERE name = ?", item == HOST_ITEM ? "host" : item == PLUGIN_ITEM ? "plugin" : "type"); @@ -169,22 +306,22 @@ add_item_id (const char *name, const int item) param_bind[0].buffer_length = strlen (name); result_bind[0].buffer_type = MYSQL_TYPE_LONG; result_bind[0].buffer = (void *) id; - pthread_mutex_lock (&mutexdb); - if (mysql_ping (conn) != 0) + pthread_mutex_lock (&db->mutexdb); + if (mysql_ping (db->conn) != 0) { ERROR ("write_mysql plugin: add_item_id - Failed to re-connect to database : %s", - mysql_error (conn)); - pthread_mutex_unlock (&mutexdb); + mysql_error (db->conn)); + pthread_mutex_unlock (&db->mutexdb); return -1; } - stmt = mysql_stmt_init (conn); + stmt = mysql_stmt_init (db->conn); if (mysql_stmt_prepare (stmt, query, strlen (query)) != 0) { ERROR ("write_mysql plugin: add_item_id - Failed to prepare statement : %s / %s", mysql_stmt_error (stmt), query); - pthread_mutex_unlock (&mutexdb); + pthread_mutex_unlock (&db->mutexdb); return -1; } if (mysql_stmt_bind_param (stmt, param_bind) != 0) @@ -192,7 +329,7 @@ add_item_id (const char *name, const int item) ERROR ("write_mysql plugin: add_item_id - Failed to bind param to statement : %s / %s", mysql_stmt_error (stmt), query); - pthread_mutex_unlock (&mutexdb); + pthread_mutex_unlock (&db->mutexdb); return -1; } if (mysql_stmt_bind_result (stmt, result_bind) != 0) @@ -200,7 +337,7 @@ add_item_id (const char *name, const int item) ERROR ("write_mysql plugin: add_item_id - Failed to bind result to statement : %s / %s", mysql_stmt_error (stmt), query); - pthread_mutex_unlock (&mutexdb); + pthread_mutex_unlock (&db->mutexdb); return -1; } if (mysql_stmt_execute (stmt) != 0) @@ -208,7 +345,7 @@ add_item_id (const char *name, const int item) ERROR ("write_mysql plugin: add_item_id - Failed to execute re-prepared statement : %s / %s", mysql_stmt_error (stmt), query); - pthread_mutex_unlock (&mutexdb); + pthread_mutex_unlock (&db->mutexdb); return -1; } if (mysql_stmt_store_result (stmt) != 0) @@ -216,14 +353,14 @@ add_item_id (const char *name, const int item) ERROR ("write_mysql plugin: add_item_id - Failed to store result : %s / %s", mysql_stmt_error (stmt), query); - pthread_mutex_unlock (&mutexdb); + pthread_mutex_unlock (&db->mutexdb); return -1; } if (mysql_stmt_fetch (stmt) == 0) { mysql_stmt_free_result (stmt); mysql_stmt_close (stmt); - pthread_mutex_unlock (&mutexdb); + pthread_mutex_unlock (&db->mutexdb); DEBUG ("get %s_id from DB : %d (%s)", item == HOST_ITEM ? "host" : item == PLUGIN_ITEM ? "plugin" : "type", *id, name); @@ -232,7 +369,7 @@ add_item_id (const char *name, const int item) { mysql_stmt_free_result (stmt); mysql_stmt_close (stmt); - stmt = mysql_stmt_init (conn); + stmt = mysql_stmt_init (db->conn); ssnprintf (query, sizeof (query), "INSERT INTO %s (name) VALUES (?)", item == HOST_ITEM ? "host" : item == PLUGIN_ITEM ? "plugin" : "type"); @@ -241,7 +378,7 @@ add_item_id (const char *name, const int item) ERROR ("write_mysql plugin: add_item_id - Failed to prepare statement : %s / %s", mysql_stmt_error (stmt), query); - pthread_mutex_unlock (&mutexdb); + pthread_mutex_unlock (&db->mutexdb); return -1; } if (mysql_stmt_bind_param (stmt, param_bind) != 0) @@ -249,7 +386,7 @@ add_item_id (const char *name, const int item) ERROR ("write_mysql plugin: add_item_id - Failed to bind param to statement : %s / %s", mysql_stmt_error (stmt), query); - pthread_mutex_unlock (&mutexdb); + pthread_mutex_unlock (&db->mutexdb); return -1; } if (mysql_stmt_execute (stmt) != 0) @@ -257,12 +394,12 @@ add_item_id (const char *name, const int item) ERROR ("write_mysql plugin: add_item_id - Failed to execute re-prepared statement : %s / %s", mysql_stmt_error (stmt), query); - pthread_mutex_unlock (&mutexdb); + pthread_mutex_unlock (&db->mutexdb); return -1; } *id = mysql_stmt_insert_id (stmt); mysql_stmt_close (stmt); - pthread_mutex_unlock (&mutexdb); + pthread_mutex_unlock (&db->mutexdb); DEBUG ("insert %s_id in DB : %d (%s)", item == HOST_ITEM ? "host" : item == PLUGIN_ITEM ? "plugin" : "type", *id, name); @@ -270,16 +407,16 @@ add_item_id (const char *name, const int item) switch (item) { case HOST_ITEM: - mutex = &mutexhost_tree; - tree = host_tree; + mutex = &db->mutexhost_tree; + tree = db->host_tree; break; case PLUGIN_ITEM: - mutex = &mutexplugin_tree; - tree = plugin_tree; + mutex = &db->mutexplugin_tree; + tree = db->plugin_tree; break; case TYPE_ITEM: - mutex = &mutextype_tree; - tree = type_tree; + mutex = &db->mutextype_tree; + tree = db->type_tree; break; } pthread_mutex_lock (mutex); @@ -289,12 +426,19 @@ add_item_id (const char *name, const int item) } static int -add_dataset_id (data_source_t * ds, int type_id) +add_dataset_id (data_source_t * ds, int type_id, user_data_t * ud) { int *id = malloc (sizeof (int)); char tree_key[DATA_MAX_NAME_LEN * 2]; dataset_t *newdataset; char *type; + mysql_database_t *db; + if ((ud == NULL) || (ud->data == NULL)) + { + ERROR ("write_mysql plugin: get_item_id: Invalid user data."); + return (-1); + } + db = (mysql_database_t *) ud->data; switch (ds->type) { case DS_TYPE_COUNTER: @@ -335,22 +479,22 @@ add_dataset_id (data_source_t * ds, int type_id) param_bind2[4].buffer = (void *) &ds->max; result_bind[0].buffer_type = MYSQL_TYPE_LONG; result_bind[0].buffer = (void *) id; - pthread_mutex_lock (&mutexdb); - if (mysql_ping (conn) != 0) + pthread_mutex_lock (&db->mutexdb); + if (mysql_ping (db->conn) != 0) { ERROR ("write_mysql plugin: add_dataset_id - Failed to re-connect to database : %s", - mysql_error (conn)); - pthread_mutex_unlock (&mutexdb); + mysql_error (db->conn)); + pthread_mutex_unlock (&db->mutexdb); return -1; } - stmt = mysql_stmt_init (conn); + stmt = mysql_stmt_init (db->conn); if (mysql_stmt_prepare (stmt, query, strlen (query)) != 0) { ERROR ("write_mysql plugin: add_dataset_id - Failed to prepare statement : %s / %s", mysql_stmt_error (stmt), query); - pthread_mutex_unlock (&mutexdb); + pthread_mutex_unlock (&db->mutexdb); return -1; } if (mysql_stmt_bind_param (stmt, param_bind) != 0) @@ -358,7 +502,7 @@ add_dataset_id (data_source_t * ds, int type_id) ERROR ("write_mysql plugin: add_dataset_id - Failed to bind param to statement : %s / %s", mysql_stmt_error (stmt), query); - pthread_mutex_unlock (&mutexdb); + pthread_mutex_unlock (&db->mutexdb); return -1; } if (mysql_stmt_bind_result (stmt, result_bind) != 0) @@ -366,7 +510,7 @@ add_dataset_id (data_source_t * ds, int type_id) ERROR ("write_mysql plugin: add_dataset_id - Failed to bind result to statement : %s / %s", mysql_stmt_error (stmt), query); - pthread_mutex_unlock (&mutexdb); + pthread_mutex_unlock (&db->mutexdb); return -1; } if (mysql_stmt_execute (stmt) != 0) @@ -374,7 +518,7 @@ add_dataset_id (data_source_t * ds, int type_id) ERROR ("write_mysql plugin: add_dataset_id - Failed to execute re-prepared statement : %s / %s", mysql_stmt_error (stmt), query); - pthread_mutex_unlock (&mutexdb); + pthread_mutex_unlock (&db->mutexdb); return -1; } if (mysql_stmt_store_result (stmt) != 0) @@ -382,21 +526,21 @@ add_dataset_id (data_source_t * ds, int type_id) ERROR ("write_mysql plugin: add_dataset_id - Failed to store result : %s / %s", mysql_stmt_error (stmt), query); - pthread_mutex_unlock (&mutexdb); + pthread_mutex_unlock (&db->mutexdb); return -1; } if (mysql_stmt_fetch (stmt) == 0) { mysql_stmt_free_result (stmt); mysql_stmt_close (stmt); - pthread_mutex_unlock (&mutexdb); + pthread_mutex_unlock (&db->mutexdb); DEBUG ("get dataset_id from DB : %d (%s) (%d)", *id, ds->name, type_id); } else { mysql_stmt_free_result (stmt); mysql_stmt_close (stmt); - stmt = mysql_stmt_init (conn); + stmt = mysql_stmt_init (db->conn); char *queryins = "INSERT INTO dataset (name,type_id,type,min,max) VALUES (?,?,?,?,?)"; if (mysql_stmt_prepare (stmt, queryins, strlen (queryins)) != 0) @@ -404,7 +548,7 @@ add_dataset_id (data_source_t * ds, int type_id) ERROR ("write_mysql plugin: add_dataset_id - Failed to prepare statement : %s / %s", mysql_stmt_error (stmt), queryins); - pthread_mutex_unlock (&mutexdb); + pthread_mutex_unlock (&db->mutexdb); return -1; } if (mysql_stmt_bind_param (stmt, param_bind2) != 0) @@ -412,7 +556,7 @@ add_dataset_id (data_source_t * ds, int type_id) ERROR ("write_mysql plugin: add_dataset_id - Failed to bind param to statement : %s / %s", mysql_stmt_error (stmt), queryins); - pthread_mutex_unlock (&mutexdb); + pthread_mutex_unlock (&db->mutexdb); return -1; } if (mysql_stmt_execute (stmt) != 0) @@ -420,48 +564,58 @@ add_dataset_id (data_source_t * ds, int type_id) ERROR ("write_mysql plugin: add_dataset_id - Failed to execute re-prepared statement : %s / %s", mysql_stmt_error (stmt), queryins); - pthread_mutex_unlock (&mutexdb); + pthread_mutex_unlock (&db->mutexdb); return -1; } *id = mysql_stmt_insert_id (stmt); mysql_stmt_close (stmt); - pthread_mutex_unlock (&mutexdb); + pthread_mutex_unlock (&db->mutexdb); DEBUG ("insert dataset_id in DB : %d (%s) (%d)", *id, ds->name, type_id); } - pthread_mutex_unlock (&mutexdb); + pthread_mutex_unlock (&db->mutexdb); ssnprintf (tree_key, sizeof (tree_key), "%s_%d", ds->name, type_id); newdataset = malloc (sizeof (dataset_t)); sstrncpy (newdataset->name, ds->name, sizeof (newdataset->name)); newdataset->id = *id; newdataset->type_id = type_id; - pthread_mutex_lock (&mutexdataset_tree); - c_avl_insert (dataset_tree, strdup (tree_key), newdataset); - pthread_mutex_unlock (&mutexdataset_tree); + pthread_mutex_lock (&db->mutexdataset_tree); + c_avl_insert (db->dataset_tree, strdup (tree_key), newdataset); + pthread_mutex_unlock (&db->mutexdataset_tree); sfree (id); return newdataset->id; } static int -get_item_id (const char *name, const int item) +get_item_id (const char *name, const int item, user_data_t * ud) { int *id; pthread_mutex_t *mutex; c_avl_tree_t *tree; + mysql_database_t *db; + + if ((ud == NULL) || (ud->data == NULL)) + { + ERROR ("write_mysql plugin: get_item_id: Invalid user data."); + return (-1); + } + + db = (mysql_database_t *) ud->data; + switch (item) { case HOST_ITEM: - mutex = &mutexhost_tree; - tree = host_tree; + mutex = &db->mutexhost_tree; + tree = db->host_tree; break; case PLUGIN_ITEM: - mutex = &mutexplugin_tree; - tree = plugin_tree; + mutex = &db->mutexplugin_tree; + tree = db->plugin_tree; break; case TYPE_ITEM: - mutex = &mutextype_tree; - tree = type_tree; + mutex = &db->mutextype_tree; + tree = db->type_tree; break; } if (strlen (name) == 0) @@ -483,27 +637,34 @@ get_item_id (const char *name, const int item) DEBUG ("get_item_id : insert %s_id for %s into cache", item == HOST_ITEM ? "host" : item == PLUGIN_ITEM ? "plugin" : "type", name); - return add_item_id (name, item); + return add_item_id (name, item, ud); } } static int -get_dataset_id (data_source_t * ds, int type_id) +get_dataset_id (data_source_t * ds, int type_id, user_data_t * ud) { char tree_key[DATA_MAX_NAME_LEN * 2]; dataset_t *newdataset; + mysql_database_t *db; + if ((ud == NULL) || (ud->data == NULL)) + { + ERROR ("write_mysql plugin: get_item_id: Invalid user data."); + return (-1); + } + db = (mysql_database_t *) ud->data; ssnprintf (tree_key, sizeof (tree_key), "%s_%d", ds->name, type_id); - pthread_mutex_lock (&mutexdataset_tree); - if (c_avl_get (dataset_tree, tree_key, (void *) &newdataset) == 0) + pthread_mutex_lock (&db->mutexdataset_tree); + if (c_avl_get (db->dataset_tree, tree_key, (void *) &newdataset) == 0) { - pthread_mutex_unlock (&mutexdataset_tree); + pthread_mutex_unlock (&db->mutexdataset_tree); DEBUG ("dataset_id from cache : %d | %s", newdataset->id, tree_key); return newdataset->id; } else { - pthread_mutex_unlock (&mutexdataset_tree); - return add_dataset_id (ds, type_id); + pthread_mutex_unlock (&db->mutexdataset_tree); + return add_dataset_id (ds, type_id, ud); } } @@ -535,15 +696,24 @@ static int wm_cdtime_t_to_mysql_time (cdtime_t in, MYSQL_TIME *out) /* {{{ */ static int write_mysql_write (const data_set_t * ds, const value_list_t * vl, - user_data_t __attribute__ ((unused)) * user_data) + user_data_t * ud) { int i; int host_id, plugin_id, type_id, aa; gauge_t *rates = NULL; - host_id = get_item_id ((char *) vl->host, HOST_ITEM); - plugin_id = get_item_id ((char *) vl->plugin, PLUGIN_ITEM); - type_id = get_item_id ((char *) vl->type, TYPE_ITEM); + host_id = get_item_id ((char *) vl->host, HOST_ITEM, ud); + plugin_id = get_item_id ((char *) vl->plugin, PLUGIN_ITEM, ud); + type_id = get_item_id ((char *) vl->type, TYPE_ITEM, ud); + + mysql_database_t *db; + if ((ud == NULL) || (ud->data == NULL)) + { + ERROR ("write_mysql plugin: get_item_id: Invalid user data."); + return (-1); + } + db = (mysql_database_t *) ud->data; + if (host_id == -1 || plugin_id == -1 || type_id == -1) { return -1; @@ -553,7 +723,7 @@ write_mysql_write (const data_set_t * ds, const value_list_t * vl, { int len; data_source_t *dsrc = ds->ds + i; - int dataset_id = get_dataset_id (dsrc, type_id); + int dataset_id = get_dataset_id (dsrc, type_id, ud); MYSQL_BIND binding[8]; MYSQL_TIME mysql_date; @@ -600,129 +770,136 @@ write_mysql_write (const data_set_t * ds, const value_list_t * vl, binding[7].buffer = (void *) &(rates[i]); } - pthread_mutex_lock (&mutexdb); + pthread_mutex_lock (&db->mutexdb); - if (mysql_ping (conn) != 0) + if (mysql_ping (db->conn) != 0) { ERROR ("write_mysql plugin: write_mysql_write - Failed to re-connect to database : %s", - mysql_error (conn)); - pthread_mutex_unlock (&mutexdb); + mysql_error (db->conn)); + pthread_mutex_unlock (&db->mutexdb); sfree (rates); return -1; } - if (mysql_stmt_bind_param (data_stmt, binding) != 0) + if (mysql_stmt_bind_param (db->data_stmt, binding) != 0) { ERROR - ("write_mysql plugin: Failed to bind param to statement : %s / %s", - mysql_stmt_error (data_stmt), data_query); - pthread_mutex_unlock (&mutexdb); + ("write_mysql plugin: write_mysql_write - Failed to bind param to statement : %s / %s", + mysql_stmt_error (db->data_stmt), db->data_query); + pthread_mutex_unlock (&db->mutexdb); sfree (rates); return -1; } - if (mysql_stmt_execute (data_stmt) != 0) + if (mysql_stmt_execute (db->data_stmt) != 0) { // Try to re-prepare statement - data_stmt = mysql_stmt_init (conn); - mysql_stmt_prepare (data_stmt, data_query, strlen (data_query)); - mysql_stmt_bind_param (data_stmt, binding); - if (mysql_stmt_execute (data_stmt) != 0) + db->data_stmt = mysql_stmt_init (db->conn); + mysql_stmt_prepare (db->data_stmt, db->data_query, strlen (db->data_query)); + mysql_stmt_bind_param (db->data_stmt, binding); + if (mysql_stmt_execute (db->data_stmt) != 0) { ERROR ("write_mysql plugin: Failed to execute re-prepared statement : %s / %s", - mysql_stmt_error (data_stmt), data_query); - pthread_mutex_unlock (&mutexdb); + mysql_stmt_error (db->data_stmt), db->data_query); + pthread_mutex_unlock (&db->mutexdb); sfree (rates); return -1; } } sfree (rates); - pthread_mutex_unlock (&mutexdb); + pthread_mutex_unlock (&db->mutexdb); } return (0); } static int -notify_write_mysql (const notification_t * n, - user_data_t __attribute__ ((unused)) * user_data) +notify_write_mysql (const notification_t * n, user_data_t * ud) { int host_id, plugin_id, type_id, len; char severity[32]; MYSQL_TIME mysql_date; - host_id = get_item_id (n->host, HOST_ITEM); - plugin_id = get_item_id (n->plugin, PLUGIN_ITEM); - type_id = get_item_id (n->type, TYPE_ITEM); + mysql_database_t *db; + if ((ud == NULL) || (ud->data == NULL)) + { + ERROR ("write_mysql plugin: get_item_id: Invalid user data."); + return (-1); + } + db = (mysql_database_t *) ud->data; + + host_id = get_item_id (n->host, HOST_ITEM, ud); + plugin_id = get_item_id (n->plugin, PLUGIN_ITEM, ud); + type_id = get_item_id (n->type, TYPE_ITEM, ud); wm_cdtime_t_to_mysql_time (n->time, &mysql_date); - memset (notif_bind, 0, sizeof (notif_bind)); - - pthread_mutex_lock (&mutexdb); - - notif_bind[0].buffer_type = MYSQL_TYPE_DATETIME; - notif_bind[0].buffer = (char *) &mysql_date; - notif_bind[0].is_null = 0; - notif_bind[0].length = 0; - notif_bind[1].buffer_type = MYSQL_TYPE_LONG; - notif_bind[1].buffer = (void *) &host_id; - notif_bind[2].buffer_type = MYSQL_TYPE_LONG; - notif_bind[2].buffer = (void *) &plugin_id; - notif_bind[3].buffer_type = MYSQL_TYPE_STRING; - notif_bind[3].buffer = (void *) n->plugin_instance; - notif_bind[3].buffer_length = strlen (n->plugin_instance); - notif_bind[4].buffer_type = MYSQL_TYPE_LONG; - notif_bind[4].buffer = (void *) &type_id; - notif_bind[5].buffer_type = MYSQL_TYPE_STRING; - notif_bind[5].buffer = (void *) n->type_instance; - notif_bind[5].buffer_length = strlen (n->type_instance); - notif_bind[6].buffer_type = MYSQL_TYPE_STRING; + memset (db->notif_bind, 0, sizeof (db->notif_bind)); + + pthread_mutex_lock (&db->mutexdb); + + db->notif_bind[0].buffer_type = MYSQL_TYPE_DATETIME; + db->notif_bind[0].buffer = (char *) &mysql_date; + db->notif_bind[0].is_null = 0; + db->notif_bind[0].length = 0; + db->notif_bind[1].buffer_type = MYSQL_TYPE_LONG; + db->notif_bind[1].buffer = (void *) &host_id; + db->notif_bind[2].buffer_type = MYSQL_TYPE_LONG; + db->notif_bind[2].buffer = (void *) &plugin_id; + db->notif_bind[3].buffer_type = MYSQL_TYPE_STRING; + db->notif_bind[3].buffer = (void *) n->plugin_instance; + db->notif_bind[3].buffer_length = strlen (n->plugin_instance); + db->notif_bind[4].buffer_type = MYSQL_TYPE_LONG; + db->notif_bind[4].buffer = (void *) &type_id; + db->notif_bind[5].buffer_type = MYSQL_TYPE_STRING; + db->notif_bind[5].buffer = (void *) n->type_instance; + db->notif_bind[5].buffer_length = strlen (n->type_instance); + db->notif_bind[6].buffer_type = MYSQL_TYPE_STRING; ssnprintf (severity, sizeof (severity), "%s", (n->severity == NOTIF_FAILURE) ? "FAILURE" : ((n->severity == NOTIF_WARNING) ? "WARNING" : ((n->severity == NOTIF_OKAY) ? "OKAY" : "UNKNOWN"))); - notif_bind[6].buffer = (void *) severity; - notif_bind[6].buffer_length = strlen (severity); - notif_bind[7].buffer_type = MYSQL_TYPE_VAR_STRING; - notif_bind[7].buffer = (void *) n->message; - notif_bind[7].buffer_length = strlen (n->message); - if (mysql_ping (conn) != 0) + db->notif_bind[6].buffer = (void *) severity; + db->notif_bind[6].buffer_length = strlen (severity); + db->notif_bind[7].buffer_type = MYSQL_TYPE_VAR_STRING; + db->notif_bind[7].buffer = (void *) n->message; + db->notif_bind[7].buffer_length = strlen (n->message); + if (mysql_ping (db->conn) != 0) { ERROR ("write_mysql plugin: write_mysql_write - Failed to re-connect to database : %s", - mysql_error (conn)); - pthread_mutex_unlock (&mutexdb); + mysql_error (db->conn)); + pthread_mutex_unlock (&db->mutexdb); return -1; } - if (mysql_stmt_bind_param (notif_stmt, notif_bind) != 0) + if (mysql_stmt_bind_param (db->notif_stmt, db->notif_bind) != 0) { ERROR - ("write_mysql plugin: Failed to bind param to statement : %s / %s", - mysql_stmt_error (notif_stmt), notif_query); - pthread_mutex_unlock (&mutexdb); + ("write_mysql plugin: notify_write_mysql - Failed to bind param to statement : %s / %s", + mysql_stmt_error (db->notif_stmt), notif_query); + pthread_mutex_unlock (&db->mutexdb); return -1; } - if (mysql_stmt_execute (notif_stmt) != 0) + if (mysql_stmt_execute (db->notif_stmt) != 0) { // Try to re-prepare statement - notif_stmt = mysql_stmt_init (conn); - mysql_stmt_prepare (notif_stmt, notif_query, strlen (notif_query)); - mysql_stmt_bind_param (notif_stmt, notif_bind); - if (mysql_stmt_execute (notif_stmt) != 0) + db->notif_stmt = mysql_stmt_init (db->conn); + mysql_stmt_prepare (db->notif_stmt, notif_query, strlen (notif_query)); + mysql_stmt_bind_param (db->notif_stmt, db->notif_bind); + if (mysql_stmt_execute (db->notif_stmt) != 0) { ERROR ("write_mysql plugin: Failed to execute re-prepared statement : %s / %s", - mysql_stmt_error (notif_stmt), notif_query); - pthread_mutex_unlock (&mutexdb); + mysql_stmt_error (db->notif_stmt), notif_query); + pthread_mutex_unlock (&db->mutexdb); return -1; } } - pthread_mutex_unlock (&mutexdb); + pthread_mutex_unlock (&db->mutexdb); return 0; } - +/* static void free_tree (c_avl_tree_t * tree) { @@ -754,16 +931,17 @@ write_mysql_shutdown (void) mysql_close (conn); return 0; } - +*/ void module_register (void) { - plugin_register_init ("write_mysql", write_mysql_init); - plugin_register_config ("write_mysql", write_mysql_config, - config_keys, config_keys_num); - plugin_register_write ("write_mysql", write_mysql_write, /* user_data = */ - NULL); - plugin_register_shutdown ("write_mysql", write_mysql_shutdown); - plugin_register_notification ("write_mysql", notify_write_mysql, - /* user_data = */ NULL); + plugin_register_complex_config ("write_mysql", write_mysql_config); +// plugin_register_init ("write_mysql", write_mysql_init); +// plugin_register_config ("write_mysql", write_mysql_config, +// config_keys, config_keys_num); +// plugin_register_write ("write_mysql", write_mysql_write, /* user_data = */ +// NULL); +// plugin_register_shutdown ("write_mysql", write_mysql_shutdown); +// plugin_register_notification ("write_mysql", notify_write_mysql, +// /* user_data = */ NULL); }