2 * collectd - src/mysql.c
3 * Copyright (C) 2006 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; either version 2 of the License, or (at your
8 * option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * Florian octo Forster <octo at verplant.org>
26 #include "configfile.h"
28 #ifdef HAVE_MYSQL_MYSQL_H
29 #include <mysql/mysql.h>
32 #define MODULE_NAME "mysql"
35 # define MYSQL_HAVE_READ 1
37 # define MYSQL_HAVE_READ 0
42 static char *host = "localhost";
45 static char *db = NULL;
48 static char init_suceeded = 0;
52 * understand `Select_*' and possibly do that stuff as well..
55 static char *commands_file = "mysql/mysql_commands-%s.rrd";
56 static char *handler_file = "mysql/mysql_handler-%s.rrd";
57 static char *qcache_file = "mysql/mysql_qcache.rrd";
58 static char *threads_file = "mysql/mysql_threads.rrd";
59 static char *traffic_file = "traffic-mysql.rrd";
61 static char *commands_ds_def[] =
63 "DS:value:COUNTER:25:0:U",
66 static int commands_ds_num = 1;
68 static char *handler_ds_def[] =
70 "DS:value:COUNTER:25:0:U",
73 static int handler_ds_num = 1;
75 static char *qcache_ds_def[] =
77 "DS:hits:COUNTER:25:0:U",
78 "DS:inserts:COUNTER:25:0:U",
79 "DS:not_cached:COUNTER:25:0:U",
80 "DS:queries_in_cache:GAUGE:25:0:U",
83 static int qcache_ds_num = 4;
85 static char *threads_ds_def[] =
87 "DS:running:GAUGE:25:0:U",
88 "DS:connected:GAUGE:25:0:U",
89 "DS:cached:GAUGE:25:0:U",
90 "DS:created:COUNTER:25:0:u",
93 static int threads_ds_num = 4;
95 static char *traffic_ds_def[] =
97 "DS:incoming:COUNTER:25:0:U",
98 "DS:outgoing:COUNTER:25:0:U",
101 static int traffic_ds_num = 2;
103 static char *config_keys[] =
111 static int config_keys_num = 4;
114 static MYSQL *getconnection (void)
122 if ((err = mysql_ping (con)) != 0)
124 syslog (LOG_WARNING, "mysql_ping failed: %s", mysql_error (con));
134 if ((con = mysql_init (con)) == NULL)
136 syslog (LOG_ERR, "mysql_init failed: %s", mysql_error (con));
141 if (mysql_real_connect (con, host, user, pass, db, 0, NULL, 0) == NULL)
143 syslog (LOG_ERR, "mysql_real_connect failed: %s", mysql_error (con));
152 } /* static MYSQL *getconnection (void) */
153 #endif /* MYSQL_HAVE_READ */
155 static void init (void)
158 if (getconnection () != NULL)
162 syslog (LOG_ERR, "The `mysql' plugin will be disabled because `init' failed to connect to `%s'", host);
165 #endif /* MYSQL_HAVE_READ */
170 static int config (char *key, char *value)
172 if (strcasecmp (key, "host") == 0)
173 return ((host = strdup (value)) == NULL ? 1 : 0);
174 else if (strcasecmp (key, "user") == 0)
175 return ((user = strdup (value)) == NULL ? 1 : 0);
176 else if (strcasecmp (key, "password") == 0)
177 return ((pass = strdup (value)) == NULL ? 1 : 0);
178 else if (strcasecmp (key, "database") == 0)
179 return ((db = strdup (value)) == NULL ? 1 : 0);
184 static void commands_write (char *host, char *inst, char *val)
188 if (snprintf (buf, BUFSIZE, commands_file, inst) >= BUFSIZE)
191 rrd_update_file (host, buf, val, commands_ds_def, commands_ds_num);
194 static void handler_write (char *host, char *inst, char *val)
198 if (snprintf (buf, BUFSIZE, handler_file, inst) >= BUFSIZE)
201 rrd_update_file (host, buf, val, handler_ds_def, handler_ds_num);
204 static void threads_write (char *host, char *inst, char *val)
206 rrd_update_file (host, threads_file, val,
207 threads_ds_def, threads_ds_num);
210 static void traffic_write (char *host, char *inst, char *val)
212 rrd_update_file (host, traffic_file, val,
213 traffic_ds_def, traffic_ds_num);
216 static void qcache_write (char *host, char *inst, char *val)
218 rrd_update_file (host, qcache_file, val,
219 qcache_ds_def, qcache_ds_num);
223 static void commands_submit (char *inst, unsigned long long value)
228 status = snprintf (buf, BUFSIZE, "%u:%llu", (unsigned int) curtime, value);
232 syslog (LOG_ERR, "snprintf failed");
235 else if (status >= BUFSIZE)
237 syslog (LOG_WARNING, "snprintf was truncated");
241 plugin_submit ("mysql_commands", inst, buf);
244 static void handler_submit (char *inst, unsigned long long value)
249 status = snprintf (buf, BUFSIZE, "%u:%llu", (unsigned int) curtime, value);
253 syslog (LOG_ERR, "snprintf failed");
256 else if (status >= BUFSIZE)
258 syslog (LOG_WARNING, "snprintf was truncated");
262 plugin_submit ("mysql_handler", inst, buf);
265 static void qcache_submit (unsigned long long hits, unsigned long long inserts,
266 unsigned long long not_cached, int queries_in_cache)
271 status = snprintf (buf, BUFSIZE, "%u:%llu:%llu:%llu:%i",
272 (unsigned int) curtime, hits, inserts, not_cached,
277 syslog (LOG_ERR, "snprintf failed");
280 else if (status >= BUFSIZE)
282 syslog (LOG_WARNING, "snprintf was truncated");
286 plugin_submit ("mysql_qcache", "-", buf);
289 static void threads_submit (int running, int connected, int cached,
290 unsigned long long created)
295 status = snprintf (buf, BUFSIZE, "%u:%i:%i:%i:%llu",
296 (unsigned int) curtime,
297 running, connected, cached, created);
301 syslog (LOG_ERR, "snprintf failed");
304 else if (status >= BUFSIZE)
306 syslog (LOG_WARNING, "snprintf was truncated");
310 plugin_submit ("mysql_threads", "-", buf);
313 static void traffic_submit (unsigned long long incoming,
314 unsigned long long outgoing)
319 status = snprintf (buf, BUFSIZE, "%u:%llu:%llu", (unsigned int) curtime,
324 syslog (LOG_ERR, "snprintf failed");
327 else if (status >= BUFSIZE)
329 syslog (LOG_WARNING, "snprintf was truncated");
333 plugin_submit ("mysql_traffic", "-", buf);
336 static void mysql_read (void)
345 unsigned long long qcache_hits = 0ULL;
346 unsigned long long qcache_inserts = 0ULL;
347 unsigned long long qcache_not_cached = 0ULL;
348 int qcache_queries_in_cache = -1;
350 int threads_running = -1;
351 int threads_connected = -1;
352 int threads_cached = -1;
353 unsigned long long threads_created = 0ULL;
355 unsigned long long traffic_incoming = 0ULL;
356 unsigned long long traffic_outgoing = 0ULL;
358 if (init_suceeded == 0)
361 /* An error message will have been printed in this case */
362 if ((con = getconnection ()) == NULL)
365 query = "SHOW STATUS";
366 query_len = strlen (query);
368 if (mysql_real_query (con, query, query_len))
370 syslog (LOG_ERR, "mysql_real_query failed: %s\n",
375 if ((res = mysql_store_result (con)) == NULL)
377 syslog (LOG_ERR, "mysql_store_result failed: %s\n",
382 field_num = mysql_num_fields (res);
383 while ((row = mysql_fetch_row (res)))
386 unsigned long long val;
389 val = atoll (row[1]);
391 if (strncmp (key, "Com_", 4) == 0)
396 /* Ignore `prepared statements' */
397 if (strncmp (key, "Com_stmt_", 9) != 0)
398 commands_submit (key + 4, val);
400 else if (strncmp (key, "Handler_", 8) == 0)
405 handler_submit (key + 8, val);
407 else if (strncmp (key, "Qcache_", 7) == 0)
409 if (strcmp (key, "Qcache_hits") == 0)
411 else if (strcmp (key, "Qcache_inserts") == 0)
412 qcache_inserts = val;
413 else if (strcmp (key, "Qcache_not_cached") == 0)
414 qcache_not_cached = val;
415 else if (strcmp (key, "Qcache_queries_in_cache") == 0)
416 qcache_queries_in_cache = (int) val;
418 else if (strncmp (key, "Bytes_", 6) == 0)
420 if (strcmp (key, "Bytes_received") == 0)
421 traffic_incoming += val;
422 else if (strcmp (key, "Bytes_sent") == 0)
423 traffic_outgoing += val;
425 else if (strncmp (key, "Threads_", 8) == 0)
427 if (strcmp (key, "Threads_running") == 0)
428 threads_running = (int) val;
429 else if (strcmp (key, "Threads_connected") == 0)
430 threads_connected = (int) val;
431 else if (strcmp (key, "Threads_cached") == 0)
432 threads_cached = (int) val;
433 else if (strcmp (key, "Threads_created") == 0)
434 threads_created = val;
437 mysql_free_result (res); res = NULL;
439 if ((qcache_hits != 0ULL)
440 || (qcache_inserts != 0ULL)
441 || (qcache_not_cached != 0ULL))
442 qcache_submit (qcache_hits, qcache_inserts, qcache_not_cached,
443 qcache_queries_in_cache);
445 if (threads_created != 0ULL)
446 threads_submit (threads_running, threads_connected,
447 threads_cached, threads_created);
449 traffic_submit (traffic_incoming, traffic_outgoing);
451 /* mysql_close (con); */
456 # define mysql_read NULL
457 #endif /* MYSQL_HAVE_READ */
459 void module_register (void)
461 plugin_register (MODULE_NAME, init, mysql_read, NULL);
462 plugin_register ("mysql_commands", NULL, NULL, commands_write);
463 plugin_register ("mysql_handler", NULL, NULL, handler_write);
464 plugin_register ("mysql_qcache", NULL, NULL, qcache_write);
465 plugin_register ("mysql_threads", NULL, NULL, threads_write);
466 plugin_register ("mysql_traffic", NULL, NULL, traffic_write);
467 cf_register (MODULE_NAME, config, config_keys, config_keys_num);