2 * collectd - src/apache.c
3 * Copyright (C) 2006-2008 Florian octo Forster
4 * Copyright (C) 2007 Florent EppO Monbillard
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.
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.
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
20 * Florian octo Forster <octo at verplant.org>
21 * Florent EppO Monbillard <eppo at darox.net>
22 * - connections/lighttpd extension
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 #define ABUFFER_SIZE 16384
40 static char apache_buffer[ABUFFER_SIZE];
41 static int apache_buffer_len = 0;
42 static char apache_curl_error[CURL_ERROR_SIZE];
44 static const char *config_keys[] =
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, void *stream)
55 size_t len = size * nmemb;
57 if ((apache_buffer_len + len) >= ABUFFER_SIZE)
59 len = (ABUFFER_SIZE - 1) - apache_buffer_len;
65 memcpy (apache_buffer + apache_buffer_len, (char *) buf, len);
66 apache_buffer_len += len;
67 apache_buffer[apache_buffer_len] = '\0';
72 static int config_set (char **var, const char *value)
80 if ((*var = strdup (value)) == NULL)
86 static int config (const char *key, const char *value)
88 if (strcasecmp (key, "url") == 0)
89 return (config_set (&url, value));
90 else if (strcasecmp (key, "user") == 0)
91 return (config_set (&user, value));
92 else if (strcasecmp (key, "password") == 0)
93 return (config_set (&pass, value));
94 else if (strcasecmp (key, "cacert") == 0)
95 return (config_set (&cacert, value));
100 static int init (void)
102 static char credentials[1024];
106 WARNING ("apache plugin: init: No URL configured, returning "
113 curl_easy_cleanup (curl);
116 if ((curl = curl_easy_init ()) == NULL)
118 ERROR ("apache plugin: init: `curl_easy_init' failed.");
122 curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, apache_curl_callback);
123 curl_easy_setopt (curl, CURLOPT_USERAGENT, PACKAGE_NAME"/"PACKAGE_VERSION);
124 curl_easy_setopt (curl, CURLOPT_ERRORBUFFER, apache_curl_error);
130 status = snprintf (credentials, sizeof (credentials), "%s:%s",
131 user, (pass == NULL) ? "" : pass);
132 if (status >= sizeof (credentials))
134 ERROR ("apache plugin: init: Returning an error "
135 "because the credentials have been "
139 credentials[sizeof (credentials) - 1] = '\0';
141 curl_easy_setopt (curl, CURLOPT_USERPWD, credentials);
144 curl_easy_setopt (curl, CURLOPT_URL, url);
148 curl_easy_setopt (curl, CURLOPT_CAINFO, cacert);
154 static void submit_counter (const char *type, const char *type_instance,
158 value_list_t vl = VALUE_LIST_INIT;
160 values[0].counter = value;
164 vl.time = time (NULL);
165 strcpy (vl.host, hostname_g);
166 strcpy (vl.plugin, "apache");
167 strcpy (vl.plugin_instance, "");
169 if (type_instance != NULL)
171 strncpy (vl.type_instance, type_instance,
172 sizeof (vl.type_instance));
173 vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
176 plugin_dispatch_values (type, &vl);
177 } /* void submit_counter */
179 static void submit_gauge (const char *type, const char *type_instance,
183 value_list_t vl = VALUE_LIST_INIT;
185 values[0].gauge = value;
189 vl.time = time (NULL);
190 strcpy (vl.host, hostname_g);
191 strcpy (vl.plugin, "apache");
192 strcpy (vl.plugin_instance, "");
194 if (type_instance != NULL)
196 strncpy (vl.type_instance, type_instance,
197 sizeof (vl.type_instance));
198 vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
201 plugin_dispatch_values (type, &vl);
202 } /* void submit_counter */
204 static void submit_scoreboard (char *buf)
208 * "_" Waiting for Connection, "S" Starting up, "R" Reading Request,
209 * "W" Sending Reply, "K" Keepalive (read), "D" DNS Lookup,
210 * "C" Closing connection, "L" Logging, "G" Gracefully finishing,
211 * "I" Idle cleanup of worker, "." Open slot with no current process
213 long long open = 0LL;
214 long long waiting = 0LL;
215 long long starting = 0LL;
216 long long reading = 0LL;
217 long long sending = 0LL;
218 long long keepalive = 0LL;
219 long long dnslookup = 0LL;
220 long long closing = 0LL;
221 long long logging = 0LL;
222 long long finishing = 0LL;
223 long long idle_cleanup = 0LL;
227 for (i = 0; buf[i] != '\0'; i++)
229 if (buf[i] == '.') open++;
230 else if (buf[i] == '_') waiting++;
231 else if (buf[i] == 'S') starting++;
232 else if (buf[i] == 'R') reading++;
233 else if (buf[i] == 'W') sending++;
234 else if (buf[i] == 'K') keepalive++;
235 else if (buf[i] == 'D') dnslookup++;
236 else if (buf[i] == 'C') closing++;
237 else if (buf[i] == 'L') logging++;
238 else if (buf[i] == 'G') finishing++;
239 else if (buf[i] == 'I') idle_cleanup++;
242 submit_gauge ("apache_scoreboard", "open" , open);
243 submit_gauge ("apache_scoreboard", "waiting" , waiting);
244 submit_gauge ("apache_scoreboard", "starting" , starting);
245 submit_gauge ("apache_scoreboard", "reading" , reading);
246 submit_gauge ("apache_scoreboard", "sending" , sending);
247 submit_gauge ("apache_scoreboard", "keepalive", keepalive);
248 submit_gauge ("apache_scoreboard", "dnslookup", dnslookup);
249 submit_gauge ("apache_scoreboard", "closing" , closing);
250 submit_gauge ("apache_scoreboard", "logging" , logging);
251 submit_gauge ("apache_scoreboard", "finishing", finishing);
252 submit_gauge ("apache_scoreboard", "idle_cleanup", idle_cleanup);
255 static int apache_read (void)
272 apache_buffer_len = 0;
273 if (curl_easy_perform (curl) != 0)
275 ERROR ("apache: curl_easy_perform failed: %s",
282 while ((lines[lines_num] = strtok_r (ptr, "\n\r", &saveptr)) != NULL)
291 for (i = 0; i < lines_num; i++)
293 fields_num = strsplit (lines[i], fields, 4);
297 if ((strcmp (fields[0], "Total") == 0)
298 && (strcmp (fields[1], "Accesses:") == 0))
299 submit_counter ("apache_requests", "",
301 else if ((strcmp (fields[0], "Total") == 0)
302 && (strcmp (fields[1], "kBytes:") == 0))
303 submit_counter ("apache_bytes", "",
304 1024LL * atoll (fields[2]));
306 else if (fields_num == 2)
308 if (strcmp (fields[0], "Scoreboard:") == 0)
309 submit_scoreboard (fields[1]);
310 else if (strcmp (fields[0], "BusyServers:") == 0)
311 submit_gauge ("apache_connections", NULL, atol (fields[1]));
315 apache_buffer_len = 0;
318 } /* int apache_read */
320 void module_register (void)
322 plugin_register_config ("apache", config,
323 config_keys, config_keys_num);
324 plugin_register_init ("apache", init);
325 plugin_register_read ("apache", apache_read);
326 } /* void module_register */