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