1 /**
2 * collectd - src/apache.c
3 * Copyright (C) 2006 Florian octo Forster
4 * Copyright (C) 2007 Florent EppO Monbillard
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; only version 2 of the License is applicable.
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 * Florent EppO Monbillard <eppo at darox.net>
22 * - connections/lighttpd extension
23 **/
25 #include "collectd.h"
26 #include "common.h"
27 #include "plugin.h"
28 #include "configfile.h"
29 #include "utils_debug.h"
31 #if HAVE_LIBCURL && HAVE_CURL_CURL_H
32 # define APACHE_HAVE_READ 1
33 # include <curl/curl.h>
34 #else
35 # define APACHE_HAVE_READ 0
36 #endif
38 /* Limit to 2^27 bytes/s. That's what a gigabit-ethernet link can handle, in
39 * theory. */
40 static data_source_t apache_bytes_dsrc[1] =
41 {
42 {"count", DS_TYPE_COUNTER, 0, 134217728.0},
43 };
45 static data_set_t apache_bytes_ds =
46 {
47 "apache_bytes", 1, apache_bytes_dsrc
48 };
50 /* Limit to 2^20 requests/s */
51 static data_source_t apache_requests_dsrc[1] =
52 {
53 {"count", DS_TYPE_COUNTER, 0, 134217728.0},
54 };
56 static data_set_t apache_requests_ds =
57 {
58 "apache_requests", 1, apache_requests_dsrc
59 };
61 static data_source_t apache_scoreboard_dsrc[1] =
62 {
63 {"count", DS_TYPE_GAUGE, 0, 65535.0},
64 };
66 static data_set_t apache_scoreboard_ds =
67 {
68 "apache_scoreboard", 1, apache_scoreboard_dsrc
69 };
71 static data_source_t apache_connections_dsrc[1] =
72 {
73 {"count", DS_TYPE_GAUGE, 0, 65535.0},
74 };
76 static data_set_t apache_connections_ds =
77 {
78 "apache_connections", 1, apache_connections_dsrc
79 };
81 #if APACHE_HAVE_READ
82 static char *url = NULL;
83 static char *user = NULL;
84 static char *pass = NULL;
85 static char *cacert = NULL;
87 static CURL *curl = NULL;
89 #define ABUFFER_SIZE 16384
90 static char apache_buffer[ABUFFER_SIZE];
91 static int apache_buffer_len = 0;
92 static char apache_curl_error[CURL_ERROR_SIZE];
94 static const char *config_keys[] =
95 {
96 "URL",
97 "User",
98 "Password",
99 "CACert",
100 NULL
101 };
102 static int config_keys_num = 4;
104 static size_t apache_curl_callback (void *buf, size_t size, size_t nmemb, void *stream)
105 {
106 size_t len = size * nmemb;
108 if ((apache_buffer_len + len) >= ABUFFER_SIZE)
109 {
110 len = (ABUFFER_SIZE - 1) - apache_buffer_len;
111 }
113 if (len <= 0)
114 return (len);
116 memcpy (apache_buffer + apache_buffer_len, (char *) buf, len);
117 apache_buffer_len += len;
118 apache_buffer[apache_buffer_len] = '\0';
120 return (len);
121 }
123 static int config_set (char **var, const char *value)
124 {
125 if (*var != NULL)
126 {
127 free (*var);
128 *var = NULL;
129 }
131 if ((*var = strdup (value)) == NULL)
132 return (1);
133 else
134 return (0);
135 }
137 static int config (const char *key, const char *value)
138 {
139 if (strcasecmp (key, "url") == 0)
140 return (config_set (&url, value));
141 else if (strcasecmp (key, "user") == 0)
142 return (config_set (&user, value));
143 else if (strcasecmp (key, "password") == 0)
144 return (config_set (&pass, value));
145 else if (strcasecmp (key, "cacert") == 0)
146 return (config_set (&cacert, value));
147 else
148 return (-1);
149 }
151 static int init (void)
152 {
153 static char credentials[1024];
155 if (curl != NULL)
156 {
157 curl_easy_cleanup (curl);
158 }
160 if ((curl = curl_easy_init ()) == NULL)
161 {
162 syslog (LOG_ERR, "apache: `curl_easy_init' failed.");
163 return (-1);
164 }
166 curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, apache_curl_callback);
167 curl_easy_setopt (curl, CURLOPT_USERAGENT, PACKAGE_NAME"/"PACKAGE_VERSION);
168 curl_easy_setopt (curl, CURLOPT_ERRORBUFFER, apache_curl_error);
170 if (user != NULL)
171 {
172 if (snprintf (credentials, 1024, "%s:%s", user, pass == NULL ? "" : pass) >= 1024)
173 {
174 syslog (LOG_ERR, "apache: Credentials would have been truncated.");
175 return (-1);
176 }
178 curl_easy_setopt (curl, CURLOPT_USERPWD, credentials);
179 }
181 if (url != NULL)
182 {
183 curl_easy_setopt (curl, CURLOPT_URL, url);
184 }
186 if (cacert != NULL)
187 {
188 curl_easy_setopt (curl, CURLOPT_CAINFO, cacert);
189 }
191 return (0);
192 } /* int init */
194 static void submit_counter (const char *type, const char *type_instance,
195 unsigned long long value)
196 {
197 value_t values[1];
198 value_list_t vl = VALUE_LIST_INIT;
200 DBG ("type = %s; type_instance = %s; value = %llu;",
201 type, type_instance, value);
203 values[0].counter = value;
205 vl.values = values;
206 vl.values_len = 1;
207 vl.time = time (NULL);
208 strcpy (vl.host, hostname);
209 strcpy (vl.plugin, "apache");
210 strcpy (vl.plugin_instance, "");
211 strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
213 plugin_dispatch_values (type, &vl);
214 } /* void submit_counter */
216 static void submit_gauge (const char *type, const char *type_instance,
217 double value)
218 {
219 value_t values[1];
220 value_list_t vl = VALUE_LIST_INIT;
222 DBG ("type = %s; type_instance = %s; value = %lf;",
223 type, type_instance, value);
225 values[0].gauge = value;
227 vl.values = values;
228 vl.values_len = 1;
229 vl.time = time (NULL);
230 strcpy (vl.host, hostname);
231 strcpy (vl.plugin, "apache");
232 strcpy (vl.plugin_instance, "");
234 if (type_instance != NULL)
235 strncpy (vl.type_instance, type_instance,
236 sizeof (vl.type_instance));
238 plugin_dispatch_values (type, &vl);
239 } /* void submit_counter */
241 static void submit_scoreboard (char *buf)
242 {
243 /*
244 * Scoreboard Key:
245 * "_" Waiting for Connection, "S" Starting up, "R" Reading Request,
246 * "W" Sending Reply, "K" Keepalive (read), "D" DNS Lookup,
247 * "C" Closing connection, "L" Logging, "G" Gracefully finishing,
248 * "I" Idle cleanup of worker, "." Open slot with no current process
249 */
250 long long open = 0LL;
251 long long waiting = 0LL;
252 long long starting = 0LL;
253 long long reading = 0LL;
254 long long sending = 0LL;
255 long long keepalive = 0LL;
256 long long dnslookup = 0LL;
257 long long closing = 0LL;
258 long long logging = 0LL;
259 long long finishing = 0LL;
260 long long idle_cleanup = 0LL;
262 int i;
264 for (i = 0; buf[i] != '\0'; i++)
265 {
266 if (buf[i] == '.') open++;
267 else if (buf[i] == '_') waiting++;
268 else if (buf[i] == 'S') starting++;
269 else if (buf[i] == 'R') reading++;
270 else if (buf[i] == 'W') sending++;
271 else if (buf[i] == 'K') keepalive++;
272 else if (buf[i] == 'D') dnslookup++;
273 else if (buf[i] == 'C') closing++;
274 else if (buf[i] == 'L') logging++;
275 else if (buf[i] == 'G') finishing++;
276 else if (buf[i] == 'I') idle_cleanup++;
277 }
279 submit_gauge ("apache_scoreboard", "open" , open);
280 submit_gauge ("apache_scoreboard", "waiting" , waiting);
281 submit_gauge ("apache_scoreboard", "starting" , starting);
282 submit_gauge ("apache_scoreboard", "reading" , reading);
283 submit_gauge ("apache_scoreboard", "sending" , sending);
284 submit_gauge ("apache_scoreboard", "keepalive", keepalive);
285 submit_gauge ("apache_scoreboard", "dnslookup", dnslookup);
286 submit_gauge ("apache_scoreboard", "closing" , closing);
287 submit_gauge ("apache_scoreboard", "logging" , logging);
288 submit_gauge ("apache_scoreboard", "finishing", finishing);
289 submit_gauge ("apache_scoreboard", "idle_cleanup", idle_cleanup);
290 }
292 static int apache_read (void)
293 {
294 int i;
296 char *ptr;
297 char *lines[16];
298 int lines_num = 0;
300 char *fields[4];
301 int fields_num;
303 if (curl == NULL)
304 return (-1);
305 if (url == NULL)
306 return (-1);
308 apache_buffer_len = 0;
309 if (curl_easy_perform (curl) != 0)
310 {
311 syslog (LOG_ERR, "apache: curl_easy_perform failed: %s",
312 apache_curl_error);
313 return (-1);
314 }
316 ptr = apache_buffer;
317 while ((lines[lines_num] = strtok (ptr, "\n\r")) != NULL)
318 {
319 ptr = NULL;
320 lines_num++;
322 if (lines_num >= 16)
323 break;
324 }
326 for (i = 0; i < lines_num; i++)
327 {
328 fields_num = strsplit (lines[i], fields, 4);
330 if (fields_num == 3)
331 {
332 if ((strcmp (fields[0], "Total") == 0)
333 && (strcmp (fields[1], "Accesses:") == 0))
334 submit_counter ("apache_requests", "",
335 atoll (fields[2]));
336 else if ((strcmp (fields[0], "Total") == 0)
337 && (strcmp (fields[1], "kBytes:") == 0))
338 submit_counter ("apache_bytes", "",
339 1024LL * atoll (fields[2]));
340 }
341 else if (fields_num == 2)
342 {
343 if (strcmp (fields[0], "Scoreboard:") == 0)
344 submit_scoreboard (fields[1]);
345 else if (strcmp (fields[0], "BusyServers:") == 0)
346 submit_gauge ("apache_connections", NULL, atol (fields[1]));
347 }
348 }
350 apache_buffer_len = 0;
352 return (0);
353 } /* int apache_read */
354 #endif /* APACHE_HAVE_READ */
356 void module_register (void)
357 {
358 plugin_register_data_set (&apache_bytes_ds);
359 plugin_register_data_set (&apache_requests_ds);
360 plugin_register_data_set (&apache_scoreboard_ds);
361 plugin_register_data_set (&apache_connections_ds);
363 #if APACHE_HAVE_READ
364 plugin_register_config ("apache", config,
365 config_keys, config_keys_num);
366 plugin_register_init ("apache", init);
367 plugin_register_read ("apache", apache_read);
368 #endif
369 }