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 *cacert = NULL;
37 static CURL *curl = NULL;
39 static char *apache_buffer = NULL;
40 static size_t apache_buffer_size = 0;
41 static size_t apache_buffer_fill = 0;
42 static char apache_curl_error[CURL_ERROR_SIZE];
44 static const char *config_keys[] =
51 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
53 static size_t apache_curl_callback (void *buf, size_t size, size_t nmemb,
56 size_t len = size * nmemb;
61 if ((apache_buffer_fill + len) >= apache_buffer_size)
65 temp = (char *) realloc (apache_buffer,
66 apache_buffer_fill + len + 1);
69 ERROR ("apache plugin: realloc failed.");
73 apache_buffer_size = apache_buffer_fill + len + 1;
76 memcpy (apache_buffer + apache_buffer_fill, (char *) buf, len);
77 apache_buffer_fill += len;
78 apache_buffer[apache_buffer_fill] = 0;
83 static int config_set (char **var, const char *value)
91 if ((*var = strdup (value)) == NULL)
97 static int config (const char *key, const char *value)
99 if (strcasecmp (key, "url") == 0)
100 return (config_set (&url, value));
101 else if (strcasecmp (key, "user") == 0)
102 return (config_set (&user, value));
103 else if (strcasecmp (key, "password") == 0)
104 return (config_set (&pass, value));
105 else if (strcasecmp (key, "cacert") == 0)
106 return (config_set (&cacert, value));
111 static int init (void)
113 static char credentials[1024];
117 WARNING ("apache plugin: init: No URL configured, returning "
124 curl_easy_cleanup (curl);
127 if ((curl = curl_easy_init ()) == NULL)
129 ERROR ("apache plugin: init: `curl_easy_init' failed.");
133 curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, apache_curl_callback);
134 curl_easy_setopt (curl, CURLOPT_USERAGENT, PACKAGE_NAME"/"PACKAGE_VERSION);
135 curl_easy_setopt (curl, CURLOPT_ERRORBUFFER, apache_curl_error);
141 status = snprintf (credentials, sizeof (credentials), "%s:%s",
142 user, (pass == NULL) ? "" : pass);
143 if (status >= sizeof (credentials))
145 ERROR ("apache plugin: init: Returning an error "
146 "because the credentials have been "
150 credentials[sizeof (credentials) - 1] = '\0';
152 curl_easy_setopt (curl, CURLOPT_USERPWD, credentials);
155 curl_easy_setopt (curl, CURLOPT_URL, url);
159 curl_easy_setopt (curl, CURLOPT_CAINFO, cacert);
165 static void submit_counter (const char *type, const char *type_instance,
169 value_list_t vl = VALUE_LIST_INIT;
171 values[0].counter = value;
175 vl.time = time (NULL);
176 strcpy (vl.host, hostname_g);
177 strcpy (vl.plugin, "apache");
178 strcpy (vl.plugin_instance, "");
180 if (type_instance != NULL)
182 strncpy (vl.type_instance, type_instance,
183 sizeof (vl.type_instance));
184 vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
187 plugin_dispatch_values (type, &vl);
188 } /* void submit_counter */
190 static void submit_gauge (const char *type, const char *type_instance,
194 value_list_t vl = VALUE_LIST_INIT;
196 values[0].gauge = value;
200 vl.time = time (NULL);
201 strcpy (vl.host, hostname_g);
202 strcpy (vl.plugin, "apache");
203 strcpy (vl.plugin_instance, "");
205 if (type_instance != NULL)
207 strncpy (vl.type_instance, type_instance,
208 sizeof (vl.type_instance));
209 vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
212 plugin_dispatch_values (type, &vl);
213 } /* void submit_counter */
215 static void submit_scoreboard (char *buf)
219 * "_" Waiting for Connection, "S" Starting up, "R" Reading Request,
220 * "W" Sending Reply, "K" Keepalive (read), "D" DNS Lookup,
221 * "C" Closing connection, "L" Logging, "G" Gracefully finishing,
222 * "I" Idle cleanup of worker, "." Open slot with no current process
224 long long open = 0LL;
225 long long waiting = 0LL;
226 long long starting = 0LL;
227 long long reading = 0LL;
228 long long sending = 0LL;
229 long long keepalive = 0LL;
230 long long dnslookup = 0LL;
231 long long closing = 0LL;
232 long long logging = 0LL;
233 long long finishing = 0LL;
234 long long idle_cleanup = 0LL;
238 for (i = 0; buf[i] != '\0'; i++)
240 if (buf[i] == '.') open++;
241 else if (buf[i] == '_') waiting++;
242 else if (buf[i] == 'S') starting++;
243 else if (buf[i] == 'R') reading++;
244 else if (buf[i] == 'W') sending++;
245 else if (buf[i] == 'K') keepalive++;
246 else if (buf[i] == 'D') dnslookup++;
247 else if (buf[i] == 'C') closing++;
248 else if (buf[i] == 'L') logging++;
249 else if (buf[i] == 'G') finishing++;
250 else if (buf[i] == 'I') idle_cleanup++;
253 submit_gauge ("apache_scoreboard", "open" , open);
254 submit_gauge ("apache_scoreboard", "waiting" , waiting);
255 submit_gauge ("apache_scoreboard", "starting" , starting);
256 submit_gauge ("apache_scoreboard", "reading" , reading);
257 submit_gauge ("apache_scoreboard", "sending" , sending);
258 submit_gauge ("apache_scoreboard", "keepalive", keepalive);
259 submit_gauge ("apache_scoreboard", "dnslookup", dnslookup);
260 submit_gauge ("apache_scoreboard", "closing" , closing);
261 submit_gauge ("apache_scoreboard", "logging" , logging);
262 submit_gauge ("apache_scoreboard", "finishing", finishing);
263 submit_gauge ("apache_scoreboard", "idle_cleanup", idle_cleanup);
266 static int apache_read (void)
283 apache_buffer_fill = 0;
284 if (curl_easy_perform (curl) != 0)
286 ERROR ("apache: curl_easy_perform failed: %s",
293 while ((lines[lines_num] = strtok_r (ptr, "\n\r", &saveptr)) != NULL)
302 for (i = 0; i < lines_num; i++)
304 fields_num = strsplit (lines[i], fields, 4);
308 if ((strcmp (fields[0], "Total") == 0)
309 && (strcmp (fields[1], "Accesses:") == 0))
310 submit_counter ("apache_requests", "",
312 else if ((strcmp (fields[0], "Total") == 0)
313 && (strcmp (fields[1], "kBytes:") == 0))
314 submit_counter ("apache_bytes", "",
315 1024LL * atoll (fields[2]));
317 else if (fields_num == 2)
319 if (strcmp (fields[0], "Scoreboard:") == 0)
320 submit_scoreboard (fields[1]);
321 else if (strcmp (fields[0], "BusyServers:") == 0)
322 submit_gauge ("apache_connections", NULL, atol (fields[1]));
326 apache_buffer_fill = 0;
329 } /* int apache_read */
331 void module_register (void)
333 plugin_register_config ("apache", config,
334 config_keys, config_keys_num);
335 plugin_register_init ("apache", init);
336 plugin_register_read ("apache", apache_read);
337 } /* void module_register */