2 * collectd - src/mysql.c
3 * Copyright (C) 2006-2009 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 verplant.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 /* {{{ */
45 /* instance == NULL => legacy mode */
59 int slave_sql_running;
64 typedef struct mysql_database_s mysql_database_t; /* }}} */
66 static int mysql_read (user_data_t *ud);
68 static void mysql_database_free (void *arg) /* {{{ */
72 DEBUG ("mysql plugin: mysql_database_free (arg = %p);", arg);
74 db = (mysql_database_t *) arg;
80 mysql_close (db->con);
89 } /* }}} void mysql_database_free */
91 /* Configuration handling functions {{{
94 * <Database "plugin_instance1">
102 static int mysql_config_set_string (char **ret_string, /* {{{ */
107 if ((ci->values_num != 1)
108 || (ci->values[0].type != OCONFIG_TYPE_STRING))
110 WARNING ("mysql plugin: The `%s' config option "
111 "needs exactly one string argument.", ci->key);
115 string = strdup (ci->values[0].value.string);
118 ERROR ("mysql plugin: strdup failed.");
122 if (*ret_string != NULL)
124 *ret_string = string;
127 } /* }}} int mysql_config_set_string */
129 static int mysql_config_set_int (int *ret_int, /* {{{ */
132 if ((ci->values_num != 1)
133 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
135 WARNING ("mysql plugin: The `%s' config option "
136 "needs exactly one string argument.", ci->key);
140 *ret_int = ci->values[0].value.number;
143 } /* }}} int mysql_config_set_int */
145 static int mysql_config_set_boolean (int *ret_boolean, /* {{{ */
150 if (ci->values_num != 1)
155 if (ci->values[0].type == OCONFIG_TYPE_BOOLEAN)
156 *ret_boolean = ci->values[0].value.boolean;
157 else if (ci->values[0].type == OCONFIG_TYPE_STRING)
159 if (IS_TRUE (ci->values[0].value.string))
161 else if (IS_FALSE (ci->values[0].value.string))
172 WARNING ("mysql plugin: The `%s' config option "
173 "needs exactly one boolean argument.", ci->key);
177 } /* }}} mysql_config_set_boolean */
179 static int mysql_config (oconfig_item_t *ci) /* {{{ */
181 mysql_database_t *db;
186 if ((ci->values_num != 1)
187 || (ci->values[0].type != OCONFIG_TYPE_STRING))
189 WARNING ("mysql plugin: The `Database' block "
190 "needs exactly one string argument.");
194 db = (mysql_database_t *) malloc (sizeof (*db));
197 ERROR ("mysql plugin: malloc failed.");
200 memset (db, 0, sizeof (*db));
202 /* initialize all the pointers */
210 /* trigger a notification, if it's not running */
211 db->slave_io_running = 1;
212 db->slave_sql_running = 1;
215 if (strcasecmp ("Plugin", ci->key) == 0)
219 else if (strcasecmp ("Database", ci->key) == 0)
222 status = mysql_config_set_string (&db->instance, ci);
228 assert (db->instance != NULL);
232 ERROR ("mysql plugin: mysql_config: "
233 "Invalid key: %s", ci->key);
237 /* Fill the `mysql_database_t' structure.. */
238 for (i = 0; i < ci->children_num; i++)
240 oconfig_item_t *child = ci->children + i;
242 if (strcasecmp ("Host", child->key) == 0)
243 status = mysql_config_set_string (&db->host, child);
244 else if (strcasecmp ("User", child->key) == 0)
245 status = mysql_config_set_string (&db->user, child);
246 else if (strcasecmp ("Password", child->key) == 0)
247 status = mysql_config_set_string (&db->pass, child);
248 else if (strcasecmp ("Port", child->key) == 0)
249 status = mysql_config_set_int (&db->port, child);
250 else if (strcasecmp ("Socket", child->key) == 0)
251 status = mysql_config_set_string (&db->socket, child);
252 /* Check if we're currently handling the `Plugin' block. If so,
253 * handle `Database' _blocks_, too. */
254 else if ((plugin_block != 0)
255 && (strcasecmp ("Database", child->key) == 0)
256 && (child->children != NULL))
258 /* If `plugin_block > 1', there has been at least one
259 * `Database' block */
261 status = mysql_config (child);
263 /* Now handle ordinary `Database' options (without children) */
264 else if ((strcasecmp ("Database", child->key) == 0)
265 && (child->children == NULL))
266 status = mysql_config_set_string (&db->database, child);
267 else if (strcasecmp ("MasterStats", child->key) == 0)
268 status = mysql_config_set_boolean (&db->master_stats, child);
269 else if (strcasecmp ("SlaveStats", child->key) == 0)
270 status = mysql_config_set_boolean (&db->slave_stats, child);
271 else if (strcasecmp ("SlaveNotifications", child->key) == 0)
272 status = mysql_config_set_boolean (&db->slave_notif, child);
275 WARNING ("mysql plugin: Option `%s' not allowed here.", child->key);
283 /* Check if there were any `Database' blocks. */
284 if (plugin_block > 1)
286 /* There were connection blocks. Don't use any legacy stuff. */
287 if ((db->host != NULL)
288 || (db->user != NULL)
289 || (db->pass != NULL)
290 || (db->database != NULL)
291 || (db->socket != NULL)
294 WARNING ("mysql plugin: At least one <Database> "
295 "block has been found. The legacy "
296 "configuration will be ignored.");
298 mysql_database_free (db);
301 else if (plugin_block != 0)
303 WARNING ("mysql plugin: You're using the legacy "
304 "configuration options. Please consider "
305 "updating your configuration!");
308 /* Check that all necessary options have been given. */
311 /* Zero is allowed and automatically handled by
312 * `mysql_real_connect'. */
313 if ((db->port < 0) || (db->port > 65535))
315 ERROR ("mysql plugin: Database %s: Port number out "
317 (db->instance != NULL)
324 } /* while (status == 0) */
326 /* If all went well, register this database for reading */
330 char cb_name[DATA_MAX_NAME_LEN];
332 DEBUG ("mysql plugin: Registering new read callback: %s",
333 (db->database != NULL) ? db->database : "<default>");
335 memset (&ud, 0, sizeof (ud));
336 ud.data = (void *) db;
337 ud.free_func = mysql_database_free;
339 if (db->database != NULL)
340 ssnprintf (cb_name, sizeof (cb_name), "mysql-%s",
343 sstrncpy (cb_name, "mysql", sizeof (cb_name));
345 plugin_register_complex_read (/* group = */ NULL, cb_name,
347 /* interval = */ NULL, &ud);
351 mysql_database_free (db);
356 } /* }}} int mysql_config */
358 /* }}} End of configuration handling functions */
360 static MYSQL *getconnection (mysql_database_t *db)
362 if (db->is_connected)
366 status = mysql_ping (db->con);
370 WARNING ("mysql plugin: Lost connection to instance \"%s\": %s",
371 (db->instance != NULL)
374 mysql_error (db->con));
376 db->is_connected = 0;
380 db->con = mysql_init (NULL);
383 ERROR ("mysql plugin: mysql_init failed: %s",
384 mysql_error (db->con));
389 if (mysql_real_connect (db->con, db->host, db->user, db->pass,
390 db->database, db->port, db->socket, 0) == NULL)
392 ERROR ("mysql plugin: Failed to connect to database %s "
394 (db->database != NULL) ? db->database : "<none>",
395 (db->host != NULL) ? db->host : "localhost",
396 mysql_error (db->con));
400 INFO ("mysql plugin: Successfully connected to database %s "
401 "at server %s (server version: %s, protocol version: %d)",
402 (db->database != NULL) ? db->database : "<none>",
403 mysql_get_host_info (db->con),
404 mysql_get_server_info (db->con),
405 mysql_get_proto_info (db->con));
407 db->is_connected = 1;
409 } /* static MYSQL *getconnection (mysql_database_t *db) */
411 static void set_host (mysql_database_t *db, char *buf, size_t buflen)
413 /* XXX legacy mode - use hostname_g */
414 if (db->instance == NULL)
415 sstrncpy (buf, hostname_g, buflen);
418 if ((db->host == NULL)
419 || (strcmp ("", db->host) == 0)
420 || (strcmp ("localhost", db->host) == 0))
421 sstrncpy (buf, hostname_g, buflen);
423 sstrncpy (buf, db->host, buflen);
427 static void set_plugin_instance (mysql_database_t *db,
428 char *buf, size_t buflen)
430 /* XXX legacy mode - no plugin_instance */
431 if (db->instance == NULL)
432 sstrncpy (buf, "", buflen);
434 sstrncpy (buf, db->instance, buflen);
437 static void submit (const char *type, const char *type_instance,
438 value_t *values, size_t values_len, mysql_database_t *db)
440 value_list_t vl = VALUE_LIST_INIT;
443 vl.values_len = values_len;
445 set_host (db, vl.host, sizeof (vl.host));
447 sstrncpy (vl.plugin, "mysql", sizeof (vl.plugin));
448 set_plugin_instance (db, vl.plugin_instance, sizeof (vl.plugin_instance));
450 sstrncpy (vl.type, type, sizeof (vl.type));
451 if (type_instance != NULL)
452 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
454 plugin_dispatch_values (&vl);
457 static void counter_submit (const char *type, const char *type_instance,
458 counter_t value, mysql_database_t *db)
462 values[0].counter = value;
463 submit (type, type_instance, values, STATIC_ARRAY_SIZE (values), db);
464 } /* void counter_submit */
466 static void gauge_submit (const char *type, const char *type_instance,
467 gauge_t value, mysql_database_t *db)
471 values[0].gauge = value;
472 submit (type, type_instance, values, STATIC_ARRAY_SIZE (values), db);
473 } /* void gauge_submit */
475 static void qcache_submit (counter_t hits, counter_t inserts,
476 counter_t not_cached, counter_t lowmem_prunes,
477 gauge_t queries_in_cache, mysql_database_t *db)
481 values[0].counter = hits;
482 values[1].counter = inserts;
483 values[2].counter = not_cached;
484 values[3].counter = lowmem_prunes;
485 values[4].gauge = queries_in_cache;
487 submit ("mysql_qcache", NULL, values, STATIC_ARRAY_SIZE (values), db);
488 } /* void qcache_submit */
490 static void threads_submit (gauge_t running, gauge_t connected, gauge_t cached,
491 counter_t created, mysql_database_t *db)
495 values[0].gauge = running;
496 values[1].gauge = connected;
497 values[2].gauge = cached;
498 values[3].counter = created;
500 submit ("mysql_threads", NULL, values, STATIC_ARRAY_SIZE (values), db);
501 } /* void threads_submit */
503 static void traffic_submit (counter_t rx, counter_t tx, mysql_database_t *db)
507 values[0].counter = rx;
508 values[1].counter = tx;
510 submit ("mysql_octets", NULL, values, STATIC_ARRAY_SIZE (values), db);
511 } /* void traffic_submit */
513 static MYSQL_RES *exec_query (MYSQL *con, const char *query)
517 int query_len = strlen (query);
519 if (mysql_real_query (con, query, query_len))
521 ERROR ("mysql plugin: Failed to execute query: %s",
523 INFO ("mysql plugin: SQL query was: %s", query);
527 res = mysql_store_result (con);
530 ERROR ("mysql plugin: Failed to store query result: %s",
532 INFO ("mysql plugin: SQL query was: %s", query);
539 static int mysql_read_master_stats (mysql_database_t *db, MYSQL *con)
546 unsigned long long position;
548 query = "SHOW MASTER STATUS";
550 res = exec_query (con, query);
554 row = mysql_fetch_row (res);
557 ERROR ("mysql plugin: Failed to get master statistics: "
558 "`%s' did not return any rows.", query);
559 mysql_free_result (res);
563 field_num = mysql_num_fields (res);
566 ERROR ("mysql plugin: Failed to get master statistics: "
567 "`%s' returned less than two columns.", query);
568 mysql_free_result (res);
572 position = atoll (row[1]);
573 counter_submit ("mysql_log_position", "master-bin", position, db);
575 row = mysql_fetch_row (res);
577 WARNING ("mysql plugin: `%s' returned more than one row - "
578 "ignoring further results.", query);
580 mysql_free_result (res);
583 } /* mysql_read_master_stats */
585 static int mysql_read_slave_stats (mysql_database_t *db, MYSQL *con)
593 /* WTF? libmysqlclient does not seem to provide any means to
594 * translate a column name to a column index ... :-/ */
595 const int READ_MASTER_LOG_POS_IDX = 6;
596 const int SLAVE_IO_RUNNING_IDX = 10;
597 const int SLAVE_SQL_RUNNING_IDX = 11;
598 const int EXEC_MASTER_LOG_POS_IDX = 21;
599 const int SECONDS_BEHIND_MASTER_IDX = 32;
601 query = "SHOW SLAVE STATUS";
603 res = exec_query (con, query);
607 row = mysql_fetch_row (res);
610 ERROR ("mysql plugin: Failed to get slave statistics: "
611 "`%s' did not return any rows.", query);
612 mysql_free_result (res);
616 field_num = mysql_num_fields (res);
619 ERROR ("mysql plugin: Failed to get slave statistics: "
620 "`%s' returned less than 33 columns.", query);
621 mysql_free_result (res);
627 unsigned long long counter;
630 counter = atoll (row[READ_MASTER_LOG_POS_IDX]);
631 counter_submit ("mysql_log_position", "slave-read", counter, db);
633 counter = atoll (row[EXEC_MASTER_LOG_POS_IDX]);
634 counter_submit ("mysql_log_position", "slave-exec", counter, db);
636 if (row[SECONDS_BEHIND_MASTER_IDX] != NULL)
638 gauge = atof (row[SECONDS_BEHIND_MASTER_IDX]);
639 gauge_submit ("time_offset", NULL, gauge, db);
645 notification_t n = { 0, time (NULL), "", "",
646 "mysql", "", "time_offset", "", NULL };
650 io = row[SLAVE_IO_RUNNING_IDX];
651 sql = row[SLAVE_SQL_RUNNING_IDX];
653 set_host (db, n.host, sizeof (n.host));
654 set_plugin_instance (db,
655 n.plugin_instance, sizeof (n.plugin_instance));
657 if (((io == NULL) || (strcasecmp (io, "yes") != 0))
658 && (db->slave_io_running))
660 n.severity = NOTIF_WARNING;
661 ssnprintf (n.message, sizeof (n.message),
662 "slave I/O thread not started or not connected to master");
663 plugin_dispatch_notification (&n);
664 db->slave_io_running = 0;
666 else if (((io != NULL) && (strcasecmp (io, "yes") == 0))
667 && (! db->slave_io_running))
669 n.severity = NOTIF_OKAY;
670 ssnprintf (n.message, sizeof (n.message),
671 "slave I/O thread started and connected to master");
672 plugin_dispatch_notification (&n);
673 db->slave_io_running = 1;
676 if (((sql == NULL) || (strcasecmp (sql, "yes") != 0))
677 && (db->slave_sql_running))
679 n.severity = NOTIF_WARNING;
680 ssnprintf (n.message, sizeof (n.message),
681 "slave SQL thread not started");
682 plugin_dispatch_notification (&n);
683 db->slave_sql_running = 0;
685 else if (((sql != NULL) && (strcasecmp (sql, "yes") == 0))
686 && (! db->slave_sql_running))
688 n.severity = NOTIF_OKAY;
689 ssnprintf (n.message, sizeof (n.message),
690 "slave SQL thread started");
691 plugin_dispatch_notification (&n);
692 db->slave_sql_running = 0;
696 row = mysql_fetch_row (res);
698 WARNING ("mysql plugin: `%s' returned more than one row - "
699 "ignoring further results.", query);
701 mysql_free_result (res);
704 } /* mysql_read_slave_stats */
706 static int mysql_read (user_data_t *ud)
708 mysql_database_t *db;
714 unsigned long long qcache_hits = 0ULL;
715 unsigned long long qcache_inserts = 0ULL;
716 unsigned long long qcache_not_cached = 0ULL;
717 unsigned long long qcache_lowmem_prunes = 0ULL;
718 int qcache_queries_in_cache = -1;
720 int threads_running = -1;
721 int threads_connected = -1;
722 int threads_cached = -1;
723 unsigned long long threads_created = 0ULL;
725 unsigned long long traffic_incoming = 0ULL;
726 unsigned long long traffic_outgoing = 0ULL;
728 if ((ud == NULL) || (ud->data == NULL))
730 ERROR ("mysql plugin: mysql_database_read: Invalid user data.");
734 db = (mysql_database_t *) ud->data;
736 /* An error message will have been printed in this case */
737 if ((con = getconnection (db)) == NULL)
740 query = "SHOW STATUS";
741 if (mysql_get_server_version (con) >= 50002)
742 query = "SHOW GLOBAL STATUS";
744 res = exec_query (con, query);
748 while ((row = mysql_fetch_row (res)))
751 unsigned long long val;
754 val = atoll (row[1]);
756 if (strncmp (key, "Com_",
757 strlen ("Com_")) == 0)
762 /* Ignore `prepared statements' */
763 if (strncmp (key, "Com_stmt_", strlen ("Com_stmt_")) != 0)
764 counter_submit ("mysql_commands",
765 key + strlen ("Com_"),
768 else if (strncmp (key, "Handler_",
769 strlen ("Handler_")) == 0)
774 counter_submit ("mysql_handler",
775 key + strlen ("Handler_"),
778 else if (strncmp (key, "Qcache_",
779 strlen ("Qcache_")) == 0)
781 if (strcmp (key, "Qcache_hits") == 0)
783 else if (strcmp (key, "Qcache_inserts") == 0)
784 qcache_inserts = val;
785 else if (strcmp (key, "Qcache_not_cached") == 0)
786 qcache_not_cached = val;
787 else if (strcmp (key, "Qcache_lowmem_prunes") == 0)
788 qcache_lowmem_prunes = val;
789 else if (strcmp (key, "Qcache_queries_in_cache") == 0)
790 qcache_queries_in_cache = (int) val;
792 else if (strncmp (key, "Bytes_",
793 strlen ("Bytes_")) == 0)
795 if (strcmp (key, "Bytes_received") == 0)
796 traffic_incoming += val;
797 else if (strcmp (key, "Bytes_sent") == 0)
798 traffic_outgoing += val;
800 else if (strncmp (key, "Threads_",
801 strlen ("Threads_")) == 0)
803 if (strcmp (key, "Threads_running") == 0)
804 threads_running = (int) val;
805 else if (strcmp (key, "Threads_connected") == 0)
806 threads_connected = (int) val;
807 else if (strcmp (key, "Threads_cached") == 0)
808 threads_cached = (int) val;
809 else if (strcmp (key, "Threads_created") == 0)
810 threads_created = val;
812 else if (strncmp (key, "Table_locks_",
813 strlen ("Table_locks_")) == 0)
815 counter_submit ("mysql_locks",
816 key + strlen ("Table_locks_"),
820 mysql_free_result (res); res = NULL;
822 if ((qcache_hits != 0ULL)
823 || (qcache_inserts != 0ULL)
824 || (qcache_not_cached != 0ULL)
825 || (qcache_lowmem_prunes != 0ULL))
826 qcache_submit (qcache_hits, qcache_inserts, qcache_not_cached,
827 qcache_lowmem_prunes, qcache_queries_in_cache, db);
829 if (threads_created != 0ULL)
830 threads_submit (threads_running, threads_connected,
831 threads_cached, threads_created, db);
833 traffic_submit (traffic_incoming, traffic_outgoing, db);
835 if (db->master_stats)
836 mysql_read_master_stats (db, con);
838 if ((db->slave_stats) || (db->slave_notif))
839 mysql_read_slave_stats (db, con);
842 } /* int mysql_read */
844 void module_register (void)
846 plugin_register_complex_config ("mysql", mysql_config);
847 } /* void module_register */