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 **/
30 #include "collectd.h"
32 #include "common.h"
33 #include "plugin.h"
35 #ifdef HAVE_MYSQL_H
36 #include <mysql.h>
37 #elif defined(HAVE_MYSQL_MYSQL_H)
38 #include <mysql/mysql.h>
39 #endif
41 struct mysql_database_s /* {{{ */
42 {
43 char *instance;
44 char *alias;
45 char *host;
46 char *user;
47 char *pass;
48 char *database;
50 /* mysql_ssl_set params */
51 char *key;
52 char *cert;
53 char *ca;
54 char *capath;
55 char *cipher;
57 char *socket;
58 int port;
59 int timeout;
61 _Bool master_stats;
62 _Bool slave_stats;
63 _Bool innodb_stats;
64 _Bool wsrep_stats;
66 _Bool slave_notif;
67 _Bool slave_io_running;
68 _Bool slave_sql_running;
70 MYSQL *con;
71 _Bool is_connected;
72 };
73 typedef struct mysql_database_s mysql_database_t; /* }}} */
75 static int mysql_read (user_data_t *ud);
77 static void mysql_database_free (void *arg) /* {{{ */
78 {
79 mysql_database_t *db;
81 DEBUG ("mysql plugin: mysql_database_free (arg = %p);", arg);
83 db = arg;
85 if (db == NULL)
86 return;
88 if (db->con != NULL)
89 mysql_close (db->con);
91 sfree (db->alias);
92 sfree (db->host);
93 sfree (db->user);
94 sfree (db->pass);
95 sfree (db->socket);
96 sfree (db->instance);
97 sfree (db->database);
98 sfree (db->key);
99 sfree (db->cert);
100 sfree (db->ca);
101 sfree (db->capath);
102 sfree (db->cipher);
103 sfree (db);
104 } /* }}} void mysql_database_free */
106 /* Configuration handling functions {{{
107 *
108 * <Plugin mysql>
109 * <Database "plugin_instance1">
110 * Host "localhost"
111 * Port 22000
112 * ...
113 * </Database>
114 * </Plugin>
115 */
116 static int mysql_config_database (oconfig_item_t *ci) /* {{{ */
117 {
118 mysql_database_t *db;
119 int status = 0;
121 if ((ci->values_num != 1)
122 || (ci->values[0].type != OCONFIG_TYPE_STRING))
123 {
124 WARNING ("mysql plugin: The `Database' block "
125 "needs exactly one string argument.");
126 return (-1);
127 }
129 db = calloc (1, sizeof (*db));
130 if (db == NULL)
131 {
132 ERROR ("mysql plugin: calloc failed.");
133 return (-1);
134 }
136 /* initialize all the pointers */
137 db->alias = NULL;
138 db->host = NULL;
139 db->user = NULL;
140 db->pass = NULL;
141 db->database = NULL;
142 db->key = NULL;
143 db->cert = NULL;
144 db->ca = NULL;
145 db->capath = NULL;
146 db->cipher = NULL;
148 db->socket = NULL;
149 db->con = NULL;
150 db->timeout = 0;
152 /* trigger a notification, if it's not running */
153 db->slave_io_running = 1;
154 db->slave_sql_running = 1;
156 status = cf_util_get_string (ci, &db->instance);
157 if (status != 0)
158 {
159 sfree (db);
160 return (status);
161 }
162 assert (db->instance != NULL);
164 /* Fill the `mysql_database_t' structure.. */
165 for (int i = 0; i < ci->children_num; i++)
166 {
167 oconfig_item_t *child = ci->children + i;
169 if (strcasecmp ("Alias", child->key) == 0)
170 status = cf_util_get_string (child, &db->alias);
171 else if (strcasecmp ("Host", child->key) == 0)
172 status = cf_util_get_string (child, &db->host);
173 else if (strcasecmp ("User", child->key) == 0)
174 status = cf_util_get_string (child, &db->user);
175 else if (strcasecmp ("Password", child->key) == 0)
176 status = cf_util_get_string (child, &db->pass);
177 else if (strcasecmp ("Port", child->key) == 0)
178 {
179 status = cf_util_get_port_number (child);
180 if (status > 0)
181 {
182 db->port = status;
183 status = 0;
184 }
185 }
186 else if (strcasecmp ("Socket", child->key) == 0)
187 status = cf_util_get_string (child, &db->socket);
188 else if (strcasecmp ("Database", child->key) == 0)
189 status = cf_util_get_string (child, &db->database);
190 else if (strcasecmp ("SSLKey", child->key) == 0)
191 status = cf_util_get_string (child, &db->key);
192 else if (strcasecmp ("SSLCert", child->key) == 0)
193 status = cf_util_get_string (child, &db->cert);
194 else if (strcasecmp ("SSLCA", child->key) == 0)
195 status = cf_util_get_string (child, &db->ca);
196 else if (strcasecmp ("SSLCAPath", child->key) == 0)
197 status = cf_util_get_string (child, &db->capath);
198 else if (strcasecmp ("SSLCipher", child->key) == 0)
199 status = cf_util_get_string (child, &db->cipher);
200 else if (strcasecmp ("ConnectTimeout", child->key) == 0)
201 status = cf_util_get_int (child, &db->timeout);
202 else if (strcasecmp ("MasterStats", child->key) == 0)
203 status = cf_util_get_boolean (child, &db->master_stats);
204 else if (strcasecmp ("SlaveStats", child->key) == 0)
205 status = cf_util_get_boolean (child, &db->slave_stats);
206 else if (strcasecmp ("SlaveNotifications", child->key) == 0)
207 status = cf_util_get_boolean (child, &db->slave_notif);
208 else if (strcasecmp ("InnodbStats", child->key) == 0)
209 status = cf_util_get_boolean (child, &db->innodb_stats);
210 else if (strcasecmp ("WsrepStats", child->key) == 0)
211 status = cf_util_get_boolean (child, &db->wsrep_stats);
212 else
213 {
214 WARNING ("mysql plugin: Option `%s' not allowed here.", child->key);
215 status = -1;
216 }
218 if (status != 0)
219 break;
220 }
222 /* If all went well, register this database for reading */
223 if (status == 0)
224 {
225 char cb_name[DATA_MAX_NAME_LEN];
227 DEBUG ("mysql plugin: Registering new read callback: %s",
228 (db->database != NULL) ? db->database : "<default>");
230 if (db->instance != NULL)
231 ssnprintf (cb_name, sizeof (cb_name), "mysql-%s",
232 db->instance);
233 else
234 sstrncpy (cb_name, "mysql", sizeof (cb_name));
236 user_data_t ud = {
237 .data = db,
238 .free_func = mysql_database_free
239 };
241 plugin_register_complex_read (/* group = */ NULL, cb_name,
242 mysql_read,
243 /* interval = */ 0, &ud);
244 }
245 else
246 {
247 mysql_database_free (db);
248 return (-1);
249 }
251 return (0);
252 } /* }}} int mysql_config_database */
254 static int mysql_config (oconfig_item_t *ci) /* {{{ */
255 {
256 if (ci == NULL)
257 return (EINVAL);
259 /* Fill the `mysql_database_t' structure.. */
260 for (int i = 0; i < ci->children_num; i++)
261 {
262 oconfig_item_t *child = ci->children + i;
264 if (strcasecmp ("Database", child->key) == 0)
265 mysql_config_database (child);
266 else
267 WARNING ("mysql plugin: Option \"%s\" not allowed here.",
268 child->key);
269 }
271 return (0);
272 } /* }}} int mysql_config */
274 /* }}} End of configuration handling functions */
276 static MYSQL *getconnection (mysql_database_t *db)
277 {
278 const char *cipher;
280 if (db->is_connected)
281 {
282 int status;
284 status = mysql_ping (db->con);
285 if (status == 0)
286 return (db->con);
288 WARNING ("mysql plugin: Lost connection to instance \"%s\": %s",
289 db->instance, mysql_error (db->con));
290 }
291 db->is_connected = 0;
293 if (db->con == NULL)
294 {
295 db->con = mysql_init (NULL);
296 if (db->con == NULL)
297 {
298 ERROR ("mysql plugin: mysql_init failed: %s",
299 mysql_error (db->con));
300 return (NULL);
301 }
302 }
304 /* Configure TCP connect timeout (default: 0) */
305 db->con->options.connect_timeout = db->timeout;
307 mysql_ssl_set (db->con, db->key, db->cert, db->ca, db->capath, db->cipher);
309 if (mysql_real_connect (db->con, db->host, db->user, db->pass,
310 db->database, db->port, db->socket, 0) == NULL)
311 {
312 ERROR ("mysql plugin: Failed to connect to database %s "
313 "at server %s: %s",
314 (db->database != NULL) ? db->database : "<none>",
315 (db->host != NULL) ? db->host : "localhost",
316 mysql_error (db->con));
317 return (NULL);
318 }
320 cipher = mysql_get_ssl_cipher (db->con);
322 INFO ("mysql plugin: Successfully connected to database %s "
323 "at server %s with cipher %s "
324 "(server version: %s, protocol version: %d) ",
325 (db->database != NULL) ? db->database : "<none>",
326 mysql_get_host_info (db->con),
327 (cipher != NULL) ? cipher : "<none>",
328 mysql_get_server_info (db->con),
329 mysql_get_proto_info (db->con));
331 db->is_connected = 1;
332 return (db->con);
333 } /* static MYSQL *getconnection (mysql_database_t *db) */
335 static void set_host (mysql_database_t *db, char *buf, size_t buflen)
336 {
337 if (db->alias)
338 sstrncpy (buf, db->alias, buflen);
339 else if ((db->host == NULL)
340 || (strcmp ("", db->host) == 0)
341 || (strcmp ("127.0.0.1", db->host) == 0)
342 || (strcmp ("localhost", db->host) == 0))
343 sstrncpy (buf, hostname_g, buflen);
344 else
345 sstrncpy (buf, db->host, buflen);
346 } /* void set_host */
348 static void submit (const char *type, const char *type_instance,
349 value_t *values, size_t values_len, mysql_database_t *db)
350 {
351 value_list_t vl = VALUE_LIST_INIT;
353 vl.values = values;
354 vl.values_len = values_len;
356 set_host (db, vl.host, sizeof (vl.host));
358 sstrncpy (vl.plugin, "mysql", sizeof (vl.plugin));
360 /* Assured by "mysql_config_database" */
361 assert (db->instance != NULL);
362 sstrncpy (vl.plugin_instance, db->instance, sizeof (vl.plugin_instance));
364 sstrncpy (vl.type, type, sizeof (vl.type));
365 if (type_instance != NULL)
366 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
368 plugin_dispatch_values (&vl);
369 } /* submit */
371 static void counter_submit (const char *type, const char *type_instance,
372 derive_t value, mysql_database_t *db)
373 {
374 value_t values[1];
376 values[0].derive = value;
377 submit (type, type_instance, values, STATIC_ARRAY_SIZE (values), db);
378 } /* void counter_submit */
380 static void gauge_submit (const char *type, const char *type_instance,
381 gauge_t value, mysql_database_t *db)
382 {
383 value_t values[1];
385 values[0].gauge = value;
386 submit (type, type_instance, values, STATIC_ARRAY_SIZE (values), db);
387 } /* void gauge_submit */
389 static void derive_submit (const char *type, const char *type_instance,
390 derive_t value, mysql_database_t *db)
391 {
392 value_t values[1];
394 values[0].derive = value;
395 submit (type, type_instance, values, STATIC_ARRAY_SIZE (values), db);
396 } /* void derive_submit */
398 static void traffic_submit (derive_t rx, derive_t tx, mysql_database_t *db)
399 {
400 value_t values[2];
402 values[0].derive = rx;
403 values[1].derive = tx;
405 submit ("mysql_octets", NULL, values, STATIC_ARRAY_SIZE (values), db);
406 } /* void traffic_submit */
408 static MYSQL_RES *exec_query (MYSQL *con, const char *query)
409 {
410 MYSQL_RES *res;
412 int query_len = strlen (query);
414 if (mysql_real_query (con, query, query_len))
415 {
416 ERROR ("mysql plugin: Failed to execute query: %s",
417 mysql_error (con));
418 INFO ("mysql plugin: SQL query was: %s", query);
419 return (NULL);
420 }
422 res = mysql_store_result (con);
423 if (res == NULL)
424 {
425 ERROR ("mysql plugin: Failed to store query result: %s",
426 mysql_error (con));
427 INFO ("mysql plugin: SQL query was: %s", query);
428 return (NULL);
429 }
431 return (res);
432 } /* exec_query */
434 static int mysql_read_master_stats (mysql_database_t *db, MYSQL *con)
435 {
436 MYSQL_RES *res;
437 MYSQL_ROW row;
439 const char *query;
440 int field_num;
441 unsigned long long position;
443 query = "SHOW MASTER STATUS";
445 res = exec_query (con, query);
446 if (res == NULL)
447 return (-1);
449 row = mysql_fetch_row (res);
450 if (row == NULL)
451 {
452 ERROR ("mysql plugin: Failed to get master statistics: "
453 "`%s' did not return any rows.", query);
454 mysql_free_result (res);
455 return (-1);
456 }
458 field_num = mysql_num_fields (res);
459 if (field_num < 2)
460 {
461 ERROR ("mysql plugin: Failed to get master statistics: "
462 "`%s' returned less than two columns.", query);
463 mysql_free_result (res);
464 return (-1);
465 }
467 position = atoll (row[1]);
468 counter_submit ("mysql_log_position", "master-bin", position, db);
470 row = mysql_fetch_row (res);
471 if (row != NULL)
472 WARNING ("mysql plugin: `%s' returned more than one row - "
473 "ignoring further results.", query);
475 mysql_free_result (res);
477 return (0);
478 } /* mysql_read_master_stats */
480 static int mysql_read_slave_stats (mysql_database_t *db, MYSQL *con)
481 {
482 MYSQL_RES *res;
483 MYSQL_ROW row;
485 const char *query;
486 int field_num;
488 /* WTF? libmysqlclient does not seem to provide any means to
489 * translate a column name to a column index ... :-/ */
490 const int READ_MASTER_LOG_POS_IDX = 6;
491 const int SLAVE_IO_RUNNING_IDX = 10;
492 const int SLAVE_SQL_RUNNING_IDX = 11;
493 const int EXEC_MASTER_LOG_POS_IDX = 21;
494 const int SECONDS_BEHIND_MASTER_IDX = 32;
496 query = "SHOW SLAVE STATUS";
498 res = exec_query (con, query);
499 if (res == NULL)
500 return (-1);
502 row = mysql_fetch_row (res);
503 if (row == NULL)
504 {
505 ERROR ("mysql plugin: Failed to get slave statistics: "
506 "`%s' did not return any rows.", query);
507 mysql_free_result (res);
508 return (-1);
509 }
511 field_num = mysql_num_fields (res);
512 if (field_num < 33)
513 {
514 ERROR ("mysql plugin: Failed to get slave statistics: "
515 "`%s' returned less than 33 columns.", query);
516 mysql_free_result (res);
517 return (-1);
518 }
520 if (db->slave_stats)
521 {
522 unsigned long long counter;
523 double gauge;
525 counter = atoll (row[READ_MASTER_LOG_POS_IDX]);
526 counter_submit ("mysql_log_position", "slave-read", counter, db);
528 counter = atoll (row[EXEC_MASTER_LOG_POS_IDX]);
529 counter_submit ("mysql_log_position", "slave-exec", counter, db);
531 if (row[SECONDS_BEHIND_MASTER_IDX] != NULL)
532 {
533 gauge = atof (row[SECONDS_BEHIND_MASTER_IDX]);
534 gauge_submit ("time_offset", NULL, gauge, db);
535 }
536 }
538 if (db->slave_notif)
539 {
540 notification_t n = { 0, cdtime (), "", "",
541 "mysql", "", "time_offset", "", NULL };
543 char *io, *sql;
545 io = row[SLAVE_IO_RUNNING_IDX];
546 sql = row[SLAVE_SQL_RUNNING_IDX];
548 set_host (db, n.host, sizeof (n.host));
550 /* Assured by "mysql_config_database" */
551 assert (db->instance != NULL);
552 sstrncpy (n.plugin_instance, db->instance, sizeof (n.plugin_instance));
554 if (((io == NULL) || (strcasecmp (io, "yes") != 0))
555 && (db->slave_io_running))
556 {
557 n.severity = NOTIF_WARNING;
558 ssnprintf (n.message, sizeof (n.message),
559 "slave I/O thread not started or not connected to master");
560 plugin_dispatch_notification (&n);
561 db->slave_io_running = 0;
562 }
563 else if (((io != NULL) && (strcasecmp (io, "yes") == 0))
564 && (! db->slave_io_running))
565 {
566 n.severity = NOTIF_OKAY;
567 ssnprintf (n.message, sizeof (n.message),
568 "slave I/O thread started and connected to master");
569 plugin_dispatch_notification (&n);
570 db->slave_io_running = 1;
571 }
573 if (((sql == NULL) || (strcasecmp (sql, "yes") != 0))
574 && (db->slave_sql_running))
575 {
576 n.severity = NOTIF_WARNING;
577 ssnprintf (n.message, sizeof (n.message),
578 "slave SQL thread not started");
579 plugin_dispatch_notification (&n);
580 db->slave_sql_running = 0;
581 }
582 else if (((sql != NULL) && (strcasecmp (sql, "yes") == 0))
583 && (! db->slave_sql_running))
584 {
585 n.severity = NOTIF_OKAY;
586 ssnprintf (n.message, sizeof (n.message),
587 "slave SQL thread started");
588 plugin_dispatch_notification (&n);
589 db->slave_sql_running = 1;
590 }
591 }
593 row = mysql_fetch_row (res);
594 if (row != NULL)
595 WARNING ("mysql plugin: `%s' returned more than one row - "
596 "ignoring further results.", query);
598 mysql_free_result (res);
600 return (0);
601 } /* mysql_read_slave_stats */
603 static int mysql_read_innodb_stats (mysql_database_t *db, MYSQL *con)
604 {
605 MYSQL_RES *res;
606 MYSQL_ROW row;
608 const char *query;
609 struct {
610 const char *key;
611 const char *type;
612 int ds_type;
613 } metrics[] = {
614 { "metadata_mem_pool_size", "bytes", DS_TYPE_GAUGE },
615 { "lock_deadlocks", "mysql_locks", DS_TYPE_DERIVE },
616 { "lock_timeouts", "mysql_locks", DS_TYPE_DERIVE },
617 { "lock_row_lock_current_waits", "mysql_locks", DS_TYPE_DERIVE },
618 { "buffer_pool_size", "bytes", DS_TYPE_GAUGE },
620 { "os_log_bytes_written", "operations", DS_TYPE_DERIVE },
621 { "os_log_pending_fsyncs", "operations", DS_TYPE_DERIVE },
622 { "os_log_pending_writes", "operations", DS_TYPE_DERIVE },
624 { "trx_rseg_history_len", "gauge", DS_TYPE_GAUGE },
626 { "adaptive_hash_searches", "operations", DS_TYPE_DERIVE },
628 { "file_num_open_files", "gauge", DS_TYPE_GAUGE },
630 { "ibuf_merges_insert", "operations", DS_TYPE_DERIVE },
631 { "ibuf_merges_delete_mark", "operations", DS_TYPE_DERIVE },
632 { "ibuf_merges_delete", "operations", DS_TYPE_DERIVE },
633 { "ibuf_merges_discard_insert", "operations", DS_TYPE_DERIVE },
634 { "ibuf_merges_discard_delete_mark", "operations", DS_TYPE_DERIVE },
635 { "ibuf_merges_discard_delete", "operations", DS_TYPE_DERIVE },
636 { "ibuf_merges_discard_merges", "operations", DS_TYPE_DERIVE },
637 { "ibuf_size", "bytes", DS_TYPE_GAUGE },
639 { "innodb_activity_count", "gauge", DS_TYPE_GAUGE },
641 { "innodb_rwlock_s_spin_waits", "operations", DS_TYPE_DERIVE },
642 { "innodb_rwlock_x_spin_waits", "operations", DS_TYPE_DERIVE },
643 { "innodb_rwlock_s_spin_rounds", "operations", DS_TYPE_DERIVE },
644 { "innodb_rwlock_x_spin_rounds", "operations", DS_TYPE_DERIVE },
645 { "innodb_rwlock_s_os_waits", "operations", DS_TYPE_DERIVE },
646 { "innodb_rwlock_x_os_waits", "operations", DS_TYPE_DERIVE },
648 { "dml_reads", "operations", DS_TYPE_DERIVE },
649 { "dml_inserts", "operations", DS_TYPE_DERIVE },
650 { "dml_deletes", "operations", DS_TYPE_DERIVE },
651 { "dml_updates", "operations", DS_TYPE_DERIVE },
653 { NULL, NULL, 0}
654 };
656 query = "SELECT name, count, type FROM information_schema.innodb_metrics WHERE status = 'enabled'";
658 res = exec_query (con, query);
659 if (res == NULL)
660 return (-1);
662 while ((row = mysql_fetch_row (res)))
663 {
664 int i;
665 char *key;
666 unsigned long long val;
668 key = row[0];
669 val = atoll (row[1]);
671 for (i = 0; metrics[i].key != NULL && strcmp(metrics[i].key, key) != 0; i++)
672 ;
674 if (metrics[i].key == NULL)
675 continue;
677 switch (metrics[i].ds_type) {
678 case DS_TYPE_COUNTER:
679 counter_submit(metrics[i].type, key, (counter_t)val, db);
680 break;
681 case DS_TYPE_GAUGE:
682 gauge_submit(metrics[i].type, key, (gauge_t)val, db);
683 break;
684 case DS_TYPE_DERIVE:
685 derive_submit(metrics[i].type, key, (derive_t)val, db);
686 break;
687 }
688 }
690 mysql_free_result(res);
691 return (0);
692 }
694 static int mysql_read_wsrep_stats (mysql_database_t *db, MYSQL *con)
695 {
696 MYSQL_RES *res;
697 MYSQL_ROW row;
699 const char *query;
700 struct {
701 const char *key;
702 const char *type;
703 int ds_type;
704 } metrics[] = {
706 { "wsrep_apply_oooe", "operations", DS_TYPE_DERIVE },
707 { "wsrep_apply_oool", "operations", DS_TYPE_DERIVE },
708 { "wsrep_causal_reads", "operations", DS_TYPE_DERIVE },
709 { "wsrep_commit_oooe", "operations", DS_TYPE_DERIVE },
710 { "wsrep_commit_oool", "operations", DS_TYPE_DERIVE },
711 { "wsrep_flow_control_recv", "operations", DS_TYPE_DERIVE },
712 { "wsrep_flow_control_sent", "operations", DS_TYPE_DERIVE },
713 { "wsrep_flow_control_paused", "operations", DS_TYPE_DERIVE },
714 { "wsrep_local_bf_aborts", "operations", DS_TYPE_DERIVE },
715 { "wsrep_local_cert_failures", "operations", DS_TYPE_DERIVE },
716 { "wsrep_local_commits", "operations", DS_TYPE_DERIVE },
717 { "wsrep_local_replays", "operations", DS_TYPE_DERIVE },
718 { "wsrep_received", "operations", DS_TYPE_DERIVE },
719 { "wsrep_replicated", "operations", DS_TYPE_DERIVE },
721 { "wsrep_received_bytes", "total_bytes", DS_TYPE_DERIVE },
722 { "wsrep_replicated_bytes", "total_bytes", DS_TYPE_DERIVE },
724 { "wsrep_apply_window", "gauge", DS_TYPE_GAUGE },
725 { "wsrep_commit_window", "gauge", DS_TYPE_GAUGE },
727 { "wsrep_cluster_size", "gauge", DS_TYPE_GAUGE },
728 { "wsrep_cert_deps_distance", "gauge", DS_TYPE_GAUGE },
730 { "wsrep_local_recv_queue", "queue_length", DS_TYPE_GAUGE },
731 { "wsrep_local_send_queue", "queue_length", DS_TYPE_GAUGE },
733 { NULL, NULL, 0}
735 };
737 query = "SHOW GLOBAL STATUS LIKE 'wsrep_%'";
739 res = exec_query (con, query);
740 if (res == NULL)
741 return (-1);
743 row = mysql_fetch_row (res);
744 if (row == NULL)
745 {
746 ERROR ("mysql plugin: Failed to get wsrep statistics: "
747 "`%s' did not return any rows.", query);
748 mysql_free_result (res);
749 return (-1);
750 }
752 while ((row = mysql_fetch_row (res)))
753 {
754 int i;
755 char *key;
756 unsigned long long val;
758 key = row[0];
759 val = atoll (row[1]);
761 for (i = 0; metrics[i].key != NULL && strcmp(metrics[i].key, key) != 0; i++)
762 ;
764 if (metrics[i].key == NULL)
765 continue;
767 switch (metrics[i].ds_type) {
768 case DS_TYPE_GAUGE:
769 gauge_submit(metrics[i].type, key, (gauge_t)val, db);
770 break;
771 case DS_TYPE_DERIVE:
772 derive_submit(metrics[i].type, key, (derive_t)val, db);
773 break;
774 }
775 }
777 mysql_free_result(res);
778 return (0);
779 } /* mysql_read_wsrep_stats */
781 static int mysql_read (user_data_t *ud)
782 {
783 mysql_database_t *db;
784 MYSQL *con;
785 MYSQL_RES *res;
786 MYSQL_ROW row;
787 const char *query;
789 derive_t qcache_hits = 0;
790 derive_t qcache_inserts = 0;
791 derive_t qcache_not_cached = 0;
792 derive_t qcache_lowmem_prunes = 0;
793 gauge_t qcache_queries_in_cache = NAN;
795 gauge_t threads_running = NAN;
796 gauge_t threads_connected = NAN;
797 gauge_t threads_cached = NAN;
798 derive_t threads_created = 0;
800 unsigned long long traffic_incoming = 0ULL;
801 unsigned long long traffic_outgoing = 0ULL;
802 unsigned long mysql_version = 0ULL;
804 if ((ud == NULL) || (ud->data == NULL))
805 {
806 ERROR ("mysql plugin: mysql_database_read: Invalid user data.");
807 return (-1);
808 }
810 db = (mysql_database_t *) ud->data;
812 /* An error message will have been printed in this case */
813 if ((con = getconnection (db)) == NULL)
814 return (-1);
816 mysql_version = mysql_get_server_version(con);
818 query = "SHOW STATUS";
819 if (mysql_version >= 50002)
820 query = "SHOW GLOBAL STATUS";
822 res = exec_query (con, query);
823 if (res == NULL)
824 return (-1);
826 while ((row = mysql_fetch_row (res)))
827 {
828 char *key;
829 unsigned long long val;
831 key = row[0];
832 val = atoll (row[1]);
834 if (strncmp (key, "Com_",
835 strlen ("Com_")) == 0)
836 {
837 if (val == 0ULL)
838 continue;
840 /* Ignore `prepared statements' */
841 if (strncmp (key, "Com_stmt_", strlen ("Com_stmt_")) != 0)
842 counter_submit ("mysql_commands",
843 key + strlen ("Com_"),
844 val, db);
845 }
846 else if (strncmp (key, "Handler_",
847 strlen ("Handler_")) == 0)
848 {
849 if (val == 0ULL)
850 continue;
852 counter_submit ("mysql_handler",
853 key + strlen ("Handler_"),
854 val, db);
855 }
856 else if (strncmp (key, "Qcache_",
857 strlen ("Qcache_")) == 0)
858 {
859 if (strcmp (key, "Qcache_hits") == 0)
860 qcache_hits = (derive_t) val;
861 else if (strcmp (key, "Qcache_inserts") == 0)
862 qcache_inserts = (derive_t) val;
863 else if (strcmp (key, "Qcache_not_cached") == 0)
864 qcache_not_cached = (derive_t) val;
865 else if (strcmp (key, "Qcache_lowmem_prunes") == 0)
866 qcache_lowmem_prunes = (derive_t) val;
867 else if (strcmp (key, "Qcache_queries_in_cache") == 0)
868 qcache_queries_in_cache = (gauge_t) val;
869 }
870 else if (strncmp (key, "Bytes_",
871 strlen ("Bytes_")) == 0)
872 {
873 if (strcmp (key, "Bytes_received") == 0)
874 traffic_incoming += val;
875 else if (strcmp (key, "Bytes_sent") == 0)
876 traffic_outgoing += val;
877 }
878 else if (strncmp (key, "Threads_",
879 strlen ("Threads_")) == 0)
880 {
881 if (strcmp (key, "Threads_running") == 0)
882 threads_running = (gauge_t) val;
883 else if (strcmp (key, "Threads_connected") == 0)
884 threads_connected = (gauge_t) val;
885 else if (strcmp (key, "Threads_cached") == 0)
886 threads_cached = (gauge_t) val;
887 else if (strcmp (key, "Threads_created") == 0)
888 threads_created = (derive_t) val;
889 }
890 else if (strncmp (key, "Table_locks_",
891 strlen ("Table_locks_")) == 0)
892 {
893 counter_submit ("mysql_locks",
894 key + strlen ("Table_locks_"),
895 val, db);
896 }
897 else if (db->innodb_stats && strncmp (key, "Innodb_", strlen ("Innodb_")) == 0)
898 {
899 /* buffer pool */
900 if (strcmp (key, "Innodb_buffer_pool_pages_data") == 0)
901 gauge_submit ("mysql_bpool_pages", "data", val, db);
902 else if (strcmp (key, "Innodb_buffer_pool_pages_dirty") == 0)
903 gauge_submit ("mysql_bpool_pages", "dirty", val, db);
904 else if (strcmp (key, "Innodb_buffer_pool_pages_flushed") == 0)
905 counter_submit ("mysql_bpool_counters", "pages_flushed", val, db);
906 else if (strcmp (key, "Innodb_buffer_pool_pages_free") == 0)
907 gauge_submit ("mysql_bpool_pages", "free", val, db);
908 else if (strcmp (key, "Innodb_buffer_pool_pages_misc") == 0)
909 gauge_submit ("mysql_bpool_pages", "misc", val, db);
910 else if (strcmp (key, "Innodb_buffer_pool_pages_total") == 0)
911 gauge_submit ("mysql_bpool_pages", "total", val, db);
912 else if (strcmp (key, "Innodb_buffer_pool_read_ahead_rnd") == 0)
913 counter_submit ("mysql_bpool_counters", "read_ahead_rnd", val, db);
914 else if (strcmp (key, "Innodb_buffer_pool_read_ahead") == 0)
915 counter_submit ("mysql_bpool_counters", "read_ahead", val, db);
916 else if (strcmp (key, "Innodb_buffer_pool_read_ahead_evicted") == 0)
917 counter_submit ("mysql_bpool_counters", "read_ahead_evicted", val, db);
918 else if (strcmp (key, "Innodb_buffer_pool_read_requests") == 0)
919 counter_submit ("mysql_bpool_counters", "read_requests", val, db);
920 else if (strcmp (key, "Innodb_buffer_pool_reads") == 0)
921 counter_submit ("mysql_bpool_counters", "reads", val, db);
922 else if (strcmp (key, "Innodb_buffer_pool_wait_free") == 0)
923 counter_submit ("mysql_bpool_counters", "wait_free", val, db);
924 else if (strcmp (key, "Innodb_buffer_pool_write_requests") == 0)
925 counter_submit ("mysql_bpool_counters", "write_requests", val, db);
926 else if (strcmp (key, "Innodb_buffer_pool_bytes_data") == 0)
927 gauge_submit ("mysql_bpool_bytes", "data", val, db);
928 else if (strcmp (key, "Innodb_buffer_pool_bytes_dirty") == 0)
929 gauge_submit ("mysql_bpool_bytes", "dirty", val, db);
931 /* data */
932 if (strcmp (key, "Innodb_data_fsyncs") == 0)
933 counter_submit ("mysql_innodb_data", "fsyncs", val, db);
934 else if (strcmp (key, "Innodb_data_read") == 0)
935 counter_submit ("mysql_innodb_data", "read", val, db);
936 else if (strcmp (key, "Innodb_data_reads") == 0)
937 counter_submit ("mysql_innodb_data", "reads", val, db);
938 else if (strcmp (key, "Innodb_data_writes") == 0)
939 counter_submit ("mysql_innodb_data", "writes", val, db);
940 else if (strcmp (key, "Innodb_data_written") == 0)
941 counter_submit ("mysql_innodb_data", "written", val, db);
943 /* double write */
944 else if (strcmp (key, "Innodb_dblwr_writes") == 0)
945 counter_submit ("mysql_innodb_dblwr", "writes", val, db);
946 else if (strcmp (key, "Innodb_dblwr_pages_written") == 0)
947 counter_submit ("mysql_innodb_dblwr", "written", val, db);
948 else if (strcmp (key, "Innodb_dblwr_page_size") == 0)
949 gauge_submit ("mysql_innodb_dblwr", "page_size", val, db);
951 /* log */
952 else if (strcmp (key, "Innodb_log_waits") == 0)
953 counter_submit ("mysql_innodb_log", "waits", val, db);
954 else if (strcmp (key, "Innodb_log_write_requests") == 0)
955 counter_submit ("mysql_innodb_log", "write_requests", val, db);
956 else if (strcmp (key, "Innodb_log_writes") == 0)
957 counter_submit ("mysql_innodb_log", "writes", val, db);
958 else if (strcmp (key, "Innodb_os_log_fsyncs") == 0)
959 counter_submit ("mysql_innodb_log", "fsyncs", val, db);
960 else if (strcmp (key, "Innodb_os_log_written") == 0)
961 counter_submit ("mysql_innodb_log", "written", val, db);
963 /* pages */
964 else if (strcmp (key, "Innodb_pages_created") == 0)
965 counter_submit ("mysql_innodb_pages", "created", val, db);
966 else if (strcmp (key, "Innodb_pages_read") == 0)
967 counter_submit ("mysql_innodb_pages", "read", val, db);
968 else if (strcmp (key, "Innodb_pages_written") == 0)
969 counter_submit ("mysql_innodb_pages", "written", val, db);
971 /* row lock */
972 else if (strcmp (key, "Innodb_row_lock_time") == 0)
973 counter_submit ("mysql_innodb_row_lock", "time", val, db);
974 else if (strcmp (key, "Innodb_row_lock_waits") == 0)
975 counter_submit ("mysql_innodb_row_lock", "waits", val, db);
977 /* rows */
978 else if (strcmp (key, "Innodb_rows_deleted") == 0)
979 counter_submit ("mysql_innodb_rows", "deleted", val, db);
980 else if (strcmp (key, "Innodb_rows_inserted") == 0)
981 counter_submit ("mysql_innodb_rows", "inserted", val, db);
982 else if (strcmp (key, "Innodb_rows_read") == 0)
983 counter_submit ("mysql_innodb_rows", "read", val, db);
984 else if (strcmp (key, "Innodb_rows_updated") == 0)
985 counter_submit ("mysql_innodb_rows", "updated", val, db);
986 }
987 else if (strncmp (key, "Select_", strlen ("Select_")) == 0)
988 {
989 counter_submit ("mysql_select", key + strlen ("Select_"),
990 val, db);
991 }
992 else if (strncmp (key, "Sort_", strlen ("Sort_")) == 0)
993 {
994 if (strcmp (key, "Sort_merge_passes") == 0)
995 counter_submit ("mysql_sort_merge_passes", NULL, val, db);
996 else if (strcmp (key, "Sort_rows") == 0)
997 counter_submit ("mysql_sort_rows", NULL, val, db);
998 else if (strcmp (key, "Sort_range") == 0)
999 counter_submit ("mysql_sort", "range", val, db);
1000 else if (strcmp (key, "Sort_scan") == 0)
1001 counter_submit ("mysql_sort", "scan", val, db);
1003 }
1004 else if (strncmp (key, "Slow_queries", strlen ("Slow_queries")) == 0)
1005 {
1006 counter_submit ("mysql_slow_queries", NULL , val, db);
1007 }
1008 }
1009 mysql_free_result (res); res = NULL;
1011 if ((qcache_hits != 0)
1012 || (qcache_inserts != 0)
1013 || (qcache_not_cached != 0)
1014 || (qcache_lowmem_prunes != 0))
1015 {
1016 derive_submit ("cache_result", "qcache-hits",
1017 qcache_hits, db);
1018 derive_submit ("cache_result", "qcache-inserts",
1019 qcache_inserts, db);
1020 derive_submit ("cache_result", "qcache-not_cached",
1021 qcache_not_cached, db);
1022 derive_submit ("cache_result", "qcache-prunes",
1023 qcache_lowmem_prunes, db);
1025 gauge_submit ("cache_size", "qcache",
1026 qcache_queries_in_cache, db);
1027 }
1029 if (threads_created != 0)
1030 {
1031 gauge_submit ("threads", "running",
1032 threads_running, db);
1033 gauge_submit ("threads", "connected",
1034 threads_connected, db);
1035 gauge_submit ("threads", "cached",
1036 threads_cached, db);
1038 derive_submit ("total_threads", "created",
1039 threads_created, db);
1040 }
1042 traffic_submit (traffic_incoming, traffic_outgoing, db);
1044 if (mysql_version >= 50600 && db->innodb_stats)
1045 mysql_read_innodb_stats (db, con);
1047 if (db->master_stats)
1048 mysql_read_master_stats (db, con);
1050 if ((db->slave_stats) || (db->slave_notif))
1051 mysql_read_slave_stats (db, con);
1053 if (db->wsrep_stats)
1054 mysql_read_wsrep_stats (db, con);
1056 return (0);
1057 } /* int mysql_read */
1059 void module_register (void)
1060 {
1061 plugin_register_complex_config ("mysql", mysql_config);
1062 } /* void module_register */