6f2d69abbfd6501b830830f25c27a15b0ed4d5de
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 int state;
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->state != 0)
241 {
242 int err;
243 if ((err = mysql_ping (db->con)) != 0)
244 {
245 /* Assured by "mysql_config_database" */
246 assert (db->instance != NULL);
247 WARNING ("mysql_ping failed for instance \"%s\": %s",
248 db->instance,
249 mysql_error (db->con));
250 db->state = 0;
251 }
252 else
253 {
254 db->state = 1;
255 return (db->con);
256 }
257 }
259 if ((db->con = mysql_init (db->con)) == NULL)
260 {
261 ERROR ("mysql_init failed: %s", mysql_error (db->con));
262 db->state = 0;
263 return (NULL);
264 }
266 if (mysql_real_connect (db->con, db->host, db->user, db->pass,
267 db->database, db->port, db->socket, 0) == NULL)
268 {
269 ERROR ("mysql plugin: Failed to connect to database %s "
270 "at server %s: %s",
271 (db->database != NULL) ? db->database : "<none>",
272 (db->host != NULL) ? db->host : "localhost",
273 mysql_error (db->con));
274 db->state = 0;
275 return (NULL);
276 }
277 else
278 {
279 INFO ("mysql plugin: Successfully connected to database %s "
280 "at server %s (server version: %s, protocol version: %d)",
281 (db->database != NULL) ? db->database : "<none>",
282 mysql_get_host_info (db->con),
283 mysql_get_server_info (db->con),
284 mysql_get_proto_info (db->con));
285 db->state = 1;
286 return (db->con);
287 }
288 } /* static MYSQL *getconnection (mysql_database_t *db) */
290 static void set_host (mysql_database_t *db, char *buf, size_t buflen)
291 {
292 if ((db->host == NULL)
293 || (strcmp ("", db->host) == 0)
294 || (strcmp ("localhost", db->host) == 0))
295 sstrncpy (buf, hostname_g, buflen);
296 else
297 sstrncpy (buf, db->host, buflen);
298 } /* void set_host */
300 static void submit (const char *type, const char *type_instance,
301 value_t *values, size_t values_len, mysql_database_t *db)
302 {
303 value_list_t vl = VALUE_LIST_INIT;
305 vl.values = values;
306 vl.values_len = values_len;
308 set_host (db, vl.host, sizeof (vl.host));
310 sstrncpy (vl.plugin, "mysql", sizeof (vl.plugin));
312 /* Assured by "mysql_config_database" */
313 assert (db->instance != NULL);
314 sstrncpy (vl.plugin_instance, db->instance, sizeof (vl.plugin_instance));
316 sstrncpy (vl.type, type, sizeof (vl.type));
317 if (type_instance != NULL)
318 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
320 plugin_dispatch_values (&vl);
321 } /* submit */
323 static void counter_submit (const char *type, const char *type_instance,
324 derive_t value, mysql_database_t *db)
325 {
326 value_t values[1];
328 values[0].derive = value;
329 submit (type, type_instance, values, STATIC_ARRAY_SIZE (values), db);
330 } /* void counter_submit */
332 static void gauge_submit (const char *type, const char *type_instance,
333 gauge_t value, mysql_database_t *db)
334 {
335 value_t values[1];
337 values[0].gauge = value;
338 submit (type, type_instance, values, STATIC_ARRAY_SIZE (values), db);
339 } /* void gauge_submit */
341 static void derive_submit (const char *type, const char *type_instance,
342 derive_t value, mysql_database_t *db)
343 {
344 value_t values[1];
346 values[0].derive = value;
347 submit (type, type_instance, values, STATIC_ARRAY_SIZE (values), db);
348 } /* void derive_submit */
350 static void traffic_submit (derive_t rx, derive_t tx, mysql_database_t *db)
351 {
352 value_t values[2];
354 values[0].derive = rx;
355 values[1].derive = tx;
357 submit ("mysql_octets", NULL, values, STATIC_ARRAY_SIZE (values), db);
358 } /* void traffic_submit */
360 static MYSQL_RES *exec_query (MYSQL *con, const char *query)
361 {
362 MYSQL_RES *res;
364 int query_len = strlen (query);
366 if (mysql_real_query (con, query, query_len))
367 {
368 ERROR ("mysql plugin: Failed to execute query: %s",
369 mysql_error (con));
370 INFO ("mysql plugin: SQL query was: %s", query);
371 return (NULL);
372 }
374 res = mysql_store_result (con);
375 if (res == NULL)
376 {
377 ERROR ("mysql plugin: Failed to store query result: %s",
378 mysql_error (con));
379 INFO ("mysql plugin: SQL query was: %s", query);
380 return (NULL);
381 }
383 return (res);
384 } /* exec_query */
386 static int mysql_read_master_stats (mysql_database_t *db, MYSQL *con)
387 {
388 MYSQL_RES *res;
389 MYSQL_ROW row;
391 char *query;
392 int field_num;
393 unsigned long long position;
395 query = "SHOW MASTER STATUS";
397 res = exec_query (con, query);
398 if (res == NULL)
399 return (-1);
401 row = mysql_fetch_row (res);
402 if (row == NULL)
403 {
404 ERROR ("mysql plugin: Failed to get master statistics: "
405 "`%s' did not return any rows.", query);
406 mysql_free_result (res);
407 return (-1);
408 }
410 field_num = mysql_num_fields (res);
411 if (field_num < 2)
412 {
413 ERROR ("mysql plugin: Failed to get master statistics: "
414 "`%s' returned less than two columns.", query);
415 mysql_free_result (res);
416 return (-1);
417 }
419 position = atoll (row[1]);
420 counter_submit ("mysql_log_position", "master-bin", position, db);
422 row = mysql_fetch_row (res);
423 if (row != NULL)
424 WARNING ("mysql plugin: `%s' returned more than one row - "
425 "ignoring further results.", query);
427 mysql_free_result (res);
429 return (0);
430 } /* mysql_read_master_stats */
432 static int mysql_read_slave_stats (mysql_database_t *db, MYSQL *con)
433 {
434 MYSQL_RES *res;
435 MYSQL_ROW row;
437 char *query;
438 int field_num;
440 /* WTF? libmysqlclient does not seem to provide any means to
441 * translate a column name to a column index ... :-/ */
442 const int READ_MASTER_LOG_POS_IDX = 6;
443 const int SLAVE_IO_RUNNING_IDX = 10;
444 const int SLAVE_SQL_RUNNING_IDX = 11;
445 const int EXEC_MASTER_LOG_POS_IDX = 21;
446 const int SECONDS_BEHIND_MASTER_IDX = 32;
448 query = "SHOW SLAVE STATUS";
450 res = exec_query (con, query);
451 if (res == NULL)
452 return (-1);
454 row = mysql_fetch_row (res);
455 if (row == NULL)
456 {
457 ERROR ("mysql plugin: Failed to get slave statistics: "
458 "`%s' did not return any rows.", query);
459 mysql_free_result (res);
460 return (-1);
461 }
463 field_num = mysql_num_fields (res);
464 if (field_num < 33)
465 {
466 ERROR ("mysql plugin: Failed to get slave statistics: "
467 "`%s' returned less than 33 columns.", query);
468 mysql_free_result (res);
469 return (-1);
470 }
472 if (db->slave_stats)
473 {
474 unsigned long long counter;
475 double gauge;
477 counter = atoll (row[READ_MASTER_LOG_POS_IDX]);
478 counter_submit ("mysql_log_position", "slave-read", counter, db);
480 counter = atoll (row[EXEC_MASTER_LOG_POS_IDX]);
481 counter_submit ("mysql_log_position", "slave-exec", counter, db);
483 if (row[SECONDS_BEHIND_MASTER_IDX] != NULL)
484 {
485 gauge = atof (row[SECONDS_BEHIND_MASTER_IDX]);
486 gauge_submit ("time_offset", NULL, gauge, db);
487 }
488 }
490 if (db->slave_notif)
491 {
492 notification_t n = { 0, cdtime (), "", "",
493 "mysql", "", "time_offset", "", NULL };
495 char *io, *sql;
497 io = row[SLAVE_IO_RUNNING_IDX];
498 sql = row[SLAVE_SQL_RUNNING_IDX];
500 set_host (db, n.host, sizeof (n.host));
502 /* Assured by "mysql_config_database" */
503 assert (db->instance != NULL);
504 sstrncpy (n.plugin_instance, db->instance, sizeof (n.plugin_instance));
506 if (((io == NULL) || (strcasecmp (io, "yes") != 0))
507 && (db->slave_io_running))
508 {
509 n.severity = NOTIF_WARNING;
510 ssnprintf (n.message, sizeof (n.message),
511 "slave I/O thread not started or not connected to master");
512 plugin_dispatch_notification (&n);
513 db->slave_io_running = 0;
514 }
515 else if (((io != NULL) && (strcasecmp (io, "yes") == 0))
516 && (! db->slave_io_running))
517 {
518 n.severity = NOTIF_OKAY;
519 ssnprintf (n.message, sizeof (n.message),
520 "slave I/O thread started and connected to master");
521 plugin_dispatch_notification (&n);
522 db->slave_io_running = 1;
523 }
525 if (((sql == NULL) || (strcasecmp (sql, "yes") != 0))
526 && (db->slave_sql_running))
527 {
528 n.severity = NOTIF_WARNING;
529 ssnprintf (n.message, sizeof (n.message),
530 "slave SQL thread not started");
531 plugin_dispatch_notification (&n);
532 db->slave_sql_running = 0;
533 }
534 else if (((sql != NULL) && (strcasecmp (sql, "yes") == 0))
535 && (! db->slave_sql_running))
536 {
537 n.severity = NOTIF_OKAY;
538 ssnprintf (n.message, sizeof (n.message),
539 "slave SQL thread started");
540 plugin_dispatch_notification (&n);
541 db->slave_sql_running = 0;
542 }
543 }
545 row = mysql_fetch_row (res);
546 if (row != NULL)
547 WARNING ("mysql plugin: `%s' returned more than one row - "
548 "ignoring further results.", query);
550 mysql_free_result (res);
552 return (0);
553 } /* mysql_read_slave_stats */
555 static int mysql_read (user_data_t *ud)
556 {
557 mysql_database_t *db;
558 MYSQL *con;
559 MYSQL_RES *res;
560 MYSQL_ROW row;
561 char *query;
563 derive_t qcache_hits = 0;
564 derive_t qcache_inserts = 0;
565 derive_t qcache_not_cached = 0;
566 derive_t qcache_lowmem_prunes = 0;
567 gauge_t qcache_queries_in_cache = NAN;
569 gauge_t threads_running = NAN;
570 gauge_t threads_connected = NAN;
571 gauge_t threads_cached = NAN;
572 derive_t threads_created = 0;
574 unsigned long long traffic_incoming = 0ULL;
575 unsigned long long traffic_outgoing = 0ULL;
577 if ((ud == NULL) || (ud->data == NULL))
578 {
579 ERROR ("mysql plugin: mysql_database_read: Invalid user data.");
580 return (-1);
581 }
583 db = (mysql_database_t *) ud->data;
585 /* An error message will have been printed in this case */
586 if ((con = getconnection (db)) == NULL)
587 return (-1);
589 query = "SHOW STATUS";
590 if (mysql_get_server_version (con) >= 50002)
591 query = "SHOW GLOBAL STATUS";
593 res = exec_query (con, query);
594 if (res == NULL)
595 return (-1);
597 while ((row = mysql_fetch_row (res)))
598 {
599 char *key;
600 unsigned long long val;
602 key = row[0];
603 val = atoll (row[1]);
605 if (strncmp (key, "Com_",
606 strlen ("Com_")) == 0)
607 {
608 if (val == 0ULL)
609 continue;
611 /* Ignore `prepared statements' */
612 if (strncmp (key, "Com_stmt_", strlen ("Com_stmt_")) != 0)
613 counter_submit ("mysql_commands",
614 key + strlen ("Com_"),
615 val, db);
616 }
617 else if (strncmp (key, "Handler_",
618 strlen ("Handler_")) == 0)
619 {
620 if (val == 0ULL)
621 continue;
623 counter_submit ("mysql_handler",
624 key + strlen ("Handler_"),
625 val, db);
626 }
627 else if (strncmp (key, "Qcache_",
628 strlen ("Qcache_")) == 0)
629 {
630 if (strcmp (key, "Qcache_hits") == 0)
631 qcache_hits = (derive_t) val;
632 else if (strcmp (key, "Qcache_inserts") == 0)
633 qcache_inserts = (derive_t) val;
634 else if (strcmp (key, "Qcache_not_cached") == 0)
635 qcache_not_cached = (derive_t) val;
636 else if (strcmp (key, "Qcache_lowmem_prunes") == 0)
637 qcache_lowmem_prunes = (derive_t) val;
638 else if (strcmp (key, "Qcache_queries_in_cache") == 0)
639 qcache_queries_in_cache = (gauge_t) val;
640 }
641 else if (strncmp (key, "Bytes_",
642 strlen ("Bytes_")) == 0)
643 {
644 if (strcmp (key, "Bytes_received") == 0)
645 traffic_incoming += val;
646 else if (strcmp (key, "Bytes_sent") == 0)
647 traffic_outgoing += val;
648 }
649 else if (strncmp (key, "Threads_",
650 strlen ("Threads_")) == 0)
651 {
652 if (strcmp (key, "Threads_running") == 0)
653 threads_running = (gauge_t) val;
654 else if (strcmp (key, "Threads_connected") == 0)
655 threads_connected = (gauge_t) val;
656 else if (strcmp (key, "Threads_cached") == 0)
657 threads_cached = (gauge_t) val;
658 else if (strcmp (key, "Threads_created") == 0)
659 threads_created = (derive_t) val;
660 }
661 else if (strncmp (key, "Table_locks_",
662 strlen ("Table_locks_")) == 0)
663 {
664 counter_submit ("mysql_locks",
665 key + strlen ("Table_locks_"),
666 val, db);
667 }
668 }
669 mysql_free_result (res); res = NULL;
671 if ((qcache_hits != 0)
672 || (qcache_inserts != 0)
673 || (qcache_not_cached != 0)
674 || (qcache_lowmem_prunes != 0))
675 {
676 derive_submit ("cache_result", "qcache-hits",
677 qcache_hits, db);
678 derive_submit ("cache_result", "qcache-inserts",
679 qcache_inserts, db);
680 derive_submit ("cache_result", "qcache-not_cached",
681 qcache_not_cached, db);
682 derive_submit ("cache_result", "qcache-prunes",
683 qcache_lowmem_prunes, db);
685 gauge_submit ("cache_size", "qcache",
686 qcache_queries_in_cache, db);
687 }
689 if (threads_created != 0)
690 {
691 gauge_submit ("threads", "running",
692 threads_running, db);
693 gauge_submit ("threads", "connected",
694 threads_connected, db);
695 gauge_submit ("threads", "cached",
696 threads_cached, db);
698 derive_submit ("total_threads", "created",
699 threads_created, db);
700 }
702 traffic_submit (traffic_incoming, traffic_outgoing, db);
704 if (db->master_stats)
705 mysql_read_master_stats (db, con);
707 if ((db->slave_stats) || (db->slave_notif))
708 mysql_read_slave_stats (db, con);
710 return (0);
711 } /* int mysql_read */
713 void module_register (void)
714 {
715 plugin_register_complex_config ("mysql", mysql_config);
716 } /* void module_register */