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;
41 #if HAVE_LIBCURL
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];
47 #endif /* HAVE_LIBCURL */
49 /* Limit to 2^27 bytes/s. That's what a gigabit-ethernet link can handle, in
50 * theory. */
51 static char *bytes_file = "apache/apache_bytes.rrd";
52 static char *bytes_ds_def[] =
53 {
54 "DS:count:COUNTER:"COLLECTD_HEARTBEAT":0:134217728",
55 NULL
56 };
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[] =
62 {
63 "DS:count:COUNTER:"COLLECTD_HEARTBEAT":0:1048576",
64 NULL
65 };
66 static int requests_ds_num = 1;
68 static char *scoreboard_file = "apache/apache_scoreboard-%s.rrd";
69 static char *scoreboard_ds_def[] =
70 {
71 "DS:count:GAUGE:"COLLECTD_HEARTBEAT":0:U",
72 NULL
73 };
74 static int scoreboard_ds_num = 1;
76 static char *config_keys[] =
77 {
78 "URL",
79 "User",
80 "Password",
81 NULL
82 };
83 static int config_keys_num = 3;
85 #if HAVE_LIBCURL
86 static size_t apache_curl_callback (void *buf, size_t size, size_t nmemb, void *stream)
87 {
88 size_t len = size * nmemb;
90 if ((apache_buffer_len + len) >= 4096)
91 {
92 len = 4095 - apache_buffer_len;
93 }
95 if (len <= 0)
96 return (len);
98 memcpy (apache_buffer + apache_buffer_len, (char *) buf, len);
99 apache_buffer_len += len;
100 apache_buffer[apache_buffer_len] = '\0';
102 return (len);
103 }
104 #endif /* HAVE_LIBCURL */
106 static int config_set (char **var, char *value)
107 {
108 if (*var != NULL)
109 {
110 free (*var);
111 *var = NULL;
112 }
114 if ((*var = strdup (value)) == NULL)
115 return (1);
116 else
117 return (0);
118 }
120 static int config (char *key, char *value)
121 {
122 if (strcasecmp (key, "url") == 0)
123 return (config_set (&url, value));
124 else if (strcasecmp (key, "user") == 0)
125 return (config_set (&user, value));
126 else if (strcasecmp (key, "password") == 0)
127 return (config_set (&pass, value));
128 else
129 return (-1);
130 }
132 static void init (void)
133 {
134 #if HAVE_LIBCURL
135 static char credentials[1024];
137 if (curl != NULL)
138 {
139 curl_easy_cleanup (curl);
140 }
142 if ((curl = curl_easy_init ()) == NULL)
143 {
144 syslog (LOG_ERR, "apache: `curl_easy_init' failed.");
145 return;
146 }
148 curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, apache_curl_callback);
149 curl_easy_setopt (curl, CURLOPT_USERAGENT, PACKAGE_NAME"/"PACKAGE_VERSION);
150 curl_easy_setopt (curl, CURLOPT_ERRORBUFFER, apache_curl_error);
152 if (user != NULL)
153 {
154 if (snprintf (credentials, 1024, "%s:%s", user, pass == NULL ? "" : pass) >= 1024)
155 {
156 syslog (LOG_ERR, "apache: Credentials would have been truncated.");
157 return;
158 }
160 curl_easy_setopt (curl, CURLOPT_USERPWD, credentials);
161 }
163 if (url != NULL)
164 {
165 curl_easy_setopt (curl, CURLOPT_URL, url);
166 }
167 #endif /* HAVE_LIBCURL */
168 }
170 static void bytes_write (char *host, char *inst, char *val)
171 {
172 rrd_update_file (host, bytes_file, val, bytes_ds_def, bytes_ds_num);
173 }
175 static void requests_write (char *host, char *inst, char *val)
176 {
177 rrd_update_file (host, requests_file, val, requests_ds_def, requests_ds_num);
178 }
180 static void scoreboard_write (char *host, char *inst, char *val)
181 {
182 char buf[1024];
184 if (snprintf (buf, 1024, scoreboard_file, inst) >= 1024)
185 return;
187 rrd_update_file (host, buf, val, scoreboard_ds_def, scoreboard_ds_num);
188 }
190 #if APACHE_HAVE_READ
191 static void submit (char *type, char *inst, long long value)
192 {
193 char buf[1024];
194 int status;
196 status = snprintf (buf, 1024, "%u:%lli", (unsigned int) curtime, value);
197 if (status < 0)
198 {
199 syslog (LOG_ERR, "apache: bytes_submit: snprintf failed");
200 return;
201 }
202 else if (status >= 1024)
203 {
204 syslog (LOG_WARNING, "apache: bytes_submit: snprintf was truncated");
205 return;
206 }
208 plugin_submit (type, inst, buf);
209 }
211 static void submit_scoreboard (char *buf)
212 {
213 /*
214 * Scoreboard Key:
215 * "_" Waiting for Connection, "S" Starting up, "R" Reading Request,
216 * "W" Sending Reply, "K" Keepalive (read), "D" DNS Lookup,
217 * "C" Closing connection, "L" Logging, "G" Gracefully finishing,
218 * "I" Idle cleanup of worker, "." Open slot with no current process
219 */
220 long long open = 0LL;
221 long long waiting = 0LL;
222 long long starting = 0LL;
223 long long reading = 0LL;
224 long long sending = 0LL;
225 long long keepalive = 0LL;
226 long long dnslookup = 0LL;
227 long long closing = 0LL;
228 long long logging = 0LL;
229 long long finishing = 0LL;
230 long long idle_cleanup = 0LL;
232 int i;
234 for (i = 0; buf[i] != '\0'; i++)
235 {
236 if (buf[i] == '.') open++;
237 else if (buf[i] == '_') waiting++;
238 else if (buf[i] == 'S') starting++;
239 else if (buf[i] == 'R') reading++;
240 else if (buf[i] == 'W') sending++;
241 else if (buf[i] == 'K') keepalive++;
242 else if (buf[i] == 'D') dnslookup++;
243 else if (buf[i] == 'C') closing++;
244 else if (buf[i] == 'L') logging++;
245 else if (buf[i] == 'G') finishing++;
246 else if (buf[i] == 'I') idle_cleanup++;
247 }
249 submit ("apache_scoreboard", "open" , open);
250 submit ("apache_scoreboard", "waiting" , waiting);
251 submit ("apache_scoreboard", "starting" , starting);
252 submit ("apache_scoreboard", "reading" , reading);
253 submit ("apache_scoreboard", "sending" , sending);
254 submit ("apache_scoreboard", "keepalive", keepalive);
255 submit ("apache_scoreboard", "dnslookup", dnslookup);
256 submit ("apache_scoreboard", "closing" , closing);
257 submit ("apache_scoreboard", "logging" , logging);
258 submit ("apache_scoreboard", "finishing", finishing);
259 submit ("apache_scoreboard", "idle_cleanup", idle_cleanup);
260 }
262 static void apache_read (void)
263 {
264 int i;
266 char *ptr;
267 char *lines[16];
268 int lines_num = 0;
270 char *fields[4];
271 int fields_num;
273 if (curl == NULL)
274 return;
275 if (url == NULL)
276 return;
278 if (curl_easy_perform (curl) != 0)
279 {
280 syslog (LOG_WARNING, "apache: curl_easy_perform failed: %s", apache_curl_error);
281 return;
282 }
284 ptr = apache_buffer;
285 while ((lines[lines_num] = strtok (ptr, "\n\r")) != NULL)
286 {
287 ptr = NULL;
288 lines_num++;
290 if (lines_num >= 16)
291 break;
292 }
294 for (i = 0; i < lines_num; i++)
295 {
296 fields_num = strsplit (lines[i], fields, 4);
298 if (fields_num == 3)
299 {
300 if ((strcmp (fields[0], "Total") == 0)
301 && (strcmp (fields[1], "Accesses:") == 0))
302 submit ("apache_requests", NULL, atoll (fields[2]));
303 else if ((strcmp (fields[0], "Total") == 0)
304 && (strcmp (fields[1], "kBytes:") == 0))
305 submit ("apache_bytes", NULL, 1024LL * atoll (fields[2]));
306 }
307 else if (fields_num == 2)
308 {
309 if (strcmp (fields[0], "Scoreboard:") == 0)
310 submit_scoreboard (fields[1]);
311 }
312 }
314 apache_buffer_len = 0;
315 }
316 #else
317 # define apache_read NULL
318 #endif /* APACHE_HAVE_READ */
320 void module_register (void)
321 {
322 plugin_register (MODULE_NAME, init, apache_read, NULL);
323 plugin_register ("apache_requests", NULL, NULL, requests_write);
324 plugin_register ("apache_bytes", NULL, NULL, bytes_write);
325 plugin_register ("apache_scoreboard", NULL, NULL, scoreboard_write);
326 cf_register (MODULE_NAME, config, config_keys, config_keys_num);
327 }
329 #undef MODULE_NAME