2 * collectd - src/apache.c
3 * Copyright (C) 2006,2007 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[] =
52 static int config_keys_num = 4;
54 static size_t apache_curl_callback (void *buf, size_t size, size_t nmemb, void *stream)
56 size_t len = size * nmemb;
58 if ((apache_buffer_len + len) >= ABUFFER_SIZE)
60 len = (ABUFFER_SIZE - 1) - apache_buffer_len;
66 memcpy (apache_buffer + apache_buffer_len, (char *) buf, len);
67 apache_buffer_len += len;
68 apache_buffer[apache_buffer_len] = '\0';
73 static int config_set (char **var, const char *value)
81 if ((*var = strdup (value)) == NULL)
87 static int config (const char *key, const char *value)
89 if (strcasecmp (key, "url") == 0)
90 return (config_set (&url, value));
91 else if (strcasecmp (key, "user") == 0)
92 return (config_set (&user, value));
93 else if (strcasecmp (key, "password") == 0)
94 return (config_set (&pass, value));
95 else if (strcasecmp (key, "cacert") == 0)
96 return (config_set (&cacert, value));
101 static int init (void)
103 static char credentials[1024];
107 curl_easy_cleanup (curl);
110 if ((curl = curl_easy_init ()) == NULL)
112 ERROR ("apache: `curl_easy_init' failed.");
116 curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, apache_curl_callback);
117 curl_easy_setopt (curl, CURLOPT_USERAGENT, PACKAGE_NAME"/"PACKAGE_VERSION);
118 curl_easy_setopt (curl, CURLOPT_ERRORBUFFER, apache_curl_error);
122 if (snprintf (credentials, 1024, "%s:%s", user, pass == NULL ? "" : pass) >= 1024)
124 ERROR ("apache: Credentials would have been truncated.");
128 curl_easy_setopt (curl, CURLOPT_USERPWD, credentials);
133 curl_easy_setopt (curl, CURLOPT_URL, url);
138 curl_easy_setopt (curl, CURLOPT_CAINFO, cacert);
144 static void submit_counter (const char *type, const char *type_instance,
145 unsigned long long value)
148 value_list_t vl = VALUE_LIST_INIT;
150 DEBUG ("type = %s; type_instance = %s; value = %llu;",
151 type, type_instance, value);
153 values[0].counter = value;
157 vl.time = time (NULL);
158 strcpy (vl.host, hostname_g);
159 strcpy (vl.plugin, "apache");
160 strcpy (vl.plugin_instance, "");
161 strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
163 plugin_dispatch_values (type, &vl);
164 } /* void submit_counter */
166 static void submit_gauge (const char *type, const char *type_instance,
170 value_list_t vl = VALUE_LIST_INIT;
172 DEBUG ("type = %s; type_instance = %s; value = %lf;",
173 type, type_instance, value);
175 values[0].gauge = value;
179 vl.time = time (NULL);
180 strcpy (vl.host, hostname_g);
181 strcpy (vl.plugin, "apache");
182 strcpy (vl.plugin_instance, "");
184 if (type_instance != NULL)
185 strncpy (vl.type_instance, type_instance,
186 sizeof (vl.type_instance));
188 plugin_dispatch_values (type, &vl);
189 } /* void submit_counter */
191 static void submit_scoreboard (char *buf)
195 * "_" Waiting for Connection, "S" Starting up, "R" Reading Request,
196 * "W" Sending Reply, "K" Keepalive (read), "D" DNS Lookup,
197 * "C" Closing connection, "L" Logging, "G" Gracefully finishing,
198 * "I" Idle cleanup of worker, "." Open slot with no current process
200 long long open = 0LL;
201 long long waiting = 0LL;
202 long long starting = 0LL;
203 long long reading = 0LL;
204 long long sending = 0LL;
205 long long keepalive = 0LL;
206 long long dnslookup = 0LL;
207 long long closing = 0LL;
208 long long logging = 0LL;
209 long long finishing = 0LL;
210 long long idle_cleanup = 0LL;
214 for (i = 0; buf[i] != '\0'; i++)
216 if (buf[i] == '.') open++;
217 else if (buf[i] == '_') waiting++;
218 else if (buf[i] == 'S') starting++;
219 else if (buf[i] == 'R') reading++;
220 else if (buf[i] == 'W') sending++;
221 else if (buf[i] == 'K') keepalive++;
222 else if (buf[i] == 'D') dnslookup++;
223 else if (buf[i] == 'C') closing++;
224 else if (buf[i] == 'L') logging++;
225 else if (buf[i] == 'G') finishing++;
226 else if (buf[i] == 'I') idle_cleanup++;
229 submit_gauge ("apache_scoreboard", "open" , open);
230 submit_gauge ("apache_scoreboard", "waiting" , waiting);
231 submit_gauge ("apache_scoreboard", "starting" , starting);
232 submit_gauge ("apache_scoreboard", "reading" , reading);
233 submit_gauge ("apache_scoreboard", "sending" , sending);
234 submit_gauge ("apache_scoreboard", "keepalive", keepalive);
235 submit_gauge ("apache_scoreboard", "dnslookup", dnslookup);
236 submit_gauge ("apache_scoreboard", "closing" , closing);
237 submit_gauge ("apache_scoreboard", "logging" , logging);
238 submit_gauge ("apache_scoreboard", "finishing", finishing);
239 submit_gauge ("apache_scoreboard", "idle_cleanup", idle_cleanup);
242 static int apache_read (void)
259 apache_buffer_len = 0;
260 if (curl_easy_perform (curl) != 0)
262 ERROR ("apache: curl_easy_perform failed: %s",
269 while ((lines[lines_num] = strtok_r (ptr, "\n\r", &saveptr)) != NULL)
278 for (i = 0; i < lines_num; i++)
280 fields_num = strsplit (lines[i], fields, 4);
284 if ((strcmp (fields[0], "Total") == 0)
285 && (strcmp (fields[1], "Accesses:") == 0))
286 submit_counter ("apache_requests", "",
288 else if ((strcmp (fields[0], "Total") == 0)
289 && (strcmp (fields[1], "kBytes:") == 0))
290 submit_counter ("apache_bytes", "",
291 1024LL * atoll (fields[2]));
293 else if (fields_num == 2)
295 if (strcmp (fields[0], "Scoreboard:") == 0)
296 submit_scoreboard (fields[1]);
297 else if (strcmp (fields[0], "BusyServers:") == 0)
298 submit_gauge ("apache_connections", NULL, atol (fields[1]));
302 apache_buffer_len = 0;
305 } /* int apache_read */
307 void module_register (void)
309 plugin_register_config ("apache", config,
310 config_keys, config_keys_num);
311 plugin_register_init ("apache", init);
312 plugin_register_read ("apache", apache_read);
313 } /* void module_register */