1 /**
2 * collectd - src/mysql.c
3 * Copyright (C) 2006-2010 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 collectd.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 char *instance;
46 char *host;
47 char *user;
48 char *pass;
49 char *database;
50 char *socket;
51 int port;
53 _Bool master_stats;
54 _Bool slave_stats;
56 _Bool slave_notif;
57 _Bool slave_io_running;
58 _Bool slave_sql_running;
60 MYSQL *con;
61 _Bool is_connected;
62 };
63 typedef struct mysql_database_s mysql_database_t; /* }}} */
65 static int mysql_read (user_data_t *ud);
67 static void mysql_database_free (void *arg) /* {{{ */
68 {
69 mysql_database_t *db;
71 DEBUG ("mysql plugin: mysql_database_free (arg = %p);", arg);
73 db = (mysql_database_t *) arg;
75 if (db == NULL)
76 return;
78 if (db->con != NULL)
79 mysql_close (db->con);
81 sfree (db->host);
82 sfree (db->user);
83 sfree (db->pass);
84 sfree (db->socket);
85 sfree (db->instance);
86 sfree (db->database);
87 sfree (db);
88 } /* }}} void mysql_database_free */
90 /* Configuration handling functions {{{
91 *
92 * <Plugin mysql>
93 * <Database "plugin_instance1">
94 * Host "localhost"
95 * Port 22000
96 * ...
97 * </Database>
98 * </Plugin>
99 */
100 static int mysql_config_database (oconfig_item_t *ci) /* {{{ */
101 {
102 mysql_database_t *db;
103 int status = 0;
104 int i;
106 if ((ci->values_num != 1)
107 || (ci->values[0].type != OCONFIG_TYPE_STRING))
108 {
109 WARNING ("mysql plugin: The `Database' block "
110 "needs exactly one string argument.");
111 return (-1);
112 }
114 db = (mysql_database_t *) malloc (sizeof (*db));
115 if (db == NULL)
116 {
117 ERROR ("mysql plugin: malloc failed.");
118 return (-1);
119 }
120 memset (db, 0, sizeof (*db));
122 /* initialize all the pointers */
123 db->host = NULL;
124 db->user = NULL;
125 db->pass = NULL;
126 db->database = NULL;
127 db->socket = NULL;
128 db->con = NULL;
130 /* trigger a notification, if it's not running */
131 db->slave_io_running = 1;
132 db->slave_sql_running = 1;
134 status = cf_util_get_string (ci, &db->instance);
135 if (status != 0)
136 {
137 sfree (db);
138 return (status);
139 }
140 assert (db->instance != NULL);
142 /* Fill the `mysql_database_t' structure.. */
143 for (i = 0; i < ci->children_num; i++)
144 {
145 oconfig_item_t *child = ci->children + i;
147 if (strcasecmp ("Host", child->key) == 0)
148 status = cf_util_get_string (child, &db->host);
149 else if (strcasecmp ("User", child->key) == 0)
150 status = cf_util_get_string (child, &db->user);
151 else if (strcasecmp ("Password", child->key) == 0)
152 status = cf_util_get_string (child, &db->pass);
153 else if (strcasecmp ("Port", child->key) == 0)
154 {
155 status = cf_util_get_port_number (child);
156 if (status > 0)
157 {
158 db->port = status;
159 status = 0;
160 }
161 }
162 else if (strcasecmp ("Socket", child->key) == 0)
163 status = cf_util_get_string (child, &db->socket);
164 else if (strcasecmp ("Database", child->key) == 0)
165 status = cf_util_get_string (child, &db->database);
166 else if (strcasecmp ("MasterStats", child->key) == 0)
167 status = cf_util_get_boolean (child, &db->master_stats);
168 else if (strcasecmp ("SlaveStats", child->key) == 0)
169 status = cf_util_get_boolean (child, &db->slave_stats);
170 else if (strcasecmp ("SlaveNotifications", child->key) == 0)
171 status = cf_util_get_boolean (child, &db->slave_notif);
172 else
173 {
174 WARNING ("mysql plugin: Option `%s' not allowed here.", child->key);
175 status = -1;
176 }
178 if (status != 0)
179 break;
180 }
182 /* If all went well, register this database for reading */
183 if (status == 0)
184 {
185 user_data_t ud;
186 char cb_name[DATA_MAX_NAME_LEN];
188 DEBUG ("mysql plugin: Registering new read callback: %s",
189 (db->database != NULL) ? db->database : "<default>");
191 memset (&ud, 0, sizeof (ud));
192 ud.data = (void *) db;
193 ud.free_func = mysql_database_free;
195 if (db->instance != NULL)
196 ssnprintf (cb_name, sizeof (cb_name), "mysql-%s",
197 db->instance);
198 else
199 sstrncpy (cb_name, "mysql", sizeof (cb_name));
201 plugin_register_complex_read (/* group = */ NULL, cb_name,
202 mysql_read,
203 /* interval = */ NULL, &ud);
204 }
205 else
206 {
207 mysql_database_free (db);
208 return (-1);
209 }
211 return (0);
212 } /* }}} int mysql_config_database */
214 static int mysql_config (oconfig_item_t *ci) /* {{{ */
215 {
216 int i;
218 if (ci == NULL)
219 return (EINVAL);
221 /* Fill the `mysql_database_t' structure.. */
222 for (i = 0; i < ci->children_num; i++)
223 {
224 oconfig_item_t *child = ci->children + i;
226 if (strcasecmp ("Database", child->key) == 0)
227 mysql_config_database (child);
228 else
229 WARNING ("mysql plugin: Option \"%s\" not allowed here.",
230 child->key);
231 }
233 return (0);
234 } /* }}} int mysql_config */
236 /* }}} End of configuration handling functions */
238 static MYSQL *getconnection (mysql_database_t *db)
239 {
240 if (db->is_connected)
241 {
242 int status;
244 status = mysql_ping (db->con);
245 if (status == 0)
246 return (db->con);
248 WARNING ("mysql plugin: Lost connection to instance \"%s\": %s",
249 db->instance, mysql_error (db->con));
250 }
251 db->is_connected = 0;
253 if (db->con == NULL)
254 {
255 db->con = mysql_init (NULL);
256 if (db->con == NULL)
257 {
258 ERROR ("mysql plugin: mysql_init failed: %s",
259 mysql_error (db->con));
260 return (NULL);
261 }
262 }
264 if (mysql_real_connect (db->con, db->host, db->user, db->pass,
265 db->database, db->port, db->socket, 0) == NULL)
266 {
267 ERROR ("mysql plugin: Failed to connect to database %s "
268 "at server %s: %s",
269 (db->database != NULL) ? db->database : "<none>",
270 (db->host != NULL) ? db->host : "localhost",
271 mysql_error (db->con));
272 return (NULL);
273 }
275 INFO ("mysql plugin: Successfully connected to database %s "
276 "at server %s (server version: %s, protocol version: %d)",
277 (db->database != NULL) ? db->database : "<none>",
278 mysql_get_host_info (db->con),
279 mysql_get_server_info (db->con),
280 mysql_get_proto_info (db->con));
282 db->is_connected = 1;
283 return (db->con);
284 } /* static MYSQL *getconnection (mysql_database_t *db) */
286 static void set_host (mysql_database_t *db, char *buf, size_t buflen)
287 {
288 if ((db->host == NULL)
289 || (strcmp ("", db->host) == 0)
290 || (strcmp ("localhost", db->host) == 0))
291 sstrncpy (buf, hostname_g, buflen);
292 else
293 sstrncpy (buf, db->host, buflen);
294 } /* void set_host */
296 static void submit (const char *type, const char *type_instance,
297 value_t *values, size_t values_len, mysql_database_t *db)
298 {
299 value_list_t vl = VALUE_LIST_INIT;
301 vl.values = values;
302 vl.values_len = values_len;
304 set_host (db, vl.host, sizeof (vl.host));
306 sstrncpy (vl.plugin, "mysql", sizeof (vl.plugin));
308 /* Assured by "mysql_config_database" */
309 assert (db->instance != NULL);
310 sstrncpy (vl.plugin_instance, db->instance, sizeof (vl.plugin_instance));
312 sstrncpy (vl.type, type, sizeof (vl.type));
313 if (type_instance != NULL)
314 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
316 plugin_dispatch_values (&vl);
317 } /* submit */
319 static void counter_submit (const char *type, const char *type_instance,
320 derive_t value, mysql_database_t *db)
321 {
322 value_t values[1];
324 values[0].derive = value;
325 submit (type, type_instance, values, STATIC_ARRAY_SIZE (values), db);
326 } /* void counter_submit */
328 static void gauge_submit (const char *type, const char *type_instance,
329 gauge_t value, mysql_database_t *db)
330 {
331 value_t values[1];
333 values[0].gauge = value;
334 submit (type, type_instance, values, STATIC_ARRAY_SIZE (values), db);
335 } /* void gauge_submit */
337 static void derive_submit (const char *type, const char *type_instance,
338 derive_t value, mysql_database_t *db)
339 {
340 value_t values[1];
342 values[0].derive = value;
343 submit (type, type_instance, values, STATIC_ARRAY_SIZE (values), db);
344 } /* void derive_submit */
346 static void traffic_submit (derive_t rx, derive_t tx, mysql_database_t *db)
347 {
348 value_t values[2];
350 values[0].derive = rx;
351 values[1].derive = tx;
353 submit ("mysql_octets", NULL, values, STATIC_ARRAY_SIZE (values), db);
354 } /* void traffic_submit */
356 static MYSQL_RES *exec_query (MYSQL *con, const char *query)
357 {
358 MYSQL_RES *res;
360 int query_len = strlen (query);
362 if (mysql_real_query (con, query, query_len))
363 {
364 ERROR ("mysql plugin: Failed to execute query: %s",
365 mysql_error (con));
366 INFO ("mysql plugin: SQL query was: %s", query);
367 return (NULL);
368 }
370 res = mysql_store_result (con);
371 if (res == NULL)
372 {
373 ERROR ("mysql plugin: Failed to store query result: %s",
374 mysql_error (con));
375 INFO ("mysql plugin: SQL query was: %s", query);
376 return (NULL);
377 }
379 return (res);
380 } /* exec_query */
382 static int mysql_read_master_stats (mysql_database_t *db, MYSQL *con)
383 {
384 MYSQL_RES *res;
385 MYSQL_ROW row;
387 char *query;
388 int field_num;
389 unsigned long long position;
391 query = "SHOW MASTER STATUS";
393 res = exec_query (con, query);
394 if (res == NULL)
395 return (-1);
397 row = mysql_fetch_row (res);
398 if (row == NULL)
399 {
400 ERROR ("mysql plugin: Failed to get master statistics: "
401 "`%s' did not return any rows.", query);
402 mysql_free_result (res);
403 return (-1);
404 }
406 field_num = mysql_num_fields (res);
407 if (field_num < 2)
408 {
409 ERROR ("mysql plugin: Failed to get master statistics: "
410 "`%s' returned less than two columns.", query);
411 mysql_free_result (res);
412 return (-1);
413 }
415 position = atoll (row[1]);
416 counter_submit ("mysql_log_position", "master-bin", position, db);
418 row = mysql_fetch_row (res);
419 if (row != NULL)
420 WARNING ("mysql plugin: `%s' returned more than one row - "
421 "ignoring further results.", query);
423 mysql_free_result (res);
425 return (0);
426 } /* mysql_read_master_stats */
428 static int mysql_read_slave_stats (mysql_database_t *db, MYSQL *con)
429 {
430 MYSQL_RES *res;
431 MYSQL_ROW row;
433 char *query;
434 int field_num;
436 /* WTF? libmysqlclient does not seem to provide any means to
437 * translate a column name to a column index ... :-/ */
438 const int READ_MASTER_LOG_POS_IDX = 6;
439 const int SLAVE_IO_RUNNING_IDX = 10;
440 const int SLAVE_SQL_RUNNING_IDX = 11;
441 const int EXEC_MASTER_LOG_POS_IDX = 21;
442 const int SECONDS_BEHIND_MASTER_IDX = 32;
444 query = "SHOW SLAVE STATUS";
446 res = exec_query (con, query);
447 if (res == NULL)
448 return (-1);
450 row = mysql_fetch_row (res);
451 if (row == NULL)
452 {
453 ERROR ("mysql plugin: Failed to get slave statistics: "
454 "`%s' did not return any rows.", query);
455 mysql_free_result (res);
456 return (-1);
457 }
459 field_num = mysql_num_fields (res);
460 if (field_num < 33)
461 {
462 ERROR ("mysql plugin: Failed to get slave statistics: "
463 "`%s' returned less than 33 columns.", query);
464 mysql_free_result (res);
465 return (-1);
466 }
468 if (db->slave_stats)
469 {
470 unsigned long long counter;
471 double gauge;
473 counter = atoll (row[READ_MASTER_LOG_POS_IDX]);
474 counter_submit ("mysql_log_position", "slave-read", counter, db);
476 counter = atoll (row[EXEC_MASTER_LOG_POS_IDX]);
477 counter_submit ("mysql_log_position", "slave-exec", counter, db);
479 if (row[SECONDS_BEHIND_MASTER_IDX] != NULL)
480 {
481 gauge = atof (row[SECONDS_BEHIND_MASTER_IDX]);
482 gauge_submit ("time_offset", NULL, gauge, db);
483 }
484 }
486 if (db->slave_notif)
487 {
488 notification_t n = { 0, cdtime (), "", "",
489 "mysql", "", "time_offset", "", NULL };
491 char *io, *sql;
493 io = row[SLAVE_IO_RUNNING_IDX];
494 sql = row[SLAVE_SQL_RUNNING_IDX];
496 set_host (db, n.host, sizeof (n.host));
498 /* Assured by "mysql_config_database" */
499 assert (db->instance != NULL);
500 sstrncpy (n.plugin_instance, db->instance, sizeof (n.plugin_instance));
502 if (((io == NULL) || (strcasecmp (io, "yes") != 0))
503 && (db->slave_io_running))
504 {
505 n.severity = NOTIF_WARNING;
506 ssnprintf (n.message, sizeof (n.message),
507 "slave I/O thread not started or not connected to master");
508 plugin_dispatch_notification (&n);
509 db->slave_io_running = 0;
510 }
511 else if (((io != NULL) && (strcasecmp (io, "yes") == 0))
512 && (! db->slave_io_running))
513 {
514 n.severity = NOTIF_OKAY;
515 ssnprintf (n.message, sizeof (n.message),
516 "slave I/O thread started and connected to master");
517 plugin_dispatch_notification (&n);
518 db->slave_io_running = 1;
519 }
521 if (((sql == NULL) || (strcasecmp (sql, "yes") != 0))
522 && (db->slave_sql_running))
523 {
524 n.severity = NOTIF_WARNING;
525 ssnprintf (n.message, sizeof (n.message),
526 "slave SQL thread not started");
527 plugin_dispatch_notification (&n);
528 db->slave_sql_running = 0;
529 }
530 else if (((sql != NULL) && (strcasecmp (sql, "yes") == 0))
531 && (! db->slave_sql_running))
532 {
533 n.severity = NOTIF_OKAY;
534 ssnprintf (n.message, sizeof (n.message),
535 "slave SQL thread started");
536 plugin_dispatch_notification (&n);
537 db->slave_sql_running = 1;
538 }
539 }
541 row = mysql_fetch_row (res);
542 if (row != NULL)
543 WARNING ("mysql plugin: `%s' returned more than one row - "
544 "ignoring further results.", query);
546 mysql_free_result (res);
548 return (0);
549 } /* mysql_read_slave_stats */
551 static int mysql_read (user_data_t *ud)
552 {
553 mysql_database_t *db;
554 MYSQL *con;
555 MYSQL_RES *res;
556 MYSQL_ROW row;
557 char *query;
559 derive_t qcache_hits = 0;
560 derive_t qcache_inserts = 0;
561 derive_t qcache_not_cached = 0;
562 derive_t qcache_lowmem_prunes = 0;
563 gauge_t qcache_queries_in_cache = NAN;
565 gauge_t threads_running = NAN;
566 gauge_t threads_connected = NAN;
567 gauge_t threads_cached = NAN;
568 derive_t threads_created = 0;
570 unsigned long long traffic_incoming = 0ULL;
571 unsigned long long traffic_outgoing = 0ULL;
573 if ((ud == NULL) || (ud->data == NULL))
574 {
575 ERROR ("mysql plugin: mysql_database_read: Invalid user data.");
576 return (-1);
577 }
579 db = (mysql_database_t *) ud->data;
581 /* An error message will have been printed in this case */
582 if ((con = getconnection (db)) == NULL)
583 return (-1);
585 query = "SHOW STATUS";
586 if (mysql_get_server_version (con) >= 50002)
587 query = "SHOW GLOBAL STATUS";
589 res = exec_query (con, query);
590 if (res == NULL)
591 return (-1);
593 while ((row = mysql_fetch_row (res)))
594 {
595 char *key;
596 unsigned long long val;
598 key = row[0];
599 val = atoll (row[1]);
601 if (strncmp (key, "Com_",
602 strlen ("Com_")) == 0)
603 {
604 if (val == 0ULL)
605 continue;
607 /* Ignore `prepared statements' */
608 if (strncmp (key, "Com_stmt_", strlen ("Com_stmt_")) != 0)
609 counter_submit ("mysql_commands",
610 key + strlen ("Com_"),
611 val, db);
612 }
613 else if (strncmp (key, "Handler_",
614 strlen ("Handler_")) == 0)
615 {
616 if (val == 0ULL)
617 continue;
619 counter_submit ("mysql_handler",
620 key + strlen ("Handler_"),
621 val, db);
622 }
623 else if (strncmp (key, "Qcache_",
624 strlen ("Qcache_")) == 0)
625 {
626 if (strcmp (key, "Qcache_hits") == 0)
627 qcache_hits = (derive_t) val;
628 else if (strcmp (key, "Qcache_inserts") == 0)
629 qcache_inserts = (derive_t) val;
630 else if (strcmp (key, "Qcache_not_cached") == 0)
631 qcache_not_cached = (derive_t) val;
632 else if (strcmp (key, "Qcache_lowmem_prunes") == 0)
633 qcache_lowmem_prunes = (derive_t) val;
634 else if (strcmp (key, "Qcache_queries_in_cache") == 0)
635 qcache_queries_in_cache = (gauge_t) val;
636 }
637 else if (strncmp (key, "Bytes_",
638 strlen ("Bytes_")) == 0)
639 {
640 if (strcmp (key, "Bytes_received") == 0)
641 traffic_incoming += val;
642 else if (strcmp (key, "Bytes_sent") == 0)
643 traffic_outgoing += val;
644 }
645 else if (strncmp (key, "Threads_",
646 strlen ("Threads_")) == 0)
647 {
648 if (strcmp (key, "Threads_running") == 0)
649 threads_running = (gauge_t) val;
650 else if (strcmp (key, "Threads_connected") == 0)
651 threads_connected = (gauge_t) val;
652 else if (strcmp (key, "Threads_cached") == 0)
653 threads_cached = (gauge_t) val;
654 else if (strcmp (key, "Threads_created") == 0)
655 threads_created = (derive_t) val;
656 }
657 else if (strncmp (key, "Table_locks_",
658 strlen ("Table_locks_")) == 0)
659 {
660 counter_submit ("mysql_locks",
661 key + strlen ("Table_locks_"),
662 val, db);
663 }
664 }
665 mysql_free_result (res); res = NULL;
667 if ((qcache_hits != 0)
668 || (qcache_inserts != 0)
669 || (qcache_not_cached != 0)
670 || (qcache_lowmem_prunes != 0))
671 {
672 derive_submit ("cache_result", "qcache-hits",
673 qcache_hits, db);
674 derive_submit ("cache_result", "qcache-inserts",
675 qcache_inserts, db);
676 derive_submit ("cache_result", "qcache-not_cached",
677 qcache_not_cached, db);
678 derive_submit ("cache_result", "qcache-prunes",
679 qcache_lowmem_prunes, db);
681 gauge_submit ("cache_size", "qcache",
682 qcache_queries_in_cache, db);
683 }
685 if (threads_created != 0)
686 {
687 gauge_submit ("threads", "running",
688 threads_running, db);
689 gauge_submit ("threads", "connected",
690 threads_connected, db);
691 gauge_submit ("threads", "cached",
692 threads_cached, db);
694 derive_submit ("total_threads", "created",
695 threads_created, db);
696 }
698 traffic_submit (traffic_incoming, traffic_outgoing, db);
700 if (db->master_stats)
701 mysql_read_master_stats (db, con);
703 if ((db->slave_stats) || (db->slave_notif))
704 mysql_read_slave_stats (db, con);
706 return (0);
707 } /* int mysql_read */
709 void module_register (void)
710 {
711 plugin_register_complex_config ("mysql", mysql_config);
712 } /* void module_register */