Plugins using libcurl: Enable the ‘CURLOPT_FOLLOWLOCATION’ option.
[collectd.git] / src / apache.c
1 /**
2  * collectd - src/apache.c
3  * Copyright (C) 2006-2008  Florian octo Forster
4  * Copyright (C) 2007  Florent EppO Monbillard
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  *   Florent EppO Monbillard <eppo at darox.net>
22  *   - connections/lighttpd extension
23  **/
24
25 #include "collectd.h"
26 #include "common.h"
27 #include "plugin.h"
28 #include "configfile.h"
29
30 #include <curl/curl.h>
31
32 static char *url         = NULL;
33 static char *user        = NULL;
34 static char *pass        = NULL;
35 static char *verify_peer = NULL;
36 static char *verify_host = NULL;
37 static char *cacert      = NULL;
38
39 static CURL *curl = NULL;
40
41 static char  *apache_buffer = NULL;
42 static size_t apache_buffer_size = 0;
43 static size_t apache_buffer_fill = 0;
44 static char   apache_curl_error[CURL_ERROR_SIZE];
45
46 static const char *config_keys[] =
47 {
48         "URL",
49         "User",
50         "Password",
51         "VerifyPeer",
52         "VerifyHost",
53         "CACert"
54 };
55 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
56
57 static size_t apache_curl_callback (void *buf, size_t size, size_t nmemb,
58                 void __attribute__((unused)) *stream)
59 {
60         size_t len = size * nmemb;
61
62         if (len <= 0)
63                 return (len);
64
65         if ((apache_buffer_fill + len) >= apache_buffer_size)
66         {
67                 char *temp;
68
69                 temp = (char *) realloc (apache_buffer,
70                                 apache_buffer_fill + len + 1);
71                 if (temp == NULL)
72                 {
73                         ERROR ("apache plugin: realloc failed.");
74                         return (0);
75                 }
76                 apache_buffer = temp;
77                 apache_buffer_size = apache_buffer_fill + len + 1;
78         }
79
80         memcpy (apache_buffer + apache_buffer_fill, (char *) buf, len);
81         apache_buffer_fill += len;
82         apache_buffer[apache_buffer_fill] = 0;
83
84         return (len);
85 }
86
87 static int config_set (char **var, const char *value)
88 {
89         if (*var != NULL)
90         {
91                 free (*var);
92                 *var = NULL;
93         }
94
95         if ((*var = strdup (value)) == NULL)
96                 return (1);
97         else
98                 return (0);
99 }
100
101 static int config (const char *key, const char *value)
102 {
103         if (strcasecmp (key, "url") == 0)
104                 return (config_set (&url, value));
105         else if (strcasecmp (key, "user") == 0)
106                 return (config_set (&user, value));
107         else if (strcasecmp (key, "password") == 0)
108                 return (config_set (&pass, value));
109         else if (strcasecmp (key, "verifypeer") == 0)
110                 return (config_set (&verify_peer, value));
111         else if (strcasecmp (key, "verifyhost") == 0)
112                 return (config_set (&verify_host, value));
113         else if (strcasecmp (key, "cacert") == 0)
114                 return (config_set (&cacert, value));
115         else
116                 return (-1);
117 }
118
119 static int init (void)
120 {
121         static char credentials[1024];
122
123         if (url == NULL)
124         {
125                 WARNING ("apache plugin: init: No URL configured, returning "
126                                 "an error.");
127                 return (-1);
128         }
129
130         if (curl != NULL)
131         {
132                 curl_easy_cleanup (curl);
133         }
134
135         if ((curl = curl_easy_init ()) == NULL)
136         {
137                 ERROR ("apache plugin: init: `curl_easy_init' failed.");
138                 return (-1);
139         }
140
141         curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, apache_curl_callback);
142         curl_easy_setopt (curl, CURLOPT_USERAGENT, PACKAGE_NAME"/"PACKAGE_VERSION);
143         curl_easy_setopt (curl, CURLOPT_ERRORBUFFER, apache_curl_error);
144
145         if (user != NULL)
146         {
147                 int status;
148
149                 status = ssnprintf (credentials, sizeof (credentials), "%s:%s",
150                                 user, (pass == NULL) ? "" : pass);
151                 if ((status < 0) || ((size_t) status >= sizeof (credentials)))
152                 {
153                         ERROR ("apache plugin: init: Returning an error "
154                                         "because the credentials have been "
155                                         "truncated.");
156                         return (-1);
157                 }
158
159                 curl_easy_setopt (curl, CURLOPT_USERPWD, credentials);
160         }
161
162         curl_easy_setopt (curl, CURLOPT_URL, url);
163         curl_easy_setopt (curl, CURLOPT_FOLLOWLOCATION, 1);
164
165         if ((verify_peer == NULL) || (strcmp (verify_peer, "true") == 0))
166         {
167                 curl_easy_setopt (curl, CURLOPT_SSL_VERIFYPEER, 1);
168         }
169         else
170         {
171                 curl_easy_setopt (curl, CURLOPT_SSL_VERIFYPEER, 0);
172         }
173
174         if ((verify_host == NULL) || (strcmp (verify_host, "true") == 0))
175         {
176                 curl_easy_setopt (curl, CURLOPT_SSL_VERIFYHOST, 2);
177         }
178         else
179         {
180                 curl_easy_setopt (curl, CURLOPT_SSL_VERIFYHOST, 0);
181         }
182
183         if (cacert != NULL)
184         {
185                 curl_easy_setopt (curl, CURLOPT_CAINFO, cacert);
186         }
187
188         return (0);
189 } /* int init */
190
191 static void submit_counter (const char *type, const char *type_instance,
192                 counter_t value)
193 {
194         value_t values[1];
195         value_list_t vl = VALUE_LIST_INIT;
196
197         values[0].counter = value;
198
199         vl.values = values;
200         vl.values_len = 1;
201         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
202         sstrncpy (vl.plugin, "apache", sizeof (vl.plugin));
203         sstrncpy (vl.plugin_instance, "", sizeof (vl.plugin_instance));
204         sstrncpy (vl.type, type, sizeof (vl.type));
205
206         if (type_instance != NULL)
207                 sstrncpy (vl.type_instance, type_instance,
208                                 sizeof (vl.type_instance));
209
210         plugin_dispatch_values (&vl);
211 } /* void submit_counter */
212
213 static void submit_gauge (const char *type, const char *type_instance,
214                 gauge_t value)
215 {
216         value_t values[1];
217         value_list_t vl = VALUE_LIST_INIT;
218
219         values[0].gauge = value;
220
221         vl.values = values;
222         vl.values_len = 1;
223         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
224         sstrncpy (vl.plugin, "apache", sizeof (vl.plugin));
225         sstrncpy (vl.plugin_instance, "", sizeof (vl.plugin_instance));
226         sstrncpy (vl.type, type, sizeof (vl.type));
227
228         if (type_instance != NULL)
229                 sstrncpy (vl.type_instance, type_instance,
230                                 sizeof (vl.type_instance));
231
232         plugin_dispatch_values (&vl);
233 } /* void submit_counter */
234
235 static void submit_scoreboard (char *buf)
236 {
237         /*
238          * Scoreboard Key:
239          * "_" Waiting for Connection, "S" Starting up, "R" Reading Request,
240          * "W" Sending Reply, "K" Keepalive (read), "D" DNS Lookup,
241          * "C" Closing connection, "L" Logging, "G" Gracefully finishing,
242          * "I" Idle cleanup of worker, "." Open slot with no current process
243          */
244         long long open      = 0LL;
245         long long waiting   = 0LL;
246         long long starting  = 0LL;
247         long long reading   = 0LL;
248         long long sending   = 0LL;
249         long long keepalive = 0LL;
250         long long dnslookup = 0LL;
251         long long closing   = 0LL;
252         long long logging   = 0LL;
253         long long finishing = 0LL;
254         long long idle_cleanup = 0LL;
255
256         int i;
257
258         for (i = 0; buf[i] != '\0'; i++)
259         {
260                 if (buf[i] == '.') open++;
261                 else if (buf[i] == '_') waiting++;
262                 else if (buf[i] == 'S') starting++;
263                 else if (buf[i] == 'R') reading++;
264                 else if (buf[i] == 'W') sending++;
265                 else if (buf[i] == 'K') keepalive++;
266                 else if (buf[i] == 'D') dnslookup++;
267                 else if (buf[i] == 'C') closing++;
268                 else if (buf[i] == 'L') logging++;
269                 else if (buf[i] == 'G') finishing++;
270                 else if (buf[i] == 'I') idle_cleanup++;
271         }
272
273         submit_gauge ("apache_scoreboard", "open"     , open);
274         submit_gauge ("apache_scoreboard", "waiting"  , waiting);
275         submit_gauge ("apache_scoreboard", "starting" , starting);
276         submit_gauge ("apache_scoreboard", "reading"  , reading);
277         submit_gauge ("apache_scoreboard", "sending"  , sending);
278         submit_gauge ("apache_scoreboard", "keepalive", keepalive);
279         submit_gauge ("apache_scoreboard", "dnslookup", dnslookup);
280         submit_gauge ("apache_scoreboard", "closing"  , closing);
281         submit_gauge ("apache_scoreboard", "logging"  , logging);
282         submit_gauge ("apache_scoreboard", "finishing", finishing);
283         submit_gauge ("apache_scoreboard", "idle_cleanup", idle_cleanup);
284 }
285
286 static int apache_read (void)
287 {
288         int i;
289
290         char *ptr;
291         char *saveptr;
292         char *lines[16];
293         int   lines_num = 0;
294
295         char *fields[4];
296         int   fields_num;
297
298         if (curl == NULL)
299                 return (-1);
300         if (url == NULL)
301                 return (-1);
302
303         apache_buffer_fill = 0;
304         if (curl_easy_perform (curl) != 0)
305         {
306                 ERROR ("apache: curl_easy_perform failed: %s",
307                                 apache_curl_error);
308                 return (-1);
309         }
310
311         ptr = apache_buffer;
312         saveptr = NULL;
313         while ((lines[lines_num] = strtok_r (ptr, "\n\r", &saveptr)) != NULL)
314         {
315                 ptr = NULL;
316                 lines_num++;
317
318                 if (lines_num >= 16)
319                         break;
320         }
321
322         for (i = 0; i < lines_num; i++)
323         {
324                 fields_num = strsplit (lines[i], fields, 4);
325
326                 if (fields_num == 3)
327                 {
328                         if ((strcmp (fields[0], "Total") == 0)
329                                         && (strcmp (fields[1], "Accesses:") == 0))
330                                 submit_counter ("apache_requests", "",
331                                                 atoll (fields[2]));
332                         else if ((strcmp (fields[0], "Total") == 0)
333                                         && (strcmp (fields[1], "kBytes:") == 0))
334                                 submit_counter ("apache_bytes", "",
335                                                 1024LL * atoll (fields[2]));
336                 }
337                 else if (fields_num == 2)
338                 {
339                         if (strcmp (fields[0], "Scoreboard:") == 0)
340                                 submit_scoreboard (fields[1]);
341                         else if (strcmp (fields[0], "BusyServers:") == 0)
342                                 submit_gauge ("apache_connections", NULL, atol (fields[1]));
343                 }
344         }
345
346         apache_buffer_fill = 0;
347
348         return (0);
349 } /* int apache_read */
350
351 void module_register (void)
352 {
353         plugin_register_config ("apache", config,
354                         config_keys, config_keys_num);
355         plugin_register_init ("apache", init);
356         plugin_register_read ("apache", apache_read);
357 } /* void module_register */