c410dc43055bb855195ab33b31d34cfeadcb225f
[collectd.git] / src / apache.c
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  **/
22
23 #include "collectd.h"
24 #include "common.h"
25 #include "plugin.h"
26 #include "configfile.h"
27
28 #define MODULE_NAME "apache"
29
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
36
37 static char *url  = NULL;
38 static char *user = NULL;
39 static char *pass = NULL;
40
41 #if APACHE_HAVE_READ
42 static CURL *curl = NULL;
43
44 static char apache_buffer[4096];
45 static int  apache_buffer_len = 0;
46 static char apache_curl_error[CURL_ERROR_SIZE];
47 #endif
48
49 /* Limit to 2^27 bytes/s. That's what a gigabit-ethernet link can handle, in
50  * theory. */
51 static char *bytes_file = "apache/apache_bytes.rrd";
52 static char *bytes_ds_def[] =
53 {
54         "DS:count:COUNTER:"COLLECTD_HEARTBEAT":0:134217728",
55         NULL
56 };
57 static int bytes_ds_num = 1;
58
59 /* Limit to 2^20 requests/s */
60 static char *requests_file = "apache/apache_requests.rrd";
61 static char *requests_ds_def[] =
62 {
63         "DS:count:COUNTER:"COLLECTD_HEARTBEAT":0:1048576",
64         NULL
65 };
66 static int requests_ds_num = 1;
67
68 static char *scoreboard_file = "apache/apache_scoreboard-%s.rrd";
69 static char *scoreboard_ds_def[] =
70 {
71         "DS:count:GAUGE:"COLLECTD_HEARTBEAT":0:U",
72         NULL
73 };
74 static int scoreboard_ds_num = 1;
75
76 static char *config_keys[] =
77 {
78         "URL",
79         "User",
80         "Password",
81         NULL
82 };
83 static int config_keys_num = 3;
84
85
86 static size_t apache_curl_callback (void *buf, size_t size, size_t nmemb, void *stream)
87 {
88         size_t len = size * nmemb;
89
90         if ((apache_buffer_len + len) >= 4096)
91         {
92                 len = 4095 - apache_buffer_len;
93         }
94
95         if (len <= 0)
96                 return (len);
97
98         memcpy (apache_buffer + apache_buffer_len, (char *) buf, len);
99         apache_buffer_len += len;
100         apache_buffer[apache_buffer_len] = '\0';
101
102         return (len);
103 }
104
105 static int config_set (char **var, char *value)
106 {
107         if (*var != NULL)
108         {
109                 free (*var);
110                 *var = NULL;
111         }
112
113         if ((*var = strdup (value)) == NULL)
114                 return (1);
115         else
116                 return (0);
117 }
118
119 static int config (char *key, char *value)
120 {
121         if (strcasecmp (key, "url") == 0)
122                 return (config_set (&url, value));
123         else if (strcasecmp (key, "user") == 0)
124                 return (config_set (&user, value));
125         else if (strcasecmp (key, "password") == 0)
126                 return (config_set (&pass, value));
127         else
128                 return (-1);
129 }
130
131 static void init (void)
132 {
133 #if APACHE_HAVE_READ
134         static char credentials[1024];
135
136         if (curl != NULL)
137         {
138                 curl_easy_cleanup (curl);
139         }
140
141         if ((curl = curl_easy_init ()) == NULL)
142         {
143                 syslog (LOG_ERR, "apache: `curl_easy_init' failed.");
144                 return;
145         }
146
147         curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, apache_curl_callback);
148         curl_easy_setopt (curl, CURLOPT_USERAGENT, PACKAGE_NAME"/"PACKAGE_VERSION);
149         curl_easy_setopt (curl, CURLOPT_ERRORBUFFER, apache_curl_error);
150
151         if (user != NULL)
152         {
153                 if (snprintf (credentials, 1024, "%s:%s", user, pass == NULL ? "" : pass) >= 1024)
154                 {
155                         syslog (LOG_ERR, "apache: Credentials would have been truncated.");
156                         return;
157                 }
158
159                 curl_easy_setopt (curl, CURLOPT_USERPWD, credentials);
160         }
161
162         if (url != NULL)
163         {
164                 curl_easy_setopt (curl, CURLOPT_URL, url);
165         }
166 #endif /* APACHE_HAVE_READ */
167 }
168
169 static void bytes_write (char *host, char *inst, char *val)
170 {
171         rrd_update_file (host, bytes_file, val, bytes_ds_def, bytes_ds_num);
172 }
173
174 static void requests_write (char *host, char *inst, char *val)
175 {
176         rrd_update_file (host, requests_file, val, requests_ds_def, requests_ds_num);
177 }
178
179 static void scoreboard_write (char *host, char *inst, char *val)
180 {
181         char buf[1024];
182
183         if (snprintf (buf, 1024, scoreboard_file, inst) >= 1024)
184                 return;
185
186         rrd_update_file (host, buf, val, scoreboard_ds_def, scoreboard_ds_num);
187 }
188
189 #if APACHE_HAVE_READ
190 static void submit (char *type, char *inst, long long value)
191 {
192         char buf[1024];
193         int  status;
194
195         status = snprintf (buf, 1024, "%u:%lli", (unsigned int) curtime, value);
196         if (status < 0)
197         {
198                 syslog (LOG_ERR, "apache: bytes_submit: snprintf failed");
199                 return;
200         }
201         else if (status >= 1024)
202         {
203                 syslog (LOG_WARNING, "apache: bytes_submit: snprintf was truncated");
204                 return;
205         }
206
207         plugin_submit (type, inst, buf);
208 }
209
210 static void submit_scoreboard (char *buf)
211 {
212         /*
213          * Scoreboard Key:
214          * "_" Waiting for Connection, "S" Starting up, "R" Reading Request,
215          * "W" Sending Reply, "K" Keepalive (read), "D" DNS Lookup,
216          * "C" Closing connection, "L" Logging, "G" Gracefully finishing,
217          * "I" Idle cleanup of worker, "." Open slot with no current process
218          */
219         long long open      = 0LL;
220         long long waiting   = 0LL;
221         long long starting  = 0LL;
222         long long reading   = 0LL;
223         long long sending   = 0LL;
224         long long keepalive = 0LL;
225         long long dnslookup = 0LL;
226         long long closing   = 0LL;
227         long long logging   = 0LL;
228         long long finishing = 0LL;
229         long long idle_cleanup = 0LL;
230
231         int i;
232
233         for (i = 0; buf[i] != '\0'; i++)
234         {
235                 if (buf[i] == '.') open++;
236                 else if (buf[i] == '_') waiting++;
237                 else if (buf[i] == 'S') starting++;
238                 else if (buf[i] == 'R') reading++;
239                 else if (buf[i] == 'W') sending++;
240                 else if (buf[i] == 'K') keepalive++;
241                 else if (buf[i] == 'D') dnslookup++;
242                 else if (buf[i] == 'C') closing++;
243                 else if (buf[i] == 'L') logging++;
244                 else if (buf[i] == 'G') finishing++;
245                 else if (buf[i] == 'I') idle_cleanup++;
246         }
247
248         submit ("apache_scoreboard", "open"     , open);
249         submit ("apache_scoreboard", "waiting"  , waiting);
250         submit ("apache_scoreboard", "starting" , starting);
251         submit ("apache_scoreboard", "reading"  , reading);
252         submit ("apache_scoreboard", "sending"  , sending);
253         submit ("apache_scoreboard", "keepalive", keepalive);
254         submit ("apache_scoreboard", "dnslookup", dnslookup);
255         submit ("apache_scoreboard", "closing"  , closing);
256         submit ("apache_scoreboard", "logging"  , logging);
257         submit ("apache_scoreboard", "finishing", finishing);
258         submit ("apache_scoreboard", "idle_cleanup", idle_cleanup);
259 }
260
261 static void apache_read (void)
262 {
263         int i;
264
265         char *ptr;
266         char *lines[16];
267         int   lines_num = 0;
268
269         char *fields[4];
270         int   fields_num;
271
272         if (curl == NULL)
273                 return;
274         if (url == NULL)
275                 return;
276
277         if (curl_easy_perform (curl) != 0)
278         {
279                 syslog (LOG_WARNING, "apache: curl_easy_perform failed: %s", apache_curl_error);
280                 return;
281         }
282
283         ptr = apache_buffer;
284         while ((lines[lines_num] = strtok (ptr, "\n\r")) != NULL)
285         {
286                 ptr = NULL;
287                 lines_num++;
288
289                 if (lines_num >= 16)
290                         break;
291         }
292
293         for (i = 0; i < lines_num; i++)
294         {
295                 fields_num = strsplit (lines[i], fields, 4);
296
297                 if (fields_num == 3)
298                 {
299                         if ((strcmp (fields[0], "Total") == 0)
300                                         && (strcmp (fields[1], "Accesses:") == 0))
301                                 submit ("apache_requests", NULL, atoll (fields[2]));
302                         else if ((strcmp (fields[0], "Total") == 0)
303                                         && (strcmp (fields[1], "kBytes:") == 0))
304                                 submit ("apache_bytes", NULL, 1024LL * atoll (fields[2]));
305                 }
306                 else if (fields_num == 2)
307                 {
308                         if (strcmp (fields[0], "Scoreboard:") == 0)
309                                 submit_scoreboard (fields[1]);
310                 }
311         }
312
313         apache_buffer_len = 0;
314 }
315 #else
316 #  define apache_read NULL
317 #endif /* APACHE_HAVE_READ */
318
319 void module_register (void)
320 {
321         plugin_register (MODULE_NAME, init, apache_read, NULL);
322         plugin_register ("apache_requests",   NULL, NULL, requests_write);
323         plugin_register ("apache_bytes",      NULL, NULL, bytes_write);
324         plugin_register ("apache_scoreboard", NULL, NULL, scoreboard_write);
325         cf_register (MODULE_NAME, config, config_keys, config_keys_num);
326 }
327
328 #undef MODULE_NAME