e83c5441662e1f5ab084fe28241ace8601eb5f27
1 /**
2 * collectd - src/mysql.c
3 * Copyright (C) 2005 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; either version 2 of the License, or (at your
8 * option) any later version.
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 **/
23 #include "collectd.h"
24 #include "common.h"
25 #include "plugin.h"
26 #include "configfile.h"
28 #ifdef HAVE_MYSQL_MYSQL_H
29 #include <mysql/mysql.h>
30 #endif
32 #define MODULE_NAME "mysql"
34 #if COLLECT_LIBMYSQL
35 # define MYSQL_HAVE_READ 1
36 #else
37 # define MYSQL_HAVE_READ 0
38 #endif
40 #define BUFSIZE 512
42 static char *host = "localhost";
43 static char *user;
44 static char *pass;
45 static char *db = NULL;
47 static char init_suceeded = 0;
49 static char *commands_file = "mysql/mysql_commands-%s.rrd";
50 static char *traffic_file = "traffic-mysql.rrd";
52 static char *commands_ds_def[] =
53 {
54 "DS:value:COUNTER:25:0:U",
55 NULL
56 };
57 static int commands_ds_num = 1;
59 static char *traffic_ds_def[] =
60 {
61 "DS:incoming:COUNTER:25:0:U",
62 "DS:outgoing:COUNTER:25:0:U",
63 NULL
64 };
65 static int traffic_ds_num = 2;
67 static char *config_keys[] =
68 {
69 "Host",
70 "User",
71 "Password",
72 "Database",
73 NULL
74 };
75 static int config_keys_num = 4;
77 #if MYSQL_HAVE_READ
78 static MYSQL *getconnection (void)
79 {
80 static MYSQL *con;
81 static int state;
83 if (state != 0)
84 {
85 int err;
86 if ((err = mysql_ping (con)) != 0)
87 {
88 syslog (LOG_WARNING, "mysql_ping failed: %s", mysql_error (con));
89 state = 0;
90 }
91 else
92 {
93 state = 1;
94 return (con);
95 }
96 }
98 if ((con = mysql_init (con)) == NULL)
99 {
100 syslog (LOG_ERR, "mysql_init failed: %s", mysql_error (con));
101 state = 0;
102 return (NULL);
103 }
105 if (mysql_real_connect (con, host, user, pass, db, 0, NULL, 0) == NULL)
106 {
107 syslog (LOG_ERR, "mysql_real_connect failed: %s", mysql_error (con));
108 state = 0;
109 return (NULL);
110 }
111 else
112 {
113 state = 1;
114 return (con);
115 }
116 } /* static MYSQL *getconnection (void) */
117 #endif /* MYSQL_HAVE_READ */
119 static void init (void)
120 {
121 #if MYSQL_HAVE_READ
122 if (getconnection () != NULL)
123 init_suceeded = 1;
124 else
125 {
126 syslog (LOG_ERR, "The `mysql' plugin will be disabled because `init' failed to connect to `%s'", host);
127 init_suceeded = 0;
128 }
129 #endif /* MYSQL_HAVE_READ */
131 return;
132 }
134 static int config (char *key, char *value)
135 {
136 if (strcasecmp (key, "host") == 0)
137 return ((host = strdup (value)) == NULL ? 1 : 0);
138 else if (strcasecmp (key, "user") == 0)
139 return ((user = strdup (value)) == NULL ? 1 : 0);
140 else if (strcasecmp (key, "password") == 0)
141 return ((pass = strdup (value)) == NULL ? 1 : 0);
142 else if (strcasecmp (key, "database") == 0)
143 return ((db = strdup (value)) == NULL ? 1 : 0);
144 else
145 return (-1);
146 }
148 static void commands_write (char *host, char *inst, char *val)
149 {
150 char buf[BUFSIZE];
152 if (snprintf (buf, BUFSIZE, commands_file, inst) >= BUFSIZE)
153 return;
155 rrd_update_file (host, buf, val, commands_ds_def, commands_ds_num);
156 }
158 static void traffic_write (char *host, char *inst, char *val)
159 {
160 rrd_update_file (host, traffic_file, val, traffic_ds_def, traffic_ds_num);
161 }
163 #if MYSQL_HAVE_READ
164 static void commands_submit (char *inst, unsigned long long value)
165 {
166 char buf[BUFSIZE];
167 int status;
169 status = snprintf (buf, BUFSIZE, "%u:%llu", (unsigned int) curtime, value);
171 if (status < 0)
172 {
173 syslog (LOG_ERR, "snprintf failed");
174 return;
175 }
176 else if (status >= BUFSIZE)
177 {
178 syslog (LOG_WARNING, "snprintf was truncated");
179 return;
180 }
182 plugin_submit ("mysql_commands", inst, buf);
183 }
185 static void traffic_submit (unsigned long long incoming,
186 unsigned long long outgoing)
187 {
188 char buf[BUFSIZE];
189 int status;
191 status = snprintf (buf, BUFSIZE, "%u:%llu:%llu", (unsigned int) curtime,
192 incoming, outgoing);
194 if (status < 0)
195 {
196 syslog (LOG_ERR, "snprintf failed");
197 return;
198 }
199 else if (status >= BUFSIZE)
200 {
201 syslog (LOG_WARNING, "snprintf was truncated");
202 return;
203 }
205 plugin_submit ("mysql_traffic", "-", buf);
206 }
208 static void mysql_read (void)
209 {
210 MYSQL *con;
211 MYSQL_RES *res;
212 MYSQL_ROW row;
213 char *query;
214 int query_len;
215 int field_num;
217 unsigned long long traffic_incoming = 0LL;
218 unsigned long long traffic_outgoing = 0LL;
220 if (init_suceeded == 0)
221 return;
223 /* An error message will have been printed in this case */
224 if ((con = getconnection ()) == NULL)
225 return;
227 query = "SHOW STATUS";
228 query_len = strlen (query);
230 if (mysql_real_query (con, query, query_len))
231 {
232 syslog (LOG_ERR, "mysql_real_query failed: %s\n", mysql_error (con));
233 return;
234 }
236 if ((res = mysql_store_result (con)) == NULL)
237 {
238 syslog (LOG_ERR, "mysql_store_result failed: %s\n", mysql_error (con));
239 return;
240 }
242 field_num = mysql_num_fields (res);
243 while ((row = mysql_fetch_row (res)))
244 {
245 char *key;
246 unsigned long long val;
248 key = row[0];
249 val = atoll (row[1]);
251 if (strncmp (key, "Com_", 4) == 0)
252 {
253 /* Ignore `prepared statements' */
254 if (strncmp (key, "Com_stmt_", 9) != 0)
255 commands_submit (key + 4, val);
256 }
257 else if (strncmp (key, "Bytes_", 6) == 0)
258 {
259 if (strcmp (key, "Bytes_received") == 0)
260 traffic_incoming += val;
261 else if (strcmp (key, "Bytes_sent") == 0)
262 traffic_outgoing += val;
263 }
264 }
265 mysql_free_result (res); res = NULL;
267 traffic_submit (traffic_incoming, traffic_outgoing);
269 /* mysql_close (con); */
271 return;
272 }
273 #else
274 # define mysql_read NULL
275 #endif /* MYSQL_HAVE_READ */
277 void module_register (void)
278 {
279 plugin_register (MODULE_NAME, init, mysql_read, NULL);
280 plugin_register ("mysql_commands", NULL, NULL, commands_write);
281 plugin_register ("mysql_traffic", NULL, NULL, traffic_write);
282 cf_register (MODULE_NAME, config, config_keys, config_keys_num);
283 }
285 #undef BUFSIZE
286 #undef MODULE_NAME