Merge branch 'collectd-4.0' into collectd-4.1
[collectd.git] / src / apache.c
1 /**
2  * collectd - src/apache.c
3  * Copyright (C) 2006,2007  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  **/
24
25 #include "collectd.h"
26 #include "common.h"
27 #include "plugin.h"
28 #include "configfile.h"
29
30 #include <curl/curl.h>
31
32 static char *url    = NULL;
33 static char *user   = NULL;
34 static char *pass   = NULL;
35 static char *cacert = NULL;
36
37 static CURL *curl = NULL;
38
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];
43
44 static const char *config_keys[] =
45 {
46         "URL",
47         "User",
48         "Password",
49         "CACert",
50         NULL
51 };
52 static int config_keys_num = 4;
53
54 static size_t apache_curl_callback (void *buf, size_t size, size_t nmemb, void *stream)
55 {
56         size_t len = size * nmemb;
57
58         if ((apache_buffer_len + len) >= ABUFFER_SIZE)
59         {
60                 len = (ABUFFER_SIZE - 1) - apache_buffer_len;
61         }
62
63         if (len <= 0)
64                 return (len);
65
66         memcpy (apache_buffer + apache_buffer_len, (char *) buf, len);
67         apache_buffer_len += len;
68         apache_buffer[apache_buffer_len] = '\0';
69
70         return (len);
71 }
72
73 static int config_set (char **var, const char *value)
74 {
75         if (*var != NULL)
76         {
77                 free (*var);
78                 *var = NULL;
79         }
80
81         if ((*var = strdup (value)) == NULL)
82                 return (1);
83         else
84                 return (0);
85 }
86
87 static int config (const char *key, const char *value)
88 {
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));
97         else
98                 return (-1);
99 }
100
101 static int init (void)
102 {
103         static char credentials[1024];
104
105         if (curl != NULL)
106         {
107                 curl_easy_cleanup (curl);
108         }
109
110         if ((curl = curl_easy_init ()) == NULL)
111         {
112                 ERROR ("apache: `curl_easy_init' failed.");
113                 return (-1);
114         }
115
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);
119
120         if (user != NULL)
121         {
122                 if (snprintf (credentials, 1024, "%s:%s", user, pass == NULL ? "" : pass) >= 1024)
123                 {
124                         ERROR ("apache: Credentials would have been truncated.");
125                         return (-1);
126                 }
127
128                 curl_easy_setopt (curl, CURLOPT_USERPWD, credentials);
129         }
130
131         if (url != NULL)
132         {
133                 curl_easy_setopt (curl, CURLOPT_URL, url);
134         }
135
136         if (cacert != NULL)
137         {
138                 curl_easy_setopt (curl, CURLOPT_CAINFO, cacert);
139         }
140
141         return (0);
142 } /* int init */
143
144 static void submit_counter (const char *type, const char *type_instance,
145                 unsigned long long value)
146 {
147         value_t values[1];
148         value_list_t vl = VALUE_LIST_INIT;
149
150         DEBUG ("type = %s; type_instance = %s; value = %llu;",
151                         type, type_instance, value);
152
153         values[0].counter = value;
154
155         vl.values = values;
156         vl.values_len = 1;
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));
162
163         plugin_dispatch_values (type, &vl);
164 } /* void submit_counter */
165
166 static void submit_gauge (const char *type, const char *type_instance,
167                 double value)
168 {
169         value_t values[1];
170         value_list_t vl = VALUE_LIST_INIT;
171
172         DEBUG ("type = %s; type_instance = %s; value = %lf;",
173                         type, type_instance, value);
174
175         values[0].gauge = value;
176
177         vl.values = values;
178         vl.values_len = 1;
179         vl.time = time (NULL);
180         strcpy (vl.host, hostname_g);
181         strcpy (vl.plugin, "apache");
182         strcpy (vl.plugin_instance, "");
183
184         if (type_instance != NULL)
185                 strncpy (vl.type_instance, type_instance,
186                                 sizeof (vl.type_instance));
187
188         plugin_dispatch_values (type, &vl);
189 } /* void submit_counter */
190
191 static void submit_scoreboard (char *buf)
192 {
193         /*
194          * Scoreboard Key:
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
199          */
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;
211
212         int i;
213
214         for (i = 0; buf[i] != '\0'; i++)
215         {
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++;
227         }
228
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);
240 }
241
242 static int apache_read (void)
243 {
244         int i;
245
246         char *ptr;
247         char *saveptr;
248         char *lines[16];
249         int   lines_num = 0;
250
251         char *fields[4];
252         int   fields_num;
253
254         if (curl == NULL)
255                 return (-1);
256         if (url == NULL)
257                 return (-1);
258
259         apache_buffer_len = 0;
260         if (curl_easy_perform (curl) != 0)
261         {
262                 ERROR ("apache: curl_easy_perform failed: %s",
263                                 apache_curl_error);
264                 return (-1);
265         }
266
267         ptr = apache_buffer;
268         saveptr = NULL;
269         while ((lines[lines_num] = strtok_r (ptr, "\n\r", &saveptr)) != NULL)
270         {
271                 ptr = NULL;
272                 lines_num++;
273
274                 if (lines_num >= 16)
275                         break;
276         }
277
278         for (i = 0; i < lines_num; i++)
279         {
280                 fields_num = strsplit (lines[i], fields, 4);
281
282                 if (fields_num == 3)
283                 {
284                         if ((strcmp (fields[0], "Total") == 0)
285                                         && (strcmp (fields[1], "Accesses:") == 0))
286                                 submit_counter ("apache_requests", "",
287                                                 atoll (fields[2]));
288                         else if ((strcmp (fields[0], "Total") == 0)
289                                         && (strcmp (fields[1], "kBytes:") == 0))
290                                 submit_counter ("apache_bytes", "",
291                                                 1024LL * atoll (fields[2]));
292                 }
293                 else if (fields_num == 2)
294                 {
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]));
299                 }
300         }
301
302         apache_buffer_len = 0;
303
304         return (0);
305 } /* int apache_read */
306
307 void module_register (void)
308 {
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 */