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 */
49 typedef struct mysql_database_s mysql_database_t; /* }}} */
51 static int mysql_read (user_data_t *ud);
53 static void mysql_database_free (void *arg) /* {{{ */
57 DEBUG ("mysql plugin: mysql_database_free (arg = %p);", arg);
59 db = (mysql_database_t *) arg;
65 mysql_close (db->con);
74 } /* }}} void mysql_database_free */
76 /* Configuration handling functions {{{
79 * <Database "plugin_instance1">
87 static int mysql_config_set_string (char **ret_string, /* {{{ */
92 if ((ci->values_num != 1)
93 || (ci->values[0].type != OCONFIG_TYPE_STRING))
95 WARNING ("mysql plugin: The `%s' config option "
96 "needs exactly one string argument.", ci->key);
100 string = strdup (ci->values[0].value.string);
103 ERROR ("mysql plugin: strdup failed.");
107 if (*ret_string != NULL)
109 *ret_string = string;
112 } /* }}} int mysql_config_set_string */
114 static int mysql_config_set_int (int *ret_int, /* {{{ */
117 if ((ci->values_num != 1)
118 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
120 WARNING ("mysql plugin: The `%s' config option "
121 "needs exactly one string argument.", ci->key);
125 *ret_int = ci->values[0].value.number;
128 } /* }}} int mysql_config_set_int */
130 static int mysql_config (oconfig_item_t *ci) /* {{{ */
132 mysql_database_t *db;
137 if ((ci->values_num != 1)
138 || (ci->values[0].type != OCONFIG_TYPE_STRING))
140 WARNING ("mysql plugin: The `Database' block "
141 "needs exactly one string argument.");
145 db = (mysql_database_t *) malloc (sizeof (*db));
148 ERROR ("mysql plugin: malloc failed.");
151 memset (db, 0, sizeof (*db));
153 /* initialize all the pointers */
162 if (strcasecmp ("Plugin", ci->key) == 0)
166 else if (strcasecmp ("Database", ci->key) == 0)
169 status = mysql_config_set_string (&db->instance, ci);
175 assert (db->instance != NULL);
176 db->database = strdup (db->instance);
180 ERROR ("mysql plugin: mysql_config: "
181 "Invalid key: %s", ci->key);
185 /* Fill the `mysql_database_t' structure.. */
186 for (i = 0; i < ci->children_num; i++)
188 oconfig_item_t *child = ci->children + i;
190 if (strcasecmp ("Host", child->key) == 0)
191 status = mysql_config_set_string (&db->host, child);
192 else if (strcasecmp ("User", child->key) == 0)
193 status = mysql_config_set_string (&db->user, child);
194 else if (strcasecmp ("Password", child->key) == 0)
195 status = mysql_config_set_string (&db->pass, child);
196 else if (strcasecmp ("Port", child->key) == 0)
197 status = mysql_config_set_int (&db->port, child);
198 else if (strcasecmp ("Socket", child->key) == 0)
199 status = mysql_config_set_string (&db->socket, child);
200 /* Check if we're currently handling the `Plugin' block. If so,
201 * handle `Database' _blocks_, too. */
202 else if ((plugin_block != 0)
203 && (strcasecmp ("Database", child->key) == 0)
204 && (child->children != NULL))
206 /* If `plugin_block > 1', there has been at least one
207 * `Database' block */
209 status = mysql_config (child);
211 /* Now handle ordinary `Database' options (without children) */
212 else if ((strcasecmp ("Database", child->key) == 0)
213 && (child->children == NULL))
214 status = mysql_config_set_string (&db->database, child);
217 WARNING ("mysql plugin: Option `%s' not allowed here.", child->key);
225 /* Check if there were any `Database' blocks. */
226 if (plugin_block > 1)
228 /* There were connection blocks. Don't use any legacy stuff. */
229 if ((db->host != NULL)
230 || (db->user != NULL)
231 || (db->pass != NULL)
232 || (db->database != NULL)
233 || (db->socket != NULL)
236 WARNING ("mysql plugin: At least one <Database> "
237 "block has been found. The legacy "
238 "configuration will be ignored.");
240 mysql_database_free (db);
243 else if (plugin_block != 0)
245 WARNING ("mysql plugin: You're using the legacy "
246 "configuration options. Please consider "
247 "updating your configuration!");
250 /* Check that all necessary options have been given. */
253 /* Zero is allowed and automatically handled by
254 * `mysql_real_connect'. */
255 if ((db->port < 0) || (db->port > 65535))
257 ERROR ("mysql plugin: Database %s: Port number out "
259 (db->instance != NULL)
265 if (db->database == NULL)
267 ERROR ("mysql plugin: No `Database' configured");
271 } /* while (status == 0) */
273 /* If all went well, register this database for reading */
278 DEBUG ("mysql plugin: Registering new read callback: %s", db->database);
280 memset (&ud, 0, sizeof (ud));
281 ud.data = (void *) db;
282 ud.free_func = mysql_database_free;
284 plugin_register_complex_read (db->database, mysql_read,
285 /* interval = */ NULL, &ud);
289 mysql_database_free (db);
294 } /* }}} int mysql_config */
296 /* }}} End of configuration handling functions */
298 static MYSQL *getconnection (mysql_database_t *db)
303 if ((err = mysql_ping (db->con)) != 0)
305 WARNING ("mysql_ping failed: %s", mysql_error (db->con));
315 if ((db->con = mysql_init (db->con)) == NULL)
317 ERROR ("mysql_init failed: %s", mysql_error (db->con));
322 if (mysql_real_connect (db->con, db->host, db->user, db->pass,
323 db->database, db->port, db->socket, 0) == NULL)
325 ERROR ("mysql_real_connect failed: %s", mysql_error (db->con));
334 } /* static MYSQL *getconnection (mysql_database_t *db) */
336 static void set_host (mysql_database_t *db, value_list_t *vl)
338 /* XXX legacy mode - use hostname_g */
339 if (db->instance == NULL)
340 sstrncpy (vl->host, hostname_g, sizeof (vl->host));
343 if ((db->host == NULL)
344 || (strcmp ("", db->host) == 0)
345 || (strcmp ("localhost", db->host) == 0))
346 sstrncpy (vl->host, hostname_g, sizeof (vl->host));
348 sstrncpy (vl->host, db->host, sizeof (vl->host));
352 static void set_plugin_instance (mysql_database_t *db, value_list_t *vl)
354 /* XXX legacy mode - no plugin_instance */
355 if (db->instance == NULL)
356 sstrncpy (vl->plugin_instance, "",
357 sizeof (vl->plugin_instance));
359 sstrncpy (vl->plugin_instance, db->instance,
360 sizeof (vl->plugin_instance));
363 static void counter_submit (const char *type, const char *type_instance,
364 counter_t value, mysql_database_t *db)
367 value_list_t vl = VALUE_LIST_INIT;
369 values[0].counter = value;
374 sstrncpy (vl.plugin, "mysql", sizeof (vl.plugin));
375 sstrncpy (vl.type, type, sizeof (vl.type));
376 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
377 set_plugin_instance (db, &vl);
379 plugin_dispatch_values (&vl);
380 } /* void counter_submit */
382 static void qcache_submit (counter_t hits, counter_t inserts,
383 counter_t not_cached, counter_t lowmem_prunes,
384 gauge_t queries_in_cache, mysql_database_t *db)
387 value_list_t vl = VALUE_LIST_INIT;
389 values[0].counter = hits;
390 values[1].counter = inserts;
391 values[2].counter = not_cached;
392 values[3].counter = lowmem_prunes;
393 values[4].gauge = queries_in_cache;
398 sstrncpy (vl.plugin, "mysql", sizeof (vl.plugin));
399 sstrncpy (vl.type, "mysql_qcache", sizeof (vl.type));
400 set_plugin_instance (db, &vl);
402 plugin_dispatch_values (&vl);
403 } /* void qcache_submit */
405 static void threads_submit (gauge_t running, gauge_t connected, gauge_t cached,
406 counter_t created, mysql_database_t *db)
409 value_list_t vl = VALUE_LIST_INIT;
411 values[0].gauge = running;
412 values[1].gauge = connected;
413 values[2].gauge = cached;
414 values[3].counter = created;
419 sstrncpy (vl.plugin, "mysql", sizeof (vl.plugin));
420 sstrncpy (vl.type, "mysql_threads", sizeof (vl.type));
421 set_plugin_instance (db, &vl);
423 plugin_dispatch_values (&vl);
424 } /* void threads_submit */
426 static void traffic_submit (counter_t rx, counter_t tx, mysql_database_t *db)
429 value_list_t vl = VALUE_LIST_INIT;
431 values[0].counter = rx;
432 values[1].counter = tx;
437 sstrncpy (vl.plugin, "mysql", sizeof (vl.plugin));
438 sstrncpy (vl.type, "mysql_octets", sizeof (vl.type));
439 set_plugin_instance (db, &vl);
441 plugin_dispatch_values (&vl);
442 } /* void traffic_submit */
444 static int mysql_read (user_data_t *ud)
446 mysql_database_t *db;
454 unsigned long long qcache_hits = 0ULL;
455 unsigned long long qcache_inserts = 0ULL;
456 unsigned long long qcache_not_cached = 0ULL;
457 unsigned long long qcache_lowmem_prunes = 0ULL;
458 int qcache_queries_in_cache = -1;
460 int threads_running = -1;
461 int threads_connected = -1;
462 int threads_cached = -1;
463 unsigned long long threads_created = 0ULL;
465 unsigned long long traffic_incoming = 0ULL;
466 unsigned long long traffic_outgoing = 0ULL;
468 if ((ud == NULL) || (ud->data == NULL))
470 ERROR ("mysql plugin: mysql_database_read: Invalid user data.");
474 db = (mysql_database_t *) ud->data;
476 /* An error message will have been printed in this case */
477 if ((con = getconnection (db)) == NULL)
480 query = "SHOW STATUS";
481 if (mysql_get_server_version (con) >= 50002)
482 query = "SHOW GLOBAL STATUS";
484 query_len = strlen (query);
486 if (mysql_real_query (con, query, query_len))
488 ERROR ("mysql_real_query failed: %s\n",
493 if ((res = mysql_store_result (con)) == NULL)
495 ERROR ("mysql_store_result failed: %s\n",
500 field_num = mysql_num_fields (res);
501 while ((row = mysql_fetch_row (res)))
504 unsigned long long val;
507 val = atoll (row[1]);
509 if (strncmp (key, "Com_", 4) == 0)
514 /* Ignore `prepared statements' */
515 if (strncmp (key, "Com_stmt_", 9) != 0)
516 counter_submit ("mysql_commands", key + 4, val, db);
518 else if (strncmp (key, "Handler_", 8) == 0)
523 counter_submit ("mysql_handler", key + 8, val, db);
525 else if (strncmp (key, "Qcache_", 7) == 0)
527 if (strcmp (key, "Qcache_hits") == 0)
529 else if (strcmp (key, "Qcache_inserts") == 0)
530 qcache_inserts = val;
531 else if (strcmp (key, "Qcache_not_cached") == 0)
532 qcache_not_cached = val;
533 else if (strcmp (key, "Qcache_lowmem_prunes") == 0)
534 qcache_lowmem_prunes = val;
535 else if (strcmp (key, "Qcache_queries_in_cache") == 0)
536 qcache_queries_in_cache = (int) val;
538 else if (strncmp (key, "Bytes_", 6) == 0)
540 if (strcmp (key, "Bytes_received") == 0)
541 traffic_incoming += val;
542 else if (strcmp (key, "Bytes_sent") == 0)
543 traffic_outgoing += val;
545 else if (strncmp (key, "Threads_", 8) == 0)
547 if (strcmp (key, "Threads_running") == 0)
548 threads_running = (int) val;
549 else if (strcmp (key, "Threads_connected") == 0)
550 threads_connected = (int) val;
551 else if (strcmp (key, "Threads_cached") == 0)
552 threads_cached = (int) val;
553 else if (strcmp (key, "Threads_created") == 0)
554 threads_created = val;
557 mysql_free_result (res); res = NULL;
559 if ((qcache_hits != 0ULL)
560 || (qcache_inserts != 0ULL)
561 || (qcache_not_cached != 0ULL)
562 || (qcache_lowmem_prunes != 0ULL))
563 qcache_submit (qcache_hits, qcache_inserts, qcache_not_cached,
564 qcache_lowmem_prunes, qcache_queries_in_cache, db);
566 if (threads_created != 0ULL)
567 threads_submit (threads_running, threads_connected,
568 threads_cached, threads_created, db);
570 traffic_submit (traffic_incoming, traffic_outgoing, db);
573 } /* int mysql_read */
575 void module_register (void)
577 plugin_register_complex_config ("mysql", mysql_config);
578 } /* void module_register */