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 **/
25 #include "collectd.h"
26 #include "common.h"
27 #include "plugin.h"
28 #include "configfile.h"
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
37 /* Limit to 2^27 bytes/s. That's what a gigabit-ethernet link can handle, in
38 * theory. */
39 static data_source_t apache_bytes_dsrc[1] =
40 {
41 {"count", DS_TYPE_COUNTER, 0, 134217728.0},
42 };
44 static data_set_t apache_bytes_ds =
45 {
46 "apache_bytes", 1, apache_bytes_dsrc
47 };
49 /* Limit to 2^20 requests/s */
50 static data_source_t apache_requests_dsrc[1] =
51 {
52 {"count", DS_TYPE_COUNTER, 0, 134217728.0},
53 };
55 static data_set_t apache_requests_ds =
56 {
57 "apache_requests", 1, apache_requests_dsrc
58 };
60 static data_source_t apache_scoreboard_dsrc[1] =
61 {
62 {"count", DS_TYPE_GAUGE, 0, 65535.0},
63 };
65 static data_set_t apache_scoreboard_ds =
66 {
67 "apache_scoreboard", 1, apache_scoreboard_dsrc
68 };
70 static data_source_t apache_connections_dsrc[1] =
71 {
72 {"count", DS_TYPE_GAUGE, 0, 65535.0},
73 };
75 static data_set_t apache_connections_ds =
76 {
77 "apache_connections", 1, apache_connections_dsrc
78 };
80 #if APACHE_HAVE_READ
81 static char *url = NULL;
82 static char *user = NULL;
83 static char *pass = NULL;
84 static char *cacert = NULL;
86 static CURL *curl = NULL;
88 #define ABUFFER_SIZE 16384
89 static char apache_buffer[ABUFFER_SIZE];
90 static int apache_buffer_len = 0;
91 static char apache_curl_error[CURL_ERROR_SIZE];
93 static const char *config_keys[] =
94 {
95 "URL",
96 "User",
97 "Password",
98 "CACert",
99 NULL
100 };
101 static int config_keys_num = 4;
103 static size_t apache_curl_callback (void *buf, size_t size, size_t nmemb, void *stream)
104 {
105 size_t len = size * nmemb;
107 if ((apache_buffer_len + len) >= ABUFFER_SIZE)
108 {
109 len = (ABUFFER_SIZE - 1) - apache_buffer_len;
110 }
112 if (len <= 0)
113 return (len);
115 memcpy (apache_buffer + apache_buffer_len, (char *) buf, len);
116 apache_buffer_len += len;
117 apache_buffer[apache_buffer_len] = '\0';
119 return (len);
120 }
122 static int config_set (char **var, const char *value)
123 {
124 if (*var != NULL)
125 {
126 free (*var);
127 *var = NULL;
128 }
130 if ((*var = strdup (value)) == NULL)
131 return (1);
132 else
133 return (0);
134 }
136 static int config (const char *key, const char *value)
137 {
138 if (strcasecmp (key, "url") == 0)
139 return (config_set (&url, value));
140 else if (strcasecmp (key, "user") == 0)
141 return (config_set (&user, value));
142 else if (strcasecmp (key, "password") == 0)
143 return (config_set (&pass, value));
144 else if (strcasecmp (key, "cacert") == 0)
145 return (config_set (&cacert, value));
146 else
147 return (-1);
148 }
150 static int init (void)
151 {
152 static char credentials[1024];
154 if (curl != NULL)
155 {
156 curl_easy_cleanup (curl);
157 }
159 if ((curl = curl_easy_init ()) == NULL)
160 {
161 ERROR ("apache: `curl_easy_init' failed.");
162 return (-1);
163 }
165 curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, apache_curl_callback);
166 curl_easy_setopt (curl, CURLOPT_USERAGENT, PACKAGE_NAME"/"PACKAGE_VERSION);
167 curl_easy_setopt (curl, CURLOPT_ERRORBUFFER, apache_curl_error);
169 if (user != NULL)
170 {
171 if (snprintf (credentials, 1024, "%s:%s", user, pass == NULL ? "" : pass) >= 1024)
172 {
173 ERROR ("apache: Credentials would have been truncated.");
174 return (-1);
175 }
177 curl_easy_setopt (curl, CURLOPT_USERPWD, credentials);
178 }
180 if (url != NULL)
181 {
182 curl_easy_setopt (curl, CURLOPT_URL, url);
183 }
185 if (cacert != NULL)
186 {
187 curl_easy_setopt (curl, CURLOPT_CAINFO, cacert);
188 }
190 return (0);
191 } /* int init */
193 static void submit_counter (const char *type, const char *type_instance,
194 unsigned long long value)
195 {
196 value_t values[1];
197 value_list_t vl = VALUE_LIST_INIT;
199 DEBUG ("type = %s; type_instance = %s; value = %llu;",
200 type, type_instance, value);
202 values[0].counter = value;
204 vl.values = values;
205 vl.values_len = 1;
206 vl.time = time (NULL);
207 strcpy (vl.host, hostname_g);
208 strcpy (vl.plugin, "apache");
209 strcpy (vl.plugin_instance, "");
210 strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
212 plugin_dispatch_values (type, &vl);
213 } /* void submit_counter */
215 static void submit_gauge (const char *type, const char *type_instance,
216 double value)
217 {
218 value_t values[1];
219 value_list_t vl = VALUE_LIST_INIT;
221 DEBUG ("type = %s; type_instance = %s; value = %lf;",
222 type, type_instance, value);
224 values[0].gauge = value;
226 vl.values = values;
227 vl.values_len = 1;
228 vl.time = time (NULL);
229 strcpy (vl.host, hostname_g);
230 strcpy (vl.plugin, "apache");
231 strcpy (vl.plugin_instance, "");
233 if (type_instance != NULL)
234 strncpy (vl.type_instance, type_instance,
235 sizeof (vl.type_instance));
237 plugin_dispatch_values (type, &vl);
238 } /* void submit_counter */
240 static void submit_scoreboard (char *buf)
241 {
242 /*
243 * Scoreboard Key:
244 * "_" Waiting for Connection, "S" Starting up, "R" Reading Request,
245 * "W" Sending Reply, "K" Keepalive (read), "D" DNS Lookup,
246 * "C" Closing connection, "L" Logging, "G" Gracefully finishing,
247 * "I" Idle cleanup of worker, "." Open slot with no current process
248 */
249 long long open = 0LL;
250 long long waiting = 0LL;
251 long long starting = 0LL;
252 long long reading = 0LL;
253 long long sending = 0LL;
254 long long keepalive = 0LL;
255 long long dnslookup = 0LL;
256 long long closing = 0LL;
257 long long logging = 0LL;
258 long long finishing = 0LL;
259 long long idle_cleanup = 0LL;
261 int i;
263 for (i = 0; buf[i] != '\0'; i++)
264 {
265 if (buf[i] == '.') open++;
266 else if (buf[i] == '_') waiting++;
267 else if (buf[i] == 'S') starting++;
268 else if (buf[i] == 'R') reading++;
269 else if (buf[i] == 'W') sending++;
270 else if (buf[i] == 'K') keepalive++;
271 else if (buf[i] == 'D') dnslookup++;
272 else if (buf[i] == 'C') closing++;
273 else if (buf[i] == 'L') logging++;
274 else if (buf[i] == 'G') finishing++;
275 else if (buf[i] == 'I') idle_cleanup++;
276 }
278 submit_gauge ("apache_scoreboard", "open" , open);
279 submit_gauge ("apache_scoreboard", "waiting" , waiting);
280 submit_gauge ("apache_scoreboard", "starting" , starting);
281 submit_gauge ("apache_scoreboard", "reading" , reading);
282 submit_gauge ("apache_scoreboard", "sending" , sending);
283 submit_gauge ("apache_scoreboard", "keepalive", keepalive);
284 submit_gauge ("apache_scoreboard", "dnslookup", dnslookup);
285 submit_gauge ("apache_scoreboard", "closing" , closing);
286 submit_gauge ("apache_scoreboard", "logging" , logging);
287 submit_gauge ("apache_scoreboard", "finishing", finishing);
288 submit_gauge ("apache_scoreboard", "idle_cleanup", idle_cleanup);
289 }
291 static int apache_read (void)
292 {
293 int i;
295 char *ptr;
296 char *saveptr;
297 char *lines[16];
298 int lines_num = 0;
300 char *fields[4];
301 int fields_num;
303 if (curl == NULL)
304 return (-1);
305 if (url == NULL)
306 return (-1);
308 apache_buffer_len = 0;
309 if (curl_easy_perform (curl) != 0)
310 {
311 ERROR ("apache: curl_easy_perform failed: %s",
312 apache_curl_error);
313 return (-1);
314 }
316 ptr = apache_buffer;
317 saveptr = NULL;
318 while ((lines[lines_num] = strtok_r (ptr, "\n\r", &saveptr)) != NULL)
319 {
320 ptr = NULL;
321 lines_num++;
323 if (lines_num >= 16)
324 break;
325 }
327 for (i = 0; i < lines_num; i++)
328 {
329 fields_num = strsplit (lines[i], fields, 4);
331 if (fields_num == 3)
332 {
333 if ((strcmp (fields[0], "Total") == 0)
334 && (strcmp (fields[1], "Accesses:") == 0))
335 submit_counter ("apache_requests", "",
336 atoll (fields[2]));
337 else if ((strcmp (fields[0], "Total") == 0)
338 && (strcmp (fields[1], "kBytes:") == 0))
339 submit_counter ("apache_bytes", "",
340 1024LL * atoll (fields[2]));
341 }
342 else if (fields_num == 2)
343 {
344 if (strcmp (fields[0], "Scoreboard:") == 0)
345 submit_scoreboard (fields[1]);
346 else if (strcmp (fields[0], "BusyServers:") == 0)
347 submit_gauge ("apache_connections", NULL, atol (fields[1]));
348 }
349 }
351 apache_buffer_len = 0;
353 return (0);
354 } /* int apache_read */
355 #endif /* APACHE_HAVE_READ */
357 void module_register (void)
358 {
359 plugin_register_data_set (&apache_bytes_ds);
360 plugin_register_data_set (&apache_requests_ds);
361 plugin_register_data_set (&apache_scoreboard_ds);
362 plugin_register_data_set (&apache_connections_ds);
364 #if APACHE_HAVE_READ
365 plugin_register_config ("apache", config,
366 config_keys, config_keys_num);
367 plugin_register_init ("apache", init);
368 plugin_register_read ("apache", apache_read);
369 #endif
370 }