1 /**
2 * collectd - src/mysql.c
3 * Copyright (C) 2006,2007 Florian octo Forster
4 *
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.
8 *
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.
13 *
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
17 *
18 * Authors:
19 * Florian octo Forster <octo at verplant.org>
20 **/
22 #include "collectd.h"
23 #include "common.h"
24 #include "plugin.h"
25 #include "configfile.h"
27 #ifdef HAVE_MYSQL_MYSQL_H
28 #include <mysql/mysql.h>
29 #endif
31 #if COLLECT_LIBMYSQL
32 # define MYSQL_HAVE_READ 1
33 #else
34 # define MYSQL_HAVE_READ 0
35 #endif
37 /* TODO: Understand `Select_*' and possibly do that stuff as well.. */
39 #if MYSQL_HAVE_READ
40 static const char *config_keys[] =
41 {
42 "Host",
43 "User",
44 "Password",
45 "Database",
46 NULL
47 };
48 static int config_keys_num = 4;
50 static char *host = "localhost";
51 static char *user;
52 static char *pass;
53 static char *db = NULL;
55 static MYSQL *getconnection (void)
56 {
57 static MYSQL *con;
58 static int state;
60 static int wait_for = 0;
61 static int wait_increase = 60;
63 if (state != 0)
64 {
65 int err;
66 if ((err = mysql_ping (con)) != 0)
67 {
68 WARNING ("mysql_ping failed: %s", mysql_error (con));
69 state = 0;
70 }
71 else
72 {
73 state = 1;
74 return (con);
75 }
76 }
78 if (wait_for > 0)
79 {
80 wait_for -= interval_g;
81 return (NULL);
82 }
84 wait_for = wait_increase;
85 wait_increase *= 2;
86 if (wait_increase > 86400)
87 wait_increase = 86400;
89 if ((con = mysql_init (con)) == NULL)
90 {
91 ERROR ("mysql_init failed: %s", mysql_error (con));
92 state = 0;
93 return (NULL);
94 }
96 if (mysql_real_connect (con, host, user, pass, db, 0, NULL, 0) == NULL)
97 {
98 ERROR ("mysql_real_connect failed: %s", mysql_error (con));
99 state = 0;
100 return (NULL);
101 }
102 else
103 {
104 state = 1;
105 wait_for = 0;
106 wait_increase = 60;
107 return (con);
108 }
109 } /* static MYSQL *getconnection (void) */
111 static int config (const char *key, const char *value)
112 {
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);
121 else
122 return (-1);
123 }
125 static void counter_submit (const char *type, const char *type_instance,
126 counter_t value)
127 {
128 value_t values[1];
129 value_list_t vl = VALUE_LIST_INIT;
131 values[0].counter = value;
133 vl.values = values;
134 vl.values_len = 1;
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)
146 {
147 value_t values[5];
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;
156 vl.values = values;
157 vl.values_len = 5;
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,
166 counter_t created)
167 {
168 value_t values[4];
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;
176 vl.values = values;
177 vl.values_len = 4;
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)
186 {
187 value_t values[2];
188 value_list_t vl = VALUE_LIST_INIT;
190 values[0].counter = rx;
191 values[1].counter = tx;
193 vl.values = values;
194 vl.values_len = 2;
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)
203 {
204 MYSQL *con;
205 MYSQL_RES *res;
206 MYSQL_ROW row;
207 char *query;
208 int query_len;
209 int field_num;
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)
227 return (-1);
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))
236 {
237 ERROR ("mysql_real_query failed: %s\n",
238 mysql_error (con));
239 return (-1);
240 }
242 if ((res = mysql_store_result (con)) == NULL)
243 {
244 ERROR ("mysql_store_result failed: %s\n",
245 mysql_error (con));
246 return (-1);
247 }
249 field_num = mysql_num_fields (res);
250 while ((row = mysql_fetch_row (res)))
251 {
252 char *key;
253 unsigned long long val;
255 key = row[0];
256 val = atoll (row[1]);
258 if (strncmp (key, "Com_", 4) == 0)
259 {
260 if (val == 0ULL)
261 continue;
263 /* Ignore `prepared statements' */
264 if (strncmp (key, "Com_stmt_", 9) != 0)
265 counter_submit ("mysql_commands", key + 4, val);
266 }
267 else if (strncmp (key, "Handler_", 8) == 0)
268 {
269 if (val == 0ULL)
270 continue;
272 counter_submit ("mysql_handler", key + 8, val);
273 }
274 else if (strncmp (key, "Qcache_", 7) == 0)
275 {
276 if (strcmp (key, "Qcache_hits") == 0)
277 qcache_hits = val;
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;
286 }
287 else if (strncmp (key, "Bytes_", 6) == 0)
288 {
289 if (strcmp (key, "Bytes_received") == 0)
290 traffic_incoming += val;
291 else if (strcmp (key, "Bytes_sent") == 0)
292 traffic_outgoing += val;
293 }
294 else if (strncmp (key, "Threads_", 8) == 0)
295 {
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;
304 }
305 }
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); */
323 return (0);
324 } /* int mysql_read */
325 #endif /* MYSQL_HAVE_READ */
327 void module_register (void)
328 {
329 #if MYSQL_HAVE_READ
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 */