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