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>
31 /* TODO: Understand `Select_*' and possibly do that stuff as well.. */
33 static const char *config_keys[] =
41 static int config_keys_num = 4;
43 static char *host = "localhost";
46 static char *db = NULL;
48 static MYSQL *getconnection (void)
53 static int wait_for = 0;
54 static int wait_increase = 60;
59 if ((err = mysql_ping (con)) != 0)
61 WARNING ("mysql_ping failed: %s", mysql_error (con));
73 wait_for -= interval_g;
77 wait_for = wait_increase;
79 if (wait_increase > 86400)
80 wait_increase = 86400;
82 if ((con = mysql_init (con)) == NULL)
84 ERROR ("mysql_init failed: %s", mysql_error (con));
89 if (mysql_real_connect (con, host, user, pass, db, 0, NULL, 0) == NULL)
91 ERROR ("mysql_real_connect failed: %s", mysql_error (con));
102 } /* static MYSQL *getconnection (void) */
104 static int config (const char *key, const char *value)
106 if (strcasecmp (key, "host") == 0)
107 return ((host = strdup (value)) == NULL ? 1 : 0);
108 else if (strcasecmp (key, "user") == 0)
109 return ((user = strdup (value)) == NULL ? 1 : 0);
110 else if (strcasecmp (key, "password") == 0)
111 return ((pass = strdup (value)) == NULL ? 1 : 0);
112 else if (strcasecmp (key, "database") == 0)
113 return ((db = strdup (value)) == NULL ? 1 : 0);
118 static void counter_submit (const char *type, const char *type_instance,
122 value_list_t vl = VALUE_LIST_INIT;
124 values[0].counter = value;
128 vl.time = time (NULL);
129 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
130 sstrncpy (vl.plugin, "mysql", sizeof (vl.plugin));
131 sstrncpy (vl.type, type, sizeof (vl.type));
132 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
134 plugin_dispatch_values (&vl);
135 } /* void counter_submit */
137 static void qcache_submit (counter_t hits, counter_t inserts,
138 counter_t not_cached, counter_t lowmem_prunes,
139 gauge_t queries_in_cache)
142 value_list_t vl = VALUE_LIST_INIT;
144 values[0].counter = hits;
145 values[1].counter = inserts;
146 values[2].counter = not_cached;
147 values[3].counter = lowmem_prunes;
148 values[4].gauge = queries_in_cache;
152 vl.time = time (NULL);
153 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
154 sstrncpy (vl.plugin, "mysql", sizeof (vl.plugin));
155 sstrncpy (vl.type, "mysql_qcache", sizeof (vl.type));
157 plugin_dispatch_values (&vl);
158 } /* void qcache_submit */
160 static void threads_submit (gauge_t running, gauge_t connected, gauge_t cached,
164 value_list_t vl = VALUE_LIST_INIT;
166 values[0].gauge = running;
167 values[1].gauge = connected;
168 values[2].gauge = cached;
169 values[3].counter = created;
173 vl.time = time (NULL);
174 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
175 sstrncpy (vl.plugin, "mysql", sizeof (vl.plugin));
176 sstrncpy (vl.type, "mysql_threads", sizeof (vl.type));
178 plugin_dispatch_values (&vl);
179 } /* void threads_submit */
181 static void traffic_submit (counter_t rx, counter_t tx)
184 value_list_t vl = VALUE_LIST_INIT;
186 values[0].counter = rx;
187 values[1].counter = tx;
191 vl.time = time (NULL);
192 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
193 sstrncpy (vl.plugin, "mysql", sizeof (vl.plugin));
194 sstrncpy (vl.type, "mysql_octets", sizeof (vl.type));
196 plugin_dispatch_values (&vl);
197 } /* void traffic_submit */
199 static int mysql_read (void)
208 unsigned long long qcache_hits = 0ULL;
209 unsigned long long qcache_inserts = 0ULL;
210 unsigned long long qcache_not_cached = 0ULL;
211 unsigned long long qcache_lowmem_prunes = 0ULL;
212 int qcache_queries_in_cache = -1;
214 int threads_running = -1;
215 int threads_connected = -1;
216 int threads_cached = -1;
217 unsigned long long threads_created = 0ULL;
219 unsigned long long traffic_incoming = 0ULL;
220 unsigned long long traffic_outgoing = 0ULL;
222 /* An error message will have been printed in this case */
223 if ((con = getconnection ()) == NULL)
226 query = "SHOW STATUS";
227 if (mysql_get_server_version (con) >= 50002)
228 query = "SHOW GLOBAL STATUS";
230 query_len = strlen (query);
232 if (mysql_real_query (con, query, query_len))
234 ERROR ("mysql_real_query failed: %s\n",
239 if ((res = mysql_store_result (con)) == NULL)
241 ERROR ("mysql_store_result failed: %s\n",
246 field_num = mysql_num_fields (res);
247 while ((row = mysql_fetch_row (res)))
250 unsigned long long val;
253 val = atoll (row[1]);
255 if (strncmp (key, "Com_", 4) == 0)
260 /* Ignore `prepared statements' */
261 if (strncmp (key, "Com_stmt_", 9) != 0)
262 counter_submit ("mysql_commands", key + 4, val);
264 else if (strncmp (key, "Handler_", 8) == 0)
269 counter_submit ("mysql_handler", key + 8, val);
271 else if (strncmp (key, "Qcache_", 7) == 0)
273 if (strcmp (key, "Qcache_hits") == 0)
275 else if (strcmp (key, "Qcache_inserts") == 0)
276 qcache_inserts = val;
277 else if (strcmp (key, "Qcache_not_cached") == 0)
278 qcache_not_cached = val;
279 else if (strcmp (key, "Qcache_lowmem_prunes") == 0)
280 qcache_lowmem_prunes = val;
281 else if (strcmp (key, "Qcache_queries_in_cache") == 0)
282 qcache_queries_in_cache = (int) val;
284 else if (strncmp (key, "Bytes_", 6) == 0)
286 if (strcmp (key, "Bytes_received") == 0)
287 traffic_incoming += val;
288 else if (strcmp (key, "Bytes_sent") == 0)
289 traffic_outgoing += val;
291 else if (strncmp (key, "Threads_", 8) == 0)
293 if (strcmp (key, "Threads_running") == 0)
294 threads_running = (int) val;
295 else if (strcmp (key, "Threads_connected") == 0)
296 threads_connected = (int) val;
297 else if (strcmp (key, "Threads_cached") == 0)
298 threads_cached = (int) val;
299 else if (strcmp (key, "Threads_created") == 0)
300 threads_created = val;
303 mysql_free_result (res); res = NULL;
305 if ((qcache_hits != 0ULL)
306 || (qcache_inserts != 0ULL)
307 || (qcache_not_cached != 0ULL)
308 || (qcache_lowmem_prunes != 0ULL))
309 qcache_submit (qcache_hits, qcache_inserts, qcache_not_cached,
310 qcache_lowmem_prunes, qcache_queries_in_cache);
312 if (threads_created != 0ULL)
313 threads_submit (threads_running, threads_connected,
314 threads_cached, threads_created);
316 traffic_submit (traffic_incoming, traffic_outgoing);
318 /* mysql_close (con); */
321 } /* int mysql_read */
323 void module_register (void)
325 plugin_register_config ("mysql", config, config_keys, config_keys_num);
326 plugin_register_read ("mysql", mysql_read);
327 } /* void module_register */