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
38 static char *url = NULL;
39 static char *user = NULL;
40 static char *pass = NULL;
41 static char *cacert = NULL;
43 static CURL *curl = NULL;
45 #define ABUFFER_SIZE 16384
46 static char apache_buffer[ABUFFER_SIZE];
47 static int apache_buffer_len = 0;
48 static char apache_curl_error[CURL_ERROR_SIZE];
50 static const char *config_keys[] =
58 static int config_keys_num = 4;
60 static size_t apache_curl_callback (void *buf, size_t size, size_t nmemb, void *stream)
62 size_t len = size * nmemb;
64 if ((apache_buffer_len + len) >= ABUFFER_SIZE)
66 len = (ABUFFER_SIZE - 1) - apache_buffer_len;
72 memcpy (apache_buffer + apache_buffer_len, (char *) buf, len);
73 apache_buffer_len += len;
74 apache_buffer[apache_buffer_len] = '\0';
79 static int config_set (char **var, const char *value)
87 if ((*var = strdup (value)) == NULL)
93 static int config (const char *key, const char *value)
95 if (strcasecmp (key, "url") == 0)
96 return (config_set (&url, value));
97 else if (strcasecmp (key, "user") == 0)
98 return (config_set (&user, value));
99 else if (strcasecmp (key, "password") == 0)
100 return (config_set (&pass, value));
101 else if (strcasecmp (key, "cacert") == 0)
102 return (config_set (&cacert, value));
107 static int init (void)
109 static char credentials[1024];
113 curl_easy_cleanup (curl);
116 if ((curl = curl_easy_init ()) == NULL)
118 ERROR ("apache: `curl_easy_init' failed.");
122 curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, apache_curl_callback);
123 curl_easy_setopt (curl, CURLOPT_USERAGENT, PACKAGE_NAME"/"PACKAGE_VERSION);
124 curl_easy_setopt (curl, CURLOPT_ERRORBUFFER, apache_curl_error);
128 if (snprintf (credentials, 1024, "%s:%s", user, pass == NULL ? "" : pass) >= 1024)
130 ERROR ("apache: Credentials would have been truncated.");
134 curl_easy_setopt (curl, CURLOPT_USERPWD, credentials);
139 curl_easy_setopt (curl, CURLOPT_URL, url);
144 curl_easy_setopt (curl, CURLOPT_CAINFO, cacert);
150 static void submit_counter (const char *type, const char *type_instance,
151 unsigned long long value)
154 value_list_t vl = VALUE_LIST_INIT;
156 DEBUG ("type = %s; type_instance = %s; value = %llu;",
157 type, type_instance, value);
159 values[0].counter = value;
163 vl.time = time (NULL);
164 strcpy (vl.host, hostname_g);
165 strcpy (vl.plugin, "apache");
166 strcpy (vl.plugin_instance, "");
167 strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
169 plugin_dispatch_values (type, &vl);
170 } /* void submit_counter */
172 static void submit_gauge (const char *type, const char *type_instance,
176 value_list_t vl = VALUE_LIST_INIT;
178 DEBUG ("type = %s; type_instance = %s; value = %lf;",
179 type, type_instance, value);
181 values[0].gauge = value;
185 vl.time = time (NULL);
186 strcpy (vl.host, hostname_g);
187 strcpy (vl.plugin, "apache");
188 strcpy (vl.plugin_instance, "");
190 if (type_instance != NULL)
191 strncpy (vl.type_instance, type_instance,
192 sizeof (vl.type_instance));
194 plugin_dispatch_values (type, &vl);
195 } /* void submit_counter */
197 static void submit_scoreboard (char *buf)
201 * "_" Waiting for Connection, "S" Starting up, "R" Reading Request,
202 * "W" Sending Reply, "K" Keepalive (read), "D" DNS Lookup,
203 * "C" Closing connection, "L" Logging, "G" Gracefully finishing,
204 * "I" Idle cleanup of worker, "." Open slot with no current process
206 long long open = 0LL;
207 long long waiting = 0LL;
208 long long starting = 0LL;
209 long long reading = 0LL;
210 long long sending = 0LL;
211 long long keepalive = 0LL;
212 long long dnslookup = 0LL;
213 long long closing = 0LL;
214 long long logging = 0LL;
215 long long finishing = 0LL;
216 long long idle_cleanup = 0LL;
220 for (i = 0; buf[i] != '\0'; i++)
222 if (buf[i] == '.') open++;
223 else if (buf[i] == '_') waiting++;
224 else if (buf[i] == 'S') starting++;
225 else if (buf[i] == 'R') reading++;
226 else if (buf[i] == 'W') sending++;
227 else if (buf[i] == 'K') keepalive++;
228 else if (buf[i] == 'D') dnslookup++;
229 else if (buf[i] == 'C') closing++;
230 else if (buf[i] == 'L') logging++;
231 else if (buf[i] == 'G') finishing++;
232 else if (buf[i] == 'I') idle_cleanup++;
235 submit_gauge ("apache_scoreboard", "open" , open);
236 submit_gauge ("apache_scoreboard", "waiting" , waiting);
237 submit_gauge ("apache_scoreboard", "starting" , starting);
238 submit_gauge ("apache_scoreboard", "reading" , reading);
239 submit_gauge ("apache_scoreboard", "sending" , sending);
240 submit_gauge ("apache_scoreboard", "keepalive", keepalive);
241 submit_gauge ("apache_scoreboard", "dnslookup", dnslookup);
242 submit_gauge ("apache_scoreboard", "closing" , closing);
243 submit_gauge ("apache_scoreboard", "logging" , logging);
244 submit_gauge ("apache_scoreboard", "finishing", finishing);
245 submit_gauge ("apache_scoreboard", "idle_cleanup", idle_cleanup);
248 static int apache_read (void)
265 apache_buffer_len = 0;
266 if (curl_easy_perform (curl) != 0)
268 ERROR ("apache: curl_easy_perform failed: %s",
275 while ((lines[lines_num] = strtok_r (ptr, "\n\r", &saveptr)) != NULL)
284 for (i = 0; i < lines_num; i++)
286 fields_num = strsplit (lines[i], fields, 4);
290 if ((strcmp (fields[0], "Total") == 0)
291 && (strcmp (fields[1], "Accesses:") == 0))
292 submit_counter ("apache_requests", "",
294 else if ((strcmp (fields[0], "Total") == 0)
295 && (strcmp (fields[1], "kBytes:") == 0))
296 submit_counter ("apache_bytes", "",
297 1024LL * atoll (fields[2]));
299 else if (fields_num == 2)
301 if (strcmp (fields[0], "Scoreboard:") == 0)
302 submit_scoreboard (fields[1]);
303 else if (strcmp (fields[0], "BusyServers:") == 0)
304 submit_gauge ("apache_connections", NULL, atol (fields[1]));
308 apache_buffer_len = 0;
311 } /* int apache_read */
312 #endif /* APACHE_HAVE_READ */
314 void module_register (void)
317 plugin_register_config ("apache", config,
318 config_keys, config_keys_num);
319 plugin_register_init ("apache", init);
320 plugin_register_read ("apache", apache_read);
322 } /* void module_register */