1 /**
2 * collectd - src/mysql.c
3 * Copyright (C) 2005 Florian octo Forster
4 *
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.
9 *
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.
14 *
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
18 *
19 * Authors:
20 * Florian octo Forster <octo at verplant.org>
21 **/
23 #include "collectd.h"
24 #include "common.h"
25 #include "plugin.h"
26 #include "configfile.h"
28 #ifdef HAVE_MYSQL_MYSQL_H
29 #include <mysql/mysql.h>
30 #endif
32 #define MODULE_NAME "mysql"
34 #if COLLECT_LIBMYSQL
35 # define MYSQL_HAVE_READ 1
36 #else
37 # define MYSQL_HAVE_READ 0
38 #endif
40 #define BUFSIZE 512
42 static char *host = "localhost";
43 static char *user;
44 static char *pass;
45 static char *db = NULL;
47 #if MYSQL_HAVE_READ
48 static char init_suceeded = 0;
49 #endif
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[] =
56 {
57 "DS:value:COUNTER:25:0:U",
58 NULL
59 };
60 static int commands_ds_num = 1;
62 static char *handler_ds_def[] =
63 {
64 "DS:value:COUNTER:25:0:U",
65 NULL
66 };
67 static int handler_ds_num = 1;
69 static char *traffic_ds_def[] =
70 {
71 "DS:incoming:COUNTER:25:0:U",
72 "DS:outgoing:COUNTER:25:0:U",
73 NULL
74 };
75 static int traffic_ds_num = 2;
77 static char *config_keys[] =
78 {
79 "Host",
80 "User",
81 "Password",
82 "Database",
83 NULL
84 };
85 static int config_keys_num = 4;
87 #if MYSQL_HAVE_READ
88 static MYSQL *getconnection (void)
89 {
90 static MYSQL *con;
91 static int state;
93 if (state != 0)
94 {
95 int err;
96 if ((err = mysql_ping (con)) != 0)
97 {
98 syslog (LOG_WARNING, "mysql_ping failed: %s", mysql_error (con));
99 state = 0;
100 }
101 else
102 {
103 state = 1;
104 return (con);
105 }
106 }
108 if ((con = mysql_init (con)) == NULL)
109 {
110 syslog (LOG_ERR, "mysql_init failed: %s", mysql_error (con));
111 state = 0;
112 return (NULL);
113 }
115 if (mysql_real_connect (con, host, user, pass, db, 0, NULL, 0) == NULL)
116 {
117 syslog (LOG_ERR, "mysql_real_connect failed: %s", mysql_error (con));
118 state = 0;
119 return (NULL);
120 }
121 else
122 {
123 state = 1;
124 return (con);
125 }
126 } /* static MYSQL *getconnection (void) */
127 #endif /* MYSQL_HAVE_READ */
129 static void init (void)
130 {
131 #if MYSQL_HAVE_READ
132 if (getconnection () != NULL)
133 init_suceeded = 1;
134 else
135 {
136 syslog (LOG_ERR, "The `mysql' plugin will be disabled because `init' failed to connect to `%s'", host);
137 init_suceeded = 0;
138 }
139 #endif /* MYSQL_HAVE_READ */
141 return;
142 }
144 static int config (char *key, char *value)
145 {
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);
154 else
155 return (-1);
156 }
158 static void commands_write (char *host, char *inst, char *val)
159 {
160 char buf[BUFSIZE];
162 if (snprintf (buf, BUFSIZE, commands_file, inst) >= BUFSIZE)
163 return;
165 rrd_update_file (host, buf, val, commands_ds_def, commands_ds_num);
166 }
168 static void handler_write (char *host, char *inst, char *val)
169 {
170 char buf[BUFSIZE];
172 if (snprintf (buf, BUFSIZE, handler_file, inst) >= BUFSIZE)
173 return;
175 rrd_update_file (host, buf, val, handler_ds_def, handler_ds_num);
176 }
178 static void traffic_write (char *host, char *inst, char *val)
179 {
180 rrd_update_file (host, traffic_file, val, traffic_ds_def, traffic_ds_num);
181 }
183 #if MYSQL_HAVE_READ
184 static void commands_submit (char *inst, unsigned long long value)
185 {
186 char buf[BUFSIZE];
187 int status;
189 status = snprintf (buf, BUFSIZE, "%u:%llu", (unsigned int) curtime, value);
191 if (status < 0)
192 {
193 syslog (LOG_ERR, "snprintf failed");
194 return;
195 }
196 else if (status >= BUFSIZE)
197 {
198 syslog (LOG_WARNING, "snprintf was truncated");
199 return;
200 }
202 plugin_submit ("mysql_commands", inst, buf);
203 }
205 static void handler_submit (char *inst, unsigned long long value)
206 {
207 char buf[BUFSIZE];
208 int status;
210 status = snprintf (buf, BUFSIZE, "%u:%llu", (unsigned int) curtime, value);
212 if (status < 0)
213 {
214 syslog (LOG_ERR, "snprintf failed");
215 return;
216 }
217 else if (status >= BUFSIZE)
218 {
219 syslog (LOG_WARNING, "snprintf was truncated");
220 return;
221 }
223 plugin_submit ("mysql_handler", inst, buf);
224 }
226 static void traffic_submit (unsigned long long incoming,
227 unsigned long long outgoing)
228 {
229 char buf[BUFSIZE];
230 int status;
232 status = snprintf (buf, BUFSIZE, "%u:%llu:%llu", (unsigned int) curtime,
233 incoming, outgoing);
235 if (status < 0)
236 {
237 syslog (LOG_ERR, "snprintf failed");
238 return;
239 }
240 else if (status >= BUFSIZE)
241 {
242 syslog (LOG_WARNING, "snprintf was truncated");
243 return;
244 }
246 plugin_submit ("mysql_traffic", "-", buf);
247 }
249 static void mysql_read (void)
250 {
251 MYSQL *con;
252 MYSQL_RES *res;
253 MYSQL_ROW row;
254 char *query;
255 int query_len;
256 int field_num;
258 unsigned long long traffic_incoming = 0LL;
259 unsigned long long traffic_outgoing = 0LL;
261 if (init_suceeded == 0)
262 return;
264 /* An error message will have been printed in this case */
265 if ((con = getconnection ()) == NULL)
266 return;
268 query = "SHOW STATUS";
269 query_len = strlen (query);
271 if (mysql_real_query (con, query, query_len))
272 {
273 syslog (LOG_ERR, "mysql_real_query failed: %s\n", mysql_error (con));
274 return;
275 }
277 if ((res = mysql_store_result (con)) == NULL)
278 {
279 syslog (LOG_ERR, "mysql_store_result failed: %s\n", mysql_error (con));
280 return;
281 }
283 field_num = mysql_num_fields (res);
284 while ((row = mysql_fetch_row (res)))
285 {
286 char *key;
287 unsigned long long val;
289 key = row[0];
290 val = atoll (row[1]);
292 if (val == 0ULL)
293 continue;
295 if (strncmp (key, "Com_", 4) == 0)
296 {
297 /* Ignore `prepared statements' */
298 if (strncmp (key, "Com_stmt_", 9) != 0)
299 commands_submit (key + 4, val);
300 }
301 else if (strncmp (key, "Handler_", 8) == 0)
302 {
303 handler_submit (key + 8, val);
304 }
305 else if (strncmp (key, "Bytes_", 6) == 0)
306 {
307 if (strcmp (key, "Bytes_received") == 0)
308 traffic_incoming += val;
309 else if (strcmp (key, "Bytes_sent") == 0)
310 traffic_outgoing += val;
311 }
312 }
313 mysql_free_result (res); res = NULL;
315 traffic_submit (traffic_incoming, traffic_outgoing);
317 /* mysql_close (con); */
319 return;
320 }
321 #else
322 # define mysql_read NULL
323 #endif /* MYSQL_HAVE_READ */
325 void module_register (void)
326 {
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);
332 }
334 #undef BUFSIZE
335 #undef MODULE_NAME