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