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"
29 #include "utils_debug.h"
31 #if HAVE_LIBCURL && HAVE_CURL_CURL_H
32 # define APACHE_HAVE_READ 1
33 # include <curl/curl.h>
35 # define APACHE_HAVE_READ 0
38 /* Limit to 2^27 bytes/s. That's what a gigabit-ethernet link can handle, in
40 static data_source_t apache_bytes_dsrc[1] =
42 {"count", DS_TYPE_COUNTER, 0, 134217728.0},
45 static data_set_t apache_bytes_ds =
47 "apache_bytes", 1, apache_bytes_dsrc
50 /* Limit to 2^20 requests/s */
51 static data_source_t apache_requests_dsrc[1] =
53 {"count", DS_TYPE_COUNTER, 0, 134217728.0},
56 static data_set_t apache_requests_ds =
58 "apache_requests", 1, apache_requests_dsrc
61 static data_source_t apache_scoreboard_dsrc[1] =
63 {"count", DS_TYPE_GAUGE, 0, 65535.0},
66 static data_set_t apache_scoreboard_ds =
68 "apache_scoreboard", 1, apache_scoreboard_dsrc
71 static data_source_t apache_connections_dsrc[1] =
73 {"count", DS_TYPE_GAUGE, 0, 65535.0},
76 static data_set_t apache_connections_ds =
78 "apache_connections", 1, apache_connections_dsrc
82 static char *url = NULL;
83 static char *user = NULL;
84 static char *pass = NULL;
85 static char *cacert = NULL;
87 static CURL *curl = NULL;
89 #define ABUFFER_SIZE 16384
90 static char apache_buffer[ABUFFER_SIZE];
91 static int apache_buffer_len = 0;
92 static char apache_curl_error[CURL_ERROR_SIZE];
94 static const char *config_keys[] =
102 static int config_keys_num = 4;
104 static size_t apache_curl_callback (void *buf, size_t size, size_t nmemb, void *stream)
106 size_t len = size * nmemb;
108 if ((apache_buffer_len + len) >= ABUFFER_SIZE)
110 len = (ABUFFER_SIZE - 1) - apache_buffer_len;
116 memcpy (apache_buffer + apache_buffer_len, (char *) buf, len);
117 apache_buffer_len += len;
118 apache_buffer[apache_buffer_len] = '\0';
123 static int config_set (char **var, const char *value)
131 if ((*var = strdup (value)) == NULL)
137 static int config (const char *key, const char *value)
139 if (strcasecmp (key, "url") == 0)
140 return (config_set (&url, value));
141 else if (strcasecmp (key, "user") == 0)
142 return (config_set (&user, value));
143 else if (strcasecmp (key, "password") == 0)
144 return (config_set (&pass, value));
145 else if (strcasecmp (key, "cacert") == 0)
146 return (config_set (&cacert, value));
151 static int init (void)
153 static char credentials[1024];
157 curl_easy_cleanup (curl);
160 if ((curl = curl_easy_init ()) == NULL)
162 syslog (LOG_ERR, "apache: `curl_easy_init' failed.");
166 curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, apache_curl_callback);
167 curl_easy_setopt (curl, CURLOPT_USERAGENT, PACKAGE_NAME"/"PACKAGE_VERSION);
168 curl_easy_setopt (curl, CURLOPT_ERRORBUFFER, apache_curl_error);
172 if (snprintf (credentials, 1024, "%s:%s", user, pass == NULL ? "" : pass) >= 1024)
174 syslog (LOG_ERR, "apache: Credentials would have been truncated.");
178 curl_easy_setopt (curl, CURLOPT_USERPWD, credentials);
183 curl_easy_setopt (curl, CURLOPT_URL, url);
188 curl_easy_setopt (curl, CURLOPT_CAINFO, cacert);
194 static void submit_counter (const char *type, const char *type_instance,
195 unsigned long long value)
198 value_list_t vl = VALUE_LIST_INIT;
200 DBG ("type = %s; type_instance = %s; value = %llu;",
201 type, type_instance, value);
203 values[0].counter = value;
207 vl.time = time (NULL);
208 strcpy (vl.host, hostname_g);
209 strcpy (vl.plugin, "apache");
210 strcpy (vl.plugin_instance, "");
211 strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
213 plugin_dispatch_values (type, &vl);
214 } /* void submit_counter */
216 static void submit_gauge (const char *type, const char *type_instance,
220 value_list_t vl = VALUE_LIST_INIT;
222 DBG ("type = %s; type_instance = %s; value = %lf;",
223 type, type_instance, value);
225 values[0].gauge = value;
229 vl.time = time (NULL);
230 strcpy (vl.host, hostname_g);
231 strcpy (vl.plugin, "apache");
232 strcpy (vl.plugin_instance, "");
234 if (type_instance != NULL)
235 strncpy (vl.type_instance, type_instance,
236 sizeof (vl.type_instance));
238 plugin_dispatch_values (type, &vl);
239 } /* void submit_counter */
241 static void submit_scoreboard (char *buf)
245 * "_" Waiting for Connection, "S" Starting up, "R" Reading Request,
246 * "W" Sending Reply, "K" Keepalive (read), "D" DNS Lookup,
247 * "C" Closing connection, "L" Logging, "G" Gracefully finishing,
248 * "I" Idle cleanup of worker, "." Open slot with no current process
250 long long open = 0LL;
251 long long waiting = 0LL;
252 long long starting = 0LL;
253 long long reading = 0LL;
254 long long sending = 0LL;
255 long long keepalive = 0LL;
256 long long dnslookup = 0LL;
257 long long closing = 0LL;
258 long long logging = 0LL;
259 long long finishing = 0LL;
260 long long idle_cleanup = 0LL;
264 for (i = 0; buf[i] != '\0'; i++)
266 if (buf[i] == '.') open++;
267 else if (buf[i] == '_') waiting++;
268 else if (buf[i] == 'S') starting++;
269 else if (buf[i] == 'R') reading++;
270 else if (buf[i] == 'W') sending++;
271 else if (buf[i] == 'K') keepalive++;
272 else if (buf[i] == 'D') dnslookup++;
273 else if (buf[i] == 'C') closing++;
274 else if (buf[i] == 'L') logging++;
275 else if (buf[i] == 'G') finishing++;
276 else if (buf[i] == 'I') idle_cleanup++;
279 submit_gauge ("apache_scoreboard", "open" , open);
280 submit_gauge ("apache_scoreboard", "waiting" , waiting);
281 submit_gauge ("apache_scoreboard", "starting" , starting);
282 submit_gauge ("apache_scoreboard", "reading" , reading);
283 submit_gauge ("apache_scoreboard", "sending" , sending);
284 submit_gauge ("apache_scoreboard", "keepalive", keepalive);
285 submit_gauge ("apache_scoreboard", "dnslookup", dnslookup);
286 submit_gauge ("apache_scoreboard", "closing" , closing);
287 submit_gauge ("apache_scoreboard", "logging" , logging);
288 submit_gauge ("apache_scoreboard", "finishing", finishing);
289 submit_gauge ("apache_scoreboard", "idle_cleanup", idle_cleanup);
292 static int apache_read (void)
309 apache_buffer_len = 0;
310 if (curl_easy_perform (curl) != 0)
312 syslog (LOG_ERR, "apache: curl_easy_perform failed: %s",
319 while ((lines[lines_num] = strtok_r (ptr, "\n\r", &saveptr)) != NULL)
328 for (i = 0; i < lines_num; i++)
330 fields_num = strsplit (lines[i], fields, 4);
334 if ((strcmp (fields[0], "Total") == 0)
335 && (strcmp (fields[1], "Accesses:") == 0))
336 submit_counter ("apache_requests", "",
338 else if ((strcmp (fields[0], "Total") == 0)
339 && (strcmp (fields[1], "kBytes:") == 0))
340 submit_counter ("apache_bytes", "",
341 1024LL * atoll (fields[2]));
343 else if (fields_num == 2)
345 if (strcmp (fields[0], "Scoreboard:") == 0)
346 submit_scoreboard (fields[1]);
347 else if (strcmp (fields[0], "BusyServers:") == 0)
348 submit_gauge ("apache_connections", NULL, atol (fields[1]));
352 apache_buffer_len = 0;
355 } /* int apache_read */
356 #endif /* APACHE_HAVE_READ */
358 void module_register (void)
360 plugin_register_data_set (&apache_bytes_ds);
361 plugin_register_data_set (&apache_requests_ds);
362 plugin_register_data_set (&apache_scoreboard_ds);
363 plugin_register_data_set (&apache_connections_ds);
366 plugin_register_config ("apache", config,
367 config_keys, config_keys_num);
368 plugin_register_init ("apache", init);
369 plugin_register_read ("apache", apache_read);