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 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 }
131 if (wait_for > 0)
132 {
133 wait_for -= interval_g;
134 return (NULL);
135 }
137 wait_for = wait_increase;
138 wait_increase *= 2;
139 if (wait_increase > 86400)
140 wait_increase = 86400;
142 if ((con = mysql_init (con)) == NULL)
143 {
144 ERROR ("mysql_init failed: %s", mysql_error (con));
145 state = 0;
146 return (NULL);
147 }
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) */
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 }
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;
184 values[0].counter = value;
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));
193 plugin_dispatch_values (type, &vl);
194 } /* void counter_submit */
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;
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;
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");
215 plugin_dispatch_values ("mysql_qcache", &vl);
216 } /* void qcache_submit */
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;
224 values[0].gauge = running;
225 values[1].gauge = connected;
226 values[2].gauge = cached;
227 values[3].counter = created;
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");
235 plugin_dispatch_values ("mysql_threads", &vl);
236 } /* void threads_submit */
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;
243 values[0].counter = rx;
244 values[1].counter = tx;
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");
252 plugin_dispatch_values ("mysql_octets", &vl);
253 } /* void traffic_submit */
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;
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;
270 int threads_running = -1;
271 int threads_connected = -1;
272 int threads_cached = -1;
273 unsigned long long threads_created = 0ULL;
275 unsigned long long traffic_incoming = 0ULL;
276 unsigned long long traffic_outgoing = 0ULL;
278 /* An error message will have been printed in this case */
279 if ((con = getconnection ()) == NULL)
280 return (-1);
282 query = "SHOW STATUS";
283 if (mysql_get_server_version (con) >= 50002)
284 query = "SHOW GLOBAL STATUS";
286 query_len = strlen (query);
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 }
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 }
302 field_num = mysql_num_fields (res);
303 while ((row = mysql_fetch_row (res)))
304 {
305 char *key;
306 unsigned long long val;
308 key = row[0];
309 val = atoll (row[1]);
311 if (strncmp (key, "Com_", 4) == 0)
312 {
313 if (val == 0ULL)
314 continue;
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;
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;
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);
368 if (threads_created != 0ULL)
369 threads_submit (threads_running, threads_connected,
370 threads_cached, threads_created);
372 traffic_submit (traffic_incoming, traffic_outgoing);
374 /* mysql_close (con); */
376 return (0);
377 } /* int mysql_read */
378 #endif /* MYSQL_HAVE_READ */
380 void module_register (modreg_e load)
381 {
382 if (load & MR_DATASETS)
383 {
384 plugin_register_data_set (&ds_commands);
385 plugin_register_data_set (&ds_handler);
386 plugin_register_data_set (&ds_qcache);
387 plugin_register_data_set (&ds_threads);
388 plugin_register_data_set (&ds_octets);
389 }
391 #if MYSQL_HAVE_READ
392 if (load & MR_READ)
393 {
394 plugin_register_config ("mysql", config, config_keys, config_keys_num);
395 plugin_register_read ("mysql", mysql_read);
396 }
397 #endif /* MYSQL_HAVE_READ */
398 } /* void module_register */