mysql plugin: Use "mysql-$db" when registering a read callback.
[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                 char cb_name[DATA_MAX_NAME_LEN];
278
279                 DEBUG ("mysql plugin: Registering new read callback: %s", db->database);
280
281                 memset (&ud, 0, sizeof (ud));
282                 ud.data = (void *) db;
283                 ud.free_func = mysql_database_free;
284
285                 if (db->database != NULL)
286                         ssnprintf (cb_name, sizeof (cb_name), "mysql-%s",
287                                         db->database);
288                 else
289                         sstrncpy (cb_name, "mysql", sizeof (cb_name));
290
291                 plugin_register_complex_read (cb_name, mysql_read,
292                                               /* interval = */ NULL, &ud);
293         }
294         else
295         {
296                 mysql_database_free (db);
297                 return (-1);
298         }
299
300         return (0);
301 } /* }}} int mysql_config */
302
303 /* }}} End of configuration handling functions */
304
305 static MYSQL *getconnection (mysql_database_t *db)
306 {
307         if (db->state != 0)
308         {
309                 int err;
310                 if ((err = mysql_ping (db->con)) != 0)
311                 {
312                         WARNING ("mysql_ping failed: %s", mysql_error (db->con));
313                         db->state = 0;
314                 }
315                 else
316                 {
317                         db->state = 1;
318                         return (db->con);
319                 }
320         }
321
322         if ((db->con = mysql_init (db->con)) == NULL)
323         {
324                 ERROR ("mysql_init failed: %s", mysql_error (db->con));
325                 db->state = 0;
326                 return (NULL);
327         }
328
329         if (mysql_real_connect (db->con, db->host, db->user, db->pass,
330                                 db->database, db->port, db->socket, 0) == NULL)
331         {
332                 ERROR ("mysql_real_connect failed: %s", mysql_error (db->con));
333                 db->state = 0;
334                 return (NULL);
335         }
336         else
337         {
338                 db->state = 1;
339                 return (db->con);
340         }
341 } /* static MYSQL *getconnection (mysql_database_t *db) */
342
343 static void set_host (mysql_database_t *db, value_list_t *vl)
344 {
345         /* XXX legacy mode - use hostname_g */
346         if (db->instance == NULL)
347                 sstrncpy (vl->host, hostname_g, sizeof (vl->host));
348         else
349         {
350                 if ((db->host == NULL)
351                                 || (strcmp ("", db->host) == 0)
352                                 || (strcmp ("localhost", db->host) == 0))
353                         sstrncpy (vl->host, hostname_g, sizeof (vl->host));
354                 else
355                         sstrncpy (vl->host, db->host, sizeof (vl->host));
356         }
357 }
358
359 static void set_plugin_instance (mysql_database_t *db, value_list_t *vl)
360 {
361         /* XXX legacy mode - no plugin_instance */
362         if (db->instance == NULL)
363                 sstrncpy (vl->plugin_instance, "",
364                                 sizeof (vl->plugin_instance));
365         else
366                 sstrncpy (vl->plugin_instance, db->instance,
367                                 sizeof (vl->plugin_instance));
368 }
369
370 static void counter_submit (const char *type, const char *type_instance,
371                 counter_t value, mysql_database_t *db)
372 {
373         value_t values[1];
374         value_list_t vl = VALUE_LIST_INIT;
375
376         values[0].counter = value;
377
378         vl.values = values;
379         vl.values_len = 1;
380         set_host (db, &vl);
381         sstrncpy (vl.plugin, "mysql", sizeof (vl.plugin));
382         sstrncpy (vl.type, type, sizeof (vl.type));
383         sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
384         set_plugin_instance (db, &vl);
385
386         plugin_dispatch_values (&vl);
387 } /* void counter_submit */
388
389 static void qcache_submit (counter_t hits, counter_t inserts,
390                 counter_t not_cached, counter_t lowmem_prunes,
391                 gauge_t queries_in_cache, mysql_database_t *db)
392 {
393         value_t values[5];
394         value_list_t vl = VALUE_LIST_INIT;
395
396         values[0].counter = hits;
397         values[1].counter = inserts;
398         values[2].counter = not_cached;
399         values[3].counter = lowmem_prunes;
400         values[4].gauge   = queries_in_cache;
401
402         vl.values = values;
403         vl.values_len = 5;
404         set_host (db, &vl);
405         sstrncpy (vl.plugin, "mysql", sizeof (vl.plugin));
406         sstrncpy (vl.type, "mysql_qcache", sizeof (vl.type));
407         set_plugin_instance (db, &vl);
408
409         plugin_dispatch_values (&vl);
410 } /* void qcache_submit */
411
412 static void threads_submit (gauge_t running, gauge_t connected, gauge_t cached,
413                 counter_t created, mysql_database_t *db)
414 {
415         value_t values[4];
416         value_list_t vl = VALUE_LIST_INIT;
417
418         values[0].gauge   = running;
419         values[1].gauge   = connected;
420         values[2].gauge   = cached;
421         values[3].counter = created;
422
423         vl.values = values;
424         vl.values_len = 4;
425         set_host (db, &vl);
426         sstrncpy (vl.plugin, "mysql", sizeof (vl.plugin));
427         sstrncpy (vl.type, "mysql_threads", sizeof (vl.type));
428         set_plugin_instance (db, &vl);
429
430         plugin_dispatch_values (&vl);
431 } /* void threads_submit */
432
433 static void traffic_submit (counter_t rx, counter_t tx, mysql_database_t *db)
434 {
435         value_t values[2];
436         value_list_t vl = VALUE_LIST_INIT;
437
438         values[0].counter = rx;
439         values[1].counter = tx;
440
441         vl.values = values;
442         vl.values_len = 2;
443         set_host (db, &vl);
444         sstrncpy (vl.plugin, "mysql", sizeof (vl.plugin));
445         sstrncpy (vl.type, "mysql_octets", sizeof (vl.type));
446         set_plugin_instance (db, &vl);
447
448         plugin_dispatch_values (&vl);
449 } /* void traffic_submit */
450
451 static int mysql_read (user_data_t *ud)
452 {
453         mysql_database_t *db;
454         MYSQL     *con;
455         MYSQL_RES *res;
456         MYSQL_ROW  row;
457         char      *query;
458         int        query_len;
459         int        field_num;
460
461         unsigned long long qcache_hits          = 0ULL;
462         unsigned long long qcache_inserts       = 0ULL;
463         unsigned long long qcache_not_cached    = 0ULL;
464         unsigned long long qcache_lowmem_prunes = 0ULL;
465         int qcache_queries_in_cache = -1;
466
467         int threads_running   = -1;
468         int threads_connected = -1;
469         int threads_cached    = -1;
470         unsigned long long threads_created = 0ULL;
471
472         unsigned long long traffic_incoming = 0ULL;
473         unsigned long long traffic_outgoing = 0ULL;
474
475         if ((ud == NULL) || (ud->data == NULL))
476         {
477                 ERROR ("mysql plugin: mysql_database_read: Invalid user data.");
478                 return (-1);
479         }
480
481         db = (mysql_database_t *) ud->data;
482
483         /* An error message will have been printed in this case */
484         if ((con = getconnection (db)) == NULL)
485                 return (-1);
486
487         query = "SHOW STATUS";
488         if (mysql_get_server_version (con) >= 50002)
489                 query = "SHOW GLOBAL STATUS";
490
491         query_len = strlen (query);
492
493         if (mysql_real_query (con, query, query_len))
494         {
495                 ERROR ("mysql_real_query failed: %s\n",
496                                 mysql_error (con));
497                 return (-1);
498         }
499
500         if ((res = mysql_store_result (con)) == NULL)
501         {
502                 ERROR ("mysql_store_result failed: %s\n",
503                                 mysql_error (con));
504                 return (-1);
505         }
506
507         field_num = mysql_num_fields (res);
508         while ((row = mysql_fetch_row (res)))
509         {
510                 char *key;
511                 unsigned long long val;
512
513                 key = row[0];
514                 val = atoll (row[1]);
515
516                 if (strncmp (key, "Com_", 4) == 0)
517                 {
518                         if (val == 0ULL)
519                                 continue;
520
521                         /* Ignore `prepared statements' */
522                         if (strncmp (key, "Com_stmt_", 9) != 0)
523                                 counter_submit ("mysql_commands", key + 4, val, db);
524                 }
525                 else if (strncmp (key, "Handler_", 8) == 0)
526                 {
527                         if (val == 0ULL)
528                                 continue;
529
530                         counter_submit ("mysql_handler", key + 8, val, db);
531                 }
532                 else if (strncmp (key, "Qcache_", 7) == 0)
533                 {
534                         if (strcmp (key, "Qcache_hits") == 0)
535                                 qcache_hits = val;
536                         else if (strcmp (key, "Qcache_inserts") == 0)
537                                 qcache_inserts = val;
538                         else if (strcmp (key, "Qcache_not_cached") == 0)
539                                 qcache_not_cached = val;
540                         else if (strcmp (key, "Qcache_lowmem_prunes") == 0)
541                                 qcache_lowmem_prunes = val;
542                         else if (strcmp (key, "Qcache_queries_in_cache") == 0)
543                                 qcache_queries_in_cache = (int) val;
544                 }
545                 else if (strncmp (key, "Bytes_", 6) == 0)
546                 {
547                         if (strcmp (key, "Bytes_received") == 0)
548                                 traffic_incoming += val;
549                         else if (strcmp (key, "Bytes_sent") == 0)
550                                 traffic_outgoing += val;
551                 }
552                 else if (strncmp (key, "Threads_", 8) == 0)
553                 {
554                         if (strcmp (key, "Threads_running") == 0)
555                                 threads_running = (int) val;
556                         else if (strcmp (key, "Threads_connected") == 0)
557                                 threads_connected = (int) val;
558                         else if (strcmp (key, "Threads_cached") == 0)
559                                 threads_cached = (int) val;
560                         else if (strcmp (key, "Threads_created") == 0)
561                                 threads_created = val;
562                 }
563         }
564         mysql_free_result (res); res = NULL;
565
566         if ((qcache_hits != 0ULL)
567                         || (qcache_inserts != 0ULL)
568                         || (qcache_not_cached != 0ULL)
569                         || (qcache_lowmem_prunes != 0ULL))
570                 qcache_submit (qcache_hits, qcache_inserts, qcache_not_cached,
571                                qcache_lowmem_prunes, qcache_queries_in_cache, db);
572
573         if (threads_created != 0ULL)
574                 threads_submit (threads_running, threads_connected,
575                                 threads_cached, threads_created, db);
576
577         traffic_submit  (traffic_incoming, traffic_outgoing, db);
578
579         return (0);
580 } /* int mysql_read */
581
582 void module_register (void)
583 {
584         plugin_register_complex_config ("mysql", mysql_config);
585 } /* void module_register */