2 * collectd - src/apache.c
3 * Copyright (C) 2006-2008 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 #include <curl/curl.h>
32 static char *url = NULL;
33 static char *user = NULL;
34 static char *pass = NULL;
35 static char *verify_peer = NULL;
36 static char *verify_host = NULL;
37 static char *cacert = NULL;
39 static CURL *curl = NULL;
41 static char *apache_buffer = NULL;
42 static size_t apache_buffer_size = 0;
43 static size_t apache_buffer_fill = 0;
44 static char apache_curl_error[CURL_ERROR_SIZE];
46 static const char *config_keys[] =
55 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
57 static size_t apache_curl_callback (void *buf, size_t size, size_t nmemb,
58 void __attribute__((unused)) *stream)
60 size_t len = size * nmemb;
65 if ((apache_buffer_fill + len) >= apache_buffer_size)
69 temp = (char *) realloc (apache_buffer,
70 apache_buffer_fill + len + 1);
73 ERROR ("apache plugin: realloc failed.");
77 apache_buffer_size = apache_buffer_fill + len + 1;
80 memcpy (apache_buffer + apache_buffer_fill, (char *) buf, len);
81 apache_buffer_fill += len;
82 apache_buffer[apache_buffer_fill] = 0;
87 static int config_set (char **var, const char *value)
95 if ((*var = strdup (value)) == NULL)
101 static int config (const char *key, const char *value)
103 if (strcasecmp (key, "url") == 0)
104 return (config_set (&url, value));
105 else if (strcasecmp (key, "user") == 0)
106 return (config_set (&user, value));
107 else if (strcasecmp (key, "password") == 0)
108 return (config_set (&pass, value));
109 else if (strcasecmp (key, "verifypeer") == 0)
110 return (config_set (&verify_peer, value));
111 else if (strcasecmp (key, "verifyhost") == 0)
112 return (config_set (&verify_host, value));
113 else if (strcasecmp (key, "cacert") == 0)
114 return (config_set (&cacert, value));
119 static int init (void)
121 static char credentials[1024];
125 WARNING ("apache plugin: init: No URL configured, returning "
132 curl_easy_cleanup (curl);
135 if ((curl = curl_easy_init ()) == NULL)
137 ERROR ("apache plugin: init: `curl_easy_init' failed.");
141 curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, apache_curl_callback);
142 curl_easy_setopt (curl, CURLOPT_USERAGENT, PACKAGE_NAME"/"PACKAGE_VERSION);
143 curl_easy_setopt (curl, CURLOPT_ERRORBUFFER, apache_curl_error);
149 status = ssnprintf (credentials, sizeof (credentials), "%s:%s",
150 user, (pass == NULL) ? "" : pass);
151 if ((status < 0) || ((size_t) status >= sizeof (credentials)))
153 ERROR ("apache plugin: init: Returning an error "
154 "because the credentials have been "
159 curl_easy_setopt (curl, CURLOPT_USERPWD, credentials);
162 curl_easy_setopt (curl, CURLOPT_URL, url);
164 if ((verify_peer == NULL) || (strcmp (verify_peer, "true") == 0))
166 curl_easy_setopt (curl, CURLOPT_SSL_VERIFYPEER, 1);
170 curl_easy_setopt (curl, CURLOPT_SSL_VERIFYPEER, 0);
173 if ((verify_host == NULL) || (strcmp (verify_host, "true") == 0))
175 curl_easy_setopt (curl, CURLOPT_SSL_VERIFYHOST, 2);
179 curl_easy_setopt (curl, CURLOPT_SSL_VERIFYHOST, 0);
184 curl_easy_setopt (curl, CURLOPT_CAINFO, cacert);
190 static void submit_counter (const char *type, const char *type_instance,
194 value_list_t vl = VALUE_LIST_INIT;
196 values[0].counter = value;
200 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
201 sstrncpy (vl.plugin, "apache", sizeof (vl.plugin));
202 sstrncpy (vl.plugin_instance, "", sizeof (vl.plugin_instance));
203 sstrncpy (vl.type, type, sizeof (vl.type));
205 if (type_instance != NULL)
206 sstrncpy (vl.type_instance, type_instance,
207 sizeof (vl.type_instance));
209 plugin_dispatch_values (&vl);
210 } /* void submit_counter */
212 static void submit_gauge (const char *type, const char *type_instance,
216 value_list_t vl = VALUE_LIST_INIT;
218 values[0].gauge = value;
222 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
223 sstrncpy (vl.plugin, "apache", sizeof (vl.plugin));
224 sstrncpy (vl.plugin_instance, "", sizeof (vl.plugin_instance));
225 sstrncpy (vl.type, type, sizeof (vl.type));
227 if (type_instance != NULL)
228 sstrncpy (vl.type_instance, type_instance,
229 sizeof (vl.type_instance));
231 plugin_dispatch_values (&vl);
232 } /* void submit_counter */
234 static void submit_scoreboard (char *buf)
238 * "_" Waiting for Connection, "S" Starting up, "R" Reading Request,
239 * "W" Sending Reply, "K" Keepalive (read), "D" DNS Lookup,
240 * "C" Closing connection, "L" Logging, "G" Gracefully finishing,
241 * "I" Idle cleanup of worker, "." Open slot with no current process
243 long long open = 0LL;
244 long long waiting = 0LL;
245 long long starting = 0LL;
246 long long reading = 0LL;
247 long long sending = 0LL;
248 long long keepalive = 0LL;
249 long long dnslookup = 0LL;
250 long long closing = 0LL;
251 long long logging = 0LL;
252 long long finishing = 0LL;
253 long long idle_cleanup = 0LL;
257 for (i = 0; buf[i] != '\0'; i++)
259 if (buf[i] == '.') open++;
260 else if (buf[i] == '_') waiting++;
261 else if (buf[i] == 'S') starting++;
262 else if (buf[i] == 'R') reading++;
263 else if (buf[i] == 'W') sending++;
264 else if (buf[i] == 'K') keepalive++;
265 else if (buf[i] == 'D') dnslookup++;
266 else if (buf[i] == 'C') closing++;
267 else if (buf[i] == 'L') logging++;
268 else if (buf[i] == 'G') finishing++;
269 else if (buf[i] == 'I') idle_cleanup++;
272 submit_gauge ("apache_scoreboard", "open" , open);
273 submit_gauge ("apache_scoreboard", "waiting" , waiting);
274 submit_gauge ("apache_scoreboard", "starting" , starting);
275 submit_gauge ("apache_scoreboard", "reading" , reading);
276 submit_gauge ("apache_scoreboard", "sending" , sending);
277 submit_gauge ("apache_scoreboard", "keepalive", keepalive);
278 submit_gauge ("apache_scoreboard", "dnslookup", dnslookup);
279 submit_gauge ("apache_scoreboard", "closing" , closing);
280 submit_gauge ("apache_scoreboard", "logging" , logging);
281 submit_gauge ("apache_scoreboard", "finishing", finishing);
282 submit_gauge ("apache_scoreboard", "idle_cleanup", idle_cleanup);
285 static int apache_read (void)
302 apache_buffer_fill = 0;
303 if (curl_easy_perform (curl) != 0)
305 ERROR ("apache: curl_easy_perform failed: %s",
312 while ((lines[lines_num] = strtok_r (ptr, "\n\r", &saveptr)) != NULL)
321 for (i = 0; i < lines_num; i++)
323 fields_num = strsplit (lines[i], fields, 4);
327 if ((strcmp (fields[0], "Total") == 0)
328 && (strcmp (fields[1], "Accesses:") == 0))
329 submit_counter ("apache_requests", "",
331 else if ((strcmp (fields[0], "Total") == 0)
332 && (strcmp (fields[1], "kBytes:") == 0))
333 submit_counter ("apache_bytes", "",
334 1024LL * atoll (fields[2]));
336 else if (fields_num == 2)
338 if (strcmp (fields[0], "Scoreboard:") == 0)
339 submit_scoreboard (fields[1]);
340 else if (strcmp (fields[0], "BusyServers:") == 0)
341 submit_gauge ("apache_connections", NULL, atol (fields[1]));
345 apache_buffer_fill = 0;
348 } /* int apache_read */
350 void module_register (void)
352 plugin_register_config ("apache", config,
353 config_keys, config_keys_num);
354 plugin_register_init ("apache", init);
355 plugin_register_read ("apache", apache_read);
356 } /* void module_register */