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