Remove configfile.h from plugins
[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
35 #ifdef HAVE_MYSQL_H
36 #include <mysql.h>
37 #elif defined(HAVE_MYSQL_MYSQL_H)
38 #include <mysql/mysql.h>
39 #endif
40
41 struct mysql_database_s /* {{{ */
42 {
43         char *instance;
44         char *alias;
45         char *host;
46         char *user;
47         char *pass;
48         char *database;
49         char *socket;
50         int   port;
51         int   timeout;
52
53         _Bool master_stats;
54         _Bool slave_stats;
55         _Bool innodb_stats;
56         _Bool wsrep_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 if (strcasecmp ("WsrepStats", child->key) == 0)
182                         status = cf_util_get_boolean (child, &db->wsrep_stats);
183                 else
184                 {
185                         WARNING ("mysql plugin: Option `%s' not allowed here.", child->key);
186                         status = -1;
187                 }
188
189                 if (status != 0)
190                         break;
191         }
192
193         /* If all went well, register this database for reading */
194         if (status == 0)
195         {
196                 user_data_t ud = { 0 };
197                 char cb_name[DATA_MAX_NAME_LEN];
198
199                 DEBUG ("mysql plugin: Registering new read callback: %s",
200                                 (db->database != NULL) ? db->database : "<default>");
201
202                 ud.data = (void *) db;
203                 ud.free_func = mysql_database_free;
204
205                 if (db->instance != NULL)
206                         ssnprintf (cb_name, sizeof (cb_name), "mysql-%s",
207                                         db->instance);
208                 else
209                         sstrncpy (cb_name, "mysql", sizeof (cb_name));
210
211                 plugin_register_complex_read (/* group = */ NULL, cb_name,
212                                               mysql_read,
213                                               /* interval = */ 0, &ud);
214         }
215         else
216         {
217                 mysql_database_free (db);
218                 return (-1);
219         }
220
221         return (0);
222 } /* }}} int mysql_config_database */
223
224 static int mysql_config (oconfig_item_t *ci) /* {{{ */
225 {
226         if (ci == NULL)
227                 return (EINVAL);
228
229         /* Fill the `mysql_database_t' structure.. */
230         for (int i = 0; i < ci->children_num; i++)
231         {
232                 oconfig_item_t *child = ci->children + i;
233
234                 if (strcasecmp ("Database", child->key) == 0)
235                         mysql_config_database (child);
236                 else
237                         WARNING ("mysql plugin: Option \"%s\" not allowed here.",
238                                         child->key);
239         }
240
241         return (0);
242 } /* }}} int mysql_config */
243
244 /* }}} End of configuration handling functions */
245
246 static MYSQL *getconnection (mysql_database_t *db)
247 {
248         if (db->is_connected)
249         {
250                 int status;
251
252                 status = mysql_ping (db->con);
253                 if (status == 0)
254                         return (db->con);
255
256                 WARNING ("mysql plugin: Lost connection to instance \"%s\": %s",
257                                 db->instance, mysql_error (db->con));
258         }
259         db->is_connected = 0;
260
261         if (db->con == NULL)
262         {
263                 db->con = mysql_init (NULL);
264                 if (db->con == NULL)
265                 {
266                         ERROR ("mysql plugin: mysql_init failed: %s",
267                                         mysql_error (db->con));
268                         return (NULL);
269                 }
270         }
271
272         /* Configure TCP connect timeout (default: 0) */
273         db->con->options.connect_timeout = db->timeout;
274
275         if (mysql_real_connect (db->con, db->host, db->user, db->pass,
276                                 db->database, db->port, db->socket, 0) == NULL)
277         {
278                 ERROR ("mysql plugin: Failed to connect to database %s "
279                                 "at server %s: %s",
280                                 (db->database != NULL) ? db->database : "<none>",
281                                 (db->host != NULL) ? db->host : "localhost",
282                                 mysql_error (db->con));
283                 return (NULL);
284         }
285
286         INFO ("mysql plugin: Successfully connected to database %s "
287                         "at server %s (server version: %s, protocol version: %d)",
288                         (db->database != NULL) ? db->database : "<none>",
289                         mysql_get_host_info (db->con),
290                         mysql_get_server_info (db->con),
291                         mysql_get_proto_info (db->con));
292
293         db->is_connected = 1;
294         return (db->con);
295 } /* static MYSQL *getconnection (mysql_database_t *db) */
296
297 static void set_host (mysql_database_t *db, char *buf, size_t buflen)
298 {
299         if (db->alias)
300                 sstrncpy (buf, db->alias, buflen);
301         else if ((db->host == NULL)
302                         || (strcmp ("", db->host) == 0)
303                         || (strcmp ("127.0.0.1", db->host) == 0)
304                         || (strcmp ("localhost", db->host) == 0))
305                 sstrncpy (buf, hostname_g, buflen);
306         else
307                 sstrncpy (buf, db->host, buflen);
308 } /* void set_host */
309
310 static void submit (const char *type, const char *type_instance,
311                 value_t *values, size_t values_len, mysql_database_t *db)
312 {
313         value_list_t vl = VALUE_LIST_INIT;
314
315         vl.values     = values;
316         vl.values_len = values_len;
317
318         set_host (db, vl.host, sizeof (vl.host));
319
320         sstrncpy (vl.plugin, "mysql", sizeof (vl.plugin));
321
322         /* Assured by "mysql_config_database" */
323         assert (db->instance != NULL);
324         sstrncpy (vl.plugin_instance, db->instance, sizeof (vl.plugin_instance));
325
326         sstrncpy (vl.type, type, sizeof (vl.type));
327         if (type_instance != NULL)
328                 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
329
330         plugin_dispatch_values (&vl);
331 } /* submit */
332
333 static void counter_submit (const char *type, const char *type_instance,
334                 derive_t value, mysql_database_t *db)
335 {
336         value_t values[1];
337
338         values[0].derive = value;
339         submit (type, type_instance, values, STATIC_ARRAY_SIZE (values), db);
340 } /* void counter_submit */
341
342 static void gauge_submit (const char *type, const char *type_instance,
343                 gauge_t value, mysql_database_t *db)
344 {
345         value_t values[1];
346
347         values[0].gauge = value;
348         submit (type, type_instance, values, STATIC_ARRAY_SIZE (values), db);
349 } /* void gauge_submit */
350
351 static void derive_submit (const char *type, const char *type_instance,
352                 derive_t value, mysql_database_t *db)
353 {
354         value_t values[1];
355
356         values[0].derive = value;
357         submit (type, type_instance, values, STATIC_ARRAY_SIZE (values), db);
358 } /* void derive_submit */
359
360 static void traffic_submit (derive_t rx, derive_t tx, mysql_database_t *db)
361 {
362         value_t values[2];
363
364         values[0].derive = rx;
365         values[1].derive = tx;
366
367         submit ("mysql_octets", NULL, values, STATIC_ARRAY_SIZE (values), db);
368 } /* void traffic_submit */
369
370 static MYSQL_RES *exec_query (MYSQL *con, const char *query)
371 {
372         MYSQL_RES *res;
373
374         int query_len = strlen (query);
375
376         if (mysql_real_query (con, query, query_len))
377         {
378                 ERROR ("mysql plugin: Failed to execute query: %s",
379                                 mysql_error (con));
380                 INFO ("mysql plugin: SQL query was: %s", query);
381                 return (NULL);
382         }
383
384         res = mysql_store_result (con);
385         if (res == NULL)
386         {
387                 ERROR ("mysql plugin: Failed to store query result: %s",
388                                 mysql_error (con));
389                 INFO ("mysql plugin: SQL query was: %s", query);
390                 return (NULL);
391         }
392
393         return (res);
394 } /* exec_query */
395
396 static int mysql_read_master_stats (mysql_database_t *db, MYSQL *con)
397 {
398         MYSQL_RES *res;
399         MYSQL_ROW  row;
400
401         const char *query;
402         int         field_num;
403         unsigned long long position;
404
405         query = "SHOW MASTER STATUS";
406
407         res = exec_query (con, query);
408         if (res == NULL)
409                 return (-1);
410
411         row = mysql_fetch_row (res);
412         if (row == NULL)
413         {
414                 ERROR ("mysql plugin: Failed to get master statistics: "
415                                 "`%s' did not return any rows.", query);
416                 mysql_free_result (res);
417                 return (-1);
418         }
419
420         field_num = mysql_num_fields (res);
421         if (field_num < 2)
422         {
423                 ERROR ("mysql plugin: Failed to get master statistics: "
424                                 "`%s' returned less than two columns.", query);
425                 mysql_free_result (res);
426                 return (-1);
427         }
428
429         position = atoll (row[1]);
430         counter_submit ("mysql_log_position", "master-bin", position, db);
431
432         row = mysql_fetch_row (res);
433         if (row != NULL)
434                 WARNING ("mysql plugin: `%s' returned more than one row - "
435                                 "ignoring further results.", query);
436
437         mysql_free_result (res);
438
439         return (0);
440 } /* mysql_read_master_stats */
441
442 static int mysql_read_slave_stats (mysql_database_t *db, MYSQL *con)
443 {
444         MYSQL_RES *res;
445         MYSQL_ROW  row;
446
447         const char *query;
448         int         field_num;
449
450         /* WTF? libmysqlclient does not seem to provide any means to
451          * translate a column name to a column index ... :-/ */
452         const int READ_MASTER_LOG_POS_IDX   = 6;
453         const int SLAVE_IO_RUNNING_IDX      = 10;
454         const int SLAVE_SQL_RUNNING_IDX     = 11;
455         const int EXEC_MASTER_LOG_POS_IDX   = 21;
456         const int SECONDS_BEHIND_MASTER_IDX = 32;
457
458         query = "SHOW SLAVE STATUS";
459
460         res = exec_query (con, query);
461         if (res == NULL)
462                 return (-1);
463
464         row = mysql_fetch_row (res);
465         if (row == NULL)
466         {
467                 ERROR ("mysql plugin: Failed to get slave statistics: "
468                                 "`%s' did not return any rows.", query);
469                 mysql_free_result (res);
470                 return (-1);
471         }
472
473         field_num = mysql_num_fields (res);
474         if (field_num < 33)
475         {
476                 ERROR ("mysql plugin: Failed to get slave statistics: "
477                                 "`%s' returned less than 33 columns.", query);
478                 mysql_free_result (res);
479                 return (-1);
480         }
481
482         if (db->slave_stats)
483         {
484                 unsigned long long counter;
485                 double gauge;
486
487                 counter = atoll (row[READ_MASTER_LOG_POS_IDX]);
488                 counter_submit ("mysql_log_position", "slave-read", counter, db);
489
490                 counter = atoll (row[EXEC_MASTER_LOG_POS_IDX]);
491                 counter_submit ("mysql_log_position", "slave-exec", counter, db);
492
493                 if (row[SECONDS_BEHIND_MASTER_IDX] != NULL)
494                 {
495                         gauge = atof (row[SECONDS_BEHIND_MASTER_IDX]);
496                         gauge_submit ("time_offset", NULL, gauge, db);
497                 }
498         }
499
500         if (db->slave_notif)
501         {
502                 notification_t n = { 0, cdtime (), "", "",
503                         "mysql", "", "time_offset", "", NULL };
504
505                 char *io, *sql;
506
507                 io  = row[SLAVE_IO_RUNNING_IDX];
508                 sql = row[SLAVE_SQL_RUNNING_IDX];
509
510                 set_host (db, n.host, sizeof (n.host));
511
512                 /* Assured by "mysql_config_database" */
513                 assert (db->instance != NULL);
514                 sstrncpy (n.plugin_instance, db->instance, sizeof (n.plugin_instance));
515
516                 if (((io == NULL) || (strcasecmp (io, "yes") != 0))
517                                 && (db->slave_io_running))
518                 {
519                         n.severity = NOTIF_WARNING;
520                         ssnprintf (n.message, sizeof (n.message),
521                                         "slave I/O thread not started or not connected to master");
522                         plugin_dispatch_notification (&n);
523                         db->slave_io_running = 0;
524                 }
525                 else if (((io != NULL) && (strcasecmp (io, "yes") == 0))
526                                 && (! db->slave_io_running))
527                 {
528                         n.severity = NOTIF_OKAY;
529                         ssnprintf (n.message, sizeof (n.message),
530                                         "slave I/O thread started and connected to master");
531                         plugin_dispatch_notification (&n);
532                         db->slave_io_running = 1;
533                 }
534
535                 if (((sql == NULL) || (strcasecmp (sql, "yes") != 0))
536                                 && (db->slave_sql_running))
537                 {
538                         n.severity = NOTIF_WARNING;
539                         ssnprintf (n.message, sizeof (n.message),
540                                         "slave SQL thread not started");
541                         plugin_dispatch_notification (&n);
542                         db->slave_sql_running = 0;
543                 }
544                 else if (((sql != NULL) && (strcasecmp (sql, "yes") == 0))
545                                 && (! db->slave_sql_running))
546                 {
547                         n.severity = NOTIF_OKAY;
548                         ssnprintf (n.message, sizeof (n.message),
549                                         "slave SQL thread started");
550                         plugin_dispatch_notification (&n);
551                         db->slave_sql_running = 1;
552                 }
553         }
554
555         row = mysql_fetch_row (res);
556         if (row != NULL)
557                 WARNING ("mysql plugin: `%s' returned more than one row - "
558                                 "ignoring further results.", query);
559
560         mysql_free_result (res);
561
562         return (0);
563 } /* mysql_read_slave_stats */
564
565 static int mysql_read_innodb_stats (mysql_database_t *db, MYSQL *con)
566 {
567         MYSQL_RES *res;
568         MYSQL_ROW  row;
569
570         const char *query;
571         struct {
572                 const char *key;
573                 const char *type;
574                 int ds_type;
575         } metrics[] = {
576                 { "metadata_mem_pool_size",          "bytes",        DS_TYPE_GAUGE },
577                 { "lock_deadlocks",                  "mysql_locks",  DS_TYPE_DERIVE },
578                 { "lock_timeouts",                   "mysql_locks",  DS_TYPE_DERIVE },
579                 { "lock_row_lock_current_waits",     "mysql_locks",  DS_TYPE_DERIVE },
580                 { "buffer_pool_size",                "bytes",        DS_TYPE_GAUGE },
581
582                 { "os_log_bytes_written",            "operations",   DS_TYPE_DERIVE },
583                 { "os_log_pending_fsyncs",           "operations",   DS_TYPE_DERIVE },
584                 { "os_log_pending_writes",           "operations",   DS_TYPE_DERIVE },
585
586                 { "trx_rseg_history_len",            "gauge",        DS_TYPE_GAUGE },
587
588                 { "adaptive_hash_searches",          "operations",   DS_TYPE_DERIVE },
589
590                 { "file_num_open_files",             "gauge",        DS_TYPE_GAUGE },
591
592                 { "ibuf_merges_insert",              "operations",   DS_TYPE_DERIVE },
593                 { "ibuf_merges_delete_mark",         "operations",   DS_TYPE_DERIVE },
594                 { "ibuf_merges_delete",              "operations",   DS_TYPE_DERIVE },
595                 { "ibuf_merges_discard_insert",      "operations",   DS_TYPE_DERIVE },
596                 { "ibuf_merges_discard_delete_mark", "operations",   DS_TYPE_DERIVE },
597                 { "ibuf_merges_discard_delete",      "operations",   DS_TYPE_DERIVE },
598                 { "ibuf_merges_discard_merges",      "operations",   DS_TYPE_DERIVE },
599                 { "ibuf_size",                       "bytes",        DS_TYPE_GAUGE },
600
601                 { "innodb_activity_count",           "gauge",        DS_TYPE_GAUGE },
602
603                 { "innodb_rwlock_s_spin_waits",      "operations",   DS_TYPE_DERIVE },
604                 { "innodb_rwlock_x_spin_waits",      "operations",   DS_TYPE_DERIVE },
605                 { "innodb_rwlock_s_spin_rounds",     "operations",   DS_TYPE_DERIVE },
606                 { "innodb_rwlock_x_spin_rounds",     "operations",   DS_TYPE_DERIVE },
607                 { "innodb_rwlock_s_os_waits",        "operations",   DS_TYPE_DERIVE },
608                 { "innodb_rwlock_x_os_waits",        "operations",   DS_TYPE_DERIVE },
609
610                 { "dml_reads",                       "operations",   DS_TYPE_DERIVE },
611                 { "dml_inserts",                     "operations",   DS_TYPE_DERIVE },
612                 { "dml_deletes",                     "operations",   DS_TYPE_DERIVE },
613                 { "dml_updates",                     "operations",   DS_TYPE_DERIVE },
614
615                 { NULL,                              NULL,           0}
616         };
617
618         query = "SELECT name, count, type FROM information_schema.innodb_metrics WHERE status = 'enabled'";
619
620         res = exec_query (con, query);
621         if (res == NULL)
622                 return (-1);
623
624         while ((row = mysql_fetch_row (res)))
625         {
626                 int i;
627                 char *key;
628                 unsigned long long val;
629
630                 key = row[0];
631                 val = atoll (row[1]);
632
633                 for (i = 0; metrics[i].key != NULL && strcmp(metrics[i].key, key) != 0; i++)
634                         ;
635
636                 if (metrics[i].key == NULL)
637                         continue;
638
639                 switch (metrics[i].ds_type) {
640                         case DS_TYPE_COUNTER:
641                                 counter_submit(metrics[i].type, key, (counter_t)val, db);
642                                 break;
643                         case DS_TYPE_GAUGE:
644                                 gauge_submit(metrics[i].type, key, (gauge_t)val, db);
645                                 break;
646                         case DS_TYPE_DERIVE:
647                                 derive_submit(metrics[i].type, key, (derive_t)val, db);
648                                 break;
649                 }
650         }
651
652         mysql_free_result(res);
653         return (0);
654 }
655
656 static int mysql_read_wsrep_stats (mysql_database_t *db, MYSQL *con)
657 {
658         MYSQL_RES *res;
659         MYSQL_ROW  row;
660
661         const char *query;
662         struct {
663                 const char *key;
664                 const char *type;
665                 int ds_type;
666         } metrics[] = {
667
668                 { "wsrep_apply_oooe",                "operations",   DS_TYPE_DERIVE },
669                 { "wsrep_apply_oool",                "operations",   DS_TYPE_DERIVE },
670                 { "wsrep_causal_reads",              "operations",   DS_TYPE_DERIVE },
671                 { "wsrep_commit_oooe",               "operations",   DS_TYPE_DERIVE },
672                 { "wsrep_commit_oool",               "operations",   DS_TYPE_DERIVE },
673                 { "wsrep_flow_control_recv",         "operations",   DS_TYPE_DERIVE },
674                 { "wsrep_flow_control_sent",         "operations",   DS_TYPE_DERIVE },
675                 { "wsrep_flow_control_paused",       "operations",   DS_TYPE_DERIVE },
676                 { "wsrep_local_bf_aborts",           "operations",   DS_TYPE_DERIVE },
677                 { "wsrep_local_cert_failures",       "operations",   DS_TYPE_DERIVE },
678                 { "wsrep_local_commits",             "operations",   DS_TYPE_DERIVE },
679                 { "wsrep_local_replays",             "operations",   DS_TYPE_DERIVE },
680                 { "wsrep_received",                  "operations",   DS_TYPE_DERIVE },
681                 { "wsrep_replicated",                "operations",   DS_TYPE_DERIVE },
682
683                 { "wsrep_received_bytes",            "total_bytes",  DS_TYPE_DERIVE },
684                 { "wsrep_replicated_bytes",          "total_bytes",  DS_TYPE_DERIVE },
685
686                 { "wsrep_apply_window",              "gauge",        DS_TYPE_GAUGE },
687                 { "wsrep_commit_window",             "gauge",        DS_TYPE_GAUGE },
688
689                 { "wsrep_cluster_size",              "gauge",        DS_TYPE_GAUGE },
690                 { "wsrep_cert_deps_distance",        "gauge",        DS_TYPE_GAUGE },
691
692                 { "wsrep_local_recv_queue",          "queue_length", DS_TYPE_GAUGE },
693                 { "wsrep_local_send_queue",          "queue_length", DS_TYPE_GAUGE },
694
695                 { NULL,                              NULL,           0}
696
697         };
698
699         query = "SHOW GLOBAL STATUS LIKE 'wsrep_%'";
700
701         res = exec_query (con, query);
702         if (res == NULL)
703                 return (-1);
704
705         row = mysql_fetch_row (res);
706         if (row == NULL)
707         {
708                 ERROR ("mysql plugin: Failed to get wsrep statistics: "
709                         "`%s' did not return any rows.", query);
710                 mysql_free_result (res);
711                 return (-1);
712         }
713
714         while ((row = mysql_fetch_row (res)))
715         {
716                 int i;
717                 char *key;
718                 unsigned long long val;
719
720                 key = row[0];
721                 val = atoll (row[1]);
722
723                 for (i = 0; metrics[i].key != NULL && strcmp(metrics[i].key, key) != 0; i++)
724                         ;
725
726                 if (metrics[i].key == NULL)
727                         continue;
728
729                 switch (metrics[i].ds_type) {
730                         case DS_TYPE_GAUGE:
731                                 gauge_submit(metrics[i].type, key, (gauge_t)val, db);
732                                 break;
733                         case DS_TYPE_DERIVE:
734                                 derive_submit(metrics[i].type, key, (derive_t)val, db);
735                                 break;
736                 }
737         }
738
739         mysql_free_result(res);
740         return (0);
741 } /* mysql_read_wsrep_stats */
742
743 static int mysql_read (user_data_t *ud)
744 {
745         mysql_database_t *db;
746         MYSQL      *con;
747         MYSQL_RES  *res;
748         MYSQL_ROW   row;
749         const char *query;
750
751         derive_t qcache_hits          = 0;
752         derive_t qcache_inserts       = 0;
753         derive_t qcache_not_cached    = 0;
754         derive_t qcache_lowmem_prunes = 0;
755         gauge_t qcache_queries_in_cache = NAN;
756
757         gauge_t threads_running   = NAN;
758         gauge_t threads_connected = NAN;
759         gauge_t threads_cached    = NAN;
760         derive_t threads_created = 0;
761
762         unsigned long long traffic_incoming = 0ULL;
763         unsigned long long traffic_outgoing = 0ULL;
764         unsigned long mysql_version = 0ULL;
765
766         if ((ud == NULL) || (ud->data == NULL))
767         {
768                 ERROR ("mysql plugin: mysql_database_read: Invalid user data.");
769                 return (-1);
770         }
771
772         db = (mysql_database_t *) ud->data;
773
774         /* An error message will have been printed in this case */
775         if ((con = getconnection (db)) == NULL)
776                 return (-1);
777
778         mysql_version = mysql_get_server_version(con);
779
780         query = "SHOW STATUS";
781         if (mysql_version >= 50002)
782                 query = "SHOW GLOBAL STATUS";
783
784         res = exec_query (con, query);
785         if (res == NULL)
786                 return (-1);
787
788         while ((row = mysql_fetch_row (res)))
789         {
790                 char *key;
791                 unsigned long long val;
792
793                 key = row[0];
794                 val = atoll (row[1]);
795
796                 if (strncmp (key, "Com_",
797                                   strlen ("Com_")) == 0)
798                 {
799                         if (val == 0ULL)
800                                 continue;
801
802                         /* Ignore `prepared statements' */
803                         if (strncmp (key, "Com_stmt_", strlen ("Com_stmt_")) != 0)
804                                 counter_submit ("mysql_commands",
805                                                 key + strlen ("Com_"),
806                                                 val, db);
807                 }
808                 else if (strncmp (key, "Handler_",
809                                         strlen ("Handler_")) == 0)
810                 {
811                         if (val == 0ULL)
812                                 continue;
813
814                         counter_submit ("mysql_handler",
815                                         key + strlen ("Handler_"),
816                                         val, db);
817                 }
818                 else if (strncmp (key, "Qcache_",
819                                         strlen ("Qcache_")) == 0)
820                 {
821                         if (strcmp (key, "Qcache_hits") == 0)
822                                 qcache_hits = (derive_t) val;
823                         else if (strcmp (key, "Qcache_inserts") == 0)
824                                 qcache_inserts = (derive_t) val;
825                         else if (strcmp (key, "Qcache_not_cached") == 0)
826                                 qcache_not_cached = (derive_t) val;
827                         else if (strcmp (key, "Qcache_lowmem_prunes") == 0)
828                                 qcache_lowmem_prunes = (derive_t) val;
829                         else if (strcmp (key, "Qcache_queries_in_cache") == 0)
830                                 qcache_queries_in_cache = (gauge_t) val;
831                 }
832                 else if (strncmp (key, "Bytes_",
833                                         strlen ("Bytes_")) == 0)
834                 {
835                         if (strcmp (key, "Bytes_received") == 0)
836                                 traffic_incoming += val;
837                         else if (strcmp (key, "Bytes_sent") == 0)
838                                 traffic_outgoing += val;
839                 }
840                 else if (strncmp (key, "Threads_",
841                                         strlen ("Threads_")) == 0)
842                 {
843                         if (strcmp (key, "Threads_running") == 0)
844                                 threads_running = (gauge_t) val;
845                         else if (strcmp (key, "Threads_connected") == 0)
846                                 threads_connected = (gauge_t) val;
847                         else if (strcmp (key, "Threads_cached") == 0)
848                                 threads_cached = (gauge_t) val;
849                         else if (strcmp (key, "Threads_created") == 0)
850                                 threads_created = (derive_t) val;
851                 }
852                 else if (strncmp (key, "Table_locks_",
853                                         strlen ("Table_locks_")) == 0)
854                 {
855                         counter_submit ("mysql_locks",
856                                         key + strlen ("Table_locks_"),
857                                         val, db);
858                 }
859                 else if (db->innodb_stats && strncmp (key, "Innodb_", strlen ("Innodb_")) == 0)
860                 {
861                         /* buffer pool */
862                         if (strcmp (key, "Innodb_buffer_pool_pages_data") == 0)
863                                 gauge_submit ("mysql_bpool_pages", "data", val, db);
864                         else if (strcmp (key, "Innodb_buffer_pool_pages_dirty") == 0)
865                                 gauge_submit ("mysql_bpool_pages", "dirty", val, db);
866                         else if (strcmp (key, "Innodb_buffer_pool_pages_flushed") == 0)
867                                 counter_submit ("mysql_bpool_counters", "pages_flushed", val, db);
868                         else if (strcmp (key, "Innodb_buffer_pool_pages_free") == 0)
869                                 gauge_submit ("mysql_bpool_pages", "free", val, db);
870                         else if (strcmp (key, "Innodb_buffer_pool_pages_misc") == 0)
871                                 gauge_submit ("mysql_bpool_pages", "misc", val, db);
872                         else if (strcmp (key, "Innodb_buffer_pool_pages_total") == 0)
873                                 gauge_submit ("mysql_bpool_pages", "total", val, db);
874                         else if (strcmp (key, "Innodb_buffer_pool_read_ahead_rnd") == 0)
875                                 counter_submit ("mysql_bpool_counters", "read_ahead_rnd", val, db);
876                         else if (strcmp (key, "Innodb_buffer_pool_read_ahead") == 0)
877                                 counter_submit ("mysql_bpool_counters", "read_ahead", val, db);
878                         else if (strcmp (key, "Innodb_buffer_pool_read_ahead_evicted") == 0)
879                                 counter_submit ("mysql_bpool_counters", "read_ahead_evicted", val, db);
880                         else if (strcmp (key, "Innodb_buffer_pool_read_requests") == 0)
881                                 counter_submit ("mysql_bpool_counters", "read_requests", val, db);
882                         else if (strcmp (key, "Innodb_buffer_pool_reads") == 0)
883                                 counter_submit ("mysql_bpool_counters", "reads", val, db);
884                         else if (strcmp (key, "Innodb_buffer_pool_wait_free") == 0)
885                                 counter_submit ("mysql_bpool_counters", "wait_free", val, db);
886                         else if (strcmp (key, "Innodb_buffer_pool_write_requests") == 0)
887                                 counter_submit ("mysql_bpool_counters", "write_requests", val, db);
888                         else if (strcmp (key, "Innodb_buffer_pool_bytes_data") == 0)
889                                 gauge_submit ("mysql_bpool_bytes", "data", val, db);
890                         else if (strcmp (key, "Innodb_buffer_pool_bytes_dirty") == 0)
891                                 gauge_submit ("mysql_bpool_bytes", "dirty", val, db);
892
893                         /* data */
894                         if (strcmp (key, "Innodb_data_fsyncs") == 0)
895                                 counter_submit ("mysql_innodb_data", "fsyncs", val, db);
896                         else if (strcmp (key, "Innodb_data_read") == 0)
897                                 counter_submit ("mysql_innodb_data", "read", val, db);
898                         else if (strcmp (key, "Innodb_data_reads") == 0)
899                                 counter_submit ("mysql_innodb_data", "reads", val, db);
900                         else if (strcmp (key, "Innodb_data_writes") == 0)
901                                 counter_submit ("mysql_innodb_data", "writes", val, db);
902                         else if (strcmp (key, "Innodb_data_written") == 0)
903                                 counter_submit ("mysql_innodb_data", "written", val, db);
904
905                         /* double write */
906                         else if (strcmp (key, "Innodb_dblwr_writes") == 0)
907                                 counter_submit ("mysql_innodb_dblwr", "writes", val, db);
908                         else if (strcmp (key, "Innodb_dblwr_pages_written") == 0)
909                                 counter_submit ("mysql_innodb_dblwr", "written", val, db);
910                         else if (strcmp (key, "Innodb_dblwr_page_size") == 0)
911                                 gauge_submit ("mysql_innodb_dblwr", "page_size", val, db);
912
913                         /* log */
914                         else if (strcmp (key, "Innodb_log_waits") == 0)
915                                 counter_submit ("mysql_innodb_log", "waits", val, db);
916                         else if (strcmp (key, "Innodb_log_write_requests") == 0)
917                                 counter_submit ("mysql_innodb_log", "write_requests", val, db);
918                         else if (strcmp (key, "Innodb_log_writes") == 0)
919                                 counter_submit ("mysql_innodb_log", "writes", val, db);
920                         else if (strcmp (key, "Innodb_os_log_fsyncs") == 0)
921                                 counter_submit ("mysql_innodb_log", "fsyncs", val, db);
922                         else if (strcmp (key, "Innodb_os_log_written") == 0)
923                                 counter_submit ("mysql_innodb_log", "written", val, db);
924
925                         /* pages */
926                         else if (strcmp (key, "Innodb_pages_created") == 0)
927                                 counter_submit ("mysql_innodb_pages", "created", val, db);
928                         else if (strcmp (key, "Innodb_pages_read") == 0)
929                                 counter_submit ("mysql_innodb_pages", "read", val, db);
930                         else if (strcmp (key, "Innodb_pages_written") == 0)
931                                 counter_submit ("mysql_innodb_pages", "written", val, db);
932
933                         /* row lock */
934                         else if (strcmp (key, "Innodb_row_lock_time") == 0)
935                                 counter_submit ("mysql_innodb_row_lock", "time", val, db);
936                         else if (strcmp (key, "Innodb_row_lock_waits") == 0)
937                                 counter_submit ("mysql_innodb_row_lock", "waits", val, db);
938
939                         /* rows */
940                         else if (strcmp (key, "Innodb_rows_deleted") == 0)
941                                 counter_submit ("mysql_innodb_rows", "deleted", val, db);
942                         else if (strcmp (key, "Innodb_rows_inserted") == 0)
943                                 counter_submit ("mysql_innodb_rows", "inserted", val, db);
944                         else if (strcmp (key, "Innodb_rows_read") == 0)
945                                 counter_submit ("mysql_innodb_rows", "read", val, db);
946                         else if (strcmp (key, "Innodb_rows_updated") == 0)
947                                 counter_submit ("mysql_innodb_rows", "updated", val, db);
948                 }
949                 else if (strncmp (key, "Select_", strlen ("Select_")) == 0)
950                 {
951                         counter_submit ("mysql_select", key + strlen ("Select_"),
952                                         val, db);
953                 }
954                 else if (strncmp (key, "Sort_", strlen ("Sort_")) == 0)
955                 {
956                         if (strcmp (key, "Sort_merge_passes") == 0)
957                                 counter_submit ("mysql_sort_merge_passes", NULL, val, db);
958                         else if (strcmp (key, "Sort_rows") == 0)
959                                 counter_submit ("mysql_sort_rows", NULL, val, db);
960                         else if (strcmp (key, "Sort_range") == 0)
961                                 counter_submit ("mysql_sort", "range", val, db);
962                         else if (strcmp (key, "Sort_scan") == 0)
963                                 counter_submit ("mysql_sort", "scan", val, db);
964
965                 }
966                 else if (strncmp (key, "Slow_queries", strlen ("Slow_queries")) == 0) 
967                 {
968                         counter_submit ("mysql_slow_queries", NULL , val, db);
969                 }
970         }
971         mysql_free_result (res); res = NULL;
972
973         if ((qcache_hits != 0)
974                         || (qcache_inserts != 0)
975                         || (qcache_not_cached != 0)
976                         || (qcache_lowmem_prunes != 0))
977         {
978                 derive_submit ("cache_result", "qcache-hits",
979                                 qcache_hits, db);
980                 derive_submit ("cache_result", "qcache-inserts",
981                                 qcache_inserts, db);
982                 derive_submit ("cache_result", "qcache-not_cached",
983                                 qcache_not_cached, db);
984                 derive_submit ("cache_result", "qcache-prunes",
985                                 qcache_lowmem_prunes, db);
986
987                 gauge_submit ("cache_size", "qcache",
988                                 qcache_queries_in_cache, db);
989         }
990
991         if (threads_created != 0)
992         {
993                 gauge_submit ("threads", "running",
994                                 threads_running, db);
995                 gauge_submit ("threads", "connected",
996                                 threads_connected, db);
997                 gauge_submit ("threads", "cached",
998                                 threads_cached, db);
999
1000                 derive_submit ("total_threads", "created",
1001                                 threads_created, db);
1002         }
1003
1004         traffic_submit  (traffic_incoming, traffic_outgoing, db);
1005
1006         if (mysql_version >= 50600 && db->innodb_stats)
1007                 mysql_read_innodb_stats (db, con);
1008
1009         if (db->master_stats)
1010                 mysql_read_master_stats (db, con);
1011
1012         if ((db->slave_stats) || (db->slave_notif))
1013                 mysql_read_slave_stats (db, con);
1014
1015         if (db->wsrep_stats)
1016                 mysql_read_wsrep_stats (db, con);
1017
1018         return (0);
1019 } /* int mysql_read */
1020
1021 void module_register (void)
1022 {
1023         plugin_register_complex_config ("mysql", mysql_config);
1024 } /* void module_register */