mysql plugin: use plugin_register_complex_read
[collectd.git] / src / mysql.c
1 /**
2  * collectd - src/mysql.c
3  * Copyright (C) 2006,2007  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; only version 2 of the License is applicable.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
17  *
18  * Authors:
19  *   Florian octo Forster <octo at verplant.org>
20  **/
21
22 #include "collectd.h"
23 #include "common.h"
24 #include "plugin.h"
25 #include "configfile.h"
26
27 #ifdef HAVE_MYSQL_H
28 #include <mysql.h>
29 #elif defined(HAVE_MYSQL_MYSQL_H)
30 #include <mysql/mysql.h>
31 #endif
32
33 /* TODO: Understand `Select_*' and possibly do that stuff as well.. */
34
35 struct mysql_database_s /* {{{ */
36 {
37         /* instance == NULL  =>  legacy mode */
38         char *instance;
39         char *host;
40         char *user;
41         char *pass;
42         char *database;
43         char *socket;
44         int   port;
45
46         MYSQL *con;
47         int    state;
48 };
49 typedef struct mysql_database_s mysql_database_t; /* }}} */
50
51 static int mysql_read (user_data_t *ud);
52
53 static void mysql_database_free (void *arg) /* {{{ */
54 {
55         mysql_database_t *db;
56
57         DEBUG ("mysql plugin: mysql_database_free (arg = %p);", arg);
58
59         db = (mysql_database_t *) arg;
60
61         if (db == NULL)
62                 return;
63
64         if (db->con != NULL)
65                 mysql_close (db->con);
66
67         sfree (db->host);
68         sfree (db->user);
69         sfree (db->pass);
70         sfree (db->socket);
71         sfree (db->instance);
72         sfree (db->database);
73         sfree (db);
74 } /* }}} void mysql_database_free */
75
76 /* Configuration handling functions {{{
77  *
78  * <Plugin mysql>
79  *   <Database "plugin_instance1">
80  *     Host "localhost"
81  *     Port 22000
82  *     ...
83  *   </Database>
84  * </Plugin>
85  */
86
87 static int mysql_config_set_string (char **ret_string, /* {{{ */
88                                     oconfig_item_t *ci)
89 {
90         char *string;
91
92         if ((ci->values_num != 1)
93             || (ci->values[0].type != OCONFIG_TYPE_STRING))
94         {
95                 WARNING ("mysql plugin: The `%s' config option "
96                          "needs exactly one string argument.", ci->key);
97                 return (-1);
98         }
99
100         string = strdup (ci->values[0].value.string);
101         if (string == NULL)
102         {
103                 ERROR ("mysql plugin: strdup failed.");
104                 return (-1);
105         }
106
107         if (*ret_string != NULL)
108                 free (*ret_string);
109         *ret_string = string;
110
111         return (0);
112 } /* }}} int mysql_config_set_string */
113
114 static int mysql_config_set_int (int *ret_int, /* {{{ */
115                                  oconfig_item_t *ci)
116 {
117         if ((ci->values_num != 1)
118             || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
119         {
120                 WARNING ("mysql plugin: The `%s' config option "
121                          "needs exactly one string argument.", ci->key);
122                 return (-1);
123         }
124
125         *ret_int = ci->values[0].value.number;
126
127         return (0);
128 } /* }}} int mysql_config_set_int */
129
130 static int mysql_config (oconfig_item_t *ci) /* {{{ */
131 {
132         mysql_database_t *db;
133         int plugin_block;
134         int status = 0;
135         int i;
136
137         if ((ci->values_num != 1)
138             || (ci->values[0].type != OCONFIG_TYPE_STRING))
139         {
140                 WARNING ("mysql plugin: The `Database' block "
141                          "needs exactly one string argument.");
142                 return (-1);
143         }
144
145         db = (mysql_database_t *) malloc (sizeof (*db));
146         if (db == NULL)
147         {
148                 ERROR ("mysql plugin: malloc failed.");
149                 return (-1);
150         }
151         memset (db, 0, sizeof (*db));
152
153         /* initialize all the pointers */
154         db->host     = NULL;
155         db->user     = NULL;
156         db->pass     = NULL;
157         db->database = NULL;
158         db->socket   = NULL;
159         db->con      = NULL;
160
161         plugin_block = 1;
162         if (strcasecmp ("Plugin", ci->key) == 0)
163         {
164                 db->instance = NULL;
165         }
166         else if (strcasecmp ("Database", ci->key) == 0)
167         {
168                 plugin_block = 0;
169                 status = mysql_config_set_string (&db->instance, ci);
170                 if (status != 0)
171                 {
172                         sfree (db);
173                         return (status);
174                 }
175                 assert (db->instance != NULL);
176                 db->database = strdup (db->instance);
177         }
178         else
179         {
180                 ERROR ("mysql plugin: mysql_config: "
181                                 "Invalid key: %s", ci->key);
182                 return (-1);
183         }
184
185         /* Fill the `mysql_database_t' structure.. */
186         for (i = 0; i < ci->children_num; i++)
187         {
188                 oconfig_item_t *child = ci->children + i;
189
190                 if (strcasecmp ("Host", child->key) == 0)
191                         status = mysql_config_set_string (&db->host, child);
192                 else if (strcasecmp ("User", child->key) == 0)
193                         status = mysql_config_set_string (&db->user, child);
194                 else if (strcasecmp ("Password", child->key) == 0)
195                         status = mysql_config_set_string (&db->pass, child);
196                 else if (strcasecmp ("Port", child->key) == 0)
197                         status = mysql_config_set_int (&db->port, child);
198                 else if (strcasecmp ("Socket", child->key) == 0)
199                         status = mysql_config_set_string (&db->socket, child);
200                 /* Check if we're currently handling the `Plugin' block. If so,
201                  * handle `Database' _blocks_, too. */
202                 else if ((plugin_block != 0)
203                                 && (strcasecmp ("Database", child->key) == 0)
204                                 && (child->children != NULL))
205                 {
206                         /* If `plugin_block > 1', there has been at least one
207                          * `Database' block */
208                         plugin_block++;
209                         status = mysql_config (child);
210                 }
211                 /* Now handle ordinary `Database' options (without children) */
212                 else if ((strcasecmp ("Database", child->key) == 0)
213                                 && (child->children == NULL))
214                         status = mysql_config_set_string (&db->database, child);
215                 else
216                 {
217                         WARNING ("mysql plugin: Option `%s' not allowed here.", child->key);
218                         status = -1;
219                 }
220
221                 if (status != 0)
222                         break;
223         }
224
225         /* Check if there were any `Database' blocks. */
226         if (plugin_block > 1)
227         {
228                 /* There were connection blocks. Don't use any legacy stuff. */
229                 if ((db->host != NULL)
230                         || (db->user != NULL)
231                         || (db->pass != NULL)
232                         || (db->database != NULL)
233                         || (db->socket != NULL)
234                         || (db->port != 0))
235                 {
236                         WARNING ("mysql plugin: At least one <Database> "
237                                         "block has been found. The legacy "
238                                         "configuration will be ignored.");
239                 }
240                 mysql_database_free (db);
241                 return (0);
242         }
243         else if (plugin_block != 0)
244         {
245                 WARNING ("mysql plugin: You're using the legacy "
246                                 "configuration options. Please consider "
247                                 "updating your configuration!");
248         }
249
250         /* Check that all necessary options have been given. */
251         while (status == 0)
252         {
253                 /* Zero is allowed and automatically handled by
254                  * `mysql_real_connect'. */
255                 if ((db->port < 0) || (db->port > 65535))
256                 {
257                         ERROR ("mysql plugin: Database %s: Port number out "
258                                         "of range: %i",
259                                         (db->instance != NULL)
260                                         ? db->instance
261                                         : "<legacy>",
262                                         db->port);
263                         status = -1;
264                 }
265                 if (db->database == NULL)
266                 {
267                         ERROR ("mysql plugin: No `Database' configured");
268                         status = -1;
269                 }
270                 break;
271         } /* while (status == 0) */
272
273         /* If all went well, register this database for reading */
274         if (status == 0)
275         {
276                 user_data_t ud;
277
278                 DEBUG ("mysql plugin: Registering new read callback: %s", db->database);
279
280                 memset (&ud, 0, sizeof (ud));
281                 ud.data = (void *) db;
282                 ud.free_func = mysql_database_free;
283
284                 plugin_register_complex_read (db->database, mysql_read,
285                                               /* interval = */ NULL, &ud);
286         }
287         else
288         {
289                 mysql_database_free (db);
290                 return (-1);
291         }
292
293         return (0);
294 } /* }}} int mysql_config */
295
296 /* }}} End of configuration handling functions */
297
298 static MYSQL *getconnection (mysql_database_t *db)
299 {
300         if (db->state != 0)
301         {
302                 int err;
303                 if ((err = mysql_ping (db->con)) != 0)
304                 {
305                         WARNING ("mysql_ping failed: %s", mysql_error (db->con));
306                         db->state = 0;
307                 }
308                 else
309                 {
310                         db->state = 1;
311                         return (db->con);
312                 }
313         }
314
315         if ((db->con = mysql_init (db->con)) == NULL)
316         {
317                 ERROR ("mysql_init failed: %s", mysql_error (db->con));
318                 db->state = 0;
319                 return (NULL);
320         }
321
322         if (mysql_real_connect (db->con, db->host, db->user, db->pass,
323                                 db->database, db->port, db->socket, 0) == NULL)
324         {
325                 ERROR ("mysql_real_connect failed: %s", mysql_error (db->con));
326                 db->state = 0;
327                 return (NULL);
328         }
329         else
330         {
331                 db->state = 1;
332                 return (db->con);
333         }
334 } /* static MYSQL *getconnection (mysql_database_t *db) */
335
336 static void set_host (mysql_database_t *db, value_list_t *vl)
337 {
338         /* XXX legacy mode - use hostname_g */
339         if (db->instance == NULL)
340                 sstrncpy (vl->host, hostname_g, sizeof (vl->host));
341         else
342         {
343                 if ((db->host == NULL)
344                                 || (strcmp ("", db->host) == 0)
345                                 || (strcmp ("localhost", db->host) == 0))
346                         sstrncpy (vl->host, hostname_g, sizeof (vl->host));
347                 else
348                         sstrncpy (vl->host, db->host, sizeof (vl->host));
349         }
350 }
351
352 static void set_plugin_instance (mysql_database_t *db, value_list_t *vl)
353 {
354         /* XXX legacy mode - no plugin_instance */
355         if (db->instance == NULL)
356                 sstrncpy (vl->plugin_instance, "",
357                                 sizeof (vl->plugin_instance));
358         else
359                 sstrncpy (vl->plugin_instance, db->instance,
360                                 sizeof (vl->plugin_instance));
361 }
362
363 static void counter_submit (const char *type, const char *type_instance,
364                 counter_t value, mysql_database_t *db)
365 {
366         value_t values[1];
367         value_list_t vl = VALUE_LIST_INIT;
368
369         values[0].counter = value;
370
371         vl.values = values;
372         vl.values_len = 1;
373         set_host (db, &vl);
374         sstrncpy (vl.plugin, "mysql", sizeof (vl.plugin));
375         sstrncpy (vl.type, type, sizeof (vl.type));
376         sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
377         set_plugin_instance (db, &vl);
378
379         plugin_dispatch_values (&vl);
380 } /* void counter_submit */
381
382 static void qcache_submit (counter_t hits, counter_t inserts,
383                 counter_t not_cached, counter_t lowmem_prunes,
384                 gauge_t queries_in_cache, mysql_database_t *db)
385 {
386         value_t values[5];
387         value_list_t vl = VALUE_LIST_INIT;
388
389         values[0].counter = hits;
390         values[1].counter = inserts;
391         values[2].counter = not_cached;
392         values[3].counter = lowmem_prunes;
393         values[4].gauge   = queries_in_cache;
394
395         vl.values = values;
396         vl.values_len = 5;
397         set_host (db, &vl);
398         sstrncpy (vl.plugin, "mysql", sizeof (vl.plugin));
399         sstrncpy (vl.type, "mysql_qcache", sizeof (vl.type));
400         set_plugin_instance (db, &vl);
401
402         plugin_dispatch_values (&vl);
403 } /* void qcache_submit */
404
405 static void threads_submit (gauge_t running, gauge_t connected, gauge_t cached,
406                 counter_t created, mysql_database_t *db)
407 {
408         value_t values[4];
409         value_list_t vl = VALUE_LIST_INIT;
410
411         values[0].gauge   = running;
412         values[1].gauge   = connected;
413         values[2].gauge   = cached;
414         values[3].counter = created;
415
416         vl.values = values;
417         vl.values_len = 4;
418         set_host (db, &vl);
419         sstrncpy (vl.plugin, "mysql", sizeof (vl.plugin));
420         sstrncpy (vl.type, "mysql_threads", sizeof (vl.type));
421         set_plugin_instance (db, &vl);
422
423         plugin_dispatch_values (&vl);
424 } /* void threads_submit */
425
426 static void traffic_submit (counter_t rx, counter_t tx, mysql_database_t *db)
427 {
428         value_t values[2];
429         value_list_t vl = VALUE_LIST_INIT;
430
431         values[0].counter = rx;
432         values[1].counter = tx;
433
434         vl.values = values;
435         vl.values_len = 2;
436         set_host (db, &vl);
437         sstrncpy (vl.plugin, "mysql", sizeof (vl.plugin));
438         sstrncpy (vl.type, "mysql_octets", sizeof (vl.type));
439         set_plugin_instance (db, &vl);
440
441         plugin_dispatch_values (&vl);
442 } /* void traffic_submit */
443
444 static int mysql_read (user_data_t *ud)
445 {
446         mysql_database_t *db;
447         MYSQL     *con;
448         MYSQL_RES *res;
449         MYSQL_ROW  row;
450         char      *query;
451         int        query_len;
452         int        field_num;
453
454         unsigned long long qcache_hits          = 0ULL;
455         unsigned long long qcache_inserts       = 0ULL;
456         unsigned long long qcache_not_cached    = 0ULL;
457         unsigned long long qcache_lowmem_prunes = 0ULL;
458         int qcache_queries_in_cache = -1;
459
460         int threads_running   = -1;
461         int threads_connected = -1;
462         int threads_cached    = -1;
463         unsigned long long threads_created = 0ULL;
464
465         unsigned long long traffic_incoming = 0ULL;
466         unsigned long long traffic_outgoing = 0ULL;
467
468         if ((ud == NULL) || (ud->data == NULL))
469         {
470                 ERROR ("mysql plugin: mysql_database_read: Invalid user data.");
471                 return (-1);
472         }
473
474         db = (mysql_database_t *) ud->data;
475
476         /* An error message will have been printed in this case */
477         if ((con = getconnection (db)) == NULL)
478                 return (-1);
479
480         query = "SHOW STATUS";
481         if (mysql_get_server_version (con) >= 50002)
482                 query = "SHOW GLOBAL STATUS";
483
484         query_len = strlen (query);
485
486         if (mysql_real_query (con, query, query_len))
487         {
488                 ERROR ("mysql_real_query failed: %s\n",
489                                 mysql_error (con));
490                 return (-1);
491         }
492
493         if ((res = mysql_store_result (con)) == NULL)
494         {
495                 ERROR ("mysql_store_result failed: %s\n",
496                                 mysql_error (con));
497                 return (-1);
498         }
499
500         field_num = mysql_num_fields (res);
501         while ((row = mysql_fetch_row (res)))
502         {
503                 char *key;
504                 unsigned long long val;
505
506                 key = row[0];
507                 val = atoll (row[1]);
508
509                 if (strncmp (key, "Com_", 4) == 0)
510                 {
511                         if (val == 0ULL)
512                                 continue;
513
514                         /* Ignore `prepared statements' */
515                         if (strncmp (key, "Com_stmt_", 9) != 0)
516                                 counter_submit ("mysql_commands", key + 4, val, db);
517                 }
518                 else if (strncmp (key, "Handler_", 8) == 0)
519                 {
520                         if (val == 0ULL)
521                                 continue;
522
523                         counter_submit ("mysql_handler", key + 8, val, db);
524                 }
525                 else if (strncmp (key, "Qcache_", 7) == 0)
526                 {
527                         if (strcmp (key, "Qcache_hits") == 0)
528                                 qcache_hits = val;
529                         else if (strcmp (key, "Qcache_inserts") == 0)
530                                 qcache_inserts = val;
531                         else if (strcmp (key, "Qcache_not_cached") == 0)
532                                 qcache_not_cached = val;
533                         else if (strcmp (key, "Qcache_lowmem_prunes") == 0)
534                                 qcache_lowmem_prunes = val;
535                         else if (strcmp (key, "Qcache_queries_in_cache") == 0)
536                                 qcache_queries_in_cache = (int) val;
537                 }
538                 else if (strncmp (key, "Bytes_", 6) == 0)
539                 {
540                         if (strcmp (key, "Bytes_received") == 0)
541                                 traffic_incoming += val;
542                         else if (strcmp (key, "Bytes_sent") == 0)
543                                 traffic_outgoing += val;
544                 }
545                 else if (strncmp (key, "Threads_", 8) == 0)
546                 {
547                         if (strcmp (key, "Threads_running") == 0)
548                                 threads_running = (int) val;
549                         else if (strcmp (key, "Threads_connected") == 0)
550                                 threads_connected = (int) val;
551                         else if (strcmp (key, "Threads_cached") == 0)
552                                 threads_cached = (int) val;
553                         else if (strcmp (key, "Threads_created") == 0)
554                                 threads_created = val;
555                 }
556         }
557         mysql_free_result (res); res = NULL;
558
559         if ((qcache_hits != 0ULL)
560                         || (qcache_inserts != 0ULL)
561                         || (qcache_not_cached != 0ULL)
562                         || (qcache_lowmem_prunes != 0ULL))
563                 qcache_submit (qcache_hits, qcache_inserts, qcache_not_cached,
564                                qcache_lowmem_prunes, qcache_queries_in_cache, db);
565
566         if (threads_created != 0ULL)
567                 threads_submit (threads_running, threads_connected,
568                                 threads_cached, threads_created, db);
569
570         traffic_submit  (traffic_incoming, traffic_outgoing, db);
571
572         return (0);
573 } /* int mysql_read */
574
575 void module_register (void)
576 {
577         plugin_register_complex_config ("mysql", mysql_config);
578 } /* void module_register */