2 * collectd - src/mysql.c
3 * Copyright (C) 2006-2009 Florian octo Forster
4 * Copyright (C) 2009 Sebastian tokkee Harl
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; only version 2 of the License is applicable.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * Florian octo Forster <octo at verplant.org>
21 * Sebastian tokkee Harl <sh at tokkee.org>
27 #include "configfile.h"
31 #elif defined(HAVE_MYSQL_MYSQL_H)
32 #include <mysql/mysql.h>
35 /* TODO: Understand `Select_*' and possibly do that stuff as well.. */
37 struct mysql_database_s /* {{{ */
39 /* instance == NULL => legacy mode */
54 typedef struct mysql_database_s mysql_database_t; /* }}} */
56 static int mysql_read (user_data_t *ud);
58 static void mysql_database_free (void *arg) /* {{{ */
62 DEBUG ("mysql plugin: mysql_database_free (arg = %p);", arg);
64 db = (mysql_database_t *) arg;
70 mysql_close (db->con);
79 } /* }}} void mysql_database_free */
81 /* Configuration handling functions {{{
84 * <Database "plugin_instance1">
92 static int mysql_config_set_string (char **ret_string, /* {{{ */
97 if ((ci->values_num != 1)
98 || (ci->values[0].type != OCONFIG_TYPE_STRING))
100 WARNING ("mysql plugin: The `%s' config option "
101 "needs exactly one string argument.", ci->key);
105 string = strdup (ci->values[0].value.string);
108 ERROR ("mysql plugin: strdup failed.");
112 if (*ret_string != NULL)
114 *ret_string = string;
117 } /* }}} int mysql_config_set_string */
119 static int mysql_config_set_int (int *ret_int, /* {{{ */
122 if ((ci->values_num != 1)
123 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
125 WARNING ("mysql plugin: The `%s' config option "
126 "needs exactly one string argument.", ci->key);
130 *ret_int = ci->values[0].value.number;
133 } /* }}} int mysql_config_set_int */
135 static int mysql_config_set_boolean (int *ret_boolean, /* {{{ */
140 if (ci->values_num != 1)
145 if (ci->values[0].type == OCONFIG_TYPE_BOOLEAN)
146 *ret_boolean = ci->values[0].value.boolean;
147 else if (ci->values[0].type == OCONFIG_TYPE_STRING)
149 if (IS_TRUE (ci->values[0].value.string))
151 else if (IS_FALSE (ci->values[0].value.string))
162 WARNING ("mysql plugin: The `%s' config option "
163 "needs exactly one boolean argument.", ci->key);
167 } /* }}} mysql_config_set_boolean */
169 static int mysql_config (oconfig_item_t *ci) /* {{{ */
171 mysql_database_t *db;
176 if ((ci->values_num != 1)
177 || (ci->values[0].type != OCONFIG_TYPE_STRING))
179 WARNING ("mysql plugin: The `Database' block "
180 "needs exactly one string argument.");
184 db = (mysql_database_t *) malloc (sizeof (*db));
187 ERROR ("mysql plugin: malloc failed.");
190 memset (db, 0, sizeof (*db));
192 /* initialize all the pointers */
201 if (strcasecmp ("Plugin", ci->key) == 0)
205 else if (strcasecmp ("Database", ci->key) == 0)
208 status = mysql_config_set_string (&db->instance, ci);
214 assert (db->instance != NULL);
215 db->database = strdup (db->instance);
219 ERROR ("mysql plugin: mysql_config: "
220 "Invalid key: %s", ci->key);
224 /* Fill the `mysql_database_t' structure.. */
225 for (i = 0; i < ci->children_num; i++)
227 oconfig_item_t *child = ci->children + i;
229 if (strcasecmp ("Host", child->key) == 0)
230 status = mysql_config_set_string (&db->host, child);
231 else if (strcasecmp ("User", child->key) == 0)
232 status = mysql_config_set_string (&db->user, child);
233 else if (strcasecmp ("Password", child->key) == 0)
234 status = mysql_config_set_string (&db->pass, child);
235 else if (strcasecmp ("Port", child->key) == 0)
236 status = mysql_config_set_int (&db->port, child);
237 else if (strcasecmp ("Socket", child->key) == 0)
238 status = mysql_config_set_string (&db->socket, child);
239 /* Check if we're currently handling the `Plugin' block. If so,
240 * handle `Database' _blocks_, too. */
241 else if ((plugin_block != 0)
242 && (strcasecmp ("Database", child->key) == 0)
243 && (child->children != NULL))
245 /* If `plugin_block > 1', there has been at least one
246 * `Database' block */
248 status = mysql_config (child);
250 /* Now handle ordinary `Database' options (without children) */
251 else if ((strcasecmp ("Database", child->key) == 0)
252 && (child->children == NULL))
253 status = mysql_config_set_string (&db->database, child);
254 else if (strcasecmp ("MasterStats", child->key) == 0)
255 status = mysql_config_set_boolean (&db->master_stats, child);
256 else if (strcasecmp ("SlaveStats", child->key) == 0)
257 status = mysql_config_set_boolean (&db->slave_stats, child);
260 WARNING ("mysql plugin: Option `%s' not allowed here.", child->key);
268 /* Check if there were any `Database' blocks. */
269 if (plugin_block > 1)
271 /* There were connection blocks. Don't use any legacy stuff. */
272 if ((db->host != NULL)
273 || (db->user != NULL)
274 || (db->pass != NULL)
275 || (db->database != NULL)
276 || (db->socket != NULL)
279 WARNING ("mysql plugin: At least one <Database> "
280 "block has been found. The legacy "
281 "configuration will be ignored.");
283 mysql_database_free (db);
286 else if (plugin_block != 0)
288 WARNING ("mysql plugin: You're using the legacy "
289 "configuration options. Please consider "
290 "updating your configuration!");
293 /* Check that all necessary options have been given. */
296 /* Zero is allowed and automatically handled by
297 * `mysql_real_connect'. */
298 if ((db->port < 0) || (db->port > 65535))
300 ERROR ("mysql plugin: Database %s: Port number out "
302 (db->instance != NULL)
308 if (db->database == NULL)
310 ERROR ("mysql plugin: No `Database' configured");
314 } /* while (status == 0) */
316 /* If all went well, register this database for reading */
320 char cb_name[DATA_MAX_NAME_LEN];
322 DEBUG ("mysql plugin: Registering new read callback: %s", db->database);
324 memset (&ud, 0, sizeof (ud));
325 ud.data = (void *) db;
326 ud.free_func = mysql_database_free;
328 if (db->database != NULL)
329 ssnprintf (cb_name, sizeof (cb_name), "mysql-%s",
332 sstrncpy (cb_name, "mysql", sizeof (cb_name));
334 plugin_register_complex_read (cb_name, mysql_read,
335 /* interval = */ NULL, &ud);
339 mysql_database_free (db);
344 } /* }}} int mysql_config */
346 /* }}} End of configuration handling functions */
348 static MYSQL *getconnection (mysql_database_t *db)
353 if ((err = mysql_ping (db->con)) != 0)
355 WARNING ("mysql_ping failed: %s", mysql_error (db->con));
365 if ((db->con = mysql_init (db->con)) == NULL)
367 ERROR ("mysql_init failed: %s", mysql_error (db->con));
372 if (mysql_real_connect (db->con, db->host, db->user, db->pass,
373 db->database, db->port, db->socket, 0) == NULL)
375 ERROR ("mysql_real_connect failed: %s", mysql_error (db->con));
384 } /* static MYSQL *getconnection (mysql_database_t *db) */
386 static void set_host (mysql_database_t *db, value_list_t *vl)
388 /* XXX legacy mode - use hostname_g */
389 if (db->instance == NULL)
390 sstrncpy (vl->host, hostname_g, sizeof (vl->host));
393 if ((db->host == NULL)
394 || (strcmp ("", db->host) == 0)
395 || (strcmp ("localhost", db->host) == 0))
396 sstrncpy (vl->host, hostname_g, sizeof (vl->host));
398 sstrncpy (vl->host, db->host, sizeof (vl->host));
402 static void set_plugin_instance (mysql_database_t *db, value_list_t *vl)
404 /* XXX legacy mode - no plugin_instance */
405 if (db->instance == NULL)
406 sstrncpy (vl->plugin_instance, "",
407 sizeof (vl->plugin_instance));
409 sstrncpy (vl->plugin_instance, db->instance,
410 sizeof (vl->plugin_instance));
413 static void submit (const char *type, const char *type_instance,
414 value_t *values, size_t values_len, mysql_database_t *db)
416 value_list_t vl = VALUE_LIST_INIT;
419 vl.values_len = values_len;
423 sstrncpy (vl.plugin, "mysql", sizeof (vl.plugin));
424 set_plugin_instance (db, &vl);
426 sstrncpy (vl.type, type, sizeof (vl.type));
427 if (type_instance != NULL)
428 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
430 plugin_dispatch_values (&vl);
433 static void counter_submit (const char *type, const char *type_instance,
434 counter_t value, mysql_database_t *db)
438 values[0].counter = value;
439 submit (type, type_instance, values, STATIC_ARRAY_SIZE (values), db);
440 } /* void counter_submit */
442 static void gauge_submit (const char *type, const char *type_instance,
443 gauge_t value, mysql_database_t *db)
447 values[0].gauge = value;
448 submit (type, type_instance, values, STATIC_ARRAY_SIZE (values), db);
449 } /* void gauge_submit */
451 static void qcache_submit (counter_t hits, counter_t inserts,
452 counter_t not_cached, counter_t lowmem_prunes,
453 gauge_t queries_in_cache, mysql_database_t *db)
457 values[0].counter = hits;
458 values[1].counter = inserts;
459 values[2].counter = not_cached;
460 values[3].counter = lowmem_prunes;
461 values[4].gauge = queries_in_cache;
463 submit ("mysql_qcache", NULL, values, STATIC_ARRAY_SIZE (values), db);
464 } /* void qcache_submit */
466 static void threads_submit (gauge_t running, gauge_t connected, gauge_t cached,
467 counter_t created, mysql_database_t *db)
471 values[0].gauge = running;
472 values[1].gauge = connected;
473 values[2].gauge = cached;
474 values[3].counter = created;
476 submit ("mysql_threads", NULL, values, STATIC_ARRAY_SIZE (values), db);
477 } /* void threads_submit */
479 static void traffic_submit (counter_t rx, counter_t tx, mysql_database_t *db)
483 values[0].counter = rx;
484 values[1].counter = tx;
486 submit ("mysql_octets", NULL, values, STATIC_ARRAY_SIZE (values), db);
487 } /* void traffic_submit */
489 static MYSQL_RES *exec_query (MYSQL *con, const char *query)
493 int query_len = strlen (query);
495 if (mysql_real_query (con, query, query_len))
497 ERROR ("mysql plugin: Failed to execute query: %s",
499 INFO ("mysql plugin: SQL query was: %s", query);
503 res = mysql_store_result (con);
506 ERROR ("mysql plugin: Failed to store query result: %s",
508 INFO ("mysql plugin: SQL query was: %s", query);
515 static int mysql_read_master_stats (mysql_database_t *db, MYSQL *con)
522 unsigned long long position;
524 query = "SHOW MASTER STATUS";
526 res = exec_query (con, query);
530 row = mysql_fetch_row (res);
533 ERROR ("mysql plugin: Failed to get master statistics: "
534 "`%s' did not return any rows.", query);
538 field_num = mysql_num_fields (res);
541 ERROR ("mysql plugin: Failed to get master statistics: "
542 "`%s' returned less than two columns.", query);
546 position = atoll (row[1]);
547 counter_submit ("mysql_log_position", "master-bin", position, db);
549 row = mysql_fetch_row (res);
551 WARNING ("mysql plugin: `%s' returned more than one row - "
552 "ignoring further results.", query);
554 mysql_free_result (res);
557 } /* mysql_read_master_stats */
559 static int mysql_read_slave_stats (mysql_database_t *db, MYSQL *con)
567 /* WTF? libmysqlclient does not seem to provide any means to
568 * translate a column name to a column index ... :-/ */
569 const int READ_MASTER_LOG_POS_IDX = 6;
570 const int EXEC_MASTER_LOG_POS_IDX = 21;
571 const int SECONDS_BEHIND_MASTER_IDX = 32;
573 unsigned long long counter;
576 query = "SHOW SLAVE STATUS";
578 res = exec_query (con, query);
582 row = mysql_fetch_row (res);
585 ERROR ("mysql plugin: Failed to get slave statistics: "
586 "`%s' did not return any rows.", query);
590 field_num = mysql_num_fields (res);
593 ERROR ("mysql plugin: Failed to get slave statistics: "
594 "`%s' returned less than 33 columns.", query);
598 counter = atoll (row[READ_MASTER_LOG_POS_IDX]);
599 counter_submit ("mysql_log_position", "slave-read", counter, db);
601 counter = atoll (row[EXEC_MASTER_LOG_POS_IDX]);
602 counter_submit ("mysql_log_position", "slave-exec", counter, db);
604 if (row[SECONDS_BEHIND_MASTER_IDX] != NULL)
606 gauge = atof (row[SECONDS_BEHIND_MASTER_IDX]);
607 gauge_submit ("time_offset", NULL, gauge, db);
610 row = mysql_fetch_row (res);
612 WARNING ("mysql plugin: `%s' returned more than one row - "
613 "ignoring further results.", query);
615 mysql_free_result (res);
618 } /* mysql_read_slave_stats */
620 static int mysql_read (user_data_t *ud)
622 mysql_database_t *db;
629 unsigned long long qcache_hits = 0ULL;
630 unsigned long long qcache_inserts = 0ULL;
631 unsigned long long qcache_not_cached = 0ULL;
632 unsigned long long qcache_lowmem_prunes = 0ULL;
633 int qcache_queries_in_cache = -1;
635 int threads_running = -1;
636 int threads_connected = -1;
637 int threads_cached = -1;
638 unsigned long long threads_created = 0ULL;
640 unsigned long long traffic_incoming = 0ULL;
641 unsigned long long traffic_outgoing = 0ULL;
643 if ((ud == NULL) || (ud->data == NULL))
645 ERROR ("mysql plugin: mysql_database_read: Invalid user data.");
649 db = (mysql_database_t *) ud->data;
651 /* An error message will have been printed in this case */
652 if ((con = getconnection (db)) == NULL)
655 query = "SHOW STATUS";
656 if (mysql_get_server_version (con) >= 50002)
657 query = "SHOW GLOBAL STATUS";
659 res = exec_query (con, query);
663 field_num = mysql_num_fields (res);
664 while ((row = mysql_fetch_row (res)))
667 unsigned long long val;
670 val = atoll (row[1]);
672 if (strncmp (key, "Com_", 4) == 0)
677 /* Ignore `prepared statements' */
678 if (strncmp (key, "Com_stmt_", 9) != 0)
679 counter_submit ("mysql_commands", key + 4, val, db);
681 else if (strncmp (key, "Handler_", 8) == 0)
686 counter_submit ("mysql_handler", key + 8, val, db);
688 else if (strncmp (key, "Qcache_", 7) == 0)
690 if (strcmp (key, "Qcache_hits") == 0)
692 else if (strcmp (key, "Qcache_inserts") == 0)
693 qcache_inserts = val;
694 else if (strcmp (key, "Qcache_not_cached") == 0)
695 qcache_not_cached = val;
696 else if (strcmp (key, "Qcache_lowmem_prunes") == 0)
697 qcache_lowmem_prunes = val;
698 else if (strcmp (key, "Qcache_queries_in_cache") == 0)
699 qcache_queries_in_cache = (int) val;
701 else if (strncmp (key, "Bytes_", 6) == 0)
703 if (strcmp (key, "Bytes_received") == 0)
704 traffic_incoming += val;
705 else if (strcmp (key, "Bytes_sent") == 0)
706 traffic_outgoing += val;
708 else if (strncmp (key, "Threads_", 8) == 0)
710 if (strcmp (key, "Threads_running") == 0)
711 threads_running = (int) val;
712 else if (strcmp (key, "Threads_connected") == 0)
713 threads_connected = (int) val;
714 else if (strcmp (key, "Threads_cached") == 0)
715 threads_cached = (int) val;
716 else if (strcmp (key, "Threads_created") == 0)
717 threads_created = val;
720 mysql_free_result (res); res = NULL;
722 if ((qcache_hits != 0ULL)
723 || (qcache_inserts != 0ULL)
724 || (qcache_not_cached != 0ULL)
725 || (qcache_lowmem_prunes != 0ULL))
726 qcache_submit (qcache_hits, qcache_inserts, qcache_not_cached,
727 qcache_lowmem_prunes, qcache_queries_in_cache, db);
729 if (threads_created != 0ULL)
730 threads_submit (threads_running, threads_connected,
731 threads_cached, threads_created, db);
733 traffic_submit (traffic_incoming, traffic_outgoing, db);
735 if (db->master_stats)
736 mysql_read_master_stats (db, con);
739 mysql_read_slave_stats (db, con);
742 } /* int mysql_read */
744 void module_register (void)
746 plugin_register_complex_config ("mysql", mysql_config);
747 } /* void module_register */