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 *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[] =
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, void *stream)
57 size_t len = size * nmemb;
59 if ((nginx_buffer_len + len) >= ABUFFER_SIZE)
61 len = (ABUFFER_SIZE - 1) - nginx_buffer_len;
67 memcpy (nginx_buffer + nginx_buffer_len, (char *) buf, len);
68 nginx_buffer_len += len;
69 nginx_buffer[nginx_buffer_len] = '\0';
74 static int config_set (char **var, const char *value)
82 if ((*var = strdup (value)) == NULL)
88 static int config (const char *key, const char *value)
90 if (strcasecmp (key, "url") == 0)
91 return (config_set (&url, value));
92 else if (strcasecmp (key, "user") == 0)
93 return (config_set (&user, value));
94 else if (strcasecmp (key, "password") == 0)
95 return (config_set (&pass, value));
96 else if (strcasecmp (key, "verifypeer") == 0)
97 return (config_set (&verify_peer, value));
98 else if (strcasecmp (key, "verifyhost") == 0)
99 return (config_set (&verify_host, value));
100 else if (strcasecmp (key, "cacert") == 0)
101 return (config_set (&cacert, value));
106 static int init (void)
108 static char credentials[1024];
111 curl_easy_cleanup (curl);
113 if ((curl = curl_easy_init ()) == NULL)
115 ERROR ("nginx plugin: curl_easy_init failed.");
119 curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, nginx_curl_callback);
120 curl_easy_setopt (curl, CURLOPT_USERAGENT, PACKAGE_NAME"/"PACKAGE_VERSION);
121 curl_easy_setopt (curl, CURLOPT_ERRORBUFFER, nginx_curl_error);
125 if (ssnprintf (credentials, sizeof (credentials),
126 "%s:%s", user, pass == NULL ? "" : pass) >= sizeof (credentials))
128 ERROR ("nginx plugin: Credentials would have been truncated.");
132 curl_easy_setopt (curl, CURLOPT_USERPWD, credentials);
137 curl_easy_setopt (curl, CURLOPT_URL, url);
140 if ((verify_peer == NULL) || (strcmp (verify_peer, "true") == 0))
142 curl_easy_setopt (curl, CURLOPT_SSL_VERIFYPEER, 1);
146 curl_easy_setopt (curl, CURLOPT_SSL_VERIFYPEER, 0);
149 if ((verify_host == NULL) || (strcmp (verify_host, "true") == 0))
151 curl_easy_setopt (curl, CURLOPT_SSL_VERIFYHOST, 2);
155 curl_easy_setopt (curl, CURLOPT_SSL_VERIFYHOST, 0);
160 curl_easy_setopt (curl, CURLOPT_CAINFO, cacert);
166 static void submit (char *type, char *inst, long long value)
169 value_list_t vl = VALUE_LIST_INIT;
171 if (strcmp (type, "nginx_connections") == 0)
172 values[0].gauge = value;
173 else if (strcmp (type, "nginx_requests") == 0)
174 values[0].counter = value;
180 vl.time = time (NULL);
181 strcpy (vl.host, hostname_g);
182 strcpy (vl.plugin, "nginx");
183 strcpy (vl.plugin_instance, "");
184 sstrncpy (vl.type, type, sizeof (vl.type));
187 sstrncpy (vl.type_instance, inst, sizeof (vl.type_instance));
189 plugin_dispatch_values (&vl);
192 static int nginx_read (void)
208 nginx_buffer_len = 0;
209 if (curl_easy_perform (curl) != 0)
211 WARNING ("nginx plugin: curl_easy_perform failed: %s", nginx_curl_error);
216 while ((lines[lines_num] = strtok (ptr, "\n\r")) != NULL)
226 * Active connections: 291
227 * server accepts handled requests
228 * 16630948 16630948 31070465
229 * Reading: 6 Writing: 179 Waiting: 106
231 for (i = 0; i < lines_num; i++)
233 fields_num = strsplit (lines[i], fields,
234 (sizeof (fields) / sizeof (fields[0])));
238 if ((strcmp (fields[0], "Active") == 0)
239 && (strcmp (fields[1], "connections:") == 0))
241 submit ("nginx_connections", "active", atoll (fields[2]));
243 else if ((atoll (fields[0]) != 0)
244 && (atoll (fields[1]) != 0)
245 && (atoll (fields[2]) != 0))
247 submit ("nginx_requests", NULL, atoll (fields[2]));
250 else if (fields_num == 6)
252 if ((strcmp (fields[0], "Reading:") == 0)
253 && (strcmp (fields[2], "Writing:") == 0)
254 && (strcmp (fields[4], "Waiting:") == 0))
256 submit ("nginx_connections", "reading", atoll (fields[1]));
257 submit ("nginx_connections", "writing", atoll (fields[3]));
258 submit ("nginx_connections", "waiting", atoll (fields[5]));
263 nginx_buffer_len = 0;
266 } /* int nginx_read */
268 void module_register (void)
270 plugin_register_config ("nginx", config, config_keys, config_keys_num);
271 plugin_register_init ("nginx", init);
272 plugin_register_read ("nginx", nginx_read);
273 } /* void module_register */
276 * vim: set shiftwidth=2 softtabstop=2 tabstop=8 :