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"
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 _Bool verify_peer;
49 _Bool 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 if (strstr (buf, "IBM_HTTP_Server") != NULL)
148 st->server_type = APACHE;
149 else
150 {
151 const char *hdr = buf;
153 hdr += strlen ("Server: ");
154 NOTICE ("apache plugin: Unknown server software: %s", hdr);
155 }
157 return (len);
158 } /* apache_header_callback */
160 /* Configuration handling functiions
161 * <Plugin apache>
162 * <Instance "instance_name">
163 * URL ...
164 * </Instance>
165 * URL ...
166 * </Plugin>
167 */
168 static int config_add (oconfig_item_t *ci)
169 {
170 apache_t *st;
171 int i;
172 int status;
174 st = malloc (sizeof (*st));
175 if (st == NULL)
176 {
177 ERROR ("apache plugin: malloc failed.");
178 return (-1);
179 }
180 memset (st, 0, sizeof (*st));
182 status = cf_util_get_string (ci, &st->name);
183 if (status != 0)
184 {
185 sfree (st);
186 return (status);
187 }
188 assert (st->name != NULL);
190 for (i = 0; i < ci->children_num; i++)
191 {
192 oconfig_item_t *child = ci->children + i;
194 if (strcasecmp ("URL", child->key) == 0)
195 status = cf_util_get_string (child, &st->url);
196 else if (strcasecmp ("Host", child->key) == 0)
197 status = cf_util_get_string (child, &st->host);
198 else if (strcasecmp ("User", child->key) == 0)
199 status = cf_util_get_string (child, &st->user);
200 else if (strcasecmp ("Password", child->key) == 0)
201 status = cf_util_get_string (child, &st->pass);
202 else if (strcasecmp ("VerifyPeer", child->key) == 0)
203 status = cf_util_get_boolean (child, &st->verify_peer);
204 else if (strcasecmp ("VerifyHost", child->key) == 0)
205 status = cf_util_get_boolean (child, &st->verify_host);
206 else if (strcasecmp ("CACert", child->key) == 0)
207 status = cf_util_get_string (child, &st->cacert);
208 else if (strcasecmp ("Server", child->key) == 0)
209 status = cf_util_get_string (child, &st->server);
210 else
211 {
212 WARNING ("apache plugin: Option `%s' not allowed here.",
213 child->key);
214 status = -1;
215 }
217 if (status != 0)
218 break;
219 }
221 /* Check if struct is complete.. */
222 if ((status == 0) && (st->url == NULL))
223 {
224 ERROR ("apache plugin: Instance `%s': "
225 "No URL has been configured.",
226 st->name);
227 status = -1;
228 }
230 if (status == 0)
231 {
232 user_data_t ud;
233 char callback_name[3*DATA_MAX_NAME_LEN];
235 memset (&ud, 0, sizeof (ud));
236 ud.data = st;
237 ud.free_func = (void *) apache_free;
239 memset (callback_name, 0, sizeof (callback_name));
240 ssnprintf (callback_name, sizeof (callback_name),
241 "apache/%s/%s",
242 (st->host != NULL) ? st->host : hostname_g,
243 (st->name != NULL) ? st->name : "default"),
245 status = plugin_register_complex_read (/* group = */ NULL,
246 /* name = */ callback_name,
247 /* callback = */ apache_read_host,
248 /* interval = */ NULL,
249 /* user_data = */ &ud);
250 }
252 if (status != 0)
253 {
254 apache_free (st);
255 return (-1);
256 }
258 return (0);
259 } /* int config_add */
261 static int config (oconfig_item_t *ci)
262 {
263 int status = 0;
264 int i;
266 for (i = 0; i < ci->children_num; i++)
267 {
268 oconfig_item_t *child = ci->children + i;
270 if (strcasecmp ("Instance", child->key) == 0)
271 config_add (child);
272 else
273 WARNING ("apache plugin: The configuration option "
274 "\"%s\" is not allowed here. Did you "
275 "forget to add an <Instance /> block "
276 "around the configuration?",
277 child->key);
278 } /* for (ci->children) */
280 return (status);
281 } /* int config */
283 /* initialize curl for each host */
284 static int init_host (apache_t *st) /* {{{ */
285 {
286 static char credentials[1024];
288 assert (st->url != NULL);
289 /* (Assured by `config_add') */
291 if (st->curl != NULL)
292 {
293 curl_easy_cleanup (st->curl);
294 st->curl = NULL;
295 }
297 if ((st->curl = curl_easy_init ()) == NULL)
298 {
299 ERROR ("apache plugin: init_host: `curl_easy_init' failed.");
300 return (-1);
301 }
303 curl_easy_setopt (st->curl, CURLOPT_NOSIGNAL, 1L);
304 curl_easy_setopt (st->curl, CURLOPT_WRITEFUNCTION, apache_curl_callback);
305 curl_easy_setopt (st->curl, CURLOPT_WRITEDATA, st);
307 /* not set as yet if the user specified string doesn't match apache or
308 * lighttpd, then ignore it. Headers will be parsed to find out the
309 * server type */
310 st->server_type = -1;
312 if (st->server != NULL)
313 {
314 if (strcasecmp(st->server, "apache") == 0)
315 st->server_type = APACHE;
316 else if (strcasecmp(st->server, "lighttpd") == 0)
317 st->server_type = LIGHTTPD;
318 else if (strcasecmp(st->server, "ibm_http_server") == 0)
319 st->server_type = APACHE;
320 else
321 WARNING ("apache plugin: Unknown `Server' setting: %s",
322 st->server);
323 }
325 /* if not found register a header callback to determine the server_type */
326 if (st->server_type == -1)
327 {
328 curl_easy_setopt (st->curl, CURLOPT_HEADERFUNCTION, apache_header_callback);
329 curl_easy_setopt (st->curl, CURLOPT_WRITEHEADER, st);
330 }
332 curl_easy_setopt (st->curl, CURLOPT_USERAGENT, COLLECTD_USERAGENT);
333 curl_easy_setopt (st->curl, CURLOPT_ERRORBUFFER, st->apache_curl_error);
335 if (st->user != NULL)
336 {
337 int status;
339 status = ssnprintf (credentials, sizeof (credentials), "%s:%s",
340 st->user, (st->pass == NULL) ? "" : st->pass);
341 if ((status < 0) || ((size_t) status >= sizeof (credentials)))
342 {
343 ERROR ("apache plugin: init_host: Returning an error "
344 "because the credentials have been "
345 "truncated.");
346 curl_easy_cleanup (st->curl);
347 st->curl = NULL;
348 return (-1);
349 }
351 curl_easy_setopt (st->curl, CURLOPT_USERPWD, credentials);
352 }
354 curl_easy_setopt (st->curl, CURLOPT_URL, st->url);
355 curl_easy_setopt (st->curl, CURLOPT_FOLLOWLOCATION, 1L);
356 curl_easy_setopt (st->curl, CURLOPT_MAXREDIRS, 50L);
358 curl_easy_setopt (st->curl, CURLOPT_SSL_VERIFYPEER,
359 (long) st->verify_peer);
360 curl_easy_setopt (st->curl, CURLOPT_SSL_VERIFYHOST,
361 st->verify_host ? 2L : 0L);
362 if (st->cacert != NULL)
363 curl_easy_setopt (st->curl, CURLOPT_CAINFO, st->cacert);
365 return (0);
366 } /* }}} int init_host */
368 static void submit_value (const char *type, const char *type_instance,
369 value_t value, apache_t *st)
370 {
371 value_list_t vl = VALUE_LIST_INIT;
373 vl.values = &value;
374 vl.values_len = 1;
376 sstrncpy (vl.host, (st->host != NULL) ? st->host : hostname_g,
377 sizeof (vl.host));
379 sstrncpy (vl.plugin, "apache", sizeof (vl.plugin));
380 if (st->name != NULL)
381 sstrncpy (vl.plugin_instance, st->name,
382 sizeof (vl.plugin_instance));
384 sstrncpy (vl.type, type, sizeof (vl.type));
385 if (type_instance != NULL)
386 sstrncpy (vl.type_instance, type_instance,
387 sizeof (vl.type_instance));
389 plugin_dispatch_values (&vl);
390 } /* void submit_value */
392 static void submit_derive (const char *type, const char *type_instance,
393 derive_t c, apache_t *st)
394 {
395 value_t v;
396 v.derive = c;
397 submit_value (type, type_instance, v, st);
398 } /* void submit_derive */
400 static void submit_gauge (const char *type, const char *type_instance,
401 gauge_t g, apache_t *st)
402 {
403 value_t v;
404 v.gauge = g;
405 submit_value (type, type_instance, v, st);
406 } /* void submit_gauge */
408 static void submit_scoreboard (char *buf, apache_t *st)
409 {
410 /*
411 * Scoreboard Key:
412 * "_" Waiting for Connection, "S" Starting up,
413 * "R" Reading Request for apache and read-POST for lighttpd,
414 * "W" Sending Reply, "K" Keepalive (read), "D" DNS Lookup,
415 * "C" Closing connection, "L" Logging, "G" Gracefully finishing,
416 * "I" Idle cleanup of worker, "." Open slot with no current process
417 * Lighttpd specific legends -
418 * "E" hard error, "." connect, "h" handle-request,
419 * "q" request-start, "Q" request-end, "s" response-start
420 * "S" response-end, "r" read
421 */
422 long long open = 0LL;
423 long long waiting = 0LL;
424 long long starting = 0LL;
425 long long reading = 0LL;
426 long long sending = 0LL;
427 long long keepalive = 0LL;
428 long long dnslookup = 0LL;
429 long long closing = 0LL;
430 long long logging = 0LL;
431 long long finishing = 0LL;
432 long long idle_cleanup = 0LL;
434 /* lighttpd specific */
435 long long hard_error = 0LL;
436 long long lighttpd_read = 0LL;
437 long long handle_request = 0LL;
438 long long request_start = 0LL;
439 long long request_end = 0LL;
440 long long response_start = 0LL;
441 long long response_end = 0LL;
443 int i;
444 for (i = 0; buf[i] != '\0'; i++)
445 {
446 if (buf[i] == '.') open++;
447 else if (buf[i] == '_') waiting++;
448 else if (buf[i] == 'S') starting++;
449 else if (buf[i] == 'R') reading++;
450 else if (buf[i] == 'W') sending++;
451 else if (buf[i] == 'K') keepalive++;
452 else if (buf[i] == 'D') dnslookup++;
453 else if (buf[i] == 'C') closing++;
454 else if (buf[i] == 'L') logging++;
455 else if (buf[i] == 'G') finishing++;
456 else if (buf[i] == 'I') idle_cleanup++;
457 else if (buf[i] == 'r') lighttpd_read++;
458 else if (buf[i] == 'h') handle_request++;
459 else if (buf[i] == 'E') hard_error++;
460 else if (buf[i] == 'q') request_start++;
461 else if (buf[i] == 'Q') request_end++;
462 else if (buf[i] == 's') response_start++;
463 else if (buf[i] == 'S') response_end++;
464 }
466 if (st->server_type == APACHE)
467 {
468 submit_gauge ("apache_scoreboard", "open" , open, st);
469 submit_gauge ("apache_scoreboard", "waiting" , waiting, st);
470 submit_gauge ("apache_scoreboard", "starting" , starting, st);
471 submit_gauge ("apache_scoreboard", "reading" , reading, st);
472 submit_gauge ("apache_scoreboard", "sending" , sending, st);
473 submit_gauge ("apache_scoreboard", "keepalive", keepalive, st);
474 submit_gauge ("apache_scoreboard", "dnslookup", dnslookup, st);
475 submit_gauge ("apache_scoreboard", "closing" , closing, st);
476 submit_gauge ("apache_scoreboard", "logging" , logging, st);
477 submit_gauge ("apache_scoreboard", "finishing", finishing, st);
478 submit_gauge ("apache_scoreboard", "idle_cleanup", idle_cleanup, st);
479 }
480 else
481 {
482 submit_gauge ("apache_scoreboard", "connect" , open, st);
483 submit_gauge ("apache_scoreboard", "close" , closing, st);
484 submit_gauge ("apache_scoreboard", "hard_error" , hard_error, st);
485 submit_gauge ("apache_scoreboard", "read" , lighttpd_read, st);
486 submit_gauge ("apache_scoreboard", "read_post" , reading, st);
487 submit_gauge ("apache_scoreboard", "write" , sending, st);
488 submit_gauge ("apache_scoreboard", "handle_request", handle_request, st);
489 submit_gauge ("apache_scoreboard", "request_start" , request_start, st);
490 submit_gauge ("apache_scoreboard", "request_end" , request_end, st);
491 submit_gauge ("apache_scoreboard", "response_start", response_start, st);
492 submit_gauge ("apache_scoreboard", "response_end" , response_end, st);
493 }
494 }
496 static int apache_read_host (user_data_t *user_data) /* {{{ */
497 {
498 int i;
500 char *ptr;
501 char *saveptr;
502 char *lines[16];
503 int lines_num = 0;
505 char *fields[4];
506 int fields_num;
508 apache_t *st;
510 st = user_data->data;
512 assert (st->url != NULL);
513 /* (Assured by `config_add') */
515 if (st->curl == NULL)
516 {
517 int status;
519 status = init_host (st);
520 if (status != 0)
521 return (-1);
522 }
523 assert (st->curl != NULL);
525 st->apache_buffer_fill = 0;
526 if (curl_easy_perform (st->curl) != CURLE_OK)
527 {
528 ERROR ("apache: curl_easy_perform failed: %s",
529 st->apache_curl_error);
530 return (-1);
531 }
533 /* fallback - server_type to apache if not set at this time */
534 if (st->server_type == -1)
535 {
536 WARNING ("apache plugin: Unable to determine server software "
537 "automatically. Will assume Apache.");
538 st->server_type = APACHE;
539 }
541 ptr = st->apache_buffer;
542 saveptr = NULL;
543 while ((lines[lines_num] = strtok_r (ptr, "\n\r", &saveptr)) != NULL)
544 {
545 ptr = NULL;
546 lines_num++;
548 if (lines_num >= 16)
549 break;
550 }
552 for (i = 0; i < lines_num; i++)
553 {
554 fields_num = strsplit (lines[i], fields, 4);
556 if (fields_num == 3)
557 {
558 if ((strcmp (fields[0], "Total") == 0)
559 && (strcmp (fields[1], "Accesses:") == 0))
560 submit_derive ("apache_requests", "",
561 atoll (fields[2]), st);
562 else if ((strcmp (fields[0], "Total") == 0)
563 && (strcmp (fields[1], "kBytes:") == 0))
564 submit_derive ("apache_bytes", "",
565 1024LL * atoll (fields[2]), st);
566 }
567 else if (fields_num == 2)
568 {
569 if (strcmp (fields[0], "Scoreboard:") == 0)
570 submit_scoreboard (fields[1], st);
571 else if ((strcmp (fields[0], "BusyServers:") == 0) /* Apache 1.* */
572 || (strcmp (fields[0], "BusyWorkers:") == 0) /* Apache 2.* */)
573 submit_gauge ("apache_connections", NULL, atol (fields[1]), st);
574 else if ((strcmp (fields[0], "IdleServers:") == 0) /* Apache 1.x */
575 || (strcmp (fields[0], "IdleWorkers:") == 0) /* Apache 2.x */)
576 submit_gauge ("apache_idle_workers", NULL, atol (fields[1]), st);
577 }
578 }
580 st->apache_buffer_fill = 0;
582 return (0);
583 } /* }}} int apache_read_host */
585 static int apache_init (void) /* {{{ */
586 {
587 /* Call this while collectd is still single-threaded to avoid
588 * initialization issues in libgcrypt. */
589 curl_global_init (CURL_GLOBAL_SSL);
590 return (0);
591 } /* }}} int apache_init */
593 void module_register (void)
594 {
595 plugin_register_complex_config ("apache", config);
596 plugin_register_init ("apache", apache_init);
597 } /* void module_register */
599 /* vim: set sw=8 noet fdm=marker : */