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