src/daemon/common.[ch]: add check_capability() function
[collectd.git] / src / nginx.c
1 /**
2  * collectd - src/nginx.c
3  * Copyright (C) 2006-2010  Florian octo Forster
4  * Copyright (C) 2008       Sebastian Harl
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  * DEALINGS IN THE SOFTWARE.
23  *
24  * Authors:
25  *   Florian octo Forster <octo at collectd.org>
26  *   Sebastian Harl <sh at tokkee.org>
27  **/
28
29 #include "collectd.h"
30
31 #include "common.h"
32 #include "plugin.h"
33 #include "configfile.h"
34
35 #include <curl/curl.h>
36
37 static char *url         = NULL;
38 static char *user        = NULL;
39 static char *pass        = NULL;
40 static char *verify_peer = NULL;
41 static char *verify_host = NULL;
42 static char *cacert      = NULL;
43 static char *timeout     = NULL;
44
45 static CURL *curl = NULL;
46
47 static char   nginx_buffer[16384];
48 static size_t nginx_buffer_len = 0;
49 static char   nginx_curl_error[CURL_ERROR_SIZE];
50
51 static const char *config_keys[] =
52 {
53   "URL",
54   "User",
55   "Password",
56   "VerifyPeer",
57   "VerifyHost",
58   "CACert",
59   "Timeout"
60 };
61 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
62
63 static size_t nginx_curl_callback (void *buf, size_t size, size_t nmemb,
64     void __attribute__((unused)) *stream)
65 {
66   size_t len = size * nmemb;
67
68   /* Check if the data fits into the memory. If not, truncate it. */
69   if ((nginx_buffer_len + len) >= sizeof (nginx_buffer))
70   {
71     assert (sizeof (nginx_buffer) > nginx_buffer_len);
72     len = (sizeof (nginx_buffer) - 1) - nginx_buffer_len;
73   }
74
75   if (len == 0)
76     return (len);
77
78   memcpy (&nginx_buffer[nginx_buffer_len], buf, len);
79   nginx_buffer_len += len;
80   nginx_buffer[nginx_buffer_len] = 0;
81
82   return (len);
83 }
84
85 static int config_set (char **var, const char *value)
86 {
87   if (*var != NULL)
88   {
89     free (*var);
90     *var = NULL;
91   }
92
93   if ((*var = strdup (value)) == NULL)
94     return (1);
95   else
96     return (0);
97 }
98
99 static int config (const char *key, const char *value)
100 {
101   if (strcasecmp (key, "url") == 0)
102     return (config_set (&url, value));
103   else if (strcasecmp (key, "user") == 0)
104     return (config_set (&user, value));
105   else if (strcasecmp (key, "password") == 0)
106     return (config_set (&pass, value));
107   else if (strcasecmp (key, "verifypeer") == 0)
108     return (config_set (&verify_peer, value));
109   else if (strcasecmp (key, "verifyhost") == 0)
110     return (config_set (&verify_host, value));
111   else if (strcasecmp (key, "cacert") == 0)
112     return (config_set (&cacert, value));
113   else if (strcasecmp (key, "timeout") == 0)
114     return (config_set (&timeout, value));
115   else
116     return (-1);
117 } /* int config */
118
119 static int init (void)
120 {
121   if (curl != NULL)
122     curl_easy_cleanup (curl);
123
124   if ((curl = curl_easy_init ()) == NULL)
125   {
126     ERROR ("nginx plugin: curl_easy_init failed.");
127     return (-1);
128   }
129
130   curl_easy_setopt (curl, CURLOPT_NOSIGNAL, 1L);
131   curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, nginx_curl_callback);
132   curl_easy_setopt (curl, CURLOPT_USERAGENT, COLLECTD_USERAGENT);
133   curl_easy_setopt (curl, CURLOPT_ERRORBUFFER, nginx_curl_error);
134
135   if (user != NULL)
136   {
137 #ifdef HAVE_CURLOPT_USERNAME
138     curl_easy_setopt (curl, CURLOPT_USERNAME, user);
139     curl_easy_setopt (curl, CURLOPT_PASSWORD, (pass == NULL) ? "" : pass);
140 #else
141     static char credentials[1024];
142     int status = ssnprintf (credentials, sizeof (credentials),
143         "%s:%s", user, pass == NULL ? "" : pass);
144     if ((status < 0) || ((size_t) status >= sizeof (credentials)))
145     {
146       ERROR ("nginx plugin: Credentials would have been truncated.");
147       return (-1);
148     }
149
150     curl_easy_setopt (curl, CURLOPT_USERPWD, credentials);
151 #endif
152   }
153
154   if (url != NULL)
155   {
156     curl_easy_setopt (curl, CURLOPT_URL, url);
157   }
158
159   curl_easy_setopt (curl, CURLOPT_FOLLOWLOCATION, 1L);
160   curl_easy_setopt (curl, CURLOPT_MAXREDIRS, 50L);
161
162   if ((verify_peer == NULL) || IS_TRUE (verify_peer))
163   {
164     curl_easy_setopt (curl, CURLOPT_SSL_VERIFYPEER, 1L);
165   }
166   else
167   {
168     curl_easy_setopt (curl, CURLOPT_SSL_VERIFYPEER, 0L);
169   }
170
171   if ((verify_host == NULL) || IS_TRUE (verify_host))
172   {
173     curl_easy_setopt (curl, CURLOPT_SSL_VERIFYHOST, 2L);
174   }
175   else
176   {
177     curl_easy_setopt (curl, CURLOPT_SSL_VERIFYHOST, 0L);
178   }
179
180   if (cacert != NULL)
181   {
182     curl_easy_setopt (curl, CURLOPT_CAINFO, cacert);
183   }
184
185 #ifdef HAVE_CURLOPT_TIMEOUT_MS
186   if (timeout != NULL)
187   {
188     curl_easy_setopt (curl, CURLOPT_TIMEOUT_MS, atol(timeout));
189   }
190   else
191   {
192     curl_easy_setopt (curl, CURLOPT_TIMEOUT_MS, (long) CDTIME_T_TO_MS(plugin_get_interval()));
193   }
194 #endif
195
196   return (0);
197 } /* void init */
198
199 static void submit (const char *type, const char *inst, long long value)
200 {
201   value_t values[1];
202   value_list_t vl = VALUE_LIST_INIT;
203
204   if (strcmp (type, "nginx_connections") == 0)
205     values[0].gauge = value;
206   else if (strcmp (type, "nginx_requests") == 0)
207     values[0].derive = value;
208   else if (strcmp (type, "connections") == 0)
209     values[0].derive = value;
210   else
211     return;
212
213   vl.values = values;
214   vl.values_len = 1;
215   sstrncpy (vl.host, hostname_g, sizeof (vl.host));
216   sstrncpy (vl.plugin, "nginx", sizeof (vl.plugin));
217   sstrncpy (vl.plugin_instance, "", sizeof (vl.plugin_instance));
218   sstrncpy (vl.type, type, sizeof (vl.type));
219
220   if (inst != NULL)
221     sstrncpy (vl.type_instance, inst, sizeof (vl.type_instance));
222
223   plugin_dispatch_values (&vl);
224 } /* void submit */
225
226 static int nginx_read (void)
227 {
228   char *ptr;
229   char *lines[16];
230   int   lines_num = 0;
231   char *saveptr;
232
233   char *fields[16];
234   int   fields_num;
235
236   if (curl == NULL)
237     return (-1);
238   if (url == NULL)
239     return (-1);
240
241   nginx_buffer_len = 0;
242   if (curl_easy_perform (curl) != CURLE_OK)
243   {
244     WARNING ("nginx plugin: curl_easy_perform failed: %s", nginx_curl_error);
245     return (-1);
246   }
247
248   ptr = nginx_buffer;
249   saveptr = NULL;
250   while ((lines[lines_num] = strtok_r (ptr, "\n\r", &saveptr)) != NULL)
251   {
252     ptr = NULL;
253     lines_num++;
254
255     if (lines_num >= 16)
256       break;
257   }
258
259   /*
260    * Active connections: 291
261    * server accepts handled requests
262    *  16630948 16630948 31070465
263    * Reading: 6 Writing: 179 Waiting: 106
264    */
265   for (int i = 0; i < lines_num; i++)
266   {
267     fields_num = strsplit (lines[i], fields,
268         (sizeof (fields) / sizeof (fields[0])));
269
270     if (fields_num == 3)
271     {
272       if ((strcmp (fields[0], "Active") == 0)
273           && (strcmp (fields[1], "connections:") == 0))
274       {
275         submit ("nginx_connections", "active", atoll (fields[2]));
276       }
277       else if ((atoll (fields[0]) != 0)
278           && (atoll (fields[1]) != 0)
279           && (atoll (fields[2]) != 0))
280       {
281         submit ("connections", "accepted", atoll (fields[0]));
282         submit ("connections", "handled", atoll (fields[1]));
283         submit ("nginx_requests", NULL, atoll (fields[2]));
284       }
285     }
286     else if (fields_num == 6)
287     {
288       if ((strcmp (fields[0], "Reading:") == 0)
289           && (strcmp (fields[2], "Writing:") == 0)
290           && (strcmp (fields[4], "Waiting:") == 0))
291       {
292         submit ("nginx_connections", "reading", atoll (fields[1]));
293         submit ("nginx_connections", "writing", atoll (fields[3]));
294         submit ("nginx_connections", "waiting", atoll (fields[5]));
295       }
296     }
297   }
298
299   nginx_buffer_len = 0;
300
301   return (0);
302 } /* int nginx_read */
303
304 void module_register (void)
305 {
306   plugin_register_config ("nginx", config, config_keys, config_keys_num);
307   plugin_register_init ("nginx", init);
308   plugin_register_read ("nginx", nginx_read);
309 } /* void module_register */
310
311 /*
312  * vim: set shiftwidth=2 softtabstop=2 tabstop=8 :
313  */