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