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