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_H
28 #include <mysql.h>
29 #elif defined(HAVE_MYSQL_MYSQL_H)
30 #include <mysql/mysql.h>
31 #endif
33 /* TODO: Understand `Select_*' and possibly do that stuff as well.. */
35 static const char *config_keys[] =
36 {
37 "Host",
38 "User",
39 "Password",
40 "Database",
41 "Port",
42 "Socket",
43 NULL
44 };
45 static int config_keys_num = 6;
47 static char *host = "localhost";
48 static char *user;
49 static char *pass;
50 static char *db = NULL;
51 static char *socket = NULL;
52 static int port = 0;
54 static MYSQL *getconnection (void)
55 {
56 static MYSQL *con;
57 static int state;
59 static int wait_for = 0;
60 static int wait_increase = 60;
62 if (state != 0)
63 {
64 int err;
65 if ((err = mysql_ping (con)) != 0)
66 {
67 WARNING ("mysql_ping failed: %s", mysql_error (con));
68 state = 0;
69 }
70 else
71 {
72 state = 1;
73 return (con);
74 }
75 }
77 if (wait_for > 0)
78 {
79 wait_for -= interval_g;
80 return (NULL);
81 }
83 wait_for = wait_increase;
84 wait_increase *= 2;
85 if (wait_increase > 86400)
86 wait_increase = 86400;
88 if ((con = mysql_init (con)) == NULL)
89 {
90 ERROR ("mysql_init failed: %s", mysql_error (con));
91 state = 0;
92 return (NULL);
93 }
95 if (mysql_real_connect (con, host, user, pass, db, port, socket, 0) == NULL)
96 {
97 ERROR ("mysql_real_connect failed: %s", mysql_error (con));
98 state = 0;
99 return (NULL);
100 }
101 else
102 {
103 state = 1;
104 wait_for = 0;
105 wait_increase = 60;
106 return (con);
107 }
108 } /* static MYSQL *getconnection (void) */
110 static int config (const char *key, const char *value)
111 {
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)
123 return ((port = atoi (value)) == NULL ? 1 : 0);
124 else
125 return (-1);
126 }
128 static void counter_submit (const char *type, const char *type_instance,
129 counter_t value)
130 {
131 value_t values[1];
132 value_list_t vl = VALUE_LIST_INIT;
134 values[0].counter = value;
136 vl.values = values;
137 vl.values_len = 1;
138 vl.time = time (NULL);
139 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
140 sstrncpy (vl.plugin, "mysql", sizeof (vl.plugin));
141 sstrncpy (vl.type, type, sizeof (vl.type));
142 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
144 plugin_dispatch_values (&vl);
145 } /* void counter_submit */
147 static void qcache_submit (counter_t hits, counter_t inserts,
148 counter_t not_cached, counter_t lowmem_prunes,
149 gauge_t queries_in_cache)
150 {
151 value_t values[5];
152 value_list_t vl = VALUE_LIST_INIT;
154 values[0].counter = hits;
155 values[1].counter = inserts;
156 values[2].counter = not_cached;
157 values[3].counter = lowmem_prunes;
158 values[4].gauge = queries_in_cache;
160 vl.values = values;
161 vl.values_len = 5;
162 vl.time = time (NULL);
163 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
164 sstrncpy (vl.plugin, "mysql", sizeof (vl.plugin));
165 sstrncpy (vl.type, "mysql_qcache", sizeof (vl.type));
167 plugin_dispatch_values (&vl);
168 } /* void qcache_submit */
170 static void threads_submit (gauge_t running, gauge_t connected, gauge_t cached,
171 counter_t created)
172 {
173 value_t values[4];
174 value_list_t vl = VALUE_LIST_INIT;
176 values[0].gauge = running;
177 values[1].gauge = connected;
178 values[2].gauge = cached;
179 values[3].counter = created;
181 vl.values = values;
182 vl.values_len = 4;
183 vl.time = time (NULL);
184 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
185 sstrncpy (vl.plugin, "mysql", sizeof (vl.plugin));
186 sstrncpy (vl.type, "mysql_threads", sizeof (vl.type));
188 plugin_dispatch_values (&vl);
189 } /* void threads_submit */
191 static void traffic_submit (counter_t rx, counter_t tx)
192 {
193 value_t values[2];
194 value_list_t vl = VALUE_LIST_INIT;
196 values[0].counter = rx;
197 values[1].counter = tx;
199 vl.values = values;
200 vl.values_len = 2;
201 vl.time = time (NULL);
202 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
203 sstrncpy (vl.plugin, "mysql", sizeof (vl.plugin));
204 sstrncpy (vl.type, "mysql_octets", sizeof (vl.type));
206 plugin_dispatch_values (&vl);
207 } /* void traffic_submit */
209 static int mysql_read (void)
210 {
211 MYSQL *con;
212 MYSQL_RES *res;
213 MYSQL_ROW row;
214 char *query;
215 int query_len;
216 int field_num;
218 unsigned long long qcache_hits = 0ULL;
219 unsigned long long qcache_inserts = 0ULL;
220 unsigned long long qcache_not_cached = 0ULL;
221 unsigned long long qcache_lowmem_prunes = 0ULL;
222 int qcache_queries_in_cache = -1;
224 int threads_running = -1;
225 int threads_connected = -1;
226 int threads_cached = -1;
227 unsigned long long threads_created = 0ULL;
229 unsigned long long traffic_incoming = 0ULL;
230 unsigned long long traffic_outgoing = 0ULL;
232 /* An error message will have been printed in this case */
233 if ((con = getconnection ()) == NULL)
234 return (-1);
236 query = "SHOW STATUS";
237 if (mysql_get_server_version (con) >= 50002)
238 query = "SHOW GLOBAL STATUS";
240 query_len = strlen (query);
242 if (mysql_real_query (con, query, query_len))
243 {
244 ERROR ("mysql_real_query failed: %s\n",
245 mysql_error (con));
246 return (-1);
247 }
249 if ((res = mysql_store_result (con)) == NULL)
250 {
251 ERROR ("mysql_store_result failed: %s\n",
252 mysql_error (con));
253 return (-1);
254 }
256 field_num = mysql_num_fields (res);
257 while ((row = mysql_fetch_row (res)))
258 {
259 char *key;
260 unsigned long long val;
262 key = row[0];
263 val = atoll (row[1]);
265 if (strncmp (key, "Com_", 4) == 0)
266 {
267 if (val == 0ULL)
268 continue;
270 /* Ignore `prepared statements' */
271 if (strncmp (key, "Com_stmt_", 9) != 0)
272 counter_submit ("mysql_commands", key + 4, val);
273 }
274 else if (strncmp (key, "Handler_", 8) == 0)
275 {
276 if (val == 0ULL)
277 continue;
279 counter_submit ("mysql_handler", key + 8, val);
280 }
281 else if (strncmp (key, "Qcache_", 7) == 0)
282 {
283 if (strcmp (key, "Qcache_hits") == 0)
284 qcache_hits = val;
285 else if (strcmp (key, "Qcache_inserts") == 0)
286 qcache_inserts = val;
287 else if (strcmp (key, "Qcache_not_cached") == 0)
288 qcache_not_cached = val;
289 else if (strcmp (key, "Qcache_lowmem_prunes") == 0)
290 qcache_lowmem_prunes = val;
291 else if (strcmp (key, "Qcache_queries_in_cache") == 0)
292 qcache_queries_in_cache = (int) val;
293 }
294 else if (strncmp (key, "Bytes_", 6) == 0)
295 {
296 if (strcmp (key, "Bytes_received") == 0)
297 traffic_incoming += val;
298 else if (strcmp (key, "Bytes_sent") == 0)
299 traffic_outgoing += val;
300 }
301 else if (strncmp (key, "Threads_", 8) == 0)
302 {
303 if (strcmp (key, "Threads_running") == 0)
304 threads_running = (int) val;
305 else if (strcmp (key, "Threads_connected") == 0)
306 threads_connected = (int) val;
307 else if (strcmp (key, "Threads_cached") == 0)
308 threads_cached = (int) val;
309 else if (strcmp (key, "Threads_created") == 0)
310 threads_created = val;
311 }
312 }
313 mysql_free_result (res); res = NULL;
315 if ((qcache_hits != 0ULL)
316 || (qcache_inserts != 0ULL)
317 || (qcache_not_cached != 0ULL)
318 || (qcache_lowmem_prunes != 0ULL))
319 qcache_submit (qcache_hits, qcache_inserts, qcache_not_cached,
320 qcache_lowmem_prunes, qcache_queries_in_cache);
322 if (threads_created != 0ULL)
323 threads_submit (threads_running, threads_connected,
324 threads_cached, threads_created);
326 traffic_submit (traffic_incoming, traffic_outgoing);
328 /* mysql_close (con); */
330 return (0);
331 } /* int mysql_read */
333 void module_register (void)
334 {
335 plugin_register_config ("mysql", config, config_keys, config_keys_num);
336 plugin_register_read ("mysql", mysql_read);
337 } /* void module_register */