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