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