Merge pull request #3339 from jkohen/patch-1
[collectd.git] / src / mysql.c
1 /**
2  * collectd - src/mysql.c
3  * Copyright (C) 2006-2010  Florian octo Forster
4  * Copyright (C) 2008       Mirko Buffoni
5  * Copyright (C) 2009       Doug MacEachern
6  * Copyright (C) 2009       Sebastian tokkee Harl
7  * Copyright (C) 2009       Rodolphe QuiĆ©deville
8  *
9  * This program is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU General Public License as published by the
11  * Free Software Foundation; only version 2 of the License is applicable.
12  *
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with this program; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
21  *
22  * Authors:
23  *   Florian octo Forster <octo at collectd.org>
24  *   Mirko Buffoni <briareos at eswat.org>
25  *   Doug MacEachern <dougm at hyperic.com>
26  *   Sebastian tokkee Harl <sh at tokkee.org>
27  *   Rodolphe QuiĆ©deville <rquiedeville at bearstech.com>
28  **/
29
30 #include "collectd.h"
31 #include "common.h"
32 #include "plugin.h"
33 #include "configfile.h"
34
35 #ifdef HAVE_MYSQL_H
36 #include <mysql.h>
37 #elif defined(HAVE_MYSQL_MYSQL_H)
38 #include <mysql/mysql.h>
39 #endif
40
41 struct mysql_database_s /* {{{ */
42 {
43         char *instance;
44         char *alias;
45         char *host;
46         char *user;
47         char *pass;
48         char *database;
49         char *socket;
50         int   port;
51         int   timeout;
52
53         _Bool master_stats;
54         _Bool slave_stats;
55         _Bool innodb_stats;
56
57         _Bool slave_notif;
58         _Bool slave_io_running;
59         _Bool slave_sql_running;
60
61         MYSQL *con;
62         _Bool  is_connected;
63 };
64 typedef struct mysql_database_s mysql_database_t; /* }}} */
65
66 static int mysql_read (user_data_t *ud);
67
68 void mysql_read_default_options(struct st_mysql_options *options,
69                 const char *filename,const char *group);
70
71 static void mysql_database_free (void *arg) /* {{{ */
72 {
73         mysql_database_t *db;
74
75         DEBUG ("mysql plugin: mysql_database_free (arg = %p);", arg);
76
77         db = (mysql_database_t *) arg;
78
79         if (db == NULL)
80                 return;
81
82         if (db->con != NULL)
83                 mysql_close (db->con);
84
85         sfree (db->alias);
86         sfree (db->host);
87         sfree (db->user);
88         sfree (db->pass);
89         sfree (db->socket);
90         sfree (db->instance);
91         sfree (db->database);
92         sfree (db);
93 } /* }}} void mysql_database_free */
94
95 /* Configuration handling functions {{{
96  *
97  * <Plugin mysql>
98  *   <Database "plugin_instance1">
99  *     Host "localhost"
100  *     Port 22000
101  *     ...
102  *   </Database>
103  * </Plugin>
104  */
105 static int mysql_config_database (oconfig_item_t *ci) /* {{{ */
106 {
107         mysql_database_t *db;
108         int status = 0;
109         int i;
110
111         if ((ci->values_num != 1)
112             || (ci->values[0].type != OCONFIG_TYPE_STRING))
113         {
114                 WARNING ("mysql plugin: The `Database' block "
115                          "needs exactly one string argument.");
116                 return (-1);
117         }
118
119         db = (mysql_database_t *) malloc (sizeof (*db));
120         if (db == NULL)
121         {
122                 ERROR ("mysql plugin: malloc failed.");
123                 return (-1);
124         }
125         memset (db, 0, sizeof (*db));
126
127         /* initialize all the pointers */
128         db->alias    = NULL;
129         db->host     = NULL;
130         db->user     = NULL;
131         db->pass     = NULL;
132         db->database = NULL;
133         db->socket   = NULL;
134         db->con      = NULL;
135         db->timeout  = 0;
136
137         /* trigger a notification, if it's not running */
138         db->slave_io_running  = 1;
139         db->slave_sql_running = 1;
140
141         status = cf_util_get_string (ci, &db->instance);
142         if (status != 0)
143         {
144                 sfree (db);
145                 return (status);
146         }
147         assert (db->instance != NULL);
148
149         /* Fill the `mysql_database_t' structure.. */
150         for (i = 0; i < ci->children_num; i++)
151         {
152                 oconfig_item_t *child = ci->children + i;
153
154                 if (strcasecmp ("Alias", child->key) == 0)
155                         status = cf_util_get_string (child, &db->alias);
156                 else if (strcasecmp ("Host", child->key) == 0)
157                         status = cf_util_get_string (child, &db->host);
158                 else if (strcasecmp ("User", child->key) == 0)
159                         status = cf_util_get_string (child, &db->user);
160                 else if (strcasecmp ("Password", child->key) == 0)
161                         status = cf_util_get_string (child, &db->pass);
162                 else if (strcasecmp ("Port", child->key) == 0)
163                 {
164                         status = cf_util_get_port_number (child);
165                         if (status > 0)
166                         {
167                                 db->port = status;
168                                 status = 0;
169                         }
170                 }
171                 else if (strcasecmp ("Socket", child->key) == 0)
172                         status = cf_util_get_string (child, &db->socket);
173                 else if (strcasecmp ("Database", child->key) == 0)
174                         status = cf_util_get_string (child, &db->database);
175                 else if (strcasecmp ("ConnectTimeout", child->key) == 0)
176                         status = cf_util_get_int (child, &db->timeout);
177                 else if (strcasecmp ("MasterStats", child->key) == 0)
178                         status = cf_util_get_boolean (child, &db->master_stats);
179                 else if (strcasecmp ("SlaveStats", child->key) == 0)
180                         status = cf_util_get_boolean (child, &db->slave_stats);
181                 else if (strcasecmp ("SlaveNotifications", child->key) == 0)
182                         status = cf_util_get_boolean (child, &db->slave_notif);
183                 else if (strcasecmp ("InnodbStats", child->key) == 0)
184                         status = cf_util_get_boolean (child, &db->innodb_stats);
185                 else
186                 {
187                         WARNING ("mysql plugin: Option `%s' not allowed here.", child->key);
188                         status = -1;
189                 }
190
191                 if (status != 0)
192                         break;
193         }
194
195         /* If all went well, register this database for reading */
196         if (status == 0)
197         {
198                 user_data_t ud;
199                 char cb_name[DATA_MAX_NAME_LEN];
200
201                 DEBUG ("mysql plugin: Registering new read callback: %s",
202                                 (db->database != NULL) ? db->database : "<default>");
203
204                 memset (&ud, 0, sizeof (ud));
205                 ud.data = (void *) db;
206                 ud.free_func = mysql_database_free;
207
208                 if (db->instance != NULL)
209                         ssnprintf (cb_name, sizeof (cb_name), "mysql-%s",
210                                         db->instance);
211                 else
212                         sstrncpy (cb_name, "mysql", sizeof (cb_name));
213
214                 plugin_register_complex_read (/* group = */ NULL, cb_name,
215                                               mysql_read,
216                                               /* interval = */ NULL, &ud);
217         }
218         else
219         {
220                 mysql_database_free (db);
221                 return (-1);
222         }
223
224         return (0);
225 } /* }}} int mysql_config_database */
226
227 static int mysql_config (oconfig_item_t *ci) /* {{{ */
228 {
229         int i;
230
231         if (ci == NULL)
232                 return (EINVAL);
233
234         /* Fill the `mysql_database_t' structure.. */
235         for (i = 0; i < ci->children_num; i++)
236         {
237                 oconfig_item_t *child = ci->children + i;
238
239                 if (strcasecmp ("Database", child->key) == 0)
240                         mysql_config_database (child);
241                 else
242                         WARNING ("mysql plugin: Option \"%s\" not allowed here.",
243                                         child->key);
244         }
245
246         return (0);
247 } /* }}} int mysql_config */
248
249 /* }}} End of configuration handling functions */
250
251 static MYSQL *getconnection (mysql_database_t *db)
252 {
253         if (db->is_connected)
254         {
255                 int status;
256
257                 status = mysql_ping (db->con);
258                 if (status == 0)
259                         return (db->con);
260
261                 WARNING ("mysql plugin: Lost connection to instance \"%s\": %s",
262                                 db->instance, mysql_error (db->con));
263         }
264         db->is_connected = 0;
265
266         if (db->con == NULL)
267         {
268                 db->con = mysql_init (NULL);
269                 if (db->con == NULL)
270                 {
271                         ERROR ("mysql plugin: mysql_init failed: %s",
272                                         mysql_error (db->con));
273                         return (NULL);
274                 }
275         }
276
277         /* Configure TCP connect timeout (default: 0) */
278         db->con->options.connect_timeout = db->timeout;
279
280         if (mysql_real_connect (db->con, db->host, db->user, db->pass,
281                                 db->database, db->port, db->socket, 0) == NULL)
282         {
283                 ERROR ("mysql plugin: Failed to connect to database %s "
284                                 "at server %s: %s",
285                                 (db->database != NULL) ? db->database : "<none>",
286                                 (db->host != NULL) ? db->host : "localhost",
287                                 mysql_error (db->con));
288                 return (NULL);
289         }
290
291         INFO ("mysql plugin: Successfully connected to database %s "
292                         "at server %s (server version: %s, protocol version: %d)",
293                         (db->database != NULL) ? db->database : "<none>",
294                         mysql_get_host_info (db->con),
295                         mysql_get_server_info (db->con),
296                         mysql_get_proto_info (db->con));
297
298         db->is_connected = 1;
299         return (db->con);
300 } /* static MYSQL *getconnection (mysql_database_t *db) */
301
302 static void set_host (mysql_database_t *db, char *buf, size_t buflen)
303 {
304         if (db->alias)
305                 sstrncpy (buf, db->alias, buflen);
306         else if ((db->host == NULL)
307                         || (strcmp ("", db->host) == 0)
308                         || (strcmp ("127.0.0.1", db->host) == 0)
309                         || (strcmp ("localhost", db->host) == 0))
310                 sstrncpy (buf, hostname_g, buflen);
311         else
312                 sstrncpy (buf, db->host, buflen);
313 } /* void set_host */
314
315 static void submit (const char *type, const char *type_instance,
316                 value_t *values, size_t values_len, mysql_database_t *db)
317 {
318         value_list_t vl = VALUE_LIST_INIT;
319
320         vl.values     = values;
321         vl.values_len = values_len;
322
323         set_host (db, vl.host, sizeof (vl.host));
324
325         sstrncpy (vl.plugin, "mysql", sizeof (vl.plugin));
326
327         /* Assured by "mysql_config_database" */
328         assert (db->instance != NULL);
329         sstrncpy (vl.plugin_instance, db->instance, sizeof (vl.plugin_instance));
330
331         sstrncpy (vl.type, type, sizeof (vl.type));
332         if (type_instance != NULL)
333                 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
334
335         plugin_dispatch_values (&vl);
336 } /* submit */
337
338 static void counter_submit (const char *type, const char *type_instance,
339                 derive_t value, mysql_database_t *db)
340 {
341         value_t values[1];
342
343         values[0].derive = value;
344         submit (type, type_instance, values, STATIC_ARRAY_SIZE (values), db);
345 } /* void counter_submit */
346
347 static void gauge_submit (const char *type, const char *type_instance,
348                 gauge_t value, mysql_database_t *db)
349 {
350         value_t values[1];
351
352         values[0].gauge = value;
353         submit (type, type_instance, values, STATIC_ARRAY_SIZE (values), db);
354 } /* void gauge_submit */
355
356 static void derive_submit (const char *type, const char *type_instance,
357                 derive_t value, mysql_database_t *db)
358 {
359         value_t values[1];
360
361         values[0].derive = value;
362         submit (type, type_instance, values, STATIC_ARRAY_SIZE (values), db);
363 } /* void derive_submit */
364
365 static void traffic_submit (derive_t rx, derive_t tx, mysql_database_t *db)
366 {
367         value_t values[2];
368
369         values[0].derive = rx;
370         values[1].derive = tx;
371
372         submit ("mysql_octets", NULL, values, STATIC_ARRAY_SIZE (values), db);
373 } /* void traffic_submit */
374
375 static MYSQL_RES *exec_query (MYSQL *con, const char *query)
376 {
377         MYSQL_RES *res;
378
379         int query_len = strlen (query);
380
381         if (mysql_real_query (con, query, query_len))
382         {
383                 ERROR ("mysql plugin: Failed to execute query: %s",
384                                 mysql_error (con));
385                 INFO ("mysql plugin: SQL query was: %s", query);
386                 return (NULL);
387         }
388
389         res = mysql_store_result (con);
390         if (res == NULL)
391         {
392                 ERROR ("mysql plugin: Failed to store query result: %s",
393                                 mysql_error (con));
394                 INFO ("mysql plugin: SQL query was: %s", query);
395                 return (NULL);
396         }
397
398         return (res);
399 } /* exec_query */
400
401 static int mysql_read_master_stats (mysql_database_t *db, MYSQL *con)
402 {
403         MYSQL_RES *res;
404         MYSQL_ROW  row;
405
406         char *query;
407         int   field_num;
408         unsigned long long position;
409
410         query = "SHOW MASTER STATUS";
411
412         res = exec_query (con, query);
413         if (res == NULL)
414                 return (-1);
415
416         row = mysql_fetch_row (res);
417         if (row == NULL)
418         {
419                 ERROR ("mysql plugin: Failed to get master statistics: "
420                                 "`%s' did not return any rows.", query);
421                 mysql_free_result (res);
422                 return (-1);
423         }
424
425         field_num = mysql_num_fields (res);
426         if (field_num < 2)
427         {
428                 ERROR ("mysql plugin: Failed to get master statistics: "
429                                 "`%s' returned less than two columns.", query);
430                 mysql_free_result (res);
431                 return (-1);
432         }
433
434         position = atoll (row[1]);
435         counter_submit ("mysql_log_position", "master-bin", position, db);
436
437         row = mysql_fetch_row (res);
438         if (row != NULL)
439                 WARNING ("mysql plugin: `%s' returned more than one row - "
440                                 "ignoring further results.", query);
441
442         mysql_free_result (res);
443
444         return (0);
445 } /* mysql_read_master_stats */
446
447 static int mysql_read_slave_stats (mysql_database_t *db, MYSQL *con)
448 {
449         MYSQL_RES *res;
450         MYSQL_ROW  row;
451
452         char *query;
453         int   field_num;
454
455         /* WTF? libmysqlclient does not seem to provide any means to
456          * translate a column name to a column index ... :-/ */
457         const int READ_MASTER_LOG_POS_IDX   = 6;
458         const int SLAVE_IO_RUNNING_IDX      = 10;
459         const int SLAVE_SQL_RUNNING_IDX     = 11;
460         const int EXEC_MASTER_LOG_POS_IDX   = 21;
461         const int SECONDS_BEHIND_MASTER_IDX = 32;
462
463         query = "SHOW SLAVE STATUS";
464
465         res = exec_query (con, query);
466         if (res == NULL)
467                 return (-1);
468
469         row = mysql_fetch_row (res);
470         if (row == NULL)
471         {
472                 ERROR ("mysql plugin: Failed to get slave statistics: "
473                                 "`%s' did not return any rows.", query);
474                 mysql_free_result (res);
475                 return (-1);
476         }
477
478         field_num = mysql_num_fields (res);
479         if (field_num < 33)
480         {
481                 ERROR ("mysql plugin: Failed to get slave statistics: "
482                                 "`%s' returned less than 33 columns.", query);
483                 mysql_free_result (res);
484                 return (-1);
485         }
486
487         if (db->slave_stats)
488         {
489                 unsigned long long counter;
490                 double gauge;
491
492                 counter = atoll (row[READ_MASTER_LOG_POS_IDX]);
493                 counter_submit ("mysql_log_position", "slave-read", counter, db);
494
495                 counter = atoll (row[EXEC_MASTER_LOG_POS_IDX]);
496                 counter_submit ("mysql_log_position", "slave-exec", counter, db);
497
498                 if (row[SECONDS_BEHIND_MASTER_IDX] != NULL)
499                 {
500                         gauge = atof (row[SECONDS_BEHIND_MASTER_IDX]);
501                         gauge_submit ("time_offset", NULL, gauge, db);
502                 }
503         }
504
505         if (db->slave_notif)
506         {
507                 notification_t n = { 0, cdtime (), "", "",
508                         "mysql", "", "time_offset", "", NULL };
509
510                 char *io, *sql;
511
512                 io  = row[SLAVE_IO_RUNNING_IDX];
513                 sql = row[SLAVE_SQL_RUNNING_IDX];
514
515                 set_host (db, n.host, sizeof (n.host));
516
517                 /* Assured by "mysql_config_database" */
518                 assert (db->instance != NULL);
519                 sstrncpy (n.plugin_instance, db->instance, sizeof (n.plugin_instance));
520
521                 if (((io == NULL) || (strcasecmp (io, "yes") != 0))
522                                 && (db->slave_io_running))
523                 {
524                         n.severity = NOTIF_WARNING;
525                         ssnprintf (n.message, sizeof (n.message),
526                                         "slave I/O thread not started or not connected to master");
527                         plugin_dispatch_notification (&n);
528                         db->slave_io_running = 0;
529                 }
530                 else if (((io != NULL) && (strcasecmp (io, "yes") == 0))
531                                 && (! db->slave_io_running))
532                 {
533                         n.severity = NOTIF_OKAY;
534                         ssnprintf (n.message, sizeof (n.message),
535                                         "slave I/O thread started and connected to master");
536                         plugin_dispatch_notification (&n);
537                         db->slave_io_running = 1;
538                 }
539
540                 if (((sql == NULL) || (strcasecmp (sql, "yes") != 0))
541                                 && (db->slave_sql_running))
542                 {
543                         n.severity = NOTIF_WARNING;
544                         ssnprintf (n.message, sizeof (n.message),
545                                         "slave SQL thread not started");
546                         plugin_dispatch_notification (&n);
547                         db->slave_sql_running = 0;
548                 }
549                 else if (((sql != NULL) && (strcasecmp (sql, "yes") == 0))
550                                 && (! db->slave_sql_running))
551                 {
552                         n.severity = NOTIF_OKAY;
553                         ssnprintf (n.message, sizeof (n.message),
554                                         "slave SQL thread started");
555                         plugin_dispatch_notification (&n);
556                         db->slave_sql_running = 1;
557                 }
558         }
559
560         row = mysql_fetch_row (res);
561         if (row != NULL)
562                 WARNING ("mysql plugin: `%s' returned more than one row - "
563                                 "ignoring further results.", query);
564
565         mysql_free_result (res);
566
567         return (0);
568 } /* mysql_read_slave_stats */
569
570 static int mysql_read (user_data_t *ud)
571 {
572         mysql_database_t *db;
573         MYSQL     *con;
574         MYSQL_RES *res;
575         MYSQL_ROW  row;
576         char      *query;
577
578         derive_t qcache_hits          = 0;
579         derive_t qcache_inserts       = 0;
580         derive_t qcache_not_cached    = 0;
581         derive_t qcache_lowmem_prunes = 0;
582         gauge_t qcache_queries_in_cache = NAN;
583
584         gauge_t threads_running   = NAN;
585         gauge_t threads_connected = NAN;
586         gauge_t threads_cached    = NAN;
587         derive_t threads_created = 0;
588
589         unsigned long long traffic_incoming = 0ULL;
590         unsigned long long traffic_outgoing = 0ULL;
591
592         if ((ud == NULL) || (ud->data == NULL))
593         {
594                 ERROR ("mysql plugin: mysql_database_read: Invalid user data.");
595                 return (-1);
596         }
597
598         db = (mysql_database_t *) ud->data;
599
600         /* An error message will have been printed in this case */
601         if ((con = getconnection (db)) == NULL)
602                 return (-1);
603
604         query = "SHOW STATUS";
605         if (mysql_get_server_version (con) >= 50002)
606                 query = "SHOW GLOBAL STATUS";
607
608         res = exec_query (con, query);
609         if (res == NULL)
610                 return (-1);
611
612         while ((row = mysql_fetch_row (res)))
613         {
614                 char *key;
615                 unsigned long long val;
616
617                 key = row[0];
618                 val = atoll (row[1]);
619
620                 if (strncmp (key, "Com_", 
621                                   strlen ("Com_")) == 0)
622                 {
623                         if (val == 0ULL)
624                                 continue;
625
626                         /* Ignore `prepared statements' */
627                         if (strncmp (key, "Com_stmt_", strlen ("Com_stmt_")) != 0)
628                                 counter_submit ("mysql_commands", 
629                                                 key + strlen ("Com_"), 
630                                                 val, db);
631                 }
632                 else if (strncmp (key, "Handler_", 
633                                         strlen ("Handler_")) == 0)
634                 {
635                         if (val == 0ULL)
636                                 continue;
637
638                         counter_submit ("mysql_handler", 
639                                         key + strlen ("Handler_"), 
640                                         val, db);
641                 }
642                 else if (strncmp (key, "Qcache_",
643                                         strlen ("Qcache_")) == 0)
644                 {
645                         if (strcmp (key, "Qcache_hits") == 0)
646                                 qcache_hits = (derive_t) val;
647                         else if (strcmp (key, "Qcache_inserts") == 0)
648                                 qcache_inserts = (derive_t) val;
649                         else if (strcmp (key, "Qcache_not_cached") == 0)
650                                 qcache_not_cached = (derive_t) val;
651                         else if (strcmp (key, "Qcache_lowmem_prunes") == 0)
652                                 qcache_lowmem_prunes = (derive_t) val;
653                         else if (strcmp (key, "Qcache_queries_in_cache") == 0)
654                                 qcache_queries_in_cache = (gauge_t) val;
655                 }
656                 else if (strncmp (key, "Bytes_", 
657                                         strlen ("Bytes_")) == 0)
658                 {
659                         if (strcmp (key, "Bytes_received") == 0)
660                                 traffic_incoming += val;
661                         else if (strcmp (key, "Bytes_sent") == 0)
662                                 traffic_outgoing += val;
663                 }
664                 else if (strncmp (key, "Threads_", 
665                                         strlen ("Threads_")) == 0)
666                 {
667                         if (strcmp (key, "Threads_running") == 0)
668                                 threads_running = (gauge_t) val;
669                         else if (strcmp (key, "Threads_connected") == 0)
670                                 threads_connected = (gauge_t) val;
671                         else if (strcmp (key, "Threads_cached") == 0)
672                                 threads_cached = (gauge_t) val;
673                         else if (strcmp (key, "Threads_created") == 0)
674                                 threads_created = (derive_t) val;
675                 }
676                 else if (strncmp (key, "Table_locks_",
677                                         strlen ("Table_locks_")) == 0)
678                 {
679                         counter_submit ("mysql_locks",
680                                         key + strlen ("Table_locks_"),
681                                         val, db);
682                 }
683                 else if (db->innodb_stats && strncmp (key, "Innodb_", strlen ("Innodb_")) == 0)
684                 {
685                         /* buffer pool */
686                         if (strcmp (key, "Innodb_buffer_pool_pages_data") == 0)
687                                 gauge_submit ("mysql_bpool_pages", "data", val, db);
688                         else if (strcmp (key, "Innodb_buffer_pool_pages_dirty") == 0)
689                                 gauge_submit ("mysql_bpool_pages", "dirty", val, db);
690                         else if (strcmp (key, "Innodb_buffer_pool_pages_flushed") == 0)
691                                 counter_submit ("mysql_bpool_counters", "flushed", val, db);
692                         else if (strcmp (key, "Innodb_buffer_pool_pages_free") == 0)
693                                 gauge_submit ("mysql_bpool_pages", "free", val, db);
694                         else if (strcmp (key, "Innodb_buffer_pool_pages_misc") == 0)
695                                 gauge_submit ("mysql_bpool_pages", "misc", val, db);
696                         else if (strcmp (key, "Innodb_buffer_pool_pages_total") == 0)
697                                 gauge_submit ("mysql_bpool_pages", "total", val, db);
698                         else if (strcmp (key, "Innodb_buffer_pool_read_ahead_rnd") == 0)
699                                 counter_submit ("mysql_bpool_counters", "read_ahead_rnd", val, db);
700                         else if (strcmp (key, "Innodb_buffer_pool_read_ahead") == 0)
701                                 counter_submit ("mysql_bpool_counters", "read_ahead", val, db);
702                         else if (strcmp (key, "Innodb_buffer_pool_read_ahead_evicted") == 0)
703                                 counter_submit ("mysql_bpool_counters", "read_ahead_evicted", val, db);
704                         else if (strcmp (key, "Innodb_buffer_pool_read_requests") == 0)
705                                 counter_submit ("mysql_bpool_counters", "read_requests", val, db);
706                         else if (strcmp (key, "Innodb_buffer_pool_reads") == 0)
707                                 counter_submit ("mysql_bpool_counters", "reads", val, db);
708                         else if (strcmp (key, "Innodb_buffer_pool_write_requests") == 0)
709                                 counter_submit ("mysql_bpool_counters", "write_requests", val, db);
710
711                         /* data */
712                         if (strcmp (key, "Innodb_data_fsyncs") == 0)
713                                 counter_submit ("mysql_innodb_data", "fsyncs", val, db);
714                         else if (strcmp (key, "Innodb_data_read") == 0)
715                                 counter_submit ("mysql_innodb_data", "read", val, db);
716                         else if (strcmp (key, "Innodb_data_reads") == 0)
717                                 counter_submit ("mysql_innodb_data", "reads", val, db);
718                         else if (strcmp (key, "Innodb_data_writes") == 0)
719                                 counter_submit ("mysql_bpool_counters", "writes", val, db);
720                         else if (strcmp (key, "Innodb_data_written") == 0)
721                                 counter_submit ("mysql_innodb_data", "written", val, db);
722
723                         /* double write */
724                         else if (strcmp (key, "Innodb_dblwr_writes") == 0)
725                                 counter_submit ("mysql_innodb_dblwr", "writes", val, db);
726                         else if (strcmp (key, "Innodb_dblwr_pages_written") == 0)
727                                 counter_submit ("mysql_innodb_dblwr", "written", val, db);
728
729                         /* rows */
730                         else if (strcmp (key, "Innodb_rows_deleted") == 0)
731                                 counter_submit ("mysql_innodb_rows", "deleted", val, db);
732                         else if (strcmp (key, "Innodb_rows_inserted") == 0)
733                                 counter_submit ("mysql_innodb_rows", "inserted", val, db);
734                         else if (strcmp (key, "Innodb_rows_read") == 0)
735                                 counter_submit ("mysql_innodb_rows", "read", val, db);
736                         else if (strcmp (key, "Innodb_rows_updated") == 0)
737                                 counter_submit ("mysql_innodb_rows", "updated", val, db);
738                 }
739                 else if (strncmp (key, "Select_", strlen ("Select_")) == 0)
740                 {
741                         counter_submit ("mysql_select", key + strlen ("Select_"),
742                                         val, db);
743                 }
744                 else if (strncmp (key, "Sort_", strlen ("Sort_")) == 0)
745                 {
746                         counter_submit ("mysql_sort", key + strlen ("Sort_"),
747                                         val, db);
748                 }
749         }
750         mysql_free_result (res); res = NULL;
751
752         if ((qcache_hits != 0)
753                         || (qcache_inserts != 0)
754                         || (qcache_not_cached != 0)
755                         || (qcache_lowmem_prunes != 0))
756         {
757                 derive_submit ("cache_result", "qcache-hits",
758                                 qcache_hits, db);
759                 derive_submit ("cache_result", "qcache-inserts",
760                                 qcache_inserts, db);
761                 derive_submit ("cache_result", "qcache-not_cached",
762                                 qcache_not_cached, db);
763                 derive_submit ("cache_result", "qcache-prunes",
764                                 qcache_lowmem_prunes, db);
765
766                 gauge_submit ("cache_size", "qcache",
767                                 qcache_queries_in_cache, db);
768         }
769
770         if (threads_created != 0)
771         {
772                 gauge_submit ("threads", "running",
773                                 threads_running, db);
774                 gauge_submit ("threads", "connected",
775                                 threads_connected, db);
776                 gauge_submit ("threads", "cached",
777                                 threads_cached, db);
778
779                 derive_submit ("total_threads", "created",
780                                 threads_created, db);
781         }
782
783         traffic_submit  (traffic_incoming, traffic_outgoing, db);
784
785         if (db->master_stats)
786                 mysql_read_master_stats (db, con);
787
788         if ((db->slave_stats) || (db->slave_notif))
789                 mysql_read_slave_stats (db, con);
790
791         return (0);
792 } /* int mysql_read */
793
794 void module_register (void)
795 {
796         plugin_register_complex_config ("mysql", mysql_config);
797 } /* void module_register */