2 * collectd - src/mysql.c
3 * Copyright (C) 2005 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;
51 static char *commands_file = "mysql/mysql_commands-%s.rrd";
52 static char *handler_file = "mysql/mysql_handler-%s.rrd";
53 static char *threads_file = "mysql/mysql_threads.rrd";
54 static char *traffic_file = "traffic-mysql.rrd";
56 static char *commands_ds_def[] =
58 "DS:value:COUNTER:25:0:U",
61 static int commands_ds_num = 1;
63 static char *handler_ds_def[] =
65 "DS:value:COUNTER:25:0:U",
68 static int handler_ds_num = 1;
70 static char *threads_ds_def[] =
72 "DS:running:GAUGE:25:0:U",
73 "DS:connected:GAUGE:25:0:U",
74 "DS:cached:GAUGE:25:0:U",
77 static int threads_ds_num = 3;
79 static char *traffic_ds_def[] =
81 "DS:incoming:COUNTER:25:0:U",
82 "DS:outgoing:COUNTER:25:0:U",
85 static int traffic_ds_num = 2;
87 static char *config_keys[] =
95 static int config_keys_num = 4;
98 static MYSQL *getconnection (void)
106 if ((err = mysql_ping (con)) != 0)
108 syslog (LOG_WARNING, "mysql_ping failed: %s", mysql_error (con));
118 if ((con = mysql_init (con)) == NULL)
120 syslog (LOG_ERR, "mysql_init failed: %s", mysql_error (con));
125 if (mysql_real_connect (con, host, user, pass, db, 0, NULL, 0) == NULL)
127 syslog (LOG_ERR, "mysql_real_connect failed: %s", mysql_error (con));
136 } /* static MYSQL *getconnection (void) */
137 #endif /* MYSQL_HAVE_READ */
139 static void init (void)
142 if (getconnection () != NULL)
146 syslog (LOG_ERR, "The `mysql' plugin will be disabled because `init' failed to connect to `%s'", host);
149 #endif /* MYSQL_HAVE_READ */
154 static int config (char *key, char *value)
156 if (strcasecmp (key, "host") == 0)
157 return ((host = strdup (value)) == NULL ? 1 : 0);
158 else if (strcasecmp (key, "user") == 0)
159 return ((user = strdup (value)) == NULL ? 1 : 0);
160 else if (strcasecmp (key, "password") == 0)
161 return ((pass = strdup (value)) == NULL ? 1 : 0);
162 else if (strcasecmp (key, "database") == 0)
163 return ((db = strdup (value)) == NULL ? 1 : 0);
168 static void commands_write (char *host, char *inst, char *val)
172 if (snprintf (buf, BUFSIZE, commands_file, inst) >= BUFSIZE)
175 rrd_update_file (host, buf, val, commands_ds_def, commands_ds_num);
178 static void handler_write (char *host, char *inst, char *val)
182 if (snprintf (buf, BUFSIZE, handler_file, inst) >= BUFSIZE)
185 rrd_update_file (host, buf, val, handler_ds_def, handler_ds_num);
188 static void threads_write (char *host, char *inst, char *val)
190 rrd_update_file (host, threads_file, val,
191 threads_ds_def, threads_ds_num);
194 static void traffic_write (char *host, char *inst, char *val)
196 rrd_update_file (host, traffic_file, val,
197 traffic_ds_def, traffic_ds_num);
201 static void commands_submit (char *inst, unsigned long long value)
206 status = snprintf (buf, BUFSIZE, "%u:%llu", (unsigned int) curtime, value);
210 syslog (LOG_ERR, "snprintf failed");
213 else if (status >= BUFSIZE)
215 syslog (LOG_WARNING, "snprintf was truncated");
219 plugin_submit ("mysql_commands", inst, buf);
222 static void handler_submit (char *inst, unsigned long long value)
227 status = snprintf (buf, BUFSIZE, "%u:%llu", (unsigned int) curtime, value);
231 syslog (LOG_ERR, "snprintf failed");
234 else if (status >= BUFSIZE)
236 syslog (LOG_WARNING, "snprintf was truncated");
240 plugin_submit ("mysql_handler", inst, buf);
243 static void threads_submit (int running, int connected, int cached)
248 status = snprintf (buf, BUFSIZE, "%u:%i:%i:%i", (unsigned int) curtime,
249 running, connected, cached);
253 syslog (LOG_ERR, "snprintf failed");
256 else if (status >= BUFSIZE)
258 syslog (LOG_WARNING, "snprintf was truncated");
262 plugin_submit ("mysql_threads", "-", buf);
265 static void traffic_submit (unsigned long long incoming,
266 unsigned long long outgoing)
271 status = snprintf (buf, BUFSIZE, "%u:%llu:%llu", (unsigned int) curtime,
276 syslog (LOG_ERR, "snprintf failed");
279 else if (status >= BUFSIZE)
281 syslog (LOG_WARNING, "snprintf was truncated");
285 plugin_submit ("mysql_traffic", "-", buf);
288 static void mysql_read (void)
297 int threads_running = -1;
298 int threads_connected = -1;
299 int threads_cached = -1;
301 unsigned long long traffic_incoming = 0LL;
302 unsigned long long traffic_outgoing = 0LL;
304 if (init_suceeded == 0)
307 /* An error message will have been printed in this case */
308 if ((con = getconnection ()) == NULL)
311 query = "SHOW STATUS";
312 query_len = strlen (query);
314 if (mysql_real_query (con, query, query_len))
316 syslog (LOG_ERR, "mysql_real_query failed: %s\n",
321 if ((res = mysql_store_result (con)) == NULL)
323 syslog (LOG_ERR, "mysql_store_result failed: %s\n",
328 field_num = mysql_num_fields (res);
329 while ((row = mysql_fetch_row (res)))
332 unsigned long long val;
335 val = atoll (row[1]);
340 if (strncmp (key, "Com_", 4) == 0)
342 /* Ignore `prepared statements' */
343 if (strncmp (key, "Com_stmt_", 9) != 0)
344 commands_submit (key + 4, val);
346 else if (strncmp (key, "Handler_", 8) == 0)
348 handler_submit (key + 8, val);
350 else if (strncmp (key, "Bytes_", 6) == 0)
352 if (strcmp (key, "Bytes_received") == 0)
353 traffic_incoming += val;
354 else if (strcmp (key, "Bytes_sent") == 0)
355 traffic_outgoing += val;
357 else if (strncmp (key, "Threads_", 8) == 0)
359 if (strcmp (key, "Threads_running") == 0)
360 threads_running = (int) val;
361 else if (strcmp (key, "Threads_connected") == 0)
362 threads_connected = (int) val;
363 else if (strcmp (key, "Threads_cached") == 0)
364 threads_cached = (int) val;
367 mysql_free_result (res); res = NULL;
369 threads_submit (threads_running, threads_connected, threads_cached);
370 traffic_submit (traffic_incoming, traffic_outgoing);
372 /* mysql_close (con); */
377 # define mysql_read NULL
378 #endif /* MYSQL_HAVE_READ */
380 void module_register (void)
382 plugin_register (MODULE_NAME, init, mysql_read, NULL);
383 plugin_register ("mysql_commands", NULL, NULL, commands_write);
384 plugin_register ("mysql_handler", NULL, NULL, handler_write);
385 plugin_register ("mysql_threads", NULL, NULL, threads_write);
386 plugin_register ("mysql_traffic", NULL, NULL, traffic_write);
387 cf_register (MODULE_NAME, config, config_keys, config_keys_num);