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