1 /**
2 * collectd - src/nginx.c
3 * Copyright (C) 2006-2010 Florian octo Forster
4 * Copyright (C) 2008 Sebastian Harl
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; either version 2 of the License, or (at your
9 * option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 *
20 * Authors:
21 * Florian octo Forster <octo at collectd.org>
22 * Sebastian Harl <sh at tokkee.org>
23 **/
25 #include "collectd.h"
26 #include "common.h"
27 #include "plugin.h"
28 #include "configfile.h"
30 #include <curl/curl.h>
32 static char *url = NULL;
33 static char *user = NULL;
34 static char *pass = NULL;
35 static char *verify_peer = NULL;
36 static char *verify_host = NULL;
37 static char *cacert = NULL;
39 static CURL *curl = NULL;
41 static char nginx_buffer[16384];
42 static size_t nginx_buffer_len = 0;
43 static char nginx_curl_error[CURL_ERROR_SIZE];
45 static const char *config_keys[] =
46 {
47 "URL",
48 "User",
49 "Password",
50 "VerifyPeer",
51 "VerifyHost",
52 "CACert"
53 };
54 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
56 static size_t nginx_curl_callback (void *buf, size_t size, size_t nmemb,
57 void __attribute__((unused)) *stream)
58 {
59 size_t len = size * nmemb;
61 /* Check if the data fits into the memory. If not, truncate it. */
62 if ((nginx_buffer_len + len) >= sizeof (nginx_buffer))
63 {
64 assert (sizeof (nginx_buffer) > nginx_buffer_len);
65 len = (sizeof (nginx_buffer) - 1) - nginx_buffer_len;
66 }
68 if (len <= 0)
69 return (len);
71 memcpy (&nginx_buffer[nginx_buffer_len], buf, len);
72 nginx_buffer_len += len;
73 nginx_buffer[nginx_buffer_len] = 0;
75 return (len);
76 }
78 static int config_set (char **var, const char *value)
79 {
80 if (*var != NULL)
81 {
82 free (*var);
83 *var = NULL;
84 }
86 if ((*var = strdup (value)) == NULL)
87 return (1);
88 else
89 return (0);
90 }
92 static int config (const char *key, const char *value)
93 {
94 if (strcasecmp (key, "url") == 0)
95 return (config_set (&url, value));
96 else if (strcasecmp (key, "user") == 0)
97 return (config_set (&user, value));
98 else if (strcasecmp (key, "password") == 0)
99 return (config_set (&pass, value));
100 else if (strcasecmp (key, "verifypeer") == 0)
101 return (config_set (&verify_peer, value));
102 else if (strcasecmp (key, "verifyhost") == 0)
103 return (config_set (&verify_host, value));
104 else if (strcasecmp (key, "cacert") == 0)
105 return (config_set (&cacert, value));
106 else
107 return (-1);
108 } /* int config */
110 static int init (void)
111 {
112 static char credentials[1024];
114 if (curl != NULL)
115 curl_easy_cleanup (curl);
117 if ((curl = curl_easy_init ()) == NULL)
118 {
119 ERROR ("nginx plugin: curl_easy_init failed.");
120 return (-1);
121 }
123 curl_easy_setopt (curl, CURLOPT_NOSIGNAL, 1L);
124 curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, nginx_curl_callback);
125 curl_easy_setopt (curl, CURLOPT_USERAGENT, COLLECTD_USERAGENT);
126 curl_easy_setopt (curl, CURLOPT_ERRORBUFFER, nginx_curl_error);
128 if (user != NULL)
129 {
130 int status = ssnprintf (credentials, sizeof (credentials),
131 "%s:%s", user, pass == NULL ? "" : pass);
132 if ((status < 0) || ((size_t) status >= sizeof (credentials)))
133 {
134 ERROR ("nginx plugin: Credentials would have been truncated.");
135 return (-1);
136 }
138 curl_easy_setopt (curl, CURLOPT_USERPWD, credentials);
139 }
141 if (url != NULL)
142 {
143 curl_easy_setopt (curl, CURLOPT_URL, url);
144 }
146 curl_easy_setopt (curl, CURLOPT_FOLLOWLOCATION, 1L);
147 curl_easy_setopt (curl, CURLOPT_MAXREDIRS, 50L);
149 if ((verify_peer == NULL) || IS_TRUE (verify_peer))
150 {
151 curl_easy_setopt (curl, CURLOPT_SSL_VERIFYPEER, 1L);
152 }
153 else
154 {
155 curl_easy_setopt (curl, CURLOPT_SSL_VERIFYPEER, 0L);
156 }
158 if ((verify_host == NULL) || IS_TRUE (verify_host))
159 {
160 curl_easy_setopt (curl, CURLOPT_SSL_VERIFYHOST, 2L);
161 }
162 else
163 {
164 curl_easy_setopt (curl, CURLOPT_SSL_VERIFYHOST, 0L);
165 }
167 if (cacert != NULL)
168 {
169 curl_easy_setopt (curl, CURLOPT_CAINFO, cacert);
170 }
172 return (0);
173 } /* void init */
175 static void submit (char *type, char *inst, long long value)
176 {
177 value_t values[1];
178 value_list_t vl = VALUE_LIST_INIT;
180 if (strcmp (type, "nginx_connections") == 0)
181 values[0].gauge = value;
182 else if (strcmp (type, "nginx_requests") == 0)
183 values[0].derive = value;
184 else if (strcmp (type, "connections") == 0)
185 values[0].derive = value;
186 else
187 return;
189 vl.values = values;
190 vl.values_len = 1;
191 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
192 sstrncpy (vl.plugin, "nginx", sizeof (vl.plugin));
193 sstrncpy (vl.plugin_instance, "", sizeof (vl.plugin_instance));
194 sstrncpy (vl.type, type, sizeof (vl.type));
196 if (inst != NULL)
197 sstrncpy (vl.type_instance, inst, sizeof (vl.type_instance));
199 plugin_dispatch_values (&vl);
200 } /* void submit */
202 static int nginx_read (void)
203 {
204 int i;
206 char *ptr;
207 char *lines[16];
208 int lines_num = 0;
209 char *saveptr;
211 char *fields[16];
212 int fields_num;
214 if (curl == NULL)
215 return (-1);
216 if (url == NULL)
217 return (-1);
219 nginx_buffer_len = 0;
220 if (curl_easy_perform (curl) != CURLE_OK)
221 {
222 WARNING ("nginx plugin: curl_easy_perform failed: %s", nginx_curl_error);
223 return (-1);
224 }
226 ptr = nginx_buffer;
227 saveptr = NULL;
228 while ((lines[lines_num] = strtok_r (ptr, "\n\r", &saveptr)) != NULL)
229 {
230 ptr = NULL;
231 lines_num++;
233 if (lines_num >= 16)
234 break;
235 }
237 /*
238 * Active connections: 291
239 * server accepts handled requests
240 * 16630948 16630948 31070465
241 * Reading: 6 Writing: 179 Waiting: 106
242 */
243 for (i = 0; i < lines_num; i++)
244 {
245 fields_num = strsplit (lines[i], fields,
246 (sizeof (fields) / sizeof (fields[0])));
248 if (fields_num == 3)
249 {
250 if ((strcmp (fields[0], "Active") == 0)
251 && (strcmp (fields[1], "connections:") == 0))
252 {
253 submit ("nginx_connections", "active", atoll (fields[2]));
254 }
255 else if ((atoll (fields[0]) != 0)
256 && (atoll (fields[1]) != 0)
257 && (atoll (fields[2]) != 0))
258 {
259 submit ("connections", "accepted", atoll (fields[0]));
260 submit ("connections", "handled", atoll (fields[1]));
261 submit ("nginx_requests", NULL, atoll (fields[2]));
262 }
263 }
264 else if (fields_num == 6)
265 {
266 if ((strcmp (fields[0], "Reading:") == 0)
267 && (strcmp (fields[2], "Writing:") == 0)
268 && (strcmp (fields[4], "Waiting:") == 0))
269 {
270 submit ("nginx_connections", "reading", atoll (fields[1]));
271 submit ("nginx_connections", "writing", atoll (fields[3]));
272 submit ("nginx_connections", "waiting", atoll (fields[5]));
273 }
274 }
275 }
277 nginx_buffer_len = 0;
279 return (0);
280 } /* int nginx_read */
282 void module_register (void)
283 {
284 plugin_register_config ("nginx", config, config_keys, config_keys_num);
285 plugin_register_init ("nginx", init);
286 plugin_register_read ("nginx", nginx_read);
287 } /* void module_register */
289 /*
290 * vim: set shiftwidth=2 softtabstop=2 tabstop=8 :
291 */