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>
33 #include "configfile.h"
37 #elif defined(HAVE_MYSQL_MYSQL_H)
38 #include <mysql/mysql.h>
41 /* TODO: Understand `Select_*' and possibly do that stuff as well.. */
43 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 void mysql_read_default_options(struct st_mysql_options *options,
71 const char *filename,const char *group);
73 static void mysql_database_free (void *arg) /* {{{ */
77 DEBUG ("mysql plugin: mysql_database_free (arg = %p);", arg);
79 db = (mysql_database_t *) arg;
85 mysql_close (db->con);
95 } /* }}} void mysql_database_free */
97 /* Configuration handling functions {{{
100 * <Database "plugin_instance1">
107 static int mysql_config_database (oconfig_item_t *ci) /* {{{ */
109 mysql_database_t *db;
113 if ((ci->values_num != 1)
114 || (ci->values[0].type != OCONFIG_TYPE_STRING))
116 WARNING ("mysql plugin: The `Database' block "
117 "needs exactly one string argument.");
121 db = (mysql_database_t *) malloc (sizeof (*db));
124 ERROR ("mysql plugin: malloc failed.");
127 memset (db, 0, sizeof (*db));
129 /* initialize all the pointers */
139 /* trigger a notification, if it's not running */
140 db->slave_io_running = 1;
141 db->slave_sql_running = 1;
143 status = cf_util_get_string (ci, &db->instance);
149 assert (db->instance != NULL);
151 /* Fill the `mysql_database_t' structure.. */
152 for (i = 0; i < ci->children_num; i++)
154 oconfig_item_t *child = ci->children + i;
156 if (strcasecmp ("Alias", child->key) == 0)
157 status = cf_util_get_string (child, &db->alias);
158 else if (strcasecmp ("Host", child->key) == 0)
159 status = cf_util_get_string (child, &db->host);
160 else if (strcasecmp ("User", child->key) == 0)
161 status = cf_util_get_string (child, &db->user);
162 else if (strcasecmp ("Password", child->key) == 0)
163 status = cf_util_get_string (child, &db->pass);
164 else if (strcasecmp ("Port", child->key) == 0)
166 status = cf_util_get_port_number (child);
173 else if (strcasecmp ("Socket", child->key) == 0)
174 status = cf_util_get_string (child, &db->socket);
175 else if (strcasecmp ("Database", child->key) == 0)
176 status = cf_util_get_string (child, &db->database);
177 else if (strcasecmp ("ConnectTimeout", child->key) == 0)
178 status = cf_util_get_int (child, &db->timeout);
179 else if (strcasecmp ("MasterStats", child->key) == 0)
180 status = cf_util_get_boolean (child, &db->master_stats);
181 else if (strcasecmp ("SlaveStats", child->key) == 0)
182 status = cf_util_get_boolean (child, &db->slave_stats);
183 else if (strcasecmp ("SlaveNotifications", child->key) == 0)
184 status = cf_util_get_boolean (child, &db->slave_notif);
185 else if (strcasecmp ("InnodbStats", child->key) == 0)
186 status = cf_util_get_boolean (child, &db->innodb_stats);
189 WARNING ("mysql plugin: Option `%s' not allowed here.", child->key);
197 /* If all went well, register this database for reading */
201 char cb_name[DATA_MAX_NAME_LEN];
203 DEBUG ("mysql plugin: Registering new read callback: %s",
204 (db->database != NULL) ? db->database : "<default>");
206 memset (&ud, 0, sizeof (ud));
207 ud.data = (void *) db;
208 ud.free_func = mysql_database_free;
210 if (db->instance != NULL)
211 ssnprintf (cb_name, sizeof (cb_name), "mysql-%s",
214 sstrncpy (cb_name, "mysql", sizeof (cb_name));
216 plugin_register_complex_read (/* group = */ NULL, cb_name,
218 /* interval = */ NULL, &ud);
222 mysql_database_free (db);
227 } /* }}} int mysql_config_database */
229 static int mysql_config (oconfig_item_t *ci) /* {{{ */
236 /* Fill the `mysql_database_t' structure.. */
237 for (i = 0; i < ci->children_num; i++)
239 oconfig_item_t *child = ci->children + i;
241 if (strcasecmp ("Database", child->key) == 0)
242 mysql_config_database (child);
244 WARNING ("mysql plugin: Option \"%s\" not allowed here.",
249 } /* }}} int mysql_config */
251 /* }}} End of configuration handling functions */
253 static MYSQL *getconnection (mysql_database_t *db)
255 if (db->is_connected)
259 status = mysql_ping (db->con);
263 WARNING ("mysql plugin: Lost connection to instance \"%s\": %s",
264 db->instance, mysql_error (db->con));
266 db->is_connected = 0;
270 db->con = mysql_init (NULL);
273 ERROR ("mysql plugin: mysql_init failed: %s",
274 mysql_error (db->con));
279 /* Configure TCP connect timeout (default: 0) */
280 db->con->options.connect_timeout = db->timeout;
282 if (mysql_real_connect (db->con, db->host, db->user, db->pass,
283 db->database, db->port, db->socket, 0) == NULL)
285 ERROR ("mysql plugin: Failed to connect to database %s "
287 (db->database != NULL) ? db->database : "<none>",
288 (db->host != NULL) ? db->host : "localhost",
289 mysql_error (db->con));
293 INFO ("mysql plugin: Successfully connected to database %s "
294 "at server %s (server version: %s, protocol version: %d)",
295 (db->database != NULL) ? db->database : "<none>",
296 mysql_get_host_info (db->con),
297 mysql_get_server_info (db->con),
298 mysql_get_proto_info (db->con));
300 db->is_connected = 1;
302 } /* static MYSQL *getconnection (mysql_database_t *db) */
304 static void set_host (mysql_database_t *db, char *buf, size_t buflen)
307 sstrncpy (buf, db->alias, buflen);
308 else if ((db->host == NULL)
309 || (strcmp ("", db->host) == 0)
310 || (strcmp ("127.0.0.1", db->host) == 0)
311 || (strcmp ("localhost", db->host) == 0))
312 sstrncpy (buf, hostname_g, buflen);
314 sstrncpy (buf, db->host, buflen);
315 } /* void set_host */
317 static void submit (const char *type, const char *type_instance,
318 value_t *values, size_t values_len, mysql_database_t *db)
320 value_list_t vl = VALUE_LIST_INIT;
323 vl.values_len = values_len;
325 set_host (db, vl.host, sizeof (vl.host));
327 sstrncpy (vl.plugin, "mysql", sizeof (vl.plugin));
329 /* Assured by "mysql_config_database" */
330 assert (db->instance != NULL);
331 sstrncpy (vl.plugin_instance, db->instance, sizeof (vl.plugin_instance));
333 sstrncpy (vl.type, type, sizeof (vl.type));
334 if (type_instance != NULL)
335 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
337 plugin_dispatch_values (&vl);
340 static void counter_submit (const char *type, const char *type_instance,
341 derive_t value, mysql_database_t *db)
345 values[0].derive = value;
346 submit (type, type_instance, values, STATIC_ARRAY_SIZE (values), db);
347 } /* void counter_submit */
349 static void gauge_submit (const char *type, const char *type_instance,
350 gauge_t value, mysql_database_t *db)
354 values[0].gauge = value;
355 submit (type, type_instance, values, STATIC_ARRAY_SIZE (values), db);
356 } /* void gauge_submit */
358 static void derive_submit (const char *type, const char *type_instance,
359 derive_t value, mysql_database_t *db)
363 values[0].derive = value;
364 submit (type, type_instance, values, STATIC_ARRAY_SIZE (values), db);
365 } /* void derive_submit */
367 static void traffic_submit (derive_t rx, derive_t tx, mysql_database_t *db)
371 values[0].derive = rx;
372 values[1].derive = tx;
374 submit ("mysql_octets", NULL, values, STATIC_ARRAY_SIZE (values), db);
375 } /* void traffic_submit */
377 static MYSQL_RES *exec_query (MYSQL *con, const char *query)
381 int query_len = strlen (query);
383 if (mysql_real_query (con, query, query_len))
385 ERROR ("mysql plugin: Failed to execute query: %s",
387 INFO ("mysql plugin: SQL query was: %s", query);
391 res = mysql_store_result (con);
394 ERROR ("mysql plugin: Failed to store query result: %s",
396 INFO ("mysql plugin: SQL query was: %s", query);
403 static int mysql_read_master_stats (mysql_database_t *db, MYSQL *con)
410 unsigned long long position;
412 query = "SHOW MASTER STATUS";
414 res = exec_query (con, query);
418 row = mysql_fetch_row (res);
421 ERROR ("mysql plugin: Failed to get master statistics: "
422 "`%s' did not return any rows.", query);
423 mysql_free_result (res);
427 field_num = mysql_num_fields (res);
430 ERROR ("mysql plugin: Failed to get master statistics: "
431 "`%s' returned less than two columns.", query);
432 mysql_free_result (res);
436 position = atoll (row[1]);
437 counter_submit ("mysql_log_position", "master-bin", position, db);
439 row = mysql_fetch_row (res);
441 WARNING ("mysql plugin: `%s' returned more than one row - "
442 "ignoring further results.", query);
444 mysql_free_result (res);
447 } /* mysql_read_master_stats */
449 static int mysql_read_slave_stats (mysql_database_t *db, MYSQL *con)
457 /* WTF? libmysqlclient does not seem to provide any means to
458 * translate a column name to a column index ... :-/ */
459 const int READ_MASTER_LOG_POS_IDX = 6;
460 const int SLAVE_IO_RUNNING_IDX = 10;
461 const int SLAVE_SQL_RUNNING_IDX = 11;
462 const int EXEC_MASTER_LOG_POS_IDX = 21;
463 const int SECONDS_BEHIND_MASTER_IDX = 32;
465 query = "SHOW SLAVE STATUS";
467 res = exec_query (con, query);
471 row = mysql_fetch_row (res);
474 ERROR ("mysql plugin: Failed to get slave statistics: "
475 "`%s' did not return any rows.", query);
476 mysql_free_result (res);
480 field_num = mysql_num_fields (res);
483 ERROR ("mysql plugin: Failed to get slave statistics: "
484 "`%s' returned less than 33 columns.", query);
485 mysql_free_result (res);
491 unsigned long long counter;
494 counter = atoll (row[READ_MASTER_LOG_POS_IDX]);
495 counter_submit ("mysql_log_position", "slave-read", counter, db);
497 counter = atoll (row[EXEC_MASTER_LOG_POS_IDX]);
498 counter_submit ("mysql_log_position", "slave-exec", counter, db);
500 if (row[SECONDS_BEHIND_MASTER_IDX] != NULL)
502 gauge = atof (row[SECONDS_BEHIND_MASTER_IDX]);
503 gauge_submit ("time_offset", NULL, gauge, db);
509 notification_t n = { 0, cdtime (), "", "",
510 "mysql", "", "time_offset", "", NULL };
514 io = row[SLAVE_IO_RUNNING_IDX];
515 sql = row[SLAVE_SQL_RUNNING_IDX];
517 set_host (db, n.host, sizeof (n.host));
519 /* Assured by "mysql_config_database" */
520 assert (db->instance != NULL);
521 sstrncpy (n.plugin_instance, db->instance, sizeof (n.plugin_instance));
523 if (((io == NULL) || (strcasecmp (io, "yes") != 0))
524 && (db->slave_io_running))
526 n.severity = NOTIF_WARNING;
527 ssnprintf (n.message, sizeof (n.message),
528 "slave I/O thread not started or not connected to master");
529 plugin_dispatch_notification (&n);
530 db->slave_io_running = 0;
532 else if (((io != NULL) && (strcasecmp (io, "yes") == 0))
533 && (! db->slave_io_running))
535 n.severity = NOTIF_OKAY;
536 ssnprintf (n.message, sizeof (n.message),
537 "slave I/O thread started and connected to master");
538 plugin_dispatch_notification (&n);
539 db->slave_io_running = 1;
542 if (((sql == NULL) || (strcasecmp (sql, "yes") != 0))
543 && (db->slave_sql_running))
545 n.severity = NOTIF_WARNING;
546 ssnprintf (n.message, sizeof (n.message),
547 "slave SQL thread not started");
548 plugin_dispatch_notification (&n);
549 db->slave_sql_running = 0;
551 else if (((sql != NULL) && (strcasecmp (sql, "yes") == 0))
552 && (! db->slave_sql_running))
554 n.severity = NOTIF_OKAY;
555 ssnprintf (n.message, sizeof (n.message),
556 "slave SQL thread started");
557 plugin_dispatch_notification (&n);
558 db->slave_sql_running = 1;
562 row = mysql_fetch_row (res);
564 WARNING ("mysql plugin: `%s' returned more than one row - "
565 "ignoring further results.", query);
567 mysql_free_result (res);
570 } /* mysql_read_slave_stats */
572 static int mysql_read (user_data_t *ud)
574 mysql_database_t *db;
580 derive_t qcache_hits = 0;
581 derive_t qcache_inserts = 0;
582 derive_t qcache_not_cached = 0;
583 derive_t qcache_lowmem_prunes = 0;
584 gauge_t qcache_queries_in_cache = NAN;
586 gauge_t threads_running = NAN;
587 gauge_t threads_connected = NAN;
588 gauge_t threads_cached = NAN;
589 derive_t threads_created = 0;
591 unsigned long long traffic_incoming = 0ULL;
592 unsigned long long traffic_outgoing = 0ULL;
594 if ((ud == NULL) || (ud->data == NULL))
596 ERROR ("mysql plugin: mysql_database_read: Invalid user data.");
600 db = (mysql_database_t *) ud->data;
602 /* An error message will have been printed in this case */
603 if ((con = getconnection (db)) == NULL)
606 query = "SHOW STATUS";
607 if (mysql_get_server_version (con) >= 50002)
608 query = "SHOW GLOBAL STATUS";
610 res = exec_query (con, query);
614 while ((row = mysql_fetch_row (res)))
617 unsigned long long val;
620 val = atoll (row[1]);
622 if (strncmp (key, "Com_",
623 strlen ("Com_")) == 0)
628 /* Ignore `prepared statements' */
629 if (strncmp (key, "Com_stmt_", strlen ("Com_stmt_")) != 0)
630 counter_submit ("mysql_commands",
631 key + strlen ("Com_"),
634 else if (strncmp (key, "Handler_",
635 strlen ("Handler_")) == 0)
640 counter_submit ("mysql_handler",
641 key + strlen ("Handler_"),
644 else if (strncmp (key, "Qcache_",
645 strlen ("Qcache_")) == 0)
647 if (strcmp (key, "Qcache_hits") == 0)
648 qcache_hits = (derive_t) val;
649 else if (strcmp (key, "Qcache_inserts") == 0)
650 qcache_inserts = (derive_t) val;
651 else if (strcmp (key, "Qcache_not_cached") == 0)
652 qcache_not_cached = (derive_t) val;
653 else if (strcmp (key, "Qcache_lowmem_prunes") == 0)
654 qcache_lowmem_prunes = (derive_t) val;
655 else if (strcmp (key, "Qcache_queries_in_cache") == 0)
656 qcache_queries_in_cache = (gauge_t) val;
658 else if (strncmp (key, "Bytes_",
659 strlen ("Bytes_")) == 0)
661 if (strcmp (key, "Bytes_received") == 0)
662 traffic_incoming += val;
663 else if (strcmp (key, "Bytes_sent") == 0)
664 traffic_outgoing += val;
666 else if (strncmp (key, "Threads_",
667 strlen ("Threads_")) == 0)
669 if (strcmp (key, "Threads_running") == 0)
670 threads_running = (gauge_t) val;
671 else if (strcmp (key, "Threads_connected") == 0)
672 threads_connected = (gauge_t) val;
673 else if (strcmp (key, "Threads_cached") == 0)
674 threads_cached = (gauge_t) val;
675 else if (strcmp (key, "Threads_created") == 0)
676 threads_created = (derive_t) val;
678 else if (strncmp (key, "Table_locks_",
679 strlen ("Table_locks_")) == 0)
681 counter_submit ("mysql_locks",
682 key + strlen ("Table_locks_"),
685 else if (db->innodb_stats && strncmp (key, "Innodb_", strlen ("Innodb_")) == 0)
688 if (strcmp (key, "Innodb_buffer_pool_pages_data") == 0)
689 gauge_submit ("mysql_bpool_pages", "data", val, db);
690 else if (strcmp (key, "Innodb_buffer_pool_pages_dirty") == 0)
691 gauge_submit ("mysql_bpool_pages", "dirty", val, db);
692 else if (strcmp (key, "Innodb_buffer_pool_pages_flushed") == 0)
693 counter_submit ("mysql_bpool_counters", "flushed", val, db);
694 else if (strcmp (key, "Innodb_buffer_pool_pages_free") == 0)
695 gauge_submit ("mysql_bpool_pages", "free", val, db);
696 else if (strcmp (key, "Innodb_buffer_pool_pages_misc") == 0)
697 gauge_submit ("mysql_bpool_pages", "misc", val, db);
698 else if (strcmp (key, "Innodb_buffer_pool_pages_total") == 0)
699 gauge_submit ("mysql_bpool_pages", "total", val, db);
700 else if (strcmp (key, "Innodb_buffer_pool_read_ahead_rnd") == 0)
701 counter_submit ("mysql_bpool_counters", "read_ahead_rnd", val, db);
702 else if (strcmp (key, "Innodb_buffer_pool_read_ahead") == 0)
703 counter_submit ("mysql_bpool_counters", "read_ahead", val, db);
704 else if (strcmp (key, "Innodb_buffer_pool_read_ahead_evicted") == 0)
705 counter_submit ("mysql_bpool_counters", "read_ahead_evicted", val, db);
706 else if (strcmp (key, "Innodb_buffer_pool_read_requests") == 0)
707 counter_submit ("mysql_bpool_counters", "read_requests", val, db);
708 else if (strcmp (key, "Innodb_buffer_pool_reads") == 0)
709 counter_submit ("mysql_bpool_counters", "reads", val, db);
710 else if (strcmp (key, "Innodb_buffer_pool_write_requests") == 0)
711 counter_submit ("mysql_bpool_counters", "write_requests", val, db);
714 if (strcmp (key, "Innodb_data_fsyncs") == 0)
715 counter_submit ("mysql_innodb_data", "fsyncs", val, db);
716 else if (strcmp (key, "Innodb_data_read") == 0)
717 counter_submit ("mysql_innodb_data", "read", val, db);
718 else if (strcmp (key, "Innodb_data_reads") == 0)
719 counter_submit ("mysql_innodb_data", "reads", val, db);
720 else if (strcmp (key, "Innodb_data_writes") == 0)
721 counter_submit ("mysql_bpool_counters", "writes", val, db);
722 else if (strcmp (key, "Innodb_data_written") == 0)
723 counter_submit ("mysql_innodb_data", "written", val, db);
726 else if (strcmp (key, "Innodb_dblwr_writes") == 0)
727 counter_submit ("mysql_innodb_dblwr", "writes", val, db);
728 else if (strcmp (key, "Innodb_dblwr_pages_written") == 0)
729 counter_submit ("mysql_innodb_dblwr", "written", val, db);
732 else if (strcmp (key, "Innodb_rows_deleted") == 0)
733 counter_submit ("mysql_innodb_rows", "deleted", val, db);
734 else if (strcmp (key, "Innodb_rows_inserted") == 0)
735 counter_submit ("mysql_innodb_rows", "inserted", val, db);
736 else if (strcmp (key, "Innodb_rows_read") == 0)
737 counter_submit ("mysql_innodb_rows", "read", val, db);
738 else if (strcmp (key, "Innodb_rows_updated") == 0)
739 counter_submit ("mysql_innodb_rows", "updated", val, db);
742 mysql_free_result (res); res = NULL;
744 if ((qcache_hits != 0)
745 || (qcache_inserts != 0)
746 || (qcache_not_cached != 0)
747 || (qcache_lowmem_prunes != 0))
749 derive_submit ("cache_result", "qcache-hits",
751 derive_submit ("cache_result", "qcache-inserts",
753 derive_submit ("cache_result", "qcache-not_cached",
754 qcache_not_cached, db);
755 derive_submit ("cache_result", "qcache-prunes",
756 qcache_lowmem_prunes, db);
758 gauge_submit ("cache_size", "qcache",
759 qcache_queries_in_cache, db);
762 if (threads_created != 0)
764 gauge_submit ("threads", "running",
765 threads_running, db);
766 gauge_submit ("threads", "connected",
767 threads_connected, db);
768 gauge_submit ("threads", "cached",
771 derive_submit ("total_threads", "created",
772 threads_created, db);
775 traffic_submit (traffic_incoming, traffic_outgoing, db);
777 if (db->master_stats)
778 mysql_read_master_stats (db, con);
780 if ((db->slave_stats) || (db->slave_notif))
781 mysql_read_slave_stats (db, con);
784 } /* int mysql_read */
786 void module_register (void)
788 plugin_register_complex_config ("mysql", mysql_config);
789 } /* void module_register */