2 * collectd - src/apache.c
3 * Copyright (C) 2006 Florian octo Forster
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; only version 2 of the License is applicable.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Florian octo Forster <octo at verplant.org>
25 #include "configfile.h"
26 #include "utils_debug.h"
28 #if HAVE_LIBCURL && HAVE_CURL_CURL_H
29 # define APACHE_HAVE_READ 1
30 # include <curl/curl.h>
32 # define APACHE_HAVE_READ 0
35 /* Limit to 2^27 bytes/s. That's what a gigabit-ethernet link can handle, in
37 static data_source_t apache_bytes_dsrc[1] =
39 {"count", DS_TYPE_COUNTER, 0, 134217728.0},
42 static data_set_t apache_bytes_ds =
44 "apache_bytes", 1, apache_bytes_dsrc
47 /* Limit to 2^20 requests/s */
48 static data_source_t apache_requests_dsrc[1] =
50 {"count", DS_TYPE_COUNTER, 0, 134217728.0},
53 static data_set_t apache_requests_ds =
55 "apache_requests", 1, apache_requests_dsrc
58 static data_source_t apache_scoreboard_dsrc[1] =
60 {"count", DS_TYPE_GAUGE, 0, 65535.0},
63 static data_set_t apache_scoreboard_ds =
65 "apache_scoreboard", 1, apache_scoreboard_dsrc
69 static char *url = NULL;
70 static char *user = NULL;
71 static char *pass = NULL;
72 static char *cacert = NULL;
74 static CURL *curl = NULL;
76 #define ABUFFER_SIZE 16384
77 static char apache_buffer[ABUFFER_SIZE];
78 static int apache_buffer_len = 0;
79 static char apache_curl_error[CURL_ERROR_SIZE];
81 static const char *config_keys[] =
89 static int config_keys_num = 4;
91 static size_t apache_curl_callback (void *buf, size_t size, size_t nmemb, void *stream)
93 size_t len = size * nmemb;
95 if ((apache_buffer_len + len) >= ABUFFER_SIZE)
97 len = (ABUFFER_SIZE - 1) - apache_buffer_len;
103 memcpy (apache_buffer + apache_buffer_len, (char *) buf, len);
104 apache_buffer_len += len;
105 apache_buffer[apache_buffer_len] = '\0';
110 static int config_set (char **var, const char *value)
118 if ((*var = strdup (value)) == NULL)
124 static int config (const char *key, const char *value)
126 if (strcasecmp (key, "url") == 0)
127 return (config_set (&url, value));
128 else if (strcasecmp (key, "user") == 0)
129 return (config_set (&user, value));
130 else if (strcasecmp (key, "password") == 0)
131 return (config_set (&pass, value));
132 else if (strcasecmp (key, "cacert") == 0)
133 return (config_set (&cacert, value));
138 static int init (void)
140 static char credentials[1024];
144 curl_easy_cleanup (curl);
147 if ((curl = curl_easy_init ()) == NULL)
149 syslog (LOG_ERR, "apache: `curl_easy_init' failed.");
153 curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, apache_curl_callback);
154 curl_easy_setopt (curl, CURLOPT_USERAGENT, PACKAGE_NAME"/"PACKAGE_VERSION);
155 curl_easy_setopt (curl, CURLOPT_ERRORBUFFER, apache_curl_error);
159 if (snprintf (credentials, 1024, "%s:%s", user, pass == NULL ? "" : pass) >= 1024)
161 syslog (LOG_ERR, "apache: Credentials would have been truncated.");
165 curl_easy_setopt (curl, CURLOPT_USERPWD, credentials);
170 curl_easy_setopt (curl, CURLOPT_URL, url);
175 curl_easy_setopt (curl, CURLOPT_CAINFO, cacert);
181 static void submit_counter (const char *type, const char *type_instance,
182 unsigned long long value)
185 value_list_t vl = VALUE_LIST_INIT;
187 DBG ("type = %s; type_instance = %s; value = %llu;",
188 type, type_instance, value);
190 values[0].counter = value;
194 vl.time = time (NULL);
195 strcpy (vl.host, hostname);
196 strcpy (vl.plugin, "apache");
197 strcpy (vl.plugin_instance, "");
198 strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
200 plugin_dispatch_values (type, &vl);
201 } /* void submit_counter */
203 static void submit_gauge (const char *type, const char *type_instance,
207 value_list_t vl = VALUE_LIST_INIT;
209 DBG ("type = %s; type_instance = %s; value = %lf;",
210 type, type_instance, value);
212 values[0].gauge = value;
216 vl.time = time (NULL);
217 strcpy (vl.host, hostname);
218 strcpy (vl.plugin, "apache");
219 strcpy (vl.plugin_instance, "");
220 strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
222 plugin_dispatch_values (type, &vl);
223 } /* void submit_counter */
225 static void submit_scoreboard (char *buf)
229 * "_" Waiting for Connection, "S" Starting up, "R" Reading Request,
230 * "W" Sending Reply, "K" Keepalive (read), "D" DNS Lookup,
231 * "C" Closing connection, "L" Logging, "G" Gracefully finishing,
232 * "I" Idle cleanup of worker, "." Open slot with no current process
234 long long open = 0LL;
235 long long waiting = 0LL;
236 long long starting = 0LL;
237 long long reading = 0LL;
238 long long sending = 0LL;
239 long long keepalive = 0LL;
240 long long dnslookup = 0LL;
241 long long closing = 0LL;
242 long long logging = 0LL;
243 long long finishing = 0LL;
244 long long idle_cleanup = 0LL;
248 for (i = 0; buf[i] != '\0'; i++)
250 if (buf[i] == '.') open++;
251 else if (buf[i] == '_') waiting++;
252 else if (buf[i] == 'S') starting++;
253 else if (buf[i] == 'R') reading++;
254 else if (buf[i] == 'W') sending++;
255 else if (buf[i] == 'K') keepalive++;
256 else if (buf[i] == 'D') dnslookup++;
257 else if (buf[i] == 'C') closing++;
258 else if (buf[i] == 'L') logging++;
259 else if (buf[i] == 'G') finishing++;
260 else if (buf[i] == 'I') idle_cleanup++;
263 submit_gauge ("apache_scoreboard", "open" , open);
264 submit_gauge ("apache_scoreboard", "waiting" , waiting);
265 submit_gauge ("apache_scoreboard", "starting" , starting);
266 submit_gauge ("apache_scoreboard", "reading" , reading);
267 submit_gauge ("apache_scoreboard", "sending" , sending);
268 submit_gauge ("apache_scoreboard", "keepalive", keepalive);
269 submit_gauge ("apache_scoreboard", "dnslookup", dnslookup);
270 submit_gauge ("apache_scoreboard", "closing" , closing);
271 submit_gauge ("apache_scoreboard", "logging" , logging);
272 submit_gauge ("apache_scoreboard", "finishing", finishing);
273 submit_gauge ("apache_scoreboard", "idle_cleanup", idle_cleanup);
276 static int apache_read (void)
292 apache_buffer_len = 0;
293 if (curl_easy_perform (curl) != 0)
295 syslog (LOG_ERR, "apache: curl_easy_perform failed: %s",
301 while ((lines[lines_num] = strtok (ptr, "\n\r")) != NULL)
310 for (i = 0; i < lines_num; i++)
312 fields_num = strsplit (lines[i], fields, 4);
316 if ((strcmp (fields[0], "Total") == 0)
317 && (strcmp (fields[1], "Accesses:") == 0))
318 submit_counter ("apache_requests", "",
320 else if ((strcmp (fields[0], "Total") == 0)
321 && (strcmp (fields[1], "kBytes:") == 0))
322 submit_counter ("apache_bytes", "",
323 1024LL * atoll (fields[2]));
325 else if (fields_num == 2)
327 if (strcmp (fields[0], "Scoreboard:") == 0)
328 submit_scoreboard (fields[1]);
332 apache_buffer_len = 0;
335 } /* int apache_read */
336 #endif /* APACHE_HAVE_READ */
338 void module_register (void)
340 plugin_register_data_set (&apache_bytes_ds);
341 plugin_register_data_set (&apache_requests_ds);
342 plugin_register_data_set (&apache_scoreboard_ds);
345 plugin_register_config ("apache", config,
346 config_keys, config_keys_num);
347 plugin_register_init ("apache", init);
348 plugin_register_read ("apache", apache_read);