9e91b8024f948f23a5c83328c828a51652481d01
[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  **/
26
27 #include "collectd.h"
28 #include "common.h"
29 #include "plugin.h"
30 #include "configfile.h"
31
32 #include <curl/curl.h>
33
34 enum server_enum
35 {
36         APACHE = 0,
37         LIGHTTPD
38 };
39
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         int timeout;
57         CURL *curl;
58 }; /* apache_s */
59
60 typedef struct apache_s apache_t;
61
62 /* TODO: Remove this prototype */
63 static int apache_read_host (user_data_t *user_data);
64
65 static void apache_free (apache_t *st)
66 {
67         if (st == NULL)
68                 return;
69
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 */
83
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;
89
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         }
97
98         if (len <= 0)
99                 return (len);
100
101         if ((st->apache_buffer_fill + len) >= st->apache_buffer_size)
102         {
103                 char *temp;
104
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         }
115
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;
119
120         return (len);
121 } /* int apache_curl_callback */
122
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;
128
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         }
136
137         if (len <= 0)
138                 return (len);
139
140         /* look for the Server header */
141         if (strncasecmp (buf, "Server: ", strlen ("Server: ")) != 0)
142                 return (len);
143
144         if (strstr (buf, "Apache") != NULL)
145                 st->server_type = APACHE;
146         else if (strstr (buf, "lighttpd") != NULL)
147                 st->server_type = LIGHTTPD;
148         else if (strstr (buf, "IBM_HTTP_Server") != NULL)
149                 st->server_type = APACHE;
150         else
151         {
152                 const char *hdr = buf;
153
154                 hdr += strlen ("Server: ");
155                 NOTICE ("apache plugin: Unknown server software: %s", hdr);
156         }
157
158         return (len);
159 } /* apache_header_callback */
160
161 /* Configuration handling functiions
162  * <Plugin apache>
163  *   <Instance "instance_name">
164  *     URL ...
165  *   </Instance>
166  *   URL ...
167  * </Plugin>
168  */
169 static int config_add (oconfig_item_t *ci)
170 {
171         apache_t *st;
172         int i;
173         int status;
174
175         st = malloc (sizeof (*st));
176         if (st == NULL)
177         {
178                 ERROR ("apache plugin: malloc failed.");
179                 return (-1);
180         }
181         memset (st, 0, sizeof (*st));
182
183         st->timeout = -1;
184
185         status = cf_util_get_string (ci, &st->name);
186         if (status != 0)
187         {
188                 sfree (st);
189                 return (status);
190         }
191         assert (st->name != NULL);
192
193         for (i = 0; i < ci->children_num; i++)
194         {
195                 oconfig_item_t *child = ci->children + i;
196
197                 if (strcasecmp ("URL", child->key) == 0)
198                         status = cf_util_get_string (child, &st->url);
199                 else if (strcasecmp ("Host", child->key) == 0)
200                         status = cf_util_get_string (child, &st->host);
201                 else if (strcasecmp ("User", child->key) == 0)
202                         status = cf_util_get_string (child, &st->user);
203                 else if (strcasecmp ("Password", child->key) == 0)
204                         status = cf_util_get_string (child, &st->pass);
205                 else if (strcasecmp ("VerifyPeer", child->key) == 0)
206                         status = cf_util_get_boolean (child, &st->verify_peer);
207                 else if (strcasecmp ("VerifyHost", child->key) == 0)
208                         status = cf_util_get_boolean (child, &st->verify_host);
209                 else if (strcasecmp ("CACert", child->key) == 0)
210                         status = cf_util_get_string (child, &st->cacert);
211                 else if (strcasecmp ("Server", child->key) == 0)
212                         status = cf_util_get_string (child, &st->server);
213                 else if (strcasecmp ("Timeout", child->key) == 0)
214                         status = cf_util_get_int (child, &st->timeout);
215                 else
216                 {
217                         WARNING ("apache plugin: Option `%s' not allowed here.",
218                                         child->key);
219                         status = -1;
220                 }
221
222                 if (status != 0)
223                         break;
224         }
225
226         /* Check if struct is complete.. */
227         if ((status == 0) && (st->url == NULL))
228         {
229                 ERROR ("apache plugin: Instance `%s': "
230                                 "No URL has been configured.",
231                                 st->name);
232                 status = -1;
233         }
234
235         if (status == 0)
236         {
237                 user_data_t ud;
238                 char callback_name[3*DATA_MAX_NAME_LEN];
239
240                 memset (&ud, 0, sizeof (ud));
241                 ud.data = st;
242                 ud.free_func = (void *) apache_free;
243
244                 memset (callback_name, 0, sizeof (callback_name));
245                 ssnprintf (callback_name, sizeof (callback_name),
246                                 "apache/%s/%s",
247                                 (st->host != NULL) ? st->host : hostname_g,
248                                 (st->name != NULL) ? st->name : "default"),
249
250                 status = plugin_register_complex_read (/* group = */ NULL,
251                                 /* name      = */ callback_name,
252                                 /* callback  = */ apache_read_host,
253                                 /* interval  = */ NULL,
254                                 /* user_data = */ &ud);
255         }
256
257         if (status != 0)
258         {
259                 apache_free (st);
260                 return (-1);
261         }
262
263         return (0);
264 } /* int config_add */
265
266 static int config (oconfig_item_t *ci)
267 {
268         int status = 0;
269         int i;
270
271         for (i = 0; i < ci->children_num; i++)
272         {
273                 oconfig_item_t *child = ci->children + i;
274
275                 if (strcasecmp ("Instance", child->key) == 0)
276                         config_add (child);
277                 else
278                         WARNING ("apache plugin: The configuration option "
279                                         "\"%s\" is not allowed here. Did you "
280                                         "forget to add an <Instance /> block "
281                                         "around the configuration?",
282                                         child->key);
283         } /* for (ci->children) */
284
285         return (status);
286 } /* int config */
287
288 /* initialize curl for each host */
289 static int init_host (apache_t *st) /* {{{ */
290 {
291         assert (st->url != NULL);
292         /* (Assured by `config_add') */
293
294         if (st->curl != NULL)
295         {
296                 curl_easy_cleanup (st->curl);
297                 st->curl = NULL;
298         }
299
300         if ((st->curl = curl_easy_init ()) == NULL)
301         {
302                 ERROR ("apache plugin: init_host: `curl_easy_init' failed.");
303                 return (-1);
304         }
305
306         curl_easy_setopt (st->curl, CURLOPT_NOSIGNAL, 1L);
307         curl_easy_setopt (st->curl, CURLOPT_WRITEFUNCTION, apache_curl_callback);
308         curl_easy_setopt (st->curl, CURLOPT_WRITEDATA, st);
309
310         /* not set as yet if the user specified string doesn't match apache or
311          * lighttpd, then ignore it. Headers will be parsed to find out the
312          * server type */
313         st->server_type = -1;
314
315         if (st->server != NULL)
316         {
317                 if (strcasecmp(st->server, "apache") == 0)
318                         st->server_type = APACHE;
319                 else if (strcasecmp(st->server, "lighttpd") == 0)
320                         st->server_type = LIGHTTPD;
321                 else if (strcasecmp(st->server, "ibm_http_server") == 0)
322                         st->server_type = APACHE;
323                 else
324                         WARNING ("apache plugin: Unknown `Server' setting: %s",
325                                         st->server);
326         }
327
328         /* if not found register a header callback to determine the server_type */
329         if (st->server_type == -1)
330         {
331                 curl_easy_setopt (st->curl, CURLOPT_HEADERFUNCTION, apache_header_callback);
332                 curl_easy_setopt (st->curl, CURLOPT_WRITEHEADER, st);
333         }
334
335         curl_easy_setopt (st->curl, CURLOPT_USERAGENT, COLLECTD_USERAGENT);
336         curl_easy_setopt (st->curl, CURLOPT_ERRORBUFFER, st->apache_curl_error);
337
338         if (st->user != NULL)
339         {
340 #ifdef HAVE_CURLOPT_USERNAME
341                 curl_easy_setopt (st->curl, CURLOPT_USERNAME, st->user);
342                 curl_easy_setopt (st->curl, CURLOPT_PASSWORD,
343                                 (st->pass == NULL) ? "" : st->pass);
344 #else
345                 static char credentials[1024];
346                 int status;
347
348                 status = ssnprintf (credentials, sizeof (credentials), "%s:%s",
349                                 st->user, (st->pass == NULL) ? "" : st->pass);
350                 if ((status < 0) || ((size_t) status >= sizeof (credentials)))
351                 {
352                         ERROR ("apache plugin: init_host: Returning an error "
353                                         "because the credentials have been "
354                                         "truncated.");
355                         curl_easy_cleanup (st->curl);
356                         st->curl = NULL;
357                         return (-1);
358                 }
359
360                 curl_easy_setopt (st->curl, CURLOPT_USERPWD, credentials);
361 #endif
362         }
363
364         curl_easy_setopt (st->curl, CURLOPT_URL, st->url);
365         curl_easy_setopt (st->curl, CURLOPT_FOLLOWLOCATION, 1L);
366         curl_easy_setopt (st->curl, CURLOPT_MAXREDIRS, 50L);
367
368         curl_easy_setopt (st->curl, CURLOPT_SSL_VERIFYPEER,
369                         (long) st->verify_peer);
370         curl_easy_setopt (st->curl, CURLOPT_SSL_VERIFYHOST,
371                         st->verify_host ? 2L : 0L);
372         if (st->cacert != NULL)
373                 curl_easy_setopt (st->curl, CURLOPT_CAINFO, st->cacert);
374
375         if (st->timeout >= 0)
376                 curl_easy_setopt (st->curl, CURLOPT_TIMEOUT_MS, st->timeout);
377         else
378                 curl_easy_setopt (st->curl, CURLOPT_TIMEOUT_MS,
379                                 CDTIME_T_TO_MS(plugin_get_interval()));
380
381         return (0);
382 } /* }}} int init_host */
383
384 static void submit_value (const char *type, const char *type_instance,
385                 value_t value, apache_t *st)
386 {
387         value_list_t vl = VALUE_LIST_INIT;
388
389         vl.values = &value;
390         vl.values_len = 1;
391
392         sstrncpy (vl.host, (st->host != NULL) ? st->host : hostname_g,
393                         sizeof (vl.host));
394
395         sstrncpy (vl.plugin, "apache", sizeof (vl.plugin));
396         if (st->name != NULL)
397                 sstrncpy (vl.plugin_instance, st->name,
398                                 sizeof (vl.plugin_instance));
399
400         sstrncpy (vl.type, type, sizeof (vl.type));
401         if (type_instance != NULL)
402                 sstrncpy (vl.type_instance, type_instance,
403                                 sizeof (vl.type_instance));
404
405         plugin_dispatch_values (&vl);
406 } /* void submit_value */
407
408 static void submit_derive (const char *type, const char *type_instance,
409                 derive_t c, apache_t *st)
410 {
411         value_t v;
412         v.derive = c;
413         submit_value (type, type_instance, v, st);
414 } /* void submit_derive */
415
416 static void submit_gauge (const char *type, const char *type_instance,
417                 gauge_t g, apache_t *st)
418 {
419         value_t v;
420         v.gauge = g;
421         submit_value (type, type_instance, v, st);
422 } /* void submit_gauge */
423
424 static void submit_scoreboard (char *buf, apache_t *st)
425 {
426         /*
427          * Scoreboard Key:
428          * "_" Waiting for Connection, "S" Starting up,
429          * "R" Reading Request for apache and read-POST for lighttpd,
430          * "W" Sending Reply, "K" Keepalive (read), "D" DNS Lookup,
431          * "C" Closing connection, "L" Logging, "G" Gracefully finishing,
432          * "I" Idle cleanup of worker, "." Open slot with no current process
433          * Lighttpd specific legends -
434          * "E" hard error, "." connect, "h" handle-request,
435          * "q" request-start, "Q" request-end, "s" response-start
436          * "S" response-end, "r" read
437          */
438         long long open      = 0LL;
439         long long waiting   = 0LL;
440         long long starting  = 0LL;
441         long long reading   = 0LL;
442         long long sending   = 0LL;
443         long long keepalive = 0LL;
444         long long dnslookup = 0LL;
445         long long closing   = 0LL;
446         long long logging   = 0LL;
447         long long finishing = 0LL;
448         long long idle_cleanup = 0LL;
449
450         /* lighttpd specific */
451         long long hard_error     = 0LL;
452         long long lighttpd_read  = 0LL;
453         long long handle_request = 0LL;
454         long long request_start  = 0LL;
455         long long request_end    = 0LL;
456         long long response_start = 0LL;
457         long long response_end   = 0LL;
458
459         int i;
460         for (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         }
481
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 }
511
512 static int apache_read_host (user_data_t *user_data) /* {{{ */
513 {
514         int i;
515
516         char *ptr;
517         char *saveptr;
518         char *lines[16];
519         int   lines_num = 0;
520
521         char *fields[4];
522         int   fields_num;
523
524         apache_t *st;
525
526         st = user_data->data;
527
528         assert (st->url != NULL);
529         /* (Assured by `config_add') */
530
531         if (st->curl == NULL)
532         {
533                 int status;
534
535                 status = init_host (st);
536                 if (status != 0)
537                         return (-1);
538         }
539         assert (st->curl != NULL);
540
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         }
548
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         }
556
557         ptr = st->apache_buffer;
558         saveptr = NULL;
559         while ((lines[lines_num] = strtok_r (ptr, "\n\r", &saveptr)) != NULL)
560         {
561                 ptr = NULL;
562                 lines_num++;
563
564                 if (lines_num >= 16)
565                         break;
566         }
567
568         for (i = 0; i < lines_num; i++)
569         {
570                 fields_num = strsplit (lines[i], fields, 4);
571
572                 if (fields_num == 3)
573                 {
574                         if ((strcmp (fields[0], "Total") == 0)
575                                         && (strcmp (fields[1], "Accesses:") == 0))
576                                 submit_derive ("apache_requests", "",
577                                                 atoll (fields[2]), st);
578                         else if ((strcmp (fields[0], "Total") == 0)
579                                         && (strcmp (fields[1], "kBytes:") == 0))
580                                 submit_derive ("apache_bytes", "",
581                                                 1024LL * atoll (fields[2]), st);
582                 }
583                 else if (fields_num == 2)
584                 {
585                         if (strcmp (fields[0], "Scoreboard:") == 0)
586                                 submit_scoreboard (fields[1], st);
587                         else if ((strcmp (fields[0], "BusyServers:") == 0) /* Apache 1.* */
588                                         || (strcmp (fields[0], "BusyWorkers:") == 0) /* Apache 2.* */)
589                                 submit_gauge ("apache_connections", NULL, atol (fields[1]), st);
590                         else if ((strcmp (fields[0], "IdleServers:") == 0) /* Apache 1.x */
591                                         || (strcmp (fields[0], "IdleWorkers:") == 0) /* Apache 2.x */)
592                                 submit_gauge ("apache_idle_workers", NULL, atol (fields[1]), st);
593                 }
594         }
595
596         st->apache_buffer_fill = 0;
597
598         return (0);
599 } /* }}} int apache_read_host */
600
601 static int apache_init (void) /* {{{ */
602 {
603         /* Call this while collectd is still single-threaded to avoid
604          * initialization issues in libgcrypt. */
605         curl_global_init (CURL_GLOBAL_SSL);
606         return (0);
607 } /* }}} int apache_init */
608
609 void module_register (void)
610 {
611         plugin_register_complex_config ("apache", config);
612         plugin_register_init ("apache", apache_init);
613 } /* void module_register */
614
615 /* vim: set sw=8 noet fdm=marker : */