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 */
277 char cb_name[DATA_MAX_NAME_LEN];
279 DEBUG ("mysql plugin: Registering new read callback: %s", db->database);
281 memset (&ud, 0, sizeof (ud));
282 ud.data = (void *) db;
283 ud.free_func = mysql_database_free;
285 if (db->database != NULL)
286 ssnprintf (cb_name, sizeof (cb_name), "mysql-%s",
289 sstrncpy (cb_name, "mysql", sizeof (cb_name));
291 plugin_register_complex_read (cb_name, mysql_read,
292 /* interval = */ NULL, &ud);
296 mysql_database_free (db);
301 } /* }}} int mysql_config */
303 /* }}} End of configuration handling functions */
305 static MYSQL *getconnection (mysql_database_t *db)
310 if ((err = mysql_ping (db->con)) != 0)
312 WARNING ("mysql_ping failed: %s", mysql_error (db->con));
322 if ((db->con = mysql_init (db->con)) == NULL)
324 ERROR ("mysql_init failed: %s", mysql_error (db->con));
329 if (mysql_real_connect (db->con, db->host, db->user, db->pass,
330 db->database, db->port, db->socket, 0) == NULL)
332 ERROR ("mysql_real_connect failed: %s", mysql_error (db->con));
341 } /* static MYSQL *getconnection (mysql_database_t *db) */
343 static void set_host (mysql_database_t *db, value_list_t *vl)
345 /* XXX legacy mode - use hostname_g */
346 if (db->instance == NULL)
347 sstrncpy (vl->host, hostname_g, sizeof (vl->host));
350 if ((db->host == NULL)
351 || (strcmp ("", db->host) == 0)
352 || (strcmp ("localhost", db->host) == 0))
353 sstrncpy (vl->host, hostname_g, sizeof (vl->host));
355 sstrncpy (vl->host, db->host, sizeof (vl->host));
359 static void set_plugin_instance (mysql_database_t *db, value_list_t *vl)
361 /* XXX legacy mode - no plugin_instance */
362 if (db->instance == NULL)
363 sstrncpy (vl->plugin_instance, "",
364 sizeof (vl->plugin_instance));
366 sstrncpy (vl->plugin_instance, db->instance,
367 sizeof (vl->plugin_instance));
370 static void counter_submit (const char *type, const char *type_instance,
371 counter_t value, mysql_database_t *db)
374 value_list_t vl = VALUE_LIST_INIT;
376 values[0].counter = value;
381 sstrncpy (vl.plugin, "mysql", sizeof (vl.plugin));
382 sstrncpy (vl.type, type, sizeof (vl.type));
383 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
384 set_plugin_instance (db, &vl);
386 plugin_dispatch_values (&vl);
387 } /* void counter_submit */
389 static void qcache_submit (counter_t hits, counter_t inserts,
390 counter_t not_cached, counter_t lowmem_prunes,
391 gauge_t queries_in_cache, mysql_database_t *db)
394 value_list_t vl = VALUE_LIST_INIT;
396 values[0].counter = hits;
397 values[1].counter = inserts;
398 values[2].counter = not_cached;
399 values[3].counter = lowmem_prunes;
400 values[4].gauge = queries_in_cache;
405 sstrncpy (vl.plugin, "mysql", sizeof (vl.plugin));
406 sstrncpy (vl.type, "mysql_qcache", sizeof (vl.type));
407 set_plugin_instance (db, &vl);
409 plugin_dispatch_values (&vl);
410 } /* void qcache_submit */
412 static void threads_submit (gauge_t running, gauge_t connected, gauge_t cached,
413 counter_t created, mysql_database_t *db)
416 value_list_t vl = VALUE_LIST_INIT;
418 values[0].gauge = running;
419 values[1].gauge = connected;
420 values[2].gauge = cached;
421 values[3].counter = created;
426 sstrncpy (vl.plugin, "mysql", sizeof (vl.plugin));
427 sstrncpy (vl.type, "mysql_threads", sizeof (vl.type));
428 set_plugin_instance (db, &vl);
430 plugin_dispatch_values (&vl);
431 } /* void threads_submit */
433 static void traffic_submit (counter_t rx, counter_t tx, mysql_database_t *db)
436 value_list_t vl = VALUE_LIST_INIT;
438 values[0].counter = rx;
439 values[1].counter = tx;
444 sstrncpy (vl.plugin, "mysql", sizeof (vl.plugin));
445 sstrncpy (vl.type, "mysql_octets", sizeof (vl.type));
446 set_plugin_instance (db, &vl);
448 plugin_dispatch_values (&vl);
449 } /* void traffic_submit */
451 static int mysql_read (user_data_t *ud)
453 mysql_database_t *db;
461 unsigned long long qcache_hits = 0ULL;
462 unsigned long long qcache_inserts = 0ULL;
463 unsigned long long qcache_not_cached = 0ULL;
464 unsigned long long qcache_lowmem_prunes = 0ULL;
465 int qcache_queries_in_cache = -1;
467 int threads_running = -1;
468 int threads_connected = -1;
469 int threads_cached = -1;
470 unsigned long long threads_created = 0ULL;
472 unsigned long long traffic_incoming = 0ULL;
473 unsigned long long traffic_outgoing = 0ULL;
475 if ((ud == NULL) || (ud->data == NULL))
477 ERROR ("mysql plugin: mysql_database_read: Invalid user data.");
481 db = (mysql_database_t *) ud->data;
483 /* An error message will have been printed in this case */
484 if ((con = getconnection (db)) == NULL)
487 query = "SHOW STATUS";
488 if (mysql_get_server_version (con) >= 50002)
489 query = "SHOW GLOBAL STATUS";
491 query_len = strlen (query);
493 if (mysql_real_query (con, query, query_len))
495 ERROR ("mysql_real_query failed: %s\n",
500 if ((res = mysql_store_result (con)) == NULL)
502 ERROR ("mysql_store_result failed: %s\n",
507 field_num = mysql_num_fields (res);
508 while ((row = mysql_fetch_row (res)))
511 unsigned long long val;
514 val = atoll (row[1]);
516 if (strncmp (key, "Com_", 4) == 0)
521 /* Ignore `prepared statements' */
522 if (strncmp (key, "Com_stmt_", 9) != 0)
523 counter_submit ("mysql_commands", key + 4, val, db);
525 else if (strncmp (key, "Handler_", 8) == 0)
530 counter_submit ("mysql_handler", key + 8, val, db);
532 else if (strncmp (key, "Qcache_", 7) == 0)
534 if (strcmp (key, "Qcache_hits") == 0)
536 else if (strcmp (key, "Qcache_inserts") == 0)
537 qcache_inserts = val;
538 else if (strcmp (key, "Qcache_not_cached") == 0)
539 qcache_not_cached = val;
540 else if (strcmp (key, "Qcache_lowmem_prunes") == 0)
541 qcache_lowmem_prunes = val;
542 else if (strcmp (key, "Qcache_queries_in_cache") == 0)
543 qcache_queries_in_cache = (int) val;
545 else if (strncmp (key, "Bytes_", 6) == 0)
547 if (strcmp (key, "Bytes_received") == 0)
548 traffic_incoming += val;
549 else if (strcmp (key, "Bytes_sent") == 0)
550 traffic_outgoing += val;
552 else if (strncmp (key, "Threads_", 8) == 0)
554 if (strcmp (key, "Threads_running") == 0)
555 threads_running = (int) val;
556 else if (strcmp (key, "Threads_connected") == 0)
557 threads_connected = (int) val;
558 else if (strcmp (key, "Threads_cached") == 0)
559 threads_cached = (int) val;
560 else if (strcmp (key, "Threads_created") == 0)
561 threads_created = val;
564 mysql_free_result (res); res = NULL;
566 if ((qcache_hits != 0ULL)
567 || (qcache_inserts != 0ULL)
568 || (qcache_not_cached != 0ULL)
569 || (qcache_lowmem_prunes != 0ULL))
570 qcache_submit (qcache_hits, qcache_inserts, qcache_not_cached,
571 qcache_lowmem_prunes, qcache_queries_in_cache, db);
573 if (threads_created != 0ULL)
574 threads_submit (threads_running, threads_connected,
575 threads_cached, threads_created, db);
577 traffic_submit (traffic_incoming, traffic_outgoing, db);
580 } /* int mysql_read */
582 void module_register (void)
584 plugin_register_complex_config ("mysql", mysql_config);
585 } /* void module_register */