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