mysql plugin: Log an info message after connecting to a server.
[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                 db->database = strdup (db->instance);
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                 if (db->database == NULL)
322                 {
323                         ERROR ("mysql plugin: No `Database' configured");
324                         status = -1;
325                 }
326                 break;
327         } /* while (status == 0) */
328
329         /* If all went well, register this database for reading */
330         if (status == 0)
331         {
332                 user_data_t ud;
333                 char cb_name[DATA_MAX_NAME_LEN];
334
335                 DEBUG ("mysql plugin: Registering new read callback: %s", db->database);
336
337                 memset (&ud, 0, sizeof (ud));
338                 ud.data = (void *) db;
339                 ud.free_func = mysql_database_free;
340
341                 if (db->database != NULL)
342                         ssnprintf (cb_name, sizeof (cb_name), "mysql-%s",
343                                         db->database);
344                 else
345                         sstrncpy (cb_name, "mysql", sizeof (cb_name));
346
347                 plugin_register_complex_read (cb_name, mysql_read,
348                                               /* interval = */ NULL, &ud);
349         }
350         else
351         {
352                 mysql_database_free (db);
353                 return (-1);
354         }
355
356         return (0);
357 } /* }}} int mysql_config */
358
359 /* }}} End of configuration handling functions */
360
361 static MYSQL *getconnection (mysql_database_t *db)
362 {
363         if (db->state != 0)
364         {
365                 int err;
366                 if ((err = mysql_ping (db->con)) != 0)
367                 {
368                         WARNING ("mysql_ping failed: %s", mysql_error (db->con));
369                         db->state = 0;
370                 }
371                 else
372                 {
373                         db->state = 1;
374                         return (db->con);
375                 }
376         }
377
378         if ((db->con = mysql_init (db->con)) == NULL)
379         {
380                 ERROR ("mysql_init failed: %s", mysql_error (db->con));
381                 db->state = 0;
382                 return (NULL);
383         }
384
385         if (mysql_real_connect (db->con, db->host, db->user, db->pass,
386                                 db->database, db->port, db->socket, 0) == NULL)
387         {
388                 ERROR ("mysql plugin: Failed to connect to database %s "
389                                 "at server %s: %s",
390                                 (db->database != NULL) ? db->database : "<none>",
391                                 (db->host != NULL) ? db->host : "localhost",
392                                 mysql_error (db->con));
393                 db->state = 0;
394                 return (NULL);
395         }
396         else
397         {
398                 INFO ("mysql plugin: Sucessfully connected to database %s "
399                                 "at server %s (server version: %s, protocol version: %d)",
400                                 (db->database != NULL) ? db->database : "<none>",
401                                 mysql_get_host_info (db->con),
402                                 mysql_get_server_info (db->con),
403                                 mysql_get_proto_info (db->con));
404                 db->state = 1;
405                 return (db->con);
406         }
407 } /* static MYSQL *getconnection (mysql_database_t *db) */
408
409 static void set_host (mysql_database_t *db, char *buf, size_t buflen)
410 {
411         /* XXX legacy mode - use hostname_g */
412         if (db->instance == NULL)
413                 sstrncpy (buf, hostname_g, buflen);
414         else
415         {
416                 if ((db->host == NULL)
417                                 || (strcmp ("", db->host) == 0)
418                                 || (strcmp ("localhost", db->host) == 0))
419                         sstrncpy (buf, hostname_g, buflen);
420                 else
421                         sstrncpy (buf, db->host, buflen);
422         }
423 }
424
425 static void set_plugin_instance (mysql_database_t *db,
426                 char *buf, size_t buflen)
427 {
428         /* XXX legacy mode - no plugin_instance */
429         if (db->instance == NULL)
430                 sstrncpy (buf, "", buflen);
431         else
432                 sstrncpy (buf, db->instance, buflen);
433 }
434
435 static void submit (const char *type, const char *type_instance,
436                 value_t *values, size_t values_len, mysql_database_t *db)
437 {
438         value_list_t vl = VALUE_LIST_INIT;
439
440         vl.values     = values;
441         vl.values_len = values_len;
442
443         set_host (db, vl.host, sizeof (vl.host));
444
445         sstrncpy (vl.plugin, "mysql", sizeof (vl.plugin));
446         set_plugin_instance (db, vl.plugin_instance, sizeof (vl.plugin_instance));
447
448         sstrncpy (vl.type, type, sizeof (vl.type));
449         if (type_instance != NULL)
450                 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
451
452         plugin_dispatch_values (&vl);
453 } /* submit */
454
455 static void counter_submit (const char *type, const char *type_instance,
456                 counter_t value, mysql_database_t *db)
457 {
458         value_t values[1];
459
460         values[0].counter = value;
461         submit (type, type_instance, values, STATIC_ARRAY_SIZE (values), db);
462 } /* void counter_submit */
463
464 static void gauge_submit (const char *type, const char *type_instance,
465                 gauge_t value, mysql_database_t *db)
466 {
467         value_t values[1];
468
469         values[0].gauge = value;
470         submit (type, type_instance, values, STATIC_ARRAY_SIZE (values), db);
471 } /* void gauge_submit */
472
473 static void qcache_submit (counter_t hits, counter_t inserts,
474                 counter_t not_cached, counter_t lowmem_prunes,
475                 gauge_t queries_in_cache, mysql_database_t *db)
476 {
477         value_t values[5];
478
479         values[0].counter = hits;
480         values[1].counter = inserts;
481         values[2].counter = not_cached;
482         values[3].counter = lowmem_prunes;
483         values[4].gauge   = queries_in_cache;
484
485         submit ("mysql_qcache", NULL, values, STATIC_ARRAY_SIZE (values), db);
486 } /* void qcache_submit */
487
488 static void threads_submit (gauge_t running, gauge_t connected, gauge_t cached,
489                 counter_t created, mysql_database_t *db)
490 {
491         value_t values[4];
492
493         values[0].gauge   = running;
494         values[1].gauge   = connected;
495         values[2].gauge   = cached;
496         values[3].counter = created;
497
498         submit ("mysql_threads", NULL, values, STATIC_ARRAY_SIZE (values), db);
499 } /* void threads_submit */
500
501 static void traffic_submit (counter_t rx, counter_t tx, mysql_database_t *db)
502 {
503         value_t values[2];
504
505         values[0].counter = rx;
506         values[1].counter = tx;
507
508         submit ("mysql_octets", NULL, values, STATIC_ARRAY_SIZE (values), db);
509 } /* void traffic_submit */
510
511 static MYSQL_RES *exec_query (MYSQL *con, const char *query)
512 {
513         MYSQL_RES *res;
514
515         int query_len = strlen (query);
516
517         if (mysql_real_query (con, query, query_len))
518         {
519                 ERROR ("mysql plugin: Failed to execute query: %s",
520                                 mysql_error (con));
521                 INFO ("mysql plugin: SQL query was: %s", query);
522                 return (NULL);
523         }
524
525         res = mysql_store_result (con);
526         if (res == NULL)
527         {
528                 ERROR ("mysql plugin: Failed to store query result: %s",
529                                 mysql_error (con));
530                 INFO ("mysql plugin: SQL query was: %s", query);
531                 return (NULL);
532         }
533
534         return (res);
535 } /* exec_query */
536
537 static int mysql_read_master_stats (mysql_database_t *db, MYSQL *con)
538 {
539         MYSQL_RES *res;
540         MYSQL_ROW  row;
541
542         char *query;
543         int   field_num;
544         unsigned long long position;
545
546         query = "SHOW MASTER STATUS";
547
548         res = exec_query (con, query);
549         if (res == NULL)
550                 return (-1);
551
552         row = mysql_fetch_row (res);
553         if (row == NULL)
554         {
555                 ERROR ("mysql plugin: Failed to get master statistics: "
556                                 "`%s' did not return any rows.", query);
557                 return (-1);
558         }
559
560         field_num = mysql_num_fields (res);
561         if (field_num < 2)
562         {
563                 ERROR ("mysql plugin: Failed to get master statistics: "
564                                 "`%s' returned less than two columns.", query);
565                 return (-1);
566         }
567
568         position = atoll (row[1]);
569         counter_submit ("mysql_log_position", "master-bin", position, db);
570
571         row = mysql_fetch_row (res);
572         if (row != NULL)
573                 WARNING ("mysql plugin: `%s' returned more than one row - "
574                                 "ignoring further results.", query);
575
576         mysql_free_result (res);
577
578         return (0);
579 } /* mysql_read_master_stats */
580
581 static int mysql_read_slave_stats (mysql_database_t *db, MYSQL *con)
582 {
583         MYSQL_RES *res;
584         MYSQL_ROW  row;
585
586         char *query;
587         int   field_num;
588
589         /* WTF? libmysqlclient does not seem to provide any means to
590          * translate a column name to a column index ... :-/ */
591         const int READ_MASTER_LOG_POS_IDX   = 6;
592         const int SLAVE_IO_RUNNING_IDX      = 10;
593         const int SLAVE_SQL_RUNNING_IDX     = 11;
594         const int EXEC_MASTER_LOG_POS_IDX   = 21;
595         const int SECONDS_BEHIND_MASTER_IDX = 32;
596
597         query = "SHOW SLAVE STATUS";
598
599         res = exec_query (con, query);
600         if (res == NULL)
601                 return (-1);
602
603         row = mysql_fetch_row (res);
604         if (row == NULL)
605         {
606                 ERROR ("mysql plugin: Failed to get slave statistics: "
607                                 "`%s' did not return any rows.", query);
608                 return (-1);
609         }
610
611         field_num = mysql_num_fields (res);
612         if (field_num < 33)
613         {
614                 ERROR ("mysql plugin: Failed to get slave statistics: "
615                                 "`%s' returned less than 33 columns.", query);
616                 return (-1);
617         }
618
619         if (db->slave_stats)
620         {
621                 unsigned long long counter;
622                 double gauge;
623
624                 counter = atoll (row[READ_MASTER_LOG_POS_IDX]);
625                 counter_submit ("mysql_log_position", "slave-read", counter, db);
626
627                 counter = atoll (row[EXEC_MASTER_LOG_POS_IDX]);
628                 counter_submit ("mysql_log_position", "slave-exec", counter, db);
629
630                 if (row[SECONDS_BEHIND_MASTER_IDX] != NULL)
631                 {
632                         gauge = atof (row[SECONDS_BEHIND_MASTER_IDX]);
633                         gauge_submit ("time_offset", NULL, gauge, db);
634                 }
635         }
636
637         if (db->slave_notif)
638         {
639                 notification_t n = { 0, time (NULL), "", "",
640                         "mysql", "", "time_offset", "", NULL };
641
642                 char *io, *sql;
643
644                 io  = row[SLAVE_IO_RUNNING_IDX];
645                 sql = row[SLAVE_SQL_RUNNING_IDX];
646
647                 set_host (db, n.host, sizeof (n.host));
648                 set_plugin_instance (db,
649                                 n.plugin_instance, sizeof (n.plugin_instance));
650
651                 if (((io == NULL) || (strcasecmp (io, "yes") != 0))
652                                 && (db->slave_io_running))
653                 {
654                         n.severity = NOTIF_WARNING;
655                         ssnprintf (n.message, sizeof (n.message),
656                                         "slave I/O thread not started or not connected to master");
657                         plugin_dispatch_notification (&n);
658                         db->slave_io_running = 0;
659                 }
660                 else if (((io != NULL) && (strcasecmp (io, "yes") == 0))
661                                 && (! db->slave_io_running))
662                 {
663                         n.severity = NOTIF_OKAY;
664                         ssnprintf (n.message, sizeof (n.message),
665                                         "slave I/O thread started and connected to master");
666                         plugin_dispatch_notification (&n);
667                         db->slave_io_running = 1;
668                 }
669
670                 if (((sql == NULL) || (strcasecmp (sql, "yes") != 0))
671                                 && (db->slave_sql_running))
672                 {
673                         n.severity = NOTIF_WARNING;
674                         ssnprintf (n.message, sizeof (n.message),
675                                         "slave SQL thread not started");
676                         plugin_dispatch_notification (&n);
677                         db->slave_sql_running = 0;
678                 }
679                 else if (((sql != NULL) && (strcasecmp (sql, "yes") == 0))
680                                 && (! db->slave_sql_running))
681                 {
682                         n.severity = NOTIF_OKAY;
683                         ssnprintf (n.message, sizeof (n.message),
684                                         "slave SQL thread started");
685                         plugin_dispatch_notification (&n);
686                         db->slave_sql_running = 0;
687                 }
688         }
689
690         row = mysql_fetch_row (res);
691         if (row != NULL)
692                 WARNING ("mysql plugin: `%s' returned more than one row - "
693                                 "ignoring further results.", query);
694
695         mysql_free_result (res);
696
697         return (0);
698 } /* mysql_read_slave_stats */
699
700 static int mysql_read (user_data_t *ud)
701 {
702         mysql_database_t *db;
703         MYSQL     *con;
704         MYSQL_RES *res;
705         MYSQL_ROW  row;
706         char      *query;
707         int        field_num;
708
709         unsigned long long qcache_hits          = 0ULL;
710         unsigned long long qcache_inserts       = 0ULL;
711         unsigned long long qcache_not_cached    = 0ULL;
712         unsigned long long qcache_lowmem_prunes = 0ULL;
713         int qcache_queries_in_cache = -1;
714
715         int threads_running   = -1;
716         int threads_connected = -1;
717         int threads_cached    = -1;
718         unsigned long long threads_created = 0ULL;
719
720         unsigned long long traffic_incoming = 0ULL;
721         unsigned long long traffic_outgoing = 0ULL;
722
723         if ((ud == NULL) || (ud->data == NULL))
724         {
725                 ERROR ("mysql plugin: mysql_database_read: Invalid user data.");
726                 return (-1);
727         }
728
729         db = (mysql_database_t *) ud->data;
730
731         /* An error message will have been printed in this case */
732         if ((con = getconnection (db)) == NULL)
733                 return (-1);
734
735         query = "SHOW STATUS";
736         if (mysql_get_server_version (con) >= 50002)
737                 query = "SHOW GLOBAL STATUS";
738
739         res = exec_query (con, query);
740         if (res == NULL)
741                 return (-1);
742
743         field_num = mysql_num_fields (res);
744         while ((row = mysql_fetch_row (res)))
745         {
746                 char *key;
747                 unsigned long long val;
748
749                 key = row[0];
750                 val = atoll (row[1]);
751
752                 if (strncmp (key, "Com_", 4) == 0)
753                 {
754                         if (val == 0ULL)
755                                 continue;
756
757                         /* Ignore `prepared statements' */
758                         if (strncmp (key, "Com_stmt_", 9) != 0)
759                                 counter_submit ("mysql_commands", key + 4, val, db);
760                 }
761                 else if (strncmp (key, "Handler_", 8) == 0)
762                 {
763                         if (val == 0ULL)
764                                 continue;
765
766                         counter_submit ("mysql_handler", key + 8, val, db);
767                 }
768                 else if (strncmp (key, "Qcache_", 7) == 0)
769                 {
770                         if (strcmp (key, "Qcache_hits") == 0)
771                                 qcache_hits = val;
772                         else if (strcmp (key, "Qcache_inserts") == 0)
773                                 qcache_inserts = val;
774                         else if (strcmp (key, "Qcache_not_cached") == 0)
775                                 qcache_not_cached = val;
776                         else if (strcmp (key, "Qcache_lowmem_prunes") == 0)
777                                 qcache_lowmem_prunes = val;
778                         else if (strcmp (key, "Qcache_queries_in_cache") == 0)
779                                 qcache_queries_in_cache = (int) val;
780                 }
781                 else if (strncmp (key, "Bytes_", 6) == 0)
782                 {
783                         if (strcmp (key, "Bytes_received") == 0)
784                                 traffic_incoming += val;
785                         else if (strcmp (key, "Bytes_sent") == 0)
786                                 traffic_outgoing += val;
787                 }
788                 else if (strncmp (key, "Threads_", 8) == 0)
789                 {
790                         if (strcmp (key, "Threads_running") == 0)
791                                 threads_running = (int) val;
792                         else if (strcmp (key, "Threads_connected") == 0)
793                                 threads_connected = (int) val;
794                         else if (strcmp (key, "Threads_cached") == 0)
795                                 threads_cached = (int) val;
796                         else if (strcmp (key, "Threads_created") == 0)
797                                 threads_created = val;
798                 }
799         }
800         mysql_free_result (res); res = NULL;
801
802         if ((qcache_hits != 0ULL)
803                         || (qcache_inserts != 0ULL)
804                         || (qcache_not_cached != 0ULL)
805                         || (qcache_lowmem_prunes != 0ULL))
806                 qcache_submit (qcache_hits, qcache_inserts, qcache_not_cached,
807                                qcache_lowmem_prunes, qcache_queries_in_cache, db);
808
809         if (threads_created != 0ULL)
810                 threads_submit (threads_running, threads_connected,
811                                 threads_cached, threads_created, db);
812
813         traffic_submit  (traffic_incoming, traffic_outgoing, db);
814
815         if (db->master_stats)
816                 mysql_read_master_stats (db, con);
817
818         if ((db->slave_stats) || (db->slave_notif))
819                 mysql_read_slave_stats (db, con);
820
821         return (0);
822 } /* int mysql_read */
823
824 void module_register (void)
825 {
826         plugin_register_complex_config ("mysql", mysql_config);
827 } /* void module_register */