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 */
58 _Bool slave_io_running;
59 _Bool 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">
101 static int mysql_config_database (oconfig_item_t *ci) /* {{{ */
103 mysql_database_t *db;
107 if ((ci->values_num != 1)
108 || (ci->values[0].type != OCONFIG_TYPE_STRING))
110 WARNING ("mysql plugin: The `Database' block "
111 "needs exactly one string argument.");
115 db = (mysql_database_t *) malloc (sizeof (*db));
118 ERROR ("mysql plugin: malloc failed.");
121 memset (db, 0, sizeof (*db));
123 /* initialize all the pointers */
131 /* trigger a notification, if it's not running */
132 db->slave_io_running = 1;
133 db->slave_sql_running = 1;
135 status = cf_util_get_string (ci, &db->instance);
141 assert (db->instance != NULL);
143 /* Fill the `mysql_database_t' structure.. */
144 for (i = 0; i < ci->children_num; i++)
146 oconfig_item_t *child = ci->children + i;
148 if (strcasecmp ("Host", child->key) == 0)
149 status = cf_util_get_string (child, &db->host);
150 else if (strcasecmp ("User", child->key) == 0)
151 status = cf_util_get_string (child, &db->user);
152 else if (strcasecmp ("Password", child->key) == 0)
153 status = cf_util_get_string (child, &db->pass);
154 else if (strcasecmp ("Port", child->key) == 0)
156 status = cf_util_get_port_number (child);
163 else if (strcasecmp ("Socket", child->key) == 0)
164 status = cf_util_get_string (child, &db->socket);
165 else if (strcasecmp ("Database", child->key) == 0)
166 status = cf_util_get_string (child, &db->database);
167 else if (strcasecmp ("MasterStats", child->key) == 0)
168 status = cf_util_get_boolean (child, &db->master_stats);
169 else if (strcasecmp ("SlaveStats", child->key) == 0)
170 status = cf_util_get_boolean (child, &db->slave_stats);
171 else if (strcasecmp ("SlaveNotifications", child->key) == 0)
172 status = cf_util_get_boolean (child, &db->slave_notif);
175 WARNING ("mysql plugin: Option `%s' not allowed here.", child->key);
183 /* If all went well, register this database for reading */
187 char cb_name[DATA_MAX_NAME_LEN];
189 DEBUG ("mysql plugin: Registering new read callback: %s",
190 (db->database != NULL) ? db->database : "<default>");
192 memset (&ud, 0, sizeof (ud));
193 ud.data = (void *) db;
194 ud.free_func = mysql_database_free;
196 if (db->database != NULL)
197 ssnprintf (cb_name, sizeof (cb_name), "mysql-%s",
200 sstrncpy (cb_name, "mysql", sizeof (cb_name));
202 plugin_register_complex_read (/* group = */ NULL, cb_name,
204 /* interval = */ NULL, &ud);
208 mysql_database_free (db);
213 } /* }}} int mysql_config_database */
215 static int mysql_config (oconfig_item_t *ci) /* {{{ */
222 /* Fill the `mysql_database_t' structure.. */
223 for (i = 0; i < ci->children_num; i++)
225 oconfig_item_t *child = ci->children + i;
227 if (strcasecmp ("Database", child->key) == 0)
228 mysql_config_database (child);
230 WARNING ("mysql plugin: Option \"%s\" not allowed here.",
235 } /* }}} int mysql_config */
237 /* }}} End of configuration handling functions */
239 static MYSQL *getconnection (mysql_database_t *db)
244 if ((err = mysql_ping (db->con)) != 0)
246 WARNING ("mysql_ping failed for %s: %s",
247 (db->instance != NULL)
250 mysql_error (db->con));
260 if ((db->con = mysql_init (db->con)) == NULL)
262 ERROR ("mysql_init failed: %s", mysql_error (db->con));
267 if (mysql_real_connect (db->con, db->host, db->user, db->pass,
268 db->database, db->port, db->socket, 0) == NULL)
270 ERROR ("mysql plugin: Failed to connect to database %s "
272 (db->database != NULL) ? db->database : "<none>",
273 (db->host != NULL) ? db->host : "localhost",
274 mysql_error (db->con));
280 INFO ("mysql plugin: Successfully connected to database %s "
281 "at server %s (server version: %s, protocol version: %d)",
282 (db->database != NULL) ? db->database : "<none>",
283 mysql_get_host_info (db->con),
284 mysql_get_server_info (db->con),
285 mysql_get_proto_info (db->con));
289 } /* static MYSQL *getconnection (mysql_database_t *db) */
291 static void set_host (mysql_database_t *db, char *buf, size_t buflen)
293 /* XXX legacy mode - use hostname_g */
294 if (db->instance == NULL)
295 sstrncpy (buf, hostname_g, buflen);
298 if ((db->host == NULL)
299 || (strcmp ("", db->host) == 0)
300 || (strcmp ("localhost", db->host) == 0))
301 sstrncpy (buf, hostname_g, buflen);
303 sstrncpy (buf, db->host, buflen);
307 static void set_plugin_instance (mysql_database_t *db,
308 char *buf, size_t buflen)
310 /* XXX legacy mode - no plugin_instance */
311 if (db->instance == NULL)
312 sstrncpy (buf, "", buflen);
314 sstrncpy (buf, db->instance, buflen);
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));
328 set_plugin_instance (db, vl.plugin_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 counter_t value, mysql_database_t *db)
342 values[0].counter = 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 (counter_t rx, counter_t tx, mysql_database_t *db)
368 values[0].counter = rx;
369 values[1].counter = 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);
423 field_num = mysql_num_fields (res);
426 ERROR ("mysql plugin: Failed to get master statistics: "
427 "`%s' returned less than two columns.", query);
431 position = atoll (row[1]);
432 counter_submit ("mysql_log_position", "master-bin", position, db);
434 row = mysql_fetch_row (res);
436 WARNING ("mysql plugin: `%s' returned more than one row - "
437 "ignoring further results.", query);
439 mysql_free_result (res);
442 } /* mysql_read_master_stats */
444 static int mysql_read_slave_stats (mysql_database_t *db, MYSQL *con)
452 /* WTF? libmysqlclient does not seem to provide any means to
453 * translate a column name to a column index ... :-/ */
454 const int READ_MASTER_LOG_POS_IDX = 6;
455 const int SLAVE_IO_RUNNING_IDX = 10;
456 const int SLAVE_SQL_RUNNING_IDX = 11;
457 const int EXEC_MASTER_LOG_POS_IDX = 21;
458 const int SECONDS_BEHIND_MASTER_IDX = 32;
460 query = "SHOW SLAVE STATUS";
462 res = exec_query (con, query);
466 row = mysql_fetch_row (res);
469 ERROR ("mysql plugin: Failed to get slave statistics: "
470 "`%s' did not return any rows.", query);
474 field_num = mysql_num_fields (res);
477 ERROR ("mysql plugin: Failed to get slave statistics: "
478 "`%s' returned less than 33 columns.", query);
484 unsigned long long counter;
487 counter = atoll (row[READ_MASTER_LOG_POS_IDX]);
488 counter_submit ("mysql_log_position", "slave-read", counter, db);
490 counter = atoll (row[EXEC_MASTER_LOG_POS_IDX]);
491 counter_submit ("mysql_log_position", "slave-exec", counter, db);
493 if (row[SECONDS_BEHIND_MASTER_IDX] != NULL)
495 gauge = atof (row[SECONDS_BEHIND_MASTER_IDX]);
496 gauge_submit ("time_offset", NULL, gauge, db);
502 notification_t n = { 0, time (NULL), "", "",
503 "mysql", "", "time_offset", "", NULL };
507 io = row[SLAVE_IO_RUNNING_IDX];
508 sql = row[SLAVE_SQL_RUNNING_IDX];
510 set_host (db, n.host, sizeof (n.host));
511 set_plugin_instance (db,
512 n.plugin_instance, sizeof (n.plugin_instance));
514 if (((io == NULL) || (strcasecmp (io, "yes") != 0))
515 && (db->slave_io_running))
517 n.severity = NOTIF_WARNING;
518 ssnprintf (n.message, sizeof (n.message),
519 "slave I/O thread not started or not connected to master");
520 plugin_dispatch_notification (&n);
521 db->slave_io_running = 0;
523 else if (((io != NULL) && (strcasecmp (io, "yes") == 0))
524 && (! db->slave_io_running))
526 n.severity = NOTIF_OKAY;
527 ssnprintf (n.message, sizeof (n.message),
528 "slave I/O thread started and connected to master");
529 plugin_dispatch_notification (&n);
530 db->slave_io_running = 1;
533 if (((sql == NULL) || (strcasecmp (sql, "yes") != 0))
534 && (db->slave_sql_running))
536 n.severity = NOTIF_WARNING;
537 ssnprintf (n.message, sizeof (n.message),
538 "slave SQL thread not started");
539 plugin_dispatch_notification (&n);
540 db->slave_sql_running = 0;
542 else if (((sql != NULL) && (strcasecmp (sql, "yes") == 0))
543 && (! db->slave_sql_running))
545 n.severity = NOTIF_OKAY;
546 ssnprintf (n.message, sizeof (n.message),
547 "slave SQL thread started");
548 plugin_dispatch_notification (&n);
549 db->slave_sql_running = 0;
553 row = mysql_fetch_row (res);
555 WARNING ("mysql plugin: `%s' returned more than one row - "
556 "ignoring further results.", query);
558 mysql_free_result (res);
561 } /* mysql_read_slave_stats */
563 static int mysql_read (user_data_t *ud)
565 mysql_database_t *db;
572 derive_t qcache_hits = 0;
573 derive_t qcache_inserts = 0;
574 derive_t qcache_not_cached = 0;
575 derive_t qcache_lowmem_prunes = 0;
576 gauge_t qcache_queries_in_cache = NAN;
578 gauge_t threads_running = NAN;
579 gauge_t threads_connected = NAN;
580 gauge_t threads_cached = NAN;
581 derive_t threads_created = 0;
583 unsigned long long traffic_incoming = 0ULL;
584 unsigned long long traffic_outgoing = 0ULL;
586 if ((ud == NULL) || (ud->data == NULL))
588 ERROR ("mysql plugin: mysql_database_read: Invalid user data.");
592 db = (mysql_database_t *) ud->data;
594 /* An error message will have been printed in this case */
595 if ((con = getconnection (db)) == NULL)
598 query = "SHOW STATUS";
599 if (mysql_get_server_version (con) >= 50002)
600 query = "SHOW GLOBAL STATUS";
602 res = exec_query (con, query);
606 field_num = mysql_num_fields (res);
607 while ((row = mysql_fetch_row (res)))
610 unsigned long long val;
613 val = atoll (row[1]);
615 if (strncmp (key, "Com_",
616 strlen ("Com_")) == 0)
621 /* Ignore `prepared statements' */
622 if (strncmp (key, "Com_stmt_", strlen ("Com_stmt_")) != 0)
623 counter_submit ("mysql_commands",
624 key + strlen ("Com_"),
627 else if (strncmp (key, "Handler_",
628 strlen ("Handler_")) == 0)
633 counter_submit ("mysql_handler",
634 key + strlen ("Handler_"),
637 else if (strncmp (key, "Qcache_",
638 strlen ("Qcache_")) == 0)
640 if (strcmp (key, "Qcache_hits") == 0)
641 qcache_hits = (derive_t) val;
642 else if (strcmp (key, "Qcache_inserts") == 0)
643 qcache_inserts = (derive_t) val;
644 else if (strcmp (key, "Qcache_not_cached") == 0)
645 qcache_not_cached = (derive_t) val;
646 else if (strcmp (key, "Qcache_lowmem_prunes") == 0)
647 qcache_lowmem_prunes = (derive_t) val;
648 else if (strcmp (key, "Qcache_queries_in_cache") == 0)
649 qcache_queries_in_cache = (gauge_t) val;
651 else if (strncmp (key, "Bytes_",
652 strlen ("Bytes_")) == 0)
654 if (strcmp (key, "Bytes_received") == 0)
655 traffic_incoming += val;
656 else if (strcmp (key, "Bytes_sent") == 0)
657 traffic_outgoing += val;
659 else if (strncmp (key, "Threads_",
660 strlen ("Threads_")) == 0)
662 if (strcmp (key, "Threads_running") == 0)
663 threads_running = (gauge_t) val;
664 else if (strcmp (key, "Threads_connected") == 0)
665 threads_connected = (gauge_t) val;
666 else if (strcmp (key, "Threads_cached") == 0)
667 threads_cached = (gauge_t) val;
668 else if (strcmp (key, "Threads_created") == 0)
669 threads_created = (derive_t) val;
671 else if (strncmp (key, "Table_locks_",
672 strlen ("Table_locks_")) == 0)
674 counter_submit ("mysql_locks",
675 key + strlen ("Table_locks_"),
679 mysql_free_result (res); res = NULL;
681 if ((qcache_hits != 0)
682 || (qcache_inserts != 0)
683 || (qcache_not_cached != 0)
684 || (qcache_lowmem_prunes != 0))
686 derive_submit ("cache_result", "qcache-hits",
688 derive_submit ("cache_result", "qcache-inserts",
690 derive_submit ("cache_result", "qcache-not_cached",
691 qcache_not_cached, db);
692 derive_submit ("cache_result", "qcache-prunes",
693 qcache_lowmem_prunes, db);
695 gauge_submit ("cache_size", "qcache",
696 qcache_queries_in_cache, db);
699 if (threads_created != 0)
701 gauge_submit ("threads", "running",
702 threads_running, db);
703 gauge_submit ("threads", "connected",
704 threads_connected, db);
705 gauge_submit ("threads", "cached",
708 derive_submit ("total_threads", "created",
709 threads_created, db);
712 traffic_submit (traffic_incoming, traffic_outgoing, db);
714 if (db->master_stats)
715 mysql_read_master_stats (db, con);
717 if ((db->slave_stats) || (db->slave_notif))
718 mysql_read_slave_stats (db, con);
721 } /* int mysql_read */
723 void module_register (void)
725 plugin_register_complex_config ("mysql", mysql_config);
726 } /* void module_register */