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:lowmem_prunes:COUNTER:25:0:U",
81 "DS:queries_in_cache:GAUGE:25:0:U",
84 static int qcache_ds_num = 5;
86 static char *threads_ds_def[] =
88 "DS:running:GAUGE:25:0:U",
89 "DS:connected:GAUGE:25:0:U",
90 "DS:cached:GAUGE:25:0:U",
91 "DS:created:COUNTER:25:0:U",
94 static int threads_ds_num = 4;
96 static char *traffic_ds_def[] =
98 "DS:incoming:COUNTER:25:0:U",
99 "DS:outgoing:COUNTER:25:0:U",
102 static int traffic_ds_num = 2;
104 static char *config_keys[] =
112 static int config_keys_num = 4;
115 static MYSQL *getconnection (void)
123 if ((err = mysql_ping (con)) != 0)
125 syslog (LOG_WARNING, "mysql_ping failed: %s", mysql_error (con));
135 if ((con = mysql_init (con)) == NULL)
137 syslog (LOG_ERR, "mysql_init failed: %s", mysql_error (con));
142 if (mysql_real_connect (con, host, user, pass, db, 0, NULL, 0) == NULL)
144 syslog (LOG_ERR, "mysql_real_connect failed: %s", mysql_error (con));
153 } /* static MYSQL *getconnection (void) */
154 #endif /* MYSQL_HAVE_READ */
156 static void init (void)
159 if (getconnection () != NULL)
163 syslog (LOG_ERR, "The `mysql' plugin will be disabled because `init' failed to connect to `%s'", host);
166 #endif /* MYSQL_HAVE_READ */
171 static int config (char *key, char *value)
173 if (strcasecmp (key, "host") == 0)
174 return ((host = strdup (value)) == NULL ? 1 : 0);
175 else if (strcasecmp (key, "user") == 0)
176 return ((user = strdup (value)) == NULL ? 1 : 0);
177 else if (strcasecmp (key, "password") == 0)
178 return ((pass = strdup (value)) == NULL ? 1 : 0);
179 else if (strcasecmp (key, "database") == 0)
180 return ((db = strdup (value)) == NULL ? 1 : 0);
185 static void commands_write (char *host, char *inst, char *val)
189 if (snprintf (buf, BUFSIZE, commands_file, inst) >= BUFSIZE)
192 rrd_update_file (host, buf, val, commands_ds_def, commands_ds_num);
195 static void handler_write (char *host, char *inst, char *val)
199 if (snprintf (buf, BUFSIZE, handler_file, inst) >= BUFSIZE)
202 rrd_update_file (host, buf, val, handler_ds_def, handler_ds_num);
205 static void qcache_write (char *host, char *inst, char *val)
207 rrd_update_file (host, qcache_file, val,
208 qcache_ds_def, qcache_ds_num);
211 static void threads_write (char *host, char *inst, char *val)
213 rrd_update_file (host, threads_file, val,
214 threads_ds_def, threads_ds_num);
217 static void traffic_write (char *host, char *inst, char *val)
219 rrd_update_file (host, traffic_file, val,
220 traffic_ds_def, traffic_ds_num);
224 static void commands_submit (char *inst, unsigned long long value)
229 status = snprintf (buf, BUFSIZE, "%u:%llu", (unsigned int) curtime, value);
233 syslog (LOG_ERR, "snprintf failed");
236 else if (status >= BUFSIZE)
238 syslog (LOG_WARNING, "snprintf was truncated");
242 plugin_submit ("mysql_commands", inst, buf);
245 static void handler_submit (char *inst, unsigned long long value)
250 status = snprintf (buf, BUFSIZE, "%u:%llu", (unsigned int) curtime, value);
254 syslog (LOG_ERR, "snprintf failed");
257 else if (status >= BUFSIZE)
259 syslog (LOG_WARNING, "snprintf was truncated");
263 plugin_submit ("mysql_handler", inst, buf);
266 static void qcache_submit (unsigned long long hits, unsigned long long inserts,
267 unsigned long long not_cached, unsigned long long lowmem_prunes,
268 int queries_in_cache)
273 status = snprintf (buf, BUFSIZE, "%u:%llu:%llu:%llu:%llu:%i",
274 (unsigned int) curtime, hits, inserts, not_cached,
275 lowmem_prunes, queries_in_cache);
279 syslog (LOG_ERR, "snprintf failed");
282 else if (status >= BUFSIZE)
284 syslog (LOG_WARNING, "snprintf was truncated");
288 plugin_submit ("mysql_qcache", "-", buf);
291 static void threads_submit (int running, int connected, int cached,
292 unsigned long long created)
297 status = snprintf (buf, BUFSIZE, "%u:%i:%i:%i:%llu",
298 (unsigned int) curtime,
299 running, connected, cached, created);
303 syslog (LOG_ERR, "snprintf failed");
306 else if (status >= BUFSIZE)
308 syslog (LOG_WARNING, "snprintf was truncated");
312 plugin_submit ("mysql_threads", "-", buf);
315 static void traffic_submit (unsigned long long incoming,
316 unsigned long long outgoing)
321 status = snprintf (buf, BUFSIZE, "%u:%llu:%llu", (unsigned int) curtime,
326 syslog (LOG_ERR, "snprintf failed");
329 else if (status >= BUFSIZE)
331 syslog (LOG_WARNING, "snprintf was truncated");
335 plugin_submit ("mysql_traffic", "-", buf);
338 static void mysql_read (void)
347 unsigned long long qcache_hits = 0ULL;
348 unsigned long long qcache_inserts = 0ULL;
349 unsigned long long qcache_not_cached = 0ULL;
350 unsigned long long qcache_lowmem_prunes = 0ULL;
351 int qcache_queries_in_cache = -1;
353 int threads_running = -1;
354 int threads_connected = -1;
355 int threads_cached = -1;
356 unsigned long long threads_created = 0ULL;
358 unsigned long long traffic_incoming = 0ULL;
359 unsigned long long traffic_outgoing = 0ULL;
361 if (init_suceeded == 0)
364 /* An error message will have been printed in this case */
365 if ((con = getconnection ()) == NULL)
368 query = "SHOW STATUS";
369 query_len = strlen (query);
371 if (mysql_real_query (con, query, query_len))
373 syslog (LOG_ERR, "mysql_real_query failed: %s\n",
378 if ((res = mysql_store_result (con)) == NULL)
380 syslog (LOG_ERR, "mysql_store_result failed: %s\n",
385 field_num = mysql_num_fields (res);
386 while ((row = mysql_fetch_row (res)))
389 unsigned long long val;
392 val = atoll (row[1]);
394 if (strncmp (key, "Com_", 4) == 0)
399 /* Ignore `prepared statements' */
400 if (strncmp (key, "Com_stmt_", 9) != 0)
401 commands_submit (key + 4, val);
403 else if (strncmp (key, "Handler_", 8) == 0)
408 handler_submit (key + 8, val);
410 else if (strncmp (key, "Qcache_", 7) == 0)
412 if (strcmp (key, "Qcache_hits") == 0)
414 else if (strcmp (key, "Qcache_inserts") == 0)
415 qcache_inserts = val;
416 else if (strcmp (key, "Qcache_not_cached") == 0)
417 qcache_not_cached = val;
418 else if (strcmp (key, "Qcache_lowmem_prunes") == 0)
419 qcache_lowmem_prunes = val;
420 else if (strcmp (key, "Qcache_queries_in_cache") == 0)
421 qcache_queries_in_cache = (int) val;
423 else if (strncmp (key, "Bytes_", 6) == 0)
425 if (strcmp (key, "Bytes_received") == 0)
426 traffic_incoming += val;
427 else if (strcmp (key, "Bytes_sent") == 0)
428 traffic_outgoing += val;
430 else if (strncmp (key, "Threads_", 8) == 0)
432 if (strcmp (key, "Threads_running") == 0)
433 threads_running = (int) val;
434 else if (strcmp (key, "Threads_connected") == 0)
435 threads_connected = (int) val;
436 else if (strcmp (key, "Threads_cached") == 0)
437 threads_cached = (int) val;
438 else if (strcmp (key, "Threads_created") == 0)
439 threads_created = val;
442 mysql_free_result (res); res = NULL;
444 if ((qcache_hits != 0ULL)
445 || (qcache_inserts != 0ULL)
446 || (qcache_not_cached != 0ULL)
447 || (qcache_lowmem_prunes != 0ULL))
448 qcache_submit (qcache_hits, qcache_inserts, qcache_not_cached,
449 qcache_lowmem_prunes, qcache_queries_in_cache);
451 if (threads_created != 0ULL)
452 threads_submit (threads_running, threads_connected,
453 threads_cached, threads_created);
455 traffic_submit (traffic_incoming, traffic_outgoing);
457 /* mysql_close (con); */
462 # define mysql_read NULL
463 #endif /* MYSQL_HAVE_READ */
465 void module_register (void)
467 plugin_register (MODULE_NAME, init, mysql_read, NULL);
468 plugin_register ("mysql_commands", NULL, NULL, commands_write);
469 plugin_register ("mysql_handler", NULL, NULL, handler_write);
470 plugin_register ("mysql_qcache", NULL, NULL, qcache_write);
471 plugin_register ("mysql_threads", NULL, NULL, threads_write);
472 plugin_register ("mysql_traffic", NULL, NULL, traffic_write);
473 cf_register (MODULE_NAME, config, config_keys, config_keys_num);