1 /**
2 * collectd - src/apache.c
3 * Copyright (C) 2006-2008 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"
30 #include <curl/curl.h>
32 static char *url = NULL;
33 static char *user = NULL;
34 static char *pass = NULL;
35 static char *cacert = NULL;
37 static CURL *curl = NULL;
39 static char *apache_buffer = NULL;
40 static size_t apache_buffer_size = 0;
41 static size_t apache_buffer_fill = 0;
42 static char apache_curl_error[CURL_ERROR_SIZE];
44 static const char *config_keys[] =
45 {
46 "URL",
47 "User",
48 "Password",
49 "CACert"
50 };
51 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
53 static size_t apache_curl_callback (void *buf, size_t size, size_t nmemb,
54 void *stream)
55 {
56 size_t len = size * nmemb;
58 if (len <= 0)
59 return (len);
61 if ((apache_buffer_fill + len) >= apache_buffer_size)
62 {
63 char *temp;
65 temp = (char *) realloc (apache_buffer,
66 apache_buffer_fill + len + 1);
67 if (temp == NULL)
68 {
69 ERROR ("apache plugin: realloc failed.");
70 return (0);
71 }
72 apache_buffer = temp;
73 apache_buffer_size = apache_buffer_fill + len + 1;
74 }
76 memcpy (apache_buffer + apache_buffer_fill, (char *) buf, len);
77 apache_buffer_fill += len;
78 apache_buffer[apache_buffer_fill] = 0;
80 return (len);
81 }
83 static int config_set (char **var, const char *value)
84 {
85 if (*var != NULL)
86 {
87 free (*var);
88 *var = NULL;
89 }
91 if ((*var = strdup (value)) == NULL)
92 return (1);
93 else
94 return (0);
95 }
97 static int config (const char *key, const char *value)
98 {
99 if (strcasecmp (key, "url") == 0)
100 return (config_set (&url, value));
101 else if (strcasecmp (key, "user") == 0)
102 return (config_set (&user, value));
103 else if (strcasecmp (key, "password") == 0)
104 return (config_set (&pass, value));
105 else if (strcasecmp (key, "cacert") == 0)
106 return (config_set (&cacert, value));
107 else
108 return (-1);
109 }
111 static int init (void)
112 {
113 static char credentials[1024];
115 if (url == NULL)
116 {
117 WARNING ("apache plugin: init: No URL configured, returning "
118 "an error.");
119 return (-1);
120 }
122 if (curl != NULL)
123 {
124 curl_easy_cleanup (curl);
125 }
127 if ((curl = curl_easy_init ()) == NULL)
128 {
129 ERROR ("apache plugin: init: `curl_easy_init' failed.");
130 return (-1);
131 }
133 curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, apache_curl_callback);
134 curl_easy_setopt (curl, CURLOPT_USERAGENT, PACKAGE_NAME"/"PACKAGE_VERSION);
135 curl_easy_setopt (curl, CURLOPT_ERRORBUFFER, apache_curl_error);
137 if (user != NULL)
138 {
139 int status;
141 status = snprintf (credentials, sizeof (credentials), "%s:%s",
142 user, (pass == NULL) ? "" : pass);
143 if (status >= sizeof (credentials))
144 {
145 ERROR ("apache plugin: init: Returning an error "
146 "because the credentials have been "
147 "truncated.");
148 return (-1);
149 }
150 credentials[sizeof (credentials) - 1] = '\0';
152 curl_easy_setopt (curl, CURLOPT_USERPWD, credentials);
153 }
155 curl_easy_setopt (curl, CURLOPT_URL, url);
157 if (cacert != NULL)
158 {
159 curl_easy_setopt (curl, CURLOPT_CAINFO, cacert);
160 }
162 return (0);
163 } /* int init */
165 static void submit_counter (const char *type, const char *type_instance,
166 counter_t value)
167 {
168 value_t values[1];
169 value_list_t vl = VALUE_LIST_INIT;
171 values[0].counter = value;
173 vl.values = values;
174 vl.values_len = 1;
175 vl.time = time (NULL);
176 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
177 sstrncpy (vl.plugin, "apache", sizeof (vl.plugin));
178 sstrncpy (vl.plugin_instance, "", sizeof (vl.plugin_instance));
180 if (type_instance != NULL)
181 {
182 strncpy (vl.type_instance, type_instance,
183 sizeof (vl.type_instance));
184 vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
185 }
187 plugin_dispatch_values (type, &vl);
188 } /* void submit_counter */
190 static void submit_gauge (const char *type, const char *type_instance,
191 gauge_t value)
192 {
193 value_t values[1];
194 value_list_t vl = VALUE_LIST_INIT;
196 values[0].gauge = value;
198 vl.values = values;
199 vl.values_len = 1;
200 vl.time = time (NULL);
201 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
202 sstrncpy (vl.plugin, "apache", sizeof (vl.plugin));
203 sstrncpy (vl.plugin_instance, "", sizeof (vl.plugin_instance));
205 if (type_instance != NULL)
206 {
207 strncpy (vl.type_instance, type_instance,
208 sizeof (vl.type_instance));
209 vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
210 }
212 plugin_dispatch_values (type, &vl);
213 } /* void submit_counter */
215 static void submit_scoreboard (char *buf)
216 {
217 /*
218 * Scoreboard Key:
219 * "_" Waiting for Connection, "S" Starting up, "R" Reading Request,
220 * "W" Sending Reply, "K" Keepalive (read), "D" DNS Lookup,
221 * "C" Closing connection, "L" Logging, "G" Gracefully finishing,
222 * "I" Idle cleanup of worker, "." Open slot with no current process
223 */
224 long long open = 0LL;
225 long long waiting = 0LL;
226 long long starting = 0LL;
227 long long reading = 0LL;
228 long long sending = 0LL;
229 long long keepalive = 0LL;
230 long long dnslookup = 0LL;
231 long long closing = 0LL;
232 long long logging = 0LL;
233 long long finishing = 0LL;
234 long long idle_cleanup = 0LL;
236 int i;
238 for (i = 0; buf[i] != '\0'; i++)
239 {
240 if (buf[i] == '.') open++;
241 else if (buf[i] == '_') waiting++;
242 else if (buf[i] == 'S') starting++;
243 else if (buf[i] == 'R') reading++;
244 else if (buf[i] == 'W') sending++;
245 else if (buf[i] == 'K') keepalive++;
246 else if (buf[i] == 'D') dnslookup++;
247 else if (buf[i] == 'C') closing++;
248 else if (buf[i] == 'L') logging++;
249 else if (buf[i] == 'G') finishing++;
250 else if (buf[i] == 'I') idle_cleanup++;
251 }
253 submit_gauge ("apache_scoreboard", "open" , open);
254 submit_gauge ("apache_scoreboard", "waiting" , waiting);
255 submit_gauge ("apache_scoreboard", "starting" , starting);
256 submit_gauge ("apache_scoreboard", "reading" , reading);
257 submit_gauge ("apache_scoreboard", "sending" , sending);
258 submit_gauge ("apache_scoreboard", "keepalive", keepalive);
259 submit_gauge ("apache_scoreboard", "dnslookup", dnslookup);
260 submit_gauge ("apache_scoreboard", "closing" , closing);
261 submit_gauge ("apache_scoreboard", "logging" , logging);
262 submit_gauge ("apache_scoreboard", "finishing", finishing);
263 submit_gauge ("apache_scoreboard", "idle_cleanup", idle_cleanup);
264 }
266 static int apache_read (void)
267 {
268 int i;
270 char *ptr;
271 char *saveptr;
272 char *lines[16];
273 int lines_num = 0;
275 char *fields[4];
276 int fields_num;
278 if (curl == NULL)
279 return (-1);
280 if (url == NULL)
281 return (-1);
283 apache_buffer_fill = 0;
284 if (curl_easy_perform (curl) != 0)
285 {
286 ERROR ("apache: curl_easy_perform failed: %s",
287 apache_curl_error);
288 return (-1);
289 }
291 ptr = apache_buffer;
292 saveptr = NULL;
293 while ((lines[lines_num] = strtok_r (ptr, "\n\r", &saveptr)) != NULL)
294 {
295 ptr = NULL;
296 lines_num++;
298 if (lines_num >= 16)
299 break;
300 }
302 for (i = 0; i < lines_num; i++)
303 {
304 fields_num = strsplit (lines[i], fields, 4);
306 if (fields_num == 3)
307 {
308 if ((strcmp (fields[0], "Total") == 0)
309 && (strcmp (fields[1], "Accesses:") == 0))
310 submit_counter ("apache_requests", "",
311 atoll (fields[2]));
312 else if ((strcmp (fields[0], "Total") == 0)
313 && (strcmp (fields[1], "kBytes:") == 0))
314 submit_counter ("apache_bytes", "",
315 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 else if (strcmp (fields[0], "BusyServers:") == 0)
322 submit_gauge ("apache_connections", NULL, atol (fields[1]));
323 }
324 }
326 apache_buffer_fill = 0;
328 return (0);
329 } /* int apache_read */
331 void module_register (void)
332 {
333 plugin_register_config ("apache", config,
334 config_keys, config_keys_num);
335 plugin_register_init ("apache", init);
336 plugin_register_read ("apache", apache_read);
337 } /* void module_register */