1 /**
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
8 *
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.
12 *
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.
17 *
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
21 *
22 * Authors:
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>
28 **/
30 #include "collectd.h"
31 #include "common.h"
32 #include "plugin.h"
33 #include "configfile.h"
35 #ifdef HAVE_MYSQL_H
36 #include <mysql.h>
37 #elif defined(HAVE_MYSQL_MYSQL_H)
38 #include <mysql/mysql.h>
39 #endif
41 /* TODO: Understand `Select_*' and possibly do that stuff as well.. */
43 struct mysql_database_s /* {{{ */
44 {
45 /* instance == NULL => legacy mode */
46 char *instance;
47 char *host;
48 char *user;
49 char *pass;
50 char *database;
51 char *socket;
52 int port;
54 _Bool master_stats;
55 _Bool slave_stats;
57 _Bool slave_notif;
58 _Bool slave_io_running;
59 _Bool slave_sql_running;
61 MYSQL *con;
62 int state;
63 };
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) /* {{{ */
69 {
70 mysql_database_t *db;
72 DEBUG ("mysql plugin: mysql_database_free (arg = %p);", arg);
74 db = (mysql_database_t *) arg;
76 if (db == NULL)
77 return;
79 if (db->con != NULL)
80 mysql_close (db->con);
82 sfree (db->host);
83 sfree (db->user);
84 sfree (db->pass);
85 sfree (db->socket);
86 sfree (db->instance);
87 sfree (db->database);
88 sfree (db);
89 } /* }}} void mysql_database_free */
91 /* Configuration handling functions {{{
92 *
93 * <Plugin mysql>
94 * <Database "plugin_instance1">
95 * Host "localhost"
96 * Port 22000
97 * ...
98 * </Database>
99 * </Plugin>
100 */
101 static int mysql_config_database (oconfig_item_t *ci) /* {{{ */
102 {
103 mysql_database_t *db;
104 int status = 0;
105 int i;
107 if ((ci->values_num != 1)
108 || (ci->values[0].type != OCONFIG_TYPE_STRING))
109 {
110 WARNING ("mysql plugin: The `Database' block "
111 "needs exactly one string argument.");
112 return (-1);
113 }
115 db = (mysql_database_t *) malloc (sizeof (*db));
116 if (db == NULL)
117 {
118 ERROR ("mysql plugin: malloc failed.");
119 return (-1);
120 }
121 memset (db, 0, sizeof (*db));
123 /* initialize all the pointers */
124 db->host = NULL;
125 db->user = NULL;
126 db->pass = NULL;
127 db->database = NULL;
128 db->socket = NULL;
129 db->con = NULL;
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);
136 if (status != 0)
137 {
138 sfree (db);
139 return (status);
140 }
141 assert (db->instance != NULL);
143 /* Fill the `mysql_database_t' structure.. */
144 for (i = 0; i < ci->children_num; i++)
145 {
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)
155 {
156 status = cf_util_get_port_number (child);
157 if (status > 0)
158 {
159 db->port = status;
160 status = 0;
161 }
162 }
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);
173 else
174 {
175 WARNING ("mysql plugin: Option `%s' not allowed here.", child->key);
176 status = -1;
177 }
179 if (status != 0)
180 break;
181 }
183 /* If all went well, register this database for reading */
184 if (status == 0)
185 {
186 user_data_t ud;
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",
198 db->database);
199 else
200 sstrncpy (cb_name, "mysql", sizeof (cb_name));
202 plugin_register_complex_read (/* group = */ NULL, cb_name,
203 mysql_read,
204 /* interval = */ NULL, &ud);
205 }
206 else
207 {
208 mysql_database_free (db);
209 return (-1);
210 }
212 return (0);
213 } /* }}} int mysql_config_database */
215 static int mysql_config (oconfig_item_t *ci) /* {{{ */
216 {
217 int i;
219 if (ci == NULL)
220 return (EINVAL);
222 /* Fill the `mysql_database_t' structure.. */
223 for (i = 0; i < ci->children_num; i++)
224 {
225 oconfig_item_t *child = ci->children + i;
227 if (strcasecmp ("Database", child->key) == 0)
228 mysql_config_database (child);
229 else
230 WARNING ("mysql plugin: Option \"%s\" not allowed here.",
231 child->key);
232 }
234 return (0);
235 } /* }}} int mysql_config */
237 /* }}} End of configuration handling functions */
239 static MYSQL *getconnection (mysql_database_t *db)
240 {
241 if (db->state != 0)
242 {
243 int err;
244 if ((err = mysql_ping (db->con)) != 0)
245 {
246 WARNING ("mysql_ping failed for %s: %s",
247 (db->instance != NULL)
248 ? db->instance
249 : "<legacy>",
250 mysql_error (db->con));
251 db->state = 0;
252 }
253 else
254 {
255 db->state = 1;
256 return (db->con);
257 }
258 }
260 if ((db->con = mysql_init (db->con)) == NULL)
261 {
262 ERROR ("mysql_init failed: %s", mysql_error (db->con));
263 db->state = 0;
264 return (NULL);
265 }
267 if (mysql_real_connect (db->con, db->host, db->user, db->pass,
268 db->database, db->port, db->socket, 0) == NULL)
269 {
270 ERROR ("mysql plugin: Failed to connect to database %s "
271 "at server %s: %s",
272 (db->database != NULL) ? db->database : "<none>",
273 (db->host != NULL) ? db->host : "localhost",
274 mysql_error (db->con));
275 db->state = 0;
276 return (NULL);
277 }
278 else
279 {
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));
286 db->state = 1;
287 return (db->con);
288 }
289 } /* static MYSQL *getconnection (mysql_database_t *db) */
291 static void set_host (mysql_database_t *db, char *buf, size_t buflen)
292 {
293 /* XXX legacy mode - use hostname_g */
294 if (db->instance == NULL)
295 sstrncpy (buf, hostname_g, buflen);
296 else
297 {
298 if ((db->host == NULL)
299 || (strcmp ("", db->host) == 0)
300 || (strcmp ("localhost", db->host) == 0))
301 sstrncpy (buf, hostname_g, buflen);
302 else
303 sstrncpy (buf, db->host, buflen);
304 }
305 }
307 static void set_plugin_instance (mysql_database_t *db,
308 char *buf, size_t buflen)
309 {
310 /* XXX legacy mode - no plugin_instance */
311 if (db->instance == NULL)
312 sstrncpy (buf, "", buflen);
313 else
314 sstrncpy (buf, db->instance, buflen);
315 }
317 static void submit (const char *type, const char *type_instance,
318 value_t *values, size_t values_len, mysql_database_t *db)
319 {
320 value_list_t vl = VALUE_LIST_INIT;
322 vl.values = values;
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);
335 } /* submit */
337 static void counter_submit (const char *type, const char *type_instance,
338 counter_t value, mysql_database_t *db)
339 {
340 value_t values[1];
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)
348 {
349 value_t values[1];
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)
358 {
359 value_t values[5];
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)
372 {
373 value_t values[4];
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)
384 {
385 value_t values[2];
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)
394 {
395 MYSQL_RES *res;
397 int query_len = strlen (query);
399 if (mysql_real_query (con, query, query_len))
400 {
401 ERROR ("mysql plugin: Failed to execute query: %s",
402 mysql_error (con));
403 INFO ("mysql plugin: SQL query was: %s", query);
404 return (NULL);
405 }
407 res = mysql_store_result (con);
408 if (res == NULL)
409 {
410 ERROR ("mysql plugin: Failed to store query result: %s",
411 mysql_error (con));
412 INFO ("mysql plugin: SQL query was: %s", query);
413 return (NULL);
414 }
416 return (res);
417 } /* exec_query */
419 static int mysql_read_master_stats (mysql_database_t *db, MYSQL *con)
420 {
421 MYSQL_RES *res;
422 MYSQL_ROW row;
424 char *query;
425 int field_num;
426 unsigned long long position;
428 query = "SHOW MASTER STATUS";
430 res = exec_query (con, query);
431 if (res == NULL)
432 return (-1);
434 row = mysql_fetch_row (res);
435 if (row == NULL)
436 {
437 ERROR ("mysql plugin: Failed to get master statistics: "
438 "`%s' did not return any rows.", query);
439 return (-1);
440 }
442 field_num = mysql_num_fields (res);
443 if (field_num < 2)
444 {
445 ERROR ("mysql plugin: Failed to get master statistics: "
446 "`%s' returned less than two columns.", query);
447 return (-1);
448 }
450 position = atoll (row[1]);
451 counter_submit ("mysql_log_position", "master-bin", position, db);
453 row = mysql_fetch_row (res);
454 if (row != NULL)
455 WARNING ("mysql plugin: `%s' returned more than one row - "
456 "ignoring further results.", query);
458 mysql_free_result (res);
460 return (0);
461 } /* mysql_read_master_stats */
463 static int mysql_read_slave_stats (mysql_database_t *db, MYSQL *con)
464 {
465 MYSQL_RES *res;
466 MYSQL_ROW row;
468 char *query;
469 int field_num;
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);
482 if (res == NULL)
483 return (-1);
485 row = mysql_fetch_row (res);
486 if (row == NULL)
487 {
488 ERROR ("mysql plugin: Failed to get slave statistics: "
489 "`%s' did not return any rows.", query);
490 return (-1);
491 }
493 field_num = mysql_num_fields (res);
494 if (field_num < 33)
495 {
496 ERROR ("mysql plugin: Failed to get slave statistics: "
497 "`%s' returned less than 33 columns.", query);
498 return (-1);
499 }
501 if (db->slave_stats)
502 {
503 unsigned long long counter;
504 double gauge;
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)
513 {
514 gauge = atof (row[SECONDS_BEHIND_MASTER_IDX]);
515 gauge_submit ("time_offset", NULL, gauge, db);
516 }
517 }
519 if (db->slave_notif)
520 {
521 notification_t n = { 0, time (NULL), "", "",
522 "mysql", "", "time_offset", "", NULL };
524 char *io, *sql;
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))
535 {
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;
541 }
542 else if (((io != NULL) && (strcasecmp (io, "yes") == 0))
543 && (! db->slave_io_running))
544 {
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;
550 }
552 if (((sql == NULL) || (strcasecmp (sql, "yes") != 0))
553 && (db->slave_sql_running))
554 {
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;
560 }
561 else if (((sql != NULL) && (strcasecmp (sql, "yes") == 0))
562 && (! db->slave_sql_running))
563 {
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;
569 }
570 }
572 row = mysql_fetch_row (res);
573 if (row != NULL)
574 WARNING ("mysql plugin: `%s' returned more than one row - "
575 "ignoring further results.", query);
577 mysql_free_result (res);
579 return (0);
580 } /* mysql_read_slave_stats */
582 static int mysql_read (user_data_t *ud)
583 {
584 mysql_database_t *db;
585 MYSQL *con;
586 MYSQL_RES *res;
587 MYSQL_ROW row;
588 char *query;
589 int field_num;
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))
606 {
607 ERROR ("mysql plugin: mysql_database_read: Invalid user data.");
608 return (-1);
609 }
611 db = (mysql_database_t *) ud->data;
613 /* An error message will have been printed in this case */
614 if ((con = getconnection (db)) == NULL)
615 return (-1);
617 query = "SHOW STATUS";
618 if (mysql_get_server_version (con) >= 50002)
619 query = "SHOW GLOBAL STATUS";
621 res = exec_query (con, query);
622 if (res == NULL)
623 return (-1);
625 field_num = mysql_num_fields (res);
626 while ((row = mysql_fetch_row (res)))
627 {
628 char *key;
629 unsigned long long val;
631 key = row[0];
632 val = atoll (row[1]);
634 if (strncmp (key, "Com_",
635 strlen ("Com_")) == 0)
636 {
637 if (val == 0ULL)
638 continue;
640 /* Ignore `prepared statements' */
641 if (strncmp (key, "Com_stmt_", strlen ("Com_stmt_")) != 0)
642 counter_submit ("mysql_commands",
643 key + strlen ("Com_"),
644 val, db);
645 }
646 else if (strncmp (key, "Handler_",
647 strlen ("Handler_")) == 0)
648 {
649 if (val == 0ULL)
650 continue;
652 counter_submit ("mysql_handler",
653 key + strlen ("Handler_"),
654 val, db);
655 }
656 else if (strncmp (key, "Qcache_",
657 strlen ("Qcache_")) == 0)
658 {
659 if (strcmp (key, "Qcache_hits") == 0)
660 qcache_hits = val;
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;
669 }
670 else if (strncmp (key, "Bytes_",
671 strlen ("Bytes_")) == 0)
672 {
673 if (strcmp (key, "Bytes_received") == 0)
674 traffic_incoming += val;
675 else if (strcmp (key, "Bytes_sent") == 0)
676 traffic_outgoing += val;
677 }
678 else if (strncmp (key, "Threads_",
679 strlen ("Threads_")) == 0)
680 {
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;
689 }
690 else if (strncmp (key, "Table_locks_",
691 strlen ("Table_locks_")) == 0)
692 {
693 counter_submit ("mysql_locks",
694 key + strlen ("Table_locks_"),
695 val, db);
696 }
697 }
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);
719 return (0);
720 } /* int mysql_read */
722 void module_register (void)
723 {
724 plugin_register_complex_config ("mysql", mysql_config);
725 } /* void module_register */