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; either version 2 of the License, or (at your
9 * option) any later version.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 * Florian octo Forster <octo at verplant.org>
22 * Florent EppO Monbillard <eppo at darox.net>
23 * - connections/lighttpd extension
29 #include "configfile.h"
31 #define MODULE_NAME "apache"
33 #if HAVE_LIBCURL && HAVE_CURL_CURL_H
34 # define APACHE_HAVE_READ 1
35 # include <curl/curl.h>
37 # define APACHE_HAVE_READ 0
40 static char *url = NULL;
41 static char *user = NULL;
42 static char *pass = NULL;
43 static char *cacert = NULL;
46 static CURL *curl = NULL;
48 #define ABUFFER_SIZE 16384
49 static char apache_buffer[ABUFFER_SIZE];
50 static int apache_buffer_len = 0;
51 static char apache_curl_error[CURL_ERROR_SIZE];
52 #endif /* HAVE_LIBCURL */
54 /* Limit to 2^27 bytes/s. That's what a gigabit-ethernet link can handle, in
56 static char *bytes_file = "apache/apache_bytes.rrd";
57 static char *bytes_ds_def[] =
59 "DS:count:COUNTER:"COLLECTD_HEARTBEAT":0:134217728",
62 static int bytes_ds_num = 1;
64 /* Limit to 2^20 requests/s */
65 static char *requests_file = "apache/apache_requests.rrd";
66 static char *requests_ds_def[] =
68 "DS:count:COUNTER:"COLLECTD_HEARTBEAT":0:1048576",
71 static int requests_ds_num = 1;
73 static char *scoreboard_file = "apache/apache_scoreboard-%s.rrd";
74 static char *scoreboard_ds_def[] =
76 "DS:count:GAUGE:"COLLECTD_HEARTBEAT":0:U",
79 static int scoreboard_ds_num = 1;
81 /* for lighttpd; Limit to 65536 active connections */
82 static char *connections_file = "apache/apache_connections.rrd";
83 static char *connections_ds_def[] =
85 "DS:connections:GAUGE:"COLLECTD_HEARTBEAT":0:65536",
88 static int connections_ds_num = 1;
90 static char *config_keys[] =
98 static int config_keys_num = 4;
101 static size_t apache_curl_callback (void *buf, size_t size, size_t nmemb, void *stream)
103 size_t len = size * nmemb;
105 if ((apache_buffer_len + len) >= ABUFFER_SIZE)
107 len = (ABUFFER_SIZE - 1) - apache_buffer_len;
113 memcpy (apache_buffer + apache_buffer_len, (char *) buf, len);
114 apache_buffer_len += len;
115 apache_buffer[apache_buffer_len] = '\0';
119 #endif /* HAVE_LIBCURL */
121 static int config_set (char **var, char *value)
129 if ((*var = strdup (value)) == NULL)
135 static int config (char *key, char *value)
137 if (strcasecmp (key, "url") == 0)
138 return (config_set (&url, value));
139 else if (strcasecmp (key, "user") == 0)
140 return (config_set (&user, value));
141 else if (strcasecmp (key, "password") == 0)
142 return (config_set (&pass, value));
143 else if (strcasecmp (key, "cacert") == 0)
144 return (config_set (&cacert, value));
149 static void init (void)
152 static char credentials[1024];
156 curl_easy_cleanup (curl);
159 if ((curl = curl_easy_init ()) == NULL)
161 syslog (LOG_ERR, "apache: `curl_easy_init' failed.");
165 curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, apache_curl_callback);
166 curl_easy_setopt (curl, CURLOPT_USERAGENT, PACKAGE_NAME"/"PACKAGE_VERSION);
167 curl_easy_setopt (curl, CURLOPT_ERRORBUFFER, apache_curl_error);
171 if (snprintf (credentials, 1024, "%s:%s", user, pass == NULL ? "" : pass) >= 1024)
173 syslog (LOG_ERR, "apache: Credentials would have been truncated.");
177 curl_easy_setopt (curl, CURLOPT_USERPWD, credentials);
182 curl_easy_setopt (curl, CURLOPT_URL, url);
187 curl_easy_setopt (curl, CURLOPT_CAINFO, cacert);
189 #endif /* HAVE_LIBCURL */
192 static void bytes_write (char *host, char *inst, char *val)
194 rrd_update_file (host, bytes_file, val, bytes_ds_def, bytes_ds_num);
197 static void requests_write (char *host, char *inst, char *val)
199 rrd_update_file (host, requests_file, val, requests_ds_def, requests_ds_num);
202 static void scoreboard_write (char *host, char *inst, char *val)
206 if (snprintf (buf, 1024, scoreboard_file, inst) >= 1024)
209 rrd_update_file (host, buf, val, scoreboard_ds_def, scoreboard_ds_num);
212 static void connections_write (char *host, char *inst, char *val)
214 rrd_update_file (host, connections_file, val, connections_ds_def,
219 static void submit (char *type, char *inst, long long value)
224 status = snprintf (buf, 1024, "%u:%lli", (unsigned int) curtime, value);
227 syslog (LOG_ERR, "apache: bytes_submit: snprintf failed");
230 else if (status >= 1024)
232 syslog (LOG_WARNING, "apache: bytes_submit: snprintf was truncated");
236 plugin_submit (type, inst, buf);
239 static void submit_scoreboard (char *buf)
243 * "_" Waiting for Connection, "S" Starting up, "R" Reading Request,
244 * "W" Sending Reply, "K" Keepalive (read), "D" DNS Lookup,
245 * "C" Closing connection, "L" Logging, "G" Gracefully finishing,
246 * "I" Idle cleanup of worker, "." Open slot with no current process
248 long long open = 0LL;
249 long long waiting = 0LL;
250 long long starting = 0LL;
251 long long reading = 0LL;
252 long long sending = 0LL;
253 long long keepalive = 0LL;
254 long long dnslookup = 0LL;
255 long long closing = 0LL;
256 long long logging = 0LL;
257 long long finishing = 0LL;
258 long long idle_cleanup = 0LL;
262 for (i = 0; buf[i] != '\0'; i++)
264 if (buf[i] == '.') open++;
265 else if (buf[i] == '_') waiting++;
266 else if (buf[i] == 'S') starting++;
267 else if (buf[i] == 'R') reading++;
268 else if (buf[i] == 'W') sending++;
269 else if (buf[i] == 'K') keepalive++;
270 else if (buf[i] == 'D') dnslookup++;
271 else if (buf[i] == 'C') closing++;
272 else if (buf[i] == 'L') logging++;
273 else if (buf[i] == 'G') finishing++;
274 else if (buf[i] == 'I') idle_cleanup++;
277 submit ("apache_scoreboard", "open" , open);
278 submit ("apache_scoreboard", "waiting" , waiting);
279 submit ("apache_scoreboard", "starting" , starting);
280 submit ("apache_scoreboard", "reading" , reading);
281 submit ("apache_scoreboard", "sending" , sending);
282 submit ("apache_scoreboard", "keepalive", keepalive);
283 submit ("apache_scoreboard", "dnslookup", dnslookup);
284 submit ("apache_scoreboard", "closing" , closing);
285 submit ("apache_scoreboard", "logging" , logging);
286 submit ("apache_scoreboard", "finishing", finishing);
287 submit ("apache_scoreboard", "idle_cleanup", idle_cleanup);
290 static void apache_read (void)
306 apache_buffer_len = 0;
307 if (curl_easy_perform (curl) != 0)
309 syslog (LOG_WARNING, "apache: curl_easy_perform failed: %s", apache_curl_error);
314 while ((lines[lines_num] = strtok (ptr, "\n\r")) != NULL)
323 for (i = 0; i < lines_num; i++)
325 fields_num = strsplit (lines[i], fields, 4);
329 if ((strcmp (fields[0], "Total") == 0)
330 && (strcmp (fields[1], "Accesses:") == 0))
331 submit ("apache_requests", NULL, atoll (fields[2]));
332 else if ((strcmp (fields[0], "Total") == 0)
333 && (strcmp (fields[1], "kBytes:") == 0))
334 submit ("apache_bytes", NULL, 1024LL * atoll (fields[2]));
336 else if (fields_num == 2)
338 if (strcmp (fields[0], "Scoreboard:") == 0)
339 submit_scoreboard (fields[1]);
340 else if (strcmp (fields[0], "BusyServers:") == 0)
341 submit ("apache_connections", NULL, atol (fields[1]));
345 apache_buffer_len = 0;
348 # define apache_read NULL
349 #endif /* APACHE_HAVE_READ */
351 void module_register (void)
353 plugin_register (MODULE_NAME, init, apache_read, NULL);
354 plugin_register ("apache_requests", NULL, NULL, requests_write);
355 plugin_register ("apache_bytes", NULL, NULL, bytes_write);
356 plugin_register ("apache_scoreboard", NULL, NULL, scoreboard_write);
357 plugin_register ("apache_connections", NULL, NULL, connections_write);
358 cf_register (MODULE_NAME, config, config_keys, config_keys_num);