Code

0c6318e30f31fd02b75a6f7ecf19e1dea334ce8b
[pkg-collectd.git] / src / apache.c
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 *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 (apache_t *st)
67 {
68         if (st == NULL)
69                 return;
71         sfree (st->name);
72         sfree (st->host);
73         sfree (st->url);
74         sfree (st->user);
75         sfree (st->pass);
76         sfree (st->cacert);
77         sfree (st->ssl_ciphers);
78         sfree (st->server);
79         sfree (st->apache_buffer);
80         if (st->curl) {
81                 curl_easy_cleanup(st->curl);
82                 st->curl = NULL;
83         }
84 } /* apache_free */
86 static size_t apache_curl_callback (void *buf, size_t size, size_t nmemb,
87                 void *user_data)
88 {
89         size_t len = size * nmemb;
90         apache_t *st;
92         st = user_data;
93         if (st == NULL)
94         {
95                 ERROR ("apache plugin: apache_curl_callback: "
96                                 "user_data pointer is NULL.");
97                 return (0);
98         }
100         if (len <= 0)
101                 return (len);
103         if ((st->apache_buffer_fill + len) >= st->apache_buffer_size)
104         {
105                 char *temp;
107                 temp = (char *) realloc (st->apache_buffer,
108                                 st->apache_buffer_fill + len + 1);
109                 if (temp == NULL)
110                 {
111                         ERROR ("apache plugin: realloc failed.");
112                         return (0);
113                 }
114                 st->apache_buffer = temp;
115                 st->apache_buffer_size = st->apache_buffer_fill + len + 1;
116         }
118         memcpy (st->apache_buffer + st->apache_buffer_fill, (char *) buf, len);
119         st->apache_buffer_fill += len;
120         st->apache_buffer[st->apache_buffer_fill] = 0;
122         return (len);
123 } /* int apache_curl_callback */
125 static size_t apache_header_callback (void *buf, size_t size, size_t nmemb,
126                 void *user_data)
128         size_t len = size * nmemb;
129         apache_t *st;
131         st = user_data;
132         if (st == NULL)
133         {
134                 ERROR ("apache plugin: apache_header_callback: "
135                                 "user_data pointer is NULL.");
136                 return (0);
137         }
139         if (len <= 0)
140                 return (len);
142         /* look for the Server header */
143         if (strncasecmp (buf, "Server: ", strlen ("Server: ")) != 0)
144                 return (len);
146         if (strstr (buf, "Apache") != NULL)
147                 st->server_type = APACHE;
148         else if (strstr (buf, "lighttpd") != NULL)
149                 st->server_type = LIGHTTPD;
150         else if (strstr (buf, "IBM_HTTP_Server") != NULL)
151                 st->server_type = APACHE;
152         else
153         {
154                 const char *hdr = buf;
156                 hdr += strlen ("Server: ");
157                 NOTICE ("apache plugin: Unknown server software: %s", hdr);
158         }
160         return (len);
161 } /* apache_header_callback */
163 /* Configuration handling functiions
164  * <Plugin apache>
165  *   <Instance "instance_name">
166  *     URL ...
167  *   </Instance>
168  *   URL ...
169  * </Plugin>
170  */
171 static int config_add (oconfig_item_t *ci)
173         apache_t *st;
174         int i;
175         int status;
177         st = malloc (sizeof (*st));
178         if (st == NULL)
179         {
180                 ERROR ("apache plugin: malloc failed.");
181                 return (-1);
182         }
183         memset (st, 0, sizeof (*st));
185         st->timeout = -1;
187         status = cf_util_get_string (ci, &st->name);
188         if (status != 0)
189         {
190                 sfree (st);
191                 return (status);
192         }
193         assert (st->name != NULL);
195         for (i = 0; i < ci->children_num; i++)
196         {
197                 oconfig_item_t *child = ci->children + i;
199                 if (strcasecmp ("URL", child->key) == 0)
200                         status = cf_util_get_string (child, &st->url);
201                 else if (strcasecmp ("Host", child->key) == 0)
202                         status = cf_util_get_string (child, &st->host);
203                 else if (strcasecmp ("User", child->key) == 0)
204                         status = cf_util_get_string (child, &st->user);
205                 else if (strcasecmp ("Password", child->key) == 0)
206                         status = cf_util_get_string (child, &st->pass);
207                 else if (strcasecmp ("VerifyPeer", child->key) == 0)
208                         status = cf_util_get_boolean (child, &st->verify_peer);
209                 else if (strcasecmp ("VerifyHost", child->key) == 0)
210                         status = cf_util_get_boolean (child, &st->verify_host);
211                 else if (strcasecmp ("CACert", child->key) == 0)
212                         status = cf_util_get_string (child, &st->cacert);
213                 else if (strcasecmp ("SSLCiphers", child->key) == 0)
214                         status = cf_util_get_string (child, &st->ssl_ciphers);
215                 else if (strcasecmp ("Server", child->key) == 0)
216                         status = cf_util_get_string (child, &st->server);
217                 else if (strcasecmp ("Timeout", child->key) == 0)
218                         status = cf_util_get_int (child, &st->timeout);
219                 else
220                 {
221                         WARNING ("apache plugin: Option `%s' not allowed here.",
222                                         child->key);
223                         status = -1;
224                 }
226                 if (status != 0)
227                         break;
228         }
230         /* Check if struct is complete.. */
231         if ((status == 0) && (st->url == NULL))
232         {
233                 ERROR ("apache plugin: Instance `%s': "
234                                 "No URL has been configured.",
235                                 st->name);
236                 status = -1;
237         }
239         if (status == 0)
240         {
241                 user_data_t ud;
242                 char callback_name[3*DATA_MAX_NAME_LEN];
244                 memset (&ud, 0, sizeof (ud));
245                 ud.data = st;
246                 ud.free_func = (void *) apache_free;
248                 memset (callback_name, 0, sizeof (callback_name));
249                 ssnprintf (callback_name, sizeof (callback_name),
250                                 "apache/%s/%s",
251                                 (st->host != NULL) ? st->host : hostname_g,
252                                 (st->name != NULL) ? st->name : "default"),
254                 status = plugin_register_complex_read (/* group = */ NULL,
255                                 /* name      = */ callback_name,
256                                 /* callback  = */ apache_read_host,
257                                 /* interval  = */ NULL,
258                                 /* user_data = */ &ud);
259         }
261         if (status != 0)
262         {
263                 apache_free (st);
264                 return (-1);
265         }
267         return (0);
268 } /* int config_add */
270 static int config (oconfig_item_t *ci)
272         int status = 0;
273         int i;
275         for (i = 0; i < ci->children_num; i++)
276         {
277                 oconfig_item_t *child = ci->children + i;
279                 if (strcasecmp ("Instance", child->key) == 0)
280                         config_add (child);
281                 else
282                         WARNING ("apache plugin: The configuration option "
283                                         "\"%s\" is not allowed here. Did you "
284                                         "forget to add an <Instance /> block "
285                                         "around the configuration?",
286                                         child->key);
287         } /* for (ci->children) */
289         return (status);
290 } /* int config */
292 /* initialize curl for each host */
293 static int init_host (apache_t *st) /* {{{ */
295         assert (st->url != NULL);
296         /* (Assured by `config_add') */
298         if (st->curl != NULL)
299         {
300                 curl_easy_cleanup (st->curl);
301                 st->curl = NULL;
302         }
304         if ((st->curl = curl_easy_init ()) == NULL)
305         {
306                 ERROR ("apache plugin: init_host: `curl_easy_init' failed.");
307                 return (-1);
308         }
310         curl_easy_setopt (st->curl, CURLOPT_NOSIGNAL, 1L);
311         curl_easy_setopt (st->curl, CURLOPT_WRITEFUNCTION, apache_curl_callback);
312         curl_easy_setopt (st->curl, CURLOPT_WRITEDATA, st);
314         /* not set as yet if the user specified string doesn't match apache or
315          * lighttpd, then ignore it. Headers will be parsed to find out the
316          * server type */
317         st->server_type = -1;
319         if (st->server != NULL)
320         {
321                 if (strcasecmp(st->server, "apache") == 0)
322                         st->server_type = APACHE;
323                 else if (strcasecmp(st->server, "lighttpd") == 0)
324                         st->server_type = LIGHTTPD;
325                 else if (strcasecmp(st->server, "ibm_http_server") == 0)
326                         st->server_type = APACHE;
327                 else
328                         WARNING ("apache plugin: Unknown `Server' setting: %s",
329                                         st->server);
330         }
332         /* if not found register a header callback to determine the server_type */
333         if (st->server_type == -1)
334         {
335                 curl_easy_setopt (st->curl, CURLOPT_HEADERFUNCTION, apache_header_callback);
336                 curl_easy_setopt (st->curl, CURLOPT_WRITEHEADER, st);
337         }
339         curl_easy_setopt (st->curl, CURLOPT_USERAGENT, COLLECTD_USERAGENT);
340         curl_easy_setopt (st->curl, CURLOPT_ERRORBUFFER, st->apache_curl_error);
342         if (st->user != NULL)
343         {
344 #ifdef HAVE_CURLOPT_USERNAME
345                 curl_easy_setopt (st->curl, CURLOPT_USERNAME, st->user);
346                 curl_easy_setopt (st->curl, CURLOPT_PASSWORD,
347                                 (st->pass == NULL) ? "" : st->pass);
348 #else
349                 static char credentials[1024];
350                 int status;
352                 status = ssnprintf (credentials, sizeof (credentials), "%s:%s",
353                                 st->user, (st->pass == NULL) ? "" : st->pass);
354                 if ((status < 0) || ((size_t) status >= sizeof (credentials)))
355                 {
356                         ERROR ("apache plugin: init_host: Returning an error "
357                                         "because the credentials have been "
358                                         "truncated.");
359                         curl_easy_cleanup (st->curl);
360                         st->curl = NULL;
361                         return (-1);
362                 }
364                 curl_easy_setopt (st->curl, CURLOPT_USERPWD, credentials);
365 #endif
366         }
368         curl_easy_setopt (st->curl, CURLOPT_URL, st->url);
369         curl_easy_setopt (st->curl, CURLOPT_FOLLOWLOCATION, 1L);
370         curl_easy_setopt (st->curl, CURLOPT_MAXREDIRS, 50L);
372         curl_easy_setopt (st->curl, CURLOPT_SSL_VERIFYPEER,
373                         (long) st->verify_peer);
374         curl_easy_setopt (st->curl, CURLOPT_SSL_VERIFYHOST,
375                         st->verify_host ? 2L : 0L);
376         if (st->cacert != NULL)
377                 curl_easy_setopt (st->curl, CURLOPT_CAINFO, st->cacert);
378         if (st->ssl_ciphers != NULL)
379                 curl_easy_setopt (st->curl, CURLOPT_SSL_CIPHER_LIST,st->ssl_ciphers);
381 #ifdef HAVE_CURLOPT_TIMEOUT_MS
382         if (st->timeout >= 0)
383                 curl_easy_setopt (st->curl, CURLOPT_TIMEOUT_MS, (long) st->timeout);
384         else
385                 curl_easy_setopt (st->curl, CURLOPT_TIMEOUT_MS,
386                                 CDTIME_T_TO_MS(plugin_get_interval()));
387 #endif
389         return (0);
390 } /* }}} int init_host */
392 static void submit_value (const char *type, const char *type_instance,
393                 value_t value, apache_t *st)
395         value_list_t vl = VALUE_LIST_INIT;
397         vl.values = &value;
398         vl.values_len = 1;
400         sstrncpy (vl.host, (st->host != NULL) ? st->host : hostname_g,
401                         sizeof (vl.host));
403         sstrncpy (vl.plugin, "apache", sizeof (vl.plugin));
404         if (st->name != NULL)
405                 sstrncpy (vl.plugin_instance, st->name,
406                                 sizeof (vl.plugin_instance));
408         sstrncpy (vl.type, type, sizeof (vl.type));
409         if (type_instance != NULL)
410                 sstrncpy (vl.type_instance, type_instance,
411                                 sizeof (vl.type_instance));
413         plugin_dispatch_values (&vl);
414 } /* void submit_value */
416 static void submit_derive (const char *type, const char *type_instance,
417                 derive_t c, apache_t *st)
419         value_t v;
420         v.derive = c;
421         submit_value (type, type_instance, v, st);
422 } /* void submit_derive */
424 static void submit_gauge (const char *type, const char *type_instance,
425                 gauge_t g, apache_t *st)
427         value_t v;
428         v.gauge = g;
429         submit_value (type, type_instance, v, st);
430 } /* void submit_gauge */
432 static void submit_scoreboard (char *buf, apache_t *st)
434         /*
435          * Scoreboard Key:
436          * "_" Waiting for Connection, "S" Starting up,
437          * "R" Reading Request for apache and read-POST for lighttpd,
438          * "W" Sending Reply, "K" Keepalive (read), "D" DNS Lookup,
439          * "C" Closing connection, "L" Logging, "G" Gracefully finishing,
440          * "I" Idle cleanup of worker, "." Open slot with no current process
441          * Lighttpd specific legends -
442          * "E" hard error, "." connect, "h" handle-request,
443          * "q" request-start, "Q" request-end, "s" response-start
444          * "S" response-end, "r" read
445          */
446         long long open      = 0LL;
447         long long waiting   = 0LL;
448         long long starting  = 0LL;
449         long long reading   = 0LL;
450         long long sending   = 0LL;
451         long long keepalive = 0LL;
452         long long dnslookup = 0LL;
453         long long closing   = 0LL;
454         long long logging   = 0LL;
455         long long finishing = 0LL;
456         long long idle_cleanup = 0LL;
458         /* lighttpd specific */
459         long long hard_error     = 0LL;
460         long long lighttpd_read  = 0LL;
461         long long handle_request = 0LL;
462         long long request_start  = 0LL;
463         long long request_end    = 0LL;
464         long long response_start = 0LL;
465         long long response_end   = 0LL;
467         int i;
468         for (i = 0; buf[i] != '\0'; i++)
469         {
470                 if (buf[i] == '.') open++;
471                 else if (buf[i] == '_') waiting++;
472                 else if (buf[i] == 'S') starting++;
473                 else if (buf[i] == 'R') reading++;
474                 else if (buf[i] == 'W') sending++;
475                 else if (buf[i] == 'K') keepalive++;
476                 else if (buf[i] == 'D') dnslookup++;
477                 else if (buf[i] == 'C') closing++;
478                 else if (buf[i] == 'L') logging++;
479                 else if (buf[i] == 'G') finishing++;
480                 else if (buf[i] == 'I') idle_cleanup++;
481                 else if (buf[i] == 'r') lighttpd_read++;
482                 else if (buf[i] == 'h') handle_request++;
483                 else if (buf[i] == 'E') hard_error++;
484                 else if (buf[i] == 'q') request_start++;
485                 else if (buf[i] == 'Q') request_end++;
486                 else if (buf[i] == 's') response_start++;
487                 else if (buf[i] == 'S') response_end++;
488         }
490         if (st->server_type == APACHE)
491         {
492                 submit_gauge ("apache_scoreboard", "open"     , open, st);
493                 submit_gauge ("apache_scoreboard", "waiting"  , waiting, st);
494                 submit_gauge ("apache_scoreboard", "starting" , starting, st);
495                 submit_gauge ("apache_scoreboard", "reading"  , reading, st);
496                 submit_gauge ("apache_scoreboard", "sending"  , sending, st);
497                 submit_gauge ("apache_scoreboard", "keepalive", keepalive, st);
498                 submit_gauge ("apache_scoreboard", "dnslookup", dnslookup, st);
499                 submit_gauge ("apache_scoreboard", "closing"  , closing, st);
500                 submit_gauge ("apache_scoreboard", "logging"  , logging, st);
501                 submit_gauge ("apache_scoreboard", "finishing", finishing, st);
502                 submit_gauge ("apache_scoreboard", "idle_cleanup", idle_cleanup, st);
503         }
504         else
505         {
506                 submit_gauge ("apache_scoreboard", "connect"       , open, st);
507                 submit_gauge ("apache_scoreboard", "close"         , closing, st);
508                 submit_gauge ("apache_scoreboard", "hard_error"    , hard_error, st);
509                 submit_gauge ("apache_scoreboard", "read"          , lighttpd_read, st);
510                 submit_gauge ("apache_scoreboard", "read_post"     , reading, st);
511                 submit_gauge ("apache_scoreboard", "write"         , sending, st);
512                 submit_gauge ("apache_scoreboard", "handle_request", handle_request, st);
513                 submit_gauge ("apache_scoreboard", "request_start" , request_start, st);
514                 submit_gauge ("apache_scoreboard", "request_end"   , request_end, st);
515                 submit_gauge ("apache_scoreboard", "response_start", response_start, st);
516                 submit_gauge ("apache_scoreboard", "response_end"  , response_end, st);
517         }
520 static int apache_read_host (user_data_t *user_data) /* {{{ */
522         int i;
524         char *ptr;
525         char *saveptr;
526         char *lines[16];
527         int   lines_num = 0;
529         char *fields[4];
530         int   fields_num;
532         apache_t *st;
534         st = user_data->data;
536         assert (st->url != NULL);
537         /* (Assured by `config_add') */
539         if (st->curl == NULL)
540         {
541                 int status;
543                 status = init_host (st);
544                 if (status != 0)
545                         return (-1);
546         }
547         assert (st->curl != NULL);
549         st->apache_buffer_fill = 0;
550         if (curl_easy_perform (st->curl) != CURLE_OK)
551         {
552                 ERROR ("apache: curl_easy_perform failed: %s",
553                                 st->apache_curl_error);
554                 return (-1);
555         }
557         /* fallback - server_type to apache if not set at this time */
558         if (st->server_type == -1)
559         {
560                 WARNING ("apache plugin: Unable to determine server software "
561                                 "automatically. Will assume Apache.");
562                 st->server_type = APACHE;
563         }
565         ptr = st->apache_buffer;
566         saveptr = NULL;
567         while ((lines[lines_num] = strtok_r (ptr, "\n\r", &saveptr)) != NULL)
568         {
569                 ptr = NULL;
570                 lines_num++;
572                 if (lines_num >= 16)
573                         break;
574         }
576         for (i = 0; i < lines_num; i++)
577         {
578                 fields_num = strsplit (lines[i], fields, 4);
580                 if (fields_num == 3)
581                 {
582                         if ((strcmp (fields[0], "Total") == 0)
583                                         && (strcmp (fields[1], "Accesses:") == 0))
584                                 submit_derive ("apache_requests", "",
585                                                 atoll (fields[2]), st);
586                         else if ((strcmp (fields[0], "Total") == 0)
587                                         && (strcmp (fields[1], "kBytes:") == 0))
588                                 submit_derive ("apache_bytes", "",
589                                                 1024LL * atoll (fields[2]), st);
590                 }
591                 else if (fields_num == 2)
592                 {
593                         if (strcmp (fields[0], "Scoreboard:") == 0)
594                                 submit_scoreboard (fields[1], st);
595                         else if ((strcmp (fields[0], "BusyServers:") == 0) /* Apache 1.* */
596                                         || (strcmp (fields[0], "BusyWorkers:") == 0) /* Apache 2.* */)
597                                 submit_gauge ("apache_connections", NULL, atol (fields[1]), st);
598                         else if ((strcmp (fields[0], "IdleServers:") == 0) /* Apache 1.x */
599                                         || (strcmp (fields[0], "IdleWorkers:") == 0) /* Apache 2.x */)
600                                 submit_gauge ("apache_idle_workers", NULL, atol (fields[1]), st);
601                 }
602         }
604         st->apache_buffer_fill = 0;
606         return (0);
607 } /* }}} int apache_read_host */
609 static int apache_init (void) /* {{{ */
611         /* Call this while collectd is still single-threaded to avoid
612          * initialization issues in libgcrypt. */
613         curl_global_init (CURL_GLOBAL_SSL);
614         return (0);
615 } /* }}} int apache_init */
617 void module_register (void)
619         plugin_register_complex_config ("apache", config);
620         plugin_register_init ("apache", apache_init);
621 } /* void module_register */
623 /* vim: set sw=8 noet fdm=marker : */