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;
42 static CURL *curl = NULL;
44 static char apache_buffer[4096];
45 static int apache_buffer_len = 0;
46 static char apache_curl_error[CURL_ERROR_SIZE];
49 /* Limit to 2^27 bytes/s. That's what a gigabit-ethernet link can handle, in
51 static char *bytes_file = "apache/apache_bytes.rrd";
52 static char *bytes_ds_def[] =
54 "DS:count:COUNTER:"COLLECTD_HEARTBEAT":0:134217728",
57 static int bytes_ds_num = 1;
59 /* Limit to 2^20 requests/s */
60 static char *requests_file = "apache/apache_requests.rrd";
61 static char *requests_ds_def[] =
63 "DS:count:COUNTER:"COLLECTD_HEARTBEAT":0:1048576",
66 static int requests_ds_num = 1;
68 static char *scoreboard_file = "apache/apache_scoreboard-%s.rrd";
69 static char *scoreboard_ds_def[] =
71 "DS:count:GAUGE:"COLLECTD_HEARTBEAT":0:U",
74 static int scoreboard_ds_num = 1;
76 static char *config_keys[] =
83 static int config_keys_num = 3;
86 static size_t apache_curl_callback (void *buf, size_t size, size_t nmemb, void *stream)
88 size_t len = size * nmemb;
90 if ((apache_buffer_len + len) >= 4096)
92 len = 4095 - apache_buffer_len;
98 memcpy (apache_buffer + apache_buffer_len, (char *) buf, len);
99 apache_buffer_len += len;
100 apache_buffer[apache_buffer_len] = '\0';
105 static int config_set (char **var, char *value)
113 if ((*var = strdup (value)) == NULL)
119 static int config (char *key, char *value)
121 if (strcasecmp (key, "url") == 0)
122 return (config_set (&url, value));
123 else if (strcasecmp (key, "user") == 0)
124 return (config_set (&user, value));
125 else if (strcasecmp (key, "password") == 0)
126 return (config_set (&pass, value));
131 static void init (void)
134 static char credentials[1024];
138 curl_easy_cleanup (curl);
141 if ((curl = curl_easy_init ()) == NULL)
143 syslog (LOG_ERR, "apache: `curl_easy_init' failed.");
147 curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, apache_curl_callback);
148 curl_easy_setopt (curl, CURLOPT_USERAGENT, PACKAGE_NAME"/"PACKAGE_VERSION);
149 curl_easy_setopt (curl, CURLOPT_ERRORBUFFER, apache_curl_error);
153 if (snprintf (credentials, 1024, "%s:%s", user, pass == NULL ? "" : pass) >= 1024)
155 syslog (LOG_ERR, "apache: Credentials would have been truncated.");
159 curl_easy_setopt (curl, CURLOPT_USERPWD, credentials);
164 curl_easy_setopt (curl, CURLOPT_URL, url);
166 #endif /* APACHE_HAVE_READ */
169 static void bytes_write (char *host, char *inst, char *val)
171 rrd_update_file (host, bytes_file, val, bytes_ds_def, bytes_ds_num);
174 static void requests_write (char *host, char *inst, char *val)
176 rrd_update_file (host, requests_file, val, requests_ds_def, requests_ds_num);
179 static void scoreboard_write (char *host, char *inst, char *val)
183 if (snprintf (buf, 1024, scoreboard_file, inst) >= 1024)
186 rrd_update_file (host, buf, val, scoreboard_ds_def, scoreboard_ds_num);
190 static void submit (char *type, char *inst, long long value)
195 status = snprintf (buf, 1024, "%u:%lli", (unsigned int) curtime, value);
198 syslog (LOG_ERR, "apache: bytes_submit: snprintf failed");
201 else if (status >= 1024)
203 syslog (LOG_WARNING, "apache: bytes_submit: snprintf was truncated");
207 plugin_submit (type, inst, buf);
210 static void submit_scoreboard (char *buf)
214 * "_" Waiting for Connection, "S" Starting up, "R" Reading Request,
215 * "W" Sending Reply, "K" Keepalive (read), "D" DNS Lookup,
216 * "C" Closing connection, "L" Logging, "G" Gracefully finishing,
217 * "I" Idle cleanup of worker, "." Open slot with no current process
219 long long open = 0LL;
220 long long waiting = 0LL;
221 long long starting = 0LL;
222 long long reading = 0LL;
223 long long sending = 0LL;
224 long long keepalive = 0LL;
225 long long dnslookup = 0LL;
226 long long closing = 0LL;
227 long long logging = 0LL;
228 long long finishing = 0LL;
229 long long idle_cleanup = 0LL;
233 for (i = 0; buf[i] != '\0'; i++)
235 if (buf[i] == '.') open++;
236 else if (buf[i] == '_') waiting++;
237 else if (buf[i] == 'S') starting++;
238 else if (buf[i] == 'R') reading++;
239 else if (buf[i] == 'W') sending++;
240 else if (buf[i] == 'K') keepalive++;
241 else if (buf[i] == 'D') dnslookup++;
242 else if (buf[i] == 'C') closing++;
243 else if (buf[i] == 'L') logging++;
244 else if (buf[i] == 'G') finishing++;
245 else if (buf[i] == 'I') idle_cleanup++;
248 submit ("apache_scoreboard", "open" , open);
249 submit ("apache_scoreboard", "waiting" , waiting);
250 submit ("apache_scoreboard", "starting" , starting);
251 submit ("apache_scoreboard", "reading" , reading);
252 submit ("apache_scoreboard", "sending" , sending);
253 submit ("apache_scoreboard", "keepalive", keepalive);
254 submit ("apache_scoreboard", "dnslookup", dnslookup);
255 submit ("apache_scoreboard", "closing" , closing);
256 submit ("apache_scoreboard", "logging" , logging);
257 submit ("apache_scoreboard", "finishing", finishing);
258 submit ("apache_scoreboard", "idle_cleanup", idle_cleanup);
261 static void apache_read (void)
277 if (curl_easy_perform (curl) != 0)
279 syslog (LOG_WARNING, "apache: curl_easy_perform failed: %s", apache_curl_error);
284 while ((lines[lines_num] = strtok (ptr, "\n\r")) != NULL)
293 for (i = 0; i < lines_num; i++)
295 fields_num = strsplit (lines[i], fields, 4);
299 if ((strcmp (fields[0], "Total") == 0)
300 && (strcmp (fields[1], "Accesses:") == 0))
301 submit ("apache_requests", NULL, atoll (fields[2]));
302 else if ((strcmp (fields[0], "Total") == 0)
303 && (strcmp (fields[1], "kBytes:") == 0))
304 submit ("apache_bytes", NULL, 1024LL * atoll (fields[2]));
306 else if (fields_num == 2)
308 if (strcmp (fields[0], "Scoreboard:") == 0)
309 submit_scoreboard (fields[1]);
313 apache_buffer_len = 0;
316 # define apache_read NULL
317 #endif /* APACHE_HAVE_READ */
319 void module_register (void)
321 plugin_register (MODULE_NAME, init, apache_read, NULL);
322 plugin_register ("apache_requests", NULL, NULL, requests_write);
323 plugin_register ("apache_bytes", NULL, NULL, bytes_write);
324 plugin_register ("apache_scoreboard", NULL, NULL, scoreboard_write);
325 cf_register (MODULE_NAME, config, config_keys, config_keys_num);