Debian Codegraph @

src/mysql.c

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