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 static char *bytes_file = "apache/apache_bytes.rrd";
50 static char *bytes_ds_def[] =
52 "DS:count:COUNTER:25:0:U",
55 static int bytes_ds_num = 1;
57 static char *requests_file = "apache/apache_requests.rrd";
58 static char *requests_ds_def[] =
60 "DS:count:COUNTER:25:0:U",
63 static int requests_ds_num = 1;
65 static char *scoreboard_file = "apache/apache_scoreboard-%s.rrd";
66 static char *scoreboard_ds_def[] =
68 "DS:count:GAUGE:25:0:U",
71 static int scoreboard_ds_num = 1;
73 static char *config_keys[] =
80 static int config_keys_num = 3;
83 static size_t apache_curl_callback (void *buf, size_t size, size_t nmemb, void *stream)
85 size_t len = size * nmemb;
87 if ((apache_buffer_len + len) >= 4096)
89 len = 4095 - apache_buffer_len;
95 memcpy (apache_buffer + apache_buffer_len, (char *) buf, len);
96 apache_buffer_len += len;
97 apache_buffer[apache_buffer_len] = '\0';
102 static int config_set (char **var, char *value)
110 if ((*var = strdup (value)) == NULL)
116 static int config (char *key, char *value)
118 if (strcasecmp (key, "uri") == 0)
119 return (config_set (&key, value));
120 else if (strcasecmp (key, "user") == 0)
121 return (config_set (&user, value));
122 else if (strcasecmp (key, "password") == 0)
123 return (config_set (&pass, value));
128 static void init (void)
131 static char credentials[1024];
135 curl_easy_cleanup (curl);
138 if ((curl = curl_easy_init ()) == NULL)
140 syslog (LOG_ERR, "apache: `curl_easy_init' failed.");
144 curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, apache_curl_callback);
145 curl_easy_setopt (curl, CURLOPT_USERAGENT, PACKAGE_NAME"/"PACKAGE_VERSION);
146 curl_easy_setopt (curl, CURLOPT_ERRORBUFFER, apache_curl_error);
150 if (snprintf (credentials, 1024, "%s:%s", user, pass == NULL ? "" : pass) >= 1024)
152 syslog (LOG_ERR, "apache: Credentials would have been truncated.");
156 curl_easy_setopt (curl, CURLOPT_USERPWD, credentials);
161 curl_easy_setopt (curl, CURLOPT_URL, url);
163 #endif /* APACHE_HAVE_READ */
166 static void bytes_write (char *host, char *inst, char *val)
168 rrd_update_file (host, bytes_file, val, bytes_ds_def, bytes_ds_num);
171 static void requests_write (char *host, char *inst, char *val)
173 rrd_update_file (host, requests_file, val, requests_ds_def, requests_ds_num);
176 static void scoreboard_write (char *host, char *inst, char *val)
180 if (snprintf (buf, 1024, scoreboard_file, inst) >= 1024)
183 rrd_update_file (host, buf, val, scoreboard_ds_def, scoreboard_ds_num);
187 static void submit (char *type, char *inst, long long value)
192 status = snprintf (buf, 1024, "%u:%lli", (unsigned int) curtime, value);
195 syslog (LOG_ERR, "apache: bytes_submit: snprintf failed");
198 else if (status >= 1024)
200 syslog (LOG_WARNING, "apache: bytes_submit: snprintf was truncated");
204 plugin_submit (type, inst, buf);
207 static void submit_scoreboard (char *buf)
211 * "_" Waiting for Connection, "S" Starting up, "R" Reading Request,
212 * "W" Sending Reply, "K" Keepalive (read), "D" DNS Lookup,
213 * "C" Closing connection, "L" Logging, "G" Gracefully finishing,
214 * "I" Idle cleanup of worker, "." Open slot with no current process
216 long long open = 0LL;
217 long long waiting = 0LL;
218 long long starting = 0LL;
219 long long reading = 0LL;
220 long long sending = 0LL;
221 long long keepalive = 0LL;
222 long long dnslookup = 0LL;
223 long long closing = 0LL;
224 long long logging = 0LL;
225 long long finishing = 0LL;
226 long long idle_cleanup = 0LL;
230 for (i = 0; buf[i] != '\0'; i++)
232 if (buf[i] == '.') open++;
233 else if (buf[i] == '_') waiting++;
234 else if (buf[i] == 'S') starting++;
235 else if (buf[i] == 'R') reading++;
236 else if (buf[i] == 'W') sending++;
237 else if (buf[i] == 'K') keepalive++;
238 else if (buf[i] == 'D') dnslookup++;
239 else if (buf[i] == 'C') closing++;
240 else if (buf[i] == 'L') logging++;
241 else if (buf[i] == 'G') finishing++;
242 else if (buf[i] == 'I') idle_cleanup++;
245 submit ("apache_scoreboard", "open" , open);
246 submit ("apache_scoreboard", "waiting" , waiting);
247 submit ("apache_scoreboard", "starting" , starting);
248 submit ("apache_scoreboard", "reading" , reading);
249 submit ("apache_scoreboard", "sending" , sending);
250 submit ("apache_scoreboard", "keepalive", keepalive);
251 submit ("apache_scoreboard", "dnslookup", dnslookup);
252 submit ("apache_scoreboard", "closing" , closing);
253 submit ("apache_scoreboard", "logging" , logging);
254 submit ("apache_scoreboard", "finishing", finishing);
255 submit ("apache_scoreboard", "idle_cleanup", idle_cleanup);
258 static void apache_read (void)
274 if (curl_easy_perform (curl) != 0)
276 syslog (LOG_WARNING, "apache: curl_easy_perform failed: %s", apache_curl_error);
281 while ((lines[lines_num] = strtok (ptr, "\n\r")) != NULL)
290 for (i = 0; i < lines_num; i++)
292 fields_num = strsplit (lines[i], fields, 4);
296 if ((strcmp (fields[0], "Total") == 0)
297 && (strcmp (fields[1], "Accesses:") == 0))
298 submit ("apache_requests", NULL, atoll (fields[2]));
299 else if ((strcmp (fields[0], "Total") == 0)
300 && (strcmp (fields[1], "kBytes:") == 0))
301 submit ("apache_bytes", NULL, 1024LL * atoll (fields[2]));
303 else if (fields_num == 2)
305 if (strcmp (fields[0], "Scoreboard:") == 0)
306 submit_scoreboard (fields[1]);
310 apache_buffer_len = 0;
313 # define apache_read NULL
314 #endif /* APACHE_HAVE_READ */
316 void module_register (void)
318 plugin_register (MODULE_NAME, init, apache_read, NULL);
319 plugin_register ("apache_requests", NULL, NULL, requests_write);
320 plugin_register ("apache_bytes", NULL, NULL, bytes_write);
321 plugin_register ("apache_scoreboard", NULL, NULL, scoreboard_write);
322 cf_register (MODULE_NAME, config, config_keys, config_keys_num);