Merge branch 'collectd-4.6'
[collectd.git] / src / mysql.c
1 /**
2  * collectd - src/mysql.c
3  * Copyright (C) 2006-2009  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 verplant.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 /* TODO: Understand `Select_*' and possibly do that stuff as well.. */
42
43 struct mysql_database_s /* {{{ */
44 {
45         /* instance == NULL  =>  legacy mode */
46         char *instance;
47         char *host;
48         char *user;
49         char *pass;
50         char *database;
51         char *socket;
52         int   port;
53
54         int   master_stats;
55         int   slave_stats;
56
57         int   slave_notif;
58         int   slave_io_running;
59         int   slave_sql_running;
60
61         MYSQL *con;
62         int    state;
63 };
64 typedef struct mysql_database_s mysql_database_t; /* }}} */
65
66 static int mysql_read (user_data_t *ud);
67
68 static void mysql_database_free (void *arg) /* {{{ */
69 {
70         mysql_database_t *db;
71
72         DEBUG ("mysql plugin: mysql_database_free (arg = %p);", arg);
73
74         db = (mysql_database_t *) arg;
75
76         if (db == NULL)
77                 return;
78
79         if (db->con != NULL)
80                 mysql_close (db->con);
81
82         sfree (db->host);
83         sfree (db->user);
84         sfree (db->pass);
85         sfree (db->socket);
86         sfree (db->instance);
87         sfree (db->database);
88         sfree (db);
89 } /* }}} void mysql_database_free */
90
91 /* Configuration handling functions {{{
92  *
93  * <Plugin mysql>
94  *   <Database "plugin_instance1">
95  *     Host "localhost"
96  *     Port 22000
97  *     ...
98  *   </Database>
99  * </Plugin>
100  */
101
102 static int mysql_config_set_string (char **ret_string, /* {{{ */
103                                     oconfig_item_t *ci)
104 {
105         char *string;
106
107         if ((ci->values_num != 1)
108             || (ci->values[0].type != OCONFIG_TYPE_STRING))
109         {
110                 WARNING ("mysql plugin: The `%s' config option "
111                          "needs exactly one string argument.", ci->key);
112                 return (-1);
113         }
114
115         string = strdup (ci->values[0].value.string);
116         if (string == NULL)
117         {
118                 ERROR ("mysql plugin: strdup failed.");
119                 return (-1);
120         }
121
122         if (*ret_string != NULL)
123                 free (*ret_string);
124         *ret_string = string;
125
126         return (0);
127 } /* }}} int mysql_config_set_string */
128
129 static int mysql_config_set_int (int *ret_int, /* {{{ */
130                                  oconfig_item_t *ci)
131 {
132         if ((ci->values_num != 1)
133             || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
134         {
135                 WARNING ("mysql plugin: The `%s' config option "
136                          "needs exactly one string argument.", ci->key);
137                 return (-1);
138         }
139
140         *ret_int = ci->values[0].value.number;
141
142         return (0);
143 } /* }}} int mysql_config_set_int */
144
145 static int mysql_config_set_boolean (int *ret_boolean, /* {{{ */
146                                 oconfig_item_t *ci)
147 {
148         int status = 0;
149
150         if (ci->values_num != 1)
151                 status = -1;
152
153         if (status == 0)
154         {
155                 if (ci->values[0].type == OCONFIG_TYPE_BOOLEAN)
156                         *ret_boolean = ci->values[0].value.boolean;
157                 else if (ci->values[0].type == OCONFIG_TYPE_STRING)
158                 {
159                         if (IS_TRUE (ci->values[0].value.string))
160                                 *ret_boolean = 1;
161                         else if (IS_FALSE (ci->values[0].value.string))
162                                 *ret_boolean = 0;
163                         else
164                                 status = -1;
165                 }
166                 else
167                         status = -1;
168         }
169
170         if (status != 0)
171         {
172                 WARNING ("mysql plugin: The `%s' config option "
173                         "needs exactly one boolean argument.", ci->key);
174                 return (-1);
175         }
176         return (0);
177 } /* }}} mysql_config_set_boolean */
178
179 static int mysql_config (oconfig_item_t *ci) /* {{{ */
180 {
181         mysql_database_t *db;
182         int plugin_block;
183         int status = 0;
184         int i;
185
186         if ((ci->values_num != 1)
187             || (ci->values[0].type != OCONFIG_TYPE_STRING))
188         {
189                 WARNING ("mysql plugin: The `Database' block "
190                          "needs exactly one string argument.");
191                 return (-1);
192         }
193
194         db = (mysql_database_t *) malloc (sizeof (*db));
195         if (db == NULL)
196         {
197                 ERROR ("mysql plugin: malloc failed.");
198                 return (-1);
199         }
200         memset (db, 0, sizeof (*db));
201
202         /* initialize all the pointers */
203         db->host     = NULL;
204         db->user     = NULL;
205         db->pass     = NULL;
206         db->database = NULL;
207         db->socket   = NULL;
208         db->con      = NULL;
209
210         /* trigger a notification, if it's not running */
211         db->slave_io_running  = 1;
212         db->slave_sql_running = 1;
213
214         plugin_block = 1;
215         if (strcasecmp ("Plugin", ci->key) == 0)
216         {
217                 db->instance = NULL;
218         }
219         else if (strcasecmp ("Database", ci->key) == 0)
220         {
221                 plugin_block = 0;
222                 status = mysql_config_set_string (&db->instance, ci);
223                 if (status != 0)
224                 {
225                         sfree (db);
226                         return (status);
227                 }
228                 assert (db->instance != NULL);
229         }
230         else
231         {
232                 ERROR ("mysql plugin: mysql_config: "
233                                 "Invalid key: %s", ci->key);
234                 return (-1);
235         }
236
237         /* Fill the `mysql_database_t' structure.. */
238         for (i = 0; i < ci->children_num; i++)
239         {
240                 oconfig_item_t *child = ci->children + i;
241
242                 if (strcasecmp ("Host", child->key) == 0)
243                         status = mysql_config_set_string (&db->host, child);
244                 else if (strcasecmp ("User", child->key) == 0)
245                         status = mysql_config_set_string (&db->user, child);
246                 else if (strcasecmp ("Password", child->key) == 0)
247                         status = mysql_config_set_string (&db->pass, child);
248                 else if (strcasecmp ("Port", child->key) == 0)
249                         status = mysql_config_set_int (&db->port, child);
250                 else if (strcasecmp ("Socket", child->key) == 0)
251                         status = mysql_config_set_string (&db->socket, child);
252                 /* Check if we're currently handling the `Plugin' block. If so,
253                  * handle `Database' _blocks_, too. */
254                 else if ((plugin_block != 0)
255                                 && (strcasecmp ("Database", child->key) == 0)
256                                 && (child->children != NULL))
257                 {
258                         /* If `plugin_block > 1', there has been at least one
259                          * `Database' block */
260                         plugin_block++;
261                         status = mysql_config (child);
262                 }
263                 /* Now handle ordinary `Database' options (without children) */
264                 else if ((strcasecmp ("Database", child->key) == 0)
265                                 && (child->children == NULL))
266                         status = mysql_config_set_string (&db->database, child);
267                 else if (strcasecmp ("MasterStats", child->key) == 0)
268                         status = mysql_config_set_boolean (&db->master_stats, child);
269                 else if (strcasecmp ("SlaveStats", child->key) == 0)
270                         status = mysql_config_set_boolean (&db->slave_stats, child);
271                 else if (strcasecmp ("SlaveNotifications", child->key) == 0)
272                         status = mysql_config_set_boolean (&db->slave_notif, child);
273                 else
274                 {
275                         WARNING ("mysql plugin: Option `%s' not allowed here.", child->key);
276                         status = -1;
277                 }
278
279                 if (status != 0)
280                         break;
281         }
282
283         /* Check if there were any `Database' blocks. */
284         if (plugin_block > 1)
285         {
286                 /* There were connection blocks. Don't use any legacy stuff. */
287                 if ((db->host != NULL)
288                         || (db->user != NULL)
289                         || (db->pass != NULL)
290                         || (db->database != NULL)
291                         || (db->socket != NULL)
292                         || (db->port != 0))
293                 {
294                         WARNING ("mysql plugin: At least one <Database> "
295                                         "block has been found. The legacy "
296                                         "configuration will be ignored.");
297                 }
298                 mysql_database_free (db);
299                 return (0);
300         }
301         else if (plugin_block != 0)
302         {
303                 WARNING ("mysql plugin: You're using the legacy "
304                                 "configuration options. Please consider "
305                                 "updating your configuration!");
306         }
307
308         /* Check that all necessary options have been given. */
309         while (status == 0)
310         {
311                 /* Zero is allowed and automatically handled by
312                  * `mysql_real_connect'. */
313                 if ((db->port < 0) || (db->port > 65535))
314                 {
315                         ERROR ("mysql plugin: Database %s: Port number out "
316                                         "of range: %i",
317                                         (db->instance != NULL)
318                                         ? db->instance
319                                         : "<legacy>",
320                                         db->port);
321                         status = -1;
322                 }
323                 break;
324         } /* while (status == 0) */
325
326         /* If all went well, register this database for reading */
327         if (status == 0)
328         {
329                 user_data_t ud;
330                 char cb_name[DATA_MAX_NAME_LEN];
331
332                 DEBUG ("mysql plugin: Registering new read callback: %s",
333                                 (db->database != NULL) ? db->database : "<default>");
334
335                 memset (&ud, 0, sizeof (ud));
336                 ud.data = (void *) db;
337                 ud.free_func = mysql_database_free;
338
339                 if (db->database != NULL)
340                         ssnprintf (cb_name, sizeof (cb_name), "mysql-%s",
341                                         db->database);
342                 else
343                         sstrncpy (cb_name, "mysql", sizeof (cb_name));
344
345                 plugin_register_complex_read (cb_name, mysql_read,
346                                               /* interval = */ NULL, &ud);
347         }
348         else
349         {
350                 mysql_database_free (db);
351                 return (-1);
352         }
353
354         return (0);
355 } /* }}} int mysql_config */
356
357 /* }}} End of configuration handling functions */
358
359 static MYSQL *getconnection (mysql_database_t *db)
360 {
361         if (db->state != 0)
362         {
363                 int err;
364                 if ((err = mysql_ping (db->con)) != 0)
365                 {
366                         WARNING ("mysql_ping failed: %s", mysql_error (db->con));
367                         db->state = 0;
368                 }
369                 else
370                 {
371                         db->state = 1;
372                         return (db->con);
373                 }
374         }
375
376         if ((db->con = mysql_init (db->con)) == NULL)
377         {
378                 ERROR ("mysql_init failed: %s", mysql_error (db->con));
379                 db->state = 0;
380                 return (NULL);
381         }
382
383         if (mysql_real_connect (db->con, db->host, db->user, db->pass,
384                                 db->database, db->port, db->socket, 0) == NULL)
385         {
386                 ERROR ("mysql plugin: Failed to connect to database %s "
387                                 "at server %s: %s",
388                                 (db->database != NULL) ? db->database : "<none>",
389                                 (db->host != NULL) ? db->host : "localhost",
390                                 mysql_error (db->con));
391                 db->state = 0;
392                 return (NULL);
393         }
394         else
395         {
396                 INFO ("mysql plugin: Sucessfully connected to database %s "
397                                 "at server %s (server version: %s, protocol version: %d)",
398                                 (db->database != NULL) ? db->database : "<none>",
399                                 mysql_get_host_info (db->con),
400                                 mysql_get_server_info (db->con),
401                                 mysql_get_proto_info (db->con));
402                 db->state = 1;
403                 return (db->con);
404         }
405 } /* static MYSQL *getconnection (mysql_database_t *db) */
406
407 static void set_host (mysql_database_t *db, char *buf, size_t buflen)
408 {
409         /* XXX legacy mode - use hostname_g */
410         if (db->instance == NULL)
411                 sstrncpy (buf, hostname_g, buflen);
412         else
413         {
414                 if ((db->host == NULL)
415                                 || (strcmp ("", db->host) == 0)
416                                 || (strcmp ("localhost", db->host) == 0))
417                         sstrncpy (buf, hostname_g, buflen);
418                 else
419                         sstrncpy (buf, db->host, buflen);
420         }
421 }
422
423 static void set_plugin_instance (mysql_database_t *db,
424                 char *buf, size_t buflen)
425 {
426         /* XXX legacy mode - no plugin_instance */
427         if (db->instance == NULL)
428                 sstrncpy (buf, "", buflen);
429         else
430                 sstrncpy (buf, db->instance, buflen);
431 }
432
433 static void submit (const char *type, const char *type_instance,
434                 value_t *values, size_t values_len, mysql_database_t *db)
435 {
436         value_list_t vl = VALUE_LIST_INIT;
437
438         vl.values     = values;
439         vl.values_len = values_len;
440
441         set_host (db, vl.host, sizeof (vl.host));
442
443         sstrncpy (vl.plugin, "mysql", sizeof (vl.plugin));
444         set_plugin_instance (db, vl.plugin_instance, sizeof (vl.plugin_instance));
445
446         sstrncpy (vl.type, type, sizeof (vl.type));
447         if (type_instance != NULL)
448                 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
449
450         plugin_dispatch_values (&vl);
451 } /* submit */
452
453 static void counter_submit (const char *type, const char *type_instance,
454                 counter_t value, mysql_database_t *db)
455 {
456         value_t values[1];
457
458         values[0].counter = value;
459         submit (type, type_instance, values, STATIC_ARRAY_SIZE (values), db);
460 } /* void counter_submit */
461
462 static void gauge_submit (const char *type, const char *type_instance,
463                 gauge_t value, mysql_database_t *db)
464 {
465         value_t values[1];
466
467         values[0].gauge = value;
468         submit (type, type_instance, values, STATIC_ARRAY_SIZE (values), db);
469 } /* void gauge_submit */
470
471 static void qcache_submit (counter_t hits, counter_t inserts,
472                 counter_t not_cached, counter_t lowmem_prunes,
473                 gauge_t queries_in_cache, mysql_database_t *db)
474 {
475         value_t values[5];
476
477         values[0].counter = hits;
478         values[1].counter = inserts;
479         values[2].counter = not_cached;
480         values[3].counter = lowmem_prunes;
481         values[4].gauge   = queries_in_cache;
482
483         submit ("mysql_qcache", NULL, values, STATIC_ARRAY_SIZE (values), db);
484 } /* void qcache_submit */
485
486 static void threads_submit (gauge_t running, gauge_t connected, gauge_t cached,
487                 counter_t created, mysql_database_t *db)
488 {
489         value_t values[4];
490
491         values[0].gauge   = running;
492         values[1].gauge   = connected;
493         values[2].gauge   = cached;
494         values[3].counter = created;
495
496         submit ("mysql_threads", NULL, values, STATIC_ARRAY_SIZE (values), db);
497 } /* void threads_submit */
498
499 static void traffic_submit (counter_t rx, counter_t tx, mysql_database_t *db)
500 {
501         value_t values[2];
502
503         values[0].counter = rx;
504         values[1].counter = tx;
505
506         submit ("mysql_octets", NULL, values, STATIC_ARRAY_SIZE (values), db);
507 } /* void traffic_submit */
508
509 static MYSQL_RES *exec_query (MYSQL *con, const char *query)
510 {
511         MYSQL_RES *res;
512
513         int query_len = strlen (query);
514
515         if (mysql_real_query (con, query, query_len))
516         {
517                 ERROR ("mysql plugin: Failed to execute query: %s",
518                                 mysql_error (con));
519                 INFO ("mysql plugin: SQL query was: %s", query);
520                 return (NULL);
521         }
522
523         res = mysql_store_result (con);
524         if (res == NULL)
525         {
526                 ERROR ("mysql plugin: Failed to store query result: %s",
527                                 mysql_error (con));
528                 INFO ("mysql plugin: SQL query was: %s", query);
529                 return (NULL);
530         }
531
532         return (res);
533 } /* exec_query */
534
535 static int mysql_read_master_stats (mysql_database_t *db, MYSQL *con)
536 {
537         MYSQL_RES *res;
538         MYSQL_ROW  row;
539
540         char *query;
541         int   field_num;
542         unsigned long long position;
543
544         query = "SHOW MASTER STATUS";
545
546         res = exec_query (con, query);
547         if (res == NULL)
548                 return (-1);
549
550         row = mysql_fetch_row (res);
551         if (row == NULL)
552         {
553                 ERROR ("mysql plugin: Failed to get master statistics: "
554                                 "`%s' did not return any rows.", query);
555                 return (-1);
556         }
557
558         field_num = mysql_num_fields (res);
559         if (field_num < 2)
560         {
561                 ERROR ("mysql plugin: Failed to get master statistics: "
562                                 "`%s' returned less than two columns.", query);
563                 return (-1);
564         }
565
566         position = atoll (row[1]);
567         counter_submit ("mysql_log_position", "master-bin", position, db);
568
569         row = mysql_fetch_row (res);
570         if (row != NULL)
571                 WARNING ("mysql plugin: `%s' returned more than one row - "
572                                 "ignoring further results.", query);
573
574         mysql_free_result (res);
575
576         return (0);
577 } /* mysql_read_master_stats */
578
579 static int mysql_read_slave_stats (mysql_database_t *db, MYSQL *con)
580 {
581         MYSQL_RES *res;
582         MYSQL_ROW  row;
583
584         char *query;
585         int   field_num;
586
587         /* WTF? libmysqlclient does not seem to provide any means to
588          * translate a column name to a column index ... :-/ */
589         const int READ_MASTER_LOG_POS_IDX   = 6;
590         const int SLAVE_IO_RUNNING_IDX      = 10;
591         const int SLAVE_SQL_RUNNING_IDX     = 11;
592         const int EXEC_MASTER_LOG_POS_IDX   = 21;
593         const int SECONDS_BEHIND_MASTER_IDX = 32;
594
595         query = "SHOW SLAVE STATUS";
596
597         res = exec_query (con, query);
598         if (res == NULL)
599                 return (-1);
600
601         row = mysql_fetch_row (res);
602         if (row == NULL)
603         {
604                 ERROR ("mysql plugin: Failed to get slave statistics: "
605                                 "`%s' did not return any rows.", query);
606                 return (-1);
607         }
608
609         field_num = mysql_num_fields (res);
610         if (field_num < 33)
611         {
612                 ERROR ("mysql plugin: Failed to get slave statistics: "
613                                 "`%s' returned less than 33 columns.", query);
614                 return (-1);
615         }
616
617         if (db->slave_stats)
618         {
619                 unsigned long long counter;
620                 double gauge;
621
622                 counter = atoll (row[READ_MASTER_LOG_POS_IDX]);
623                 counter_submit ("mysql_log_position", "slave-read", counter, db);
624
625                 counter = atoll (row[EXEC_MASTER_LOG_POS_IDX]);
626                 counter_submit ("mysql_log_position", "slave-exec", counter, db);
627
628                 if (row[SECONDS_BEHIND_MASTER_IDX] != NULL)
629                 {
630                         gauge = atof (row[SECONDS_BEHIND_MASTER_IDX]);
631                         gauge_submit ("time_offset", NULL, gauge, db);
632                 }
633         }
634
635         if (db->slave_notif)
636         {
637                 notification_t n = { 0, time (NULL), "", "",
638                         "mysql", "", "time_offset", "", NULL };
639
640                 char *io, *sql;
641
642                 io  = row[SLAVE_IO_RUNNING_IDX];
643                 sql = row[SLAVE_SQL_RUNNING_IDX];
644
645                 set_host (db, n.host, sizeof (n.host));
646                 set_plugin_instance (db,
647                                 n.plugin_instance, sizeof (n.plugin_instance));
648
649                 if (((io == NULL) || (strcasecmp (io, "yes") != 0))
650                                 && (db->slave_io_running))
651                 {
652                         n.severity = NOTIF_WARNING;
653                         ssnprintf (n.message, sizeof (n.message),
654                                         "slave I/O thread not started or not connected to master");
655                         plugin_dispatch_notification (&n);
656                         db->slave_io_running = 0;
657                 }
658                 else if (((io != NULL) && (strcasecmp (io, "yes") == 0))
659                                 && (! db->slave_io_running))
660                 {
661                         n.severity = NOTIF_OKAY;
662                         ssnprintf (n.message, sizeof (n.message),
663                                         "slave I/O thread started and connected to master");
664                         plugin_dispatch_notification (&n);
665                         db->slave_io_running = 1;
666                 }
667
668                 if (((sql == NULL) || (strcasecmp (sql, "yes") != 0))
669                                 && (db->slave_sql_running))
670                 {
671                         n.severity = NOTIF_WARNING;
672                         ssnprintf (n.message, sizeof (n.message),
673                                         "slave SQL thread not started");
674                         plugin_dispatch_notification (&n);
675                         db->slave_sql_running = 0;
676                 }
677                 else if (((sql != NULL) && (strcasecmp (sql, "yes") == 0))
678                                 && (! db->slave_sql_running))
679                 {
680                         n.severity = NOTIF_OKAY;
681                         ssnprintf (n.message, sizeof (n.message),
682                                         "slave SQL thread started");
683                         plugin_dispatch_notification (&n);
684                         db->slave_sql_running = 0;
685                 }
686         }
687
688         row = mysql_fetch_row (res);
689         if (row != NULL)
690                 WARNING ("mysql plugin: `%s' returned more than one row - "
691                                 "ignoring further results.", query);
692
693         mysql_free_result (res);
694
695         return (0);
696 } /* mysql_read_slave_stats */
697
698 static int mysql_read (user_data_t *ud)
699 {
700         mysql_database_t *db;
701         MYSQL     *con;
702         MYSQL_RES *res;
703         MYSQL_ROW  row;
704         char      *query;
705         int        field_num;
706
707         unsigned long long qcache_hits          = 0ULL;
708         unsigned long long qcache_inserts       = 0ULL;
709         unsigned long long qcache_not_cached    = 0ULL;
710         unsigned long long qcache_lowmem_prunes = 0ULL;
711         int qcache_queries_in_cache = -1;
712
713         int threads_running   = -1;
714         int threads_connected = -1;
715         int threads_cached    = -1;
716         unsigned long long threads_created = 0ULL;
717
718         unsigned long long traffic_incoming = 0ULL;
719         unsigned long long traffic_outgoing = 0ULL;
720
721         if ((ud == NULL) || (ud->data == NULL))
722         {
723                 ERROR ("mysql plugin: mysql_database_read: Invalid user data.");
724                 return (-1);
725         }
726
727         db = (mysql_database_t *) ud->data;
728
729         /* An error message will have been printed in this case */
730         if ((con = getconnection (db)) == NULL)
731                 return (-1);
732
733         query = "SHOW STATUS";
734         if (mysql_get_server_version (con) >= 50002)
735                 query = "SHOW GLOBAL STATUS";
736
737         res = exec_query (con, query);
738         if (res == NULL)
739                 return (-1);
740
741         field_num = mysql_num_fields (res);
742         while ((row = mysql_fetch_row (res)))
743         {
744                 char *key;
745                 unsigned long long val;
746
747                 key = row[0];
748                 val = atoll (row[1]);
749
750                 if (strncmp (key, "Com_", 
751                                   strlen ("Com_")) == 0)
752                 {
753                         if (val == 0ULL)
754                                 continue;
755
756                         /* Ignore `prepared statements' */
757                         if (strncmp (key, "Com_stmt_", strlen ("Com_stmt_")) != 0)
758                                 counter_submit ("mysql_commands", 
759                                                 key + strlen ("Com_"), 
760                                                 val, db);
761                 }
762                 else if (strncmp (key, "Handler_", 
763                                         strlen ("Handler_")) == 0)
764                 {
765                         if (val == 0ULL)
766                                 continue;
767
768                         counter_submit ("mysql_handler", 
769                                         key + strlen ("Handler_"), 
770                                         val, db);
771                 }
772                 else if (strncmp (key, "Qcache_",
773                                         strlen ("Qcache_")) == 0)
774                 {
775                         if (strcmp (key, "Qcache_hits") == 0)
776                                 qcache_hits = val;
777                         else if (strcmp (key, "Qcache_inserts") == 0)
778                                 qcache_inserts = val;
779                         else if (strcmp (key, "Qcache_not_cached") == 0)
780                                 qcache_not_cached = val;
781                         else if (strcmp (key, "Qcache_lowmem_prunes") == 0)
782                                 qcache_lowmem_prunes = val;
783                         else if (strcmp (key, "Qcache_queries_in_cache") == 0)
784                                 qcache_queries_in_cache = (int) val;
785                 }
786                 else if (strncmp (key, "Bytes_", 
787                                         strlen ("Bytes_")) == 0)
788                 {
789                         if (strcmp (key, "Bytes_received") == 0)
790                                 traffic_incoming += val;
791                         else if (strcmp (key, "Bytes_sent") == 0)
792                                 traffic_outgoing += val;
793                 }
794                 else if (strncmp (key, "Threads_", 
795                                         strlen ("Threads_")) == 0)
796                 {
797                         if (strcmp (key, "Threads_running") == 0)
798                                 threads_running = (int) val;
799                         else if (strcmp (key, "Threads_connected") == 0)
800                                 threads_connected = (int) val;
801                         else if (strcmp (key, "Threads_cached") == 0)
802                                 threads_cached = (int) val;
803                         else if (strcmp (key, "Threads_created") == 0)
804                                 threads_created = val;
805                 }
806                 else if (strncmp (key, "Table_locks_",
807                                         strlen ("Table_locks_")) == 0)
808                 {
809                         counter_submit ("mysql_locks",
810                                         key + strlen ("Table_locks_"),
811                                         val, db);
812                 }
813         }
814         mysql_free_result (res); res = NULL;
815
816         if ((qcache_hits != 0ULL)
817                         || (qcache_inserts != 0ULL)
818                         || (qcache_not_cached != 0ULL)
819                         || (qcache_lowmem_prunes != 0ULL))
820                 qcache_submit (qcache_hits, qcache_inserts, qcache_not_cached,
821                                qcache_lowmem_prunes, qcache_queries_in_cache, db);
822
823         if (threads_created != 0ULL)
824                 threads_submit (threads_running, threads_connected,
825                                 threads_cached, threads_created, db);
826
827         traffic_submit  (traffic_incoming, traffic_outgoing, db);
828
829         if (db->master_stats)
830                 mysql_read_master_stats (db, con);
831
832         if ((db->slave_stats) || (db->slave_notif))
833                 mysql_read_slave_stats (db, con);
834
835         return (0);
836 } /* int mysql_read */
837
838 void module_register (void)
839 {
840         plugin_register_complex_config ("mysql", mysql_config);
841 } /* void module_register */