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 qcache_submit (counter_t hits, counter_t inserts,
356 counter_t not_cached, counter_t lowmem_prunes,
357 gauge_t queries_in_cache, mysql_database_t *db)
361 values[0].counter = hits;
362 values[1].counter = inserts;
363 values[2].counter = not_cached;
364 values[3].counter = lowmem_prunes;
365 values[4].gauge = queries_in_cache;
367 submit ("mysql_qcache", NULL, values, STATIC_ARRAY_SIZE (values), db);
368 } /* void qcache_submit */
370 static void threads_submit (gauge_t running, gauge_t connected, gauge_t cached,
371 counter_t created, mysql_database_t *db)
375 values[0].gauge = running;
376 values[1].gauge = connected;
377 values[2].gauge = cached;
378 values[3].counter = created;
380 submit ("mysql_threads", NULL, values, STATIC_ARRAY_SIZE (values), db);
381 } /* void threads_submit */
383 static void traffic_submit (counter_t rx, counter_t tx, mysql_database_t *db)
387 values[0].counter = rx;
388 values[1].counter = tx;
390 submit ("mysql_octets", NULL, values, STATIC_ARRAY_SIZE (values), db);
391 } /* void traffic_submit */
393 static MYSQL_RES *exec_query (MYSQL *con, const char *query)
397 int query_len = strlen (query);
399 if (mysql_real_query (con, query, query_len))
401 ERROR ("mysql plugin: Failed to execute query: %s",
403 INFO ("mysql plugin: SQL query was: %s", query);
407 res = mysql_store_result (con);
410 ERROR ("mysql plugin: Failed to store query result: %s",
412 INFO ("mysql plugin: SQL query was: %s", query);
419 static int mysql_read_master_stats (mysql_database_t *db, MYSQL *con)
426 unsigned long long position;
428 query = "SHOW MASTER STATUS";
430 res = exec_query (con, query);
434 row = mysql_fetch_row (res);
437 ERROR ("mysql plugin: Failed to get master statistics: "
438 "`%s' did not return any rows.", query);
442 field_num = mysql_num_fields (res);
445 ERROR ("mysql plugin: Failed to get master statistics: "
446 "`%s' returned less than two columns.", query);
450 position = atoll (row[1]);
451 counter_submit ("mysql_log_position", "master-bin", position, db);
453 row = mysql_fetch_row (res);
455 WARNING ("mysql plugin: `%s' returned more than one row - "
456 "ignoring further results.", query);
458 mysql_free_result (res);
461 } /* mysql_read_master_stats */
463 static int mysql_read_slave_stats (mysql_database_t *db, MYSQL *con)
471 /* WTF? libmysqlclient does not seem to provide any means to
472 * translate a column name to a column index ... :-/ */
473 const int READ_MASTER_LOG_POS_IDX = 6;
474 const int SLAVE_IO_RUNNING_IDX = 10;
475 const int SLAVE_SQL_RUNNING_IDX = 11;
476 const int EXEC_MASTER_LOG_POS_IDX = 21;
477 const int SECONDS_BEHIND_MASTER_IDX = 32;
479 query = "SHOW SLAVE STATUS";
481 res = exec_query (con, query);
485 row = mysql_fetch_row (res);
488 ERROR ("mysql plugin: Failed to get slave statistics: "
489 "`%s' did not return any rows.", query);
493 field_num = mysql_num_fields (res);
496 ERROR ("mysql plugin: Failed to get slave statistics: "
497 "`%s' returned less than 33 columns.", query);
503 unsigned long long counter;
506 counter = atoll (row[READ_MASTER_LOG_POS_IDX]);
507 counter_submit ("mysql_log_position", "slave-read", counter, db);
509 counter = atoll (row[EXEC_MASTER_LOG_POS_IDX]);
510 counter_submit ("mysql_log_position", "slave-exec", counter, db);
512 if (row[SECONDS_BEHIND_MASTER_IDX] != NULL)
514 gauge = atof (row[SECONDS_BEHIND_MASTER_IDX]);
515 gauge_submit ("time_offset", NULL, gauge, db);
521 notification_t n = { 0, time (NULL), "", "",
522 "mysql", "", "time_offset", "", NULL };
526 io = row[SLAVE_IO_RUNNING_IDX];
527 sql = row[SLAVE_SQL_RUNNING_IDX];
529 set_host (db, n.host, sizeof (n.host));
530 set_plugin_instance (db,
531 n.plugin_instance, sizeof (n.plugin_instance));
533 if (((io == NULL) || (strcasecmp (io, "yes") != 0))
534 && (db->slave_io_running))
536 n.severity = NOTIF_WARNING;
537 ssnprintf (n.message, sizeof (n.message),
538 "slave I/O thread not started or not connected to master");
539 plugin_dispatch_notification (&n);
540 db->slave_io_running = 0;
542 else if (((io != NULL) && (strcasecmp (io, "yes") == 0))
543 && (! db->slave_io_running))
545 n.severity = NOTIF_OKAY;
546 ssnprintf (n.message, sizeof (n.message),
547 "slave I/O thread started and connected to master");
548 plugin_dispatch_notification (&n);
549 db->slave_io_running = 1;
552 if (((sql == NULL) || (strcasecmp (sql, "yes") != 0))
553 && (db->slave_sql_running))
555 n.severity = NOTIF_WARNING;
556 ssnprintf (n.message, sizeof (n.message),
557 "slave SQL thread not started");
558 plugin_dispatch_notification (&n);
559 db->slave_sql_running = 0;
561 else if (((sql != NULL) && (strcasecmp (sql, "yes") == 0))
562 && (! db->slave_sql_running))
564 n.severity = NOTIF_OKAY;
565 ssnprintf (n.message, sizeof (n.message),
566 "slave SQL thread started");
567 plugin_dispatch_notification (&n);
568 db->slave_sql_running = 0;
572 row = mysql_fetch_row (res);
574 WARNING ("mysql plugin: `%s' returned more than one row - "
575 "ignoring further results.", query);
577 mysql_free_result (res);
580 } /* mysql_read_slave_stats */
582 static int mysql_read (user_data_t *ud)
584 mysql_database_t *db;
591 unsigned long long qcache_hits = 0ULL;
592 unsigned long long qcache_inserts = 0ULL;
593 unsigned long long qcache_not_cached = 0ULL;
594 unsigned long long qcache_lowmem_prunes = 0ULL;
595 int qcache_queries_in_cache = -1;
597 int threads_running = -1;
598 int threads_connected = -1;
599 int threads_cached = -1;
600 unsigned long long threads_created = 0ULL;
602 unsigned long long traffic_incoming = 0ULL;
603 unsigned long long traffic_outgoing = 0ULL;
605 if ((ud == NULL) || (ud->data == NULL))
607 ERROR ("mysql plugin: mysql_database_read: Invalid user data.");
611 db = (mysql_database_t *) ud->data;
613 /* An error message will have been printed in this case */
614 if ((con = getconnection (db)) == NULL)
617 query = "SHOW STATUS";
618 if (mysql_get_server_version (con) >= 50002)
619 query = "SHOW GLOBAL STATUS";
621 res = exec_query (con, query);
625 field_num = mysql_num_fields (res);
626 while ((row = mysql_fetch_row (res)))
629 unsigned long long val;
632 val = atoll (row[1]);
634 if (strncmp (key, "Com_",
635 strlen ("Com_")) == 0)
640 /* Ignore `prepared statements' */
641 if (strncmp (key, "Com_stmt_", strlen ("Com_stmt_")) != 0)
642 counter_submit ("mysql_commands",
643 key + strlen ("Com_"),
646 else if (strncmp (key, "Handler_",
647 strlen ("Handler_")) == 0)
652 counter_submit ("mysql_handler",
653 key + strlen ("Handler_"),
656 else if (strncmp (key, "Qcache_",
657 strlen ("Qcache_")) == 0)
659 if (strcmp (key, "Qcache_hits") == 0)
661 else if (strcmp (key, "Qcache_inserts") == 0)
662 qcache_inserts = val;
663 else if (strcmp (key, "Qcache_not_cached") == 0)
664 qcache_not_cached = val;
665 else if (strcmp (key, "Qcache_lowmem_prunes") == 0)
666 qcache_lowmem_prunes = val;
667 else if (strcmp (key, "Qcache_queries_in_cache") == 0)
668 qcache_queries_in_cache = (int) val;
670 else if (strncmp (key, "Bytes_",
671 strlen ("Bytes_")) == 0)
673 if (strcmp (key, "Bytes_received") == 0)
674 traffic_incoming += val;
675 else if (strcmp (key, "Bytes_sent") == 0)
676 traffic_outgoing += val;
678 else if (strncmp (key, "Threads_",
679 strlen ("Threads_")) == 0)
681 if (strcmp (key, "Threads_running") == 0)
682 threads_running = (int) val;
683 else if (strcmp (key, "Threads_connected") == 0)
684 threads_connected = (int) val;
685 else if (strcmp (key, "Threads_cached") == 0)
686 threads_cached = (int) val;
687 else if (strcmp (key, "Threads_created") == 0)
688 threads_created = val;
690 else if (strncmp (key, "Table_locks_",
691 strlen ("Table_locks_")) == 0)
693 counter_submit ("mysql_locks",
694 key + strlen ("Table_locks_"),
698 mysql_free_result (res); res = NULL;
700 if ((qcache_hits != 0ULL)
701 || (qcache_inserts != 0ULL)
702 || (qcache_not_cached != 0ULL)
703 || (qcache_lowmem_prunes != 0ULL))
704 qcache_submit (qcache_hits, qcache_inserts, qcache_not_cached,
705 qcache_lowmem_prunes, qcache_queries_in_cache, db);
707 if (threads_created != 0ULL)
708 threads_submit (threads_running, threads_connected,
709 threads_cached, threads_created, db);
711 traffic_submit (traffic_incoming, traffic_outgoing, db);
713 if (db->master_stats)
714 mysql_read_master_stats (db, con);
716 if ((db->slave_stats) || (db->slave_notif))
717 mysql_read_slave_stats (db, con);
720 } /* int mysql_read */
722 void module_register (void)
724 plugin_register_complex_config ("mysql", mysql_config);
725 } /* void module_register */