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"
29 #elif defined(HAVE_MYSQL_MYSQL_H)
30 #include <mysql/mysql.h>
33 /* TODO: Understand `Select_*' and possibly do that stuff as well.. */
35 static const char *config_keys[] =
43 static int config_keys_num = 4;
45 static char *host = "localhost";
48 static char *db = NULL;
50 static MYSQL *getconnection (void)
55 static int wait_for = 0;
56 static int wait_increase = 60;
61 if ((err = mysql_ping (con)) != 0)
63 WARNING ("mysql_ping failed: %s", mysql_error (con));
75 wait_for -= interval_g;
79 wait_for = wait_increase;
81 if (wait_increase > 86400)
82 wait_increase = 86400;
84 if ((con = mysql_init (con)) == NULL)
86 ERROR ("mysql_init failed: %s", mysql_error (con));
91 if (mysql_real_connect (con, host, user, pass, db, 0, NULL, 0) == NULL)
93 ERROR ("mysql_real_connect failed: %s", mysql_error (con));
104 } /* static MYSQL *getconnection (void) */
106 static int config (const char *key, const char *value)
108 if (strcasecmp (key, "host") == 0)
109 return ((host = strdup (value)) == NULL ? 1 : 0);
110 else if (strcasecmp (key, "user") == 0)
111 return ((user = strdup (value)) == NULL ? 1 : 0);
112 else if (strcasecmp (key, "password") == 0)
113 return ((pass = strdup (value)) == NULL ? 1 : 0);
114 else if (strcasecmp (key, "database") == 0)
115 return ((db = strdup (value)) == NULL ? 1 : 0);
120 static void counter_submit (const char *type, const char *type_instance,
124 value_list_t vl = VALUE_LIST_INIT;
126 values[0].counter = value;
130 vl.time = time (NULL);
131 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
132 sstrncpy (vl.plugin, "mysql", sizeof (vl.plugin));
133 sstrncpy (vl.type, type, sizeof (vl.type));
134 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
136 plugin_dispatch_values (&vl);
137 } /* void counter_submit */
139 static void qcache_submit (counter_t hits, counter_t inserts,
140 counter_t not_cached, counter_t lowmem_prunes,
141 gauge_t queries_in_cache)
144 value_list_t vl = VALUE_LIST_INIT;
146 values[0].counter = hits;
147 values[1].counter = inserts;
148 values[2].counter = not_cached;
149 values[3].counter = lowmem_prunes;
150 values[4].gauge = queries_in_cache;
154 vl.time = time (NULL);
155 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
156 sstrncpy (vl.plugin, "mysql", sizeof (vl.plugin));
157 sstrncpy (vl.type, "mysql_qcache", sizeof (vl.type));
159 plugin_dispatch_values (&vl);
160 } /* void qcache_submit */
162 static void threads_submit (gauge_t running, gauge_t connected, gauge_t cached,
166 value_list_t vl = VALUE_LIST_INIT;
168 values[0].gauge = running;
169 values[1].gauge = connected;
170 values[2].gauge = cached;
171 values[3].counter = created;
175 vl.time = time (NULL);
176 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
177 sstrncpy (vl.plugin, "mysql", sizeof (vl.plugin));
178 sstrncpy (vl.type, "mysql_threads", sizeof (vl.type));
180 plugin_dispatch_values (&vl);
181 } /* void threads_submit */
183 static void traffic_submit (counter_t rx, counter_t tx)
186 value_list_t vl = VALUE_LIST_INIT;
188 values[0].counter = rx;
189 values[1].counter = tx;
193 vl.time = time (NULL);
194 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
195 sstrncpy (vl.plugin, "mysql", sizeof (vl.plugin));
196 sstrncpy (vl.type, "mysql_octets", sizeof (vl.type));
198 plugin_dispatch_values (&vl);
199 } /* void traffic_submit */
201 static int mysql_read (void)
210 unsigned long long qcache_hits = 0ULL;
211 unsigned long long qcache_inserts = 0ULL;
212 unsigned long long qcache_not_cached = 0ULL;
213 unsigned long long qcache_lowmem_prunes = 0ULL;
214 int qcache_queries_in_cache = -1;
216 int threads_running = -1;
217 int threads_connected = -1;
218 int threads_cached = -1;
219 unsigned long long threads_created = 0ULL;
221 unsigned long long traffic_incoming = 0ULL;
222 unsigned long long traffic_outgoing = 0ULL;
224 /* An error message will have been printed in this case */
225 if ((con = getconnection ()) == NULL)
228 query = "SHOW STATUS";
229 if (mysql_get_server_version (con) >= 50002)
230 query = "SHOW GLOBAL STATUS";
232 query_len = strlen (query);
234 if (mysql_real_query (con, query, query_len))
236 ERROR ("mysql_real_query failed: %s\n",
241 if ((res = mysql_store_result (con)) == NULL)
243 ERROR ("mysql_store_result failed: %s\n",
248 field_num = mysql_num_fields (res);
249 while ((row = mysql_fetch_row (res)))
252 unsigned long long val;
255 val = atoll (row[1]);
257 if (strncmp (key, "Com_", 4) == 0)
262 /* Ignore `prepared statements' */
263 if (strncmp (key, "Com_stmt_", 9) != 0)
264 counter_submit ("mysql_commands", key + 4, val);
266 else if (strncmp (key, "Handler_", 8) == 0)
271 counter_submit ("mysql_handler", key + 8, val);
273 else if (strncmp (key, "Qcache_", 7) == 0)
275 if (strcmp (key, "Qcache_hits") == 0)
277 else if (strcmp (key, "Qcache_inserts") == 0)
278 qcache_inserts = val;
279 else if (strcmp (key, "Qcache_not_cached") == 0)
280 qcache_not_cached = val;
281 else if (strcmp (key, "Qcache_lowmem_prunes") == 0)
282 qcache_lowmem_prunes = val;
283 else if (strcmp (key, "Qcache_queries_in_cache") == 0)
284 qcache_queries_in_cache = (int) val;
286 else if (strncmp (key, "Bytes_", 6) == 0)
288 if (strcmp (key, "Bytes_received") == 0)
289 traffic_incoming += val;
290 else if (strcmp (key, "Bytes_sent") == 0)
291 traffic_outgoing += val;
293 else if (strncmp (key, "Threads_", 8) == 0)
295 if (strcmp (key, "Threads_running") == 0)
296 threads_running = (int) val;
297 else if (strcmp (key, "Threads_connected") == 0)
298 threads_connected = (int) val;
299 else if (strcmp (key, "Threads_cached") == 0)
300 threads_cached = (int) val;
301 else if (strcmp (key, "Threads_created") == 0)
302 threads_created = val;
305 mysql_free_result (res); res = NULL;
307 if ((qcache_hits != 0ULL)
308 || (qcache_inserts != 0ULL)
309 || (qcache_not_cached != 0ULL)
310 || (qcache_lowmem_prunes != 0ULL))
311 qcache_submit (qcache_hits, qcache_inserts, qcache_not_cached,
312 qcache_lowmem_prunes, qcache_queries_in_cache);
314 if (threads_created != 0ULL)
315 threads_submit (threads_running, threads_connected,
316 threads_cached, threads_created);
318 traffic_submit (traffic_incoming, traffic_outgoing);
320 /* mysql_close (con); */
323 } /* int mysql_read */
325 void module_register (void)
327 plugin_register_config ("mysql", config, config_keys, config_keys_num);
328 plugin_register_read ("mysql", mysql_read);
329 } /* void module_register */