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 *verify_peer = NULL;
36 static char *verify_host = NULL;
37 static char *cacert = NULL;
39 static CURL *curl = NULL;
41 static char *apache_buffer = NULL;
42 static size_t apache_buffer_size = 0;
43 static size_t apache_buffer_fill = 0;
44 static char apache_curl_error[CURL_ERROR_SIZE];
46 static const char *config_keys[] =
47 {
48 "URL",
49 "User",
50 "Password",
51 "VerifyPeer",
52 "VerifyHost",
53 "CACert"
54 };
55 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
57 static size_t apache_curl_callback (void *buf, size_t size, size_t nmemb,
58 void *stream)
59 {
60 size_t len = size * nmemb;
62 if (len <= 0)
63 return (len);
65 if ((apache_buffer_fill + len) >= apache_buffer_size)
66 {
67 char *temp;
69 temp = (char *) realloc (apache_buffer,
70 apache_buffer_fill + len + 1);
71 if (temp == NULL)
72 {
73 ERROR ("apache plugin: realloc failed.");
74 return (0);
75 }
76 apache_buffer = temp;
77 apache_buffer_size = apache_buffer_fill + len + 1;
78 }
80 memcpy (apache_buffer + apache_buffer_fill, (char *) buf, len);
81 apache_buffer_fill += len;
82 apache_buffer[apache_buffer_fill] = 0;
84 return (len);
85 }
87 static int config_set (char **var, const char *value)
88 {
89 if (*var != NULL)
90 {
91 free (*var);
92 *var = NULL;
93 }
95 if ((*var = strdup (value)) == NULL)
96 return (1);
97 else
98 return (0);
99 }
101 static int config (const char *key, const char *value)
102 {
103 if (strcasecmp (key, "url") == 0)
104 return (config_set (&url, value));
105 else if (strcasecmp (key, "user") == 0)
106 return (config_set (&user, value));
107 else if (strcasecmp (key, "password") == 0)
108 return (config_set (&pass, value));
109 else if (strcasecmp (key, "verifypeer") == 0)
110 return (config_set (&verify_peer, value));
111 else if (strcasecmp (key, "verifyhost") == 0)
112 return (config_set (&verify_host, value));
113 else if (strcasecmp (key, "cacert") == 0)
114 return (config_set (&cacert, value));
115 else
116 return (-1);
117 }
119 static int init (void)
120 {
121 static char credentials[1024];
123 if (url == NULL)
124 {
125 WARNING ("apache plugin: init: No URL configured, returning "
126 "an error.");
127 return (-1);
128 }
130 if (curl != NULL)
131 {
132 curl_easy_cleanup (curl);
133 }
135 if ((curl = curl_easy_init ()) == NULL)
136 {
137 ERROR ("apache plugin: init: `curl_easy_init' failed.");
138 return (-1);
139 }
141 curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, apache_curl_callback);
142 curl_easy_setopt (curl, CURLOPT_USERAGENT, PACKAGE_NAME"/"PACKAGE_VERSION);
143 curl_easy_setopt (curl, CURLOPT_ERRORBUFFER, apache_curl_error);
145 if (user != NULL)
146 {
147 int status;
149 status = snprintf (credentials, sizeof (credentials), "%s:%s",
150 user, (pass == NULL) ? "" : pass);
151 if (status >= sizeof (credentials))
152 {
153 ERROR ("apache plugin: init: Returning an error "
154 "because the credentials have been "
155 "truncated.");
156 return (-1);
157 }
158 credentials[sizeof (credentials) - 1] = '\0';
160 curl_easy_setopt (curl, CURLOPT_USERPWD, credentials);
161 }
163 curl_easy_setopt (curl, CURLOPT_URL, url);
165 if ((verify_peer == NULL) || (strcmp (verify_peer, "true") == 0))
166 {
167 curl_easy_setopt (curl, CURLOPT_SSL_VERIFYPEER, 1);
168 }
169 else
170 {
171 curl_easy_setopt (curl, CURLOPT_SSL_VERIFYPEER, 0);
172 }
174 if ((verify_host == NULL) || (strcmp (verify_host, "true") == 0))
175 {
176 curl_easy_setopt (curl, CURLOPT_SSL_VERIFYHOST, 2);
177 }
178 else
179 {
180 curl_easy_setopt (curl, CURLOPT_SSL_VERIFYHOST, 0);
181 }
183 if (cacert != NULL)
184 {
185 curl_easy_setopt (curl, CURLOPT_CAINFO, cacert);
186 }
188 return (0);
189 } /* int init */
191 static void submit_counter (const char *type, const char *type_instance,
192 counter_t value)
193 {
194 value_t values[1];
195 value_list_t vl = VALUE_LIST_INIT;
197 values[0].counter = value;
199 vl.values = values;
200 vl.values_len = 1;
201 vl.time = time (NULL);
202 strcpy (vl.host, hostname_g);
203 strcpy (vl.plugin, "apache");
204 strcpy (vl.plugin_instance, "");
205 strncpy (vl.type, type, sizeof (vl.type));
207 if (type_instance != NULL)
208 {
209 strncpy (vl.type_instance, type_instance,
210 sizeof (vl.type_instance));
211 vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
212 }
214 plugin_dispatch_values (&vl);
215 } /* void submit_counter */
217 static void submit_gauge (const char *type, const char *type_instance,
218 gauge_t value)
219 {
220 value_t values[1];
221 value_list_t vl = VALUE_LIST_INIT;
223 values[0].gauge = value;
225 vl.values = values;
226 vl.values_len = 1;
227 vl.time = time (NULL);
228 strcpy (vl.host, hostname_g);
229 strcpy (vl.plugin, "apache");
230 strcpy (vl.plugin_instance, "");
231 strncpy (vl.type, type, sizeof (vl.type));
233 if (type_instance != NULL)
234 {
235 strncpy (vl.type_instance, type_instance,
236 sizeof (vl.type_instance));
237 vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
238 }
240 plugin_dispatch_values (&vl);
241 } /* void submit_counter */
243 static void submit_scoreboard (char *buf)
244 {
245 /*
246 * Scoreboard Key:
247 * "_" Waiting for Connection, "S" Starting up, "R" Reading Request,
248 * "W" Sending Reply, "K" Keepalive (read), "D" DNS Lookup,
249 * "C" Closing connection, "L" Logging, "G" Gracefully finishing,
250 * "I" Idle cleanup of worker, "." Open slot with no current process
251 */
252 long long open = 0LL;
253 long long waiting = 0LL;
254 long long starting = 0LL;
255 long long reading = 0LL;
256 long long sending = 0LL;
257 long long keepalive = 0LL;
258 long long dnslookup = 0LL;
259 long long closing = 0LL;
260 long long logging = 0LL;
261 long long finishing = 0LL;
262 long long idle_cleanup = 0LL;
264 int i;
266 for (i = 0; buf[i] != '\0'; i++)
267 {
268 if (buf[i] == '.') open++;
269 else if (buf[i] == '_') waiting++;
270 else if (buf[i] == 'S') starting++;
271 else if (buf[i] == 'R') reading++;
272 else if (buf[i] == 'W') sending++;
273 else if (buf[i] == 'K') keepalive++;
274 else if (buf[i] == 'D') dnslookup++;
275 else if (buf[i] == 'C') closing++;
276 else if (buf[i] == 'L') logging++;
277 else if (buf[i] == 'G') finishing++;
278 else if (buf[i] == 'I') idle_cleanup++;
279 }
281 submit_gauge ("apache_scoreboard", "open" , open);
282 submit_gauge ("apache_scoreboard", "waiting" , waiting);
283 submit_gauge ("apache_scoreboard", "starting" , starting);
284 submit_gauge ("apache_scoreboard", "reading" , reading);
285 submit_gauge ("apache_scoreboard", "sending" , sending);
286 submit_gauge ("apache_scoreboard", "keepalive", keepalive);
287 submit_gauge ("apache_scoreboard", "dnslookup", dnslookup);
288 submit_gauge ("apache_scoreboard", "closing" , closing);
289 submit_gauge ("apache_scoreboard", "logging" , logging);
290 submit_gauge ("apache_scoreboard", "finishing", finishing);
291 submit_gauge ("apache_scoreboard", "idle_cleanup", idle_cleanup);
292 }
294 static int apache_read (void)
295 {
296 int i;
298 char *ptr;
299 char *saveptr;
300 char *lines[16];
301 int lines_num = 0;
303 char *fields[4];
304 int fields_num;
306 if (curl == NULL)
307 return (-1);
308 if (url == NULL)
309 return (-1);
311 apache_buffer_fill = 0;
312 if (curl_easy_perform (curl) != 0)
313 {
314 ERROR ("apache: curl_easy_perform failed: %s",
315 apache_curl_error);
316 return (-1);
317 }
319 ptr = apache_buffer;
320 saveptr = NULL;
321 while ((lines[lines_num] = strtok_r (ptr, "\n\r", &saveptr)) != NULL)
322 {
323 ptr = NULL;
324 lines_num++;
326 if (lines_num >= 16)
327 break;
328 }
330 for (i = 0; i < lines_num; i++)
331 {
332 fields_num = strsplit (lines[i], fields, 4);
334 if (fields_num == 3)
335 {
336 if ((strcmp (fields[0], "Total") == 0)
337 && (strcmp (fields[1], "Accesses:") == 0))
338 submit_counter ("apache_requests", "",
339 atoll (fields[2]));
340 else if ((strcmp (fields[0], "Total") == 0)
341 && (strcmp (fields[1], "kBytes:") == 0))
342 submit_counter ("apache_bytes", "",
343 1024LL * atoll (fields[2]));
344 }
345 else if (fields_num == 2)
346 {
347 if (strcmp (fields[0], "Scoreboard:") == 0)
348 submit_scoreboard (fields[1]);
349 else if (strcmp (fields[0], "BusyServers:") == 0)
350 submit_gauge ("apache_connections", NULL, atol (fields[1]));
351 }
352 }
354 apache_buffer_fill = 0;
356 return (0);
357 } /* int apache_read */
359 void module_register (void)
360 {
361 plugin_register_config ("apache", config,
362 config_keys, config_keys_num);
363 plugin_register_init ("apache", init);
364 plugin_register_read ("apache", apache_read);
365 } /* void module_register */