2 * collectd - src/apache.c
3 * Copyright (C) 2006 Florian octo Forster
4 * Copyright (C) 2007 Florent EppO Monbillard
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.
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.
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
20 * Florian octo Forster <octo at verplant.org>
21 * Florent EppO Monbillard <eppo at darox.net>
22 * - connections/lighttpd extension
28 #include "configfile.h"
30 #if HAVE_LIBCURL && HAVE_CURL_CURL_H
31 # define APACHE_HAVE_READ 1
32 # include <curl/curl.h>
34 # define APACHE_HAVE_READ 0
37 /* Limit to 2^27 bytes/s. That's what a gigabit-ethernet link can handle, in
39 static data_source_t apache_bytes_dsrc[1] =
41 {"count", DS_TYPE_COUNTER, 0, 134217728.0},
44 static data_set_t apache_bytes_ds =
46 "apache_bytes", 1, apache_bytes_dsrc
49 /* Limit to 2^20 requests/s */
50 static data_source_t apache_requests_dsrc[1] =
52 {"count", DS_TYPE_COUNTER, 0, 134217728.0},
55 static data_set_t apache_requests_ds =
57 "apache_requests", 1, apache_requests_dsrc
60 static data_source_t apache_scoreboard_dsrc[1] =
62 {"count", DS_TYPE_GAUGE, 0, 65535.0},
65 static data_set_t apache_scoreboard_ds =
67 "apache_scoreboard", 1, apache_scoreboard_dsrc
70 static data_source_t apache_connections_dsrc[1] =
72 {"count", DS_TYPE_GAUGE, 0, 65535.0},
75 static data_set_t apache_connections_ds =
77 "apache_connections", 1, apache_connections_dsrc
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[] =
101 static int config_keys_num = 4;
103 static size_t apache_curl_callback (void *buf, size_t size, size_t nmemb, void *stream)
105 size_t len = size * nmemb;
107 if ((apache_buffer_len + len) >= ABUFFER_SIZE)
109 len = (ABUFFER_SIZE - 1) - apache_buffer_len;
115 memcpy (apache_buffer + apache_buffer_len, (char *) buf, len);
116 apache_buffer_len += len;
117 apache_buffer[apache_buffer_len] = '\0';
122 static int config_set (char **var, const char *value)
130 if ((*var = strdup (value)) == NULL)
136 static int config (const char *key, const char *value)
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));
150 static int init (void)
152 static char credentials[1024];
156 curl_easy_cleanup (curl);
159 if ((curl = curl_easy_init ()) == NULL)
161 ERROR ("apache: `curl_easy_init' failed.");
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);
171 if (snprintf (credentials, 1024, "%s:%s", user, pass == NULL ? "" : pass) >= 1024)
173 ERROR ("apache: Credentials would have been truncated.");
177 curl_easy_setopt (curl, CURLOPT_USERPWD, credentials);
182 curl_easy_setopt (curl, CURLOPT_URL, url);
187 curl_easy_setopt (curl, CURLOPT_CAINFO, cacert);
193 static void submit_counter (const char *type, const char *type_instance,
194 unsigned long long value)
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;
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,
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;
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)
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
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;
263 for (i = 0; buf[i] != '\0'; i++)
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++;
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);
291 static int apache_read (void)
308 apache_buffer_len = 0;
309 if (curl_easy_perform (curl) != 0)
311 ERROR ("apache: curl_easy_perform failed: %s",
318 while ((lines[lines_num] = strtok_r (ptr, "\n\r", &saveptr)) != NULL)
327 for (i = 0; i < lines_num; i++)
329 fields_num = strsplit (lines[i], fields, 4);
333 if ((strcmp (fields[0], "Total") == 0)
334 && (strcmp (fields[1], "Accesses:") == 0))
335 submit_counter ("apache_requests", "",
337 else if ((strcmp (fields[0], "Total") == 0)
338 && (strcmp (fields[1], "kBytes:") == 0))
339 submit_counter ("apache_bytes", "",
340 1024LL * atoll (fields[2]));
342 else if (fields_num == 2)
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]));
351 apache_buffer_len = 0;
354 } /* int apache_read */
355 #endif /* APACHE_HAVE_READ */
357 void module_register (void)
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);
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);