apache plugin: Add support for multiple servers.
[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  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License as published by the
8  * Free Software Foundation; only version 2 of the License is applicable.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
18  *
19  * Authors:
20  *   Florian octo Forster <octo at verplant.org>
21  *   Florent EppO Monbillard <eppo at darox.net>
22  *   - connections/lighttpd extension
23  **/
24
25 #include "collectd.h"
26 #include "common.h"
27 #include "plugin.h"
28 #include "configfile.h"
29
30 #include <curl/curl.h>
31
32 static char *url         = NULL;
33 static char *user        = NULL;
34 static char *pass        = NULL;
35 static char *verify_peer = NULL;
36 static char *verify_host = NULL;
37 static char *cacert      = NULL;
38
39 static CURL *curl = NULL;
40
41 static char  *apache_buffer = NULL;
42 static size_t apache_buffer_size = 0;
43 static size_t apache_buffer_fill = 0;
44 static char   apache_curl_error[CURL_ERROR_SIZE];
45
46 struct apache_s
47 {
48         char *name;
49         char *host;
50         char *url;
51         char *user;
52         char *pass;
53         char *verify_peer;
54         char *verify_host;
55         char *cacert;
56         CURL *curl;
57 }; /* apache_s */
58
59 typedef struct apache_s apache_t;
60
61 static apache_t **apache     = NULL;
62 static size_t     apache_num = 0;
63
64 static void apache_free (apache_t *st)
65 {
66         if (st == NULL)
67                 return;
68
69         sfree (st->name);
70         sfree (st->host);
71         sfree (st->url);
72         sfree (st->user);
73         sfree (st->pass);
74         sfree (st->verify_peer);
75         sfree (st->verify_host);
76         sfree (st->cacert);
77         if (st->curl) {
78                 curl_easy_cleanup(st->curl);
79                 st->curl = NULL;
80         }
81 } /* apache_free */
82
83 static size_t apache_curl_callback (void *buf, size_t size, size_t nmemb,
84                 void __attribute__((unused)) *stream)
85 {
86         size_t len = size * nmemb;
87
88         if (len <= 0)
89                 return (len);
90
91         if ((apache_buffer_fill + len) >= apache_buffer_size)
92         {
93                 char *temp;
94
95                 temp = (char *) realloc (apache_buffer,
96                                 apache_buffer_fill + len + 1);
97                 if (temp == NULL)
98                 {
99                         ERROR ("apache plugin: realloc failed.");
100                         return (0);
101                 }
102                 apache_buffer = temp;
103                 apache_buffer_size = apache_buffer_fill + len + 1;
104         }
105
106         memcpy (apache_buffer + apache_buffer_fill, (char *) buf, len);
107         apache_buffer_fill += len;
108         apache_buffer[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_add (oconfig_item_t *ci)
149 {
150         apache_t *st;
151         int i;
152         int status;
153
154         if ((ci->values_num != 1)
155                 || (ci->values[0].type != OCONFIG_TYPE_STRING))
156         {
157                 WARNING ("apache plugin: The `%s' config option "
158                         "needs exactly one string argument.", ci->key);
159                 return (-1);
160         }
161
162         st = (apache_t *) malloc (sizeof (*st));
163         if (st == NULL)
164         {
165                 ERROR ("apache plugin: malloc failed.");
166                 return (-1);
167         }
168
169         memset (st, 0, sizeof (*st));
170
171         status = config_set_string (&st->name, ci);
172         if (status != 0)
173         {
174         sfree (st);
175         return (status);
176         }
177
178         for (i = 0; i < ci->children_num; i++)
179         {
180                 oconfig_item_t *child = ci->children + i;
181
182                 if (strcasecmp ("URL", child->key) == 0)
183                         status = config_set_string (&st->url, child);
184                 else if (strcasecmp ("Host", child->key) == 0)
185                         status = config_set_string (&st->host, child);
186                 else if (strcasecmp ("User", child->key) == 0)
187                         status = config_set_string (&st->user, child);
188                 else if (strcasecmp ("Password", child->key) == 0)
189                         status = config_set_string (&st->pass, child);
190                 else if (strcasecmp ("VerifyPeer", child->key) == 0)
191                         status = config_set_string (&st->verify_peer, child);
192                 else if (strcasecmp ("VerifyHost", child->key) == 0)
193                         status = config_set_string (&st->verify_host, child);
194                 else if (strcasecmp ("CACert", child->key) == 0)
195                         status = config_set_string (&st->cacert, child);
196                 else
197                 {
198                         WARNING ("apache plugin: Option `%s' not allowed here.", child->key);
199                         status = -1;
200                 }
201
202                 if (status != 0)
203                         break;
204         }
205
206         if (status == 0)
207         {
208                 apache_t **temp;
209                 temp = (apache_t **) realloc (apache, sizeof (*apache) * (apache_num + 1));
210                 if (temp == NULL)
211                 {
212                         ERROR ("apache plugin: realloc failed");
213                         status = -1;
214                 }
215                 else
216                 {
217                         apache = temp;
218                         apache[apache_num] = st;
219                         apache_num++;
220                 }
221         }
222
223         if (status != 0)
224         {
225                 apache_free(st);
226                 return (-1);
227         }
228
229         return (0);
230 } /* int config_add */
231
232 static int config (oconfig_item_t *ci)
233 {
234         int status = 0;
235         int i;
236         oconfig_item_t *lci = NULL; /* legacy config */
237
238         for (i = 0; i < ci->children_num; i++)
239         {
240                 oconfig_item_t *child = ci->children + i;
241
242                 if (strcasecmp ("Instance", child->key) == 0 && child->children_num > 0)
243                         config_add (child);
244                 else
245                 {
246                         /* legacy mode - convert to <Instance ...> config */
247                         if (lci == NULL)
248                         {
249                                 lci = malloc (sizeof(*lci));
250                                 if (lci == NULL)
251                                 {
252                                         ERROR ("apache plugin: malloc failed.");
253                                         return (-1);
254                                 }
255                                 memset (lci, '\0', sizeof (*lci));
256                         }
257                         if (strcasecmp ("Instance", child->key) == 0)
258                         {
259                                 lci->key = child->key;
260                                 lci->values = child->values;
261                                 lci->values_num = child->values_num;
262                                 lci->parent = child->parent;
263                         }
264                         else
265                         {
266                                 lci->children_num++;
267                                 lci->children =
268                                         realloc (lci->children,
269                                                  lci->children_num * sizeof (*child));
270                                 if (lci->children == NULL)
271                                 {
272                                         ERROR ("apache plugin: realloc failed.");
273                                         return (-1);
274                                 }
275                                 memcpy (&lci->children[lci->children_num-1], child, sizeof (*child));
276                         }
277                 }
278         } /* for (ci->children) */
279
280         if (lci)
281         {
282                 // create a <Instance ""> entry
283                 lci->key = "Instance";
284                 lci->values_num = 1;
285                 lci->values = (oconfig_value_t *) malloc (lci->values_num * sizeof (oconfig_value_t));
286                 lci->values[0].type = OCONFIG_TYPE_STRING;
287                 lci->values[0].value.string = "";
288
289                 status = config_add (lci);
290                 sfree (lci->children);
291                 sfree (lci);
292         }
293
294         return status;
295 } /* int config */
296
297
298 // initialize curl for each host
299 static int init_host (apache_t *st) /* {{{ */
300 {
301         static char credentials[1024];
302
303         if (st->url == NULL)
304         {
305                 WARNING ("apache plugin: init: No URL configured, returning "
306                                 "an error.");
307                 return (-1);
308         }
309
310         if (st->curl != NULL)
311         {
312                 curl_easy_cleanup (st->curl);
313         }
314
315         if ((st->curl = curl_easy_init ()) == NULL)
316         {
317                 ERROR ("apache plugin: init: `curl_easy_init' failed.");
318                 return (-1);
319         }
320
321         curl_easy_setopt (st->curl, CURLOPT_WRITEFUNCTION, apache_curl_callback);
322         curl_easy_setopt (st->curl, CURLOPT_USERAGENT, PACKAGE_NAME"/"PACKAGE_VERSION);
323         curl_easy_setopt (st->curl, CURLOPT_ERRORBUFFER, apache_curl_error);
324
325         if (st->user != NULL)
326         {
327                 int status;
328
329                 status = ssnprintf (credentials, sizeof (credentials), "%s:%s",
330                                 st->user, (st->pass == NULL) ? "" : st->pass);
331                 if ((status < 0) || ((size_t) status >= sizeof (credentials)))
332                 {
333                         ERROR ("apache plugin: init: Returning an error "
334                                         "because the credentials have been "
335                                         "truncated.");
336                         return (-1);
337                 }
338
339                 curl_easy_setopt (st->curl, CURLOPT_USERPWD, credentials);
340         }
341
342         curl_easy_setopt (st->curl, CURLOPT_URL, st->url);
343
344         if ((st->verify_peer == NULL) || (strcmp (st->verify_peer, "true") == 0))
345         {
346                 curl_easy_setopt (st->curl, CURLOPT_SSL_VERIFYPEER, 1);
347         }
348         else
349         {
350                 curl_easy_setopt (st->curl, CURLOPT_SSL_VERIFYPEER, 0);
351         }
352
353         if ((st->verify_host == NULL) || (strcmp (st->verify_host, "true") == 0))
354         {
355                 curl_easy_setopt (st->curl, CURLOPT_SSL_VERIFYHOST, 2);
356         }
357         else
358         {
359                 curl_easy_setopt (st->curl, CURLOPT_SSL_VERIFYHOST, 0);
360         }
361
362         if (st->cacert != NULL)
363         {
364                 curl_easy_setopt (st->curl, CURLOPT_CAINFO, st->cacert);
365         }
366
367         return (0);
368 } /* int init_host */
369
370 static int init (void)
371 {
372         size_t i;
373         int success = 0;
374         int status;
375
376         for (i = 0; i < apache_num; i++)
377         {
378                 status = init_host (apache[i]);
379                 if (status == 0)
380                         success++;
381         }
382
383         if (success == 0)
384         {
385                 ERROR ("apache plugin init: No host could be initialized. Will return an error so "
386                         "the plugin will be delayed.");
387                 return (-1);
388         }
389
390         return (0);
391 } /* int init */
392
393 static void set_plugin (apache_t *st, value_list_t *vl)
394 {
395         // if there is no instance name, assume apache
396         if ( (0 == strcmp(st->name, "")) )
397         {
398                 sstrncpy (vl->plugin, "apache", sizeof (vl->plugin));
399         }
400         else
401         {
402                 sstrncpy (vl->plugin, st->name, sizeof (vl->plugin));
403         }
404 } /* void set_plugin */
405
406 static void submit_counter (const char *type, const char *type_instance,
407                 counter_t value, char *host, apache_t *st)
408 {
409         value_t values[1];
410         value_list_t vl = VALUE_LIST_INIT;
411
412         values[0].counter = value;
413
414         vl.values = values;
415         vl.values_len = 1;
416         sstrncpy (vl.host, host, sizeof (vl.host));
417         sstrncpy (vl.plugin_instance, "", sizeof (vl.plugin_instance));
418         sstrncpy (vl.type, type, sizeof (vl.type));
419
420         if (type_instance != NULL)
421                 sstrncpy (vl.type_instance, type_instance,
422                                 sizeof (vl.type_instance));
423
424         set_plugin (st, &vl);
425
426         plugin_dispatch_values (&vl);
427 } /* void submit_counter */
428
429 static void submit_gauge (const char *type, const char *type_instance,
430                 gauge_t value, char *host, apache_t *st)
431 {
432         value_t values[1];
433         value_list_t vl = VALUE_LIST_INIT;
434
435         values[0].gauge = value;
436
437         vl.values = values;
438         vl.values_len = 1;
439         sstrncpy (vl.host, host, sizeof (vl.host));
440         sstrncpy (vl.plugin_instance, "", sizeof (vl.plugin_instance));
441         sstrncpy (vl.type, type, sizeof (vl.type));
442
443         if (type_instance != NULL)
444                 sstrncpy (vl.type_instance, type_instance,
445                                 sizeof (vl.type_instance));
446
447         set_plugin (st, &vl);
448
449         plugin_dispatch_values (&vl);
450 } /* void submit_counter */
451
452 static void submit_scoreboard (char *buf, char *host, apache_t *st)
453 {
454         /*
455          * Scoreboard Key:
456          * "_" Waiting for Connection, "S" Starting up, "R" Reading Request,
457          * "W" Sending Reply, "K" Keepalive (read), "D" DNS Lookup,
458          * "C" Closing connection, "L" Logging, "G" Gracefully finishing,
459          * "I" Idle cleanup of worker, "." Open slot with no current process
460          */
461         long long open      = 0LL;
462         long long waiting   = 0LL;
463         long long starting  = 0LL;
464         long long reading   = 0LL;
465         long long sending   = 0LL;
466         long long keepalive = 0LL;
467         long long dnslookup = 0LL;
468         long long closing   = 0LL;
469         long long logging   = 0LL;
470         long long finishing = 0LL;
471         long long idle_cleanup = 0LL;
472
473         int i;
474
475         for (i = 0; buf[i] != '\0'; i++)
476         {
477                 if (buf[i] == '.') open++;
478                 else if (buf[i] == '_') waiting++;
479                 else if (buf[i] == 'S') starting++;
480                 else if (buf[i] == 'R') reading++;
481                 else if (buf[i] == 'W') sending++;
482                 else if (buf[i] == 'K') keepalive++;
483                 else if (buf[i] == 'D') dnslookup++;
484                 else if (buf[i] == 'C') closing++;
485                 else if (buf[i] == 'L') logging++;
486                 else if (buf[i] == 'G') finishing++;
487                 else if (buf[i] == 'I') idle_cleanup++;
488         }
489
490         submit_gauge ("apache_scoreboard", "open"     , open, host, st);
491         submit_gauge ("apache_scoreboard", "waiting"  , waiting, host, st);
492         submit_gauge ("apache_scoreboard", "starting" , starting, host, st);
493         submit_gauge ("apache_scoreboard", "reading"  , reading, host, st);
494         submit_gauge ("apache_scoreboard", "sending"  , sending, host, st);
495         submit_gauge ("apache_scoreboard", "keepalive", keepalive, host, st);
496         submit_gauge ("apache_scoreboard", "dnslookup", dnslookup, host, st);
497         submit_gauge ("apache_scoreboard", "closing"  , closing, host, st);
498         submit_gauge ("apache_scoreboard", "logging"  , logging, host, st);
499         submit_gauge ("apache_scoreboard", "finishing", finishing, host, st);
500         submit_gauge ("apache_scoreboard", "idle_cleanup", idle_cleanup, host, st);
501 }
502
503 static int apache_read_host (apache_t *st)
504 {
505         int i;
506
507         char *ptr;
508         char *saveptr;
509         char *lines[16];
510         int   lines_num = 0;
511
512         char *fields[4];
513         char *host;
514         int   fields_num;
515
516         if (st->curl == NULL)
517                 return (-1);
518         if (st->url == NULL)
519                 return (-1);
520
521         apache_buffer_fill = 0;
522         if (curl_easy_perform (st->curl) != 0)
523         {
524                 ERROR ("apache: curl_easy_perform failed: %s",
525                                 apache_curl_error);
526                 return (-1);
527         }
528
529         ptr = apache_buffer;
530         saveptr = NULL;
531         while ((lines[lines_num] = strtok_r (ptr, "\n\r", &saveptr)) != NULL)
532         {
533                 ptr = NULL;
534                 lines_num++;
535
536                 if (lines_num >= 16)
537                         break;
538         }
539
540         // set the host to localhost if st->host is not specified
541         if ( (st->host == NULL)
542                 || (0 == strcmp(st->host, "")) ) {
543                 st->host = hostname_g;
544         }
545
546         for (i = 0; i < lines_num; i++)
547         {
548                 fields_num = strsplit (lines[i], fields, 4);
549
550                 if (fields_num == 3)
551                 {
552                         if ((strcmp (fields[0], "Total") == 0)
553                                         && (strcmp (fields[1], "Accesses:") == 0))
554                                 submit_counter ("apache_requests", "",
555                                                 atoll (fields[2]), st->host, st);
556                         else if ((strcmp (fields[0], "Total") == 0)
557                                         && (strcmp (fields[1], "kBytes:") == 0))
558                                 submit_counter ("apache_bytes", "",
559                                                 1024LL * atoll (fields[2]), st->host, st);
560                 }
561                 else if (fields_num == 2)
562                 {
563                         if (strcmp (fields[0], "Scoreboard:") == 0)
564                                 submit_scoreboard (fields[1], st->host, st);
565                         else if (strcmp (fields[0], "BusyServers:") == 0)
566                                 submit_gauge ("apache_connections", NULL, atol (fields[1]), st->host, st);
567                 }
568         }
569
570         apache_buffer_fill = 0;
571
572         return (0);
573 } /* int apache_read_host */
574
575 static int apache_read (void)
576 {
577         size_t i;
578         int success = 0;
579         int status;
580
581         for (i = 0; i < apache_num; i++)
582         {
583                 status = apache_read_host (apache[i]);
584                 if (status == 0)
585                         success++;
586         }
587
588         if (success == 0)
589         {
590                 ERROR ("apache plugin: No host could be read. Will return an error so "
591                        "the plugin will be delayed.");
592                 return (-1);
593         }
594
595         return (0);
596  } /* int apache_read */
597
598 void module_register (void)
599 {
600         plugin_register_complex_config ("apache", config);
601         plugin_register_init ("apache", init);
602         plugin_register_read ("apache", apache_read);
603 } /* void module_register */