Removed the `LOGFILE' and `COLLECT_{STEP,HEARTBEAT,XFF,RRAROWS}' defines.
[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_MYSQL_H
28 #include <mysql/mysql.h>
29 #endif
30
31 #if COLLECT_LIBMYSQL
32 # define MYSQL_HAVE_READ 1
33 #else
34 # define MYSQL_HAVE_READ 0
35 #endif
36
37 /* TODO: Understand `Select_*' and possibly do that stuff as well.. */
38
39 static data_source_t data_source_counter[1] =
40 {
41         {"value", DS_TYPE_COUNTER, 0, NAN}
42 };
43
44 static data_set_t ds_commands =
45 {
46         "mysql_commands", 1, data_source_counter
47 };
48
49 static data_set_t ds_handler =
50 {
51         "mysql_handler", 1, data_source_counter
52 };
53
54 static data_source_t data_source_qcache[5] =
55 {
56         {"hits",             DS_TYPE_COUNTER, 0, NAN},
57         {"inserts",          DS_TYPE_COUNTER, 0, NAN},
58         {"not_cached",       DS_TYPE_COUNTER, 0, NAN},
59         {"lowmem_prunes",    DS_TYPE_COUNTER, 0, NAN},
60         {"queries_in_cache", DS_TYPE_GAUGE,   0, NAN}
61 };
62
63 static data_set_t ds_qcache =
64 {
65         "mysql_qcache", 5, data_source_qcache
66 };
67
68 static data_source_t data_source_threads[4] =
69 {
70         {"running",   DS_TYPE_GAUGE,   0, NAN},
71         {"connected", DS_TYPE_GAUGE,   0, NAN},
72         {"cached",    DS_TYPE_GAUGE,   0, NAN},
73         {"created",   DS_TYPE_COUNTER, 0, NAN}
74 };
75
76 static data_set_t ds_threads =
77 {
78         "mysql_threads", 4, data_source_threads
79 };
80
81 static data_source_t data_source_octets[2] =
82 {
83         {"rx", DS_TYPE_COUNTER, 0, 4294967295.0},
84         {"tx", DS_TYPE_COUNTER, 0, 4294967295.0}
85 };
86
87 static data_set_t ds_octets =
88 {
89         "mysql_octets", 2, data_source_octets
90 };
91
92 #if MYSQL_HAVE_READ
93 static const char *config_keys[] =
94 {
95         "Host",
96         "User",
97         "Password",
98         "Database",
99         NULL
100 };
101 static int config_keys_num = 4;
102
103 static char *host = "localhost";
104 static char *user;
105 static char *pass;
106 static char *db = NULL;
107
108 static MYSQL *getconnection (void)
109 {
110         static MYSQL *con;
111         static int    state;
112
113         static int wait_for = 0;
114         static int wait_increase = 60;
115
116         if (state != 0)
117         {
118                 int err;
119                 if ((err = mysql_ping (con)) != 0)
120                 {
121                         WARNING ("mysql_ping failed: %s", mysql_error (con));
122                         state = 0;
123                 }
124                 else
125                 {
126                         state = 1;
127                         return (con);
128                 }
129         }
130
131         if (wait_for > 0)
132         {
133                 wait_for -= interval_g;
134                 return (NULL);
135         }
136
137         wait_for = wait_increase;
138         wait_increase *= 2;
139         if (wait_increase > 86400)
140                 wait_increase = 86400;
141
142         if ((con = mysql_init (con)) == NULL)
143         {
144                 ERROR ("mysql_init failed: %s", mysql_error (con));
145                 state = 0;
146                 return (NULL);
147         }
148
149         if (mysql_real_connect (con, host, user, pass, db, 0, NULL, 0) == NULL)
150         {
151                 ERROR ("mysql_real_connect failed: %s", mysql_error (con));
152                 state = 0;
153                 return (NULL);
154         }
155         else
156         {
157                 state = 1;
158                 wait_for = 0;
159                 wait_increase = 60;
160                 return (con);
161         }
162 } /* static MYSQL *getconnection (void) */
163
164 static int config (const char *key, const char *value)
165 {
166         if (strcasecmp (key, "host") == 0)
167                 return ((host = strdup (value)) == NULL ? 1 : 0);
168         else if (strcasecmp (key, "user") == 0)
169                 return ((user = strdup (value)) == NULL ? 1 : 0);
170         else if (strcasecmp (key, "password") == 0)
171                 return ((pass = strdup (value)) == NULL ? 1 : 0);
172         else if (strcasecmp (key, "database") == 0)
173                 return ((db = strdup (value)) == NULL ? 1 : 0);
174         else
175                 return (-1);
176 }
177
178 static void counter_submit (const char *type, const char *type_instance,
179                 counter_t value)
180 {
181         value_t values[1];
182         value_list_t vl = VALUE_LIST_INIT;
183
184         values[0].counter = value;
185
186         vl.values = values;
187         vl.values_len = 1;
188         vl.time = time (NULL);
189         strcpy (vl.host, hostname_g);
190         strcpy (vl.plugin, "mysql");
191         strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
192
193         plugin_dispatch_values (type, &vl);
194 } /* void counter_submit */
195
196 static void qcache_submit (counter_t hits, counter_t inserts,
197                 counter_t not_cached, counter_t lowmem_prunes,
198                 gauge_t queries_in_cache)
199 {
200         value_t values[5];
201         value_list_t vl = VALUE_LIST_INIT;
202
203         values[0].counter = hits;
204         values[1].counter = inserts;
205         values[2].counter = not_cached;
206         values[3].counter = lowmem_prunes;
207         values[4].gauge   = queries_in_cache;
208
209         vl.values = values;
210         vl.values_len = 5;
211         vl.time = time (NULL);
212         strcpy (vl.host, hostname_g);
213         strcpy (vl.plugin, "mysql");
214
215         plugin_dispatch_values ("mysql_qcache", &vl);
216 } /* void qcache_submit */
217
218 static void threads_submit (gauge_t running, gauge_t connected, gauge_t cached,
219                 counter_t created)
220 {
221         value_t values[4];
222         value_list_t vl = VALUE_LIST_INIT;
223
224         values[0].gauge   = running;
225         values[1].gauge   = connected;
226         values[2].gauge   = cached;
227         values[3].counter = created;
228
229         vl.values = values;
230         vl.values_len = 4;
231         vl.time = time (NULL);
232         strcpy (vl.host, hostname_g);
233         strcpy (vl.plugin, "mysql");
234
235         plugin_dispatch_values ("mysql_threads", &vl);
236 } /* void threads_submit */
237
238 static void traffic_submit (counter_t rx, counter_t tx)
239 {
240         value_t values[2];
241         value_list_t vl = VALUE_LIST_INIT;
242
243         values[0].counter = rx;
244         values[1].counter = tx;
245
246         vl.values = values;
247         vl.values_len = 2;
248         vl.time = time (NULL);
249         strcpy (vl.host, hostname_g);
250         strcpy (vl.plugin, "mysql");
251
252         plugin_dispatch_values ("mysql_octets", &vl);
253 } /* void traffic_submit */
254
255 static int mysql_read (void)
256 {
257         MYSQL     *con;
258         MYSQL_RES *res;
259         MYSQL_ROW  row;
260         char      *query;
261         int        query_len;
262         int        field_num;
263
264         unsigned long long qcache_hits          = 0ULL;
265         unsigned long long qcache_inserts       = 0ULL;
266         unsigned long long qcache_not_cached    = 0ULL;
267         unsigned long long qcache_lowmem_prunes = 0ULL;
268         int qcache_queries_in_cache = -1;
269
270         int threads_running   = -1;
271         int threads_connected = -1;
272         int threads_cached    = -1;
273         unsigned long long threads_created = 0ULL;
274
275         unsigned long long traffic_incoming = 0ULL;
276         unsigned long long traffic_outgoing = 0ULL;
277
278         /* An error message will have been printed in this case */
279         if ((con = getconnection ()) == NULL)
280                 return (-1);
281
282         query = "SHOW STATUS";
283         if (mysql_get_server_version (con) >= 50002)
284                 query = "SHOW GLOBAL STATUS";
285
286         query_len = strlen (query);
287
288         if (mysql_real_query (con, query, query_len))
289         {
290                 ERROR ("mysql_real_query failed: %s\n",
291                                 mysql_error (con));
292                 return (-1);
293         }
294
295         if ((res = mysql_store_result (con)) == NULL)
296         {
297                 ERROR ("mysql_store_result failed: %s\n",
298                                 mysql_error (con));
299                 return (-1);
300         }
301
302         field_num = mysql_num_fields (res);
303         while ((row = mysql_fetch_row (res)))
304         {
305                 char *key;
306                 unsigned long long val;
307
308                 key = row[0];
309                 val = atoll (row[1]);
310
311                 if (strncmp (key, "Com_", 4) == 0)
312                 {
313                         if (val == 0ULL)
314                                 continue;
315
316                         /* Ignore `prepared statements' */
317                         if (strncmp (key, "Com_stmt_", 9) != 0)
318                                 counter_submit ("mysql_commands", key + 4, val);
319                 }
320                 else if (strncmp (key, "Handler_", 8) == 0)
321                 {
322                         if (val == 0ULL)
323                                 continue;
324
325                         counter_submit ("mysql_handler", key + 8, val);
326                 }
327                 else if (strncmp (key, "Qcache_", 7) == 0)
328                 {
329                         if (strcmp (key, "Qcache_hits") == 0)
330                                 qcache_hits = val;
331                         else if (strcmp (key, "Qcache_inserts") == 0)
332                                 qcache_inserts = val;
333                         else if (strcmp (key, "Qcache_not_cached") == 0)
334                                 qcache_not_cached = val;
335                         else if (strcmp (key, "Qcache_lowmem_prunes") == 0)
336                                 qcache_lowmem_prunes = val;
337                         else if (strcmp (key, "Qcache_queries_in_cache") == 0)
338                                 qcache_queries_in_cache = (int) val;
339                 }
340                 else if (strncmp (key, "Bytes_", 6) == 0)
341                 {
342                         if (strcmp (key, "Bytes_received") == 0)
343                                 traffic_incoming += val;
344                         else if (strcmp (key, "Bytes_sent") == 0)
345                                 traffic_outgoing += val;
346                 }
347                 else if (strncmp (key, "Threads_", 8) == 0)
348                 {
349                         if (strcmp (key, "Threads_running") == 0)
350                                 threads_running = (int) val;
351                         else if (strcmp (key, "Threads_connected") == 0)
352                                 threads_connected = (int) val;
353                         else if (strcmp (key, "Threads_cached") == 0)
354                                 threads_cached = (int) val;
355                         else if (strcmp (key, "Threads_created") == 0)
356                                 threads_created = val;
357                 }
358         }
359         mysql_free_result (res); res = NULL;
360
361         if ((qcache_hits != 0ULL)
362                         || (qcache_inserts != 0ULL)
363                         || (qcache_not_cached != 0ULL)
364                         || (qcache_lowmem_prunes != 0ULL))
365                 qcache_submit (qcache_hits, qcache_inserts, qcache_not_cached,
366                                 qcache_lowmem_prunes, qcache_queries_in_cache);
367
368         if (threads_created != 0ULL)
369                 threads_submit (threads_running, threads_connected,
370                                 threads_cached, threads_created);
371
372         traffic_submit  (traffic_incoming, traffic_outgoing);
373
374         /* mysql_close (con); */
375
376         return (0);
377 } /* int mysql_read */
378 #endif /* MYSQL_HAVE_READ */
379
380 void module_register (void)
381 {
382         plugin_register_data_set (&ds_commands);
383         plugin_register_data_set (&ds_handler);
384         plugin_register_data_set (&ds_qcache);
385         plugin_register_data_set (&ds_threads);
386         plugin_register_data_set (&ds_octets);
387
388 #if MYSQL_HAVE_READ
389         plugin_register_config ("mysql", config, config_keys, config_keys_num);
390         plugin_register_read ("mysql", mysql_read);
391 #endif /* MYSQL_HAVE_READ */
392 } /* void module_register */