added SSLCiphers into Instance section to select ciphers from client
[collectd.git] / src / apache.c
1 /**
2  * collectd - src/apache.c
3  * Copyright (C) 2006-2010  Florian octo Forster
4  * Copyright (C) 2007       Florent EppO Monbillard
5  * Copyright (C) 2009       Amit Gupta
6  *
7  * This program is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License as published by the
9  * Free Software Foundation; only version 2 of the License is applicable.
10  *
11  * This program is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this program; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
19  *
20  * Authors:
21  *   Florian octo Forster <octo at collectd.org>
22  *   Florent EppO Monbillard <eppo at darox.net>
23  *   - connections/lighttpd extension
24  *   Amit Gupta <amit.gupta221 at gmail.com>
25  **/
26
27 #include "collectd.h"
28 #include "common.h"
29 #include "plugin.h"
30 #include "configfile.h"
31
32 #include <curl/curl.h>
33
34 enum server_enum
35 {
36         APACHE = 0,
37         LIGHTTPD
38 };
39
40 struct apache_s
41 {
42         int server_type;
43         char *name;
44         char *host;
45         char *url;
46         char *user;
47         char *pass;
48         _Bool verify_peer;
49         _Bool verify_host;
50         char *cacert;
51         char *ssl_ciphers;
52         char *server; /* user specific server type */
53         char *apache_buffer;
54         char apache_curl_error[CURL_ERROR_SIZE];
55         size_t apache_buffer_size;
56         size_t apache_buffer_fill;
57         CURL *curl;
58 }; /* apache_s */
59
60 typedef struct apache_s apache_t;
61
62 /* TODO: Remove this prototype */
63 static int apache_read_host (user_data_t *user_data);
64
65 static void apache_free (apache_t *st)
66 {
67         if (st == NULL)
68                 return;
69
70         sfree (st->name);
71         sfree (st->host);
72         sfree (st->url);
73         sfree (st->user);
74         sfree (st->pass);
75         sfree (st->cacert);
76         sfree (st->ssl_ciphers);
77         sfree (st->server);
78         sfree (st->apache_buffer);
79         if (st->curl) {
80                 curl_easy_cleanup(st->curl);
81                 st->curl = NULL;
82         }
83 } /* apache_free */
84
85 static size_t apache_curl_callback (void *buf, size_t size, size_t nmemb,
86                 void *user_data)
87 {
88         size_t len = size * nmemb;
89         apache_t *st;
90
91         st = user_data;
92         if (st == NULL)
93         {
94                 ERROR ("apache plugin: apache_curl_callback: "
95                                 "user_data pointer is NULL.");
96                 return (0);
97         }
98
99         if (len <= 0)
100                 return (len);
101
102         if ((st->apache_buffer_fill + len) >= st->apache_buffer_size)
103         {
104                 char *temp;
105
106                 temp = (char *) realloc (st->apache_buffer,
107                                 st->apache_buffer_fill + len + 1);
108                 if (temp == NULL)
109                 {
110                         ERROR ("apache plugin: realloc failed.");
111                         return (0);
112                 }
113                 st->apache_buffer = temp;
114                 st->apache_buffer_size = st->apache_buffer_fill + len + 1;
115         }
116
117         memcpy (st->apache_buffer + st->apache_buffer_fill, (char *) buf, len);
118         st->apache_buffer_fill += len;
119         st->apache_buffer[st->apache_buffer_fill] = 0;
120
121         return (len);
122 } /* int apache_curl_callback */
123
124 static size_t apache_header_callback (void *buf, size_t size, size_t nmemb,
125                 void *user_data)
126 {
127         size_t len = size * nmemb;
128         apache_t *st;
129
130         st = user_data;
131         if (st == NULL)
132         {
133                 ERROR ("apache plugin: apache_header_callback: "
134                                 "user_data pointer is NULL.");
135                 return (0);
136         }
137
138         if (len <= 0)
139                 return (len);
140
141         /* look for the Server header */
142         if (strncasecmp (buf, "Server: ", strlen ("Server: ")) != 0)
143                 return (len);
144
145         if (strstr (buf, "Apache") != NULL)
146                 st->server_type = APACHE;
147         else if (strstr (buf, "lighttpd") != NULL)
148                 st->server_type = LIGHTTPD;
149         else if (strstr (buf, "IBM_HTTP_Server") != NULL)
150                 st->server_type = APACHE;
151         else
152         {
153                 const char *hdr = buf;
154
155                 hdr += strlen ("Server: ");
156                 NOTICE ("apache plugin: Unknown server software: %s", hdr);
157         }
158
159         return (len);
160 } /* apache_header_callback */
161
162 /* Configuration handling functiions
163  * <Plugin apache>
164  *   <Instance "instance_name">
165  *     URL ...
166  *   </Instance>
167  *   URL ...
168  * </Plugin>
169  */
170 static int config_add (oconfig_item_t *ci)
171 {
172         apache_t *st;
173         int i;
174         int status;
175
176         st = malloc (sizeof (*st));
177         if (st == NULL)
178         {
179                 ERROR ("apache plugin: malloc failed.");
180                 return (-1);
181         }
182         memset (st, 0, sizeof (*st));
183
184         status = cf_util_get_string (ci, &st->name);
185         if (status != 0)
186         {
187                 sfree (st);
188                 return (status);
189         }
190         assert (st->name != NULL);
191
192         for (i = 0; i < ci->children_num; i++)
193         {
194                 oconfig_item_t *child = ci->children + i;
195
196                 if (strcasecmp ("URL", child->key) == 0)
197                         status = cf_util_get_string (child, &st->url);
198                 else if (strcasecmp ("Host", child->key) == 0)
199                         status = cf_util_get_string (child, &st->host);
200                 else if (strcasecmp ("User", child->key) == 0)
201                         status = cf_util_get_string (child, &st->user);
202                 else if (strcasecmp ("Password", child->key) == 0)
203                         status = cf_util_get_string (child, &st->pass);
204                 else if (strcasecmp ("VerifyPeer", child->key) == 0)
205                         status = cf_util_get_boolean (child, &st->verify_peer);
206                 else if (strcasecmp ("VerifyHost", child->key) == 0)
207                         status = cf_util_get_boolean (child, &st->verify_host);
208                 else if (strcasecmp ("CACert", child->key) == 0)
209                         status = cf_util_get_string (child, &st->cacert);
210                 else if (strcasecmp ("SSLCiphers", child->key) == 0)
211                         status = cf_util_get_string (child, &st->ssl_ciphers);
212                 else if (strcasecmp ("Server", child->key) == 0)
213                         status = cf_util_get_string (child, &st->server);
214                 else
215                 {
216                         WARNING ("apache plugin: Option `%s' not allowed here.",
217                                         child->key);
218                         status = -1;
219                 }
220
221                 if (status != 0)
222                         break;
223         }
224
225         /* Check if struct is complete.. */
226         if ((status == 0) && (st->url == NULL))
227         {
228                 ERROR ("apache plugin: Instance `%s': "
229                                 "No URL has been configured.",
230                                 st->name);
231                 status = -1;
232         }
233
234         if (status == 0)
235         {
236                 user_data_t ud;
237                 char callback_name[3*DATA_MAX_NAME_LEN];
238
239                 memset (&ud, 0, sizeof (ud));
240                 ud.data = st;
241                 ud.free_func = (void *) apache_free;
242
243                 memset (callback_name, 0, sizeof (callback_name));
244                 ssnprintf (callback_name, sizeof (callback_name),
245                                 "apache/%s/%s",
246                                 (st->host != NULL) ? st->host : hostname_g,
247                                 (st->name != NULL) ? st->name : "default"),
248
249                 status = plugin_register_complex_read (/* group = */ NULL,
250                                 /* name      = */ callback_name,
251                                 /* callback  = */ apache_read_host,
252                                 /* interval  = */ NULL,
253                                 /* user_data = */ &ud);
254         }
255
256         if (status != 0)
257         {
258                 apache_free (st);
259                 return (-1);
260         }
261
262         return (0);
263 } /* int config_add */
264
265 static int config (oconfig_item_t *ci)
266 {
267         int status = 0;
268         int i;
269
270         for (i = 0; i < ci->children_num; i++)
271         {
272                 oconfig_item_t *child = ci->children + i;
273
274                 if (strcasecmp ("Instance", child->key) == 0)
275                         config_add (child);
276                 else
277                         WARNING ("apache plugin: The configuration option "
278                                         "\"%s\" is not allowed here. Did you "
279                                         "forget to add an <Instance /> block "
280                                         "around the configuration?",
281                                         child->key);
282         } /* for (ci->children) */
283
284         return (status);
285 } /* int config */
286
287 /* initialize curl for each host */
288 static int init_host (apache_t *st) /* {{{ */
289 {
290         static char credentials[1024];
291
292         assert (st->url != NULL);
293         /* (Assured by `config_add') */
294
295         if (st->curl != NULL)
296         {
297                 curl_easy_cleanup (st->curl);
298                 st->curl = NULL;
299         }
300
301         if ((st->curl = curl_easy_init ()) == NULL)
302         {
303                 ERROR ("apache plugin: init_host: `curl_easy_init' failed.");
304                 return (-1);
305         }
306
307         curl_easy_setopt (st->curl, CURLOPT_NOSIGNAL, 1L);
308         curl_easy_setopt (st->curl, CURLOPT_WRITEFUNCTION, apache_curl_callback);
309         curl_easy_setopt (st->curl, CURLOPT_WRITEDATA, st);
310
311         /* not set as yet if the user specified string doesn't match apache or
312          * lighttpd, then ignore it. Headers will be parsed to find out the
313          * server type */
314         st->server_type = -1;
315
316         if (st->server != NULL)
317         {
318                 if (strcasecmp(st->server, "apache") == 0)
319                         st->server_type = APACHE;
320                 else if (strcasecmp(st->server, "lighttpd") == 0)
321                         st->server_type = LIGHTTPD;
322                 else if (strcasecmp(st->server, "ibm_http_server") == 0)
323                         st->server_type = APACHE;
324                 else
325                         WARNING ("apache plugin: Unknown `Server' setting: %s",
326                                         st->server);
327         }
328
329         /* if not found register a header callback to determine the server_type */
330         if (st->server_type == -1)
331         {
332                 curl_easy_setopt (st->curl, CURLOPT_HEADERFUNCTION, apache_header_callback);
333                 curl_easy_setopt (st->curl, CURLOPT_WRITEHEADER, st);
334         }
335
336         curl_easy_setopt (st->curl, CURLOPT_USERAGENT, COLLECTD_USERAGENT);
337         curl_easy_setopt (st->curl, CURLOPT_ERRORBUFFER, st->apache_curl_error);
338
339         if (st->user != NULL)
340         {
341                 int status;
342
343                 status = ssnprintf (credentials, sizeof (credentials), "%s:%s",
344                                 st->user, (st->pass == NULL) ? "" : st->pass);
345                 if ((status < 0) || ((size_t) status >= sizeof (credentials)))
346                 {
347                         ERROR ("apache plugin: init_host: Returning an error "
348                                         "because the credentials have been "
349                                         "truncated.");
350                         curl_easy_cleanup (st->curl);
351                         st->curl = NULL;
352                         return (-1);
353                 }
354
355                 curl_easy_setopt (st->curl, CURLOPT_USERPWD, credentials);
356         }
357
358         curl_easy_setopt (st->curl, CURLOPT_URL, st->url);
359         curl_easy_setopt (st->curl, CURLOPT_FOLLOWLOCATION, 1L);
360         curl_easy_setopt (st->curl, CURLOPT_MAXREDIRS, 50L);
361
362         curl_easy_setopt (st->curl, CURLOPT_SSL_VERIFYPEER,
363                         (long) st->verify_peer);
364         curl_easy_setopt (st->curl, CURLOPT_SSL_VERIFYHOST,
365                         st->verify_host ? 2L : 0L);
366         if (st->cacert != NULL)
367                 curl_easy_setopt (st->curl, CURLOPT_CAINFO, st->cacert);
368         if (st->ssl_ciphers != NULL)
369                 curl_easy_setopt (st->curl, CURLOPT_SSL_CIPHER_LIST,st->ssl_ciphers);
370
371         return (0);
372 } /* }}} int init_host */
373
374 static void submit_value (const char *type, const char *type_instance,
375                 value_t value, apache_t *st)
376 {
377         value_list_t vl = VALUE_LIST_INIT;
378
379         vl.values = &value;
380         vl.values_len = 1;
381
382         sstrncpy (vl.host, (st->host != NULL) ? st->host : hostname_g,
383                         sizeof (vl.host));
384
385         sstrncpy (vl.plugin, "apache", sizeof (vl.plugin));
386         if (st->name != NULL)
387                 sstrncpy (vl.plugin_instance, st->name,
388                                 sizeof (vl.plugin_instance));
389
390         sstrncpy (vl.type, type, sizeof (vl.type));
391         if (type_instance != NULL)
392                 sstrncpy (vl.type_instance, type_instance,
393                                 sizeof (vl.type_instance));
394
395         plugin_dispatch_values (&vl);
396 } /* void submit_value */
397
398 static void submit_derive (const char *type, const char *type_instance,
399                 derive_t c, apache_t *st)
400 {
401         value_t v;
402         v.derive = c;
403         submit_value (type, type_instance, v, st);
404 } /* void submit_derive */
405
406 static void submit_gauge (const char *type, const char *type_instance,
407                 gauge_t g, apache_t *st)
408 {
409         value_t v;
410         v.gauge = g;
411         submit_value (type, type_instance, v, st);
412 } /* void submit_gauge */
413
414 static void submit_scoreboard (char *buf, apache_t *st)
415 {
416         /*
417          * Scoreboard Key:
418          * "_" Waiting for Connection, "S" Starting up,
419          * "R" Reading Request for apache and read-POST for lighttpd,
420          * "W" Sending Reply, "K" Keepalive (read), "D" DNS Lookup,
421          * "C" Closing connection, "L" Logging, "G" Gracefully finishing,
422          * "I" Idle cleanup of worker, "." Open slot with no current process
423          * Lighttpd specific legends -
424          * "E" hard error, "." connect, "h" handle-request,
425          * "q" request-start, "Q" request-end, "s" response-start
426          * "S" response-end, "r" read
427          */
428         long long open      = 0LL;
429         long long waiting   = 0LL;
430         long long starting  = 0LL;
431         long long reading   = 0LL;
432         long long sending   = 0LL;
433         long long keepalive = 0LL;
434         long long dnslookup = 0LL;
435         long long closing   = 0LL;
436         long long logging   = 0LL;
437         long long finishing = 0LL;
438         long long idle_cleanup = 0LL;
439
440         /* lighttpd specific */
441         long long hard_error     = 0LL;
442         long long lighttpd_read  = 0LL;
443         long long handle_request = 0LL;
444         long long request_start  = 0LL;
445         long long request_end    = 0LL;
446         long long response_start = 0LL;
447         long long response_end   = 0LL;
448
449         int i;
450         for (i = 0; buf[i] != '\0'; i++)
451         {
452                 if (buf[i] == '.') open++;
453                 else if (buf[i] == '_') waiting++;
454                 else if (buf[i] == 'S') starting++;
455                 else if (buf[i] == 'R') reading++;
456                 else if (buf[i] == 'W') sending++;
457                 else if (buf[i] == 'K') keepalive++;
458                 else if (buf[i] == 'D') dnslookup++;
459                 else if (buf[i] == 'C') closing++;
460                 else if (buf[i] == 'L') logging++;
461                 else if (buf[i] == 'G') finishing++;
462                 else if (buf[i] == 'I') idle_cleanup++;
463                 else if (buf[i] == 'r') lighttpd_read++;
464                 else if (buf[i] == 'h') handle_request++;
465                 else if (buf[i] == 'E') hard_error++;
466                 else if (buf[i] == 'q') request_start++;
467                 else if (buf[i] == 'Q') request_end++;
468                 else if (buf[i] == 's') response_start++;
469                 else if (buf[i] == 'S') response_end++;
470         }
471
472         if (st->server_type == APACHE)
473         {
474                 submit_gauge ("apache_scoreboard", "open"     , open, st);
475                 submit_gauge ("apache_scoreboard", "waiting"  , waiting, st);
476                 submit_gauge ("apache_scoreboard", "starting" , starting, st);
477                 submit_gauge ("apache_scoreboard", "reading"  , reading, st);
478                 submit_gauge ("apache_scoreboard", "sending"  , sending, st);
479                 submit_gauge ("apache_scoreboard", "keepalive", keepalive, st);
480                 submit_gauge ("apache_scoreboard", "dnslookup", dnslookup, st);
481                 submit_gauge ("apache_scoreboard", "closing"  , closing, st);
482                 submit_gauge ("apache_scoreboard", "logging"  , logging, st);
483                 submit_gauge ("apache_scoreboard", "finishing", finishing, st);
484                 submit_gauge ("apache_scoreboard", "idle_cleanup", idle_cleanup, st);
485         }
486         else
487         {
488                 submit_gauge ("apache_scoreboard", "connect"       , open, st);
489                 submit_gauge ("apache_scoreboard", "close"         , closing, st);
490                 submit_gauge ("apache_scoreboard", "hard_error"    , hard_error, st);
491                 submit_gauge ("apache_scoreboard", "read"          , lighttpd_read, st);
492                 submit_gauge ("apache_scoreboard", "read_post"     , reading, st);
493                 submit_gauge ("apache_scoreboard", "write"         , sending, st);
494                 submit_gauge ("apache_scoreboard", "handle_request", handle_request, st);
495                 submit_gauge ("apache_scoreboard", "request_start" , request_start, st);
496                 submit_gauge ("apache_scoreboard", "request_end"   , request_end, st);
497                 submit_gauge ("apache_scoreboard", "response_start", response_start, st);
498                 submit_gauge ("apache_scoreboard", "response_end"  , response_end, st);
499         }
500 }
501
502 static int apache_read_host (user_data_t *user_data) /* {{{ */
503 {
504         int i;
505
506         char *ptr;
507         char *saveptr;
508         char *lines[16];
509         int   lines_num = 0;
510
511         char *fields[4];
512         int   fields_num;
513
514         apache_t *st;
515
516         st = user_data->data;
517
518         assert (st->url != NULL);
519         /* (Assured by `config_add') */
520
521         if (st->curl == NULL)
522         {
523                 int status;
524
525                 status = init_host (st);
526                 if (status != 0)
527                         return (-1);
528         }
529         assert (st->curl != NULL);
530
531         st->apache_buffer_fill = 0;
532         if (curl_easy_perform (st->curl) != CURLE_OK)
533         {
534                 ERROR ("apache: curl_easy_perform failed: %s",
535                                 st->apache_curl_error);
536                 return (-1);
537         }
538
539         /* fallback - server_type to apache if not set at this time */
540         if (st->server_type == -1)
541         {
542                 WARNING ("apache plugin: Unable to determine server software "
543                                 "automatically. Will assume Apache.");
544                 st->server_type = APACHE;
545         }
546
547         ptr = st->apache_buffer;
548         saveptr = NULL;
549         while ((lines[lines_num] = strtok_r (ptr, "\n\r", &saveptr)) != NULL)
550         {
551                 ptr = NULL;
552                 lines_num++;
553
554                 if (lines_num >= 16)
555                         break;
556         }
557
558         for (i = 0; i < lines_num; i++)
559         {
560                 fields_num = strsplit (lines[i], fields, 4);
561
562                 if (fields_num == 3)
563                 {
564                         if ((strcmp (fields[0], "Total") == 0)
565                                         && (strcmp (fields[1], "Accesses:") == 0))
566                                 submit_derive ("apache_requests", "",
567                                                 atoll (fields[2]), st);
568                         else if ((strcmp (fields[0], "Total") == 0)
569                                         && (strcmp (fields[1], "kBytes:") == 0))
570                                 submit_derive ("apache_bytes", "",
571                                                 1024LL * atoll (fields[2]), st);
572                 }
573                 else if (fields_num == 2)
574                 {
575                         if (strcmp (fields[0], "Scoreboard:") == 0)
576                                 submit_scoreboard (fields[1], st);
577                         else if ((strcmp (fields[0], "BusyServers:") == 0) /* Apache 1.* */
578                                         || (strcmp (fields[0], "BusyWorkers:") == 0) /* Apache 2.* */)
579                                 submit_gauge ("apache_connections", NULL, atol (fields[1]), st);
580                         else if ((strcmp (fields[0], "IdleServers:") == 0) /* Apache 1.x */
581                                         || (strcmp (fields[0], "IdleWorkers:") == 0) /* Apache 2.x */)
582                                 submit_gauge ("apache_idle_workers", NULL, atol (fields[1]), st);
583                 }
584         }
585
586         st->apache_buffer_fill = 0;
587
588         return (0);
589 } /* }}} int apache_read_host */
590
591 static int apache_init (void) /* {{{ */
592 {
593         /* Call this while collectd is still single-threaded to avoid
594          * initialization issues in libgcrypt. */
595         curl_global_init (CURL_GLOBAL_SSL);
596         return (0);
597 } /* }}} int apache_init */
598
599 void module_register (void)
600 {
601         plugin_register_complex_config ("apache", config);
602         plugin_register_init ("apache", apache_init);
603 } /* void module_register */
604
605 /* vim: set sw=8 noet fdm=marker : */