Merge branch 'collectd-3.11' into collectd-4.0
[collectd.git] / src / apache.c
1 /**
2  * collectd - src/apache.c
3  * Copyright (C) 2006  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 #if HAVE_LIBCURL && HAVE_CURL_CURL_H
31 #  define APACHE_HAVE_READ 1
32 #  include <curl/curl.h>
33 #else
34 #  define APACHE_HAVE_READ 0
35 #endif
36
37 #if APACHE_HAVE_READ
38 static char *url    = NULL;
39 static char *user   = NULL;
40 static char *pass   = NULL;
41 static char *cacert = NULL;
42
43 static CURL *curl = NULL;
44
45 #define ABUFFER_SIZE 16384
46 static char apache_buffer[ABUFFER_SIZE];
47 static int  apache_buffer_len = 0;
48 static char apache_curl_error[CURL_ERROR_SIZE];
49
50 static const char *config_keys[] =
51 {
52         "URL",
53         "User",
54         "Password",
55         "CACert",
56         NULL
57 };
58 static int config_keys_num = 4;
59
60 static size_t apache_curl_callback (void *buf, size_t size, size_t nmemb, void *stream)
61 {
62         size_t len = size * nmemb;
63
64         if ((apache_buffer_len + len) >= ABUFFER_SIZE)
65         {
66                 len = (ABUFFER_SIZE - 1) - apache_buffer_len;
67         }
68
69         if (len <= 0)
70                 return (len);
71
72         memcpy (apache_buffer + apache_buffer_len, (char *) buf, len);
73         apache_buffer_len += len;
74         apache_buffer[apache_buffer_len] = '\0';
75
76         return (len);
77 }
78
79 static int config_set (char **var, const char *value)
80 {
81         if (*var != NULL)
82         {
83                 free (*var);
84                 *var = NULL;
85         }
86
87         if ((*var = strdup (value)) == NULL)
88                 return (1);
89         else
90                 return (0);
91 }
92
93 static int config (const char *key, const char *value)
94 {
95         if (strcasecmp (key, "url") == 0)
96                 return (config_set (&url, value));
97         else if (strcasecmp (key, "user") == 0)
98                 return (config_set (&user, value));
99         else if (strcasecmp (key, "password") == 0)
100                 return (config_set (&pass, value));
101         else if (strcasecmp (key, "cacert") == 0)
102                 return (config_set (&cacert, value));
103         else
104                 return (-1);
105 }
106
107 static int init (void)
108 {
109         static char credentials[1024];
110
111         if (curl != NULL)
112         {
113                 curl_easy_cleanup (curl);
114         }
115
116         if ((curl = curl_easy_init ()) == NULL)
117         {
118                 ERROR ("apache: `curl_easy_init' failed.");
119                 return (-1);
120         }
121
122         curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, apache_curl_callback);
123         curl_easy_setopt (curl, CURLOPT_USERAGENT, PACKAGE_NAME"/"PACKAGE_VERSION);
124         curl_easy_setopt (curl, CURLOPT_ERRORBUFFER, apache_curl_error);
125
126         if (user != NULL)
127         {
128                 if (snprintf (credentials, 1024, "%s:%s", user, pass == NULL ? "" : pass) >= 1024)
129                 {
130                         ERROR ("apache: Credentials would have been truncated.");
131                         return (-1);
132                 }
133
134                 curl_easy_setopt (curl, CURLOPT_USERPWD, credentials);
135         }
136
137         if (url != NULL)
138         {
139                 curl_easy_setopt (curl, CURLOPT_URL, url);
140         }
141
142         if (cacert != NULL)
143         {
144                 curl_easy_setopt (curl, CURLOPT_CAINFO, cacert);
145         }
146
147         return (0);
148 } /* int init */
149
150 static void submit_counter (const char *type, const char *type_instance,
151                 unsigned long long value)
152 {
153         value_t values[1];
154         value_list_t vl = VALUE_LIST_INIT;
155
156         DEBUG ("type = %s; type_instance = %s; value = %llu;",
157                         type, type_instance, value);
158
159         values[0].counter = value;
160
161         vl.values = values;
162         vl.values_len = 1;
163         vl.time = time (NULL);
164         strcpy (vl.host, hostname_g);
165         strcpy (vl.plugin, "apache");
166         strcpy (vl.plugin_instance, "");
167         strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
168
169         plugin_dispatch_values (type, &vl);
170 } /* void submit_counter */
171
172 static void submit_gauge (const char *type, const char *type_instance,
173                 double value)
174 {
175         value_t values[1];
176         value_list_t vl = VALUE_LIST_INIT;
177
178         DEBUG ("type = %s; type_instance = %s; value = %lf;",
179                         type, type_instance, value);
180
181         values[0].gauge = value;
182
183         vl.values = values;
184         vl.values_len = 1;
185         vl.time = time (NULL);
186         strcpy (vl.host, hostname_g);
187         strcpy (vl.plugin, "apache");
188         strcpy (vl.plugin_instance, "");
189
190         if (type_instance != NULL)
191                 strncpy (vl.type_instance, type_instance,
192                                 sizeof (vl.type_instance));
193
194         plugin_dispatch_values (type, &vl);
195 } /* void submit_counter */
196
197 static void submit_scoreboard (char *buf)
198 {
199         /*
200          * Scoreboard Key:
201          * "_" Waiting for Connection, "S" Starting up, "R" Reading Request,
202          * "W" Sending Reply, "K" Keepalive (read), "D" DNS Lookup,
203          * "C" Closing connection, "L" Logging, "G" Gracefully finishing,
204          * "I" Idle cleanup of worker, "." Open slot with no current process
205          */
206         long long open      = 0LL;
207         long long waiting   = 0LL;
208         long long starting  = 0LL;
209         long long reading   = 0LL;
210         long long sending   = 0LL;
211         long long keepalive = 0LL;
212         long long dnslookup = 0LL;
213         long long closing   = 0LL;
214         long long logging   = 0LL;
215         long long finishing = 0LL;
216         long long idle_cleanup = 0LL;
217
218         int i;
219
220         for (i = 0; buf[i] != '\0'; i++)
221         {
222                 if (buf[i] == '.') open++;
223                 else if (buf[i] == '_') waiting++;
224                 else if (buf[i] == 'S') starting++;
225                 else if (buf[i] == 'R') reading++;
226                 else if (buf[i] == 'W') sending++;
227                 else if (buf[i] == 'K') keepalive++;
228                 else if (buf[i] == 'D') dnslookup++;
229                 else if (buf[i] == 'C') closing++;
230                 else if (buf[i] == 'L') logging++;
231                 else if (buf[i] == 'G') finishing++;
232                 else if (buf[i] == 'I') idle_cleanup++;
233         }
234
235         submit_gauge ("apache_scoreboard", "open"     , open);
236         submit_gauge ("apache_scoreboard", "waiting"  , waiting);
237         submit_gauge ("apache_scoreboard", "starting" , starting);
238         submit_gauge ("apache_scoreboard", "reading"  , reading);
239         submit_gauge ("apache_scoreboard", "sending"  , sending);
240         submit_gauge ("apache_scoreboard", "keepalive", keepalive);
241         submit_gauge ("apache_scoreboard", "dnslookup", dnslookup);
242         submit_gauge ("apache_scoreboard", "closing"  , closing);
243         submit_gauge ("apache_scoreboard", "logging"  , logging);
244         submit_gauge ("apache_scoreboard", "finishing", finishing);
245         submit_gauge ("apache_scoreboard", "idle_cleanup", idle_cleanup);
246 }
247
248 static int apache_read (void)
249 {
250         int i;
251
252         char *ptr;
253         char *saveptr;
254         char *lines[16];
255         int   lines_num = 0;
256
257         char *fields[4];
258         int   fields_num;
259
260         if (curl == NULL)
261                 return (-1);
262         if (url == NULL)
263                 return (-1);
264
265         apache_buffer_len = 0;
266         if (curl_easy_perform (curl) != 0)
267         {
268                 ERROR ("apache: curl_easy_perform failed: %s",
269                                 apache_curl_error);
270                 return (-1);
271         }
272
273         ptr = apache_buffer;
274         saveptr = NULL;
275         while ((lines[lines_num] = strtok_r (ptr, "\n\r", &saveptr)) != NULL)
276         {
277                 ptr = NULL;
278                 lines_num++;
279
280                 if (lines_num >= 16)
281                         break;
282         }
283
284         for (i = 0; i < lines_num; i++)
285         {
286                 fields_num = strsplit (lines[i], fields, 4);
287
288                 if (fields_num == 3)
289                 {
290                         if ((strcmp (fields[0], "Total") == 0)
291                                         && (strcmp (fields[1], "Accesses:") == 0))
292                                 submit_counter ("apache_requests", "",
293                                                 atoll (fields[2]));
294                         else if ((strcmp (fields[0], "Total") == 0)
295                                         && (strcmp (fields[1], "kBytes:") == 0))
296                                 submit_counter ("apache_bytes", "",
297                                                 1024LL * atoll (fields[2]));
298                 }
299                 else if (fields_num == 2)
300                 {
301                         if (strcmp (fields[0], "Scoreboard:") == 0)
302                                 submit_scoreboard (fields[1]);
303                         else if (strcmp (fields[0], "BusyServers:") == 0)
304                                 submit_gauge ("apache_connections", NULL, atol (fields[1]));
305                 }
306         }
307
308         apache_buffer_len = 0;
309
310         return (0);
311 } /* int apache_read */
312 #endif /* APACHE_HAVE_READ */
313
314 void module_register (void)
315 {
316 #if APACHE_HAVE_READ
317         plugin_register_config ("apache", config,
318                         config_keys, config_keys_num);
319         plugin_register_init ("apache", init);
320         plugin_register_read ("apache", apache_read);
321 #endif
322 } /* void module_register */