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 derive_submit (const char *type, const char *type_instance,
356 derive_t value, mysql_database_t *db)
357 {
358 value_t values[1];
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)
365 {
366 value_t values[2];
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)
375 {
376 MYSQL_RES *res;
378 int query_len = strlen (query);
380 if (mysql_real_query (con, query, query_len))
381 {
382 ERROR ("mysql plugin: Failed to execute query: %s",
383 mysql_error (con));
384 INFO ("mysql plugin: SQL query was: %s", query);
385 return (NULL);
386 }
388 res = mysql_store_result (con);
389 if (res == NULL)
390 {
391 ERROR ("mysql plugin: Failed to store query result: %s",
392 mysql_error (con));
393 INFO ("mysql plugin: SQL query was: %s", query);
394 return (NULL);
395 }
397 return (res);
398 } /* exec_query */
400 static int mysql_read_master_stats (mysql_database_t *db, MYSQL *con)
401 {
402 MYSQL_RES *res;
403 MYSQL_ROW row;
405 char *query;
406 int field_num;
407 unsigned long long position;
409 query = "SHOW MASTER STATUS";
411 res = exec_query (con, query);
412 if (res == NULL)
413 return (-1);
415 row = mysql_fetch_row (res);
416 if (row == NULL)
417 {
418 ERROR ("mysql plugin: Failed to get master statistics: "
419 "`%s' did not return any rows.", query);
420 return (-1);
421 }
423 field_num = mysql_num_fields (res);
424 if (field_num < 2)
425 {
426 ERROR ("mysql plugin: Failed to get master statistics: "
427 "`%s' returned less than two columns.", query);
428 return (-1);
429 }
431 position = atoll (row[1]);
432 counter_submit ("mysql_log_position", "master-bin", position, db);
434 row = mysql_fetch_row (res);
435 if (row != NULL)
436 WARNING ("mysql plugin: `%s' returned more than one row - "
437 "ignoring further results.", query);
439 mysql_free_result (res);
441 return (0);
442 } /* mysql_read_master_stats */
444 static int mysql_read_slave_stats (mysql_database_t *db, MYSQL *con)
445 {
446 MYSQL_RES *res;
447 MYSQL_ROW row;
449 char *query;
450 int field_num;
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);
463 if (res == NULL)
464 return (-1);
466 row = mysql_fetch_row (res);
467 if (row == NULL)
468 {
469 ERROR ("mysql plugin: Failed to get slave statistics: "
470 "`%s' did not return any rows.", query);
471 return (-1);
472 }
474 field_num = mysql_num_fields (res);
475 if (field_num < 33)
476 {
477 ERROR ("mysql plugin: Failed to get slave statistics: "
478 "`%s' returned less than 33 columns.", query);
479 return (-1);
480 }
482 if (db->slave_stats)
483 {
484 unsigned long long counter;
485 double gauge;
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)
494 {
495 gauge = atof (row[SECONDS_BEHIND_MASTER_IDX]);
496 gauge_submit ("time_offset", NULL, gauge, db);
497 }
498 }
500 if (db->slave_notif)
501 {
502 notification_t n = { 0, time (NULL), "", "",
503 "mysql", "", "time_offset", "", NULL };
505 char *io, *sql;
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))
516 {
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;
522 }
523 else if (((io != NULL) && (strcasecmp (io, "yes") == 0))
524 && (! db->slave_io_running))
525 {
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;
531 }
533 if (((sql == NULL) || (strcasecmp (sql, "yes") != 0))
534 && (db->slave_sql_running))
535 {
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;
541 }
542 else if (((sql != NULL) && (strcasecmp (sql, "yes") == 0))
543 && (! db->slave_sql_running))
544 {
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;
550 }
551 }
553 row = mysql_fetch_row (res);
554 if (row != NULL)
555 WARNING ("mysql plugin: `%s' returned more than one row - "
556 "ignoring further results.", query);
558 mysql_free_result (res);
560 return (0);
561 } /* mysql_read_slave_stats */
563 static int mysql_read (user_data_t *ud)
564 {
565 mysql_database_t *db;
566 MYSQL *con;
567 MYSQL_RES *res;
568 MYSQL_ROW row;
569 char *query;
570 int field_num;
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))
587 {
588 ERROR ("mysql plugin: mysql_database_read: Invalid user data.");
589 return (-1);
590 }
592 db = (mysql_database_t *) ud->data;
594 /* An error message will have been printed in this case */
595 if ((con = getconnection (db)) == NULL)
596 return (-1);
598 query = "SHOW STATUS";
599 if (mysql_get_server_version (con) >= 50002)
600 query = "SHOW GLOBAL STATUS";
602 res = exec_query (con, query);
603 if (res == NULL)
604 return (-1);
606 field_num = mysql_num_fields (res);
607 while ((row = mysql_fetch_row (res)))
608 {
609 char *key;
610 unsigned long long val;
612 key = row[0];
613 val = atoll (row[1]);
615 if (strncmp (key, "Com_",
616 strlen ("Com_")) == 0)
617 {
618 if (val == 0ULL)
619 continue;
621 /* Ignore `prepared statements' */
622 if (strncmp (key, "Com_stmt_", strlen ("Com_stmt_")) != 0)
623 counter_submit ("mysql_commands",
624 key + strlen ("Com_"),
625 val, db);
626 }
627 else if (strncmp (key, "Handler_",
628 strlen ("Handler_")) == 0)
629 {
630 if (val == 0ULL)
631 continue;
633 counter_submit ("mysql_handler",
634 key + strlen ("Handler_"),
635 val, db);
636 }
637 else if (strncmp (key, "Qcache_",
638 strlen ("Qcache_")) == 0)
639 {
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;
650 }
651 else if (strncmp (key, "Bytes_",
652 strlen ("Bytes_")) == 0)
653 {
654 if (strcmp (key, "Bytes_received") == 0)
655 traffic_incoming += val;
656 else if (strcmp (key, "Bytes_sent") == 0)
657 traffic_outgoing += val;
658 }
659 else if (strncmp (key, "Threads_",
660 strlen ("Threads_")) == 0)
661 {
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;
670 }
671 else if (strncmp (key, "Table_locks_",
672 strlen ("Table_locks_")) == 0)
673 {
674 counter_submit ("mysql_locks",
675 key + strlen ("Table_locks_"),
676 val, db);
677 }
678 }
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))
685 {
686 derive_submit ("cache_result", "qcache-hits",
687 qcache_hits, db);
688 derive_submit ("cache_result", "qcache-inserts",
689 qcache_inserts, db);
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);
697 }
699 if (threads_created != 0)
700 {
701 gauge_submit ("threads", "running",
702 threads_running, db);
703 gauge_submit ("threads", "connected",
704 threads_connected, db);
705 gauge_submit ("threads", "cached",
706 threads_cached, db);
708 derive_submit ("total_threads", "created",
709 threads_created, db);
710 }
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);
720 return (0);
721 } /* int mysql_read */
723 void module_register (void)
724 {
725 plugin_register_complex_config ("mysql", mysql_config);
726 } /* void module_register */