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
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.
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.
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
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>
34 #include "configfile.h"
38 #elif defined(HAVE_MYSQL_MYSQL_H)
39 #include <mysql/mysql.h>
42 struct mysql_database_s /* {{{ */
60 _Bool slave_io_running;
61 _Bool slave_sql_running;
66 typedef struct mysql_database_s mysql_database_t; /* }}} */
68 static int mysql_read (user_data_t *ud);
70 static void mysql_database_free (void *arg) /* {{{ */
74 DEBUG ("mysql plugin: mysql_database_free (arg = %p);", arg);
82 mysql_close (db->con);
92 } /* }}} void mysql_database_free */
94 /* Configuration handling functions {{{
97 * <Database "plugin_instance1">
104 static int mysql_config_database (oconfig_item_t *ci) /* {{{ */
106 mysql_database_t *db;
110 if ((ci->values_num != 1)
111 || (ci->values[0].type != OCONFIG_TYPE_STRING))
113 WARNING ("mysql plugin: The `Database' block "
114 "needs exactly one string argument.");
118 db = calloc (1, sizeof (*db));
121 ERROR ("mysql plugin: calloc failed.");
125 /* initialize all the pointers */
135 /* trigger a notification, if it's not running */
136 db->slave_io_running = 1;
137 db->slave_sql_running = 1;
139 status = cf_util_get_string (ci, &db->instance);
145 assert (db->instance != NULL);
147 /* Fill the `mysql_database_t' structure.. */
148 for (i = 0; i < ci->children_num; i++)
150 oconfig_item_t *child = ci->children + i;
152 if (strcasecmp ("Alias", child->key) == 0)
153 status = cf_util_get_string (child, &db->alias);
154 else if (strcasecmp ("Host", child->key) == 0)
155 status = cf_util_get_string (child, &db->host);
156 else if (strcasecmp ("User", child->key) == 0)
157 status = cf_util_get_string (child, &db->user);
158 else if (strcasecmp ("Password", child->key) == 0)
159 status = cf_util_get_string (child, &db->pass);
160 else if (strcasecmp ("Port", child->key) == 0)
162 status = cf_util_get_port_number (child);
169 else if (strcasecmp ("Socket", child->key) == 0)
170 status = cf_util_get_string (child, &db->socket);
171 else if (strcasecmp ("Database", child->key) == 0)
172 status = cf_util_get_string (child, &db->database);
173 else if (strcasecmp ("ConnectTimeout", child->key) == 0)
174 status = cf_util_get_int (child, &db->timeout);
175 else if (strcasecmp ("MasterStats", child->key) == 0)
176 status = cf_util_get_boolean (child, &db->master_stats);
177 else if (strcasecmp ("SlaveStats", child->key) == 0)
178 status = cf_util_get_boolean (child, &db->slave_stats);
179 else if (strcasecmp ("SlaveNotifications", child->key) == 0)
180 status = cf_util_get_boolean (child, &db->slave_notif);
181 else if (strcasecmp ("InnodbStats", child->key) == 0)
182 status = cf_util_get_boolean (child, &db->innodb_stats);
183 else if (strcasecmp ("WsrepStats", child->key) == 0)
184 status = cf_util_get_boolean (child, &db->wsrep_stats);
187 WARNING ("mysql plugin: Option `%s' not allowed here.", child->key);
195 /* If all went well, register this database for reading */
198 user_data_t ud = { 0 };
199 char cb_name[DATA_MAX_NAME_LEN];
201 DEBUG ("mysql plugin: Registering new read callback: %s",
202 (db->database != NULL) ? db->database : "<default>");
204 ud.data = (void *) db;
205 ud.free_func = mysql_database_free;
207 if (db->instance != NULL)
208 ssnprintf (cb_name, sizeof (cb_name), "mysql-%s",
211 sstrncpy (cb_name, "mysql", sizeof (cb_name));
213 plugin_register_complex_read (/* group = */ NULL, cb_name,
215 /* interval = */ 0, &ud);
219 mysql_database_free (db);
224 } /* }}} int mysql_config_database */
226 static int mysql_config (oconfig_item_t *ci) /* {{{ */
233 /* Fill the `mysql_database_t' structure.. */
234 for (i = 0; i < ci->children_num; i++)
236 oconfig_item_t *child = ci->children + i;
238 if (strcasecmp ("Database", child->key) == 0)
239 mysql_config_database (child);
241 WARNING ("mysql plugin: Option \"%s\" not allowed here.",
246 } /* }}} int mysql_config */
248 /* }}} End of configuration handling functions */
250 static MYSQL *getconnection (mysql_database_t *db)
252 if (db->is_connected)
256 status = mysql_ping (db->con);
260 WARNING ("mysql plugin: Lost connection to instance \"%s\": %s",
261 db->instance, mysql_error (db->con));
263 db->is_connected = 0;
267 db->con = mysql_init (NULL);
270 ERROR ("mysql plugin: mysql_init failed: %s",
271 mysql_error (db->con));
276 /* Configure TCP connect timeout (default: 0) */
277 db->con->options.connect_timeout = db->timeout;
279 if (mysql_real_connect (db->con, db->host, db->user, db->pass,
280 db->database, db->port, db->socket, 0) == NULL)
282 ERROR ("mysql plugin: Failed to connect to database %s "
284 (db->database != NULL) ? db->database : "<none>",
285 (db->host != NULL) ? db->host : "localhost",
286 mysql_error (db->con));
290 INFO ("mysql plugin: Successfully connected to database %s "
291 "at server %s (server version: %s, protocol version: %d)",
292 (db->database != NULL) ? db->database : "<none>",
293 mysql_get_host_info (db->con),
294 mysql_get_server_info (db->con),
295 mysql_get_proto_info (db->con));
297 db->is_connected = 1;
299 } /* static MYSQL *getconnection (mysql_database_t *db) */
301 static void set_host (mysql_database_t *db, char *buf, size_t buflen)
304 sstrncpy (buf, db->alias, buflen);
305 else if ((db->host == NULL)
306 || (strcmp ("", db->host) == 0)
307 || (strcmp ("127.0.0.1", db->host) == 0)
308 || (strcmp ("localhost", db->host) == 0))
309 sstrncpy (buf, hostname_g, buflen);
311 sstrncpy (buf, db->host, buflen);
312 } /* void set_host */
314 static void submit (const char *type, const char *type_instance,
315 value_t *values, size_t values_len, mysql_database_t *db)
317 value_list_t vl = VALUE_LIST_INIT;
320 vl.values_len = values_len;
322 set_host (db, vl.host, sizeof (vl.host));
324 sstrncpy (vl.plugin, "mysql", sizeof (vl.plugin));
326 /* Assured by "mysql_config_database" */
327 assert (db->instance != NULL);
328 sstrncpy (vl.plugin_instance, db->instance, sizeof (vl.plugin_instance));
330 sstrncpy (vl.type, type, sizeof (vl.type));
331 if (type_instance != NULL)
332 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
334 plugin_dispatch_values (&vl);
337 static void counter_submit (const char *type, const char *type_instance,
338 derive_t value, mysql_database_t *db)
342 values[0].derive = value;
343 submit (type, type_instance, values, STATIC_ARRAY_SIZE (values), db);
344 } /* void counter_submit */
346 static void gauge_submit (const char *type, const char *type_instance,
347 gauge_t value, mysql_database_t *db)
351 values[0].gauge = value;
352 submit (type, type_instance, values, STATIC_ARRAY_SIZE (values), db);
353 } /* void gauge_submit */
355 static void derive_submit (const char *type, const char *type_instance,
356 derive_t value, mysql_database_t *db)
360 values[0].derive = value;
361 submit (type, type_instance, values, STATIC_ARRAY_SIZE (values), db);
362 } /* void derive_submit */
364 static void traffic_submit (derive_t rx, derive_t tx, mysql_database_t *db)
368 values[0].derive = rx;
369 values[1].derive = tx;
371 submit ("mysql_octets", NULL, values, STATIC_ARRAY_SIZE (values), db);
372 } /* void traffic_submit */
374 static MYSQL_RES *exec_query (MYSQL *con, const char *query)
378 int query_len = strlen (query);
380 if (mysql_real_query (con, query, query_len))
382 ERROR ("mysql plugin: Failed to execute query: %s",
384 INFO ("mysql plugin: SQL query was: %s", query);
388 res = mysql_store_result (con);
391 ERROR ("mysql plugin: Failed to store query result: %s",
393 INFO ("mysql plugin: SQL query was: %s", query);
400 static int mysql_read_master_stats (mysql_database_t *db, MYSQL *con)
407 unsigned long long position;
409 query = "SHOW MASTER STATUS";
411 res = exec_query (con, query);
415 row = mysql_fetch_row (res);
418 ERROR ("mysql plugin: Failed to get master statistics: "
419 "`%s' did not return any rows.", query);
420 mysql_free_result (res);
424 field_num = mysql_num_fields (res);
427 ERROR ("mysql plugin: Failed to get master statistics: "
428 "`%s' returned less than two columns.", query);
429 mysql_free_result (res);
433 position = atoll (row[1]);
434 counter_submit ("mysql_log_position", "master-bin", position, db);
436 row = mysql_fetch_row (res);
438 WARNING ("mysql plugin: `%s' returned more than one row - "
439 "ignoring further results.", query);
441 mysql_free_result (res);
444 } /* mysql_read_master_stats */
446 static int mysql_read_slave_stats (mysql_database_t *db, MYSQL *con)
454 /* WTF? libmysqlclient does not seem to provide any means to
455 * translate a column name to a column index ... :-/ */
456 const int READ_MASTER_LOG_POS_IDX = 6;
457 const int SLAVE_IO_RUNNING_IDX = 10;
458 const int SLAVE_SQL_RUNNING_IDX = 11;
459 const int EXEC_MASTER_LOG_POS_IDX = 21;
460 const int SECONDS_BEHIND_MASTER_IDX = 32;
462 query = "SHOW SLAVE STATUS";
464 res = exec_query (con, query);
468 row = mysql_fetch_row (res);
471 ERROR ("mysql plugin: Failed to get slave statistics: "
472 "`%s' did not return any rows.", query);
473 mysql_free_result (res);
477 field_num = mysql_num_fields (res);
480 ERROR ("mysql plugin: Failed to get slave statistics: "
481 "`%s' returned less than 33 columns.", query);
482 mysql_free_result (res);
488 unsigned long long counter;
491 counter = atoll (row[READ_MASTER_LOG_POS_IDX]);
492 counter_submit ("mysql_log_position", "slave-read", counter, db);
494 counter = atoll (row[EXEC_MASTER_LOG_POS_IDX]);
495 counter_submit ("mysql_log_position", "slave-exec", counter, db);
497 if (row[SECONDS_BEHIND_MASTER_IDX] != NULL)
499 gauge = atof (row[SECONDS_BEHIND_MASTER_IDX]);
500 gauge_submit ("time_offset", NULL, gauge, db);
506 notification_t n = { 0, cdtime (), "", "",
507 "mysql", "", "time_offset", "", NULL };
511 io = row[SLAVE_IO_RUNNING_IDX];
512 sql = row[SLAVE_SQL_RUNNING_IDX];
514 set_host (db, n.host, sizeof (n.host));
516 /* Assured by "mysql_config_database" */
517 assert (db->instance != NULL);
518 sstrncpy (n.plugin_instance, db->instance, sizeof (n.plugin_instance));
520 if (((io == NULL) || (strcasecmp (io, "yes") != 0))
521 && (db->slave_io_running))
523 n.severity = NOTIF_WARNING;
524 ssnprintf (n.message, sizeof (n.message),
525 "slave I/O thread not started or not connected to master");
526 plugin_dispatch_notification (&n);
527 db->slave_io_running = 0;
529 else if (((io != NULL) && (strcasecmp (io, "yes") == 0))
530 && (! db->slave_io_running))
532 n.severity = NOTIF_OKAY;
533 ssnprintf (n.message, sizeof (n.message),
534 "slave I/O thread started and connected to master");
535 plugin_dispatch_notification (&n);
536 db->slave_io_running = 1;
539 if (((sql == NULL) || (strcasecmp (sql, "yes") != 0))
540 && (db->slave_sql_running))
542 n.severity = NOTIF_WARNING;
543 ssnprintf (n.message, sizeof (n.message),
544 "slave SQL thread not started");
545 plugin_dispatch_notification (&n);
546 db->slave_sql_running = 0;
548 else if (((sql != NULL) && (strcasecmp (sql, "yes") == 0))
549 && (! db->slave_sql_running))
551 n.severity = NOTIF_OKAY;
552 ssnprintf (n.message, sizeof (n.message),
553 "slave SQL thread started");
554 plugin_dispatch_notification (&n);
555 db->slave_sql_running = 1;
559 row = mysql_fetch_row (res);
561 WARNING ("mysql plugin: `%s' returned more than one row - "
562 "ignoring further results.", query);
564 mysql_free_result (res);
567 } /* mysql_read_slave_stats */
569 static int mysql_read_innodb_stats (mysql_database_t *db, MYSQL *con)
580 { "metadata_mem_pool_size", "bytes", DS_TYPE_GAUGE },
581 { "lock_deadlocks", "mysql_locks", DS_TYPE_DERIVE },
582 { "lock_timeouts", "mysql_locks", DS_TYPE_DERIVE },
583 { "lock_row_lock_current_waits", "mysql_locks", DS_TYPE_DERIVE },
584 { "buffer_pool_size", "bytes", DS_TYPE_GAUGE },
586 { "buffer_pool_reads", "operations", DS_TYPE_DERIVE },
587 { "buffer_pool_read_requests", "operations", DS_TYPE_DERIVE },
588 { "buffer_pool_write_requests", "operations", DS_TYPE_DERIVE },
589 { "buffer_pool_wait_free", "operations", DS_TYPE_DERIVE },
590 { "buffer_pool_read_ahead", "operations", DS_TYPE_DERIVE },
591 { "buffer_pool_read_ahead_evicted", "operations", DS_TYPE_DERIVE },
593 { "buffer_pool_pages_total", "gauge", DS_TYPE_GAUGE },
594 { "buffer_pool_pages_misc", "gauge", DS_TYPE_GAUGE },
595 { "buffer_pool_pages_data", "gauge", DS_TYPE_GAUGE },
596 { "buffer_pool_bytes_data", "gauge", DS_TYPE_GAUGE },
597 { "buffer_pool_pages_dirty", "gauge", DS_TYPE_GAUGE },
598 { "buffer_pool_bytes_dirty", "gauge", DS_TYPE_GAUGE },
599 { "buffer_pool_pages_free", "gauge", DS_TYPE_GAUGE },
601 { "buffer_pages_created", "operations", DS_TYPE_DERIVE },
602 { "buffer_pages_written", "operations", DS_TYPE_DERIVE },
603 { "buffer_pages_read", "operations", DS_TYPE_DERIVE },
604 { "buffer_data_reads", "operations", DS_TYPE_DERIVE },
605 { "buffer_data_written", "operations", DS_TYPE_DERIVE },
607 { "os_data_reads", "operations", DS_TYPE_DERIVE },
608 { "os_data_writes", "operations", DS_TYPE_DERIVE },
609 { "os_data_fsyncs", "operations", DS_TYPE_DERIVE },
610 { "os_log_bytes_written", "operations", DS_TYPE_DERIVE },
611 { "os_log_fsyncs", "operations", DS_TYPE_DERIVE },
612 { "os_log_pending_fsyncs", "operations", DS_TYPE_DERIVE },
613 { "os_log_pending_writes", "operations", DS_TYPE_DERIVE },
615 { "trx_rseg_history_len", "gauge", DS_TYPE_GAUGE },
617 { "log_waits", "operations", DS_TYPE_DERIVE },
618 { "log_write_requests", "operations", DS_TYPE_DERIVE },
619 { "log_writes", "operations", DS_TYPE_DERIVE },
620 { "adaptive_hash_searches", "operations", DS_TYPE_DERIVE },
622 { "file_num_open_files", "gauge", DS_TYPE_GAUGE },
624 { "ibuf_merges_insert", "operations", DS_TYPE_DERIVE },
625 { "ibuf_merges_delete_mark", "operations", DS_TYPE_DERIVE },
626 { "ibuf_merges_delete", "operations", DS_TYPE_DERIVE },
627 { "ibuf_merges_discard_insert", "operations", DS_TYPE_DERIVE },
628 { "ibuf_merges_discard_delete_mark", "operations", DS_TYPE_DERIVE },
629 { "ibuf_merges_discard_delete", "operations", DS_TYPE_DERIVE },
630 { "ibuf_merges_discard_merges", "operations", DS_TYPE_DERIVE },
631 { "ibuf_size", "bytes", DS_TYPE_GAUGE },
633 { "innodb_activity_count", "gauge", DS_TYPE_GAUGE },
634 { "innodb_dblwr_writes", "operations", DS_TYPE_DERIVE },
635 { "innodb_dblwr_pages_written", "operations", DS_TYPE_DERIVE },
636 { "innodb_dblwr_page_size", "gauge", DS_TYPE_GAUGE },
638 { "innodb_rwlock_s_spin_waits", "operations", DS_TYPE_DERIVE },
639 { "innodb_rwlock_x_spin_waits", "operations", DS_TYPE_DERIVE },
640 { "innodb_rwlock_s_spin_rounds", "operations", DS_TYPE_DERIVE },
641 { "innodb_rwlock_x_spin_rounds", "operations", DS_TYPE_DERIVE },
642 { "innodb_rwlock_s_os_waits", "operations", DS_TYPE_DERIVE },
643 { "innodb_rwlock_x_os_waits", "operations", DS_TYPE_DERIVE },
645 { "dml_reads", "operations", DS_TYPE_DERIVE },
646 { "dml_inserts", "operations", DS_TYPE_DERIVE },
647 { "dml_deletes", "operations", DS_TYPE_DERIVE },
648 { "dml_updates", "operations", DS_TYPE_DERIVE },
653 query = "SELECT name, count, type FROM information_schema.innodb_metrics WHERE status = 'enabled'";
655 res = exec_query (con, query);
659 while ((row = mysql_fetch_row (res)))
663 unsigned long long val;
666 val = atoll (row[1]);
668 for (i = 0; metrics[i].key != NULL && strcmp(metrics[i].key, key) != 0; i++)
671 if (metrics[i].key == NULL)
674 switch (metrics[i].ds_type) {
675 case DS_TYPE_COUNTER:
676 counter_submit(metrics[i].type, key, (counter_t)val, db);
679 gauge_submit(metrics[i].type, key, (gauge_t)val, db);
682 derive_submit(metrics[i].type, key, (derive_t)val, db);
687 mysql_free_result(res);
691 static int mysql_read_wsrep_stats (mysql_database_t *db, MYSQL *con)
703 { "wsrep_apply_oooe", "operations", DS_TYPE_DERIVE },
704 { "wsrep_apply_oool", "operations", DS_TYPE_DERIVE },
705 { "wsrep_causal_reads", "operations", DS_TYPE_DERIVE },
706 { "wsrep_commit_oooe", "operations", DS_TYPE_DERIVE },
707 { "wsrep_commit_oool", "operations", DS_TYPE_DERIVE },
708 { "wsrep_flow_control_recv", "operations", DS_TYPE_DERIVE },
709 { "wsrep_flow_control_sent", "operations", DS_TYPE_DERIVE },
710 { "wsrep_flow_control_paused", "operations", DS_TYPE_DERIVE },
711 { "wsrep_local_bf_aborts", "operations", DS_TYPE_DERIVE },
712 { "wsrep_local_cert_failures", "operations", DS_TYPE_DERIVE },
713 { "wsrep_local_commits", "operations", DS_TYPE_DERIVE },
714 { "wsrep_local_replays", "operations", DS_TYPE_DERIVE },
715 { "wsrep_received", "operations", DS_TYPE_DERIVE },
716 { "wsrep_replicated", "operations", DS_TYPE_DERIVE },
718 { "wsrep_received_bytes", "total_bytes", DS_TYPE_DERIVE },
719 { "wsrep_replicated_bytes", "total_bytes", DS_TYPE_DERIVE },
721 { "wsrep_apply_window", "gauge", DS_TYPE_GAUGE },
722 { "wsrep_commit_window", "gauge", DS_TYPE_GAUGE },
724 { "wsrep_cluster_size", "gauge", DS_TYPE_GAUGE },
725 { "wsrep_cert_deps_distance", "gauge", DS_TYPE_GAUGE },
727 { "wsrep_local_recv_queue", "queue_length", DS_TYPE_GAUGE },
728 { "wsrep_local_send_queue", "queue_length", DS_TYPE_GAUGE },
734 query = "SHOW GLOBAL STATUS LIKE 'wsrep_%'";
736 res = exec_query (con, query);
740 row = mysql_fetch_row (res);
743 ERROR ("mysql plugin: Failed to get wsrep statistics: "
744 "`%s' did not return any rows.", query);
745 mysql_free_result (res);
749 while ((row = mysql_fetch_row (res)))
753 unsigned long long val;
756 val = atoll (row[1]);
758 for (i = 0; metrics[i].key != NULL && strcmp(metrics[i].key, key) != 0; i++)
761 if (metrics[i].key == NULL)
764 switch (metrics[i].ds_type) {
766 gauge_submit(metrics[i].type, key, (gauge_t)val, db);
769 derive_submit(metrics[i].type, key, (derive_t)val, db);
774 mysql_free_result(res);
776 } /* mysql_read_wsrep_stats */
778 static int mysql_read (user_data_t *ud)
780 mysql_database_t *db;
786 derive_t qcache_hits = 0;
787 derive_t qcache_inserts = 0;
788 derive_t qcache_not_cached = 0;
789 derive_t qcache_lowmem_prunes = 0;
790 gauge_t qcache_queries_in_cache = NAN;
792 gauge_t threads_running = NAN;
793 gauge_t threads_connected = NAN;
794 gauge_t threads_cached = NAN;
795 derive_t threads_created = 0;
797 unsigned long long traffic_incoming = 0ULL;
798 unsigned long long traffic_outgoing = 0ULL;
799 unsigned long mysql_version = 0ULL;
801 if ((ud == NULL) || (ud->data == NULL))
803 ERROR ("mysql plugin: mysql_database_read: Invalid user data.");
807 db = (mysql_database_t *) ud->data;
809 /* An error message will have been printed in this case */
810 if ((con = getconnection (db)) == NULL)
813 mysql_version = mysql_get_server_version(con);
815 query = "SHOW STATUS";
816 if (mysql_version >= 50002)
817 query = "SHOW GLOBAL STATUS";
819 res = exec_query (con, query);
823 while ((row = mysql_fetch_row (res)))
826 unsigned long long val;
829 val = atoll (row[1]);
831 if (strncmp (key, "Com_",
832 strlen ("Com_")) == 0)
837 /* Ignore `prepared statements' */
838 if (strncmp (key, "Com_stmt_", strlen ("Com_stmt_")) != 0)
839 counter_submit ("mysql_commands",
840 key + strlen ("Com_"),
843 else if (strncmp (key, "Handler_",
844 strlen ("Handler_")) == 0)
849 counter_submit ("mysql_handler",
850 key + strlen ("Handler_"),
853 else if (strncmp (key, "Qcache_",
854 strlen ("Qcache_")) == 0)
856 if (strcmp (key, "Qcache_hits") == 0)
857 qcache_hits = (derive_t) val;
858 else if (strcmp (key, "Qcache_inserts") == 0)
859 qcache_inserts = (derive_t) val;
860 else if (strcmp (key, "Qcache_not_cached") == 0)
861 qcache_not_cached = (derive_t) val;
862 else if (strcmp (key, "Qcache_lowmem_prunes") == 0)
863 qcache_lowmem_prunes = (derive_t) val;
864 else if (strcmp (key, "Qcache_queries_in_cache") == 0)
865 qcache_queries_in_cache = (gauge_t) val;
867 else if (strncmp (key, "Bytes_",
868 strlen ("Bytes_")) == 0)
870 if (strcmp (key, "Bytes_received") == 0)
871 traffic_incoming += val;
872 else if (strcmp (key, "Bytes_sent") == 0)
873 traffic_outgoing += val;
875 else if (strncmp (key, "Threads_",
876 strlen ("Threads_")) == 0)
878 if (strcmp (key, "Threads_running") == 0)
879 threads_running = (gauge_t) val;
880 else if (strcmp (key, "Threads_connected") == 0)
881 threads_connected = (gauge_t) val;
882 else if (strcmp (key, "Threads_cached") == 0)
883 threads_cached = (gauge_t) val;
884 else if (strcmp (key, "Threads_created") == 0)
885 threads_created = (derive_t) val;
887 else if (strncmp (key, "Table_locks_",
888 strlen ("Table_locks_")) == 0)
890 counter_submit ("mysql_locks",
891 key + strlen ("Table_locks_"),
894 else if (db->innodb_stats && strncmp (key, "Innodb_", strlen ("Innodb_")) == 0)
897 if (strcmp (key, "Innodb_buffer_pool_pages_data") == 0)
898 gauge_submit ("mysql_bpool_pages", "data", val, db);
899 else if (strcmp (key, "Innodb_buffer_pool_pages_dirty") == 0)
900 gauge_submit ("mysql_bpool_pages", "dirty", val, db);
901 else if (strcmp (key, "Innodb_buffer_pool_pages_flushed") == 0)
902 counter_submit ("mysql_bpool_counters", "pages_flushed", val, db);
903 else if (strcmp (key, "Innodb_buffer_pool_pages_free") == 0)
904 gauge_submit ("mysql_bpool_pages", "free", val, db);
905 else if (strcmp (key, "Innodb_buffer_pool_pages_misc") == 0)
906 gauge_submit ("mysql_bpool_pages", "misc", val, db);
907 else if (strcmp (key, "Innodb_buffer_pool_pages_total") == 0)
908 gauge_submit ("mysql_bpool_pages", "total", val, db);
909 else if (strcmp (key, "Innodb_buffer_pool_read_ahead_rnd") == 0)
910 counter_submit ("mysql_bpool_counters", "read_ahead_rnd", val, db);
911 else if (strcmp (key, "Innodb_buffer_pool_read_ahead") == 0)
912 counter_submit ("mysql_bpool_counters", "read_ahead", val, db);
913 else if (strcmp (key, "Innodb_buffer_pool_read_ahead_evicted") == 0)
914 counter_submit ("mysql_bpool_counters", "read_ahead_evicted", val, db);
915 else if (strcmp (key, "Innodb_buffer_pool_read_requests") == 0)
916 counter_submit ("mysql_bpool_counters", "read_requests", val, db);
917 else if (strcmp (key, "Innodb_buffer_pool_reads") == 0)
918 counter_submit ("mysql_bpool_counters", "reads", val, db);
919 else if (strcmp (key, "Innodb_buffer_pool_write_requests") == 0)
920 counter_submit ("mysql_bpool_counters", "write_requests", val, db);
921 else if (strcmp (key, "Innodb_buffer_pool_bytes_data") == 0)
922 gauge_submit ("mysql_bpool_bytes", "data", val, db);
923 else if (strcmp (key, "Innodb_buffer_pool_bytes_dirty") == 0)
924 gauge_submit ("mysql_bpool_bytes", "dirty", val, db);
927 if (strcmp (key, "Innodb_data_fsyncs") == 0)
928 counter_submit ("mysql_innodb_data", "fsyncs", val, db);
929 else if (strcmp (key, "Innodb_data_read") == 0)
930 counter_submit ("mysql_innodb_data", "read", val, db);
931 else if (strcmp (key, "Innodb_data_reads") == 0)
932 counter_submit ("mysql_innodb_data", "reads", val, db);
933 else if (strcmp (key, "Innodb_data_writes") == 0)
934 counter_submit ("mysql_innodb_data", "writes", val, db);
935 else if (strcmp (key, "Innodb_data_written") == 0)
936 counter_submit ("mysql_innodb_data", "written", val, db);
939 else if (strcmp (key, "Innodb_dblwr_writes") == 0)
940 counter_submit ("mysql_innodb_dblwr", "writes", val, db);
941 else if (strcmp (key, "Innodb_dblwr_pages_written") == 0)
942 counter_submit ("mysql_innodb_dblwr", "written", val, db);
945 else if (strcmp (key, "Innodb_log_waits") == 0)
946 counter_submit ("mysql_innodb_log", "waits", val, db);
947 else if (strcmp (key, "Innodb_log_write_requests") == 0)
948 counter_submit ("mysql_innodb_log", "write_requests", val, db);
949 else if (strcmp (key, "Innodb_log_writes") == 0)
950 counter_submit ("mysql_innodb_log", "writes", val, db);
951 else if (strcmp (key, "Innodb_os_log_fsyncs") == 0)
952 counter_submit ("mysql_innodb_log", "fsyncs", val, db);
953 else if (strcmp (key, "Innodb_os_log_written") == 0)
954 counter_submit ("mysql_innodb_log", "written", val, db);
957 else if (strcmp (key, "Innodb_pages_created") == 0)
958 counter_submit ("mysql_innodb_pages", "created", val, db);
959 else if (strcmp (key, "Innodb_pages_read") == 0)
960 counter_submit ("mysql_innodb_pages", "read", val, db);
961 else if (strcmp (key, "Innodb_pages_written") == 0)
962 counter_submit ("mysql_innodb_pages", "written", val, db);
965 else if (strcmp (key, "Innodb_row_lock_time") == 0)
966 counter_submit ("mysql_innodb_row_lock", "time", val, db);
967 else if (strcmp (key, "Innodb_row_lock_waits") == 0)
968 counter_submit ("mysql_innodb_row_lock", "waits", val, db);
971 else if (strcmp (key, "Innodb_rows_deleted") == 0)
972 counter_submit ("mysql_innodb_rows", "deleted", val, db);
973 else if (strcmp (key, "Innodb_rows_inserted") == 0)
974 counter_submit ("mysql_innodb_rows", "inserted", val, db);
975 else if (strcmp (key, "Innodb_rows_read") == 0)
976 counter_submit ("mysql_innodb_rows", "read", val, db);
977 else if (strcmp (key, "Innodb_rows_updated") == 0)
978 counter_submit ("mysql_innodb_rows", "updated", val, db);
980 else if (strncmp (key, "Select_", strlen ("Select_")) == 0)
982 counter_submit ("mysql_select", key + strlen ("Select_"),
985 else if (strncmp (key, "Sort_", strlen ("Sort_")) == 0)
987 if (strcmp (key, "Sort_merge_passes") == 0)
988 counter_submit ("mysql_sort_merge_passes", NULL, val, db);
989 else if (strcmp (key, "Sort_rows") == 0)
990 counter_submit ("mysql_sort_rows", NULL, val, db);
991 else if (strcmp (key, "Sort_range") == 0)
992 counter_submit ("mysql_sort", "range", val, db);
993 else if (strcmp (key, "Sort_scan") == 0)
994 counter_submit ("mysql_sort", "scan", val, db);
997 else if (strncmp (key, "Slow_queries", strlen ("Slow_queries")) == 0)
999 counter_submit ("mysql_slow_queries", NULL , val, db);
1002 mysql_free_result (res); res = NULL;
1004 if ((qcache_hits != 0)
1005 || (qcache_inserts != 0)
1006 || (qcache_not_cached != 0)
1007 || (qcache_lowmem_prunes != 0))
1009 derive_submit ("cache_result", "qcache-hits",
1011 derive_submit ("cache_result", "qcache-inserts",
1012 qcache_inserts, db);
1013 derive_submit ("cache_result", "qcache-not_cached",
1014 qcache_not_cached, db);
1015 derive_submit ("cache_result", "qcache-prunes",
1016 qcache_lowmem_prunes, db);
1018 gauge_submit ("cache_size", "qcache",
1019 qcache_queries_in_cache, db);
1022 if (threads_created != 0)
1024 gauge_submit ("threads", "running",
1025 threads_running, db);
1026 gauge_submit ("threads", "connected",
1027 threads_connected, db);
1028 gauge_submit ("threads", "cached",
1029 threads_cached, db);
1031 derive_submit ("total_threads", "created",
1032 threads_created, db);
1035 traffic_submit (traffic_incoming, traffic_outgoing, db);
1037 if (mysql_version >= 50600 && db->innodb_stats)
1038 mysql_read_innodb_stats (db, con);
1040 if (db->master_stats)
1041 mysql_read_master_stats (db, con);
1043 if ((db->slave_stats) || (db->slave_notif))
1044 mysql_read_slave_stats (db, con);
1046 if (db->wsrep_stats)
1047 mysql_read_wsrep_stats (db, con);
1050 } /* int mysql_read */
1052 void module_register (void)
1054 plugin_register_complex_config ("mysql", mysql_config);
1055 } /* void module_register */