f36f5141a0db30b6c3a2ce47abb645a158009924
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 struct apache_s
35 {
36 char *name;
37 char *host;
38 char *url;
39 char *user;
40 char *pass;
41 char *verify_peer;
42 char *verify_host;
43 char *cacert;
44 char *apache_buffer;
45 char apache_curl_error[CURL_ERROR_SIZE];
46 size_t apache_buffer_size;
47 size_t apache_buffer_fill;
48 CURL *curl;
49 }; /* apache_s */
51 typedef struct apache_s apache_t;
53 /* TODO: Remove this prototype */
54 static int apache_read_host (user_data_t *user_data);
56 static void apache_free (apache_t *st)
57 {
58 if (st == NULL)
59 return;
61 sfree (st->name);
62 sfree (st->host);
63 sfree (st->url);
64 sfree (st->user);
65 sfree (st->pass);
66 sfree (st->verify_peer);
67 sfree (st->verify_host);
68 sfree (st->cacert);
69 sfree (st->apache_buffer);
70 if (st->curl) {
71 curl_easy_cleanup(st->curl);
72 st->curl = NULL;
73 }
74 } /* apache_free */
76 static size_t apache_curl_callback (void *buf, size_t size, size_t nmemb,
77 void *user_data)
78 {
79 size_t len = size * nmemb;
80 apache_t *st;
82 st = user_data;
83 if (st == NULL)
84 {
85 ERROR ("apache plugin: apache_curl_callback: "
86 "user_data pointer is NULL.");
87 return (0);
88 }
90 if (len <= 0)
91 return (len);
93 if ((st->apache_buffer_fill + len) >= st->apache_buffer_size)
94 {
95 char *temp;
97 temp = (char *) realloc (st->apache_buffer,
98 st->apache_buffer_fill + len + 1);
99 if (temp == NULL)
100 {
101 ERROR ("apache plugin: realloc failed.");
102 return (0);
103 }
104 st->apache_buffer = temp;
105 st->apache_buffer_size = st->apache_buffer_fill + len + 1;
106 }
108 memcpy (st->apache_buffer + st->apache_buffer_fill, (char *) buf, len);
109 st->apache_buffer_fill += len;
110 st->apache_buffer[st->apache_buffer_fill] = 0;
112 return (len);
113 } /* int apache_curl_callback */
115 /* Configuration handling functiions
116 * <Plugin apache>
117 * <Instance "instance_name">
118 * URL ...
119 * </Instance>
120 * URL ...
121 * </Plugin>
122 */
123 static int config_set_string (char **ret_string,
124 oconfig_item_t *ci)
125 {
126 char *string;
128 if ((ci->values_num != 1)
129 || (ci->values[0].type != OCONFIG_TYPE_STRING))
130 {
131 WARNING ("apache plugin: The `%s' config option "
132 "needs exactly one string argument.", ci->key);
133 return (-1);
134 }
136 string = strdup (ci->values[0].value.string);
137 if (string == NULL)
138 {
139 ERROR ("apache plugin: strdup failed.");
140 return (-1);
141 }
143 if (*ret_string != NULL)
144 free (*ret_string);
145 *ret_string = string;
147 return (0);
148 } /* int config_set_string */
150 static int config_add (oconfig_item_t *ci)
151 {
152 apache_t *st;
153 int i;
154 int status;
156 if ((ci->values_num != 1)
157 || (ci->values[0].type != OCONFIG_TYPE_STRING))
158 {
159 WARNING ("apache plugin: The `%s' config option "
160 "needs exactly one string argument.", ci->key);
161 return (-1);
162 }
164 st = (apache_t *) malloc (sizeof (*st));
165 if (st == NULL)
166 {
167 ERROR ("apache plugin: malloc failed.");
168 return (-1);
169 }
171 memset (st, 0, sizeof (*st));
173 status = config_set_string (&st->name, ci);
174 if (status != 0)
175 {
176 sfree (st);
177 return (status);
178 }
180 for (i = 0; i < ci->children_num; i++)
181 {
182 oconfig_item_t *child = ci->children + i;
184 if (strcasecmp ("URL", child->key) == 0)
185 status = config_set_string (&st->url, child);
186 else if (strcasecmp ("Host", child->key) == 0)
187 status = config_set_string (&st->host, child);
188 else if (strcasecmp ("User", child->key) == 0)
189 status = config_set_string (&st->user, child);
190 else if (strcasecmp ("Password", child->key) == 0)
191 status = config_set_string (&st->pass, child);
192 else if (strcasecmp ("VerifyPeer", child->key) == 0)
193 status = config_set_string (&st->verify_peer, child);
194 else if (strcasecmp ("VerifyHost", child->key) == 0)
195 status = config_set_string (&st->verify_host, child);
196 else if (strcasecmp ("CACert", child->key) == 0)
197 status = config_set_string (&st->cacert, child);
198 else
199 {
200 WARNING ("apache plugin: Option `%s' not allowed here.", child->key);
201 status = -1;
202 }
204 if (status != 0)
205 break;
206 }
208 if (status == 0)
209 {
210 user_data_t ud;
211 char callback_name[3*DATA_MAX_NAME_LEN];
213 memset (&ud, 0, sizeof (ud));
214 ud.data = st;
215 ud.free_func = (void *) apache_free;
217 memset (callback_name, 0, sizeof (callback_name));
218 ssnprintf (callback_name, sizeof (callback_name),
219 "apache/%s/%s",
220 (st->host != NULL) ? st->host : hostname_g,
221 (st->name != NULL) ? st->name : "default"),
223 status = plugin_register_complex_read (callback_name,
224 /* callback = */ apache_read_host,
225 /* interval = */ NULL,
226 /* user_data = */ &ud);
227 }
229 if (status != 0)
230 {
231 apache_free(st);
232 return (-1);
233 }
235 return (0);
236 } /* int config_add */
238 static int config (oconfig_item_t *ci)
239 {
240 int status = 0;
241 int i;
242 oconfig_item_t *lci = NULL; /* legacy config */
244 for (i = 0; i < ci->children_num; i++)
245 {
246 oconfig_item_t *child = ci->children + i;
248 if (strcasecmp ("Instance", child->key) == 0 && child->children_num > 0)
249 config_add (child);
250 else
251 {
252 /* legacy mode - convert to <Instance ...> config */
253 if (lci == NULL)
254 {
255 lci = malloc (sizeof(*lci));
256 if (lci == NULL)
257 {
258 ERROR ("apache plugin: malloc failed.");
259 return (-1);
260 }
261 memset (lci, '\0', sizeof (*lci));
262 }
264 lci->children_num++;
265 lci->children =
266 realloc (lci->children,
267 lci->children_num * sizeof (*child));
268 if (lci->children == NULL)
269 {
270 ERROR ("apache plugin: realloc failed.");
271 return (-1);
272 }
273 memcpy (&lci->children[lci->children_num-1], child, sizeof (*child));
274 }
275 } /* for (ci->children) */
277 if (lci)
278 {
279 /* create a <Instance ""> entry */
280 lci->key = "Instance";
281 lci->values_num = 1;
282 lci->values = (oconfig_value_t *) malloc (lci->values_num * sizeof (oconfig_value_t));
283 lci->values[0].type = OCONFIG_TYPE_STRING;
284 lci->values[0].value.string = "";
286 status = config_add (lci);
287 sfree (lci->values);
288 sfree (lci->children);
289 sfree (lci);
290 }
292 return status;
293 } /* int config */
295 /* initialize curl for each host */
296 static int init_host (apache_t *st) /* {{{ */
297 {
298 static char credentials[1024];
300 if (st->url == NULL)
301 {
302 WARNING ("apache plugin: init_host: No URL configured, returning "
303 "an error.");
304 return (-1);
305 }
307 if (st->curl != NULL)
308 {
309 curl_easy_cleanup (st->curl);
310 }
312 if ((st->curl = curl_easy_init ()) == NULL)
313 {
314 ERROR ("apache plugin: init_host: `curl_easy_init' failed.");
315 return (-1);
316 }
318 curl_easy_setopt (st->curl, CURLOPT_WRITEFUNCTION, apache_curl_callback);
319 curl_easy_setopt (st->curl, CURLOPT_WRITEDATA, st);
320 curl_easy_setopt (st->curl, CURLOPT_USERAGENT, PACKAGE_NAME"/"PACKAGE_VERSION);
321 curl_easy_setopt (st->curl, CURLOPT_ERRORBUFFER, st->apache_curl_error);
323 if (st->user != NULL)
324 {
325 int status;
327 status = ssnprintf (credentials, sizeof (credentials), "%s:%s",
328 st->user, (st->pass == NULL) ? "" : st->pass);
329 if ((status < 0) || ((size_t) status >= sizeof (credentials)))
330 {
331 ERROR ("apache plugin: init_host: Returning an error "
332 "because the credentials have been "
333 "truncated.");
334 curl_easy_cleanup (st->curl);
335 st->curl = NULL;
336 return (-1);
337 }
339 curl_easy_setopt (st->curl, CURLOPT_USERPWD, credentials);
340 }
342 curl_easy_setopt (st->curl, CURLOPT_URL, st->url);
344 if ((st->verify_peer == NULL) || (strcmp (st->verify_peer, "true") == 0))
345 {
346 curl_easy_setopt (st->curl, CURLOPT_SSL_VERIFYPEER, 1);
347 }
348 else
349 {
350 curl_easy_setopt (st->curl, CURLOPT_SSL_VERIFYPEER, 0);
351 }
353 if ((st->verify_host == NULL) || (strcmp (st->verify_host, "true") == 0))
354 {
355 curl_easy_setopt (st->curl, CURLOPT_SSL_VERIFYHOST, 2);
356 }
357 else
358 {
359 curl_easy_setopt (st->curl, CURLOPT_SSL_VERIFYHOST, 0);
360 }
362 if (st->cacert != NULL)
363 {
364 curl_easy_setopt (st->curl, CURLOPT_CAINFO, st->cacert);
365 }
367 return (0);
368 } /* }}} int init_host */
370 static void submit_value (const char *type, const char *type_instance,
371 value_t value, apache_t *st)
372 {
373 value_list_t vl = VALUE_LIST_INIT;
375 vl.values = &value;
376 vl.values_len = 1;
378 sstrncpy (vl.host, (st->host != NULL) ? st->host : hostname_g,
379 sizeof (vl.host));
381 sstrncpy (vl.plugin, "apache", sizeof (vl.plugin));
382 if (st->name != NULL)
383 sstrncpy (vl.plugin_instance, st->name,
384 sizeof (vl.plugin_instance));
386 sstrncpy (vl.type, type, sizeof (vl.type));
387 if (type_instance != NULL)
388 sstrncpy (vl.type_instance, type_instance,
389 sizeof (vl.type_instance));
391 plugin_dispatch_values (&vl);
392 } /* void submit_value */
394 static void submit_counter (const char *type, const char *type_instance,
395 counter_t c, apache_t *st)
396 {
397 value_t v;
398 v.counter = c;
399 submit_value (type, type_instance, v, st);
400 } /* void submit_counter */
402 static void submit_gauge (const char *type, const char *type_instance,
403 gauge_t g, apache_t *st)
404 {
405 value_t v;
406 v.gauge = g;
407 submit_value (type, type_instance, v, st);
408 } /* void submit_gauge */
410 static void submit_scoreboard (char *buf, apache_t *st)
411 {
412 /*
413 * Scoreboard Key:
414 * "_" Waiting for Connection, "S" Starting up, "R" Reading Request,
415 * "W" Sending Reply, "K" Keepalive (read), "D" DNS Lookup,
416 * "C" Closing connection, "L" Logging, "G" Gracefully finishing,
417 * "I" Idle cleanup of worker, "." Open slot with no current process
418 */
419 long long open = 0LL;
420 long long waiting = 0LL;
421 long long starting = 0LL;
422 long long reading = 0LL;
423 long long sending = 0LL;
424 long long keepalive = 0LL;
425 long long dnslookup = 0LL;
426 long long closing = 0LL;
427 long long logging = 0LL;
428 long long finishing = 0LL;
429 long long idle_cleanup = 0LL;
431 int i;
433 for (i = 0; buf[i] != '\0'; i++)
434 {
435 if (buf[i] == '.') open++;
436 else if (buf[i] == '_') waiting++;
437 else if (buf[i] == 'S') starting++;
438 else if (buf[i] == 'R') reading++;
439 else if (buf[i] == 'W') sending++;
440 else if (buf[i] == 'K') keepalive++;
441 else if (buf[i] == 'D') dnslookup++;
442 else if (buf[i] == 'C') closing++;
443 else if (buf[i] == 'L') logging++;
444 else if (buf[i] == 'G') finishing++;
445 else if (buf[i] == 'I') idle_cleanup++;
446 }
448 submit_gauge ("apache_scoreboard", "open" , open, st);
449 submit_gauge ("apache_scoreboard", "waiting" , waiting, st);
450 submit_gauge ("apache_scoreboard", "starting" , starting, st);
451 submit_gauge ("apache_scoreboard", "reading" , reading, st);
452 submit_gauge ("apache_scoreboard", "sending" , sending, st);
453 submit_gauge ("apache_scoreboard", "keepalive", keepalive, st);
454 submit_gauge ("apache_scoreboard", "dnslookup", dnslookup, st);
455 submit_gauge ("apache_scoreboard", "closing" , closing, st);
456 submit_gauge ("apache_scoreboard", "logging" , logging, st);
457 submit_gauge ("apache_scoreboard", "finishing", finishing, st);
458 submit_gauge ("apache_scoreboard", "idle_cleanup", idle_cleanup, st);
459 }
461 static int apache_read_host (user_data_t *user_data) /* {{{ */
462 {
463 int i;
465 char *ptr;
466 char *saveptr;
467 char *lines[16];
468 int lines_num = 0;
470 char *fields[4];
471 int fields_num;
473 apache_t *st;
475 st = user_data->data;
477 if (st->curl == NULL)
478 {
479 int status;
481 status = init_host (st);
482 if (status != 0)
483 return (-1);
484 }
485 assert (st->curl != NULL);
487 if (st->url == NULL)
488 return (-1);
490 st->apache_buffer_fill = 0;
491 if (curl_easy_perform (st->curl) != 0)
492 {
493 ERROR ("apache: curl_easy_perform failed: %s",
494 st->apache_curl_error);
495 return (-1);
496 }
498 ptr = st->apache_buffer;
499 saveptr = NULL;
500 while ((lines[lines_num] = strtok_r (ptr, "\n\r", &saveptr)) != NULL)
501 {
502 ptr = NULL;
503 lines_num++;
505 if (lines_num >= 16)
506 break;
507 }
509 for (i = 0; i < lines_num; i++)
510 {
511 fields_num = strsplit (lines[i], fields, 4);
513 if (fields_num == 3)
514 {
515 if ((strcmp (fields[0], "Total") == 0)
516 && (strcmp (fields[1], "Accesses:") == 0))
517 submit_counter ("apache_requests", "",
518 atoll (fields[2]), st);
519 else if ((strcmp (fields[0], "Total") == 0)
520 && (strcmp (fields[1], "kBytes:") == 0))
521 submit_counter ("apache_bytes", "",
522 1024LL * atoll (fields[2]), st);
523 }
524 else if (fields_num == 2)
525 {
526 if (strcmp (fields[0], "Scoreboard:") == 0)
527 submit_scoreboard (fields[1], st);
528 else if (strcmp (fields[0], "BusyServers:") == 0)
529 submit_gauge ("apache_connections", NULL, atol (fields[1]), st);
530 }
531 }
533 st->apache_buffer_fill = 0;
535 return (0);
536 } /* }}} int apache_read_host */
538 void module_register (void)
539 {
540 plugin_register_complex_config ("apache", config);
541 } /* void module_register */
543 /* vim: set sw=8 noet fdm=marker : */