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"
27 #ifdef HAVE_MYSQL_MYSQL_H
28 #include <mysql/mysql.h>
32 # define MYSQL_HAVE_READ 1
34 # define MYSQL_HAVE_READ 0
37 /* TODO: Understand `Select_*' and possibly do that stuff as well.. */
39 static data_source_t data_source_counter[1] =
41 {"value", DS_TYPE_COUNTER, 0, NAN}
44 static data_set_t ds_commands =
46 "mysql_commands", 1, data_source_counter
49 static data_set_t ds_handler =
51 "mysql_handler", 1, data_source_counter
54 static data_source_t data_source_qcache[5] =
56 {"hits", DS_TYPE_COUNTER, 0, NAN},
57 {"inserts", DS_TYPE_COUNTER, 0, NAN},
58 {"not_cached", DS_TYPE_COUNTER, 0, NAN},
59 {"lowmem_prunes", DS_TYPE_COUNTER, 0, NAN},
60 {"queries_in_cache", DS_TYPE_GAUGE, 0, NAN}
63 static data_set_t ds_qcache =
65 "mysql_qcache", 5, data_source_qcache
68 static data_source_t data_source_threads[4] =
70 {"running", DS_TYPE_GAUGE, 0, NAN},
71 {"connected", DS_TYPE_GAUGE, 0, NAN},
72 {"cached", DS_TYPE_GAUGE, 0, NAN},
73 {"created", DS_TYPE_COUNTER, 0, NAN}
76 static data_set_t ds_threads =
78 "mysql_threads", 4, data_source_threads
81 static data_source_t data_source_octets[2] =
83 {"rx", DS_TYPE_COUNTER, 0, 4294967295.0},
84 {"tx", DS_TYPE_COUNTER, 0, 4294967295.0}
87 static data_set_t ds_octets =
89 "mysql_octets", 2, data_source_octets
93 static const char *config_keys[] =
101 static int config_keys_num = 4;
103 static char *host = "localhost";
106 static char *db = NULL;
108 static MYSQL *getconnection (void)
113 static int wait_for = 0;
114 static int wait_increase = 60;
119 if ((err = mysql_ping (con)) != 0)
121 WARNING ("mysql_ping failed: %s", mysql_error (con));
133 wait_for -= interval_g;
137 wait_for = wait_increase;
139 if (wait_increase > 86400)
140 wait_increase = 86400;
142 if ((con = mysql_init (con)) == NULL)
144 ERROR ("mysql_init failed: %s", mysql_error (con));
149 if (mysql_real_connect (con, host, user, pass, db, 0, NULL, 0) == NULL)
151 ERROR ("mysql_real_connect failed: %s", mysql_error (con));
162 } /* static MYSQL *getconnection (void) */
164 static int config (const char *key, const char *value)
166 if (strcasecmp (key, "host") == 0)
167 return ((host = strdup (value)) == NULL ? 1 : 0);
168 else if (strcasecmp (key, "user") == 0)
169 return ((user = strdup (value)) == NULL ? 1 : 0);
170 else if (strcasecmp (key, "password") == 0)
171 return ((pass = strdup (value)) == NULL ? 1 : 0);
172 else if (strcasecmp (key, "database") == 0)
173 return ((db = strdup (value)) == NULL ? 1 : 0);
178 static void counter_submit (const char *type, const char *type_instance,
182 value_list_t vl = VALUE_LIST_INIT;
184 values[0].counter = value;
188 vl.time = time (NULL);
189 strcpy (vl.host, hostname_g);
190 strcpy (vl.plugin, "mysql");
191 strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
193 plugin_dispatch_values (type, &vl);
194 } /* void counter_submit */
196 static void qcache_submit (counter_t hits, counter_t inserts,
197 counter_t not_cached, counter_t lowmem_prunes,
198 gauge_t queries_in_cache)
201 value_list_t vl = VALUE_LIST_INIT;
203 values[0].counter = hits;
204 values[1].counter = inserts;
205 values[2].counter = not_cached;
206 values[3].counter = lowmem_prunes;
207 values[4].gauge = queries_in_cache;
211 vl.time = time (NULL);
212 strcpy (vl.host, hostname_g);
213 strcpy (vl.plugin, "mysql");
215 plugin_dispatch_values ("mysql_qcache", &vl);
216 } /* void qcache_submit */
218 static void threads_submit (gauge_t running, gauge_t connected, gauge_t cached,
222 value_list_t vl = VALUE_LIST_INIT;
224 values[0].gauge = running;
225 values[1].gauge = connected;
226 values[2].gauge = cached;
227 values[3].counter = created;
231 vl.time = time (NULL);
232 strcpy (vl.host, hostname_g);
233 strcpy (vl.plugin, "mysql");
235 plugin_dispatch_values ("mysql_threads", &vl);
236 } /* void threads_submit */
238 static void traffic_submit (counter_t rx, counter_t tx)
241 value_list_t vl = VALUE_LIST_INIT;
243 values[0].counter = rx;
244 values[1].counter = tx;
248 vl.time = time (NULL);
249 strcpy (vl.host, hostname_g);
250 strcpy (vl.plugin, "mysql");
252 plugin_dispatch_values ("mysql_octets", &vl);
253 } /* void traffic_submit */
255 static int mysql_read (void)
264 unsigned long long qcache_hits = 0ULL;
265 unsigned long long qcache_inserts = 0ULL;
266 unsigned long long qcache_not_cached = 0ULL;
267 unsigned long long qcache_lowmem_prunes = 0ULL;
268 int qcache_queries_in_cache = -1;
270 int threads_running = -1;
271 int threads_connected = -1;
272 int threads_cached = -1;
273 unsigned long long threads_created = 0ULL;
275 unsigned long long traffic_incoming = 0ULL;
276 unsigned long long traffic_outgoing = 0ULL;
278 /* An error message will have been printed in this case */
279 if ((con = getconnection ()) == NULL)
282 query = "SHOW STATUS";
283 if (mysql_get_server_version (con) >= 50002)
284 query = "SHOW GLOBAL STATUS";
286 query_len = strlen (query);
288 if (mysql_real_query (con, query, query_len))
290 ERROR ("mysql_real_query failed: %s\n",
295 if ((res = mysql_store_result (con)) == NULL)
297 ERROR ("mysql_store_result failed: %s\n",
302 field_num = mysql_num_fields (res);
303 while ((row = mysql_fetch_row (res)))
306 unsigned long long val;
309 val = atoll (row[1]);
311 if (strncmp (key, "Com_", 4) == 0)
316 /* Ignore `prepared statements' */
317 if (strncmp (key, "Com_stmt_", 9) != 0)
318 counter_submit ("mysql_commands", key + 4, val);
320 else if (strncmp (key, "Handler_", 8) == 0)
325 counter_submit ("mysql_handler", key + 8, val);
327 else if (strncmp (key, "Qcache_", 7) == 0)
329 if (strcmp (key, "Qcache_hits") == 0)
331 else if (strcmp (key, "Qcache_inserts") == 0)
332 qcache_inserts = val;
333 else if (strcmp (key, "Qcache_not_cached") == 0)
334 qcache_not_cached = val;
335 else if (strcmp (key, "Qcache_lowmem_prunes") == 0)
336 qcache_lowmem_prunes = val;
337 else if (strcmp (key, "Qcache_queries_in_cache") == 0)
338 qcache_queries_in_cache = (int) val;
340 else if (strncmp (key, "Bytes_", 6) == 0)
342 if (strcmp (key, "Bytes_received") == 0)
343 traffic_incoming += val;
344 else if (strcmp (key, "Bytes_sent") == 0)
345 traffic_outgoing += val;
347 else if (strncmp (key, "Threads_", 8) == 0)
349 if (strcmp (key, "Threads_running") == 0)
350 threads_running = (int) val;
351 else if (strcmp (key, "Threads_connected") == 0)
352 threads_connected = (int) val;
353 else if (strcmp (key, "Threads_cached") == 0)
354 threads_cached = (int) val;
355 else if (strcmp (key, "Threads_created") == 0)
356 threads_created = val;
359 mysql_free_result (res); res = NULL;
361 if ((qcache_hits != 0ULL)
362 || (qcache_inserts != 0ULL)
363 || (qcache_not_cached != 0ULL)
364 || (qcache_lowmem_prunes != 0ULL))
365 qcache_submit (qcache_hits, qcache_inserts, qcache_not_cached,
366 qcache_lowmem_prunes, qcache_queries_in_cache);
368 if (threads_created != 0ULL)
369 threads_submit (threads_running, threads_connected,
370 threads_cached, threads_created);
372 traffic_submit (traffic_incoming, traffic_outgoing);
374 /* mysql_close (con); */
377 } /* int mysql_read */
378 #endif /* MYSQL_HAVE_READ */
380 void module_register (void)
382 plugin_register_data_set (&ds_commands);
383 plugin_register_data_set (&ds_handler);
384 plugin_register_data_set (&ds_qcache);
385 plugin_register_data_set (&ds_threads);
386 plugin_register_data_set (&ds_octets);
389 plugin_register_config ("mysql", config, config_keys, config_keys_num);
390 plugin_register_read ("mysql", mysql_read);
391 #endif /* MYSQL_HAVE_READ */
392 } /* void module_register */