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