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; either version 2 of the License, or (at your
8 * option) any later version.
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>
26 #include "configfile.h"
28 #define MODULE_NAME "apache"
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
37 static char *url = NULL;
38 static char *user = NULL;
39 static char *pass = NULL;
40 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];
49 #endif /* HAVE_LIBCURL */
51 /* Limit to 2^27 bytes/s. That's what a gigabit-ethernet link can handle, in
53 static char *bytes_file = "apache/apache_bytes.rrd";
54 static char *bytes_ds_def[] =
56 "DS:count:COUNTER:"COLLECTD_HEARTBEAT":0:134217728",
59 static int bytes_ds_num = 1;
61 /* Limit to 2^20 requests/s */
62 static char *requests_file = "apache/apache_requests.rrd";
63 static char *requests_ds_def[] =
65 "DS:count:COUNTER:"COLLECTD_HEARTBEAT":0:1048576",
68 static int requests_ds_num = 1;
70 static char *scoreboard_file = "apache/apache_scoreboard-%s.rrd";
71 static char *scoreboard_ds_def[] =
73 "DS:count:GAUGE:"COLLECTD_HEARTBEAT":0:U",
76 static int scoreboard_ds_num = 1;
78 static char *config_keys[] =
86 static int config_keys_num = 4;
89 static size_t apache_curl_callback (void *buf, size_t size, size_t nmemb, void *stream)
91 size_t len = size * nmemb;
93 if ((apache_buffer_len + len) >= ABUFFER_SIZE)
95 len = (ABUFFER_SIZE - 1) - apache_buffer_len;
101 memcpy (apache_buffer + apache_buffer_len, (char *) buf, len);
102 apache_buffer_len += len;
103 apache_buffer[apache_buffer_len] = '\0';
107 #endif /* HAVE_LIBCURL */
109 static int config_set (char **var, char *value)
117 if ((*var = strdup (value)) == NULL)
123 static int config (char *key, char *value)
125 if (strcasecmp (key, "url") == 0)
126 return (config_set (&url, value));
127 else if (strcasecmp (key, "user") == 0)
128 return (config_set (&user, value));
129 else if (strcasecmp (key, "password") == 0)
130 return (config_set (&pass, value));
131 else if (strcasecmp (key, "cacert") == 0)
132 return (config_set (&cacert, value));
137 static void 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);
177 #endif /* HAVE_LIBCURL */
180 static void bytes_write (char *host, char *inst, char *val)
182 rrd_update_file (host, bytes_file, val, bytes_ds_def, bytes_ds_num);
185 static void requests_write (char *host, char *inst, char *val)
187 rrd_update_file (host, requests_file, val, requests_ds_def, requests_ds_num);
190 static void scoreboard_write (char *host, char *inst, char *val)
194 if (snprintf (buf, 1024, scoreboard_file, inst) >= 1024)
197 rrd_update_file (host, buf, val, scoreboard_ds_def, scoreboard_ds_num);
201 static void submit (char *type, char *inst, long long value)
206 status = snprintf (buf, 1024, "%u:%lli", (unsigned int) curtime, value);
209 syslog (LOG_ERR, "apache: bytes_submit: snprintf failed");
212 else if (status >= 1024)
214 syslog (LOG_WARNING, "apache: bytes_submit: snprintf was truncated");
218 plugin_submit (type, inst, buf);
221 static void submit_scoreboard (char *buf)
225 * "_" Waiting for Connection, "S" Starting up, "R" Reading Request,
226 * "W" Sending Reply, "K" Keepalive (read), "D" DNS Lookup,
227 * "C" Closing connection, "L" Logging, "G" Gracefully finishing,
228 * "I" Idle cleanup of worker, "." Open slot with no current process
230 long long open = 0LL;
231 long long waiting = 0LL;
232 long long starting = 0LL;
233 long long reading = 0LL;
234 long long sending = 0LL;
235 long long keepalive = 0LL;
236 long long dnslookup = 0LL;
237 long long closing = 0LL;
238 long long logging = 0LL;
239 long long finishing = 0LL;
240 long long idle_cleanup = 0LL;
244 for (i = 0; buf[i] != '\0'; i++)
246 if (buf[i] == '.') open++;
247 else if (buf[i] == '_') waiting++;
248 else if (buf[i] == 'S') starting++;
249 else if (buf[i] == 'R') reading++;
250 else if (buf[i] == 'W') sending++;
251 else if (buf[i] == 'K') keepalive++;
252 else if (buf[i] == 'D') dnslookup++;
253 else if (buf[i] == 'C') closing++;
254 else if (buf[i] == 'L') logging++;
255 else if (buf[i] == 'G') finishing++;
256 else if (buf[i] == 'I') idle_cleanup++;
259 submit ("apache_scoreboard", "open" , open);
260 submit ("apache_scoreboard", "waiting" , waiting);
261 submit ("apache_scoreboard", "starting" , starting);
262 submit ("apache_scoreboard", "reading" , reading);
263 submit ("apache_scoreboard", "sending" , sending);
264 submit ("apache_scoreboard", "keepalive", keepalive);
265 submit ("apache_scoreboard", "dnslookup", dnslookup);
266 submit ("apache_scoreboard", "closing" , closing);
267 submit ("apache_scoreboard", "logging" , logging);
268 submit ("apache_scoreboard", "finishing", finishing);
269 submit ("apache_scoreboard", "idle_cleanup", idle_cleanup);
272 static void apache_read (void)
288 apache_buffer_len = 0;
289 if (curl_easy_perform (curl) != 0)
291 syslog (LOG_WARNING, "apache: curl_easy_perform failed: %s", apache_curl_error);
296 while ((lines[lines_num] = strtok (ptr, "\n\r")) != NULL)
305 for (i = 0; i < lines_num; i++)
307 fields_num = strsplit (lines[i], fields, 4);
311 if ((strcmp (fields[0], "Total") == 0)
312 && (strcmp (fields[1], "Accesses:") == 0))
313 submit ("apache_requests", NULL, atoll (fields[2]));
314 else if ((strcmp (fields[0], "Total") == 0)
315 && (strcmp (fields[1], "kBytes:") == 0))
316 submit ("apache_bytes", NULL, 1024LL * atoll (fields[2]));
318 else if (fields_num == 2)
320 if (strcmp (fields[0], "Scoreboard:") == 0)
321 submit_scoreboard (fields[1]);
325 apache_buffer_len = 0;
328 # define apache_read NULL
329 #endif /* APACHE_HAVE_READ */
331 void module_register (void)
333 plugin_register (MODULE_NAME, init, apache_read, NULL);
334 plugin_register ("apache_requests", NULL, NULL, requests_write);
335 plugin_register ("apache_bytes", NULL, NULL, bytes_write);
336 plugin_register ("apache_scoreboard", NULL, NULL, scoreboard_write);
337 cf_register (MODULE_NAME, config, config_keys, config_keys_num);