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 /* {{{ */
44 typedef struct mysql_database_s mysql_database_t; /* }}} */
46 static mysql_database_t **databases = NULL;
47 static size_t databases_num = 0;
49 static void mysql_database_free (mysql_database_t *db) /* {{{ */
59 } /* }}} void mysql_database_free */
61 /* Configuration handling functions {{{
64 * <Database "plugin_instance1">
72 static int mysql_config_set_string (char **ret_string, /* {{{ */
77 if ((ci->values_num != 1)
78 || (ci->values[0].type != OCONFIG_TYPE_STRING))
80 WARNING ("mysql plugin: The `%s' config option "
81 "needs exactly one string argument.", ci->key);
85 string = strdup (ci->values[0].value.string);
88 ERROR ("mysql plugin: strdup failed.");
92 if (*ret_string != NULL)
97 } /* }}} int mysql_config_set_string */
99 static int mysql_config_set_int (int *ret_int, /* {{{ */
102 if ((ci->values_num != 1)
103 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
105 WARNING ("mysql plugin: The `%s' config option "
106 "needs exactly one string argument.", ci->key);
110 *ret_int = ci->values[0].value.number;
113 } /* }}} int mysql_config_set_int */
115 static int mysql_config_add_database (oconfig_item_t *ci) /* {{{ */
117 mysql_database_t *db;
121 if ((ci->values_num != 1)
122 || (ci->values[0].type != OCONFIG_TYPE_STRING))
124 WARNING ("mysql plugin: The `Database' block "
125 "needs exactly one string argument.");
129 db = (mysql_database_t *) malloc (sizeof (*db));
132 ERROR ("mysql plugin: malloc failed.");
135 memset (db, 0, sizeof (*db));
137 status = mysql_config_set_string (&db->name, ci);
144 /* Fill the `mysql_database_t' structure.. */
145 for (i = 0; i < ci->children_num; i++)
147 oconfig_item_t *child = ci->children + i;
149 if (strcasecmp ("Host", child->key) == 0)
150 status = mysql_config_set_string (&db->host, child);
151 else if (strcasecmp ("User", child->key) == 0)
152 status = mysql_config_set_string (&db->user, child);
153 else if (strcasecmp ("Password", child->key) == 0)
154 status = mysql_config_set_string (&db->pass, child);
155 else if (strcasecmp ("Port", child->key) == 0)
156 status = mysql_config_set_int (&db->port, child);
157 else if (strcasecmp ("Socket", child->key) == 0)
158 status = mysql_config_set_string (&db->socket, child);
161 WARNING ("mysql plugin: Option `%s' not allowed here.", child->key);
169 /* Check that all necessary options have been given. */
172 if ((db->port < 0) || (db->port >= 65535))
174 ERROR ("mysql plugin: Port number out of range: %i",
179 } /* while (status == 0) */
181 /* If all went well, add this database to the global list of databases. */
184 mysql_database_t **temp;
186 temp = (mysql_database_t **) realloc (databases,
187 sizeof (*databases) * (databases_num + 1));
190 ERROR ("mysql plugin: realloc failed");
196 databases[databases_num] = db;
203 mysql_database_free (db);
208 } /* }}} int mysql_config_add_database */
210 static int mysql_config (oconfig_item_t *ci) /* {{{ */
214 oconfig_item_t *lci = NULL; /* legacy config */
216 for (i = 0; i < ci->children_num; i++)
218 oconfig_item_t *child = ci->children + i;
220 if (strcasecmp ("Database", child->key) == 0 && child->children_num > 0)
221 mysql_config_add_database (child);
224 /* legacy mode - convert to <Database ...> config */
227 lci = malloc (sizeof(*lci));
230 ERROR ("mysql plugin: malloc failed.");
233 memset (lci, '\0', sizeof (*lci));
235 if (strcasecmp ("Database", child->key) == 0)
237 lci->key = child->key;
238 lci->values = child->values;
239 lci->values_num = child->values_num;
240 lci->parent = child->parent;
246 realloc (lci->children,
247 lci->children_num * sizeof (*child));
248 if (lci->children == NULL)
250 ERROR ("mysql plugin: realloc failed.");
253 memcpy (&lci->children[lci->children_num-1], child, sizeof (*child));
256 } /* for (ci->children) */
260 if (lci->key == NULL)
262 ERROR ("mysql plugin: no Database configured.");
266 mysql_config_add_database (lci);
267 sfree (lci->children);
271 } /* }}} int mysql_config */
273 static MYSQL *getconnection (mysql_database_t *db)
278 static int wait_for = 0;
279 static int wait_increase = 60;
284 if ((err = mysql_ping (con)) != 0)
286 WARNING ("mysql_ping failed: %s", mysql_error (con));
298 wait_for -= interval_g;
302 wait_for = wait_increase;
304 if (wait_increase > 86400)
305 wait_increase = 86400;
307 if ((con = mysql_init (con)) == NULL)
309 ERROR ("mysql_init failed: %s", mysql_error (con));
314 if (mysql_real_connect (con, db->host, db->user, db->pass, db->name, db->port, db->socket, 0) == NULL)
316 ERROR ("mysql_real_connect failed: %s", mysql_error (con));
327 } /* static MYSQL *getconnection (void) */
329 static void set_plugin_instance (mysql_database_t *db, value_list_t *vl)
331 /* XXX legacy mode - no plugin_instance */
332 if (databases_num > 0)
333 sstrncpy (vl->plugin_instance, db->name, sizeof (vl->plugin_instance));
336 static void counter_submit (const char *type, const char *type_instance,
337 counter_t value, mysql_database_t *db)
340 value_list_t vl = VALUE_LIST_INIT;
342 values[0].counter = value;
346 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
347 sstrncpy (vl.plugin, "mysql", sizeof (vl.plugin));
348 sstrncpy (vl.type, type, sizeof (vl.type));
349 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
350 set_plugin_instance (db, &vl);
352 plugin_dispatch_values (&vl);
353 } /* void counter_submit */
355 static void qcache_submit (counter_t hits, counter_t inserts,
356 counter_t not_cached, counter_t lowmem_prunes,
357 gauge_t queries_in_cache, mysql_database_t *db)
360 value_list_t vl = VALUE_LIST_INIT;
362 values[0].counter = hits;
363 values[1].counter = inserts;
364 values[2].counter = not_cached;
365 values[3].counter = lowmem_prunes;
366 values[4].gauge = queries_in_cache;
370 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
371 sstrncpy (vl.plugin, "mysql", sizeof (vl.plugin));
372 sstrncpy (vl.type, "mysql_qcache", sizeof (vl.type));
373 set_plugin_instance (db, &vl);
375 plugin_dispatch_values (&vl);
376 } /* void qcache_submit */
378 static void threads_submit (gauge_t running, gauge_t connected, gauge_t cached,
379 counter_t created, mysql_database_t *db)
382 value_list_t vl = VALUE_LIST_INIT;
384 values[0].gauge = running;
385 values[1].gauge = connected;
386 values[2].gauge = cached;
387 values[3].counter = created;
391 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
392 sstrncpy (vl.plugin, "mysql", sizeof (vl.plugin));
393 sstrncpy (vl.type, "mysql_threads", sizeof (vl.type));
394 set_plugin_instance (db, &vl);
396 plugin_dispatch_values (&vl);
397 } /* void threads_submit */
399 static void traffic_submit (counter_t rx, counter_t tx, mysql_database_t *db)
402 value_list_t vl = VALUE_LIST_INIT;
404 values[0].counter = rx;
405 values[1].counter = tx;
409 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
410 sstrncpy (vl.plugin, "mysql", sizeof (vl.plugin));
411 sstrncpy (vl.type, "mysql_octets", sizeof (vl.type));
412 set_plugin_instance (db, &vl);
414 plugin_dispatch_values (&vl);
415 } /* void traffic_submit */
417 static int mysql_read_database (mysql_database_t *db)
426 unsigned long long qcache_hits = 0ULL;
427 unsigned long long qcache_inserts = 0ULL;
428 unsigned long long qcache_not_cached = 0ULL;
429 unsigned long long qcache_lowmem_prunes = 0ULL;
430 int qcache_queries_in_cache = -1;
432 int threads_running = -1;
433 int threads_connected = -1;
434 int threads_cached = -1;
435 unsigned long long threads_created = 0ULL;
437 unsigned long long traffic_incoming = 0ULL;
438 unsigned long long traffic_outgoing = 0ULL;
440 /* An error message will have been printed in this case */
441 if ((con = getconnection (db)) == NULL)
444 query = "SHOW STATUS";
445 if (mysql_get_server_version (con) >= 50002)
446 query = "SHOW GLOBAL STATUS";
448 query_len = strlen (query);
450 if (mysql_real_query (con, query, query_len))
452 ERROR ("mysql_real_query failed: %s\n",
457 if ((res = mysql_store_result (con)) == NULL)
459 ERROR ("mysql_store_result failed: %s\n",
464 field_num = mysql_num_fields (res);
465 while ((row = mysql_fetch_row (res)))
468 unsigned long long val;
471 val = atoll (row[1]);
473 if (strncmp (key, "Com_", 4) == 0)
478 /* Ignore `prepared statements' */
479 if (strncmp (key, "Com_stmt_", 9) != 0)
480 counter_submit ("mysql_commands", key + 4, val, db);
482 else if (strncmp (key, "Handler_", 8) == 0)
487 counter_submit ("mysql_handler", key + 8, val, db);
489 else if (strncmp (key, "Qcache_", 7) == 0)
491 if (strcmp (key, "Qcache_hits") == 0)
493 else if (strcmp (key, "Qcache_inserts") == 0)
494 qcache_inserts = val;
495 else if (strcmp (key, "Qcache_not_cached") == 0)
496 qcache_not_cached = val;
497 else if (strcmp (key, "Qcache_lowmem_prunes") == 0)
498 qcache_lowmem_prunes = val;
499 else if (strcmp (key, "Qcache_queries_in_cache") == 0)
500 qcache_queries_in_cache = (int) val;
502 else if (strncmp (key, "Bytes_", 6) == 0)
504 if (strcmp (key, "Bytes_received") == 0)
505 traffic_incoming += val;
506 else if (strcmp (key, "Bytes_sent") == 0)
507 traffic_outgoing += val;
509 else if (strncmp (key, "Threads_", 8) == 0)
511 if (strcmp (key, "Threads_running") == 0)
512 threads_running = (int) val;
513 else if (strcmp (key, "Threads_connected") == 0)
514 threads_connected = (int) val;
515 else if (strcmp (key, "Threads_cached") == 0)
516 threads_cached = (int) val;
517 else if (strcmp (key, "Threads_created") == 0)
518 threads_created = val;
521 mysql_free_result (res); res = NULL;
523 if ((qcache_hits != 0ULL)
524 || (qcache_inserts != 0ULL)
525 || (qcache_not_cached != 0ULL)
526 || (qcache_lowmem_prunes != 0ULL))
527 qcache_submit (qcache_hits, qcache_inserts, qcache_not_cached,
528 qcache_lowmem_prunes, qcache_queries_in_cache, db);
530 if (threads_created != 0ULL)
531 threads_submit (threads_running, threads_connected,
532 threads_cached, threads_created, db);
534 traffic_submit (traffic_incoming, traffic_outgoing, db);
536 /* mysql_close (con); */
539 } /* int mysql_read_database */
541 static int mysql_read (void)
547 for (i = 0; i < databases_num; i++)
549 status = mysql_read_database (databases[i]);
556 ERROR ("mysql plugin: No database could be read. Will return an error so "
557 "the plugin will be delayed.");
562 } /* int mysql_read */
564 void module_register (void)
566 plugin_register_complex_config ("mysql", mysql_config);
567 plugin_register_read ("mysql", mysql_read);
568 } /* void module_register */