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,
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 >= 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 vl.time = time (NULL);
201 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
202 sstrncpy (vl.plugin, "apache", sizeof (vl.plugin));
203 sstrncpy (vl.plugin_instance, "", sizeof (vl.plugin_instance));
204 sstrncpy (vl.type, type, sizeof (vl.type));
206 if (type_instance != NULL)
207 sstrncpy (vl.type_instance, type_instance,
208 sizeof (vl.type_instance));
210 plugin_dispatch_values (&vl);
211 } /* void submit_counter */
213 static void submit_gauge (const char *type, const char *type_instance,
217 value_list_t vl = VALUE_LIST_INIT;
219 values[0].gauge = value;
223 vl.time = time (NULL);
224 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
225 sstrncpy (vl.plugin, "apache", sizeof (vl.plugin));
226 sstrncpy (vl.plugin_instance, "", sizeof (vl.plugin_instance));
227 sstrncpy (vl.type, type, sizeof (vl.type));
229 if (type_instance != NULL)
230 sstrncpy (vl.type_instance, type_instance,
231 sizeof (vl.type_instance));
233 plugin_dispatch_values (&vl);
234 } /* void submit_counter */
236 static void submit_scoreboard (char *buf)
240 * "_" Waiting for Connection, "S" Starting up, "R" Reading Request,
241 * "W" Sending Reply, "K" Keepalive (read), "D" DNS Lookup,
242 * "C" Closing connection, "L" Logging, "G" Gracefully finishing,
243 * "I" Idle cleanup of worker, "." Open slot with no current process
245 long long open = 0LL;
246 long long waiting = 0LL;
247 long long starting = 0LL;
248 long long reading = 0LL;
249 long long sending = 0LL;
250 long long keepalive = 0LL;
251 long long dnslookup = 0LL;
252 long long closing = 0LL;
253 long long logging = 0LL;
254 long long finishing = 0LL;
255 long long idle_cleanup = 0LL;
259 for (i = 0; buf[i] != '\0'; i++)
261 if (buf[i] == '.') open++;
262 else if (buf[i] == '_') waiting++;
263 else if (buf[i] == 'S') starting++;
264 else if (buf[i] == 'R') reading++;
265 else if (buf[i] == 'W') sending++;
266 else if (buf[i] == 'K') keepalive++;
267 else if (buf[i] == 'D') dnslookup++;
268 else if (buf[i] == 'C') closing++;
269 else if (buf[i] == 'L') logging++;
270 else if (buf[i] == 'G') finishing++;
271 else if (buf[i] == 'I') idle_cleanup++;
274 submit_gauge ("apache_scoreboard", "open" , open);
275 submit_gauge ("apache_scoreboard", "waiting" , waiting);
276 submit_gauge ("apache_scoreboard", "starting" , starting);
277 submit_gauge ("apache_scoreboard", "reading" , reading);
278 submit_gauge ("apache_scoreboard", "sending" , sending);
279 submit_gauge ("apache_scoreboard", "keepalive", keepalive);
280 submit_gauge ("apache_scoreboard", "dnslookup", dnslookup);
281 submit_gauge ("apache_scoreboard", "closing" , closing);
282 submit_gauge ("apache_scoreboard", "logging" , logging);
283 submit_gauge ("apache_scoreboard", "finishing", finishing);
284 submit_gauge ("apache_scoreboard", "idle_cleanup", idle_cleanup);
287 static int apache_read (void)
304 apache_buffer_fill = 0;
305 if (curl_easy_perform (curl) != 0)
307 ERROR ("apache: curl_easy_perform failed: %s",
314 while ((lines[lines_num] = strtok_r (ptr, "\n\r", &saveptr)) != NULL)
323 for (i = 0; i < lines_num; i++)
325 fields_num = strsplit (lines[i], fields, 4);
329 if ((strcmp (fields[0], "Total") == 0)
330 && (strcmp (fields[1], "Accesses:") == 0))
331 submit_counter ("apache_requests", "",
333 else if ((strcmp (fields[0], "Total") == 0)
334 && (strcmp (fields[1], "kBytes:") == 0))
335 submit_counter ("apache_bytes", "",
336 1024LL * atoll (fields[2]));
338 else if (fields_num == 2)
340 if (strcmp (fields[0], "Scoreboard:") == 0)
341 submit_scoreboard (fields[1]);
342 else if (strcmp (fields[0], "BusyServers:") == 0)
343 submit_gauge ("apache_connections", NULL, atol (fields[1]));
347 apache_buffer_fill = 0;
350 } /* int apache_read */
352 void module_register (void)
354 plugin_register_config ("apache", config,
355 config_keys, config_keys_num);
356 plugin_register_init ("apache", init);
357 plugin_register_read ("apache", apache_read);
358 } /* void module_register */