apache plugin: Added Amit to list of copyright holders.
[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 static char *url         = NULL;
35 static char *user        = NULL;
36 static char *pass        = NULL;
37 static char *verify_peer = NULL;
38 static char *verify_host = NULL;
39 static char *cacert      = NULL;
40
41 static CURL *curl = NULL;
42
43 static char  *apache_buffer = NULL;
44 static size_t apache_buffer_size = 0;
45 static size_t apache_buffer_fill = 0;
46 static char   apache_curl_error[CURL_ERROR_SIZE];
47
48 struct apache_s
49 {
50         char *name;
51         char *host;
52         char *url;
53         char *user;
54         char *pass;
55         char *verify_peer;
56         char *verify_host;
57         char *cacert;
58         CURL *curl;
59 }; /* apache_s */
60
61 typedef struct apache_s apache_t;
62
63 static apache_t **apache     = NULL;
64 static size_t     apache_num = 0;
65
66 static void apache_free (apache_t *st)
67 {
68         if (st == NULL)
69                 return;
70
71         sfree (st->name);
72         sfree (st->host);
73         sfree (st->url);
74         sfree (st->user);
75         sfree (st->pass);
76         sfree (st->verify_peer);
77         sfree (st->verify_host);
78         sfree (st->cacert);
79         if (st->curl) {
80                 curl_easy_cleanup(st->curl);
81                 st->curl = NULL;
82         }
83 } /* apache_free */
84
85 static size_t apache_curl_callback (void *buf, size_t size, size_t nmemb,
86                 void __attribute__((unused)) *stream)
87 {
88         size_t len = size * nmemb;
89
90         if (len <= 0)
91                 return (len);
92
93         if ((apache_buffer_fill + len) >= apache_buffer_size)
94         {
95                 char *temp;
96
97                 temp = (char *) realloc (apache_buffer,
98                                 apache_buffer_fill + len + 1);
99                 if (temp == NULL)
100                 {
101                         ERROR ("apache plugin: realloc failed.");
102                         return (0);
103                 }
104                 apache_buffer = temp;
105                 apache_buffer_size = apache_buffer_fill + len + 1;
106         }
107
108         memcpy (apache_buffer + apache_buffer_fill, (char *) buf, len);
109         apache_buffer_fill += len;
110         apache_buffer[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                         if (strcasecmp ("Instance", child->key) == 0)
260                         {
261                                 lci->key = child->key;
262                                 lci->values = child->values;
263                                 lci->values_num = child->values_num;
264                                 lci->parent = child->parent;
265                         }
266                         else
267                         {
268                                 lci->children_num++;
269                                 lci->children =
270                                         realloc (lci->children,
271                                                  lci->children_num * sizeof (*child));
272                                 if (lci->children == NULL)
273                                 {
274                                         ERROR ("apache plugin: realloc failed.");
275                                         return (-1);
276                                 }
277                                 memcpy (&lci->children[lci->children_num-1], child, sizeof (*child));
278                         }
279                 }
280         } /* for (ci->children) */
281
282         if (lci)
283         {
284                 // create a <Instance ""> entry
285                 lci->key = "Instance";
286                 lci->values_num = 1;
287                 lci->values = (oconfig_value_t *) malloc (lci->values_num * sizeof (oconfig_value_t));
288                 lci->values[0].type = OCONFIG_TYPE_STRING;
289                 lci->values[0].value.string = "";
290
291                 status = config_add (lci);
292                 sfree (lci->children);
293                 sfree (lci);
294         }
295
296         return status;
297 } /* int config */
298
299
300 // initialize curl for each host
301 static int init_host (apache_t *st) /* {{{ */
302 {
303         static char credentials[1024];
304
305         if (st->url == NULL)
306         {
307                 WARNING ("apache plugin: init: No URL configured, returning "
308                                 "an error.");
309                 return (-1);
310         }
311
312         if (st->curl != NULL)
313         {
314                 curl_easy_cleanup (st->curl);
315         }
316
317         if ((st->curl = curl_easy_init ()) == NULL)
318         {
319                 ERROR ("apache plugin: init: `curl_easy_init' failed.");
320                 return (-1);
321         }
322
323         curl_easy_setopt (st->curl, CURLOPT_WRITEFUNCTION, apache_curl_callback);
324         curl_easy_setopt (st->curl, CURLOPT_USERAGENT, PACKAGE_NAME"/"PACKAGE_VERSION);
325         curl_easy_setopt (st->curl, CURLOPT_ERRORBUFFER, apache_curl_error);
326
327         if (st->user != NULL)
328         {
329                 int status;
330
331                 status = ssnprintf (credentials, sizeof (credentials), "%s:%s",
332                                 st->user, (st->pass == NULL) ? "" : st->pass);
333                 if ((status < 0) || ((size_t) status >= sizeof (credentials)))
334                 {
335                         ERROR ("apache plugin: init: Returning an error "
336                                         "because the credentials have been "
337                                         "truncated.");
338                         return (-1);
339                 }
340
341                 curl_easy_setopt (st->curl, CURLOPT_USERPWD, credentials);
342         }
343
344         curl_easy_setopt (st->curl, CURLOPT_URL, st->url);
345
346         if ((st->verify_peer == NULL) || (strcmp (st->verify_peer, "true") == 0))
347         {
348                 curl_easy_setopt (st->curl, CURLOPT_SSL_VERIFYPEER, 1);
349         }
350         else
351         {
352                 curl_easy_setopt (st->curl, CURLOPT_SSL_VERIFYPEER, 0);
353         }
354
355         if ((st->verify_host == NULL) || (strcmp (st->verify_host, "true") == 0))
356         {
357                 curl_easy_setopt (st->curl, CURLOPT_SSL_VERIFYHOST, 2);
358         }
359         else
360         {
361                 curl_easy_setopt (st->curl, CURLOPT_SSL_VERIFYHOST, 0);
362         }
363
364         if (st->cacert != NULL)
365         {
366                 curl_easy_setopt (st->curl, CURLOPT_CAINFO, st->cacert);
367         }
368
369         return (0);
370 } /* int init_host */
371
372 static int init (void)
373 {
374         size_t i;
375         int success = 0;
376         int status;
377
378         for (i = 0; i < apache_num; i++)
379         {
380                 status = init_host (apache[i]);
381                 if (status == 0)
382                         success++;
383         }
384
385         if (success == 0)
386         {
387                 ERROR ("apache plugin init: No host could be initialized. Will return an error so "
388                         "the plugin will be delayed.");
389                 return (-1);
390         }
391
392         return (0);
393 } /* int init */
394
395 static void set_plugin (apache_t *st, value_list_t *vl)
396 {
397         // if there is no instance name, assume apache
398         if ( (0 == strcmp(st->name, "")) )
399         {
400                 sstrncpy (vl->plugin, "apache", sizeof (vl->plugin));
401         }
402         else
403         {
404                 sstrncpy (vl->plugin, st->name, sizeof (vl->plugin));
405         }
406 } /* void set_plugin */
407
408 static void submit_counter (const char *type, const char *type_instance,
409                 counter_t value, char *host, apache_t *st)
410 {
411         value_t values[1];
412         value_list_t vl = VALUE_LIST_INIT;
413
414         values[0].counter = value;
415
416         vl.values = values;
417         vl.values_len = 1;
418         sstrncpy (vl.host, host, sizeof (vl.host));
419         sstrncpy (vl.plugin_instance, "", sizeof (vl.plugin_instance));
420         sstrncpy (vl.type, type, sizeof (vl.type));
421
422         if (type_instance != NULL)
423                 sstrncpy (vl.type_instance, type_instance,
424                                 sizeof (vl.type_instance));
425
426         set_plugin (st, &vl);
427
428         plugin_dispatch_values (&vl);
429 } /* void submit_counter */
430
431 static void submit_gauge (const char *type, const char *type_instance,
432                 gauge_t value, char *host, apache_t *st)
433 {
434         value_t values[1];
435         value_list_t vl = VALUE_LIST_INIT;
436
437         values[0].gauge = value;
438
439         vl.values = values;
440         vl.values_len = 1;
441         sstrncpy (vl.host, host, sizeof (vl.host));
442         sstrncpy (vl.plugin_instance, "", sizeof (vl.plugin_instance));
443         sstrncpy (vl.type, type, sizeof (vl.type));
444
445         if (type_instance != NULL)
446                 sstrncpy (vl.type_instance, type_instance,
447                                 sizeof (vl.type_instance));
448
449         set_plugin (st, &vl);
450
451         plugin_dispatch_values (&vl);
452 } /* void submit_counter */
453
454 static void submit_scoreboard (char *buf, char *host, apache_t *st)
455 {
456         /*
457          * Scoreboard Key:
458          * "_" Waiting for Connection, "S" Starting up, "R" Reading Request,
459          * "W" Sending Reply, "K" Keepalive (read), "D" DNS Lookup,
460          * "C" Closing connection, "L" Logging, "G" Gracefully finishing,
461          * "I" Idle cleanup of worker, "." Open slot with no current process
462          */
463         long long open      = 0LL;
464         long long waiting   = 0LL;
465         long long starting  = 0LL;
466         long long reading   = 0LL;
467         long long sending   = 0LL;
468         long long keepalive = 0LL;
469         long long dnslookup = 0LL;
470         long long closing   = 0LL;
471         long long logging   = 0LL;
472         long long finishing = 0LL;
473         long long idle_cleanup = 0LL;
474
475         int i;
476
477         for (i = 0; buf[i] != '\0'; i++)
478         {
479                 if (buf[i] == '.') open++;
480                 else if (buf[i] == '_') waiting++;
481                 else if (buf[i] == 'S') starting++;
482                 else if (buf[i] == 'R') reading++;
483                 else if (buf[i] == 'W') sending++;
484                 else if (buf[i] == 'K') keepalive++;
485                 else if (buf[i] == 'D') dnslookup++;
486                 else if (buf[i] == 'C') closing++;
487                 else if (buf[i] == 'L') logging++;
488                 else if (buf[i] == 'G') finishing++;
489                 else if (buf[i] == 'I') idle_cleanup++;
490         }
491
492         submit_gauge ("apache_scoreboard", "open"     , open, host, st);
493         submit_gauge ("apache_scoreboard", "waiting"  , waiting, host, st);
494         submit_gauge ("apache_scoreboard", "starting" , starting, host, st);
495         submit_gauge ("apache_scoreboard", "reading"  , reading, host, st);
496         submit_gauge ("apache_scoreboard", "sending"  , sending, host, st);
497         submit_gauge ("apache_scoreboard", "keepalive", keepalive, host, st);
498         submit_gauge ("apache_scoreboard", "dnslookup", dnslookup, host, st);
499         submit_gauge ("apache_scoreboard", "closing"  , closing, host, st);
500         submit_gauge ("apache_scoreboard", "logging"  , logging, host, st);
501         submit_gauge ("apache_scoreboard", "finishing", finishing, host, st);
502         submit_gauge ("apache_scoreboard", "idle_cleanup", idle_cleanup, host, st);
503 }
504
505 static int apache_read_host (apache_t *st)
506 {
507         int i;
508
509         char *ptr;
510         char *saveptr;
511         char *lines[16];
512         int   lines_num = 0;
513
514         char *fields[4];
515         char *host;
516         int   fields_num;
517
518         if (st->curl == NULL)
519                 return (-1);
520         if (st->url == NULL)
521                 return (-1);
522
523         apache_buffer_fill = 0;
524         if (curl_easy_perform (st->curl) != 0)
525         {
526                 ERROR ("apache: curl_easy_perform failed: %s",
527                                 apache_curl_error);
528                 return (-1);
529         }
530
531         ptr = apache_buffer;
532         saveptr = NULL;
533         while ((lines[lines_num] = strtok_r (ptr, "\n\r", &saveptr)) != NULL)
534         {
535                 ptr = NULL;
536                 lines_num++;
537
538                 if (lines_num >= 16)
539                         break;
540         }
541
542         // set the host to localhost if st->host is not specified
543         if ( (st->host == NULL)
544                 || (0 == strcmp(st->host, "")) ) {
545                 st->host = hostname_g;
546         }
547
548         for (i = 0; i < lines_num; i++)
549         {
550                 fields_num = strsplit (lines[i], fields, 4);
551
552                 if (fields_num == 3)
553                 {
554                         if ((strcmp (fields[0], "Total") == 0)
555                                         && (strcmp (fields[1], "Accesses:") == 0))
556                                 submit_counter ("apache_requests", "",
557                                                 atoll (fields[2]), st->host, st);
558                         else if ((strcmp (fields[0], "Total") == 0)
559                                         && (strcmp (fields[1], "kBytes:") == 0))
560                                 submit_counter ("apache_bytes", "",
561                                                 1024LL * atoll (fields[2]), st->host, st);
562                 }
563                 else if (fields_num == 2)
564                 {
565                         if (strcmp (fields[0], "Scoreboard:") == 0)
566                                 submit_scoreboard (fields[1], st->host, st);
567                         else if (strcmp (fields[0], "BusyServers:") == 0)
568                                 submit_gauge ("apache_connections", NULL, atol (fields[1]), st->host, st);
569                 }
570         }
571
572         apache_buffer_fill = 0;
573
574         return (0);
575 } /* int apache_read_host */
576
577 static int apache_read (void)
578 {
579         size_t i;
580         int success = 0;
581         int status;
582
583         for (i = 0; i < apache_num; i++)
584         {
585                 status = apache_read_host (apache[i]);
586                 if (status == 0)
587                         success++;
588         }
589
590         if (success == 0)
591         {
592                 ERROR ("apache plugin: No host could be read. Will return an error so "
593                        "the plugin will be delayed.");
594                 return (-1);
595         }
596
597         return (0);
598  } /* int apache_read */
599
600 void module_register (void)
601 {
602         plugin_register_complex_config ("apache", config);
603         plugin_register_init ("apache", init);
604         plugin_register_read ("apache", apache_read);
605 } /* void module_register */