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 #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
52 * theory. */
53 static char *bytes_file = "apache/apache_bytes.rrd";
54 static char *bytes_ds_def[] =
55 {
56 "DS:count:COUNTER:"COLLECTD_HEARTBEAT":0:134217728",
57 NULL
58 };
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[] =
64 {
65 "DS:count:COUNTER:"COLLECTD_HEARTBEAT":0:1048576",
66 NULL
67 };
68 static int requests_ds_num = 1;
70 static char *scoreboard_file = "apache/apache_scoreboard-%s.rrd";
71 static char *scoreboard_ds_def[] =
72 {
73 "DS:count:GAUGE:"COLLECTD_HEARTBEAT":0:U",
74 NULL
75 };
76 static int scoreboard_ds_num = 1;
78 static char *config_keys[] =
79 {
80 "URL",
81 "User",
82 "Password",
83 "CACert",
84 NULL
85 };
86 static int config_keys_num = 4;
88 #if HAVE_LIBCURL
89 static size_t apache_curl_callback (void *buf, size_t size, size_t nmemb, void *stream)
90 {
91 size_t len = size * nmemb;
93 if ((apache_buffer_len + len) >= ABUFFER_SIZE)
94 {
95 len = (ABUFFER_SIZE - 1) - apache_buffer_len;
96 }
98 if (len <= 0)
99 return (len);
101 memcpy (apache_buffer + apache_buffer_len, (char *) buf, len);
102 apache_buffer_len += len;
103 apache_buffer[apache_buffer_len] = '\0';
105 return (len);
106 }
107 #endif /* HAVE_LIBCURL */
109 static int config_set (char **var, char *value)
110 {
111 if (*var != NULL)
112 {
113 free (*var);
114 *var = NULL;
115 }
117 if ((*var = strdup (value)) == NULL)
118 return (1);
119 else
120 return (0);
121 }
123 static int config (char *key, char *value)
124 {
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));
133 else
134 return (-1);
135 }
137 static void init (void)
138 {
139 #if HAVE_LIBCURL
140 static char credentials[1024];
142 if (curl != NULL)
143 {
144 curl_easy_cleanup (curl);
145 }
147 if ((curl = curl_easy_init ()) == NULL)
148 {
149 syslog (LOG_ERR, "apache: `curl_easy_init' failed.");
150 return;
151 }
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);
157 if (user != NULL)
158 {
159 if (snprintf (credentials, 1024, "%s:%s", user, pass == NULL ? "" : pass) >= 1024)
160 {
161 syslog (LOG_ERR, "apache: Credentials would have been truncated.");
162 return;
163 }
165 curl_easy_setopt (curl, CURLOPT_USERPWD, credentials);
166 }
168 if (url != NULL)
169 {
170 curl_easy_setopt (curl, CURLOPT_URL, url);
171 }
173 if (cacert != NULL)
174 {
175 curl_easy_setopt (curl, CURLOPT_CAINFO, cacert);
176 }
177 #endif /* HAVE_LIBCURL */
178 }
180 static void bytes_write (char *host, char *inst, char *val)
181 {
182 rrd_update_file (host, bytes_file, val, bytes_ds_def, bytes_ds_num);
183 }
185 static void requests_write (char *host, char *inst, char *val)
186 {
187 rrd_update_file (host, requests_file, val, requests_ds_def, requests_ds_num);
188 }
190 static void scoreboard_write (char *host, char *inst, char *val)
191 {
192 char buf[1024];
194 if (snprintf (buf, 1024, scoreboard_file, inst) >= 1024)
195 return;
197 rrd_update_file (host, buf, val, scoreboard_ds_def, scoreboard_ds_num);
198 }
200 #if APACHE_HAVE_READ
201 static void submit (char *type, char *inst, long long value)
202 {
203 char buf[1024];
204 int status;
206 status = snprintf (buf, 1024, "%u:%lli", (unsigned int) curtime, value);
207 if (status < 0)
208 {
209 syslog (LOG_ERR, "apache: bytes_submit: snprintf failed");
210 return;
211 }
212 else if (status >= 1024)
213 {
214 syslog (LOG_WARNING, "apache: bytes_submit: snprintf was truncated");
215 return;
216 }
218 plugin_submit (type, inst, buf);
219 }
221 static void submit_scoreboard (char *buf)
222 {
223 /*
224 * Scoreboard Key:
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
229 */
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;
242 int i;
244 for (i = 0; buf[i] != '\0'; i++)
245 {
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++;
257 }
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);
270 }
272 static void apache_read (void)
273 {
274 int i;
276 char *ptr;
277 char *lines[16];
278 int lines_num = 0;
280 char *fields[4];
281 int fields_num;
283 if (curl == NULL)
284 return;
285 if (url == NULL)
286 return;
288 apache_buffer_len = 0;
289 if (curl_easy_perform (curl) != 0)
290 {
291 syslog (LOG_WARNING, "apache: curl_easy_perform failed: %s", apache_curl_error);
292 return;
293 }
295 ptr = apache_buffer;
296 while ((lines[lines_num] = strtok (ptr, "\n\r")) != NULL)
297 {
298 ptr = NULL;
299 lines_num++;
301 if (lines_num >= 16)
302 break;
303 }
305 for (i = 0; i < lines_num; i++)
306 {
307 fields_num = strsplit (lines[i], fields, 4);
309 if (fields_num == 3)
310 {
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]));
317 }
318 else if (fields_num == 2)
319 {
320 if (strcmp (fields[0], "Scoreboard:") == 0)
321 submit_scoreboard (fields[1]);
322 }
323 }
325 apache_buffer_len = 0;
326 }
327 #else
328 # define apache_read NULL
329 #endif /* APACHE_HAVE_READ */
331 void module_register (void)
332 {
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);
338 }
340 #undef MODULE_NAME