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 *traffic_file = "traffic-mysql.rrd";
55 static char *commands_ds_def[] =
57 "DS:value:COUNTER:25:0:U",
60 static int commands_ds_num = 1;
62 static char *handler_ds_def[] =
64 "DS:value:COUNTER:25:0:U",
67 static int handler_ds_num = 1;
69 static char *traffic_ds_def[] =
71 "DS:incoming:COUNTER:25:0:U",
72 "DS:outgoing:COUNTER:25:0:U",
75 static int traffic_ds_num = 2;
77 static char *config_keys[] =
85 static int config_keys_num = 4;
88 static MYSQL *getconnection (void)
96 if ((err = mysql_ping (con)) != 0)
98 syslog (LOG_WARNING, "mysql_ping failed: %s", mysql_error (con));
108 if ((con = mysql_init (con)) == NULL)
110 syslog (LOG_ERR, "mysql_init failed: %s", mysql_error (con));
115 if (mysql_real_connect (con, host, user, pass, db, 0, NULL, 0) == NULL)
117 syslog (LOG_ERR, "mysql_real_connect failed: %s", mysql_error (con));
126 } /* static MYSQL *getconnection (void) */
127 #endif /* MYSQL_HAVE_READ */
129 static void init (void)
132 if (getconnection () != NULL)
136 syslog (LOG_ERR, "The `mysql' plugin will be disabled because `init' failed to connect to `%s'", host);
139 #endif /* MYSQL_HAVE_READ */
144 static int config (char *key, char *value)
146 if (strcasecmp (key, "host") == 0)
147 return ((host = strdup (value)) == NULL ? 1 : 0);
148 else if (strcasecmp (key, "user") == 0)
149 return ((user = strdup (value)) == NULL ? 1 : 0);
150 else if (strcasecmp (key, "password") == 0)
151 return ((pass = strdup (value)) == NULL ? 1 : 0);
152 else if (strcasecmp (key, "database") == 0)
153 return ((db = strdup (value)) == NULL ? 1 : 0);
158 static void commands_write (char *host, char *inst, char *val)
162 if (snprintf (buf, BUFSIZE, commands_file, inst) >= BUFSIZE)
165 rrd_update_file (host, buf, val, commands_ds_def, commands_ds_num);
168 static void handler_write (char *host, char *inst, char *val)
172 if (snprintf (buf, BUFSIZE, handler_file, inst) >= BUFSIZE)
175 rrd_update_file (host, buf, val, handler_ds_def, handler_ds_num);
178 static void traffic_write (char *host, char *inst, char *val)
180 rrd_update_file (host, traffic_file, val, traffic_ds_def, traffic_ds_num);
184 static void commands_submit (char *inst, unsigned long long value)
189 status = snprintf (buf, BUFSIZE, "%u:%llu", (unsigned int) curtime, value);
193 syslog (LOG_ERR, "snprintf failed");
196 else if (status >= BUFSIZE)
198 syslog (LOG_WARNING, "snprintf was truncated");
202 plugin_submit ("mysql_commands", inst, buf);
205 static void handler_submit (char *inst, unsigned long long value)
210 status = snprintf (buf, BUFSIZE, "%u:%llu", (unsigned int) curtime, value);
214 syslog (LOG_ERR, "snprintf failed");
217 else if (status >= BUFSIZE)
219 syslog (LOG_WARNING, "snprintf was truncated");
223 plugin_submit ("mysql_handler", inst, buf);
226 static void traffic_submit (unsigned long long incoming,
227 unsigned long long outgoing)
232 status = snprintf (buf, BUFSIZE, "%u:%llu:%llu", (unsigned int) curtime,
237 syslog (LOG_ERR, "snprintf failed");
240 else if (status >= BUFSIZE)
242 syslog (LOG_WARNING, "snprintf was truncated");
246 plugin_submit ("mysql_traffic", "-", buf);
249 static void mysql_read (void)
258 unsigned long long traffic_incoming = 0LL;
259 unsigned long long traffic_outgoing = 0LL;
261 if (init_suceeded == 0)
264 /* An error message will have been printed in this case */
265 if ((con = getconnection ()) == NULL)
268 query = "SHOW STATUS";
269 query_len = strlen (query);
271 if (mysql_real_query (con, query, query_len))
273 syslog (LOG_ERR, "mysql_real_query failed: %s\n", mysql_error (con));
277 if ((res = mysql_store_result (con)) == NULL)
279 syslog (LOG_ERR, "mysql_store_result failed: %s\n", mysql_error (con));
283 field_num = mysql_num_fields (res);
284 while ((row = mysql_fetch_row (res)))
287 unsigned long long val;
290 val = atoll (row[1]);
295 if (strncmp (key, "Com_", 4) == 0)
297 /* Ignore `prepared statements' */
298 if (strncmp (key, "Com_stmt_", 9) != 0)
299 commands_submit (key + 4, val);
301 else if (strncmp (key, "Handler_", 8) == 0)
303 handler_submit (key + 8, val);
305 else if (strncmp (key, "Bytes_", 6) == 0)
307 if (strcmp (key, "Bytes_received") == 0)
308 traffic_incoming += val;
309 else if (strcmp (key, "Bytes_sent") == 0)
310 traffic_outgoing += val;
313 mysql_free_result (res); res = NULL;
315 traffic_submit (traffic_incoming, traffic_outgoing);
317 /* mysql_close (con); */
322 # define mysql_read NULL
323 #endif /* MYSQL_HAVE_READ */
325 void module_register (void)
327 plugin_register (MODULE_NAME, init, mysql_read, NULL);
328 plugin_register ("mysql_commands", NULL, NULL, commands_write);
329 plugin_register ("mysql_handler", NULL, NULL, handler_write);
330 plugin_register ("mysql_traffic", NULL, NULL, traffic_write);
331 cf_register (MODULE_NAME, config, config_keys, config_keys_num);