2 * collectd - src/mysql.c
3 * Copyright (C) 2006,2007 Florian octo Forster
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.
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.
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
19 * Florian octo Forster <octo at verplant.org>
25 #include "configfile.h"
27 #ifdef HAVE_MYSQL_MYSQL_H
28 #include <mysql/mysql.h>
32 # define MYSQL_HAVE_READ 1
34 # define MYSQL_HAVE_READ 0
37 /* TODO: Understand `Select_*' and possibly do that stuff as well.. */
40 static const char *config_keys[] =
48 static int config_keys_num = 4;
50 static char *host = "localhost";
53 static char *db = NULL;
55 static MYSQL *getconnection (void)
60 static int wait_for = 0;
61 static int wait_increase = 60;
66 if ((err = mysql_ping (con)) != 0)
68 WARNING ("mysql_ping failed: %s", mysql_error (con));
80 wait_for -= interval_g;
84 wait_for = wait_increase;
86 if (wait_increase > 86400)
87 wait_increase = 86400;
89 if ((con = mysql_init (con)) == NULL)
91 ERROR ("mysql_init failed: %s", mysql_error (con));
96 if (mysql_real_connect (con, host, user, pass, db, 0, NULL, 0) == NULL)
98 ERROR ("mysql_real_connect failed: %s", mysql_error (con));
109 } /* static MYSQL *getconnection (void) */
111 static int config (const char *key, const char *value)
113 if (strcasecmp (key, "host") == 0)
114 return ((host = strdup (value)) == NULL ? 1 : 0);
115 else if (strcasecmp (key, "user") == 0)
116 return ((user = strdup (value)) == NULL ? 1 : 0);
117 else if (strcasecmp (key, "password") == 0)
118 return ((pass = strdup (value)) == NULL ? 1 : 0);
119 else if (strcasecmp (key, "database") == 0)
120 return ((db = strdup (value)) == NULL ? 1 : 0);
125 static void counter_submit (const char *type, const char *type_instance,
129 value_list_t vl = VALUE_LIST_INIT;
131 values[0].counter = value;
135 vl.time = time (NULL);
136 strcpy (vl.host, hostname_g);
137 strcpy (vl.plugin, "mysql");
138 strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
140 plugin_dispatch_values (type, &vl);
141 } /* void counter_submit */
143 static void qcache_submit (counter_t hits, counter_t inserts,
144 counter_t not_cached, counter_t lowmem_prunes,
145 gauge_t queries_in_cache)
148 value_list_t vl = VALUE_LIST_INIT;
150 values[0].counter = hits;
151 values[1].counter = inserts;
152 values[2].counter = not_cached;
153 values[3].counter = lowmem_prunes;
154 values[4].gauge = queries_in_cache;
158 vl.time = time (NULL);
159 strcpy (vl.host, hostname_g);
160 strcpy (vl.plugin, "mysql");
162 plugin_dispatch_values ("mysql_qcache", &vl);
163 } /* void qcache_submit */
165 static void threads_submit (gauge_t running, gauge_t connected, gauge_t cached,
169 value_list_t vl = VALUE_LIST_INIT;
171 values[0].gauge = running;
172 values[1].gauge = connected;
173 values[2].gauge = cached;
174 values[3].counter = created;
178 vl.time = time (NULL);
179 strcpy (vl.host, hostname_g);
180 strcpy (vl.plugin, "mysql");
182 plugin_dispatch_values ("mysql_threads", &vl);
183 } /* void threads_submit */
185 static void traffic_submit (counter_t rx, counter_t tx)
188 value_list_t vl = VALUE_LIST_INIT;
190 values[0].counter = rx;
191 values[1].counter = tx;
195 vl.time = time (NULL);
196 strcpy (vl.host, hostname_g);
197 strcpy (vl.plugin, "mysql");
199 plugin_dispatch_values ("mysql_octets", &vl);
200 } /* void traffic_submit */
202 static int mysql_read (void)
211 unsigned long long qcache_hits = 0ULL;
212 unsigned long long qcache_inserts = 0ULL;
213 unsigned long long qcache_not_cached = 0ULL;
214 unsigned long long qcache_lowmem_prunes = 0ULL;
215 int qcache_queries_in_cache = -1;
217 int threads_running = -1;
218 int threads_connected = -1;
219 int threads_cached = -1;
220 unsigned long long threads_created = 0ULL;
222 unsigned long long traffic_incoming = 0ULL;
223 unsigned long long traffic_outgoing = 0ULL;
225 /* An error message will have been printed in this case */
226 if ((con = getconnection ()) == NULL)
229 query = "SHOW STATUS";
230 if (mysql_get_server_version (con) >= 50002)
231 query = "SHOW GLOBAL STATUS";
233 query_len = strlen (query);
235 if (mysql_real_query (con, query, query_len))
237 ERROR ("mysql_real_query failed: %s\n",
242 if ((res = mysql_store_result (con)) == NULL)
244 ERROR ("mysql_store_result failed: %s\n",
249 field_num = mysql_num_fields (res);
250 while ((row = mysql_fetch_row (res)))
253 unsigned long long val;
256 val = atoll (row[1]);
258 if (strncmp (key, "Com_", 4) == 0)
263 /* Ignore `prepared statements' */
264 if (strncmp (key, "Com_stmt_", 9) != 0)
265 counter_submit ("mysql_commands", key + 4, val);
267 else if (strncmp (key, "Handler_", 8) == 0)
272 counter_submit ("mysql_handler", key + 8, val);
274 else if (strncmp (key, "Qcache_", 7) == 0)
276 if (strcmp (key, "Qcache_hits") == 0)
278 else if (strcmp (key, "Qcache_inserts") == 0)
279 qcache_inserts = val;
280 else if (strcmp (key, "Qcache_not_cached") == 0)
281 qcache_not_cached = val;
282 else if (strcmp (key, "Qcache_lowmem_prunes") == 0)
283 qcache_lowmem_prunes = val;
284 else if (strcmp (key, "Qcache_queries_in_cache") == 0)
285 qcache_queries_in_cache = (int) val;
287 else if (strncmp (key, "Bytes_", 6) == 0)
289 if (strcmp (key, "Bytes_received") == 0)
290 traffic_incoming += val;
291 else if (strcmp (key, "Bytes_sent") == 0)
292 traffic_outgoing += val;
294 else if (strncmp (key, "Threads_", 8) == 0)
296 if (strcmp (key, "Threads_running") == 0)
297 threads_running = (int) val;
298 else if (strcmp (key, "Threads_connected") == 0)
299 threads_connected = (int) val;
300 else if (strcmp (key, "Threads_cached") == 0)
301 threads_cached = (int) val;
302 else if (strcmp (key, "Threads_created") == 0)
303 threads_created = val;
306 mysql_free_result (res); res = NULL;
308 if ((qcache_hits != 0ULL)
309 || (qcache_inserts != 0ULL)
310 || (qcache_not_cached != 0ULL)
311 || (qcache_lowmem_prunes != 0ULL))
312 qcache_submit (qcache_hits, qcache_inserts, qcache_not_cached,
313 qcache_lowmem_prunes, qcache_queries_in_cache);
315 if (threads_created != 0ULL)
316 threads_submit (threads_running, threads_connected,
317 threads_cached, threads_created);
319 traffic_submit (traffic_incoming, traffic_outgoing);
321 /* mysql_close (con); */
324 } /* int mysql_read */
325 #endif /* MYSQL_HAVE_READ */
327 void module_register (void)
330 plugin_register_config ("mysql", config, config_keys, config_keys_num);
331 plugin_register_read ("mysql", mysql_read);
332 #endif /* MYSQL_HAVE_READ */
333 } /* void module_register */