Merge branch 'ff/nginx'
[collectd.git] / src / nginx.c
1 /**
2  * collectd - src/nginx.c
3  * Copyright (C) 2006,2007  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 "utils_debug.h"
27 #include "configfile.h"
28
29 #define MODULE_NAME "nginx"
30
31 #if HAVE_LIBCURL && HAVE_CURL_CURL_H
32 #  define NGINX_HAVE_READ 1
33 #  include <curl/curl.h>
34 #else
35 #  define NGINX_HAVE_READ 0
36 #endif
37
38 static char *url    = NULL;
39 static char *user   = NULL;
40 static char *pass   = NULL;
41 static char *cacert = NULL;
42
43 #if HAVE_LIBCURL
44 static CURL *curl = NULL;
45
46 #define ABUFFER_SIZE 16384
47 static char nginx_buffer[ABUFFER_SIZE];
48 static int  nginx_buffer_len = 0;
49 static char nginx_curl_error[CURL_ERROR_SIZE];
50 #endif /* HAVE_LIBCURL */
51
52 static char *connections_file = "nginx/nginx_connections-%s.rrd";
53 static char *connections_ds_def[] =
54 {
55   "DS:value:GAUGE:"COLLECTD_HEARTBEAT":0:U",
56   NULL
57 };
58 static int connections_ds_num = 1;
59
60 /* Limit to 2^20 requests/s */
61 static char *requests_file = "nginx/nginx_requests.rrd";
62 static char *requests_ds_def[] =
63 {
64   "DS:value:COUNTER:"COLLECTD_HEARTBEAT":0:1048576",
65   NULL
66 };
67 static int requests_ds_num = 1;
68
69 static char *config_keys[] =
70 {
71   "URL",
72   "User",
73   "Password",
74   "CACert",
75   NULL
76 };
77 static int config_keys_num = 4;
78
79 #if HAVE_LIBCURL
80 static size_t nginx_curl_callback (void *buf, size_t size, size_t nmemb, void *stream)
81 {
82   size_t len = size * nmemb;
83
84   if ((nginx_buffer_len + len) >= ABUFFER_SIZE)
85   {
86     len = (ABUFFER_SIZE - 1) - nginx_buffer_len;
87   }
88
89   if (len <= 0)
90     return (len);
91
92   memcpy (nginx_buffer + nginx_buffer_len, (char *) buf, len);
93   nginx_buffer_len += len;
94   nginx_buffer[nginx_buffer_len] = '\0';
95
96   return (len);
97 }
98 #endif /* HAVE_LIBCURL */
99
100 static int config_set (char **var, char *value)
101 {
102   if (*var != NULL)
103   {
104     free (*var);
105     *var = NULL;
106   }
107
108   if ((*var = strdup (value)) == NULL)
109     return (1);
110   else
111     return (0);
112 }
113
114 static int config (char *key, char *value)
115 {
116   if (strcasecmp (key, "url") == 0)
117     return (config_set (&url, value));
118   else if (strcasecmp (key, "user") == 0)
119     return (config_set (&user, value));
120   else if (strcasecmp (key, "password") == 0)
121     return (config_set (&pass, value));
122   else if (strcasecmp (key, "cacert") == 0)
123     return (config_set (&cacert, value));
124   else
125     return (-1);
126 }
127
128 static void init (void)
129 {
130 #if HAVE_LIBCURL
131   static char credentials[1024];
132
133   if (curl != NULL)
134   {
135     curl_easy_cleanup (curl);
136   }
137
138   if ((curl = curl_easy_init ()) == NULL)
139   {
140     syslog (LOG_ERR, "nginx: `curl_easy_init' failed.");
141     return;
142   }
143
144   curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, nginx_curl_callback);
145   curl_easy_setopt (curl, CURLOPT_USERAGENT, PACKAGE_NAME"/"PACKAGE_VERSION);
146   curl_easy_setopt (curl, CURLOPT_ERRORBUFFER, nginx_curl_error);
147
148   if (user != NULL)
149   {
150     if (snprintf (credentials, 1024, "%s:%s", user, pass == NULL ? "" : pass) >= 1024)
151     {
152       syslog (LOG_ERR, "nginx: Credentials would have been truncated.");
153       return;
154     }
155
156     curl_easy_setopt (curl, CURLOPT_USERPWD, credentials);
157   }
158
159   if (url != NULL)
160   {
161     curl_easy_setopt (curl, CURLOPT_URL, url);
162   }
163
164   if (cacert != NULL)
165   {
166     curl_easy_setopt (curl, CURLOPT_CAINFO, cacert);
167   }
168 #endif /* HAVE_LIBCURL */
169 } /* void init */
170
171 static void connections_write (char *host, char *inst, char *val)
172 {
173   char buf[1024];
174
175   if (snprintf (buf, 1024, connections_file, inst) >= 1024)
176     return;
177
178   rrd_update_file (host, buf, val,
179       connections_ds_def, connections_ds_num);
180 }
181
182 static void requests_write (char *host, char *inst, char *val)
183 {
184   rrd_update_file (host, requests_file, val,
185       requests_ds_def, requests_ds_num);
186 }
187
188 #if NGINX_HAVE_READ
189 static void submit (char *type, char *inst, long long value)
190 {
191   char buf[1024];
192   int  status;
193
194   DBG ("type = %s; inst = %s; value = %lli;",
195       type, (inst == NULL) ? "(nil)" : inst, value);
196
197   status = snprintf (buf, 1024, "%u:%lli", (unsigned int) curtime, value);
198   if ((status < 0) || (status >= 1024))
199   {
200     syslog (LOG_ERR, "nginx: snprintf failed");
201     return;
202   }
203
204   plugin_submit (type, inst, buf);
205 }
206
207 static void nginx_read (void)
208 {
209   int i;
210
211   char *ptr;
212   char *lines[16];
213   int   lines_num = 0;
214
215   char *fields[16];
216   int   fields_num;
217
218   if (curl == NULL)
219     return;
220   if (url == NULL)
221     return;
222
223   nginx_buffer_len = 0;
224   if (curl_easy_perform (curl) != 0)
225   {
226     syslog (LOG_WARNING, "nginx: curl_easy_perform failed: %s", nginx_curl_error);
227     return;
228   }
229
230   ptr = nginx_buffer;
231   while ((lines[lines_num] = strtok (ptr, "\n\r")) != NULL)
232   {
233     ptr = NULL;
234     lines_num++;
235
236     if (lines_num >= 16)
237       break;
238   }
239
240   /*
241    * Active connections: 291
242    * server accepts handled requests
243    *  16630948 16630948 31070465
244    * Reading: 6 Writing: 179 Waiting: 106
245    */
246   for (i = 0; i < lines_num; i++)
247   {
248     fields_num = strsplit (lines[i], fields,
249         (sizeof (fields) / sizeof (fields[0])));
250
251     if (fields_num == 3)
252     {
253       if ((strcmp (fields[0], "Active") == 0)
254           && (strcmp (fields[1], "connections:") == 0))
255       {
256         submit ("nginx_connections", "active", atoll (fields[2]));
257       }
258       else if ((atoll (fields[0]) != 0)
259           && (atoll (fields[1]) != 0)
260           && (atoll (fields[2]) != 0))
261       {
262         submit ("nginx_requests", NULL, atoll (fields[2]));
263       }
264     }
265     else if (fields_num == 6)
266     {
267       if ((strcmp (fields[0], "Reading:") == 0)
268           && (strcmp (fields[2], "Writing:") == 0)
269           && (strcmp (fields[4], "Waiting:") == 0))
270       {
271         submit ("nginx_connections", "reading", atoll (fields[1]));
272         submit ("nginx_connections", "writing", atoll (fields[3]));
273         submit ("nginx_connections", "waiting", atoll (fields[5]));
274       }
275     }
276   }
277
278   nginx_buffer_len = 0;
279 }
280 #else
281 #  define nginx_read NULL
282 #endif /* NGINX_HAVE_READ */
283
284 void module_register (void)
285 {
286   plugin_register (MODULE_NAME, init, nginx_read, NULL);
287   plugin_register ("nginx_requests",   NULL, NULL, requests_write);
288   plugin_register ("nginx_connections", NULL, NULL, connections_write);
289   cf_register (MODULE_NAME, config, config_keys, config_keys_num);
290 }
291
292 #undef MODULE_NAME
293
294 /*
295  * vim: set shiftwidth=2 softtabstop=2 tabstop=8 :
296  */