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