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