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 (strcmp (type, "nginx_connections") == 0)
145 values[0].gauge = value;
146 else if (strcmp (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 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
155 sstrncpy (vl.plugin, "nginx", sizeof (vl.plugin));
156 sstrncpy (vl.plugin_instance, "", sizeof (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;
174 char *saveptr;
176 char *fields[16];
177 int fields_num;
179 if (curl == NULL)
180 return (-1);
181 if (url == NULL)
182 return (-1);
184 nginx_buffer_len = 0;
185 if (curl_easy_perform (curl) != 0)
186 {
187 WARNING ("nginx plugin: curl_easy_perform failed: %s", nginx_curl_error);
188 return (-1);
189 }
191 ptr = nginx_buffer;
192 saveptr = NULL;
193 while ((lines[lines_num] = strtok_r (ptr, "\n\r", &saveptr)) != NULL)
194 {
195 ptr = NULL;
196 lines_num++;
198 if (lines_num >= 16)
199 break;
200 }
202 /*
203 * Active connections: 291
204 * server accepts handled requests
205 * 16630948 16630948 31070465
206 * Reading: 6 Writing: 179 Waiting: 106
207 */
208 for (i = 0; i < lines_num; i++)
209 {
210 fields_num = strsplit (lines[i], fields,
211 (sizeof (fields) / sizeof (fields[0])));
213 if (fields_num == 3)
214 {
215 if ((strcmp (fields[0], "Active") == 0)
216 && (strcmp (fields[1], "connections:") == 0))
217 {
218 submit ("nginx_connections", "active", atoll (fields[2]));
219 }
220 else if ((atoll (fields[0]) != 0)
221 && (atoll (fields[1]) != 0)
222 && (atoll (fields[2]) != 0))
223 {
224 submit ("nginx_requests", NULL, atoll (fields[2]));
225 }
226 }
227 else if (fields_num == 6)
228 {
229 if ((strcmp (fields[0], "Reading:") == 0)
230 && (strcmp (fields[2], "Writing:") == 0)
231 && (strcmp (fields[4], "Waiting:") == 0))
232 {
233 submit ("nginx_connections", "reading", atoll (fields[1]));
234 submit ("nginx_connections", "writing", atoll (fields[3]));
235 submit ("nginx_connections", "waiting", atoll (fields[5]));
236 }
237 }
238 }
240 nginx_buffer_len = 0;
242 return (0);
243 } /* int nginx_read */
245 void module_register (void)
246 {
247 plugin_register_config ("nginx", config, config_keys, config_keys_num);
248 plugin_register_init ("nginx", init);
249 plugin_register_read ("nginx", nginx_read);
250 } /* void module_register */
252 /*
253 * vim: set shiftwidth=2 softtabstop=2 tabstop=8 :
254 */