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;
43 static CURL *curl = NULL;
45 static char nginx_buffer[16384];
46 static size_t nginx_buffer_len = 0;
47 static char nginx_curl_error[CURL_ERROR_SIZE];
49 static const char *config_keys[] =
50 {
51 "URL",
52 "User",
53 "Password",
54 "VerifyPeer",
55 "VerifyHost",
56 "CACert"
57 };
58 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
60 static size_t nginx_curl_callback (void *buf, size_t size, size_t nmemb,
61 void __attribute__((unused)) *stream)
62 {
63 size_t len = size * nmemb;
65 /* Check if the data fits into the memory. If not, truncate it. */
66 if ((nginx_buffer_len + len) >= sizeof (nginx_buffer))
67 {
68 assert (sizeof (nginx_buffer) > nginx_buffer_len);
69 len = (sizeof (nginx_buffer) - 1) - nginx_buffer_len;
70 }
72 if (len <= 0)
73 return (len);
75 memcpy (&nginx_buffer[nginx_buffer_len], buf, len);
76 nginx_buffer_len += len;
77 nginx_buffer[nginx_buffer_len] = 0;
79 return (len);
80 }
82 static int config_set (char **var, const char *value)
83 {
84 if (*var != NULL)
85 {
86 free (*var);
87 *var = NULL;
88 }
90 if ((*var = strdup (value)) == NULL)
91 return (1);
92 else
93 return (0);
94 }
96 static int config (const char *key, const char *value)
97 {
98 if (strcasecmp (key, "url") == 0)
99 return (config_set (&url, value));
100 else if (strcasecmp (key, "user") == 0)
101 return (config_set (&user, value));
102 else if (strcasecmp (key, "password") == 0)
103 return (config_set (&pass, value));
104 else if (strcasecmp (key, "verifypeer") == 0)
105 return (config_set (&verify_peer, value));
106 else if (strcasecmp (key, "verifyhost") == 0)
107 return (config_set (&verify_host, value));
108 else if (strcasecmp (key, "cacert") == 0)
109 return (config_set (&cacert, value));
110 else
111 return (-1);
112 } /* int config */
114 static int init (void)
115 {
116 if (curl != NULL)
117 curl_easy_cleanup (curl);
119 if ((curl = curl_easy_init ()) == NULL)
120 {
121 ERROR ("nginx plugin: curl_easy_init failed.");
122 return (-1);
123 }
125 curl_easy_setopt (curl, CURLOPT_NOSIGNAL, 1L);
126 curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, nginx_curl_callback);
127 curl_easy_setopt (curl, CURLOPT_USERAGENT, COLLECTD_USERAGENT);
128 curl_easy_setopt (curl, CURLOPT_ERRORBUFFER, nginx_curl_error);
130 if (user != NULL)
131 {
132 #ifdef HAVE_CURLOPT_USERNAME
133 curl_easy_setopt (curl, CURLOPT_USERNAME, user);
134 curl_easy_setopt (curl, CURLOPT_PASSWORD, (pass == NULL) ? "" : pass);
135 #else
136 static char credentials[1024];
137 int status = ssnprintf (credentials, sizeof (credentials),
138 "%s:%s", user, pass == NULL ? "" : pass);
139 if ((status < 0) || ((size_t) status >= sizeof (credentials)))
140 {
141 ERROR ("nginx plugin: Credentials would have been truncated.");
142 return (-1);
143 }
145 curl_easy_setopt (curl, CURLOPT_USERPWD, credentials);
146 #endif
147 }
149 if (url != NULL)
150 {
151 curl_easy_setopt (curl, CURLOPT_URL, url);
152 }
154 curl_easy_setopt (curl, CURLOPT_FOLLOWLOCATION, 1L);
155 curl_easy_setopt (curl, CURLOPT_MAXREDIRS, 50L);
157 if ((verify_peer == NULL) || IS_TRUE (verify_peer))
158 {
159 curl_easy_setopt (curl, CURLOPT_SSL_VERIFYPEER, 1L);
160 }
161 else
162 {
163 curl_easy_setopt (curl, CURLOPT_SSL_VERIFYPEER, 0L);
164 }
166 if ((verify_host == NULL) || IS_TRUE (verify_host))
167 {
168 curl_easy_setopt (curl, CURLOPT_SSL_VERIFYHOST, 2L);
169 }
170 else
171 {
172 curl_easy_setopt (curl, CURLOPT_SSL_VERIFYHOST, 0L);
173 }
175 if (cacert != NULL)
176 {
177 curl_easy_setopt (curl, CURLOPT_CAINFO, cacert);
178 }
180 return (0);
181 } /* void init */
183 static void submit (char *type, char *inst, long long value)
184 {
185 value_t values[1];
186 value_list_t vl = VALUE_LIST_INIT;
188 if (strcmp (type, "nginx_connections") == 0)
189 values[0].gauge = value;
190 else if (strcmp (type, "nginx_requests") == 0)
191 values[0].derive = value;
192 else if (strcmp (type, "connections") == 0)
193 values[0].derive = value;
194 else
195 return;
197 vl.values = values;
198 vl.values_len = 1;
199 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
200 sstrncpy (vl.plugin, "nginx", sizeof (vl.plugin));
201 sstrncpy (vl.plugin_instance, "", sizeof (vl.plugin_instance));
202 sstrncpy (vl.type, type, sizeof (vl.type));
204 if (inst != NULL)
205 sstrncpy (vl.type_instance, inst, sizeof (vl.type_instance));
207 plugin_dispatch_values (&vl);
208 } /* void submit */
210 static int nginx_read (void)
211 {
212 int i;
214 char *ptr;
215 char *lines[16];
216 int lines_num = 0;
217 char *saveptr;
219 char *fields[16];
220 int fields_num;
222 if (curl == NULL)
223 return (-1);
224 if (url == NULL)
225 return (-1);
227 nginx_buffer_len = 0;
228 if (curl_easy_perform (curl) != CURLE_OK)
229 {
230 WARNING ("nginx plugin: curl_easy_perform failed: %s", nginx_curl_error);
231 return (-1);
232 }
234 ptr = nginx_buffer;
235 saveptr = NULL;
236 while ((lines[lines_num] = strtok_r (ptr, "\n\r", &saveptr)) != NULL)
237 {
238 ptr = NULL;
239 lines_num++;
241 if (lines_num >= 16)
242 break;
243 }
245 /*
246 * Active connections: 291
247 * server accepts handled requests
248 * 16630948 16630948 31070465
249 * Reading: 6 Writing: 179 Waiting: 106
250 */
251 for (i = 0; i < lines_num; i++)
252 {
253 fields_num = strsplit (lines[i], fields,
254 (sizeof (fields) / sizeof (fields[0])));
256 if (fields_num == 3)
257 {
258 if ((strcmp (fields[0], "Active") == 0)
259 && (strcmp (fields[1], "connections:") == 0))
260 {
261 submit ("nginx_connections", "active", atoll (fields[2]));
262 }
263 else if ((atoll (fields[0]) != 0)
264 && (atoll (fields[1]) != 0)
265 && (atoll (fields[2]) != 0))
266 {
267 submit ("connections", "accepted", atoll (fields[0]));
268 submit ("connections", "handled", atoll (fields[1]));
269 submit ("nginx_requests", NULL, atoll (fields[2]));
270 }
271 }
272 else if (fields_num == 6)
273 {
274 if ((strcmp (fields[0], "Reading:") == 0)
275 && (strcmp (fields[2], "Writing:") == 0)
276 && (strcmp (fields[4], "Waiting:") == 0))
277 {
278 submit ("nginx_connections", "reading", atoll (fields[1]));
279 submit ("nginx_connections", "writing", atoll (fields[3]));
280 submit ("nginx_connections", "waiting", atoll (fields[5]));
281 }
282 }
283 }
285 nginx_buffer_len = 0;
287 return (0);
288 } /* int nginx_read */
290 void module_register (void)
291 {
292 plugin_register_config ("nginx", config, config_keys, config_keys_num);
293 plugin_register_init ("nginx", init);
294 plugin_register_read ("nginx", nginx_read);
295 } /* void module_register */
297 /*
298 * vim: set shiftwidth=2 softtabstop=2 tabstop=8 :
299 */