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; only version 2 of the License is applicable.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 *
18 * Authors:
19 * Florian octo Forster <octo at verplant.org>
20 **/
22 #include "collectd.h"
23 #include "common.h"
24 #include "plugin.h"
25 #include "configfile.h"
26 #include "utils_debug.h"
28 #if HAVE_LIBCURL && HAVE_CURL_CURL_H
29 # define APACHE_HAVE_READ 1
30 # include <curl/curl.h>
31 #else
32 # define APACHE_HAVE_READ 0
33 #endif
35 /* Limit to 2^27 bytes/s. That's what a gigabit-ethernet link can handle, in
36 * theory. */
37 static data_source_t apache_bytes_dsrc[1] =
38 {
39 {"count", DS_TYPE_COUNTER, 0, 134217728.0},
40 };
42 static data_set_t apache_bytes_ds =
43 {
44 "apache_bytes", 1, apache_bytes_dsrc
45 };
47 /* Limit to 2^20 requests/s */
48 static data_source_t apache_requests_dsrc[1] =
49 {
50 {"count", DS_TYPE_COUNTER, 0, 134217728.0},
51 };
53 static data_set_t apache_requests_ds =
54 {
55 "apache_requests", 1, apache_requests_dsrc
56 };
58 static data_source_t apache_scoreboard_dsrc[1] =
59 {
60 {"count", DS_TYPE_GAUGE, 0, 65535.0},
61 };
63 static data_set_t apache_scoreboard_ds =
64 {
65 "apache_scoreboard", 1, apache_scoreboard_dsrc
66 };
68 #if APACHE_HAVE_READ
69 static char *url = NULL;
70 static char *user = NULL;
71 static char *pass = NULL;
72 static char *cacert = NULL;
74 static CURL *curl = NULL;
76 #define ABUFFER_SIZE 16384
77 static char apache_buffer[ABUFFER_SIZE];
78 static int apache_buffer_len = 0;
79 static char apache_curl_error[CURL_ERROR_SIZE];
81 static const char *config_keys[] =
82 {
83 "URL",
84 "User",
85 "Password",
86 "CACert",
87 NULL
88 };
89 static int config_keys_num = 4;
91 static size_t apache_curl_callback (void *buf, size_t size, size_t nmemb, void *stream)
92 {
93 size_t len = size * nmemb;
95 if ((apache_buffer_len + len) >= ABUFFER_SIZE)
96 {
97 len = (ABUFFER_SIZE - 1) - apache_buffer_len;
98 }
100 if (len <= 0)
101 return (len);
103 memcpy (apache_buffer + apache_buffer_len, (char *) buf, len);
104 apache_buffer_len += len;
105 apache_buffer[apache_buffer_len] = '\0';
107 return (len);
108 }
110 static int config_set (char **var, const char *value)
111 {
112 if (*var != NULL)
113 {
114 free (*var);
115 *var = NULL;
116 }
118 if ((*var = strdup (value)) == NULL)
119 return (1);
120 else
121 return (0);
122 }
124 static int config (const char *key, const char *value)
125 {
126 if (strcasecmp (key, "url") == 0)
127 return (config_set (&url, value));
128 else if (strcasecmp (key, "user") == 0)
129 return (config_set (&user, value));
130 else if (strcasecmp (key, "password") == 0)
131 return (config_set (&pass, value));
132 else if (strcasecmp (key, "cacert") == 0)
133 return (config_set (&cacert, value));
134 else
135 return (-1);
136 }
138 static int init (void)
139 {
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 (-1);
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 (-1);
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 }
178 return (0);
179 } /* int init */
181 static void submit_counter (const char *type, const char *type_instance,
182 unsigned long long value)
183 {
184 value_t values[1];
185 value_list_t vl = VALUE_LIST_INIT;
187 DBG ("type = %s; type_instance = %s; value = %llu;",
188 type, type_instance, value);
190 values[0].counter = value;
192 vl.values = values;
193 vl.values_len = 1;
194 vl.time = time (NULL);
195 strcpy (vl.host, hostname);
196 strcpy (vl.plugin, "apache");
197 strcpy (vl.plugin_instance, "");
198 strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
200 plugin_dispatch_values (type, &vl);
201 } /* void submit_counter */
203 static void submit_gauge (const char *type, const char *type_instance,
204 double value)
205 {
206 value_t values[1];
207 value_list_t vl = VALUE_LIST_INIT;
209 DBG ("type = %s; type_instance = %s; value = %lf;",
210 type, type_instance, value);
212 values[0].gauge = value;
214 vl.values = values;
215 vl.values_len = 1;
216 vl.time = time (NULL);
217 strcpy (vl.host, hostname);
218 strcpy (vl.plugin, "apache");
219 strcpy (vl.plugin_instance, "");
220 strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
222 plugin_dispatch_values (type, &vl);
223 } /* void submit_counter */
225 static void submit_scoreboard (char *buf)
226 {
227 /*
228 * Scoreboard Key:
229 * "_" Waiting for Connection, "S" Starting up, "R" Reading Request,
230 * "W" Sending Reply, "K" Keepalive (read), "D" DNS Lookup,
231 * "C" Closing connection, "L" Logging, "G" Gracefully finishing,
232 * "I" Idle cleanup of worker, "." Open slot with no current process
233 */
234 long long open = 0LL;
235 long long waiting = 0LL;
236 long long starting = 0LL;
237 long long reading = 0LL;
238 long long sending = 0LL;
239 long long keepalive = 0LL;
240 long long dnslookup = 0LL;
241 long long closing = 0LL;
242 long long logging = 0LL;
243 long long finishing = 0LL;
244 long long idle_cleanup = 0LL;
246 int i;
248 for (i = 0; buf[i] != '\0'; i++)
249 {
250 if (buf[i] == '.') open++;
251 else if (buf[i] == '_') waiting++;
252 else if (buf[i] == 'S') starting++;
253 else if (buf[i] == 'R') reading++;
254 else if (buf[i] == 'W') sending++;
255 else if (buf[i] == 'K') keepalive++;
256 else if (buf[i] == 'D') dnslookup++;
257 else if (buf[i] == 'C') closing++;
258 else if (buf[i] == 'L') logging++;
259 else if (buf[i] == 'G') finishing++;
260 else if (buf[i] == 'I') idle_cleanup++;
261 }
263 submit_gauge ("apache_scoreboard", "open" , open);
264 submit_gauge ("apache_scoreboard", "waiting" , waiting);
265 submit_gauge ("apache_scoreboard", "starting" , starting);
266 submit_gauge ("apache_scoreboard", "reading" , reading);
267 submit_gauge ("apache_scoreboard", "sending" , sending);
268 submit_gauge ("apache_scoreboard", "keepalive", keepalive);
269 submit_gauge ("apache_scoreboard", "dnslookup", dnslookup);
270 submit_gauge ("apache_scoreboard", "closing" , closing);
271 submit_gauge ("apache_scoreboard", "logging" , logging);
272 submit_gauge ("apache_scoreboard", "finishing", finishing);
273 submit_gauge ("apache_scoreboard", "idle_cleanup", idle_cleanup);
274 }
276 static int apache_read (void)
277 {
278 int i;
280 char *ptr;
281 char *lines[16];
282 int lines_num = 0;
284 char *fields[4];
285 int fields_num;
287 if (curl == NULL)
288 return (-1);
289 if (url == NULL)
290 return (-1);
292 apache_buffer_len = 0;
293 if (curl_easy_perform (curl) != 0)
294 {
295 syslog (LOG_ERR, "apache: curl_easy_perform failed: %s",
296 apache_curl_error);
297 return (-1);
298 }
300 ptr = apache_buffer;
301 while ((lines[lines_num] = strtok (ptr, "\n\r")) != NULL)
302 {
303 ptr = NULL;
304 lines_num++;
306 if (lines_num >= 16)
307 break;
308 }
310 for (i = 0; i < lines_num; i++)
311 {
312 fields_num = strsplit (lines[i], fields, 4);
314 if (fields_num == 3)
315 {
316 if ((strcmp (fields[0], "Total") == 0)
317 && (strcmp (fields[1], "Accesses:") == 0))
318 submit_counter ("apache_requests", "",
319 atoll (fields[2]));
320 else if ((strcmp (fields[0], "Total") == 0)
321 && (strcmp (fields[1], "kBytes:") == 0))
322 submit_counter ("apache_bytes", "",
323 1024LL * atoll (fields[2]));
324 }
325 else if (fields_num == 2)
326 {
327 if (strcmp (fields[0], "Scoreboard:") == 0)
328 submit_scoreboard (fields[1]);
329 }
330 }
332 apache_buffer_len = 0;
334 return (0);
335 } /* int apache_read */
336 #endif /* APACHE_HAVE_READ */
338 void module_register (void)
339 {
340 plugin_register_data_set (&apache_bytes_ds);
341 plugin_register_data_set (&apache_requests_ds);
342 plugin_register_data_set (&apache_scoreboard_ds);
344 #if APACHE_HAVE_READ
345 plugin_register_config ("apache", config,
346 config_keys, config_keys_num);
347 plugin_register_init ("apache", init);
348 plugin_register_read ("apache", apache_read);
349 #endif
350 }