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 *cacert = NULL;
35 static CURL *curl = NULL;
37 #define ABUFFER_SIZE 16384
38 static char nginx_buffer[ABUFFER_SIZE];
39 static int nginx_buffer_len = 0;
40 static char nginx_curl_error[CURL_ERROR_SIZE];
42 static const char *config_keys[] =
43 {
44 "URL",
45 "User",
46 "Password",
47 "CACert"
48 };
49 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
51 static size_t nginx_curl_callback (void *buf, size_t size, size_t nmemb, void *stream)
52 {
53 size_t len = size * nmemb;
55 if ((nginx_buffer_len + len) >= ABUFFER_SIZE)
56 {
57 len = (ABUFFER_SIZE - 1) - nginx_buffer_len;
58 }
60 if (len <= 0)
61 return (len);
63 memcpy (nginx_buffer + nginx_buffer_len, (char *) buf, len);
64 nginx_buffer_len += len;
65 nginx_buffer[nginx_buffer_len] = '\0';
67 return (len);
68 }
70 static int config_set (char **var, const char *value)
71 {
72 if (*var != NULL)
73 {
74 free (*var);
75 *var = NULL;
76 }
78 if ((*var = strdup (value)) == NULL)
79 return (1);
80 else
81 return (0);
82 }
84 static int config (const char *key, const char *value)
85 {
86 if (strcasecmp (key, "url") == 0)
87 return (config_set (&url, value));
88 else if (strcasecmp (key, "user") == 0)
89 return (config_set (&user, value));
90 else if (strcasecmp (key, "password") == 0)
91 return (config_set (&pass, value));
92 else if (strcasecmp (key, "cacert") == 0)
93 return (config_set (&cacert, value));
94 else
95 return (-1);
96 } /* int config */
98 static int init (void)
99 {
100 static char credentials[1024];
102 if (curl != NULL)
103 curl_easy_cleanup (curl);
105 if ((curl = curl_easy_init ()) == NULL)
106 {
107 ERROR ("nginx plugin: curl_easy_init failed.");
108 return (-1);
109 }
111 curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, nginx_curl_callback);
112 curl_easy_setopt (curl, CURLOPT_USERAGENT, PACKAGE_NAME"/"PACKAGE_VERSION);
113 curl_easy_setopt (curl, CURLOPT_ERRORBUFFER, nginx_curl_error);
115 if (user != NULL)
116 {
117 if (snprintf (credentials, 1024, "%s:%s", user, pass == NULL ? "" : pass) >= 1024)
118 {
119 ERROR ("nginx plugin: Credentials would have been truncated.");
120 return (-1);
121 }
123 curl_easy_setopt (curl, CURLOPT_USERPWD, credentials);
124 }
126 if (url != NULL)
127 {
128 curl_easy_setopt (curl, CURLOPT_URL, url);
129 }
131 if (cacert != NULL)
132 {
133 curl_easy_setopt (curl, CURLOPT_CAINFO, cacert);
134 }
136 return (0);
137 } /* void init */
139 static void submit (char *type, char *inst, long long value)
140 {
141 value_t values[1];
142 value_list_t vl = VALUE_LIST_INIT;
144 if (strcpy (type, "nginx_connections") == 0)
145 values[0].gauge = value;
146 else if (strcpy (type, "nginx_requests") == 0)
147 values[0].counter = value;
148 else
149 return;
151 vl.values = values;
152 vl.values_len = 1;
153 vl.time = time (NULL);
154 strcpy (vl.host, hostname_g);
155 strcpy (vl.plugin, "nginx");
156 strcpy (vl.plugin_instance, "");
158 if (inst != NULL)
159 {
160 strncpy (vl.type_instance, inst, sizeof (vl.type_instance));
161 vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
162 }
164 plugin_dispatch_values (type, &vl);
165 } /* void submit */
167 static int nginx_read (void)
168 {
169 int i;
171 char *ptr;
172 char *lines[16];
173 int lines_num = 0;
175 char *fields[16];
176 int fields_num;
178 if (curl == NULL)
179 return (-1);
180 if (url == NULL)
181 return (-1);
183 nginx_buffer_len = 0;
184 if (curl_easy_perform (curl) != 0)
185 {
186 WARNING ("nginx plugin: curl_easy_perform failed: %s", nginx_curl_error);
187 return (-1);
188 }
190 ptr = nginx_buffer;
191 while ((lines[lines_num] = strtok (ptr, "\n\r")) != NULL)
192 {
193 ptr = NULL;
194 lines_num++;
196 if (lines_num >= 16)
197 break;
198 }
200 /*
201 * Active connections: 291
202 * server accepts handled requests
203 * 16630948 16630948 31070465
204 * Reading: 6 Writing: 179 Waiting: 106
205 */
206 for (i = 0; i < lines_num; i++)
207 {
208 fields_num = strsplit (lines[i], fields,
209 (sizeof (fields) / sizeof (fields[0])));
211 if (fields_num == 3)
212 {
213 if ((strcmp (fields[0], "Active") == 0)
214 && (strcmp (fields[1], "connections:") == 0))
215 {
216 submit ("nginx_connections", "active", atoll (fields[2]));
217 }
218 else if ((atoll (fields[0]) != 0)
219 && (atoll (fields[1]) != 0)
220 && (atoll (fields[2]) != 0))
221 {
222 submit ("nginx_requests", NULL, atoll (fields[2]));
223 }
224 }
225 else if (fields_num == 6)
226 {
227 if ((strcmp (fields[0], "Reading:") == 0)
228 && (strcmp (fields[2], "Writing:") == 0)
229 && (strcmp (fields[4], "Waiting:") == 0))
230 {
231 submit ("nginx_connections", "reading", atoll (fields[1]));
232 submit ("nginx_connections", "writing", atoll (fields[3]));
233 submit ("nginx_connections", "waiting", atoll (fields[5]));
234 }
235 }
236 }
238 nginx_buffer_len = 0;
240 return (0);
241 } /* int nginx_read */
243 void module_register (void)
244 {
245 plugin_register_config ("nginx", config, config_keys, config_keys_num);
246 plugin_register_init ("nginx", init);
247 plugin_register_read ("nginx", nginx_read);
248 } /* void module_register */
250 /*
251 * vim: set shiftwidth=2 softtabstop=2 tabstop=8 :
252 */