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