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 "utils_debug.h"
27 #include "configfile.h"
29 #define MODULE_NAME "nginx"
31 #if HAVE_LIBCURL && HAVE_CURL_CURL_H
32 # define NGINX_HAVE_READ 1
33 # include <curl/curl.h>
35 # define NGINX_HAVE_READ 0
38 static char *url = NULL;
39 static char *user = NULL;
40 static char *pass = NULL;
41 static char *cacert = NULL;
44 static CURL *curl = NULL;
46 #define ABUFFER_SIZE 16384
47 static char nginx_buffer[ABUFFER_SIZE];
48 static int nginx_buffer_len = 0;
49 static char nginx_curl_error[CURL_ERROR_SIZE];
50 #endif /* HAVE_LIBCURL */
52 static char *connections_file = "nginx/nginx_connections-%s.rrd";
53 static char *connections_ds_def[] =
55 "DS:value:GAUGE:"COLLECTD_HEARTBEAT":0:U",
58 static int connections_ds_num = 1;
60 /* Limit to 2^20 requests/s */
61 static char *requests_file = "nginx/nginx_requests.rrd";
62 static char *requests_ds_def[] =
64 "DS:value:COUNTER:"COLLECTD_HEARTBEAT":0:1048576",
67 static int requests_ds_num = 1;
69 static char *config_keys[] =
77 static int config_keys_num = 4;
80 static size_t nginx_curl_callback (void *buf, size_t size, size_t nmemb, void *stream)
82 size_t len = size * nmemb;
84 if ((nginx_buffer_len + len) >= ABUFFER_SIZE)
86 len = (ABUFFER_SIZE - 1) - nginx_buffer_len;
92 memcpy (nginx_buffer + nginx_buffer_len, (char *) buf, len);
93 nginx_buffer_len += len;
94 nginx_buffer[nginx_buffer_len] = '\0';
98 #endif /* HAVE_LIBCURL */
100 static int config_set (char **var, char *value)
108 if ((*var = strdup (value)) == NULL)
114 static int config (char *key, char *value)
116 if (strcasecmp (key, "url") == 0)
117 return (config_set (&url, value));
118 else if (strcasecmp (key, "user") == 0)
119 return (config_set (&user, value));
120 else if (strcasecmp (key, "password") == 0)
121 return (config_set (&pass, value));
122 else if (strcasecmp (key, "cacert") == 0)
123 return (config_set (&cacert, value));
128 static void init (void)
131 static char credentials[1024];
135 curl_easy_cleanup (curl);
138 if ((curl = curl_easy_init ()) == NULL)
140 syslog (LOG_ERR, "nginx: `curl_easy_init' failed.");
144 curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, nginx_curl_callback);
145 curl_easy_setopt (curl, CURLOPT_USERAGENT, PACKAGE_NAME"/"PACKAGE_VERSION);
146 curl_easy_setopt (curl, CURLOPT_ERRORBUFFER, nginx_curl_error);
150 if (snprintf (credentials, 1024, "%s:%s", user, pass == NULL ? "" : pass) >= 1024)
152 syslog (LOG_ERR, "nginx: Credentials would have been truncated.");
156 curl_easy_setopt (curl, CURLOPT_USERPWD, credentials);
161 curl_easy_setopt (curl, CURLOPT_URL, url);
166 curl_easy_setopt (curl, CURLOPT_CAINFO, cacert);
168 #endif /* HAVE_LIBCURL */
171 static void connections_write (char *host, char *inst, char *val)
175 if (snprintf (buf, 1024, connections_file, inst) >= 1024)
178 rrd_update_file (host, buf, val,
179 connections_ds_def, connections_ds_num);
182 static void requests_write (char *host, char *inst, char *val)
184 rrd_update_file (host, requests_file, val,
185 requests_ds_def, requests_ds_num);
189 static void submit (char *type, char *inst, long long value)
194 DBG ("type = %s; inst = %s; value = %lli;",
195 type, (inst == NULL) ? "(nil)" : inst, value);
197 status = snprintf (buf, 1024, "%u:%lli", (unsigned int) curtime, value);
198 if ((status < 0) || (status >= 1024))
200 syslog (LOG_ERR, "nginx: snprintf failed");
204 plugin_submit (type, inst, buf);
207 static void nginx_read (void)
223 nginx_buffer_len = 0;
224 if (curl_easy_perform (curl) != 0)
226 syslog (LOG_WARNING, "nginx: curl_easy_perform failed: %s", nginx_curl_error);
231 while ((lines[lines_num] = strtok (ptr, "\n\r")) != NULL)
241 * Active connections: 291
242 * server accepts handled requests
243 * 16630948 16630948 31070465
244 * Reading: 6 Writing: 179 Waiting: 106
246 for (i = 0; i < lines_num; i++)
248 fields_num = strsplit (lines[i], fields,
249 (sizeof (fields) / sizeof (fields[0])));
253 if ((strcmp (fields[0], "Active") == 0)
254 && (strcmp (fields[1], "connections:") == 0))
256 submit ("nginx_connections", "active", atoll (fields[2]));
258 else if ((atoll (fields[0]) != 0)
259 && (atoll (fields[1]) != 0)
260 && (atoll (fields[2]) != 0))
262 submit ("nginx_requests", NULL, atoll (fields[2]));
265 else if (fields_num == 6)
267 if ((strcmp (fields[0], "Reading:") == 0)
268 && (strcmp (fields[2], "Writing:") == 0)
269 && (strcmp (fields[4], "Waiting:") == 0))
271 submit ("nginx_connections", "reading", atoll (fields[1]));
272 submit ("nginx_connections", "writing", atoll (fields[3]));
273 submit ("nginx_connections", "waiting", atoll (fields[5]));
278 nginx_buffer_len = 0;
281 # define nginx_read NULL
282 #endif /* NGINX_HAVE_READ */
284 void module_register (void)
286 plugin_register (MODULE_NAME, init, nginx_read, NULL);
287 plugin_register ("nginx_requests", NULL, NULL, requests_write);
288 plugin_register ("nginx_connections", NULL, NULL, connections_write);
289 cf_register (MODULE_NAME, config, config_keys, config_keys_num);
295 * vim: set shiftwidth=2 softtabstop=2 tabstop=8 :