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
45 static char *db = NULL;
47 static char init_suceeded = 0;
49 static char *commands_file = "mysql_commands.rrd";
50 static char *traffic_file = "mysql_traffic.rrd";
52 static char *commands_ds_def[] =
54 "DS:insert:COUNTER:25:0:U",
55 "DS:select:COUNTER:25:0:U",
56 "DS:show:COUNTER:25:0:U",
57 "DS:update:COUNTER:25:0:U",
58 "DS:other:COUNTER:25:0:U",
61 static int commands_ds_num = 5;
63 static char *traffic_ds_def[] =
65 "DS:incoming:COUNTER:25:0:U",
66 "DS:outgoing:COUNTER:25:0:U",
69 static int traffic_ds_num = 2;
71 static char *config_keys[] =
79 static int config_keys_num = 4;
82 static MYSQL *getconnection (void)
90 if ((err = mysql_ping (con)) != 0)
92 syslog (LOG_WARNING, "mysql_ping failed: %s", mysql_error (con));
102 if ((con = mysql_init (con)) == NULL)
104 syslog (LOG_ERR, "mysql_init failed: %s", mysql_error (con));
109 if (mysql_real_connect (con, host, user, pass, db, 0, NULL, 0) == NULL)
111 syslog (LOG_ERR, "mysql_real_connect failed: %s", mysql_error (con));
120 } /* static MYSQL *getconnection (void) */
121 #endif /* MYSQL_HAVE_READ */
123 static void init (void)
126 if (getconnection () != NULL)
130 #endif /* MYSQL_HAVE_READ */
135 static int config (char *key, char *value)
137 if (strcasecmp (key, "host") == 0)
138 return ((host = strdup (value)) == NULL ? 1 : 0);
139 else if (strcasecmp (key, "user") == 0)
140 return ((user = strdup (value)) == NULL ? 1 : 0);
141 else if (strcasecmp (key, "password") == 0)
142 return ((pass = strdup (value)) == NULL ? 1 : 0);
143 else if (strcasecmp (key, "database") == 0)
144 return ((db = strdup (value)) == NULL ? 1 : 0);
149 static void commands_write (char *host, char *inst, char *val)
151 rrd_update_file (host, commands_file, val, commands_ds_def, commands_ds_num);
154 static void traffic_write (char *host, char *inst, char *val)
156 rrd_update_file (host, traffic_file, val, traffic_ds_def, traffic_ds_num);
160 static void commands_submit (unsigned long long insert,
161 unsigned long long select,
162 unsigned long long show,
163 unsigned long long update,
164 unsigned long long other)
169 status = snprintf (buf, BUFSIZE, "%u:%llu:%llu:%llu:%llu:%llu", (unsigned int) curtime,
170 insert, select, show, update, other);
174 syslog (LOG_ERR, "snprintf failed");
177 else if (status >= BUFSIZE)
179 syslog (LOG_WARNING, "snprintf was truncated");
183 plugin_submit ("mysql_commands", "-", buf);
186 static void traffic_submit (unsigned long long incoming,
187 unsigned long long outgoing)
192 status = snprintf (buf, BUFSIZE, "%u:%llu:%llu", (unsigned int) curtime,
197 syslog (LOG_ERR, "snprintf failed");
200 else if (status >= BUFSIZE)
202 syslog (LOG_WARNING, "snprintf was truncated");
206 plugin_submit ("mysql_traffic", "-", buf);
209 static void mysql_read (void)
218 unsigned long long cmd_insert = 0LL;
219 unsigned long long cmd_select = 0LL;
220 unsigned long long cmd_show = 0LL;
221 unsigned long long cmd_update = 0LL;
222 unsigned long long cmd_other = 0LL;
224 unsigned long long traffic_incoming = 0LL;
225 unsigned long long traffic_outgoing = 0LL;
227 if (init_suceeded == 0)
230 /* An error message will have been printed in this case */
231 if ((con = getconnection ()) == NULL)
234 query = "SHOW STATUS";
235 query_len = strlen (query);
237 if (mysql_real_query (con, query, query_len))
239 syslog (LOG_ERR, "mysql_real_query failed: %s\n", mysql_error (con));
243 if ((res = mysql_store_result (con)) == NULL)
245 syslog (LOG_ERR, "mysql_store_result failed: %s\n", mysql_error (con));
249 field_num = mysql_num_fields (res);
250 while ((row = mysql_fetch_row (res)))
253 unsigned long long val;
256 val = atoll (row[1]);
258 if (strncmp (key, "Com_", 4) == 0)
260 if (strncmp (key, "Com_insert", 10) == 0)
262 else if (strncmp (key, "Com_select", 10) == 0)
264 else if (strncmp (key, "Com_show", 8) == 0)
266 else if (strncmp (key, "Com_update", 10) == 0)
268 else if (strncmp (key, "Com_stmt_", 9) == 0)
273 else if (strncmp (key, "Bytes_", 6) == 0)
275 if (strncmp (key, "Bytes_received", 14) == 0)
276 traffic_incoming += val;
277 else if (strncmp (key, "Bytes_sent", 10) == 0)
278 traffic_outgoing += val;
281 mysql_free_result (res); res = NULL;
283 commands_submit (cmd_insert, cmd_select, cmd_show, cmd_update, cmd_other);
284 traffic_submit (traffic_incoming, traffic_outgoing);
286 /* mysql_close (con); */
291 # define mysql_read NULL
292 #endif /* MYSQL_HAVE_READ */
294 void module_register (void)
296 plugin_register (MODULE_NAME, init, mysql_read, NULL);
297 plugin_register ("mysql_commands", NULL, NULL, commands_write);
298 plugin_register ("mysql_traffic", NULL, NULL, traffic_write);
299 cf_register (MODULE_NAME, config, config_keys, config_keys_num);