7ccdb60d56a91963d5ca99e08ca9346c6695ea23
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 collectd.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"
29 #include "common.h"
30 #include "plugin.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 _Bool verify_peer;
49 _Bool verify_host;
50 char *cacert;
51 char *ssl_ciphers;
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 int timeout;
58 CURL *curl;
59 }; /* apache_s */
61 typedef struct apache_s apache_t;
63 /* TODO: Remove this prototype */
64 static int apache_read_host (user_data_t *user_data);
66 static void apache_free (void *arg)
67 {
68 apache_t *st = arg;
70 if (st == NULL)
71 return;
73 sfree (st->name);
74 sfree (st->host);
75 sfree (st->url);
76 sfree (st->user);
77 sfree (st->pass);
78 sfree (st->cacert);
79 sfree (st->ssl_ciphers);
80 sfree (st->server);
81 sfree (st->apache_buffer);
82 if (st->curl) {
83 curl_easy_cleanup(st->curl);
84 st->curl = NULL;
85 }
86 sfree (st);
87 } /* apache_free */
89 static size_t apache_curl_callback (void *buf, size_t size, size_t nmemb,
90 void *user_data)
91 {
92 size_t len = size * nmemb;
93 apache_t *st;
95 st = user_data;
96 if (st == NULL)
97 {
98 ERROR ("apache plugin: apache_curl_callback: "
99 "user_data pointer is NULL.");
100 return (0);
101 }
103 if (len == 0)
104 return (len);
106 if ((st->apache_buffer_fill + len) >= st->apache_buffer_size)
107 {
108 char *temp;
110 temp = realloc (st->apache_buffer,
111 st->apache_buffer_fill + len + 1);
112 if (temp == NULL)
113 {
114 ERROR ("apache plugin: realloc failed.");
115 return (0);
116 }
117 st->apache_buffer = temp;
118 st->apache_buffer_size = st->apache_buffer_fill + len + 1;
119 }
121 memcpy (st->apache_buffer + st->apache_buffer_fill, (char *) buf, len);
122 st->apache_buffer_fill += len;
123 st->apache_buffer[st->apache_buffer_fill] = 0;
125 return (len);
126 } /* int apache_curl_callback */
128 static size_t apache_header_callback (void *buf, size_t size, size_t nmemb,
129 void *user_data)
130 {
131 size_t len = size * nmemb;
132 apache_t *st;
134 st = user_data;
135 if (st == NULL)
136 {
137 ERROR ("apache plugin: apache_header_callback: "
138 "user_data pointer is NULL.");
139 return (0);
140 }
142 if (len == 0)
143 return (len);
145 /* look for the Server header */
146 if (strncasecmp (buf, "Server: ", strlen ("Server: ")) != 0)
147 return (len);
149 if (strstr (buf, "Apache") != NULL)
150 st->server_type = APACHE;
151 else if (strstr (buf, "lighttpd") != NULL)
152 st->server_type = LIGHTTPD;
153 else if (strstr (buf, "IBM_HTTP_Server") != NULL)
154 st->server_type = APACHE;
155 else
156 {
157 const char *hdr = buf;
159 hdr += strlen ("Server: ");
160 NOTICE ("apache plugin: Unknown server software: %s", hdr);
161 }
163 return (len);
164 } /* apache_header_callback */
166 /* Configuration handling functiions
167 * <Plugin apache>
168 * <Instance "instance_name">
169 * URL ...
170 * </Instance>
171 * URL ...
172 * </Plugin>
173 */
174 static int config_add (oconfig_item_t *ci)
175 {
176 apache_t *st;
177 int status;
179 st = calloc (1, sizeof (*st));
180 if (st == NULL)
181 {
182 ERROR ("apache plugin: calloc failed.");
183 return (-1);
184 }
186 st->timeout = -1;
188 status = cf_util_get_string (ci, &st->name);
189 if (status != 0)
190 {
191 sfree (st);
192 return (status);
193 }
194 assert (st->name != NULL);
196 for (int i = 0; i < ci->children_num; i++)
197 {
198 oconfig_item_t *child = ci->children + i;
200 if (strcasecmp ("URL", child->key) == 0)
201 status = cf_util_get_string (child, &st->url);
202 else if (strcasecmp ("Host", child->key) == 0)
203 status = cf_util_get_string (child, &st->host);
204 else if (strcasecmp ("User", child->key) == 0)
205 status = cf_util_get_string (child, &st->user);
206 else if (strcasecmp ("Password", child->key) == 0)
207 status = cf_util_get_string (child, &st->pass);
208 else if (strcasecmp ("VerifyPeer", child->key) == 0)
209 status = cf_util_get_boolean (child, &st->verify_peer);
210 else if (strcasecmp ("VerifyHost", child->key) == 0)
211 status = cf_util_get_boolean (child, &st->verify_host);
212 else if (strcasecmp ("CACert", child->key) == 0)
213 status = cf_util_get_string (child, &st->cacert);
214 else if (strcasecmp ("SSLCiphers", child->key) == 0)
215 status = cf_util_get_string (child, &st->ssl_ciphers);
216 else if (strcasecmp ("Server", child->key) == 0)
217 status = cf_util_get_string (child, &st->server);
218 else if (strcasecmp ("Timeout", child->key) == 0)
219 status = cf_util_get_int (child, &st->timeout);
220 else
221 {
222 WARNING ("apache plugin: Option `%s' not allowed here.",
223 child->key);
224 status = -1;
225 }
227 if (status != 0)
228 break;
229 }
231 /* Check if struct is complete.. */
232 if ((status == 0) && (st->url == NULL))
233 {
234 ERROR ("apache plugin: Instance `%s': "
235 "No URL has been configured.",
236 st->name);
237 status = -1;
238 }
240 if (status == 0)
241 {
242 char callback_name[3*DATA_MAX_NAME_LEN];
244 ssnprintf (callback_name, sizeof (callback_name),
245 "apache/%s/%s",
246 (st->host != NULL) ? st->host : hostname_g,
247 (st->name != NULL) ? st->name : "default");
249 status = plugin_register_complex_read (/* group = */ NULL,
250 /* name = */ callback_name,
251 /* callback = */ apache_read_host,
252 /* interval = */ 0,
253 &(user_data_t) {
254 .data = st,
255 .free_func = apache_free,
256 });
258 }
260 if (status != 0)
261 {
262 apache_free (st);
263 return (-1);
264 }
266 return (0);
267 } /* int config_add */
269 static int config (oconfig_item_t *ci)
270 {
271 int status = 0;
273 for (int i = 0; i < ci->children_num; i++)
274 {
275 oconfig_item_t *child = ci->children + i;
277 if (strcasecmp ("Instance", child->key) == 0)
278 config_add (child);
279 else
280 WARNING ("apache plugin: The configuration option "
281 "\"%s\" is not allowed here. Did you "
282 "forget to add an <Instance /> block "
283 "around the configuration?",
284 child->key);
285 } /* for (ci->children) */
287 return (status);
288 } /* int config */
290 /* initialize curl for each host */
291 static int init_host (apache_t *st) /* {{{ */
292 {
293 assert (st->url != NULL);
294 /* (Assured by `config_add') */
296 if (st->curl != NULL)
297 {
298 curl_easy_cleanup (st->curl);
299 st->curl = NULL;
300 }
302 if ((st->curl = curl_easy_init ()) == NULL)
303 {
304 ERROR ("apache plugin: init_host: `curl_easy_init' failed.");
305 return (-1);
306 }
308 curl_easy_setopt (st->curl, CURLOPT_NOSIGNAL, 1L);
309 curl_easy_setopt (st->curl, CURLOPT_WRITEFUNCTION, apache_curl_callback);
310 curl_easy_setopt (st->curl, CURLOPT_WRITEDATA, st);
312 /* not set as yet if the user specified string doesn't match apache or
313 * lighttpd, then ignore it. Headers will be parsed to find out the
314 * server type */
315 st->server_type = -1;
317 if (st->server != NULL)
318 {
319 if (strcasecmp(st->server, "apache") == 0)
320 st->server_type = APACHE;
321 else if (strcasecmp(st->server, "lighttpd") == 0)
322 st->server_type = LIGHTTPD;
323 else if (strcasecmp(st->server, "ibm_http_server") == 0)
324 st->server_type = APACHE;
325 else
326 WARNING ("apache plugin: Unknown `Server' setting: %s",
327 st->server);
328 }
330 /* if not found register a header callback to determine the server_type */
331 if (st->server_type == -1)
332 {
333 curl_easy_setopt (st->curl, CURLOPT_HEADERFUNCTION, apache_header_callback);
334 curl_easy_setopt (st->curl, CURLOPT_WRITEHEADER, st);
335 }
337 curl_easy_setopt (st->curl, CURLOPT_USERAGENT, COLLECTD_USERAGENT);
338 curl_easy_setopt (st->curl, CURLOPT_ERRORBUFFER, st->apache_curl_error);
340 if (st->user != NULL)
341 {
342 #ifdef HAVE_CURLOPT_USERNAME
343 curl_easy_setopt (st->curl, CURLOPT_USERNAME, st->user);
344 curl_easy_setopt (st->curl, CURLOPT_PASSWORD,
345 (st->pass == NULL) ? "" : st->pass);
346 #else
347 static char credentials[1024];
348 int status;
350 status = ssnprintf (credentials, sizeof (credentials), "%s:%s",
351 st->user, (st->pass == NULL) ? "" : st->pass);
352 if ((status < 0) || ((size_t) status >= sizeof (credentials)))
353 {
354 ERROR ("apache plugin: init_host: Returning an error "
355 "because the credentials have been "
356 "truncated.");
357 curl_easy_cleanup (st->curl);
358 st->curl = NULL;
359 return (-1);
360 }
362 curl_easy_setopt (st->curl, CURLOPT_USERPWD, credentials);
363 #endif
364 }
366 curl_easy_setopt (st->curl, CURLOPT_URL, st->url);
367 curl_easy_setopt (st->curl, CURLOPT_FOLLOWLOCATION, 1L);
368 curl_easy_setopt (st->curl, CURLOPT_MAXREDIRS, 50L);
370 curl_easy_setopt (st->curl, CURLOPT_SSL_VERIFYPEER,
371 (long) st->verify_peer);
372 curl_easy_setopt (st->curl, CURLOPT_SSL_VERIFYHOST,
373 st->verify_host ? 2L : 0L);
374 if (st->cacert != NULL)
375 curl_easy_setopt (st->curl, CURLOPT_CAINFO, st->cacert);
376 if (st->ssl_ciphers != NULL)
377 curl_easy_setopt (st->curl, CURLOPT_SSL_CIPHER_LIST,st->ssl_ciphers);
379 #ifdef HAVE_CURLOPT_TIMEOUT_MS
380 if (st->timeout >= 0)
381 curl_easy_setopt (st->curl, CURLOPT_TIMEOUT_MS, (long) st->timeout);
382 else
383 curl_easy_setopt (st->curl, CURLOPT_TIMEOUT_MS, (long) CDTIME_T_TO_MS(plugin_get_interval()));
384 #endif
386 return (0);
387 } /* }}} int init_host */
389 static void submit_value (const char *type, const char *type_instance,
390 value_t value, apache_t *st)
391 {
392 value_list_t vl = VALUE_LIST_INIT;
394 vl.values = &value;
395 vl.values_len = 1;
397 if (st->host != NULL)
398 sstrncpy (vl.host, st->host, sizeof (vl.host));
400 sstrncpy (vl.plugin, "apache", sizeof (vl.plugin));
401 if (st->name != NULL)
402 sstrncpy (vl.plugin_instance, st->name,
403 sizeof (vl.plugin_instance));
405 sstrncpy (vl.type, type, sizeof (vl.type));
406 if (type_instance != NULL)
407 sstrncpy (vl.type_instance, type_instance,
408 sizeof (vl.type_instance));
410 plugin_dispatch_values (&vl);
411 } /* void submit_value */
413 static void submit_derive (const char *type, const char *type_instance,
414 derive_t d, apache_t *st)
415 {
416 submit_value (type, type_instance, (value_t) { .derive = d }, st);
417 } /* void submit_derive */
419 static void submit_gauge (const char *type, const char *type_instance,
420 gauge_t g, apache_t *st)
421 {
422 submit_value (type, type_instance, (value_t) { .gauge = g }, st);
423 } /* void submit_gauge */
425 static void submit_scoreboard (char *buf, apache_t *st)
426 {
427 /*
428 * Scoreboard Key:
429 * "_" Waiting for Connection, "S" Starting up,
430 * "R" Reading Request for apache and read-POST for lighttpd,
431 * "W" Sending Reply, "K" Keepalive (read), "D" DNS Lookup,
432 * "C" Closing connection, "L" Logging, "G" Gracefully finishing,
433 * "I" Idle cleanup of worker, "." Open slot with no current process
434 * Lighttpd specific legends -
435 * "E" hard error, "." connect, "h" handle-request,
436 * "q" request-start, "Q" request-end, "s" response-start
437 * "S" response-end, "r" read
438 */
439 long long open = 0LL;
440 long long waiting = 0LL;
441 long long starting = 0LL;
442 long long reading = 0LL;
443 long long sending = 0LL;
444 long long keepalive = 0LL;
445 long long dnslookup = 0LL;
446 long long closing = 0LL;
447 long long logging = 0LL;
448 long long finishing = 0LL;
449 long long idle_cleanup = 0LL;
451 /* lighttpd specific */
452 long long hard_error = 0LL;
453 long long lighttpd_read = 0LL;
454 long long handle_request = 0LL;
455 long long request_start = 0LL;
456 long long request_end = 0LL;
457 long long response_start = 0LL;
458 long long response_end = 0LL;
460 for (int i = 0; buf[i] != '\0'; i++)
461 {
462 if (buf[i] == '.') open++;
463 else if (buf[i] == '_') waiting++;
464 else if (buf[i] == 'S') starting++;
465 else if (buf[i] == 'R') reading++;
466 else if (buf[i] == 'W') sending++;
467 else if (buf[i] == 'K') keepalive++;
468 else if (buf[i] == 'D') dnslookup++;
469 else if (buf[i] == 'C') closing++;
470 else if (buf[i] == 'L') logging++;
471 else if (buf[i] == 'G') finishing++;
472 else if (buf[i] == 'I') idle_cleanup++;
473 else if (buf[i] == 'r') lighttpd_read++;
474 else if (buf[i] == 'h') handle_request++;
475 else if (buf[i] == 'E') hard_error++;
476 else if (buf[i] == 'q') request_start++;
477 else if (buf[i] == 'Q') request_end++;
478 else if (buf[i] == 's') response_start++;
479 else if (buf[i] == 'S') response_end++;
480 }
482 if (st->server_type == APACHE)
483 {
484 submit_gauge ("apache_scoreboard", "open" , open, st);
485 submit_gauge ("apache_scoreboard", "waiting" , waiting, st);
486 submit_gauge ("apache_scoreboard", "starting" , starting, st);
487 submit_gauge ("apache_scoreboard", "reading" , reading, st);
488 submit_gauge ("apache_scoreboard", "sending" , sending, st);
489 submit_gauge ("apache_scoreboard", "keepalive", keepalive, st);
490 submit_gauge ("apache_scoreboard", "dnslookup", dnslookup, st);
491 submit_gauge ("apache_scoreboard", "closing" , closing, st);
492 submit_gauge ("apache_scoreboard", "logging" , logging, st);
493 submit_gauge ("apache_scoreboard", "finishing", finishing, st);
494 submit_gauge ("apache_scoreboard", "idle_cleanup", idle_cleanup, st);
495 }
496 else
497 {
498 submit_gauge ("apache_scoreboard", "connect" , open, st);
499 submit_gauge ("apache_scoreboard", "close" , closing, st);
500 submit_gauge ("apache_scoreboard", "hard_error" , hard_error, st);
501 submit_gauge ("apache_scoreboard", "read" , lighttpd_read, st);
502 submit_gauge ("apache_scoreboard", "read_post" , reading, st);
503 submit_gauge ("apache_scoreboard", "write" , sending, st);
504 submit_gauge ("apache_scoreboard", "handle_request", handle_request, st);
505 submit_gauge ("apache_scoreboard", "request_start" , request_start, st);
506 submit_gauge ("apache_scoreboard", "request_end" , request_end, st);
507 submit_gauge ("apache_scoreboard", "response_start", response_start, st);
508 submit_gauge ("apache_scoreboard", "response_end" , response_end, st);
509 }
510 }
512 static int apache_read_host (user_data_t *user_data) /* {{{ */
513 {
514 char *ptr;
515 char *saveptr;
516 char *line;
518 char *fields[4];
519 int fields_num;
521 apache_t *st;
523 st = user_data->data;
525 int status;
527 char *content_type;
528 static const char *text_plain = "text/plain";
530 assert (st->url != NULL);
531 /* (Assured by `config_add') */
533 if (st->curl == NULL)
534 {
535 status = init_host (st);
536 if (status != 0)
537 return (-1);
538 }
539 assert (st->curl != NULL);
541 st->apache_buffer_fill = 0;
542 if (curl_easy_perform (st->curl) != CURLE_OK)
543 {
544 ERROR ("apache: curl_easy_perform failed: %s",
545 st->apache_curl_error);
546 return (-1);
547 }
549 /* fallback - server_type to apache if not set at this time */
550 if (st->server_type == -1)
551 {
552 WARNING ("apache plugin: Unable to determine server software "
553 "automatically. Will assume Apache.");
554 st->server_type = APACHE;
555 }
557 status = curl_easy_getinfo (st->curl, CURLINFO_CONTENT_TYPE, &content_type);
558 if ((status == CURLE_OK) && (content_type != NULL) &&
559 (strncasecmp (content_type, text_plain, strlen (text_plain)) != 0))
560 {
561 WARNING ("apache plugin: `Content-Type' response header is not `%s' "
562 "(received: `%s'). Expecting unparseable data. Please check `URL' "
563 "parameter (missing `?auto' suffix ?)",
564 text_plain, content_type);
565 }
567 ptr = st->apache_buffer;
568 saveptr = NULL;
569 while ((line = strtok_r (ptr, "\n\r", &saveptr)) != NULL)
570 {
571 ptr = NULL;
572 fields_num = strsplit (line, fields, STATIC_ARRAY_SIZE (fields));
574 if (fields_num == 3)
575 {
576 if ((strcmp (fields[0], "Total") == 0) && (strcmp (fields[1], "Accesses:") == 0))
577 submit_derive ("apache_requests", "", atoll (fields[2]), st);
578 else if ((strcmp (fields[0], "Total") == 0) && (strcmp (fields[1], "kBytes:") == 0))
579 submit_derive ("apache_bytes", "", 1024LL * atoll (fields[2]), st);
580 }
581 else if (fields_num == 2)
582 {
583 if (strcmp (fields[0], "Scoreboard:") == 0)
584 submit_scoreboard (fields[1], st);
585 else if ((strcmp (fields[0], "BusyServers:") == 0) /* Apache 1.* */
586 || (strcmp (fields[0], "BusyWorkers:") == 0) /* Apache 2.* */)
587 submit_gauge ("apache_connections", NULL, atol (fields[1]), st);
588 else if ((strcmp (fields[0], "IdleServers:") == 0) /* Apache 1.x */
589 || (strcmp (fields[0], "IdleWorkers:") == 0) /* Apache 2.x */)
590 submit_gauge ("apache_idle_workers", NULL, atol (fields[1]), st);
591 }
592 }
594 st->apache_buffer_fill = 0;
596 return (0);
597 } /* }}} int apache_read_host */
599 static int apache_init (void) /* {{{ */
600 {
601 /* Call this while collectd is still single-threaded to avoid
602 * initialization issues in libgcrypt. */
603 curl_global_init (CURL_GLOBAL_SSL);
604 return (0);
605 } /* }}} int apache_init */
607 void module_register (void)
608 {
609 plugin_register_complex_config ("apache", config);
610 plugin_register_init ("apache", apache_init);
611 } /* void module_register */
613 /* vim: set sw=8 noet fdm=marker : */