dfa4f9832f0252acdfe789d5effc7e560ebcaa4c
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 **/
23 #include "collectd.h"
24 #include "common.h"
25 #include "plugin.h"
26 #include "configfile.h"
28 #include <curl/curl.h>
30 static char *url = NULL;
31 static char *user = NULL;
32 static char *pass = NULL;
33 static char *verify_peer = NULL;
34 static char *verify_host = NULL;
35 static char *cacert = NULL;
37 static CURL *curl = NULL;
39 #define ABUFFER_SIZE 16384
40 static char nginx_buffer[ABUFFER_SIZE];
41 static int nginx_buffer_len = 0;
42 static char nginx_curl_error[CURL_ERROR_SIZE];
44 static const char *config_keys[] =
45 {
46 "URL",
47 "User",
48 "Password",
49 "VerifyPeer",
50 "VerifyHost",
51 "CACert"
52 };
53 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
55 static size_t nginx_curl_callback (void *buf, size_t size, size_t nmemb,
56 void __attribute__((unused)) *stream)
57 {
58 size_t len = size * nmemb;
60 if ((nginx_buffer_len + len) >= ABUFFER_SIZE)
61 {
62 len = (ABUFFER_SIZE - 1) - nginx_buffer_len;
63 }
65 if (len <= 0)
66 return (len);
68 memcpy (nginx_buffer + nginx_buffer_len, (char *) buf, len);
69 nginx_buffer_len += len;
70 nginx_buffer[nginx_buffer_len] = '\0';
72 return (len);
73 }
75 static int config_set (char **var, const char *value)
76 {
77 if (*var != NULL)
78 {
79 free (*var);
80 *var = NULL;
81 }
83 if ((*var = strdup (value)) == NULL)
84 return (1);
85 else
86 return (0);
87 }
89 static int config (const char *key, const char *value)
90 {
91 if (strcasecmp (key, "url") == 0)
92 return (config_set (&url, value));
93 else if (strcasecmp (key, "user") == 0)
94 return (config_set (&user, value));
95 else if (strcasecmp (key, "password") == 0)
96 return (config_set (&pass, value));
97 else if (strcasecmp (key, "verifypeer") == 0)
98 return (config_set (&verify_peer, value));
99 else if (strcasecmp (key, "verifyhost") == 0)
100 return (config_set (&verify_host, value));
101 else if (strcasecmp (key, "cacert") == 0)
102 return (config_set (&cacert, value));
103 else
104 return (-1);
105 } /* int config */
107 static int init (void)
108 {
109 static char credentials[1024];
111 if (curl != NULL)
112 curl_easy_cleanup (curl);
114 if ((curl = curl_easy_init ()) == NULL)
115 {
116 ERROR ("nginx plugin: curl_easy_init failed.");
117 return (-1);
118 }
120 curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, nginx_curl_callback);
121 curl_easy_setopt (curl, CURLOPT_USERAGENT, PACKAGE_NAME"/"PACKAGE_VERSION);
122 curl_easy_setopt (curl, CURLOPT_ERRORBUFFER, nginx_curl_error);
124 if (user != NULL)
125 {
126 if (ssnprintf (credentials, sizeof (credentials),
127 "%s:%s", user, pass == NULL ? "" : pass) >= sizeof (credentials))
128 {
129 ERROR ("nginx plugin: Credentials would have been truncated.");
130 return (-1);
131 }
133 curl_easy_setopt (curl, CURLOPT_USERPWD, credentials);
134 }
136 if (url != NULL)
137 {
138 curl_easy_setopt (curl, CURLOPT_URL, url);
139 }
141 if ((verify_peer == NULL) || (strcmp (verify_peer, "true") == 0))
142 {
143 curl_easy_setopt (curl, CURLOPT_SSL_VERIFYPEER, 1);
144 }
145 else
146 {
147 curl_easy_setopt (curl, CURLOPT_SSL_VERIFYPEER, 0);
148 }
150 if ((verify_host == NULL) || (strcmp (verify_host, "true") == 0))
151 {
152 curl_easy_setopt (curl, CURLOPT_SSL_VERIFYHOST, 2);
153 }
154 else
155 {
156 curl_easy_setopt (curl, CURLOPT_SSL_VERIFYHOST, 0);
157 }
159 if (cacert != NULL)
160 {
161 curl_easy_setopt (curl, CURLOPT_CAINFO, cacert);
162 }
164 return (0);
165 } /* void init */
167 static void submit (char *type, char *inst, long long value)
168 {
169 value_t values[1];
170 value_list_t vl = VALUE_LIST_INIT;
172 if (strcmp (type, "nginx_connections") == 0)
173 values[0].gauge = value;
174 else if (strcmp (type, "nginx_requests") == 0)
175 values[0].counter = value;
176 else
177 return;
179 vl.values = values;
180 vl.values_len = 1;
181 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
182 sstrncpy (vl.plugin, "nginx", sizeof (vl.plugin));
183 sstrncpy (vl.plugin_instance, "", sizeof (vl.plugin_instance));
184 sstrncpy (vl.type, type, sizeof (vl.type));
186 if (inst != NULL)
187 sstrncpy (vl.type_instance, inst, sizeof (vl.type_instance));
189 plugin_dispatch_values (&vl);
190 } /* void submit */
192 static int nginx_read (void)
193 {
194 int i;
196 char *ptr;
197 char *lines[16];
198 int lines_num = 0;
199 char *saveptr;
201 char *fields[16];
202 int fields_num;
204 if (curl == NULL)
205 return (-1);
206 if (url == NULL)
207 return (-1);
209 nginx_buffer_len = 0;
210 if (curl_easy_perform (curl) != 0)
211 {
212 WARNING ("nginx plugin: curl_easy_perform failed: %s", nginx_curl_error);
213 return (-1);
214 }
216 ptr = nginx_buffer;
217 saveptr = NULL;
218 while ((lines[lines_num] = strtok_r (ptr, "\n\r", &saveptr)) != NULL)
219 {
220 ptr = NULL;
221 lines_num++;
223 if (lines_num >= 16)
224 break;
225 }
227 /*
228 * Active connections: 291
229 * server accepts handled requests
230 * 16630948 16630948 31070465
231 * Reading: 6 Writing: 179 Waiting: 106
232 */
233 for (i = 0; i < lines_num; i++)
234 {
235 fields_num = strsplit (lines[i], fields,
236 (sizeof (fields) / sizeof (fields[0])));
238 if (fields_num == 3)
239 {
240 if ((strcmp (fields[0], "Active") == 0)
241 && (strcmp (fields[1], "connections:") == 0))
242 {
243 submit ("nginx_connections", "active", atoll (fields[2]));
244 }
245 else if ((atoll (fields[0]) != 0)
246 && (atoll (fields[1]) != 0)
247 && (atoll (fields[2]) != 0))
248 {
249 submit ("nginx_requests", NULL, atoll (fields[2]));
250 }
251 }
252 else if (fields_num == 6)
253 {
254 if ((strcmp (fields[0], "Reading:") == 0)
255 && (strcmp (fields[2], "Writing:") == 0)
256 && (strcmp (fields[4], "Waiting:") == 0))
257 {
258 submit ("nginx_connections", "reading", atoll (fields[1]));
259 submit ("nginx_connections", "writing", atoll (fields[3]));
260 submit ("nginx_connections", "waiting", atoll (fields[5]));
261 }
262 }
263 }
265 nginx_buffer_len = 0;
267 return (0);
268 } /* int nginx_read */
270 void module_register (void)
271 {
272 plugin_register_config ("nginx", config, config_keys, config_keys_num);
273 plugin_register_init ("nginx", init);
274 plugin_register_read ("nginx", nginx_read);
275 } /* void module_register */
277 /*
278 * vim: set shiftwidth=2 softtabstop=2 tabstop=8 :
279 */