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