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[] =
45 static int config_keys_num = 6;
47 static char *host = "localhost";
50 static char *db = NULL;
51 static char *socket = NULL;
54 static MYSQL *getconnection (void)
59 static int wait_for = 0;
60 static int wait_increase = 60;
65 if ((err = mysql_ping (con)) != 0)
67 WARNING ("mysql_ping failed: %s", mysql_error (con));
79 wait_for -= interval_g;
83 wait_for = wait_increase;
85 if (wait_increase > 86400)
86 wait_increase = 86400;
88 if ((con = mysql_init (con)) == NULL)
90 ERROR ("mysql_init failed: %s", mysql_error (con));
95 if (mysql_real_connect (con, host, user, pass, db, port, socket, 0) == NULL)
97 ERROR ("mysql_real_connect failed: %s", mysql_error (con));
108 } /* static MYSQL *getconnection (void) */
110 static int config (const char *key, const char *value)
112 if (strcasecmp (key, "host") == 0)
113 return ((host = strdup (value)) == NULL ? 1 : 0);
114 else if (strcasecmp (key, "user") == 0)
115 return ((user = strdup (value)) == NULL ? 1 : 0);
116 else if (strcasecmp (key, "password") == 0)
117 return ((pass = strdup (value)) == NULL ? 1 : 0);
118 else if (strcasecmp (key, "database") == 0)
119 return ((db = strdup (value)) == NULL ? 1 : 0);
120 else if (strcasecmp (key, "socket") == 0)
121 return ((socket = strdup (value)) == NULL ? 1 : 0);
122 else if (strcasecmp (key, "port") == 0)
128 temp = strtol (value, $endptr, 0);
129 if ((errno != 0) || (value == endptr))
131 ERROR ("mysql plugin: Invalid \"Port\" argument: %s",
136 else if ((temp < 0) || (temp >= 65535))
138 ERROR ("mysql plugin: Port number out of range: %i",
150 static void counter_submit (const char *type, const char *type_instance,
154 value_list_t vl = VALUE_LIST_INIT;
156 values[0].counter = value;
160 vl.time = time (NULL);
161 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
162 sstrncpy (vl.plugin, "mysql", sizeof (vl.plugin));
163 sstrncpy (vl.type, type, sizeof (vl.type));
164 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
166 plugin_dispatch_values (&vl);
167 } /* void counter_submit */
169 static void qcache_submit (counter_t hits, counter_t inserts,
170 counter_t not_cached, counter_t lowmem_prunes,
171 gauge_t queries_in_cache)
174 value_list_t vl = VALUE_LIST_INIT;
176 values[0].counter = hits;
177 values[1].counter = inserts;
178 values[2].counter = not_cached;
179 values[3].counter = lowmem_prunes;
180 values[4].gauge = queries_in_cache;
184 vl.time = time (NULL);
185 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
186 sstrncpy (vl.plugin, "mysql", sizeof (vl.plugin));
187 sstrncpy (vl.type, "mysql_qcache", sizeof (vl.type));
189 plugin_dispatch_values (&vl);
190 } /* void qcache_submit */
192 static void threads_submit (gauge_t running, gauge_t connected, gauge_t cached,
196 value_list_t vl = VALUE_LIST_INIT;
198 values[0].gauge = running;
199 values[1].gauge = connected;
200 values[2].gauge = cached;
201 values[3].counter = created;
205 vl.time = time (NULL);
206 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
207 sstrncpy (vl.plugin, "mysql", sizeof (vl.plugin));
208 sstrncpy (vl.type, "mysql_threads", sizeof (vl.type));
210 plugin_dispatch_values (&vl);
211 } /* void threads_submit */
213 static void traffic_submit (counter_t rx, counter_t tx)
216 value_list_t vl = VALUE_LIST_INIT;
218 values[0].counter = rx;
219 values[1].counter = tx;
223 vl.time = time (NULL);
224 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
225 sstrncpy (vl.plugin, "mysql", sizeof (vl.plugin));
226 sstrncpy (vl.type, "mysql_octets", sizeof (vl.type));
228 plugin_dispatch_values (&vl);
229 } /* void traffic_submit */
231 static int mysql_read (void)
240 unsigned long long qcache_hits = 0ULL;
241 unsigned long long qcache_inserts = 0ULL;
242 unsigned long long qcache_not_cached = 0ULL;
243 unsigned long long qcache_lowmem_prunes = 0ULL;
244 int qcache_queries_in_cache = -1;
246 int threads_running = -1;
247 int threads_connected = -1;
248 int threads_cached = -1;
249 unsigned long long threads_created = 0ULL;
251 unsigned long long traffic_incoming = 0ULL;
252 unsigned long long traffic_outgoing = 0ULL;
254 /* An error message will have been printed in this case */
255 if ((con = getconnection ()) == NULL)
258 query = "SHOW STATUS";
259 if (mysql_get_server_version (con) >= 50002)
260 query = "SHOW GLOBAL STATUS";
262 query_len = strlen (query);
264 if (mysql_real_query (con, query, query_len))
266 ERROR ("mysql_real_query failed: %s\n",
271 if ((res = mysql_store_result (con)) == NULL)
273 ERROR ("mysql_store_result failed: %s\n",
278 field_num = mysql_num_fields (res);
279 while ((row = mysql_fetch_row (res)))
282 unsigned long long val;
285 val = atoll (row[1]);
287 if (strncmp (key, "Com_", 4) == 0)
292 /* Ignore `prepared statements' */
293 if (strncmp (key, "Com_stmt_", 9) != 0)
294 counter_submit ("mysql_commands", key + 4, val);
296 else if (strncmp (key, "Handler_", 8) == 0)
301 counter_submit ("mysql_handler", key + 8, val);
303 else if (strncmp (key, "Qcache_", 7) == 0)
305 if (strcmp (key, "Qcache_hits") == 0)
307 else if (strcmp (key, "Qcache_inserts") == 0)
308 qcache_inserts = val;
309 else if (strcmp (key, "Qcache_not_cached") == 0)
310 qcache_not_cached = val;
311 else if (strcmp (key, "Qcache_lowmem_prunes") == 0)
312 qcache_lowmem_prunes = val;
313 else if (strcmp (key, "Qcache_queries_in_cache") == 0)
314 qcache_queries_in_cache = (int) val;
316 else if (strncmp (key, "Bytes_", 6) == 0)
318 if (strcmp (key, "Bytes_received") == 0)
319 traffic_incoming += val;
320 else if (strcmp (key, "Bytes_sent") == 0)
321 traffic_outgoing += val;
323 else if (strncmp (key, "Threads_", 8) == 0)
325 if (strcmp (key, "Threads_running") == 0)
326 threads_running = (int) val;
327 else if (strcmp (key, "Threads_connected") == 0)
328 threads_connected = (int) val;
329 else if (strcmp (key, "Threads_cached") == 0)
330 threads_cached = (int) val;
331 else if (strcmp (key, "Threads_created") == 0)
332 threads_created = val;
335 mysql_free_result (res); res = NULL;
337 if ((qcache_hits != 0ULL)
338 || (qcache_inserts != 0ULL)
339 || (qcache_not_cached != 0ULL)
340 || (qcache_lowmem_prunes != 0ULL))
341 qcache_submit (qcache_hits, qcache_inserts, qcache_not_cached,
342 qcache_lowmem_prunes, qcache_queries_in_cache);
344 if (threads_created != 0ULL)
345 threads_submit (threads_running, threads_connected,
346 threads_cached, threads_created);
348 traffic_submit (traffic_incoming, traffic_outgoing);
350 /* mysql_close (con); */
353 } /* int mysql_read */
355 void module_register (void)
357 plugin_register_config ("mysql", config, config_keys, config_keys_num);
358 plugin_register_read ("mysql", mysql_read);
359 } /* void module_register */