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;
121 if ((err = mysql_ping (con)) != 0)
123 syslog (LOG_WARNING, "mysql_ping failed: %s", mysql_error (con));
133 step = atoi (COLLECTD_STEP);
141 wait_for = wait_increase;
143 if (wait_increase > 86400)
144 wait_increase = 86400;
146 if ((con = mysql_init (con)) == NULL)
148 syslog (LOG_ERR, "mysql_init failed: %s", mysql_error (con));
153 if (mysql_real_connect (con, host, user, pass, db, 0, NULL, 0) == NULL)
155 syslog (LOG_ERR, "mysql_real_connect failed: %s", mysql_error (con));
166 } /* static MYSQL *getconnection (void) */
168 static int config (const char *key, const char *value)
170 if (strcasecmp (key, "host") == 0)
171 return ((host = strdup (value)) == NULL ? 1 : 0);
172 else if (strcasecmp (key, "user") == 0)
173 return ((user = strdup (value)) == NULL ? 1 : 0);
174 else if (strcasecmp (key, "password") == 0)
175 return ((pass = strdup (value)) == NULL ? 1 : 0);
176 else if (strcasecmp (key, "database") == 0)
177 return ((db = strdup (value)) == NULL ? 1 : 0);
182 static void counter_submit (const char *type, const char *type_instance,
186 value_list_t vl = VALUE_LIST_INIT;
188 values[0].counter = value;
192 vl.time = time (NULL);
193 strcpy (vl.host, hostname_g);
194 strcpy (vl.plugin, "mysql");
195 strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
197 plugin_dispatch_values (type, &vl);
198 } /* void counter_submit */
200 static void qcache_submit (counter_t hits, counter_t inserts,
201 counter_t not_cached, counter_t lowmem_prunes,
202 gauge_t queries_in_cache)
205 value_list_t vl = VALUE_LIST_INIT;
207 values[0].counter = hits;
208 values[1].counter = inserts;
209 values[2].counter = not_cached;
210 values[3].counter = lowmem_prunes;
211 values[4].gauge = queries_in_cache;
215 vl.time = time (NULL);
216 strcpy (vl.host, hostname_g);
217 strcpy (vl.plugin, "mysql");
219 plugin_dispatch_values ("mysql_qcache", &vl);
220 } /* void qcache_submit */
222 static void threads_submit (gauge_t running, gauge_t connected, gauge_t cached,
226 value_list_t vl = VALUE_LIST_INIT;
228 values[0].gauge = running;
229 values[1].gauge = connected;
230 values[2].gauge = cached;
231 values[3].counter = created;
235 vl.time = time (NULL);
236 strcpy (vl.host, hostname_g);
237 strcpy (vl.plugin, "mysql");
239 plugin_dispatch_values ("mysql_threads", &vl);
240 } /* void threads_submit */
242 static void traffic_submit (counter_t rx, counter_t tx)
245 value_list_t vl = VALUE_LIST_INIT;
247 values[0].counter = rx;
248 values[1].counter = tx;
252 vl.time = time (NULL);
253 strcpy (vl.host, hostname_g);
254 strcpy (vl.plugin, "mysql");
256 plugin_dispatch_values ("mysql_octets", &vl);
257 } /* void traffic_submit */
259 static int mysql_read (void)
268 unsigned long long qcache_hits = 0ULL;
269 unsigned long long qcache_inserts = 0ULL;
270 unsigned long long qcache_not_cached = 0ULL;
271 unsigned long long qcache_lowmem_prunes = 0ULL;
272 int qcache_queries_in_cache = -1;
274 int threads_running = -1;
275 int threads_connected = -1;
276 int threads_cached = -1;
277 unsigned long long threads_created = 0ULL;
279 unsigned long long traffic_incoming = 0ULL;
280 unsigned long long traffic_outgoing = 0ULL;
282 /* An error message will have been printed in this case */
283 if ((con = getconnection ()) == NULL)
286 query = "SHOW STATUS";
287 if (mysql_get_server_version (con) >= 50002)
288 query = "SHOW GLOBAL STATUS";
290 query_len = strlen (query);
292 if (mysql_real_query (con, query, query_len))
294 syslog (LOG_ERR, "mysql_real_query failed: %s\n",
299 if ((res = mysql_store_result (con)) == NULL)
301 syslog (LOG_ERR, "mysql_store_result failed: %s\n",
306 field_num = mysql_num_fields (res);
307 while ((row = mysql_fetch_row (res)))
310 unsigned long long val;
313 val = atoll (row[1]);
315 if (strncmp (key, "Com_", 4) == 0)
320 /* Ignore `prepared statements' */
321 if (strncmp (key, "Com_stmt_", 9) != 0)
322 counter_submit ("mysql_commands", key + 4, val);
324 else if (strncmp (key, "Handler_", 8) == 0)
329 counter_submit ("mysql_handler", key + 8, val);
331 else if (strncmp (key, "Qcache_", 7) == 0)
333 if (strcmp (key, "Qcache_hits") == 0)
335 else if (strcmp (key, "Qcache_inserts") == 0)
336 qcache_inserts = val;
337 else if (strcmp (key, "Qcache_not_cached") == 0)
338 qcache_not_cached = val;
339 else if (strcmp (key, "Qcache_lowmem_prunes") == 0)
340 qcache_lowmem_prunes = val;
341 else if (strcmp (key, "Qcache_queries_in_cache") == 0)
342 qcache_queries_in_cache = (int) val;
344 else if (strncmp (key, "Bytes_", 6) == 0)
346 if (strcmp (key, "Bytes_received") == 0)
347 traffic_incoming += val;
348 else if (strcmp (key, "Bytes_sent") == 0)
349 traffic_outgoing += val;
351 else if (strncmp (key, "Threads_", 8) == 0)
353 if (strcmp (key, "Threads_running") == 0)
354 threads_running = (int) val;
355 else if (strcmp (key, "Threads_connected") == 0)
356 threads_connected = (int) val;
357 else if (strcmp (key, "Threads_cached") == 0)
358 threads_cached = (int) val;
359 else if (strcmp (key, "Threads_created") == 0)
360 threads_created = val;
363 mysql_free_result (res); res = NULL;
365 if ((qcache_hits != 0ULL)
366 || (qcache_inserts != 0ULL)
367 || (qcache_not_cached != 0ULL)
368 || (qcache_lowmem_prunes != 0ULL))
369 qcache_submit (qcache_hits, qcache_inserts, qcache_not_cached,
370 qcache_lowmem_prunes, qcache_queries_in_cache);
372 if (threads_created != 0ULL)
373 threads_submit (threads_running, threads_connected,
374 threads_cached, threads_created);
376 traffic_submit (traffic_incoming, traffic_outgoing);
378 /* mysql_close (con); */
381 } /* int mysql_read */
382 #endif /* MYSQL_HAVE_READ */
384 void module_register (void)
386 plugin_register_data_set (&ds_commands);
387 plugin_register_data_set (&ds_handler);
388 plugin_register_data_set (&ds_qcache);
389 plugin_register_data_set (&ds_threads);
390 plugin_register_data_set (&ds_octets);
393 plugin_register_config ("mysql", config, config_keys, config_keys_num);
394 plugin_register_read ("mysql", mysql_read);
395 #endif /* MYSQL_HAVE_READ */
396 } /* void module_register */