Added `threads' to the MySQL plugin
[collectd.git] / src / mysql.c
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  **/
22
23 #include "collectd.h"
24 #include "common.h"
25 #include "plugin.h"
26 #include "configfile.h"
27
28 #ifdef HAVE_MYSQL_MYSQL_H
29 #include <mysql/mysql.h>
30 #endif
31
32 #define MODULE_NAME "mysql"
33
34 #if COLLECT_LIBMYSQL
35 # define MYSQL_HAVE_READ 1
36 #else
37 # define MYSQL_HAVE_READ 0
38 #endif
39
40 #define BUFSIZE 512
41
42 static char *host = "localhost";
43 static char *user;
44 static char *pass;
45 static char *db = NULL;
46
47 #if MYSQL_HAVE_READ
48 static char  init_suceeded = 0;
49 #endif
50
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";
55
56 static char *commands_ds_def[] =
57 {
58         "DS:value:COUNTER:25:0:U",
59         NULL
60 };
61 static int commands_ds_num = 1;
62
63 static char *handler_ds_def[] =
64 {
65         "DS:value:COUNTER:25:0:U",
66         NULL
67 };
68 static int handler_ds_num = 1;
69
70 static char *threads_ds_def[] =
71 {
72         "DS:running:GAUGE:25:0:U",
73         "DS:connected:GAUGE:25:0:U",
74         "DS:cached:GAUGE:25:0:U",
75         NULL
76 };
77 static int threads_ds_num = 3;
78
79 static char *traffic_ds_def[] =
80 {
81         "DS:incoming:COUNTER:25:0:U",
82         "DS:outgoing:COUNTER:25:0:U",
83         NULL
84 };
85 static int traffic_ds_num = 2;
86
87 static char *config_keys[] =
88 {
89         "Host",
90         "User",
91         "Password",
92         "Database",
93         NULL
94 };
95 static int config_keys_num = 4;
96
97 #if MYSQL_HAVE_READ
98 static MYSQL *getconnection (void)
99 {
100         static MYSQL *con;
101         static int    state;
102
103         if (state != 0)
104         {
105                 int err;
106                 if ((err = mysql_ping (con)) != 0)
107                 {
108                         syslog (LOG_WARNING, "mysql_ping failed: %s", mysql_error (con));
109                         state = 0;
110                 }
111                 else
112                 {
113                         state = 1;
114                         return (con);
115                 }
116         }
117
118         if ((con = mysql_init (con)) == NULL)
119         {
120                 syslog (LOG_ERR, "mysql_init failed: %s", mysql_error (con));
121                 state = 0;
122                 return (NULL);
123         }
124
125         if (mysql_real_connect (con, host, user, pass, db, 0, NULL, 0) == NULL)
126         {
127                 syslog (LOG_ERR, "mysql_real_connect failed: %s", mysql_error (con));
128                 state = 0;
129                 return (NULL);
130         }
131         else
132         {
133                 state = 1;
134                 return (con);
135         }
136 } /* static MYSQL *getconnection (void) */
137 #endif /* MYSQL_HAVE_READ */
138
139 static void init (void)
140 {
141 #if MYSQL_HAVE_READ
142         if (getconnection () != NULL)
143                 init_suceeded = 1;
144         else
145         {
146                 syslog (LOG_ERR, "The `mysql' plugin will be disabled because `init' failed to connect to `%s'", host);
147                 init_suceeded = 0;
148         }
149 #endif /* MYSQL_HAVE_READ */
150
151         return;
152 }
153
154 static int config (char *key, char *value)
155 {
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);
164         else
165                 return (-1);
166 }
167
168 static void commands_write (char *host, char *inst, char *val)
169 {
170         char buf[BUFSIZE];
171
172         if (snprintf (buf, BUFSIZE, commands_file, inst) >= BUFSIZE)
173                 return;
174
175         rrd_update_file (host, buf, val, commands_ds_def, commands_ds_num);
176 }
177
178 static void handler_write (char *host, char *inst, char *val)
179 {
180         char buf[BUFSIZE];
181
182         if (snprintf (buf, BUFSIZE, handler_file, inst) >= BUFSIZE)
183                 return;
184
185         rrd_update_file (host, buf, val, handler_ds_def, handler_ds_num);
186 }
187
188 static void threads_write (char *host, char *inst, char *val)
189 {
190         rrd_update_file (host, threads_file, val,
191                         threads_ds_def, threads_ds_num);
192 }
193
194 static void traffic_write (char *host, char *inst, char *val)
195 {
196         rrd_update_file (host, traffic_file, val,
197                         traffic_ds_def, traffic_ds_num);
198 }
199
200 #if MYSQL_HAVE_READ
201 static void commands_submit (char *inst, unsigned long long value)
202 {
203         char buf[BUFSIZE];
204         int  status;
205
206         status = snprintf (buf, BUFSIZE, "%u:%llu", (unsigned int) curtime, value);
207
208         if (status < 0)
209         {
210                 syslog (LOG_ERR, "snprintf failed");
211                 return;
212         }
213         else if (status >= BUFSIZE)
214         {
215                 syslog (LOG_WARNING, "snprintf was truncated");
216                 return;
217         }
218
219         plugin_submit ("mysql_commands", inst, buf);
220 }
221
222 static void handler_submit (char *inst, unsigned long long value)
223 {
224         char buf[BUFSIZE];
225         int  status;
226
227         status = snprintf (buf, BUFSIZE, "%u:%llu", (unsigned int) curtime, value);
228
229         if (status < 0)
230         {
231                 syslog (LOG_ERR, "snprintf failed");
232                 return;
233         }
234         else if (status >= BUFSIZE)
235         {
236                 syslog (LOG_WARNING, "snprintf was truncated");
237                 return;
238         }
239
240         plugin_submit ("mysql_handler", inst, buf);
241 }
242
243 static void threads_submit (int running, int connected, int cached)
244 {
245         char buf[BUFSIZE];
246         int  status;
247
248         status = snprintf (buf, BUFSIZE, "%u:%i:%i:%i", (unsigned int) curtime,
249                         running, connected, cached);
250
251         if (status < 0)
252         {
253                 syslog (LOG_ERR, "snprintf failed");
254                 return;
255         }
256         else if (status >= BUFSIZE)
257         {
258                 syslog (LOG_WARNING, "snprintf was truncated");
259                 return;
260         }
261
262         plugin_submit ("mysql_threads", "-", buf);
263 }
264
265 static void traffic_submit (unsigned long long incoming,
266                 unsigned long long outgoing)
267 {
268         char buf[BUFSIZE];
269         int  status;
270
271         status = snprintf (buf, BUFSIZE, "%u:%llu:%llu", (unsigned int) curtime,
272                         incoming, outgoing);
273
274         if (status < 0)
275         {
276                 syslog (LOG_ERR, "snprintf failed");
277                 return;
278         }
279         else if (status >= BUFSIZE)
280         {
281                 syslog (LOG_WARNING, "snprintf was truncated");
282                 return;
283         }
284
285         plugin_submit ("mysql_traffic", "-", buf);
286 }
287
288 static void mysql_read (void)
289 {
290         MYSQL     *con;
291         MYSQL_RES *res;
292         MYSQL_ROW  row;
293         char      *query;
294         int        query_len;
295         int        field_num;
296
297         int threads_running   = -1;
298         int threads_connected = -1;
299         int threads_cached    = -1;
300
301         unsigned long long traffic_incoming = 0LL;
302         unsigned long long traffic_outgoing = 0LL;
303
304         if (init_suceeded == 0)
305                 return;
306
307         /* An error message will have been printed in this case */
308         if ((con = getconnection ()) == NULL)
309                 return;
310
311         query = "SHOW STATUS";
312         query_len = strlen (query);
313
314         if (mysql_real_query (con, query, query_len))
315         {
316                 syslog (LOG_ERR, "mysql_real_query failed: %s\n",
317                                 mysql_error (con));
318                 return;
319         }
320
321         if ((res = mysql_store_result (con)) == NULL)
322         {
323                 syslog (LOG_ERR, "mysql_store_result failed: %s\n",
324                                 mysql_error (con));
325                 return;
326         }
327
328         field_num = mysql_num_fields (res);
329         while ((row = mysql_fetch_row (res)))
330         {
331                 char *key;
332                 unsigned long long val;
333
334                 key = row[0];
335                 val = atoll (row[1]);
336
337                 if (val == 0ULL)
338                         continue;
339
340                 if (strncmp (key, "Com_", 4) == 0)
341                 {
342                         /* Ignore `prepared statements' */
343                         if (strncmp (key, "Com_stmt_", 9) != 0)
344                                 commands_submit (key + 4, val);
345                 }
346                 else if (strncmp (key, "Handler_", 8) == 0)
347                 {
348                         handler_submit (key + 8, val);
349                 }
350                 else if (strncmp (key, "Bytes_", 6) == 0)
351                 {
352                         if (strcmp (key, "Bytes_received") == 0)
353                                 traffic_incoming += val;
354                         else if (strcmp (key, "Bytes_sent") == 0)
355                                 traffic_outgoing += val;
356                 }
357                 else if (strncmp (key, "Threads_", 8) == 0)
358                 {
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;
365                 }
366         }
367         mysql_free_result (res); res = NULL;
368
369         threads_submit  (threads_running, threads_connected, threads_cached);
370         traffic_submit  (traffic_incoming, traffic_outgoing);
371
372         /* mysql_close (con); */
373
374         return;
375 }
376 #else
377 # define mysql_read NULL
378 #endif /* MYSQL_HAVE_READ */
379
380 void module_register (void)
381 {
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);
388 }
389
390 #undef BUFSIZE
391 #undef MODULE_NAME