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 **/
29 #include "collectd.h"
30 #include "common.h"
31 #include "plugin.h"
32 #include "configfile.h"
34 #include <curl/curl.h>
36 static char *url = NULL;
37 static char *user = NULL;
38 static char *pass = NULL;
39 static char *verify_peer = NULL;
40 static char *verify_host = NULL;
41 static char *cacert = NULL;
42 static char *timeout = NULL;
44 static CURL *curl = NULL;
46 static char nginx_buffer[16384];
47 static size_t nginx_buffer_len = 0;
48 static char nginx_curl_error[CURL_ERROR_SIZE];
50 static const char *config_keys[] =
51 {
52 "URL",
53 "User",
54 "Password",
55 "VerifyPeer",
56 "VerifyHost",
57 "CACert",
58 "Timeout"
59 };
60 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
62 static size_t nginx_curl_callback (void *buf, size_t size, size_t nmemb,
63 void __attribute__((unused)) *stream)
64 {
65 size_t len = size * nmemb;
67 /* Check if the data fits into the memory. If not, truncate it. */
68 if ((nginx_buffer_len + len) >= sizeof (nginx_buffer))
69 {
70 assert (sizeof (nginx_buffer) > nginx_buffer_len);
71 len = (sizeof (nginx_buffer) - 1) - nginx_buffer_len;
72 }
74 if (len <= 0)
75 return (len);
77 memcpy (&nginx_buffer[nginx_buffer_len], buf, len);
78 nginx_buffer_len += len;
79 nginx_buffer[nginx_buffer_len] = 0;
81 return (len);
82 }
84 static int config_set (char **var, const char *value)
85 {
86 if (*var != NULL)
87 {
88 free (*var);
89 *var = NULL;
90 }
92 if ((*var = strdup (value)) == NULL)
93 return (1);
94 else
95 return (0);
96 }
98 static int config (const char *key, const char *value)
99 {
100 if (strcasecmp (key, "url") == 0)
101 return (config_set (&url, value));
102 else if (strcasecmp (key, "user") == 0)
103 return (config_set (&user, value));
104 else if (strcasecmp (key, "password") == 0)
105 return (config_set (&pass, value));
106 else if (strcasecmp (key, "verifypeer") == 0)
107 return (config_set (&verify_peer, value));
108 else if (strcasecmp (key, "verifyhost") == 0)
109 return (config_set (&verify_host, value));
110 else if (strcasecmp (key, "cacert") == 0)
111 return (config_set (&cacert, value));
112 else if (strcasecmp (key, "timeout") == 0)
113 return (config_set (&timeout, value));
114 else
115 return (-1);
116 } /* int config */
118 static int init (void)
119 {
120 if (curl != NULL)
121 curl_easy_cleanup (curl);
123 if ((curl = curl_easy_init ()) == NULL)
124 {
125 ERROR ("nginx plugin: curl_easy_init failed.");
126 return (-1);
127 }
129 curl_easy_setopt (curl, CURLOPT_NOSIGNAL, 1L);
130 curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, nginx_curl_callback);
131 curl_easy_setopt (curl, CURLOPT_USERAGENT, COLLECTD_USERAGENT);
132 curl_easy_setopt (curl, CURLOPT_ERRORBUFFER, nginx_curl_error);
134 if (user != NULL)
135 {
136 #ifdef HAVE_CURLOPT_USERNAME
137 curl_easy_setopt (curl, CURLOPT_USERNAME, user);
138 curl_easy_setopt (curl, CURLOPT_PASSWORD, (pass == NULL) ? "" : pass);
139 #else
140 static char credentials[1024];
141 int status = ssnprintf (credentials, sizeof (credentials),
142 "%s:%s", user, pass == NULL ? "" : pass);
143 if ((status < 0) || ((size_t) status >= sizeof (credentials)))
144 {
145 ERROR ("nginx plugin: Credentials would have been truncated.");
146 return (-1);
147 }
149 curl_easy_setopt (curl, CURLOPT_USERPWD, credentials);
150 #endif
151 }
153 if (url != NULL)
154 {
155 curl_easy_setopt (curl, CURLOPT_URL, url);
156 }
158 curl_easy_setopt (curl, CURLOPT_FOLLOWLOCATION, 1L);
159 curl_easy_setopt (curl, CURLOPT_MAXREDIRS, 50L);
161 if ((verify_peer == NULL) || IS_TRUE (verify_peer))
162 {
163 curl_easy_setopt (curl, CURLOPT_SSL_VERIFYPEER, 1L);
164 }
165 else
166 {
167 curl_easy_setopt (curl, CURLOPT_SSL_VERIFYPEER, 0L);
168 }
170 if ((verify_host == NULL) || IS_TRUE (verify_host))
171 {
172 curl_easy_setopt (curl, CURLOPT_SSL_VERIFYHOST, 2L);
173 }
174 else
175 {
176 curl_easy_setopt (curl, CURLOPT_SSL_VERIFYHOST, 0L);
177 }
179 if (cacert != NULL)
180 {
181 curl_easy_setopt (curl, CURLOPT_CAINFO, cacert);
182 }
184 #ifdef HAVE_CURLOPT_TIMEOUT_MS
185 if (timeout != NULL)
186 {
187 curl_easy_setopt (curl, CURLOPT_TIMEOUT_MS, atol(timeout));
188 }
189 else
190 {
191 curl_easy_setopt (curl, CURLOPT_TIMEOUT_MS, (long) CDTIME_T_TO_MS(plugin_get_interval()));
192 }
193 #endif
195 return (0);
196 } /* void init */
198 static void submit (const char *type, const char *inst, long long value)
199 {
200 value_t values[1];
201 value_list_t vl = VALUE_LIST_INIT;
203 if (strcmp (type, "nginx_connections") == 0)
204 values[0].gauge = value;
205 else if (strcmp (type, "nginx_requests") == 0)
206 values[0].derive = value;
207 else if (strcmp (type, "connections") == 0)
208 values[0].derive = value;
209 else
210 return;
212 vl.values = values;
213 vl.values_len = 1;
214 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
215 sstrncpy (vl.plugin, "nginx", sizeof (vl.plugin));
216 sstrncpy (vl.plugin_instance, "", sizeof (vl.plugin_instance));
217 sstrncpy (vl.type, type, sizeof (vl.type));
219 if (inst != NULL)
220 sstrncpy (vl.type_instance, inst, sizeof (vl.type_instance));
222 plugin_dispatch_values (&vl);
223 } /* void submit */
225 static int nginx_read (void)
226 {
227 int i;
229 char *ptr;
230 char *lines[16];
231 int lines_num = 0;
232 char *saveptr;
234 char *fields[16];
235 int fields_num;
237 if (curl == NULL)
238 return (-1);
239 if (url == NULL)
240 return (-1);
242 nginx_buffer_len = 0;
243 if (curl_easy_perform (curl) != CURLE_OK)
244 {
245 WARNING ("nginx plugin: curl_easy_perform failed: %s", nginx_curl_error);
246 return (-1);
247 }
249 ptr = nginx_buffer;
250 saveptr = NULL;
251 while ((lines[lines_num] = strtok_r (ptr, "\n\r", &saveptr)) != NULL)
252 {
253 ptr = NULL;
254 lines_num++;
256 if (lines_num >= 16)
257 break;
258 }
260 /*
261 * Active connections: 291
262 * server accepts handled requests
263 * 16630948 16630948 31070465
264 * Reading: 6 Writing: 179 Waiting: 106
265 */
266 for (i = 0; i < lines_num; i++)
267 {
268 fields_num = strsplit (lines[i], fields,
269 (sizeof (fields) / sizeof (fields[0])));
271 if (fields_num == 3)
272 {
273 if ((strcmp (fields[0], "Active") == 0)
274 && (strcmp (fields[1], "connections:") == 0))
275 {
276 submit ("nginx_connections", "active", atoll (fields[2]));
277 }
278 else if ((atoll (fields[0]) != 0)
279 && (atoll (fields[1]) != 0)
280 && (atoll (fields[2]) != 0))
281 {
282 submit ("connections", "accepted", atoll (fields[0]));
283 submit ("connections", "handled", atoll (fields[1]));
284 submit ("nginx_requests", NULL, atoll (fields[2]));
285 }
286 }
287 else if (fields_num == 6)
288 {
289 if ((strcmp (fields[0], "Reading:") == 0)
290 && (strcmp (fields[2], "Writing:") == 0)
291 && (strcmp (fields[4], "Waiting:") == 0))
292 {
293 submit ("nginx_connections", "reading", atoll (fields[1]));
294 submit ("nginx_connections", "writing", atoll (fields[3]));
295 submit ("nginx_connections", "waiting", atoll (fields[5]));
296 }
297 }
298 }
300 nginx_buffer_len = 0;
302 return (0);
303 } /* int nginx_read */
305 void module_register (void)
306 {
307 plugin_register_config ("nginx", config, config_keys, config_keys_num);
308 plugin_register_init ("nginx", init);
309 plugin_register_read ("nginx", nginx_read);
310 } /* void module_register */
312 /*
313 * vim: set shiftwidth=2 softtabstop=2 tabstop=8 :
314 */