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