69633d6fb760f26457f9fc42e0aa2a0278c465ac
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 };
41 struct apache_s
42 {
43 int server_type;
44 char *name;
45 char *host;
46 char *url;
47 char *user;
48 char *pass;
49 int verify_peer;
50 int verify_host;
51 char *cacert;
52 char *server; // user specific server type
53 char *apache_buffer;
54 char apache_curl_error[CURL_ERROR_SIZE];
55 size_t apache_buffer_size;
56 size_t apache_buffer_fill;
57 CURL *curl;
58 }; /* apache_s */
60 typedef struct apache_s apache_t;
62 /* TODO: Remove this prototype */
63 static int apache_read_host (user_data_t *user_data);
65 static void apache_free (apache_t *st)
66 {
67 if (st == NULL)
68 return;
70 sfree (st->name);
71 sfree (st->host);
72 sfree (st->url);
73 sfree (st->user);
74 sfree (st->pass);
75 sfree (st->cacert);
76 sfree (st->server);
77 sfree (st->apache_buffer);
78 if (st->curl) {
79 curl_easy_cleanup(st->curl);
80 st->curl = NULL;
81 }
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 ((strstr(buf, "Server: ") != NULL) &&
142 (strstr(buf, "lighttpd") != NULL)) {
143 st->server_type = LIGHTTPD;
144 }
146 return len;
147 } /* apache_header_callback */
149 /* Configuration handling functiions
150 * <Plugin apache>
151 * <Instance "instance_name">
152 * URL ...
153 * </Instance>
154 * URL ...
155 * </Plugin>
156 */
157 static int config_set_string (char **ret_string, /* {{{ */
158 oconfig_item_t *ci)
159 {
160 char *string;
162 if ((ci->values_num != 1)
163 || (ci->values[0].type != OCONFIG_TYPE_STRING))
164 {
165 WARNING ("apache plugin: The `%s' config option "
166 "needs exactly one string argument.", ci->key);
167 return (-1);
168 }
170 string = strdup (ci->values[0].value.string);
171 if (string == NULL)
172 {
173 ERROR ("apache plugin: strdup failed.");
174 return (-1);
175 }
177 if (*ret_string != NULL)
178 free (*ret_string);
179 *ret_string = string;
181 return (0);
182 } /* }}} int config_set_string */
184 static int config_set_boolean (int *ret_boolean, /* {{{ */
185 oconfig_item_t *ci)
186 {
187 if ((ci->values_num != 1)
188 || ((ci->values[0].type != OCONFIG_TYPE_BOOLEAN)
189 && (ci->values[0].type != OCONFIG_TYPE_STRING)))
190 {
191 WARNING ("apache plugin: The `%s' config option "
192 "needs exactly one boolean argument.", ci->key);
193 return (-1);
194 }
196 if (ci->values[0].type != OCONFIG_TYPE_BOOLEAN)
197 {
198 if (ci->values[0].value.boolean)
199 *ret_boolean = 1;
200 else
201 *ret_boolean = 0;
202 }
203 else /* if (ci->values[0].type != OCONFIG_TYPE_STRING) */
204 {
205 char *string = ci->values[0].value.string;
206 if ((strcasecmp ("true", string) == 0)
207 || (strcasecmp ("yes", string) == 0)
208 || (strcasecmp ("on", string) == 0))
209 *ret_boolean = 1;
210 else if ((strcasecmp ("false", string) == 0)
211 || (strcasecmp ("no", string) == 0)
212 || (strcasecmp ("off", string) == 0))
213 *ret_boolean = 0;
214 else
215 {
216 ERROR ("apache plugin: Cannot parse string "
217 "as boolean value: %s", string);
218 return (-1);
219 }
220 }
222 return (0);
223 } /* }}} int config_set_boolean */
225 static int config_add (oconfig_item_t *ci)
226 {
227 apache_t *st;
228 int i;
229 int status;
231 if ((ci->values_num != 1)
232 || (ci->values[0].type != OCONFIG_TYPE_STRING))
233 {
234 WARNING ("apache plugin: The `%s' config option "
235 "needs exactly one string argument.", ci->key);
236 return (-1);
237 }
239 st = (apache_t *) malloc (sizeof (*st));
240 if (st == NULL)
241 {
242 ERROR ("apache plugin: malloc failed.");
243 return (-1);
244 }
246 memset (st, 0, sizeof (*st));
248 status = config_set_string (&st->name, ci);
249 if (status != 0)
250 {
251 sfree (st);
252 return (status);
253 }
254 assert (st->name != NULL);
256 for (i = 0; i < ci->children_num; i++)
257 {
258 oconfig_item_t *child = ci->children + i;
260 if (strcasecmp ("URL", child->key) == 0)
261 status = config_set_string (&st->url, child);
262 else if (strcasecmp ("Host", child->key) == 0)
263 status = config_set_string (&st->host, child);
264 else if (strcasecmp ("User", child->key) == 0)
265 status = config_set_string (&st->user, child);
266 else if (strcasecmp ("Password", child->key) == 0)
267 status = config_set_string (&st->pass, child);
268 else if (strcasecmp ("VerifyPeer", child->key) == 0)
269 status = config_set_boolean (&st->verify_peer, child);
270 else if (strcasecmp ("VerifyHost", child->key) == 0)
271 status = config_set_boolean (&st->verify_host, child);
272 else if (strcasecmp ("CACert", child->key) == 0)
273 status = config_set_string (&st->cacert, child);
274 else if (strcasecmp ("Server", child->key) == 0)
275 status = config_set_string (&st->server, child);
276 else
277 {
278 WARNING ("apache plugin: Option `%s' not allowed here.",
279 child->key);
280 status = -1;
281 }
283 if (status != 0)
284 break;
285 }
287 /* Check if struct is complete.. */
288 if ((status == 0) && (st->url == NULL))
289 {
290 ERROR ("apache plugin: Instance `%s': "
291 "No URL has been configured.",
292 st->name);
293 status = -1;
294 }
296 if (status == 0)
297 {
298 user_data_t ud;
299 char callback_name[3*DATA_MAX_NAME_LEN];
301 memset (&ud, 0, sizeof (ud));
302 ud.data = st;
303 ud.free_func = (void *) apache_free;
305 memset (callback_name, 0, sizeof (callback_name));
306 ssnprintf (callback_name, sizeof (callback_name),
307 "apache/%s/%s",
308 (st->host != NULL) ? st->host : hostname_g,
309 (st->name != NULL) ? st->name : "default"),
311 status = plugin_register_complex_read (callback_name,
312 /* callback = */ apache_read_host,
313 /* interval = */ NULL,
314 /* user_data = */ &ud);
315 }
317 if (status != 0)
318 {
319 apache_free(st);
320 return (-1);
321 }
323 return (0);
324 } /* int config_add */
326 static int config (oconfig_item_t *ci)
327 {
328 int status = 0;
329 int i;
330 oconfig_item_t *lci = NULL; /* legacy config */
332 for (i = 0; i < ci->children_num; i++)
333 {
334 oconfig_item_t *child = ci->children + i;
336 if (strcasecmp ("Instance", child->key) == 0 && child->children_num > 0)
337 config_add (child);
338 else
339 {
340 /* legacy mode - convert to <Instance ...> config */
341 if (lci == NULL)
342 {
343 lci = malloc (sizeof(*lci));
344 if (lci == NULL)
345 {
346 ERROR ("apache plugin: malloc failed.");
347 return (-1);
348 }
349 memset (lci, '\0', sizeof (*lci));
350 }
352 lci->children_num++;
353 lci->children =
354 realloc (lci->children,
355 lci->children_num * sizeof (*child));
356 if (lci->children == NULL)
357 {
358 ERROR ("apache plugin: realloc failed.");
359 return (-1);
360 }
361 memcpy (&lci->children[lci->children_num-1], child, sizeof (*child));
362 }
363 } /* for (ci->children) */
365 if (lci)
366 {
367 /* create a <Instance ""> entry */
368 lci->key = "Instance";
369 lci->values_num = 1;
370 lci->values = (oconfig_value_t *) malloc (lci->values_num * sizeof (oconfig_value_t));
371 lci->values[0].type = OCONFIG_TYPE_STRING;
372 lci->values[0].value.string = "";
374 status = config_add (lci);
375 sfree (lci->values);
376 sfree (lci->children);
377 sfree (lci);
378 }
380 return status;
381 } /* int config */
383 /* initialize curl for each host */
384 static int init_host (apache_t *st) /* {{{ */
385 {
386 static char credentials[1024];
388 assert (st->url != NULL);
389 /* (Assured by `config_add') */
391 if (st->curl != NULL)
392 {
393 curl_easy_cleanup (st->curl);
394 st->curl = NULL;
395 }
397 if ((st->curl = curl_easy_init ()) == NULL)
398 {
399 ERROR ("apache plugin: init_host: `curl_easy_init' failed.");
400 return (-1);
401 }
403 curl_easy_setopt (st->curl, CURLOPT_WRITEFUNCTION, apache_curl_callback);
404 curl_easy_setopt (st->curl, CURLOPT_WRITEDATA, st);
406 st->server_type = -1; // not set as yet
407 // if the user specified string doesn't match apache or lighttpd, then
408 // ignore it. Headers will be parsed to find out the server type
409 if (st->server != NULL)
410 {
411 if (strcasecmp(st->server, "apache") == 0)
412 st->server_type = APACHE;
413 else if (strcasecmp(st->server, "lighttpd") == 0)
414 st->server_type = LIGHTTPD;
415 }
417 // if not found register a header callback to determine the
418 // 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 }
425 curl_easy_setopt (st->curl, CURLOPT_USERAGENT, PACKAGE_NAME"/"PACKAGE_VERSION);
426 curl_easy_setopt (st->curl, CURLOPT_ERRORBUFFER, st->apache_curl_error);
428 if (st->user != NULL)
429 {
430 int status;
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 }
444 curl_easy_setopt (st->curl, CURLOPT_USERPWD, credentials);
445 }
447 curl_easy_setopt (st->curl, CURLOPT_URL, st->url);
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 }
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 }
467 if (st->cacert != NULL)
468 {
469 curl_easy_setopt (st->curl, CURLOPT_CAINFO, st->cacert);
470 }
472 return (0);
473 } /* }}} int init_host */
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;
480 vl.values = &value;
481 vl.values_len = 1;
483 sstrncpy (vl.host, (st->host != NULL) ? st->host : hostname_g,
484 sizeof (vl.host));
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));
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));
496 plugin_dispatch_values (&vl);
497 } /* void submit_value */
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 */
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 */
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;
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;
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 }
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 }
602 static int apache_read_host (user_data_t *user_data) /* {{{ */
603 {
604 int i;
606 char *ptr;
607 char *saveptr;
608 char *lines[16];
609 int lines_num = 0;
611 char *fields[4];
612 int fields_num;
614 apache_t *st;
616 st = user_data->data;
618 assert (st->url != NULL);
619 /* (Assured by `config_add') */
621 if (st->curl == NULL)
622 {
623 int status;
625 status = init_host (st);
626 if (status != 0)
627 return (-1);
628 }
629 assert (st->curl != NULL);
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 }
639 // fallback - server_type to apache if not set at this time
640 if (st->server_type == -1)
641 st->server_type = APACHE;
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++;
650 if (lines_num >= 16)
651 break;
652 }
654 for (i = 0; i < lines_num; i++)
655 {
656 fields_num = strsplit (lines[i], fields, 4);
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 }
678 st->apache_buffer_fill = 0;
680 return (0);
681 } /* }}} int apache_read_host */
683 void module_register (void)
684 {
685 plugin_register_complex_config ("apache", config);
686 } /* void module_register */
688 /* vim: set sw=8 noet fdm=marker : */