1 /**
2 * collectd - src/mysql.c
3 * Copyright (C) 2006–2008 Florian octo Forster
4 * Copyright (C) 2008 Mirko Buffoni
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; only version 2 of the License is applicable.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 * Authors:
20 * Florian octo Forster <octo at verplant.org>
21 * Mirko Buffoni <briareos at eswat.org>
22 **/
24 #include "collectd.h"
25 #include "common.h"
26 #include "plugin.h"
27 #include "configfile.h"
29 #ifdef HAVE_MYSQL_H
30 #include <mysql.h>
31 #elif defined(HAVE_MYSQL_MYSQL_H)
32 #include <mysql/mysql.h>
33 #endif
35 /* TODO: Understand `Select_*' and possibly do that stuff as well.. */
37 static const char *config_keys[] =
38 {
39 "Host",
40 "User",
41 "Password",
42 "Database",
43 "Port",
44 "Socket",
45 NULL
46 };
47 static int config_keys_num = 6;
49 static char *host = "localhost";
50 static char *user;
51 static char *pass;
52 static char *db = NULL;
53 static char *socket = NULL;
54 static int port = 0;
56 static MYSQL *getconnection (void)
57 {
58 static MYSQL *con;
59 static int state;
61 static int wait_for = 0;
62 static int wait_increase = 60;
64 if (state != 0)
65 {
66 int err;
67 if ((err = mysql_ping (con)) != 0)
68 {
69 WARNING ("mysql_ping failed: %s", mysql_error (con));
70 state = 0;
71 }
72 else
73 {
74 state = 1;
75 return (con);
76 }
77 }
79 if (wait_for > 0)
80 {
81 wait_for -= interval_g;
82 return (NULL);
83 }
85 wait_for = wait_increase;
86 wait_increase *= 2;
87 if (wait_increase > 86400)
88 wait_increase = 86400;
90 if ((con = mysql_init (con)) == NULL)
91 {
92 ERROR ("mysql_init failed: %s", mysql_error (con));
93 state = 0;
94 return (NULL);
95 }
97 if (mysql_real_connect (con, host, user, pass, db, port, socket, 0) == NULL)
98 {
99 ERROR ("mysql_real_connect failed: %s", mysql_error (con));
100 state = 0;
101 return (NULL);
102 }
103 else
104 {
105 state = 1;
106 wait_for = 0;
107 wait_increase = 60;
108 return (con);
109 }
110 } /* static MYSQL *getconnection (void) */
112 static int config (const char *key, const char *value)
113 {
114 if (strcasecmp (key, "host") == 0)
115 return ((host = strdup (value)) == NULL ? 1 : 0);
116 else if (strcasecmp (key, "user") == 0)
117 return ((user = strdup (value)) == NULL ? 1 : 0);
118 else if (strcasecmp (key, "password") == 0)
119 return ((pass = strdup (value)) == NULL ? 1 : 0);
120 else if (strcasecmp (key, "database") == 0)
121 return ((db = strdup (value)) == NULL ? 1 : 0);
122 else if (strcasecmp (key, "socket") == 0)
123 return ((socket = strdup (value)) == NULL ? 1 : 0);
124 else if (strcasecmp (key, "port") == 0)
125 {
126 char *endptr = NULL;
127 int temp;
129 errno = 0;
130 temp = strtol (value, &endptr, 0);
131 if ((errno != 0) || (value == endptr))
132 {
133 ERROR ("mysql plugin: Invalid \"Port\" argument: %s",
134 value);
135 port = 0;
136 return (1);
137 }
138 else if ((temp < 0) || (temp >= 65535))
139 {
140 ERROR ("mysql plugin: Port number out of range: %i",
141 temp);
142 port = 0;
143 return (1);
144 }
146 port = temp;
147 return (0);
148 }
149 else
150 return (-1);
151 } /* int config */
153 static void counter_submit (const char *type, const char *type_instance,
154 counter_t value)
155 {
156 value_t values[1];
157 value_list_t vl = VALUE_LIST_INIT;
159 values[0].counter = value;
161 vl.values = values;
162 vl.values_len = 1;
163 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
164 sstrncpy (vl.plugin, "mysql", sizeof (vl.plugin));
165 sstrncpy (vl.type, type, sizeof (vl.type));
166 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
168 plugin_dispatch_values (&vl);
169 } /* void counter_submit */
171 static void qcache_submit (counter_t hits, counter_t inserts,
172 counter_t not_cached, counter_t lowmem_prunes,
173 gauge_t queries_in_cache)
174 {
175 value_t values[5];
176 value_list_t vl = VALUE_LIST_INIT;
178 values[0].counter = hits;
179 values[1].counter = inserts;
180 values[2].counter = not_cached;
181 values[3].counter = lowmem_prunes;
182 values[4].gauge = queries_in_cache;
184 vl.values = values;
185 vl.values_len = 5;
186 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
187 sstrncpy (vl.plugin, "mysql", sizeof (vl.plugin));
188 sstrncpy (vl.type, "mysql_qcache", sizeof (vl.type));
190 plugin_dispatch_values (&vl);
191 } /* void qcache_submit */
193 static void threads_submit (gauge_t running, gauge_t connected, gauge_t cached,
194 counter_t created)
195 {
196 value_t values[4];
197 value_list_t vl = VALUE_LIST_INIT;
199 values[0].gauge = running;
200 values[1].gauge = connected;
201 values[2].gauge = cached;
202 values[3].counter = created;
204 vl.values = values;
205 vl.values_len = 4;
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)
214 {
215 value_t values[2];
216 value_list_t vl = VALUE_LIST_INIT;
218 values[0].counter = rx;
219 values[1].counter = tx;
221 vl.values = values;
222 vl.values_len = 2;
223 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
224 sstrncpy (vl.plugin, "mysql", sizeof (vl.plugin));
225 sstrncpy (vl.type, "mysql_octets", sizeof (vl.type));
227 plugin_dispatch_values (&vl);
228 } /* void traffic_submit */
230 static int mysql_read (void)
231 {
232 MYSQL *con;
233 MYSQL_RES *res;
234 MYSQL_ROW row;
235 char *query;
236 int query_len;
237 int field_num;
239 unsigned long long qcache_hits = 0ULL;
240 unsigned long long qcache_inserts = 0ULL;
241 unsigned long long qcache_not_cached = 0ULL;
242 unsigned long long qcache_lowmem_prunes = 0ULL;
243 int qcache_queries_in_cache = -1;
245 int threads_running = -1;
246 int threads_connected = -1;
247 int threads_cached = -1;
248 unsigned long long threads_created = 0ULL;
250 unsigned long long traffic_incoming = 0ULL;
251 unsigned long long traffic_outgoing = 0ULL;
253 /* An error message will have been printed in this case */
254 if ((con = getconnection ()) == NULL)
255 return (-1);
257 query = "SHOW STATUS";
258 if (mysql_get_server_version (con) >= 50002)
259 query = "SHOW GLOBAL STATUS";
261 query_len = strlen (query);
263 if (mysql_real_query (con, query, query_len))
264 {
265 ERROR ("mysql_real_query failed: %s\n",
266 mysql_error (con));
267 return (-1);
268 }
270 if ((res = mysql_store_result (con)) == NULL)
271 {
272 ERROR ("mysql_store_result failed: %s\n",
273 mysql_error (con));
274 return (-1);
275 }
277 field_num = mysql_num_fields (res);
278 while ((row = mysql_fetch_row (res)))
279 {
280 char *key;
281 unsigned long long val;
283 key = row[0];
284 val = atoll (row[1]);
286 if (strncmp (key, "Com_", 4) == 0)
287 {
288 if (val == 0ULL)
289 continue;
291 /* Ignore `prepared statements' */
292 if (strncmp (key, "Com_stmt_", 9) != 0)
293 counter_submit ("mysql_commands", key + 4, val);
294 }
295 else if (strncmp (key, "Handler_", 8) == 0)
296 {
297 if (val == 0ULL)
298 continue;
300 counter_submit ("mysql_handler", key + 8, val);
301 }
302 else if (strncmp (key, "Qcache_", 7) == 0)
303 {
304 if (strcmp (key, "Qcache_hits") == 0)
305 qcache_hits = val;
306 else if (strcmp (key, "Qcache_inserts") == 0)
307 qcache_inserts = val;
308 else if (strcmp (key, "Qcache_not_cached") == 0)
309 qcache_not_cached = val;
310 else if (strcmp (key, "Qcache_lowmem_prunes") == 0)
311 qcache_lowmem_prunes = val;
312 else if (strcmp (key, "Qcache_queries_in_cache") == 0)
313 qcache_queries_in_cache = (int) val;
314 }
315 else if (strncmp (key, "Bytes_", 6) == 0)
316 {
317 if (strcmp (key, "Bytes_received") == 0)
318 traffic_incoming += val;
319 else if (strcmp (key, "Bytes_sent") == 0)
320 traffic_outgoing += val;
321 }
322 else if (strncmp (key, "Threads_", 8) == 0)
323 {
324 if (strcmp (key, "Threads_running") == 0)
325 threads_running = (int) val;
326 else if (strcmp (key, "Threads_connected") == 0)
327 threads_connected = (int) val;
328 else if (strcmp (key, "Threads_cached") == 0)
329 threads_cached = (int) val;
330 else if (strcmp (key, "Threads_created") == 0)
331 threads_created = val;
332 }
333 }
334 mysql_free_result (res); res = NULL;
336 if ((qcache_hits != 0ULL)
337 || (qcache_inserts != 0ULL)
338 || (qcache_not_cached != 0ULL)
339 || (qcache_lowmem_prunes != 0ULL))
340 qcache_submit (qcache_hits, qcache_inserts, qcache_not_cached,
341 qcache_lowmem_prunes, qcache_queries_in_cache);
343 if (threads_created != 0ULL)
344 threads_submit (threads_running, threads_connected,
345 threads_cached, threads_created);
347 traffic_submit (traffic_incoming, traffic_outgoing);
349 /* mysql_close (con); */
351 return (0);
352 } /* int mysql_read */
354 void module_register (void)
355 {
356 plugin_register_config ("mysql", config, config_keys, config_keys_num);
357 plugin_register_read ("mysql", mysql_read);
358 } /* void module_register */