13c8db66801291fb05cac46171ce089e14175e47
1 /**
2 * collectd - src/apache.c
3 * Copyright (C) 2006-2010 Florian octo Forster
4 * Copyright (C) 2007 Florent EppO Monbillard
5 * Copyright (C) 2009 Amit Gupta
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; only version 2 of the License is applicable.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 *
20 * Authors:
21 * Florian octo Forster <octo at verplant.org>
22 * Florent EppO Monbillard <eppo at darox.net>
23 * - connections/lighttpd extension
24 * Amit Gupta <amit.gupta221 at gmail.com>
25 **/
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 sfree (st);
82 } /* apache_free */
84 static size_t apache_curl_callback (void *buf, size_t size, size_t nmemb,
85 void *user_data)
86 {
87 size_t len = size * nmemb;
88 apache_t *st;
90 st = user_data;
91 if (st == NULL)
92 {
93 ERROR ("apache plugin: apache_curl_callback: "
94 "user_data pointer is NULL.");
95 return (0);
96 }
98 if (len <= 0)
99 return (len);
101 if ((st->apache_buffer_fill + len) >= st->apache_buffer_size)
102 {
103 char *temp;
105 temp = (char *) realloc (st->apache_buffer,
106 st->apache_buffer_fill + len + 1);
107 if (temp == NULL)
108 {
109 ERROR ("apache plugin: realloc failed.");
110 return (0);
111 }
112 st->apache_buffer = temp;
113 st->apache_buffer_size = st->apache_buffer_fill + len + 1;
114 }
116 memcpy (st->apache_buffer + st->apache_buffer_fill, (char *) buf, len);
117 st->apache_buffer_fill += len;
118 st->apache_buffer[st->apache_buffer_fill] = 0;
120 return (len);
121 } /* int apache_curl_callback */
123 static size_t apache_header_callback (void *buf, size_t size, size_t nmemb,
124 void *user_data)
125 {
126 size_t len = size * nmemb;
127 apache_t *st;
129 st = user_data;
130 if (st == NULL)
131 {
132 ERROR ("apache plugin: apache_header_callback: "
133 "user_data pointer is NULL.");
134 return (0);
135 }
137 if (len <= 0)
138 return (len);
140 /* look for the Server header */
141 if (strncasecmp (buf, "Server: ", strlen ("Server: ")) != 0)
142 return (len);
144 if (strstr (buf, "Apache") != NULL)
145 st->server_type = APACHE;
146 else if (strstr (buf, "lighttpd") != NULL)
147 st->server_type = LIGHTTPD;
148 else if (strstr (buf, "IBM_HTTP_Server") != NULL)
149 st->server_type = APACHE;
150 else
151 {
152 const char *hdr = buf;
154 hdr += strlen ("Server: ");
155 NOTICE ("apache plugin: Unknown server software: %s", hdr);
156 }
158 return (len);
159 } /* apache_header_callback */
161 /* Configuration handling functiions
162 * <Plugin apache>
163 * <Instance "instance_name">
164 * URL ...
165 * </Instance>
166 * URL ...
167 * </Plugin>
168 */
169 static int config_set_string (char **ret_string, /* {{{ */
170 oconfig_item_t *ci)
171 {
172 char *string;
174 if ((ci->values_num != 1)
175 || (ci->values[0].type != OCONFIG_TYPE_STRING))
176 {
177 WARNING ("apache plugin: The `%s' config option "
178 "needs exactly one string argument.", ci->key);
179 return (-1);
180 }
182 string = strdup (ci->values[0].value.string);
183 if (string == NULL)
184 {
185 ERROR ("apache plugin: strdup failed.");
186 return (-1);
187 }
189 if (*ret_string != NULL)
190 free (*ret_string);
191 *ret_string = string;
193 return (0);
194 } /* }}} int config_set_string */
196 static int config_set_boolean (int *ret_boolean, /* {{{ */
197 oconfig_item_t *ci)
198 {
199 if ((ci->values_num != 1)
200 || ((ci->values[0].type != OCONFIG_TYPE_BOOLEAN)
201 && (ci->values[0].type != OCONFIG_TYPE_STRING)))
202 {
203 WARNING ("apache plugin: The `%s' config option "
204 "needs exactly one boolean argument.", ci->key);
205 return (-1);
206 }
208 if (ci->values[0].type == OCONFIG_TYPE_BOOLEAN)
209 {
210 if (ci->values[0].value.boolean)
211 *ret_boolean = 1;
212 else
213 *ret_boolean = 0;
214 }
215 else /* if (ci->values[0].type != OCONFIG_TYPE_STRING) */
216 {
217 char *string = ci->values[0].value.string;
218 if (IS_TRUE (string))
219 *ret_boolean = 1;
220 else if (IS_FALSE (string))
221 *ret_boolean = 0;
222 else
223 {
224 ERROR ("apache plugin: Cannot parse string "
225 "as boolean value: %s", string);
226 return (-1);
227 }
228 }
230 return (0);
231 } /* }}} int config_set_boolean */
233 static int config_add (oconfig_item_t *ci)
234 {
235 apache_t *st;
236 int i;
237 int status;
239 if ((ci->values_num != 1)
240 || (ci->values[0].type != OCONFIG_TYPE_STRING))
241 {
242 WARNING ("apache plugin: The `%s' config option "
243 "needs exactly one string argument.", ci->key);
244 return (-1);
245 }
247 st = (apache_t *) malloc (sizeof (*st));
248 if (st == NULL)
249 {
250 ERROR ("apache plugin: malloc failed.");
251 return (-1);
252 }
254 memset (st, 0, sizeof (*st));
256 status = config_set_string (&st->name, ci);
257 if (status != 0)
258 {
259 sfree (st);
260 return (status);
261 }
262 assert (st->name != NULL);
264 for (i = 0; i < ci->children_num; i++)
265 {
266 oconfig_item_t *child = ci->children + i;
268 if (strcasecmp ("URL", child->key) == 0)
269 status = config_set_string (&st->url, child);
270 else if (strcasecmp ("Host", child->key) == 0)
271 status = config_set_string (&st->host, child);
272 else if (strcasecmp ("User", child->key) == 0)
273 status = config_set_string (&st->user, child);
274 else if (strcasecmp ("Password", child->key) == 0)
275 status = config_set_string (&st->pass, child);
276 else if (strcasecmp ("VerifyPeer", child->key) == 0)
277 status = config_set_boolean (&st->verify_peer, child);
278 else if (strcasecmp ("VerifyHost", child->key) == 0)
279 status = config_set_boolean (&st->verify_host, child);
280 else if (strcasecmp ("CACert", child->key) == 0)
281 status = config_set_string (&st->cacert, child);
282 else if (strcasecmp ("Server", child->key) == 0)
283 status = config_set_string (&st->server, child);
284 else
285 {
286 WARNING ("apache plugin: Option `%s' not allowed here.",
287 child->key);
288 status = -1;
289 }
291 if (status != 0)
292 break;
293 }
295 /* Check if struct is complete.. */
296 if ((status == 0) && (st->url == NULL))
297 {
298 ERROR ("apache plugin: Instance `%s': "
299 "No URL has been configured.",
300 st->name);
301 status = -1;
302 }
304 if (status == 0)
305 {
306 user_data_t ud;
307 char callback_name[3*DATA_MAX_NAME_LEN];
309 memset (&ud, 0, sizeof (ud));
310 ud.data = st;
311 ud.free_func = (void *) apache_free;
313 memset (callback_name, 0, sizeof (callback_name));
314 ssnprintf (callback_name, sizeof (callback_name),
315 "apache/%s/%s",
316 (st->host != NULL) ? st->host : hostname_g,
317 (st->name != NULL) ? st->name : "default"),
319 status = plugin_register_complex_read (/* group = */ NULL,
320 /* name = */ callback_name,
321 /* callback = */ apache_read_host,
322 /* interval = */ NULL,
323 /* user_data = */ &ud);
324 }
326 if (status != 0)
327 {
328 apache_free(st);
329 return (-1);
330 }
332 return (0);
333 } /* int config_add */
335 static int config (oconfig_item_t *ci)
336 {
337 int status = 0;
338 int i;
340 for (i = 0; i < ci->children_num; i++)
341 {
342 oconfig_item_t *child = ci->children + i;
344 if (strcasecmp ("Instance", child->key) == 0)
345 config_add (child);
346 else
347 WARNING ("apache plugin: The configuration option "
348 "\"%s\" is not allowed here. Did you "
349 "forget to add an <Instance /> block "
350 "around the configuration?",
351 child->key);
352 } /* for (ci->children) */
354 return (status);
355 } /* int config */
357 /* initialize curl for each host */
358 static int init_host (apache_t *st) /* {{{ */
359 {
360 static char credentials[1024];
362 assert (st->url != NULL);
363 /* (Assured by `config_add') */
365 if (st->curl != NULL)
366 {
367 curl_easy_cleanup (st->curl);
368 st->curl = NULL;
369 }
371 if ((st->curl = curl_easy_init ()) == NULL)
372 {
373 ERROR ("apache plugin: init_host: `curl_easy_init' failed.");
374 return (-1);
375 }
377 curl_easy_setopt (st->curl, CURLOPT_NOSIGNAL, 1L);
378 curl_easy_setopt (st->curl, CURLOPT_WRITEFUNCTION, apache_curl_callback);
379 curl_easy_setopt (st->curl, CURLOPT_WRITEDATA, st);
381 /* not set as yet if the user specified string doesn't match apache or
382 * lighttpd, then ignore it. Headers will be parsed to find out the
383 * server type */
384 st->server_type = -1;
386 if (st->server != NULL)
387 {
388 if (strcasecmp(st->server, "apache") == 0)
389 st->server_type = APACHE;
390 else if (strcasecmp(st->server, "lighttpd") == 0)
391 st->server_type = LIGHTTPD;
392 else if (strcasecmp(st->server, "ibm_http_server") == 0)
393 st->server_type = APACHE;
394 else
395 WARNING ("apache plugin: Unknown `Server' setting: %s",
396 st->server);
397 }
399 /* if not found register a header callback to determine the server_type */
400 if (st->server_type == -1)
401 {
402 curl_easy_setopt (st->curl, CURLOPT_HEADERFUNCTION, apache_header_callback);
403 curl_easy_setopt (st->curl, CURLOPT_WRITEHEADER, st);
404 }
406 curl_easy_setopt (st->curl, CURLOPT_USERAGENT, PACKAGE_NAME"/"PACKAGE_VERSION);
407 curl_easy_setopt (st->curl, CURLOPT_ERRORBUFFER, st->apache_curl_error);
409 if (st->user != NULL)
410 {
411 int status;
413 status = ssnprintf (credentials, sizeof (credentials), "%s:%s",
414 st->user, (st->pass == NULL) ? "" : st->pass);
415 if ((status < 0) || ((size_t) status >= sizeof (credentials)))
416 {
417 ERROR ("apache plugin: init_host: Returning an error "
418 "because the credentials have been "
419 "truncated.");
420 curl_easy_cleanup (st->curl);
421 st->curl = NULL;
422 return (-1);
423 }
425 curl_easy_setopt (st->curl, CURLOPT_USERPWD, credentials);
426 }
428 curl_easy_setopt (st->curl, CURLOPT_URL, st->url);
429 curl_easy_setopt (st->curl, CURLOPT_FOLLOWLOCATION, 1L);
430 curl_easy_setopt (st->curl, CURLOPT_MAXREDIRS, 50L);
432 if (st->verify_peer != 0)
433 {
434 curl_easy_setopt (st->curl, CURLOPT_SSL_VERIFYPEER, 1L);
435 }
436 else
437 {
438 curl_easy_setopt (st->curl, CURLOPT_SSL_VERIFYPEER, 0L);
439 }
441 if (st->verify_host != 0)
442 {
443 curl_easy_setopt (st->curl, CURLOPT_SSL_VERIFYHOST, 2L);
444 }
445 else
446 {
447 curl_easy_setopt (st->curl, CURLOPT_SSL_VERIFYHOST, 0L);
448 }
450 if (st->cacert != NULL)
451 {
452 curl_easy_setopt (st->curl, CURLOPT_CAINFO, st->cacert);
453 }
455 return (0);
456 } /* }}} int init_host */
458 static void submit_value (const char *type, const char *type_instance,
459 value_t value, apache_t *st)
460 {
461 value_list_t vl = VALUE_LIST_INIT;
463 vl.values = &value;
464 vl.values_len = 1;
466 sstrncpy (vl.host, (st->host != NULL) ? st->host : hostname_g,
467 sizeof (vl.host));
469 sstrncpy (vl.plugin, "apache", sizeof (vl.plugin));
470 if (st->name != NULL)
471 sstrncpy (vl.plugin_instance, st->name,
472 sizeof (vl.plugin_instance));
474 sstrncpy (vl.type, type, sizeof (vl.type));
475 if (type_instance != NULL)
476 sstrncpy (vl.type_instance, type_instance,
477 sizeof (vl.type_instance));
479 plugin_dispatch_values (&vl);
480 } /* void submit_value */
482 static void submit_derive (const char *type, const char *type_instance,
483 derive_t c, apache_t *st)
484 {
485 value_t v;
486 v.derive = c;
487 submit_value (type, type_instance, v, st);
488 } /* void submit_derive */
490 static void submit_gauge (const char *type, const char *type_instance,
491 gauge_t g, apache_t *st)
492 {
493 value_t v;
494 v.gauge = g;
495 submit_value (type, type_instance, v, st);
496 } /* void submit_gauge */
498 static void submit_scoreboard (char *buf, apache_t *st)
499 {
500 /*
501 * Scoreboard Key:
502 * "_" Waiting for Connection, "S" Starting up,
503 * "R" Reading Request for apache and read-POST for lighttpd,
504 * "W" Sending Reply, "K" Keepalive (read), "D" DNS Lookup,
505 * "C" Closing connection, "L" Logging, "G" Gracefully finishing,
506 * "I" Idle cleanup of worker, "." Open slot with no current process
507 * Lighttpd specific legends -
508 * "E" hard error, "." connect, "h" handle-request,
509 * "q" request-start, "Q" request-end, "s" response-start
510 * "S" response-end, "r" read
511 */
512 long long open = 0LL;
513 long long waiting = 0LL;
514 long long starting = 0LL;
515 long long reading = 0LL;
516 long long sending = 0LL;
517 long long keepalive = 0LL;
518 long long dnslookup = 0LL;
519 long long closing = 0LL;
520 long long logging = 0LL;
521 long long finishing = 0LL;
522 long long idle_cleanup = 0LL;
524 /* lighttpd specific */
525 long long hard_error = 0LL;
526 long long lighttpd_read = 0LL;
527 long long handle_request = 0LL;
528 long long request_start = 0LL;
529 long long request_end = 0LL;
530 long long response_start = 0LL;
531 long long response_end = 0LL;
533 int i;
534 for (i = 0; buf[i] != '\0'; i++)
535 {
536 if (buf[i] == '.') open++;
537 else if (buf[i] == '_') waiting++;
538 else if (buf[i] == 'S') starting++;
539 else if (buf[i] == 'R') reading++;
540 else if (buf[i] == 'W') sending++;
541 else if (buf[i] == 'K') keepalive++;
542 else if (buf[i] == 'D') dnslookup++;
543 else if (buf[i] == 'C') closing++;
544 else if (buf[i] == 'L') logging++;
545 else if (buf[i] == 'G') finishing++;
546 else if (buf[i] == 'I') idle_cleanup++;
547 else if (buf[i] == 'r') lighttpd_read++;
548 else if (buf[i] == 'h') handle_request++;
549 else if (buf[i] == 'E') hard_error++;
550 else if (buf[i] == 'q') request_start++;
551 else if (buf[i] == 'Q') request_end++;
552 else if (buf[i] == 's') response_start++;
553 else if (buf[i] == 'S') response_end++;
554 }
556 if (st->server_type == APACHE)
557 {
558 submit_gauge ("apache_scoreboard", "open" , open, st);
559 submit_gauge ("apache_scoreboard", "waiting" , waiting, st);
560 submit_gauge ("apache_scoreboard", "starting" , starting, st);
561 submit_gauge ("apache_scoreboard", "reading" , reading, st);
562 submit_gauge ("apache_scoreboard", "sending" , sending, st);
563 submit_gauge ("apache_scoreboard", "keepalive", keepalive, st);
564 submit_gauge ("apache_scoreboard", "dnslookup", dnslookup, st);
565 submit_gauge ("apache_scoreboard", "closing" , closing, st);
566 submit_gauge ("apache_scoreboard", "logging" , logging, st);
567 submit_gauge ("apache_scoreboard", "finishing", finishing, st);
568 submit_gauge ("apache_scoreboard", "idle_cleanup", idle_cleanup, st);
569 }
570 else
571 {
572 submit_gauge ("apache_scoreboard", "connect" , open, st);
573 submit_gauge ("apache_scoreboard", "close" , closing, st);
574 submit_gauge ("apache_scoreboard", "hard_error" , hard_error, st);
575 submit_gauge ("apache_scoreboard", "read" , lighttpd_read, st);
576 submit_gauge ("apache_scoreboard", "read_post" , reading, st);
577 submit_gauge ("apache_scoreboard", "write" , sending, st);
578 submit_gauge ("apache_scoreboard", "handle_request", handle_request, st);
579 submit_gauge ("apache_scoreboard", "request_start" , request_start, st);
580 submit_gauge ("apache_scoreboard", "request_end" , request_end, st);
581 submit_gauge ("apache_scoreboard", "response_start", response_start, st);
582 submit_gauge ("apache_scoreboard", "response_end" , response_end, st);
583 }
584 }
586 static int apache_read_host (user_data_t *user_data) /* {{{ */
587 {
588 char *ptr;
589 char *saveptr;
590 char *line;
592 char *fields[4];
593 int fields_num;
595 apache_t *st;
597 st = user_data->data;
599 assert (st->url != NULL);
600 /* (Assured by `config_add') */
602 if (st->curl == NULL)
603 {
604 int status;
606 status = init_host (st);
607 if (status != 0)
608 return (-1);
609 }
610 assert (st->curl != NULL);
612 st->apache_buffer_fill = 0;
613 if (curl_easy_perform (st->curl) != CURLE_OK)
614 {
615 ERROR ("apache: curl_easy_perform failed: %s",
616 st->apache_curl_error);
617 return (-1);
618 }
620 /* fallback - server_type to apache if not set at this time */
621 if (st->server_type == -1)
622 {
623 WARNING ("apache plugin: Unable to determine server software "
624 "automatically. Will assume Apache.");
625 st->server_type = APACHE;
626 }
628 ptr = st->apache_buffer;
629 saveptr = NULL;
630 while ((line = strtok_r (ptr, "\n\r", &saveptr)) != NULL)
631 {
632 ptr = NULL;
633 fields_num = strsplit (line, fields, STATIC_ARRAY_SIZE (fields));
635 if (fields_num == 3)
636 {
637 if ((strcmp (fields[0], "Total") == 0) && (strcmp (fields[1], "Accesses:") == 0))
638 submit_derive ("apache_requests", "", atoll (fields[2]), st);
639 else if ((strcmp (fields[0], "Total") == 0) && (strcmp (fields[1], "kBytes:") == 0))
640 submit_derive ("apache_bytes", "", 1024LL * atoll (fields[2]), st);
641 }
642 else if (fields_num == 2)
643 {
644 if (strcmp (fields[0], "Scoreboard:") == 0)
645 submit_scoreboard (fields[1], st);
646 else if ((strcmp (fields[0], "BusyServers:") == 0) /* Apache 1.* */
647 || (strcmp (fields[0], "BusyWorkers:") == 0) /* Apache 2.* */)
648 submit_gauge ("apache_connections", NULL, atol (fields[1]), st);
649 else if ((strcmp (fields[0], "IdleServers:") == 0) /* Apache 1.x */
650 || (strcmp (fields[0], "IdleWorkers:") == 0) /* Apache 2.x */)
651 submit_gauge ("apache_idle_workers", NULL, atol (fields[1]), st);
652 }
653 }
655 st->apache_buffer_fill = 0;
657 return (0);
658 } /* }}} int apache_read_host */
660 static int apache_init (void) /* {{{ */
661 {
662 /* Call this while collectd is still single-threaded to avoid
663 * initialization issues in libgcrypt. */
664 curl_global_init (CURL_GLOBAL_SSL);
665 return (0);
666 } /* }}} int apache_init */
668 void module_register (void)
669 {
670 plugin_register_complex_config ("apache", config);
671 plugin_register_init ("apache", apache_init);
672 } /* void module_register */
674 /* vim: set sw=8 noet fdm=marker : */