1 /**
2 * collectd - src/apache.c
3 * Copyright (C) 2006 Florian octo Forster
4 *
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.
9 *
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.
14 *
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
18 *
19 * Authors:
20 * Florian octo Forster <octo at verplant.org>
21 **/
23 #include "collectd.h"
24 #include "common.h"
25 #include "plugin.h"
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>
33 #else
34 # define APACHE_HAVE_READ 0
35 #endif
37 static char *url = NULL;
38 static char *user = NULL;
39 static char *pass = NULL;
40 static char *cacert = NULL;
42 #if HAVE_LIBCURL
43 static CURL *curl = NULL;
45 static char apache_buffer[4096];
46 static int apache_buffer_len = 0;
47 static char apache_curl_error[CURL_ERROR_SIZE];
48 #endif /* HAVE_LIBCURL */
50 /* Limit to 2^27 bytes/s. That's what a gigabit-ethernet link can handle, in
51 * theory. */
52 static char *bytes_file = "apache/apache_bytes.rrd";
53 static char *bytes_ds_def[] =
54 {
55 "DS:count:COUNTER:"COLLECTD_HEARTBEAT":0:134217728",
56 NULL
57 };
58 static int bytes_ds_num = 1;
60 /* Limit to 2^20 requests/s */
61 static char *requests_file = "apache/apache_requests.rrd";
62 static char *requests_ds_def[] =
63 {
64 "DS:count:COUNTER:"COLLECTD_HEARTBEAT":0:1048576",
65 NULL
66 };
67 static int requests_ds_num = 1;
69 static char *scoreboard_file = "apache/apache_scoreboard-%s.rrd";
70 static char *scoreboard_ds_def[] =
71 {
72 "DS:count:GAUGE:"COLLECTD_HEARTBEAT":0:U",
73 NULL
74 };
75 static int scoreboard_ds_num = 1;
77 static char *config_keys[] =
78 {
79 "URL",
80 "User",
81 "Password",
82 "CACert",
83 NULL
84 };
85 static int config_keys_num = 4;
87 #if HAVE_LIBCURL
88 static size_t apache_curl_callback (void *buf, size_t size, size_t nmemb, void *stream)
89 {
90 size_t len = size * nmemb;
92 if ((apache_buffer_len + len) >= 4096)
93 {
94 len = 4095 - apache_buffer_len;
95 }
97 if (len <= 0)
98 return (len);
100 memcpy (apache_buffer + apache_buffer_len, (char *) buf, len);
101 apache_buffer_len += len;
102 apache_buffer[apache_buffer_len] = '\0';
104 return (len);
105 }
106 #endif /* HAVE_LIBCURL */
108 static int config_set (char **var, char *value)
109 {
110 if (*var != NULL)
111 {
112 free (*var);
113 *var = NULL;
114 }
116 if ((*var = strdup (value)) == NULL)
117 return (1);
118 else
119 return (0);
120 }
122 static int config (char *key, char *value)
123 {
124 if (strcasecmp (key, "url") == 0)
125 return (config_set (&url, value));
126 else if (strcasecmp (key, "user") == 0)
127 return (config_set (&user, value));
128 else if (strcasecmp (key, "password") == 0)
129 return (config_set (&pass, value));
130 else if (strcasecmp (key, "cacert") == 0)
131 return (config_set (&cacert, value));
132 else
133 return (-1);
134 }
136 static void init (void)
137 {
138 #if HAVE_LIBCURL
139 static char credentials[1024];
141 if (curl != NULL)
142 {
143 curl_easy_cleanup (curl);
144 }
146 if ((curl = curl_easy_init ()) == NULL)
147 {
148 syslog (LOG_ERR, "apache: `curl_easy_init' failed.");
149 return;
150 }
152 curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, apache_curl_callback);
153 curl_easy_setopt (curl, CURLOPT_USERAGENT, PACKAGE_NAME"/"PACKAGE_VERSION);
154 curl_easy_setopt (curl, CURLOPT_ERRORBUFFER, apache_curl_error);
156 if (user != NULL)
157 {
158 if (snprintf (credentials, 1024, "%s:%s", user, pass == NULL ? "" : pass) >= 1024)
159 {
160 syslog (LOG_ERR, "apache: Credentials would have been truncated.");
161 return;
162 }
164 curl_easy_setopt (curl, CURLOPT_USERPWD, credentials);
165 }
167 if (url != NULL)
168 {
169 curl_easy_setopt (curl, CURLOPT_URL, url);
170 }
172 if (cacert != NULL)
173 {
174 curl_easy_setopt (curl, CURLOPT_CAINFO, cacert);
175 }
176 #endif /* HAVE_LIBCURL */
177 }
179 static void bytes_write (char *host, char *inst, char *val)
180 {
181 rrd_update_file (host, bytes_file, val, bytes_ds_def, bytes_ds_num);
182 }
184 static void requests_write (char *host, char *inst, char *val)
185 {
186 rrd_update_file (host, requests_file, val, requests_ds_def, requests_ds_num);
187 }
189 static void scoreboard_write (char *host, char *inst, char *val)
190 {
191 char buf[1024];
193 if (snprintf (buf, 1024, scoreboard_file, inst) >= 1024)
194 return;
196 rrd_update_file (host, buf, val, scoreboard_ds_def, scoreboard_ds_num);
197 }
199 #if APACHE_HAVE_READ
200 static void submit (char *type, char *inst, long long value)
201 {
202 char buf[1024];
203 int status;
205 status = snprintf (buf, 1024, "%u:%lli", (unsigned int) curtime, value);
206 if (status < 0)
207 {
208 syslog (LOG_ERR, "apache: bytes_submit: snprintf failed");
209 return;
210 }
211 else if (status >= 1024)
212 {
213 syslog (LOG_WARNING, "apache: bytes_submit: snprintf was truncated");
214 return;
215 }
217 plugin_submit (type, inst, buf);
218 }
220 static void submit_scoreboard (char *buf)
221 {
222 /*
223 * Scoreboard Key:
224 * "_" Waiting for Connection, "S" Starting up, "R" Reading Request,
225 * "W" Sending Reply, "K" Keepalive (read), "D" DNS Lookup,
226 * "C" Closing connection, "L" Logging, "G" Gracefully finishing,
227 * "I" Idle cleanup of worker, "." Open slot with no current process
228 */
229 long long open = 0LL;
230 long long waiting = 0LL;
231 long long starting = 0LL;
232 long long reading = 0LL;
233 long long sending = 0LL;
234 long long keepalive = 0LL;
235 long long dnslookup = 0LL;
236 long long closing = 0LL;
237 long long logging = 0LL;
238 long long finishing = 0LL;
239 long long idle_cleanup = 0LL;
241 int i;
243 for (i = 0; buf[i] != '\0'; i++)
244 {
245 if (buf[i] == '.') open++;
246 else if (buf[i] == '_') waiting++;
247 else if (buf[i] == 'S') starting++;
248 else if (buf[i] == 'R') reading++;
249 else if (buf[i] == 'W') sending++;
250 else if (buf[i] == 'K') keepalive++;
251 else if (buf[i] == 'D') dnslookup++;
252 else if (buf[i] == 'C') closing++;
253 else if (buf[i] == 'L') logging++;
254 else if (buf[i] == 'G') finishing++;
255 else if (buf[i] == 'I') idle_cleanup++;
256 }
258 submit ("apache_scoreboard", "open" , open);
259 submit ("apache_scoreboard", "waiting" , waiting);
260 submit ("apache_scoreboard", "starting" , starting);
261 submit ("apache_scoreboard", "reading" , reading);
262 submit ("apache_scoreboard", "sending" , sending);
263 submit ("apache_scoreboard", "keepalive", keepalive);
264 submit ("apache_scoreboard", "dnslookup", dnslookup);
265 submit ("apache_scoreboard", "closing" , closing);
266 submit ("apache_scoreboard", "logging" , logging);
267 submit ("apache_scoreboard", "finishing", finishing);
268 submit ("apache_scoreboard", "idle_cleanup", idle_cleanup);
269 }
271 static void apache_read (void)
272 {
273 int i;
275 char *ptr;
276 char *lines[16];
277 int lines_num = 0;
279 char *fields[4];
280 int fields_num;
282 if (curl == NULL)
283 return;
284 if (url == NULL)
285 return;
287 apache_buffer_len = 0;
288 if (curl_easy_perform (curl) != 0)
289 {
290 syslog (LOG_WARNING, "apache: curl_easy_perform failed: %s", apache_curl_error);
291 return;
292 }
294 ptr = apache_buffer;
295 while ((lines[lines_num] = strtok (ptr, "\n\r")) != NULL)
296 {
297 ptr = NULL;
298 lines_num++;
300 if (lines_num >= 16)
301 break;
302 }
304 for (i = 0; i < lines_num; i++)
305 {
306 fields_num = strsplit (lines[i], fields, 4);
308 if (fields_num == 3)
309 {
310 if ((strcmp (fields[0], "Total") == 0)
311 && (strcmp (fields[1], "Accesses:") == 0))
312 submit ("apache_requests", NULL, atoll (fields[2]));
313 else if ((strcmp (fields[0], "Total") == 0)
314 && (strcmp (fields[1], "kBytes:") == 0))
315 submit ("apache_bytes", NULL, 1024LL * atoll (fields[2]));
316 }
317 else if (fields_num == 2)
318 {
319 if (strcmp (fields[0], "Scoreboard:") == 0)
320 submit_scoreboard (fields[1]);
321 }
322 }
324 apache_buffer_len = 0;
325 }
326 #else
327 # define apache_read NULL
328 #endif /* APACHE_HAVE_READ */
330 void module_register (void)
331 {
332 plugin_register (MODULE_NAME, init, apache_read, NULL);
333 plugin_register ("apache_requests", NULL, NULL, requests_write);
334 plugin_register ("apache_bytes", NULL, NULL, bytes_write);
335 plugin_register ("apache_scoreboard", NULL, NULL, scoreboard_write);
336 cf_register (MODULE_NAME, config, config_keys, config_keys_num);
337 }
339 #undef MODULE_NAME