2 * collectd - src/mysql.c
3 * Copyright (C) 2006-2009 Florian octo Forster
4 * Copyright (C) 2009 Doug MacEachern
5 * Copyright (C) 2009 Sebastian tokkee Harl
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; only version 2 of the License is applicable.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 * Florian octo Forster <octo at verplant.org>
22 * Mirko Buffoni <briareos at eswat.org>
23 * Doug MacEachern <dougm at hyperic.com>
24 * Sebastian tokkee Harl <sh at tokkee.org>
30 #include "configfile.h"
34 #elif defined(HAVE_MYSQL_MYSQL_H)
35 #include <mysql/mysql.h>
38 /* TODO: Understand `Select_*' and possibly do that stuff as well.. */
40 struct mysql_database_s /* {{{ */
42 /* instance == NULL => legacy mode */
56 int slave_sql_running;
61 typedef struct mysql_database_s mysql_database_t; /* }}} */
63 static int mysql_read (user_data_t *ud);
65 static void mysql_database_free (void *arg) /* {{{ */
69 DEBUG ("mysql plugin: mysql_database_free (arg = %p);", arg);
71 db = (mysql_database_t *) arg;
77 mysql_close (db->con);
86 } /* }}} void mysql_database_free */
88 /* Configuration handling functions {{{
91 * <Database "plugin_instance1">
99 static int mysql_config_set_string (char **ret_string, /* {{{ */
104 if ((ci->values_num != 1)
105 || (ci->values[0].type != OCONFIG_TYPE_STRING))
107 WARNING ("mysql plugin: The `%s' config option "
108 "needs exactly one string argument.", ci->key);
112 string = strdup (ci->values[0].value.string);
115 ERROR ("mysql plugin: strdup failed.");
119 if (*ret_string != NULL)
121 *ret_string = string;
124 } /* }}} int mysql_config_set_string */
126 static int mysql_config_set_int (int *ret_int, /* {{{ */
129 if ((ci->values_num != 1)
130 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
132 WARNING ("mysql plugin: The `%s' config option "
133 "needs exactly one string argument.", ci->key);
137 *ret_int = ci->values[0].value.number;
140 } /* }}} int mysql_config_set_int */
142 static int mysql_config_set_boolean (int *ret_boolean, /* {{{ */
147 if (ci->values_num != 1)
152 if (ci->values[0].type == OCONFIG_TYPE_BOOLEAN)
153 *ret_boolean = ci->values[0].value.boolean;
154 else if (ci->values[0].type == OCONFIG_TYPE_STRING)
156 if (IS_TRUE (ci->values[0].value.string))
158 else if (IS_FALSE (ci->values[0].value.string))
169 WARNING ("mysql plugin: The `%s' config option "
170 "needs exactly one boolean argument.", ci->key);
174 } /* }}} mysql_config_set_boolean */
176 static int mysql_config (oconfig_item_t *ci) /* {{{ */
178 mysql_database_t *db;
183 if ((ci->values_num != 1)
184 || (ci->values[0].type != OCONFIG_TYPE_STRING))
186 WARNING ("mysql plugin: The `Database' block "
187 "needs exactly one string argument.");
191 db = (mysql_database_t *) malloc (sizeof (*db));
194 ERROR ("mysql plugin: malloc failed.");
197 memset (db, 0, sizeof (*db));
199 /* initialize all the pointers */
207 /* trigger a notification, if it's not running */
208 db->slave_io_running = 1;
209 db->slave_sql_running = 1;
212 if (strcasecmp ("Plugin", ci->key) == 0)
216 else if (strcasecmp ("Database", ci->key) == 0)
219 status = mysql_config_set_string (&db->instance, ci);
225 assert (db->instance != NULL);
226 db->database = strdup (db->instance);
230 ERROR ("mysql plugin: mysql_config: "
231 "Invalid key: %s", ci->key);
235 /* Fill the `mysql_database_t' structure.. */
236 for (i = 0; i < ci->children_num; i++)
238 oconfig_item_t *child = ci->children + i;
240 if (strcasecmp ("Host", child->key) == 0)
241 status = mysql_config_set_string (&db->host, child);
242 else if (strcasecmp ("User", child->key) == 0)
243 status = mysql_config_set_string (&db->user, child);
244 else if (strcasecmp ("Password", child->key) == 0)
245 status = mysql_config_set_string (&db->pass, child);
246 else if (strcasecmp ("Port", child->key) == 0)
247 status = mysql_config_set_int (&db->port, child);
248 else if (strcasecmp ("Socket", child->key) == 0)
249 status = mysql_config_set_string (&db->socket, child);
250 /* Check if we're currently handling the `Plugin' block. If so,
251 * handle `Database' _blocks_, too. */
252 else if ((plugin_block != 0)
253 && (strcasecmp ("Database", child->key) == 0)
254 && (child->children != NULL))
256 /* If `plugin_block > 1', there has been at least one
257 * `Database' block */
259 status = mysql_config (child);
261 /* Now handle ordinary `Database' options (without children) */
262 else if ((strcasecmp ("Database", child->key) == 0)
263 && (child->children == NULL))
264 status = mysql_config_set_string (&db->database, child);
265 else if (strcasecmp ("MasterStats", child->key) == 0)
266 status = mysql_config_set_boolean (&db->master_stats, child);
267 else if (strcasecmp ("SlaveStats", child->key) == 0)
268 status = mysql_config_set_boolean (&db->slave_stats, child);
269 else if (strcasecmp ("SlaveNotifications", child->key) == 0)
270 status = mysql_config_set_boolean (&db->slave_notif, child);
273 WARNING ("mysql plugin: Option `%s' not allowed here.", child->key);
281 /* Check if there were any `Database' blocks. */
282 if (plugin_block > 1)
284 /* There were connection blocks. Don't use any legacy stuff. */
285 if ((db->host != NULL)
286 || (db->user != NULL)
287 || (db->pass != NULL)
288 || (db->database != NULL)
289 || (db->socket != NULL)
292 WARNING ("mysql plugin: At least one <Database> "
293 "block has been found. The legacy "
294 "configuration will be ignored.");
296 mysql_database_free (db);
299 else if (plugin_block != 0)
301 WARNING ("mysql plugin: You're using the legacy "
302 "configuration options. Please consider "
303 "updating your configuration!");
306 /* Check that all necessary options have been given. */
309 /* Zero is allowed and automatically handled by
310 * `mysql_real_connect'. */
311 if ((db->port < 0) || (db->port > 65535))
313 ERROR ("mysql plugin: Database %s: Port number out "
315 (db->instance != NULL)
321 if (db->database == NULL)
323 ERROR ("mysql plugin: No `Database' configured");
327 } /* while (status == 0) */
329 /* If all went well, register this database for reading */
333 char cb_name[DATA_MAX_NAME_LEN];
335 DEBUG ("mysql plugin: Registering new read callback: %s", db->database);
337 memset (&ud, 0, sizeof (ud));
338 ud.data = (void *) db;
339 ud.free_func = mysql_database_free;
341 if (db->database != NULL)
342 ssnprintf (cb_name, sizeof (cb_name), "mysql-%s",
345 sstrncpy (cb_name, "mysql", sizeof (cb_name));
347 plugin_register_complex_read (cb_name, mysql_read,
348 /* interval = */ NULL, &ud);
352 mysql_database_free (db);
357 } /* }}} int mysql_config */
359 /* }}} End of configuration handling functions */
361 static MYSQL *getconnection (mysql_database_t *db)
366 if ((err = mysql_ping (db->con)) != 0)
368 WARNING ("mysql_ping failed: %s", mysql_error (db->con));
378 if ((db->con = mysql_init (db->con)) == NULL)
380 ERROR ("mysql_init failed: %s", mysql_error (db->con));
385 if (mysql_real_connect (db->con, db->host, db->user, db->pass,
386 db->database, db->port, db->socket, 0) == NULL)
388 ERROR ("mysql_real_connect failed: %s", mysql_error (db->con));
397 } /* static MYSQL *getconnection (mysql_database_t *db) */
399 static void set_host (mysql_database_t *db, char *buf, size_t buflen)
401 /* XXX legacy mode - use hostname_g */
402 if (db->instance == NULL)
403 sstrncpy (buf, hostname_g, buflen);
406 if ((db->host == NULL)
407 || (strcmp ("", db->host) == 0)
408 || (strcmp ("localhost", db->host) == 0))
409 sstrncpy (buf, hostname_g, buflen);
411 sstrncpy (buf, db->host, buflen);
415 static void set_plugin_instance (mysql_database_t *db,
416 char *buf, size_t buflen)
418 /* XXX legacy mode - no plugin_instance */
419 if (db->instance == NULL)
420 sstrncpy (buf, "", buflen);
422 sstrncpy (buf, db->instance, buflen);
425 static void submit (const char *type, const char *type_instance,
426 value_t *values, size_t values_len, mysql_database_t *db)
428 value_list_t vl = VALUE_LIST_INIT;
431 vl.values_len = values_len;
433 set_host (db, vl.host, sizeof (vl.host));
435 sstrncpy (vl.plugin, "mysql", sizeof (vl.plugin));
436 set_plugin_instance (db, vl.plugin_instance, sizeof (vl.plugin_instance));
438 sstrncpy (vl.type, type, sizeof (vl.type));
439 if (type_instance != NULL)
440 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
442 plugin_dispatch_values (&vl);
445 static void counter_submit (const char *type, const char *type_instance,
446 counter_t value, mysql_database_t *db)
450 values[0].counter = value;
451 submit (type, type_instance, values, STATIC_ARRAY_SIZE (values), db);
452 } /* void counter_submit */
454 static void gauge_submit (const char *type, const char *type_instance,
455 gauge_t value, mysql_database_t *db)
459 values[0].gauge = value;
460 submit (type, type_instance, values, STATIC_ARRAY_SIZE (values), db);
461 } /* void gauge_submit */
463 static void qcache_submit (counter_t hits, counter_t inserts,
464 counter_t not_cached, counter_t lowmem_prunes,
465 gauge_t queries_in_cache, mysql_database_t *db)
469 values[0].counter = hits;
470 values[1].counter = inserts;
471 values[2].counter = not_cached;
472 values[3].counter = lowmem_prunes;
473 values[4].gauge = queries_in_cache;
475 submit ("mysql_qcache", NULL, values, STATIC_ARRAY_SIZE (values), db);
476 } /* void qcache_submit */
478 static void threads_submit (gauge_t running, gauge_t connected, gauge_t cached,
479 counter_t created, mysql_database_t *db)
483 values[0].gauge = running;
484 values[1].gauge = connected;
485 values[2].gauge = cached;
486 values[3].counter = created;
488 submit ("mysql_threads", NULL, values, STATIC_ARRAY_SIZE (values), db);
489 } /* void threads_submit */
491 static void traffic_submit (counter_t rx, counter_t tx, mysql_database_t *db)
495 values[0].counter = rx;
496 values[1].counter = tx;
498 submit ("mysql_octets", NULL, values, STATIC_ARRAY_SIZE (values), db);
499 } /* void traffic_submit */
501 static MYSQL_RES *exec_query (MYSQL *con, const char *query)
505 int query_len = strlen (query);
507 if (mysql_real_query (con, query, query_len))
509 ERROR ("mysql plugin: Failed to execute query: %s",
511 INFO ("mysql plugin: SQL query was: %s", query);
515 res = mysql_store_result (con);
518 ERROR ("mysql plugin: Failed to store query result: %s",
520 INFO ("mysql plugin: SQL query was: %s", query);
527 static int mysql_read_master_stats (mysql_database_t *db, MYSQL *con)
534 unsigned long long position;
536 query = "SHOW MASTER STATUS";
538 res = exec_query (con, query);
542 row = mysql_fetch_row (res);
545 ERROR ("mysql plugin: Failed to get master statistics: "
546 "`%s' did not return any rows.", query);
550 field_num = mysql_num_fields (res);
553 ERROR ("mysql plugin: Failed to get master statistics: "
554 "`%s' returned less than two columns.", query);
558 position = atoll (row[1]);
559 counter_submit ("mysql_log_position", "master-bin", position, db);
561 row = mysql_fetch_row (res);
563 WARNING ("mysql plugin: `%s' returned more than one row - "
564 "ignoring further results.", query);
566 mysql_free_result (res);
569 } /* mysql_read_master_stats */
571 static int mysql_read_slave_stats (mysql_database_t *db, MYSQL *con)
579 /* WTF? libmysqlclient does not seem to provide any means to
580 * translate a column name to a column index ... :-/ */
581 const int READ_MASTER_LOG_POS_IDX = 6;
582 const int SLAVE_IO_RUNNING_IDX = 10;
583 const int SLAVE_SQL_RUNNING_IDX = 11;
584 const int EXEC_MASTER_LOG_POS_IDX = 21;
585 const int SECONDS_BEHIND_MASTER_IDX = 32;
587 query = "SHOW SLAVE STATUS";
589 res = exec_query (con, query);
593 row = mysql_fetch_row (res);
596 ERROR ("mysql plugin: Failed to get slave statistics: "
597 "`%s' did not return any rows.", query);
601 field_num = mysql_num_fields (res);
604 ERROR ("mysql plugin: Failed to get slave statistics: "
605 "`%s' returned less than 33 columns.", query);
611 unsigned long long counter;
614 counter = atoll (row[READ_MASTER_LOG_POS_IDX]);
615 counter_submit ("mysql_log_position", "slave-read", counter, db);
617 counter = atoll (row[EXEC_MASTER_LOG_POS_IDX]);
618 counter_submit ("mysql_log_position", "slave-exec", counter, db);
620 if (row[SECONDS_BEHIND_MASTER_IDX] != NULL)
622 gauge = atof (row[SECONDS_BEHIND_MASTER_IDX]);
623 gauge_submit ("time_offset", NULL, gauge, db);
629 notification_t n = { 0, time (NULL), "", "",
630 "mysql", "", "time_offset", "", NULL };
634 io = row[SLAVE_IO_RUNNING_IDX];
635 sql = row[SLAVE_SQL_RUNNING_IDX];
637 set_host (db, n.host, sizeof (n.host));
638 set_plugin_instance (db,
639 n.plugin_instance, sizeof (n.plugin_instance));
641 if (((io == NULL) || (strcasecmp (io, "yes") != 0))
642 && (db->slave_io_running))
644 n.severity = NOTIF_WARNING;
645 ssnprintf (n.message, sizeof (n.message),
646 "slave I/O thread not started or not connected to master");
647 plugin_dispatch_notification (&n);
648 db->slave_io_running = 0;
650 else if (((io != NULL) && (strcasecmp (io, "yes") == 0))
651 && (! db->slave_io_running))
653 n.severity = NOTIF_OKAY;
654 ssnprintf (n.message, sizeof (n.message),
655 "slave I/O thread started and connected to master");
656 plugin_dispatch_notification (&n);
657 db->slave_io_running = 1;
660 if (((sql == NULL) || (strcasecmp (sql, "yes") != 0))
661 && (db->slave_sql_running))
663 n.severity = NOTIF_WARNING;
664 ssnprintf (n.message, sizeof (n.message),
665 "slave SQL thread not started");
666 plugin_dispatch_notification (&n);
667 db->slave_sql_running = 0;
669 else if (((sql != NULL) && (strcasecmp (sql, "yes") == 0))
670 && (! db->slave_sql_running))
672 n.severity = NOTIF_OKAY;
673 ssnprintf (n.message, sizeof (n.message),
674 "slave SQL thread started");
675 plugin_dispatch_notification (&n);
676 db->slave_sql_running = 0;
680 row = mysql_fetch_row (res);
682 WARNING ("mysql plugin: `%s' returned more than one row - "
683 "ignoring further results.", query);
685 mysql_free_result (res);
688 } /* mysql_read_slave_stats */
690 static int mysql_read (user_data_t *ud)
692 mysql_database_t *db;
699 unsigned long long qcache_hits = 0ULL;
700 unsigned long long qcache_inserts = 0ULL;
701 unsigned long long qcache_not_cached = 0ULL;
702 unsigned long long qcache_lowmem_prunes = 0ULL;
703 int qcache_queries_in_cache = -1;
705 int threads_running = -1;
706 int threads_connected = -1;
707 int threads_cached = -1;
708 unsigned long long threads_created = 0ULL;
710 unsigned long long traffic_incoming = 0ULL;
711 unsigned long long traffic_outgoing = 0ULL;
713 if ((ud == NULL) || (ud->data == NULL))
715 ERROR ("mysql plugin: mysql_database_read: Invalid user data.");
719 db = (mysql_database_t *) ud->data;
721 /* An error message will have been printed in this case */
722 if ((con = getconnection (db)) == NULL)
725 query = "SHOW STATUS";
726 if (mysql_get_server_version (con) >= 50002)
727 query = "SHOW GLOBAL STATUS";
729 res = exec_query (con, query);
733 field_num = mysql_num_fields (res);
734 while ((row = mysql_fetch_row (res)))
737 unsigned long long val;
740 val = atoll (row[1]);
742 if (strncmp (key, "Com_", 4) == 0)
747 /* Ignore `prepared statements' */
748 if (strncmp (key, "Com_stmt_", 9) != 0)
749 counter_submit ("mysql_commands", key + 4, val, db);
751 else if (strncmp (key, "Handler_", 8) == 0)
756 counter_submit ("mysql_handler", key + 8, val, db);
758 else if (strncmp (key, "Qcache_", 7) == 0)
760 if (strcmp (key, "Qcache_hits") == 0)
762 else if (strcmp (key, "Qcache_inserts") == 0)
763 qcache_inserts = val;
764 else if (strcmp (key, "Qcache_not_cached") == 0)
765 qcache_not_cached = val;
766 else if (strcmp (key, "Qcache_lowmem_prunes") == 0)
767 qcache_lowmem_prunes = val;
768 else if (strcmp (key, "Qcache_queries_in_cache") == 0)
769 qcache_queries_in_cache = (int) val;
771 else if (strncmp (key, "Bytes_", 6) == 0)
773 if (strcmp (key, "Bytes_received") == 0)
774 traffic_incoming += val;
775 else if (strcmp (key, "Bytes_sent") == 0)
776 traffic_outgoing += val;
778 else if (strncmp (key, "Threads_", 8) == 0)
780 if (strcmp (key, "Threads_running") == 0)
781 threads_running = (int) val;
782 else if (strcmp (key, "Threads_connected") == 0)
783 threads_connected = (int) val;
784 else if (strcmp (key, "Threads_cached") == 0)
785 threads_cached = (int) val;
786 else if (strcmp (key, "Threads_created") == 0)
787 threads_created = val;
790 mysql_free_result (res); res = NULL;
792 if ((qcache_hits != 0ULL)
793 || (qcache_inserts != 0ULL)
794 || (qcache_not_cached != 0ULL)
795 || (qcache_lowmem_prunes != 0ULL))
796 qcache_submit (qcache_hits, qcache_inserts, qcache_not_cached,
797 qcache_lowmem_prunes, qcache_queries_in_cache, db);
799 if (threads_created != 0ULL)
800 threads_submit (threads_running, threads_connected,
801 threads_cached, threads_created, db);
803 traffic_submit (traffic_incoming, traffic_outgoing, db);
805 if (db->master_stats)
806 mysql_read_master_stats (db, con);
808 if ((db->slave_stats) || (db->slave_notif))
809 mysql_read_slave_stats (db, con);
812 } /* int mysql_read */
814 void module_register (void)
816 plugin_register_complex_config ("mysql", mysql_config);
817 } /* void module_register */