2 * collectd - src/mysql.c
3 * Copyright (C) 2006,2007 Florian octo Forster
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; only version 2 of the License is applicable.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Florian octo Forster <octo at verplant.org>
25 #include "configfile.h"
29 #elif defined(HAVE_MYSQL_MYSQL_H)
30 #include <mysql/mysql.h>
33 /* TODO: Understand `Select_*' and possibly do that stuff as well.. */
35 struct mysql_database_s /* {{{ */
37 /* instance == NULL => legacy mode */
52 typedef struct mysql_database_s mysql_database_t; /* }}} */
54 static mysql_database_t **databases = NULL;
55 static size_t databases_num = 0;
57 static void mysql_database_free (mysql_database_t *db) /* {{{ */
63 mysql_close (db->con);
70 } /* }}} void mysql_database_free */
72 /* Configuration handling functions {{{
75 * <Database "plugin_instance1">
83 static int mysql_config_set_string (char **ret_string, /* {{{ */
88 if ((ci->values_num != 1)
89 || (ci->values[0].type != OCONFIG_TYPE_STRING))
91 WARNING ("mysql plugin: The `%s' config option "
92 "needs exactly one string argument.", ci->key);
96 string = strdup (ci->values[0].value.string);
99 ERROR ("mysql plugin: strdup failed.");
103 if (*ret_string != NULL)
105 *ret_string = string;
108 } /* }}} int mysql_config_set_string */
110 static int mysql_config_set_int (int *ret_int, /* {{{ */
113 if ((ci->values_num != 1)
114 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
116 WARNING ("mysql plugin: The `%s' config option "
117 "needs exactly one string argument.", ci->key);
121 *ret_int = ci->values[0].value.number;
124 } /* }}} int mysql_config_set_int */
126 static int mysql_config (oconfig_item_t *ci) /* {{{ */
128 mysql_database_t *db;
133 if ((ci->values_num != 1)
134 || (ci->values[0].type != OCONFIG_TYPE_STRING))
136 WARNING ("mysql plugin: The `Database' block "
137 "needs exactly one string argument.");
141 db = (mysql_database_t *) malloc (sizeof (*db));
144 ERROR ("mysql plugin: malloc failed.");
147 memset (db, 0, sizeof (*db));
149 /* initialize all the pointers */
156 /* initialize non-configurable defaults */
157 db->wait_increase = 60;
160 if (strcasecmp ("Plugin", ci->key) == 0)
164 else if (strcasecmp ("Database", ci->key) == 0)
167 status = mysql_config_set_string (&db->instance, ci);
173 assert (db->instance != NULL);
177 ERROR ("mysql plugin: mysql_config: "
178 "Invalid key: %s", ci->key);
182 /* Fill the `mysql_database_t' structure.. */
183 for (i = 0; i < ci->children_num; i++)
185 oconfig_item_t *child = ci->children + i;
187 if (strcasecmp ("Host", child->key) == 0)
188 status = mysql_config_set_string (&db->host, child);
189 else if (strcasecmp ("User", child->key) == 0)
190 status = mysql_config_set_string (&db->user, child);
191 else if (strcasecmp ("Password", child->key) == 0)
192 status = mysql_config_set_string (&db->pass, child);
193 else if (strcasecmp ("Port", child->key) == 0)
194 status = mysql_config_set_int (&db->port, child);
195 else if (strcasecmp ("Socket", child->key) == 0)
196 status = mysql_config_set_string (&db->socket, child);
197 /* Check if we're currently handling the `Plugin' block. If so,
198 * handle `Database' _blocks_, too. */
199 else if ((plugin_block != 0)
200 && (strcasecmp ("Database", child->key) == 0)
201 && (child->children != NULL))
203 /* If `plugin_block > 1', there has been at least one
204 * `Database' block */
206 status = mysql_config (child);
208 /* Now handle ordinary `Database' options (without children) */
209 else if ((strcasecmp ("Database", child->key) == 0)
210 && (child->children == NULL))
211 status = mysql_config_set_string (&db->database, child);
214 WARNING ("mysql plugin: Option `%s' not allowed here.", child->key);
222 /* Check if there were any `Database' blocks. */
223 if (plugin_block > 1)
225 /* There were connection blocks. Don't use any legacy stuff. */
226 if ((db->host != NULL)
227 || (db->user != NULL)
228 || (db->pass != NULL)
229 || (db->database != NULL)
230 || (db->socket != NULL)
233 WARNING ("mysql plugin: At least one <Database> "
234 "block has been found. The legacy "
235 "configuration will be ignored.");
237 mysql_database_free (db);
240 else if (plugin_block != 0)
242 WARNING ("mysql plugin: You're using the legacy "
243 "configuration options. Please consider "
244 "updating your configuration!");
247 /* Check that all necessary options have been given. */
250 /* Zero is allowed and automatically handled by
251 * `mysql_real_connect'. */
252 if ((db->port < 0) || (db->port > 65535))
254 ERROR ("mysql plugin: Database %s: Port number out "
256 (db->instance != NULL)
263 } /* while (status == 0) */
265 /* If all went well, add this database to the global list of databases. */
268 mysql_database_t **temp;
270 temp = (mysql_database_t **) realloc (databases,
271 sizeof (*databases) * (databases_num + 1));
274 ERROR ("mysql plugin: realloc failed");
280 databases[databases_num] = db;
287 mysql_database_free (db);
292 } /* }}} int mysql_config */
294 /* }}} End of configuration handling functions */
296 static MYSQL *getconnection (mysql_database_t *db)
301 if ((err = mysql_ping (db->con)) != 0)
303 WARNING ("mysql_ping failed: %s", mysql_error (db->con));
313 if (db->wait_for > 0)
315 db->wait_for -= interval_g;
319 db->wait_for = db->wait_increase;
320 db->wait_increase *= 2;
321 if (db->wait_increase > 86400)
322 db->wait_increase = 86400;
324 if ((db->con = mysql_init (db->con)) == NULL)
326 ERROR ("mysql_init failed: %s", mysql_error (db->con));
331 if (mysql_real_connect (db->con, db->host, db->user, db->pass,
332 db->database, db->port, db->socket, 0) == NULL)
334 ERROR ("mysql_real_connect failed: %s", mysql_error (db->con));
342 db->wait_increase = 60;
345 } /* static MYSQL *getconnection (mysql_database_t *db) */
347 static void set_host (mysql_database_t *db, value_list_t *vl)
349 /* XXX legacy mode - use hostname_g */
350 if (db->instance == NULL)
351 sstrncpy (vl->host, hostname_g, sizeof (vl->host));
354 if ((db->host == NULL)
355 || (strcmp ("", db->host) == 0)
356 || (strcmp ("localhost", db->host) == 0))
357 sstrncpy (vl->host, hostname_g, sizeof (vl->host));
359 sstrncpy (vl->host, db->host, sizeof (vl->host));
363 static void set_plugin_instance (mysql_database_t *db, value_list_t *vl)
365 /* XXX legacy mode - no plugin_instance */
366 if (db->instance == NULL)
367 sstrncpy (vl->plugin_instance, "",
368 sizeof (vl->plugin_instance));
370 sstrncpy (vl->plugin_instance, db->instance,
371 sizeof (vl->plugin_instance));
374 static void counter_submit (const char *type, const char *type_instance,
375 counter_t value, mysql_database_t *db)
378 value_list_t vl = VALUE_LIST_INIT;
380 values[0].counter = value;
385 sstrncpy (vl.plugin, "mysql", sizeof (vl.plugin));
386 sstrncpy (vl.type, type, sizeof (vl.type));
387 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
388 set_plugin_instance (db, &vl);
390 plugin_dispatch_values (&vl);
391 } /* void counter_submit */
393 static void qcache_submit (counter_t hits, counter_t inserts,
394 counter_t not_cached, counter_t lowmem_prunes,
395 gauge_t queries_in_cache, mysql_database_t *db)
398 value_list_t vl = VALUE_LIST_INIT;
400 values[0].counter = hits;
401 values[1].counter = inserts;
402 values[2].counter = not_cached;
403 values[3].counter = lowmem_prunes;
404 values[4].gauge = queries_in_cache;
409 sstrncpy (vl.plugin, "mysql", sizeof (vl.plugin));
410 sstrncpy (vl.type, "mysql_qcache", sizeof (vl.type));
411 set_plugin_instance (db, &vl);
413 plugin_dispatch_values (&vl);
414 } /* void qcache_submit */
416 static void threads_submit (gauge_t running, gauge_t connected, gauge_t cached,
417 counter_t created, mysql_database_t *db)
420 value_list_t vl = VALUE_LIST_INIT;
422 values[0].gauge = running;
423 values[1].gauge = connected;
424 values[2].gauge = cached;
425 values[3].counter = created;
430 sstrncpy (vl.plugin, "mysql", sizeof (vl.plugin));
431 sstrncpy (vl.type, "mysql_threads", sizeof (vl.type));
432 set_plugin_instance (db, &vl);
434 plugin_dispatch_values (&vl);
435 } /* void threads_submit */
437 static void traffic_submit (counter_t rx, counter_t tx, mysql_database_t *db)
440 value_list_t vl = VALUE_LIST_INIT;
442 values[0].counter = rx;
443 values[1].counter = tx;
448 sstrncpy (vl.plugin, "mysql", sizeof (vl.plugin));
449 sstrncpy (vl.type, "mysql_octets", sizeof (vl.type));
450 set_plugin_instance (db, &vl);
452 plugin_dispatch_values (&vl);
453 } /* void traffic_submit */
455 static int mysql_read_database (mysql_database_t *db)
464 unsigned long long qcache_hits = 0ULL;
465 unsigned long long qcache_inserts = 0ULL;
466 unsigned long long qcache_not_cached = 0ULL;
467 unsigned long long qcache_lowmem_prunes = 0ULL;
468 int qcache_queries_in_cache = -1;
470 int threads_running = -1;
471 int threads_connected = -1;
472 int threads_cached = -1;
473 unsigned long long threads_created = 0ULL;
475 unsigned long long traffic_incoming = 0ULL;
476 unsigned long long traffic_outgoing = 0ULL;
478 /* An error message will have been printed in this case */
479 if ((con = getconnection (db)) == NULL)
482 query = "SHOW STATUS";
483 if (mysql_get_server_version (con) >= 50002)
484 query = "SHOW GLOBAL STATUS";
486 query_len = strlen (query);
488 if (mysql_real_query (con, query, query_len))
490 ERROR ("mysql_real_query failed: %s\n",
495 if ((res = mysql_store_result (con)) == NULL)
497 ERROR ("mysql_store_result failed: %s\n",
502 field_num = mysql_num_fields (res);
503 while ((row = mysql_fetch_row (res)))
506 unsigned long long val;
509 val = atoll (row[1]);
511 if (strncmp (key, "Com_", 4) == 0)
516 /* Ignore `prepared statements' */
517 if (strncmp (key, "Com_stmt_", 9) != 0)
518 counter_submit ("mysql_commands", key + 4, val, db);
520 else if (strncmp (key, "Handler_", 8) == 0)
525 counter_submit ("mysql_handler", key + 8, val, db);
527 else if (strncmp (key, "Qcache_", 7) == 0)
529 if (strcmp (key, "Qcache_hits") == 0)
531 else if (strcmp (key, "Qcache_inserts") == 0)
532 qcache_inserts = val;
533 else if (strcmp (key, "Qcache_not_cached") == 0)
534 qcache_not_cached = val;
535 else if (strcmp (key, "Qcache_lowmem_prunes") == 0)
536 qcache_lowmem_prunes = val;
537 else if (strcmp (key, "Qcache_queries_in_cache") == 0)
538 qcache_queries_in_cache = (int) val;
540 else if (strncmp (key, "Bytes_", 6) == 0)
542 if (strcmp (key, "Bytes_received") == 0)
543 traffic_incoming += val;
544 else if (strcmp (key, "Bytes_sent") == 0)
545 traffic_outgoing += val;
547 else if (strncmp (key, "Threads_", 8) == 0)
549 if (strcmp (key, "Threads_running") == 0)
550 threads_running = (int) val;
551 else if (strcmp (key, "Threads_connected") == 0)
552 threads_connected = (int) val;
553 else if (strcmp (key, "Threads_cached") == 0)
554 threads_cached = (int) val;
555 else if (strcmp (key, "Threads_created") == 0)
556 threads_created = val;
559 mysql_free_result (res); res = NULL;
561 if ((qcache_hits != 0ULL)
562 || (qcache_inserts != 0ULL)
563 || (qcache_not_cached != 0ULL)
564 || (qcache_lowmem_prunes != 0ULL))
565 qcache_submit (qcache_hits, qcache_inserts, qcache_not_cached,
566 qcache_lowmem_prunes, qcache_queries_in_cache, db);
568 if (threads_created != 0ULL)
569 threads_submit (threads_running, threads_connected,
570 threads_cached, threads_created, db);
572 traffic_submit (traffic_incoming, traffic_outgoing, db);
575 } /* int mysql_read_database */
577 static int mysql_read (void)
583 for (i = 0; i < databases_num; i++)
585 status = mysql_read_database (databases[i]);
592 ERROR ("mysql plugin: No database could be read. Will return an error so "
593 "the plugin will be delayed.");
598 } /* int mysql_read */
600 void module_register (void)
602 plugin_register_complex_config ("mysql", mysql_config);
603 plugin_register_read ("mysql", mysql_read);
604 } /* void module_register */