apache plugin: Use boolean members for "verify_{peer,host}".
[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         if (status == 0)
250         {
251                 user_data_t ud;
252                 char callback_name[3*DATA_MAX_NAME_LEN];
253
254                 memset (&ud, 0, sizeof (ud));
255                 ud.data = st;
256                 ud.free_func = (void *) apache_free;
257
258                 memset (callback_name, 0, sizeof (callback_name));
259                 ssnprintf (callback_name, sizeof (callback_name),
260                                 "apache/%s/%s",
261                                 (st->host != NULL) ? st->host : hostname_g,
262                                 (st->name != NULL) ? st->name : "default"),
263
264                 status = plugin_register_complex_read (callback_name,
265                                 /* callback  = */ apache_read_host,
266                                 /* interval  = */ NULL,
267                                 /* user_data = */ &ud);
268         }
269
270         if (status != 0)
271         {
272                 apache_free(st);
273                 return (-1);
274         }
275
276         return (0);
277 } /* int config_add */
278
279 static int config (oconfig_item_t *ci)
280 {
281         int status = 0;
282         int i;
283         oconfig_item_t *lci = NULL; /* legacy config */
284
285         for (i = 0; i < ci->children_num; i++)
286         {
287                 oconfig_item_t *child = ci->children + i;
288
289                 if (strcasecmp ("Instance", child->key) == 0 && child->children_num > 0)
290                         config_add (child);
291                 else
292                 {
293                         /* legacy mode - convert to <Instance ...> config */
294                         if (lci == NULL)
295                         {
296                                 lci = malloc (sizeof(*lci));
297                                 if (lci == NULL)
298                                 {
299                                         ERROR ("apache plugin: malloc failed.");
300                                         return (-1);
301                                 }
302                                 memset (lci, '\0', sizeof (*lci));
303                         }
304
305                         lci->children_num++;
306                         lci->children =
307                                 realloc (lci->children,
308                                          lci->children_num * sizeof (*child));
309                         if (lci->children == NULL)
310                         {
311                                 ERROR ("apache plugin: realloc failed.");
312                                 return (-1);
313                         }
314                         memcpy (&lci->children[lci->children_num-1], child, sizeof (*child));
315                 }
316         } /* for (ci->children) */
317
318         if (lci)
319         {
320                 /* create a <Instance ""> entry */
321                 lci->key = "Instance";
322                 lci->values_num = 1;
323                 lci->values = (oconfig_value_t *) malloc (lci->values_num * sizeof (oconfig_value_t));
324                 lci->values[0].type = OCONFIG_TYPE_STRING;
325                 lci->values[0].value.string = "";
326
327                 status = config_add (lci);
328                 sfree (lci->values);
329                 sfree (lci->children);
330                 sfree (lci);
331         }
332
333         return status;
334 } /* int config */
335
336 /* initialize curl for each host */
337 static int init_host (apache_t *st) /* {{{ */
338 {
339         static char credentials[1024];
340
341         if (st->url == NULL)
342         {
343                 WARNING ("apache plugin: init_host: No URL configured, returning "
344                                 "an error.");
345                 return (-1);
346         }
347
348         if (st->curl != NULL)
349         {
350                 curl_easy_cleanup (st->curl);
351         }
352
353         if ((st->curl = curl_easy_init ()) == NULL)
354         {
355                 ERROR ("apache plugin: init_host: `curl_easy_init' failed.");
356                 return (-1);
357         }
358
359         curl_easy_setopt (st->curl, CURLOPT_WRITEFUNCTION, apache_curl_callback);
360         curl_easy_setopt (st->curl, CURLOPT_WRITEDATA, st);
361         curl_easy_setopt (st->curl, CURLOPT_USERAGENT, PACKAGE_NAME"/"PACKAGE_VERSION);
362         curl_easy_setopt (st->curl, CURLOPT_ERRORBUFFER, st->apache_curl_error);
363
364         if (st->user != NULL)
365         {
366                 int status;
367
368                 status = ssnprintf (credentials, sizeof (credentials), "%s:%s",
369                                 st->user, (st->pass == NULL) ? "" : st->pass);
370                 if ((status < 0) || ((size_t) status >= sizeof (credentials)))
371                 {
372                         ERROR ("apache plugin: init_host: Returning an error "
373                                         "because the credentials have been "
374                                         "truncated.");
375                         curl_easy_cleanup (st->curl);
376                         st->curl = NULL;
377                         return (-1);
378                 }
379
380                 curl_easy_setopt (st->curl, CURLOPT_USERPWD, credentials);
381         }
382
383         curl_easy_setopt (st->curl, CURLOPT_URL, st->url);
384
385         if (st->verify_peer != 0)
386         {
387                 curl_easy_setopt (st->curl, CURLOPT_SSL_VERIFYPEER, 1);
388         }
389         else
390         {
391                 curl_easy_setopt (st->curl, CURLOPT_SSL_VERIFYPEER, 0);
392         }
393
394         if (st->verify_host != 0)
395         {
396                 curl_easy_setopt (st->curl, CURLOPT_SSL_VERIFYHOST, 2);
397         }
398         else
399         {
400                 curl_easy_setopt (st->curl, CURLOPT_SSL_VERIFYHOST, 0);
401         }
402
403         if (st->cacert != NULL)
404         {
405                 curl_easy_setopt (st->curl, CURLOPT_CAINFO, st->cacert);
406         }
407
408         return (0);
409 } /* }}} int init_host */
410
411 static void submit_value (const char *type, const char *type_instance,
412                 value_t value, apache_t *st)
413 {
414         value_list_t vl = VALUE_LIST_INIT;
415
416         vl.values = &value;
417         vl.values_len = 1;
418
419         sstrncpy (vl.host, (st->host != NULL) ? st->host : hostname_g,
420                         sizeof (vl.host));
421
422         sstrncpy (vl.plugin, "apache", sizeof (vl.plugin));
423         if (st->name != NULL)
424                 sstrncpy (vl.plugin_instance, st->name,
425                                 sizeof (vl.plugin_instance));
426
427         sstrncpy (vl.type, type, sizeof (vl.type));
428         if (type_instance != NULL)
429                 sstrncpy (vl.type_instance, type_instance,
430                                 sizeof (vl.type_instance));
431
432         plugin_dispatch_values (&vl);
433 } /* void submit_value */
434
435 static void submit_counter (const char *type, const char *type_instance,
436                 counter_t c, apache_t *st)
437 {
438         value_t v;
439         v.counter = c;
440         submit_value (type, type_instance, v, st);
441 } /* void submit_counter */
442
443 static void submit_gauge (const char *type, const char *type_instance,
444                 gauge_t g, apache_t *st)
445 {
446         value_t v;
447         v.gauge = g;
448         submit_value (type, type_instance, v, st);
449 } /* void submit_gauge */
450
451 static void submit_scoreboard (char *buf, apache_t *st)
452 {
453         /*
454          * Scoreboard Key:
455          * "_" Waiting for Connection, "S" Starting up, "R" Reading Request,
456          * "W" Sending Reply, "K" Keepalive (read), "D" DNS Lookup,
457          * "C" Closing connection, "L" Logging, "G" Gracefully finishing,
458          * "I" Idle cleanup of worker, "." Open slot with no current process
459          */
460         long long open      = 0LL;
461         long long waiting   = 0LL;
462         long long starting  = 0LL;
463         long long reading   = 0LL;
464         long long sending   = 0LL;
465         long long keepalive = 0LL;
466         long long dnslookup = 0LL;
467         long long closing   = 0LL;
468         long long logging   = 0LL;
469         long long finishing = 0LL;
470         long long idle_cleanup = 0LL;
471
472         int i;
473
474         for (i = 0; buf[i] != '\0'; i++)
475         {
476                 if (buf[i] == '.') open++;
477                 else if (buf[i] == '_') waiting++;
478                 else if (buf[i] == 'S') starting++;
479                 else if (buf[i] == 'R') reading++;
480                 else if (buf[i] == 'W') sending++;
481                 else if (buf[i] == 'K') keepalive++;
482                 else if (buf[i] == 'D') dnslookup++;
483                 else if (buf[i] == 'C') closing++;
484                 else if (buf[i] == 'L') logging++;
485                 else if (buf[i] == 'G') finishing++;
486                 else if (buf[i] == 'I') idle_cleanup++;
487         }
488
489         submit_gauge ("apache_scoreboard", "open"     , open, st);
490         submit_gauge ("apache_scoreboard", "waiting"  , waiting, st);
491         submit_gauge ("apache_scoreboard", "starting" , starting, st);
492         submit_gauge ("apache_scoreboard", "reading"  , reading, st);
493         submit_gauge ("apache_scoreboard", "sending"  , sending, st);
494         submit_gauge ("apache_scoreboard", "keepalive", keepalive, st);
495         submit_gauge ("apache_scoreboard", "dnslookup", dnslookup, st);
496         submit_gauge ("apache_scoreboard", "closing"  , closing, st);
497         submit_gauge ("apache_scoreboard", "logging"  , logging, st);
498         submit_gauge ("apache_scoreboard", "finishing", finishing, st);
499         submit_gauge ("apache_scoreboard", "idle_cleanup", idle_cleanup, st);
500 }
501
502 static int apache_read_host (user_data_t *user_data) /* {{{ */
503 {
504         int i;
505
506         char *ptr;
507         char *saveptr;
508         char *lines[16];
509         int   lines_num = 0;
510
511         char *fields[4];
512         int   fields_num;
513
514         apache_t *st;
515
516         st = user_data->data;
517
518         if (st->curl == NULL)
519         {
520                 int status;
521
522                 status = init_host (st);
523                 if (status != 0)
524                         return (-1);
525         }
526         assert (st->curl != NULL);
527
528         if (st->url == NULL)
529                 return (-1);
530
531         st->apache_buffer_fill = 0;
532         if (curl_easy_perform (st->curl) != 0)
533         {
534                 ERROR ("apache: curl_easy_perform failed: %s",
535                                 st->apache_curl_error);
536                 return (-1);
537         }
538
539         ptr = st->apache_buffer;
540         saveptr = NULL;
541         while ((lines[lines_num] = strtok_r (ptr, "\n\r", &saveptr)) != NULL)
542         {
543                 ptr = NULL;
544                 lines_num++;
545
546                 if (lines_num >= 16)
547                         break;
548         }
549
550         for (i = 0; i < lines_num; i++)
551         {
552                 fields_num = strsplit (lines[i], fields, 4);
553
554                 if (fields_num == 3)
555                 {
556                         if ((strcmp (fields[0], "Total") == 0)
557                                         && (strcmp (fields[1], "Accesses:") == 0))
558                                 submit_counter ("apache_requests", "",
559                                                 atoll (fields[2]), st);
560                         else if ((strcmp (fields[0], "Total") == 0)
561                                         && (strcmp (fields[1], "kBytes:") == 0))
562                                 submit_counter ("apache_bytes", "",
563                                                 1024LL * atoll (fields[2]), st);
564                 }
565                 else if (fields_num == 2)
566                 {
567                         if (strcmp (fields[0], "Scoreboard:") == 0)
568                                 submit_scoreboard (fields[1], st);
569                         else if (strcmp (fields[0], "BusyServers:") == 0)
570                                 submit_gauge ("apache_connections", NULL, atol (fields[1]), st);
571                 }
572         }
573
574         st->apache_buffer_fill = 0;
575
576         return (0);
577 } /* }}} int apache_read_host */
578
579 void module_register (void)
580 {
581         plugin_register_complex_config ("apache", config);
582 } /* void module_register */
583
584 /* vim: set sw=8 noet fdm=marker : */