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