Changed default `host' for `mysql' to `localhost'
[collectd.git] / src / mysql.c
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  **/
22
23 #include "collectd.h"
24 #include "common.h"
25 #include "plugin.h"
26 #include "configfile.h"
27
28 #ifdef HAVE_MYSQL_MYSQL_H
29 #include <mysql/mysql.h>
30 #endif
31
32 #define MODULE_NAME "mysql"
33
34 #if COLLECT_LIBMYSQL
35 # define MYSQL_HAVE_READ 1
36 #else
37 # define MYSQL_HAVE_READ 0
38 #endif
39
40 #define BUFSIZE 512
41
42 static char *host = "localhost";
43 static char *user;
44 static char *pass;
45 static char *db = NULL;
46
47 static char  init_suceeded = 0;
48
49 static char *commands_file = "mysql_commands.rrd";
50 static char *traffic_file  = "mysql_traffic.rrd";
51
52 static char *commands_ds_def[] =
53 {
54         "DS:insert:COUNTER:25:0:U",
55         "DS:select:COUNTER:25:0:U",
56         "DS:show:COUNTER:25:0:U",
57         "DS:update:COUNTER:25:0:U",
58         "DS:other:COUNTER:25:0:U",
59         NULL
60 };
61 static int commands_ds_num = 5;
62
63 static char *traffic_ds_def[] =
64 {
65         "DS:incoming:COUNTER:25:0:U",
66         "DS:outgoing:COUNTER:25:0:U",
67         NULL
68 };
69 static int traffic_ds_num = 2;
70
71 static char *config_keys[] =
72 {
73         "Host",
74         "User",
75         "Password",
76         "Database",
77         NULL
78 };
79 static int config_keys_num = 4;
80
81 #if MYSQL_HAVE_READ
82 static MYSQL *getconnection (void)
83 {
84         static MYSQL *con;
85         static int    state;
86
87         if (state != 0)
88         {
89                 int err;
90                 if ((err = mysql_ping (con)) != 0)
91                 {
92                         syslog (LOG_WARNING, "mysql_ping failed: %s", mysql_error (con));
93                         state = 0;
94                 }
95                 else
96                 {
97                         state = 1;
98                         return (con);
99                 }
100         }
101
102         if ((con = mysql_init (con)) == NULL)
103         {
104                 syslog (LOG_ERR, "mysql_init failed: %s", mysql_error (con));
105                 state = 0;
106                 return (NULL);
107         }
108
109         if (mysql_real_connect (con, host, user, pass, db, 0, NULL, 0) == NULL)
110         {
111                 syslog (LOG_ERR, "mysql_real_connect failed: %s", mysql_error (con));
112                 state = 0;
113                 return (NULL);
114         }
115         else
116         {
117                 state = 1;
118                 return (con);
119         }
120 } /* static MYSQL *getconnection (void) */
121 #endif /* MYSQL_HAVE_READ */
122
123 static void init (void)
124 {
125 #if MYSQL_HAVE_READ
126         if (getconnection () != NULL)
127                 init_suceeded = 1;
128         else
129         {
130                 syslog (LOG_ERR, "The `mysql' plugin will be disabled because `init' failed to connect to `%s'", host);
131                 init_suceeded = 0;
132         }
133 #endif /* MYSQL_HAVE_READ */
134
135         return;
136 }
137
138 static int config (char *key, char *value)
139 {
140         if (strcasecmp (key, "host") == 0)
141                 return ((host = strdup (value)) == NULL ? 1 : 0);
142         else if (strcasecmp (key, "user") == 0)
143                 return ((user = strdup (value)) == NULL ? 1 : 0);
144         else if (strcasecmp (key, "password") == 0)
145                 return ((pass = strdup (value)) == NULL ? 1 : 0);
146         else if (strcasecmp (key, "database") == 0)
147                 return ((db = strdup (value)) == NULL ? 1 : 0);
148         else
149                 return (-1);
150 }
151
152 static void commands_write (char *host, char *inst, char *val)
153 {
154         rrd_update_file (host, commands_file, val, commands_ds_def, commands_ds_num);
155 }
156
157 static void traffic_write (char *host, char *inst, char *val)
158 {
159         rrd_update_file (host, traffic_file, val, traffic_ds_def, traffic_ds_num);
160 }
161
162 #if MYSQL_HAVE_READ
163 static void commands_submit (unsigned long long insert,
164                 unsigned long long select,
165                 unsigned long long show,
166                 unsigned long long update,
167                 unsigned long long other)
168 {
169         char buf[BUFSIZE];
170         int  status;
171
172         status = snprintf (buf, BUFSIZE, "%u:%llu:%llu:%llu:%llu:%llu", (unsigned int) curtime,
173                         insert, select, show, update, other);
174
175         if (status < 0)
176         {
177                 syslog (LOG_ERR, "snprintf failed");
178                 return;
179         }
180         else if (status >= BUFSIZE)
181         {
182                 syslog (LOG_WARNING, "snprintf was truncated");
183                 return;
184         }
185
186         plugin_submit ("mysql_commands", "-", buf);
187 }
188
189 static void traffic_submit (unsigned long long incoming,
190                 unsigned long long outgoing)
191 {
192         char buf[BUFSIZE];
193         int  status;
194
195         status = snprintf (buf, BUFSIZE, "%u:%llu:%llu", (unsigned int) curtime,
196                         incoming, outgoing);
197
198         if (status < 0)
199         {
200                 syslog (LOG_ERR, "snprintf failed");
201                 return;
202         }
203         else if (status >= BUFSIZE)
204         {
205                 syslog (LOG_WARNING, "snprintf was truncated");
206                 return;
207         }
208
209         plugin_submit ("mysql_traffic", "-", buf);
210 }
211
212 static void mysql_read (void)
213 {
214         MYSQL     *con;
215         MYSQL_RES *res;
216         MYSQL_ROW  row;
217         char      *query;
218         int        query_len;
219         int        field_num;
220
221         unsigned long long cmd_insert = 0LL;
222         unsigned long long cmd_select = 0LL;
223         unsigned long long cmd_show   = 0LL;
224         unsigned long long cmd_update = 0LL;
225         unsigned long long cmd_other  = 0LL;
226
227         unsigned long long traffic_incoming = 0LL;
228         unsigned long long traffic_outgoing = 0LL;
229
230         if (init_suceeded == 0)
231                 return;
232
233         /* An error message will have been printed in this case */
234         if ((con = getconnection ()) == NULL)
235                 return;
236
237         query = "SHOW STATUS";
238         query_len = strlen (query);
239
240         if (mysql_real_query (con, query, query_len))
241         {
242                 syslog (LOG_ERR, "mysql_real_query failed: %s\n", mysql_error (con));
243                 return;
244         }
245
246         if ((res = mysql_store_result (con)) == NULL)
247         {
248                 syslog (LOG_ERR, "mysql_store_result failed: %s\n", mysql_error (con));
249                 return;
250         }
251
252         field_num = mysql_num_fields (res);
253         while ((row = mysql_fetch_row (res)))
254         {
255                 char *key;
256                 unsigned long long val;
257
258                 key = row[0];
259                 val = atoll (row[1]);
260
261                 if (strncmp (key, "Com_", 4) == 0)
262                 {
263                         if (strncmp (key, "Com_insert", 10) == 0)
264                                 cmd_insert += val;
265                         else if (strncmp (key, "Com_select", 10) == 0)
266                                 cmd_select += val;
267                         else if (strncmp (key, "Com_show", 8) == 0)
268                                 cmd_show += val;
269                         else if (strncmp (key, "Com_update", 10) == 0)
270                                 cmd_update += val;
271                         else if (strncmp (key, "Com_stmt_", 9) == 0)
272                         { /* do nothing */ }
273                         else
274                                 cmd_other += val;
275                 }
276                 else if (strncmp (key, "Bytes_", 6) == 0)
277                 {
278                         if (strncmp (key, "Bytes_received", 14) == 0)
279                                 traffic_incoming += val;
280                         else if (strncmp (key, "Bytes_sent", 10) == 0)
281                                 traffic_outgoing += val;
282                 }
283         }
284         mysql_free_result (res); res = NULL;
285
286         commands_submit (cmd_insert, cmd_select, cmd_show, cmd_update, cmd_other);
287         traffic_submit  (traffic_incoming, traffic_outgoing);
288
289         /* mysql_close (con); */
290
291         return;
292 }
293 #else
294 # define mysql_read NULL
295 #endif /* MYSQL_HAVE_READ */
296
297 void module_register (void)
298 {
299         plugin_register (MODULE_NAME, init, mysql_read, NULL);
300         plugin_register ("mysql_commands", NULL, NULL, commands_write);
301         plugin_register ("mysql_traffic", NULL, NULL, traffic_write);
302         cf_register (MODULE_NAME, config, config_keys, config_keys_num);
303 }
304
305 #undef BUFSIZE
306 #undef MODULE_NAME