2 * collectd - src/nginx.c
3 * Copyright (C) 2006,2007 Florian octo Forster
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.
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.
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
20 * Florian octo Forster <octo at verplant.org>
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[] =
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)
53 size_t len = size * nmemb;
55 if ((nginx_buffer_len + len) >= ABUFFER_SIZE)
57 len = (ABUFFER_SIZE - 1) - nginx_buffer_len;
63 memcpy (nginx_buffer + nginx_buffer_len, (char *) buf, len);
64 nginx_buffer_len += len;
65 nginx_buffer[nginx_buffer_len] = '\0';
70 static int config_set (char **var, const char *value)
78 if ((*var = strdup (value)) == NULL)
84 static int config (const char *key, const char *value)
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));
98 static int init (void)
100 static char credentials[1024];
103 curl_easy_cleanup (curl);
105 if ((curl = curl_easy_init ()) == NULL)
107 ERROR ("nginx plugin: curl_easy_init failed.");
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);
117 if (snprintf (credentials, 1024, "%s:%s", user, pass == NULL ? "" : pass) >= 1024)
119 ERROR ("nginx plugin: Credentials would have been truncated.");
123 curl_easy_setopt (curl, CURLOPT_USERPWD, credentials);
128 curl_easy_setopt (curl, CURLOPT_URL, url);
133 curl_easy_setopt (curl, CURLOPT_CAINFO, cacert);
139 static void submit (char *type, char *inst, long long value)
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;
153 vl.time = time (NULL);
154 strcpy (vl.host, hostname_g);
155 strcpy (vl.plugin, "nginx");
156 strcpy (vl.plugin_instance, "");
160 strncpy (vl.type_instance, inst, sizeof (vl.type_instance));
161 vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
164 plugin_dispatch_values (type, &vl);
167 static int nginx_read (void)
183 nginx_buffer_len = 0;
184 if (curl_easy_perform (curl) != 0)
186 WARNING ("nginx plugin: curl_easy_perform failed: %s", nginx_curl_error);
191 while ((lines[lines_num] = strtok (ptr, "\n\r")) != NULL)
201 * Active connections: 291
202 * server accepts handled requests
203 * 16630948 16630948 31070465
204 * Reading: 6 Writing: 179 Waiting: 106
206 for (i = 0; i < lines_num; i++)
208 fields_num = strsplit (lines[i], fields,
209 (sizeof (fields) / sizeof (fields[0])));
213 if ((strcmp (fields[0], "Active") == 0)
214 && (strcmp (fields[1], "connections:") == 0))
216 submit ("nginx_connections", "active", atoll (fields[2]));
218 else if ((atoll (fields[0]) != 0)
219 && (atoll (fields[1]) != 0)
220 && (atoll (fields[2]) != 0))
222 submit ("nginx_requests", NULL, atoll (fields[2]));
225 else if (fields_num == 6)
227 if ((strcmp (fields[0], "Reading:") == 0)
228 && (strcmp (fields[2], "Writing:") == 0)
229 && (strcmp (fields[4], "Waiting:") == 0))
231 submit ("nginx_connections", "reading", atoll (fields[1]));
232 submit ("nginx_connections", "writing", atoll (fields[3]));
233 submit ("nginx_connections", "waiting", atoll (fields[5]));
238 nginx_buffer_len = 0;
241 } /* int nginx_read */
243 void module_register (void)
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 */
251 * vim: set shiftwidth=2 softtabstop=2 tabstop=8 :