e27350d382b922c5a4b929fe068dff062e7d673f
1 /**
2 * collectd - src/apache.c
3 * Copyright (C) 2006 Florian octo Forster
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version.
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 **/
23 #include "collectd.h"
24 #include "common.h"
25 #include "plugin.h"
26 #include "configfile.h"
28 #define MODULE_NAME "apache"
30 #if HAVE_LIBCURL && HAVE_CURL_CURL_H
31 # define APACHE_HAVE_READ 1
32 # include <curl/curl.h>
33 #else
34 # define APACHE_HAVE_READ 0
35 #endif
37 static char *url = NULL;
38 static char *user = NULL;
39 static char *pass = NULL;
41 #if APACHE_HAVE_READ
42 static CURL *curl = NULL;
44 static char apache_buffer[4096];
45 static int apache_buffer_len = 0;
46 static char apache_curl_error[CURL_ERROR_SIZE];
47 #endif
49 static char *bytes_file = "apache/apache_bytes.rrd";
50 static char *bytes_ds_def[] =
51 {
52 "DS:count:COUNTER:25:0:U",
53 NULL
54 };
55 static int bytes_ds_num = 1;
57 static char *requests_file = "apache/apache_requests.rrd";
58 static char *requests_ds_def[] =
59 {
60 "DS:count:COUNTER:25:0:U",
61 NULL
62 };
63 static int requests_ds_num = 1;
65 static char *scoreboard_file = "apache/apache_scoreboard-%s.rrd";
66 static char *scoreboard_ds_def[] =
67 {
68 "DS:count:GAUGE:25:0:U",
69 NULL
70 };
71 static int scoreboard_ds_num = 1;
73 static char *config_keys[] =
74 {
75 "URL",
76 "User",
77 "Password",
78 NULL
79 };
80 static int config_keys_num = 3;
83 static size_t apache_curl_callback (void *buf, size_t size, size_t nmemb, void *stream)
84 {
85 size_t len = size * nmemb;
87 if ((apache_buffer_len + len) >= 4096)
88 {
89 len = 4095 - apache_buffer_len;
90 }
92 if (len <= 0)
93 return (len);
95 memcpy (apache_buffer + apache_buffer_len, (char *) buf, len);
96 apache_buffer_len += len;
97 apache_buffer[apache_buffer_len] = '\0';
99 return (len);
100 }
102 static int config_set (char **var, char *value)
103 {
104 if (*var != NULL)
105 {
106 free (*var);
107 *var = NULL;
108 }
110 if ((*var = strdup (value)) == NULL)
111 return (1);
112 else
113 return (0);
114 }
116 static int config (char *key, char *value)
117 {
118 if (strcasecmp (key, "url") == 0)
119 return (config_set (&url, value));
120 else if (strcasecmp (key, "user") == 0)
121 return (config_set (&user, value));
122 else if (strcasecmp (key, "password") == 0)
123 return (config_set (&pass, value));
124 else
125 return (-1);
126 }
128 static void init (void)
129 {
130 #if APACHE_HAVE_READ
131 static char credentials[1024];
133 if (curl != NULL)
134 {
135 curl_easy_cleanup (curl);
136 }
138 if ((curl = curl_easy_init ()) == NULL)
139 {
140 syslog (LOG_ERR, "apache: `curl_easy_init' failed.");
141 return;
142 }
144 curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, apache_curl_callback);
145 curl_easy_setopt (curl, CURLOPT_USERAGENT, PACKAGE_NAME"/"PACKAGE_VERSION);
146 curl_easy_setopt (curl, CURLOPT_ERRORBUFFER, apache_curl_error);
148 if (user != NULL)
149 {
150 if (snprintf (credentials, 1024, "%s:%s", user, pass == NULL ? "" : pass) >= 1024)
151 {
152 syslog (LOG_ERR, "apache: Credentials would have been truncated.");
153 return;
154 }
156 curl_easy_setopt (curl, CURLOPT_USERPWD, credentials);
157 }
159 if (url != NULL)
160 {
161 curl_easy_setopt (curl, CURLOPT_URL, url);
162 }
163 #endif /* APACHE_HAVE_READ */
164 }
166 static void bytes_write (char *host, char *inst, char *val)
167 {
168 rrd_update_file (host, bytes_file, val, bytes_ds_def, bytes_ds_num);
169 }
171 static void requests_write (char *host, char *inst, char *val)
172 {
173 rrd_update_file (host, requests_file, val, requests_ds_def, requests_ds_num);
174 }
176 static void scoreboard_write (char *host, char *inst, char *val)
177 {
178 char buf[1024];
180 if (snprintf (buf, 1024, scoreboard_file, inst) >= 1024)
181 return;
183 rrd_update_file (host, buf, val, scoreboard_ds_def, scoreboard_ds_num);
184 }
186 #if APACHE_HAVE_READ
187 static void submit (char *type, char *inst, long long value)
188 {
189 char buf[1024];
190 int status;
192 status = snprintf (buf, 1024, "%u:%lli", (unsigned int) curtime, value);
193 if (status < 0)
194 {
195 syslog (LOG_ERR, "apache: bytes_submit: snprintf failed");
196 return;
197 }
198 else if (status >= 1024)
199 {
200 syslog (LOG_WARNING, "apache: bytes_submit: snprintf was truncated");
201 return;
202 }
204 plugin_submit (type, inst, buf);
205 }
207 static void submit_scoreboard (char *buf)
208 {
209 /*
210 * Scoreboard Key:
211 * "_" Waiting for Connection, "S" Starting up, "R" Reading Request,
212 * "W" Sending Reply, "K" Keepalive (read), "D" DNS Lookup,
213 * "C" Closing connection, "L" Logging, "G" Gracefully finishing,
214 * "I" Idle cleanup of worker, "." Open slot with no current process
215 */
216 long long open = 0LL;
217 long long waiting = 0LL;
218 long long starting = 0LL;
219 long long reading = 0LL;
220 long long sending = 0LL;
221 long long keepalive = 0LL;
222 long long dnslookup = 0LL;
223 long long closing = 0LL;
224 long long logging = 0LL;
225 long long finishing = 0LL;
226 long long idle_cleanup = 0LL;
228 int i;
230 for (i = 0; buf[i] != '\0'; i++)
231 {
232 if (buf[i] == '.') open++;
233 else if (buf[i] == '_') waiting++;
234 else if (buf[i] == 'S') starting++;
235 else if (buf[i] == 'R') reading++;
236 else if (buf[i] == 'W') sending++;
237 else if (buf[i] == 'K') keepalive++;
238 else if (buf[i] == 'D') dnslookup++;
239 else if (buf[i] == 'C') closing++;
240 else if (buf[i] == 'L') logging++;
241 else if (buf[i] == 'G') finishing++;
242 else if (buf[i] == 'I') idle_cleanup++;
243 }
245 submit ("apache_scoreboard", "open" , open);
246 submit ("apache_scoreboard", "waiting" , waiting);
247 submit ("apache_scoreboard", "starting" , starting);
248 submit ("apache_scoreboard", "reading" , reading);
249 submit ("apache_scoreboard", "sending" , sending);
250 submit ("apache_scoreboard", "keepalive", keepalive);
251 submit ("apache_scoreboard", "dnslookup", dnslookup);
252 submit ("apache_scoreboard", "closing" , closing);
253 submit ("apache_scoreboard", "logging" , logging);
254 submit ("apache_scoreboard", "finishing", finishing);
255 submit ("apache_scoreboard", "idle_cleanup", idle_cleanup);
256 }
258 static void apache_read (void)
259 {
260 int i;
262 char *ptr;
263 char *lines[16];
264 int lines_num = 0;
266 char *fields[4];
267 int fields_num;
269 if (curl == NULL)
270 return;
271 if (url == NULL)
272 return;
274 if (curl_easy_perform (curl) != 0)
275 {
276 syslog (LOG_WARNING, "apache: curl_easy_perform failed: %s", apache_curl_error);
277 return;
278 }
280 ptr = apache_buffer;
281 while ((lines[lines_num] = strtok (ptr, "\n\r")) != NULL)
282 {
283 ptr = NULL;
284 lines_num++;
286 if (lines_num >= 16)
287 break;
288 }
290 for (i = 0; i < lines_num; i++)
291 {
292 fields_num = strsplit (lines[i], fields, 4);
294 if (fields_num == 3)
295 {
296 if ((strcmp (fields[0], "Total") == 0)
297 && (strcmp (fields[1], "Accesses:") == 0))
298 submit ("apache_requests", NULL, atoll (fields[2]));
299 else if ((strcmp (fields[0], "Total") == 0)
300 && (strcmp (fields[1], "kBytes:") == 0))
301 submit ("apache_bytes", NULL, 1024LL * atoll (fields[2]));
302 }
303 else if (fields_num == 2)
304 {
305 if (strcmp (fields[0], "Scoreboard:") == 0)
306 submit_scoreboard (fields[1]);
307 }
308 }
310 apache_buffer_len = 0;
311 }
312 #else
313 # define apache_read NULL
314 #endif /* APACHE_HAVE_READ */
316 void module_register (void)
317 {
318 plugin_register (MODULE_NAME, init, apache_read, NULL);
319 plugin_register ("apache_requests", NULL, NULL, requests_write);
320 plugin_register ("apache_bytes", NULL, NULL, bytes_write);
321 plugin_register ("apache_scoreboard", NULL, NULL, scoreboard_write);
322 cf_register (MODULE_NAME, config, config_keys, config_keys_num);
323 }
325 #undef MODULE_NAME