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