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 struct mysql_database_s /* {{{ */
42 {
43 char *instance;
44 char *alias;
45 char *host;
46 char *user;
47 char *pass;
48 char *database;
49 char *socket;
50 int port;
51 int timeout;
53 _Bool master_stats;
54 _Bool slave_stats;
55 _Bool innodb_stats;
57 _Bool slave_notif;
58 _Bool slave_io_running;
59 _Bool slave_sql_running;
61 MYSQL *con;
62 _Bool is_connected;
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->alias);
83 sfree (db->host);
84 sfree (db->user);
85 sfree (db->pass);
86 sfree (db->socket);
87 sfree (db->instance);
88 sfree (db->database);
89 sfree (db);
90 } /* }}} void mysql_database_free */
92 /* Configuration handling functions {{{
93 *
94 * <Plugin mysql>
95 * <Database "plugin_instance1">
96 * Host "localhost"
97 * Port 22000
98 * ...
99 * </Database>
100 * </Plugin>
101 */
102 static int mysql_config_database (oconfig_item_t *ci) /* {{{ */
103 {
104 mysql_database_t *db;
105 int status = 0;
106 int i;
108 if ((ci->values_num != 1)
109 || (ci->values[0].type != OCONFIG_TYPE_STRING))
110 {
111 WARNING ("mysql plugin: The `Database' block "
112 "needs exactly one string argument.");
113 return (-1);
114 }
116 db = calloc (1, sizeof (*db));
117 if (db == NULL)
118 {
119 ERROR ("mysql plugin: calloc failed.");
120 return (-1);
121 }
123 /* initialize all the pointers */
124 db->alias = NULL;
125 db->host = NULL;
126 db->user = NULL;
127 db->pass = NULL;
128 db->database = NULL;
129 db->socket = NULL;
130 db->con = NULL;
131 db->timeout = 0;
133 /* trigger a notification, if it's not running */
134 db->slave_io_running = 1;
135 db->slave_sql_running = 1;
137 status = cf_util_get_string (ci, &db->instance);
138 if (status != 0)
139 {
140 sfree (db);
141 return (status);
142 }
143 assert (db->instance != NULL);
145 /* Fill the `mysql_database_t' structure.. */
146 for (i = 0; i < ci->children_num; i++)
147 {
148 oconfig_item_t *child = ci->children + i;
150 if (strcasecmp ("Alias", child->key) == 0)
151 status = cf_util_get_string (child, &db->alias);
152 else if (strcasecmp ("Host", child->key) == 0)
153 status = cf_util_get_string (child, &db->host);
154 else if (strcasecmp ("User", child->key) == 0)
155 status = cf_util_get_string (child, &db->user);
156 else if (strcasecmp ("Password", child->key) == 0)
157 status = cf_util_get_string (child, &db->pass);
158 else if (strcasecmp ("Port", child->key) == 0)
159 {
160 status = cf_util_get_port_number (child);
161 if (status > 0)
162 {
163 db->port = status;
164 status = 0;
165 }
166 }
167 else if (strcasecmp ("Socket", child->key) == 0)
168 status = cf_util_get_string (child, &db->socket);
169 else if (strcasecmp ("Database", child->key) == 0)
170 status = cf_util_get_string (child, &db->database);
171 else if (strcasecmp ("ConnectTimeout", child->key) == 0)
172 status = cf_util_get_int (child, &db->timeout);
173 else if (strcasecmp ("MasterStats", child->key) == 0)
174 status = cf_util_get_boolean (child, &db->master_stats);
175 else if (strcasecmp ("SlaveStats", child->key) == 0)
176 status = cf_util_get_boolean (child, &db->slave_stats);
177 else if (strcasecmp ("SlaveNotifications", child->key) == 0)
178 status = cf_util_get_boolean (child, &db->slave_notif);
179 else if (strcasecmp ("InnodbStats", child->key) == 0)
180 status = cf_util_get_boolean (child, &db->innodb_stats);
181 else
182 {
183 WARNING ("mysql plugin: Option `%s' not allowed here.", child->key);
184 status = -1;
185 }
187 if (status != 0)
188 break;
189 }
191 /* If all went well, register this database for reading */
192 if (status == 0)
193 {
194 user_data_t ud;
195 char cb_name[DATA_MAX_NAME_LEN];
197 DEBUG ("mysql plugin: Registering new read callback: %s",
198 (db->database != NULL) ? db->database : "<default>");
200 memset (&ud, 0, sizeof (ud));
201 ud.data = (void *) db;
202 ud.free_func = mysql_database_free;
204 if (db->instance != NULL)
205 ssnprintf (cb_name, sizeof (cb_name), "mysql-%s",
206 db->instance);
207 else
208 sstrncpy (cb_name, "mysql", sizeof (cb_name));
210 plugin_register_complex_read (/* group = */ NULL, cb_name,
211 mysql_read,
212 /* interval = */ 0, &ud);
213 }
214 else
215 {
216 mysql_database_free (db);
217 return (-1);
218 }
220 return (0);
221 } /* }}} int mysql_config_database */
223 static int mysql_config (oconfig_item_t *ci) /* {{{ */
224 {
225 int i;
227 if (ci == NULL)
228 return (EINVAL);
230 /* Fill the `mysql_database_t' structure.. */
231 for (i = 0; i < ci->children_num; i++)
232 {
233 oconfig_item_t *child = ci->children + i;
235 if (strcasecmp ("Database", child->key) == 0)
236 mysql_config_database (child);
237 else
238 WARNING ("mysql plugin: Option \"%s\" not allowed here.",
239 child->key);
240 }
242 return (0);
243 } /* }}} int mysql_config */
245 /* }}} End of configuration handling functions */
247 static MYSQL *getconnection (mysql_database_t *db)
248 {
249 if (db->is_connected)
250 {
251 int status;
253 status = mysql_ping (db->con);
254 if (status == 0)
255 return (db->con);
257 WARNING ("mysql plugin: Lost connection to instance \"%s\": %s",
258 db->instance, mysql_error (db->con));
259 }
260 db->is_connected = 0;
262 if (db->con == NULL)
263 {
264 db->con = mysql_init (NULL);
265 if (db->con == NULL)
266 {
267 ERROR ("mysql plugin: mysql_init failed: %s",
268 mysql_error (db->con));
269 return (NULL);
270 }
271 }
273 /* Configure TCP connect timeout (default: 0) */
274 db->con->options.connect_timeout = db->timeout;
276 if (mysql_real_connect (db->con, db->host, db->user, db->pass,
277 db->database, db->port, db->socket, 0) == NULL)
278 {
279 ERROR ("mysql plugin: Failed to connect to database %s "
280 "at server %s: %s",
281 (db->database != NULL) ? db->database : "<none>",
282 (db->host != NULL) ? db->host : "localhost",
283 mysql_error (db->con));
284 return (NULL);
285 }
287 INFO ("mysql plugin: Successfully connected to database %s "
288 "at server %s (server version: %s, protocol version: %d)",
289 (db->database != NULL) ? db->database : "<none>",
290 mysql_get_host_info (db->con),
291 mysql_get_server_info (db->con),
292 mysql_get_proto_info (db->con));
294 db->is_connected = 1;
295 return (db->con);
296 } /* static MYSQL *getconnection (mysql_database_t *db) */
298 static void set_host (mysql_database_t *db, char *buf, size_t buflen)
299 {
300 if (db->alias)
301 sstrncpy (buf, db->alias, buflen);
302 else if ((db->host == NULL)
303 || (strcmp ("", db->host) == 0)
304 || (strcmp ("127.0.0.1", db->host) == 0)
305 || (strcmp ("localhost", db->host) == 0))
306 sstrncpy (buf, hostname_g, buflen);
307 else
308 sstrncpy (buf, db->host, buflen);
309 } /* void set_host */
311 static void submit (const char *type, const char *type_instance,
312 value_t *values, size_t values_len, mysql_database_t *db)
313 {
314 value_list_t vl = VALUE_LIST_INIT;
316 vl.values = values;
317 vl.values_len = values_len;
319 set_host (db, vl.host, sizeof (vl.host));
321 sstrncpy (vl.plugin, "mysql", sizeof (vl.plugin));
323 /* Assured by "mysql_config_database" */
324 assert (db->instance != NULL);
325 sstrncpy (vl.plugin_instance, db->instance, sizeof (vl.plugin_instance));
327 sstrncpy (vl.type, type, sizeof (vl.type));
328 if (type_instance != NULL)
329 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
331 plugin_dispatch_values (&vl);
332 } /* submit */
334 static void counter_submit (const char *type, const char *type_instance,
335 derive_t value, mysql_database_t *db)
336 {
337 value_t values[1];
339 values[0].derive = value;
340 submit (type, type_instance, values, STATIC_ARRAY_SIZE (values), db);
341 } /* void counter_submit */
343 static void gauge_submit (const char *type, const char *type_instance,
344 gauge_t value, mysql_database_t *db)
345 {
346 value_t values[1];
348 values[0].gauge = value;
349 submit (type, type_instance, values, STATIC_ARRAY_SIZE (values), db);
350 } /* void gauge_submit */
352 static void derive_submit (const char *type, const char *type_instance,
353 derive_t value, mysql_database_t *db)
354 {
355 value_t values[1];
357 values[0].derive = value;
358 submit (type, type_instance, values, STATIC_ARRAY_SIZE (values), db);
359 } /* void derive_submit */
361 static void traffic_submit (derive_t rx, derive_t tx, mysql_database_t *db)
362 {
363 value_t values[2];
365 values[0].derive = rx;
366 values[1].derive = tx;
368 submit ("mysql_octets", NULL, values, STATIC_ARRAY_SIZE (values), db);
369 } /* void traffic_submit */
371 static MYSQL_RES *exec_query (MYSQL *con, const char *query)
372 {
373 MYSQL_RES *res;
375 int query_len = strlen (query);
377 if (mysql_real_query (con, query, query_len))
378 {
379 ERROR ("mysql plugin: Failed to execute query: %s",
380 mysql_error (con));
381 INFO ("mysql plugin: SQL query was: %s", query);
382 return (NULL);
383 }
385 res = mysql_store_result (con);
386 if (res == NULL)
387 {
388 ERROR ("mysql plugin: Failed to store query result: %s",
389 mysql_error (con));
390 INFO ("mysql plugin: SQL query was: %s", query);
391 return (NULL);
392 }
394 return (res);
395 } /* exec_query */
397 static int mysql_read_master_stats (mysql_database_t *db, MYSQL *con)
398 {
399 MYSQL_RES *res;
400 MYSQL_ROW row;
402 const char *query;
403 int field_num;
404 unsigned long long position;
406 query = "SHOW MASTER STATUS";
408 res = exec_query (con, query);
409 if (res == NULL)
410 return (-1);
412 row = mysql_fetch_row (res);
413 if (row == NULL)
414 {
415 ERROR ("mysql plugin: Failed to get master statistics: "
416 "`%s' did not return any rows.", query);
417 mysql_free_result (res);
418 return (-1);
419 }
421 field_num = mysql_num_fields (res);
422 if (field_num < 2)
423 {
424 ERROR ("mysql plugin: Failed to get master statistics: "
425 "`%s' returned less than two columns.", query);
426 mysql_free_result (res);
427 return (-1);
428 }
430 position = atoll (row[1]);
431 counter_submit ("mysql_log_position", "master-bin", position, db);
433 row = mysql_fetch_row (res);
434 if (row != NULL)
435 WARNING ("mysql plugin: `%s' returned more than one row - "
436 "ignoring further results.", query);
438 mysql_free_result (res);
440 return (0);
441 } /* mysql_read_master_stats */
443 static int mysql_read_slave_stats (mysql_database_t *db, MYSQL *con)
444 {
445 MYSQL_RES *res;
446 MYSQL_ROW row;
448 const char *query;
449 int field_num;
451 /* WTF? libmysqlclient does not seem to provide any means to
452 * translate a column name to a column index ... :-/ */
453 const int READ_MASTER_LOG_POS_IDX = 6;
454 const int SLAVE_IO_RUNNING_IDX = 10;
455 const int SLAVE_SQL_RUNNING_IDX = 11;
456 const int EXEC_MASTER_LOG_POS_IDX = 21;
457 const int SECONDS_BEHIND_MASTER_IDX = 32;
459 query = "SHOW SLAVE STATUS";
461 res = exec_query (con, query);
462 if (res == NULL)
463 return (-1);
465 row = mysql_fetch_row (res);
466 if (row == NULL)
467 {
468 ERROR ("mysql plugin: Failed to get slave statistics: "
469 "`%s' did not return any rows.", query);
470 mysql_free_result (res);
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 mysql_free_result (res);
480 return (-1);
481 }
483 if (db->slave_stats)
484 {
485 unsigned long long counter;
486 double gauge;
488 counter = atoll (row[READ_MASTER_LOG_POS_IDX]);
489 counter_submit ("mysql_log_position", "slave-read", counter, db);
491 counter = atoll (row[EXEC_MASTER_LOG_POS_IDX]);
492 counter_submit ("mysql_log_position", "slave-exec", counter, db);
494 if (row[SECONDS_BEHIND_MASTER_IDX] != NULL)
495 {
496 gauge = atof (row[SECONDS_BEHIND_MASTER_IDX]);
497 gauge_submit ("time_offset", NULL, gauge, db);
498 }
499 }
501 if (db->slave_notif)
502 {
503 notification_t n = { 0, cdtime (), "", "",
504 "mysql", "", "time_offset", "", NULL };
506 char *io, *sql;
508 io = row[SLAVE_IO_RUNNING_IDX];
509 sql = row[SLAVE_SQL_RUNNING_IDX];
511 set_host (db, n.host, sizeof (n.host));
513 /* Assured by "mysql_config_database" */
514 assert (db->instance != NULL);
515 sstrncpy (n.plugin_instance, db->instance, sizeof (n.plugin_instance));
517 if (((io == NULL) || (strcasecmp (io, "yes") != 0))
518 && (db->slave_io_running))
519 {
520 n.severity = NOTIF_WARNING;
521 ssnprintf (n.message, sizeof (n.message),
522 "slave I/O thread not started or not connected to master");
523 plugin_dispatch_notification (&n);
524 db->slave_io_running = 0;
525 }
526 else if (((io != NULL) && (strcasecmp (io, "yes") == 0))
527 && (! db->slave_io_running))
528 {
529 n.severity = NOTIF_OKAY;
530 ssnprintf (n.message, sizeof (n.message),
531 "slave I/O thread started and connected to master");
532 plugin_dispatch_notification (&n);
533 db->slave_io_running = 1;
534 }
536 if (((sql == NULL) || (strcasecmp (sql, "yes") != 0))
537 && (db->slave_sql_running))
538 {
539 n.severity = NOTIF_WARNING;
540 ssnprintf (n.message, sizeof (n.message),
541 "slave SQL thread not started");
542 plugin_dispatch_notification (&n);
543 db->slave_sql_running = 0;
544 }
545 else if (((sql != NULL) && (strcasecmp (sql, "yes") == 0))
546 && (! db->slave_sql_running))
547 {
548 n.severity = NOTIF_OKAY;
549 ssnprintf (n.message, sizeof (n.message),
550 "slave SQL thread started");
551 plugin_dispatch_notification (&n);
552 db->slave_sql_running = 1;
553 }
554 }
556 row = mysql_fetch_row (res);
557 if (row != NULL)
558 WARNING ("mysql plugin: `%s' returned more than one row - "
559 "ignoring further results.", query);
561 mysql_free_result (res);
563 return (0);
564 } /* mysql_read_slave_stats */
566 static int mysql_read_innodb_stats (mysql_database_t *db, MYSQL *con)
567 {
568 MYSQL_RES *res;
569 MYSQL_ROW row;
571 const char *query;
572 struct {
573 const char *key;
574 const char *type;
575 int ds_type;
576 } metrics[] = {
577 { "metadata_mem_pool_size", "bytes", DS_TYPE_GAUGE },
578 { "lock_deadlocks", "mysql_locks", DS_TYPE_DERIVE },
579 { "lock_timeouts", "mysql_locks", DS_TYPE_DERIVE },
580 { "lock_row_lock_current_waits", "mysql_locks", DS_TYPE_DERIVE },
581 { "buffer_pool_size", "bytes", DS_TYPE_GAUGE },
583 { "buffer_pool_reads", "operations", DS_TYPE_DERIVE },
584 { "buffer_pool_read_requests", "operations", DS_TYPE_DERIVE },
585 { "buffer_pool_write_requests", "operations", DS_TYPE_DERIVE },
586 { "buffer_pool_wait_free", "operations", DS_TYPE_DERIVE },
587 { "buffer_pool_read_ahead", "operations", DS_TYPE_DERIVE },
588 { "buffer_pool_read_ahead_evicted", "operations", DS_TYPE_DERIVE },
590 { "buffer_pool_pages_total", "gauge", DS_TYPE_GAUGE },
591 { "buffer_pool_pages_misc", "gauge", DS_TYPE_GAUGE },
592 { "buffer_pool_pages_data", "gauge", DS_TYPE_GAUGE },
593 { "buffer_pool_bytes_data", "gauge", DS_TYPE_GAUGE },
594 { "buffer_pool_pages_dirty", "gauge", DS_TYPE_GAUGE },
595 { "buffer_pool_bytes_dirty", "gauge", DS_TYPE_GAUGE },
596 { "buffer_pool_pages_free", "gauge", DS_TYPE_GAUGE },
598 { "buffer_pages_created", "operations", DS_TYPE_DERIVE },
599 { "buffer_pages_written", "operations", DS_TYPE_DERIVE },
600 { "buffer_pages_read", "operations", DS_TYPE_DERIVE },
601 { "buffer_data_reads", "operations", DS_TYPE_DERIVE },
602 { "buffer_data_written", "operations", DS_TYPE_DERIVE },
604 { "os_data_reads", "operations", DS_TYPE_DERIVE },
605 { "os_data_writes", "operations", DS_TYPE_DERIVE },
606 { "os_data_fsyncs", "operations", DS_TYPE_DERIVE },
607 { "os_log_bytes_written", "operations", DS_TYPE_DERIVE },
608 { "os_log_fsyncs", "operations", DS_TYPE_DERIVE },
609 { "os_log_pending_fsyncs", "operations", DS_TYPE_DERIVE },
610 { "os_log_pending_writes", "operations", DS_TYPE_DERIVE },
612 { "trx_rseg_history_len", "gauge", DS_TYPE_GAUGE },
614 { "log_waits", "operations", DS_TYPE_DERIVE },
615 { "log_write_requests", "operations", DS_TYPE_DERIVE },
616 { "log_writes", "operations", DS_TYPE_DERIVE },
617 { "adaptive_hash_searches", "operations", DS_TYPE_DERIVE },
619 { "file_num_open_files", "gauge", DS_TYPE_GAUGE },
621 { "ibuf_merges_insert", "operations", DS_TYPE_DERIVE },
622 { "ibuf_merges_delete_mark", "operations", DS_TYPE_DERIVE },
623 { "ibuf_merges_delete", "operations", DS_TYPE_DERIVE },
624 { "ibuf_merges_discard_insert", "operations", DS_TYPE_DERIVE },
625 { "ibuf_merges_discard_delete_mark", "operations", DS_TYPE_DERIVE },
626 { "ibuf_merges_discard_delete", "operations", DS_TYPE_DERIVE },
627 { "ibuf_merges_discard_merges", "operations", DS_TYPE_DERIVE },
628 { "ibuf_size", "bytes", DS_TYPE_GAUGE },
630 { "innodb_activity_count", "gauge", DS_TYPE_GAUGE },
631 { "innodb_dblwr_writes", "operations", DS_TYPE_DERIVE },
632 { "innodb_dblwr_pages_written", "operations", DS_TYPE_DERIVE },
633 { "innodb_dblwr_page_size", "gauge", DS_TYPE_GAUGE },
635 { "innodb_rwlock_s_spin_waits", "operations", DS_TYPE_DERIVE },
636 { "innodb_rwlock_x_spin_waits", "operations", DS_TYPE_DERIVE },
637 { "innodb_rwlock_s_spin_rounds", "operations", DS_TYPE_DERIVE },
638 { "innodb_rwlock_x_spin_rounds", "operations", DS_TYPE_DERIVE },
639 { "innodb_rwlock_s_os_waits", "operations", DS_TYPE_DERIVE },
640 { "innodb_rwlock_x_os_waits", "operations", DS_TYPE_DERIVE },
642 { "dml_reads", "operations", DS_TYPE_DERIVE },
643 { "dml_inserts", "operations", DS_TYPE_DERIVE },
644 { "dml_deletes", "operations", DS_TYPE_DERIVE },
645 { "dml_updates", "operations", DS_TYPE_DERIVE },
647 { NULL, NULL, 0}
648 };
650 query = "SELECT name, count, type FROM information_schema.innodb_metrics WHERE status = 'enabled'";
652 res = exec_query (con, query);
653 if (res == NULL)
654 return (-1);
656 while ((row = mysql_fetch_row (res)))
657 {
658 int i;
659 char *key;
660 unsigned long long val;
662 key = row[0];
663 val = atoll (row[1]);
665 for (i = 0; metrics[i].key != NULL && strcmp(metrics[i].key, key) != 0; i++)
666 ;
668 if (metrics[i].key == NULL)
669 continue;
671 switch (metrics[i].ds_type) {
672 case DS_TYPE_COUNTER:
673 counter_submit(metrics[i].type, key, (counter_t)val, db);
674 break;
675 case DS_TYPE_GAUGE:
676 gauge_submit(metrics[i].type, key, (gauge_t)val, db);
677 break;
678 case DS_TYPE_DERIVE:
679 derive_submit(metrics[i].type, key, (derive_t)val, db);
680 break;
681 }
682 }
684 mysql_free_result(res);
685 return (0);
686 }
688 static int mysql_read (user_data_t *ud)
689 {
690 mysql_database_t *db;
691 MYSQL *con;
692 MYSQL_RES *res;
693 MYSQL_ROW row;
694 const char *query;
696 derive_t qcache_hits = 0;
697 derive_t qcache_inserts = 0;
698 derive_t qcache_not_cached = 0;
699 derive_t qcache_lowmem_prunes = 0;
700 gauge_t qcache_queries_in_cache = NAN;
702 gauge_t threads_running = NAN;
703 gauge_t threads_connected = NAN;
704 gauge_t threads_cached = NAN;
705 derive_t threads_created = 0;
707 unsigned long long traffic_incoming = 0ULL;
708 unsigned long long traffic_outgoing = 0ULL;
709 unsigned long mysql_version = 0ULL;
711 if ((ud == NULL) || (ud->data == NULL))
712 {
713 ERROR ("mysql plugin: mysql_database_read: Invalid user data.");
714 return (-1);
715 }
717 db = (mysql_database_t *) ud->data;
719 /* An error message will have been printed in this case */
720 if ((con = getconnection (db)) == NULL)
721 return (-1);
723 mysql_version = mysql_get_server_version(con);
725 query = "SHOW STATUS";
726 if (mysql_version >= 50002)
727 query = "SHOW GLOBAL STATUS";
729 res = exec_query (con, query);
730 if (res == NULL)
731 return (-1);
733 while ((row = mysql_fetch_row (res)))
734 {
735 char *key;
736 unsigned long long val;
738 key = row[0];
739 val = atoll (row[1]);
741 if (strncmp (key, "Com_",
742 strlen ("Com_")) == 0)
743 {
744 if (val == 0ULL)
745 continue;
747 /* Ignore `prepared statements' */
748 if (strncmp (key, "Com_stmt_", strlen ("Com_stmt_")) != 0)
749 counter_submit ("mysql_commands",
750 key + strlen ("Com_"),
751 val, db);
752 }
753 else if (strncmp (key, "Handler_",
754 strlen ("Handler_")) == 0)
755 {
756 if (val == 0ULL)
757 continue;
759 counter_submit ("mysql_handler",
760 key + strlen ("Handler_"),
761 val, db);
762 }
763 else if (strncmp (key, "Qcache_",
764 strlen ("Qcache_")) == 0)
765 {
766 if (strcmp (key, "Qcache_hits") == 0)
767 qcache_hits = (derive_t) val;
768 else if (strcmp (key, "Qcache_inserts") == 0)
769 qcache_inserts = (derive_t) val;
770 else if (strcmp (key, "Qcache_not_cached") == 0)
771 qcache_not_cached = (derive_t) val;
772 else if (strcmp (key, "Qcache_lowmem_prunes") == 0)
773 qcache_lowmem_prunes = (derive_t) val;
774 else if (strcmp (key, "Qcache_queries_in_cache") == 0)
775 qcache_queries_in_cache = (gauge_t) val;
776 }
777 else if (strncmp (key, "Bytes_",
778 strlen ("Bytes_")) == 0)
779 {
780 if (strcmp (key, "Bytes_received") == 0)
781 traffic_incoming += val;
782 else if (strcmp (key, "Bytes_sent") == 0)
783 traffic_outgoing += val;
784 }
785 else if (strncmp (key, "Threads_",
786 strlen ("Threads_")) == 0)
787 {
788 if (strcmp (key, "Threads_running") == 0)
789 threads_running = (gauge_t) val;
790 else if (strcmp (key, "Threads_connected") == 0)
791 threads_connected = (gauge_t) val;
792 else if (strcmp (key, "Threads_cached") == 0)
793 threads_cached = (gauge_t) val;
794 else if (strcmp (key, "Threads_created") == 0)
795 threads_created = (derive_t) val;
796 }
797 else if (strncmp (key, "Table_locks_",
798 strlen ("Table_locks_")) == 0)
799 {
800 counter_submit ("mysql_locks",
801 key + strlen ("Table_locks_"),
802 val, db);
803 }
804 else if (db->innodb_stats && strncmp (key, "Innodb_", strlen ("Innodb_")) == 0)
805 {
806 /* buffer pool */
807 if (strcmp (key, "Innodb_buffer_pool_pages_data") == 0)
808 gauge_submit ("mysql_bpool_pages", "data", val, db);
809 else if (strcmp (key, "Innodb_buffer_pool_pages_dirty") == 0)
810 gauge_submit ("mysql_bpool_pages", "dirty", val, db);
811 else if (strcmp (key, "Innodb_buffer_pool_pages_flushed") == 0)
812 counter_submit ("mysql_bpool_counters", "pages_flushed", val, db);
813 else if (strcmp (key, "Innodb_buffer_pool_pages_free") == 0)
814 gauge_submit ("mysql_bpool_pages", "free", val, db);
815 else if (strcmp (key, "Innodb_buffer_pool_pages_misc") == 0)
816 gauge_submit ("mysql_bpool_pages", "misc", val, db);
817 else if (strcmp (key, "Innodb_buffer_pool_pages_total") == 0)
818 gauge_submit ("mysql_bpool_pages", "total", val, db);
819 else if (strcmp (key, "Innodb_buffer_pool_read_ahead_rnd") == 0)
820 counter_submit ("mysql_bpool_counters", "read_ahead_rnd", val, db);
821 else if (strcmp (key, "Innodb_buffer_pool_read_ahead") == 0)
822 counter_submit ("mysql_bpool_counters", "read_ahead", val, db);
823 else if (strcmp (key, "Innodb_buffer_pool_read_ahead_evicted") == 0)
824 counter_submit ("mysql_bpool_counters", "read_ahead_evicted", val, db);
825 else if (strcmp (key, "Innodb_buffer_pool_read_requests") == 0)
826 counter_submit ("mysql_bpool_counters", "read_requests", val, db);
827 else if (strcmp (key, "Innodb_buffer_pool_reads") == 0)
828 counter_submit ("mysql_bpool_counters", "reads", val, db);
829 else if (strcmp (key, "Innodb_buffer_pool_write_requests") == 0)
830 counter_submit ("mysql_bpool_counters", "write_requests", val, db);
831 else if (strcmp (key, "Innodb_buffer_pool_bytes_data") == 0)
832 gauge_submit ("mysql_bpool_bytes", "data", val, db);
833 else if (strcmp (key, "Innodb_buffer_pool_bytes_dirty") == 0)
834 gauge_submit ("mysql_bpool_bytes", "dirty", val, db);
836 /* data */
837 if (strcmp (key, "Innodb_data_fsyncs") == 0)
838 counter_submit ("mysql_innodb_data", "fsyncs", val, db);
839 else if (strcmp (key, "Innodb_data_read") == 0)
840 counter_submit ("mysql_innodb_data", "read", val, db);
841 else if (strcmp (key, "Innodb_data_reads") == 0)
842 counter_submit ("mysql_innodb_data", "reads", val, db);
843 else if (strcmp (key, "Innodb_data_writes") == 0)
844 counter_submit ("mysql_innodb_data", "writes", val, db);
845 else if (strcmp (key, "Innodb_data_written") == 0)
846 counter_submit ("mysql_innodb_data", "written", val, db);
848 /* double write */
849 else if (strcmp (key, "Innodb_dblwr_writes") == 0)
850 counter_submit ("mysql_innodb_dblwr", "writes", val, db);
851 else if (strcmp (key, "Innodb_dblwr_pages_written") == 0)
852 counter_submit ("mysql_innodb_dblwr", "written", val, db);
854 /* log */
855 else if (strcmp (key, "Innodb_log_waits") == 0)
856 counter_submit ("mysql_innodb_log", "waits", val, db);
857 else if (strcmp (key, "Innodb_log_write_requests") == 0)
858 counter_submit ("mysql_innodb_log", "write_requests", val, db);
859 else if (strcmp (key, "Innodb_log_writes") == 0)
860 counter_submit ("mysql_innodb_log", "writes", val, db);
861 else if (strcmp (key, "Innodb_os_log_fsyncs") == 0)
862 counter_submit ("mysql_innodb_log", "fsyncs", val, db);
863 else if (strcmp (key, "Innodb_os_log_written") == 0)
864 counter_submit ("mysql_innodb_log", "written", val, db);
866 /* pages */
867 else if (strcmp (key, "Innodb_pages_created") == 0)
868 counter_submit ("mysql_innodb_pages", "created", val, db);
869 else if (strcmp (key, "Innodb_pages_read") == 0)
870 counter_submit ("mysql_innodb_pages", "read", val, db);
871 else if (strcmp (key, "Innodb_pages_written") == 0)
872 counter_submit ("mysql_innodb_pages", "written", val, db);
874 /* row lock */
875 else if (strcmp (key, "Innodb_row_lock_time") == 0)
876 counter_submit ("mysql_innodb_row_lock", "time", val, db);
877 else if (strcmp (key, "Innodb_row_lock_waits") == 0)
878 counter_submit ("mysql_innodb_row_lock", "waits", val, db);
880 /* rows */
881 else if (strcmp (key, "Innodb_rows_deleted") == 0)
882 counter_submit ("mysql_innodb_rows", "deleted", val, db);
883 else if (strcmp (key, "Innodb_rows_inserted") == 0)
884 counter_submit ("mysql_innodb_rows", "inserted", val, db);
885 else if (strcmp (key, "Innodb_rows_read") == 0)
886 counter_submit ("mysql_innodb_rows", "read", val, db);
887 else if (strcmp (key, "Innodb_rows_updated") == 0)
888 counter_submit ("mysql_innodb_rows", "updated", val, db);
889 }
890 else if (strncmp (key, "Select_", strlen ("Select_")) == 0)
891 {
892 counter_submit ("mysql_select", key + strlen ("Select_"),
893 val, db);
894 }
895 else if (strncmp (key, "Sort_", strlen ("Sort_")) == 0)
896 {
897 if (strcmp (key, "Sort_merge_passes") == 0)
898 counter_submit ("mysql_sort_merge_passes", NULL, val, db);
899 else if (strcmp (key, "Sort_rows") == 0)
900 counter_submit ("mysql_sort_rows", NULL, val, db);
901 else if (strcmp (key, "Sort_range") == 0)
902 counter_submit ("mysql_sort", "range", val, db);
903 else if (strcmp (key, "Sort_scan") == 0)
904 counter_submit ("mysql_sort", "scan", val, db);
906 }
907 }
908 mysql_free_result (res); res = NULL;
910 if ((qcache_hits != 0)
911 || (qcache_inserts != 0)
912 || (qcache_not_cached != 0)
913 || (qcache_lowmem_prunes != 0))
914 {
915 derive_submit ("cache_result", "qcache-hits",
916 qcache_hits, db);
917 derive_submit ("cache_result", "qcache-inserts",
918 qcache_inserts, db);
919 derive_submit ("cache_result", "qcache-not_cached",
920 qcache_not_cached, db);
921 derive_submit ("cache_result", "qcache-prunes",
922 qcache_lowmem_prunes, db);
924 gauge_submit ("cache_size", "qcache",
925 qcache_queries_in_cache, db);
926 }
928 if (threads_created != 0)
929 {
930 gauge_submit ("threads", "running",
931 threads_running, db);
932 gauge_submit ("threads", "connected",
933 threads_connected, db);
934 gauge_submit ("threads", "cached",
935 threads_cached, db);
937 derive_submit ("total_threads", "created",
938 threads_created, db);
939 }
941 traffic_submit (traffic_incoming, traffic_outgoing, db);
943 if (mysql_version >= 50600 && db->innodb_stats)
944 mysql_read_innodb_stats (db, con);
946 if (db->master_stats)
947 mysql_read_master_stats (db, con);
949 if ((db->slave_stats) || (db->slave_notif))
950 mysql_read_slave_stats (db, con);
952 return (0);
953 } /* int mysql_read */
955 void module_register (void)
956 {
957 plugin_register_complex_config ("mysql", mysql_config);
958 } /* void module_register */