Various plugins: Update copyright information.
[collectd.git] / src / nginx.c
1 /**
2  * collectd - src/nginx.c
3  * Copyright (C) 2006,2007  Florian octo Forster
4  * Copyright (C) 2008       Sebastian Harl
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License as published by the
8  * Free Software Foundation; either version 2 of the License, or (at your
9  * option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this program; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
19  *
20  * Authors:
21  *   Florian octo Forster <octo at verplant.org>
22  *   Sebastian Harl <sh at tokkee.org>
23  **/
24
25 #include "collectd.h"
26 #include "common.h"
27 #include "plugin.h"
28 #include "configfile.h"
29
30 #include <curl/curl.h>
31
32 static char *url         = NULL;
33 static char *user        = NULL;
34 static char *pass        = NULL;
35 static char *verify_peer = NULL;
36 static char *verify_host = NULL;
37 static char *cacert      = NULL;
38
39 static CURL *curl = NULL;
40
41 #define ABUFFER_SIZE 16384
42 static char nginx_buffer[ABUFFER_SIZE];
43 static int  nginx_buffer_len = 0;
44 static char nginx_curl_error[CURL_ERROR_SIZE];
45
46 static const char *config_keys[] =
47 {
48   "URL",
49   "User",
50   "Password",
51   "VerifyPeer",
52   "VerifyHost",
53   "CACert"
54 };
55 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
56
57 static size_t nginx_curl_callback (void *buf, size_t size, size_t nmemb, void *stream)
58 {
59   size_t len = size * nmemb;
60
61   if ((nginx_buffer_len + len) >= ABUFFER_SIZE)
62   {
63     len = (ABUFFER_SIZE - 1) - nginx_buffer_len;
64   }
65
66   if (len <= 0)
67     return (len);
68
69   memcpy (nginx_buffer + nginx_buffer_len, (char *) buf, len);
70   nginx_buffer_len += len;
71   nginx_buffer[nginx_buffer_len] = '\0';
72
73   return (len);
74 }
75
76 static int config_set (char **var, const char *value)
77 {
78   if (*var != NULL)
79   {
80     free (*var);
81     *var = NULL;
82   }
83
84   if ((*var = strdup (value)) == NULL)
85     return (1);
86   else
87     return (0);
88 }
89
90 static int config (const char *key, const char *value)
91 {
92   if (strcasecmp (key, "url") == 0)
93     return (config_set (&url, value));
94   else if (strcasecmp (key, "user") == 0)
95     return (config_set (&user, value));
96   else if (strcasecmp (key, "password") == 0)
97     return (config_set (&pass, value));
98   else if (strcasecmp (key, "verifypeer") == 0)
99     return (config_set (&verify_peer, value));
100   else if (strcasecmp (key, "verifyhost") == 0)
101     return (config_set (&verify_host, value));
102   else if (strcasecmp (key, "cacert") == 0)
103     return (config_set (&cacert, value));
104   else
105     return (-1);
106 } /* int config */
107
108 static int init (void)
109 {
110   static char credentials[1024];
111
112   if (curl != NULL)
113     curl_easy_cleanup (curl);
114
115   if ((curl = curl_easy_init ()) == NULL)
116   {
117     ERROR ("nginx plugin: curl_easy_init failed.");
118     return (-1);
119   }
120
121   curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, nginx_curl_callback);
122   curl_easy_setopt (curl, CURLOPT_USERAGENT, PACKAGE_NAME"/"PACKAGE_VERSION);
123   curl_easy_setopt (curl, CURLOPT_ERRORBUFFER, nginx_curl_error);
124
125   if (user != NULL)
126   {
127     if (ssnprintf (credentials, sizeof (credentials),
128           "%s:%s", user, pass == NULL ? "" : pass) >= sizeof (credentials))
129     {
130       ERROR ("nginx plugin: Credentials would have been truncated.");
131       return (-1);
132     }
133
134     curl_easy_setopt (curl, CURLOPT_USERPWD, credentials);
135   }
136
137   if (url != NULL)
138   {
139     curl_easy_setopt (curl, CURLOPT_URL, url);
140   }
141
142   if ((verify_peer == NULL) || (strcmp (verify_peer, "true") == 0))
143   {
144     curl_easy_setopt (curl, CURLOPT_SSL_VERIFYPEER, 1);
145   }
146   else
147   {
148     curl_easy_setopt (curl, CURLOPT_SSL_VERIFYPEER, 0);
149   }
150
151   if ((verify_host == NULL) || (strcmp (verify_host, "true") == 0))
152   {
153     curl_easy_setopt (curl, CURLOPT_SSL_VERIFYHOST, 2);
154   }
155   else
156   {
157     curl_easy_setopt (curl, CURLOPT_SSL_VERIFYHOST, 0);
158   }
159
160   if (cacert != NULL)
161   {
162     curl_easy_setopt (curl, CURLOPT_CAINFO, cacert);
163   }
164
165   return (0);
166 } /* void init */
167
168 static void submit (char *type, char *inst, long long value)
169 {
170   value_t values[1];
171   value_list_t vl = VALUE_LIST_INIT;
172
173   if (strcmp (type, "nginx_connections") == 0)
174     values[0].gauge = value;
175   else if (strcmp (type, "nginx_requests") == 0)
176     values[0].counter = value;
177   else
178     return;
179
180   vl.values = values;
181   vl.values_len = 1;
182   vl.time = time (NULL);
183   sstrncpy (vl.host, hostname_g, sizeof (vl.host));
184   sstrncpy (vl.plugin, "nginx", sizeof (vl.plugin));
185   sstrncpy (vl.plugin_instance, "", sizeof (vl.plugin_instance));
186   sstrncpy (vl.type, type, sizeof (vl.type));
187
188   if (inst != NULL)
189     sstrncpy (vl.type_instance, inst, sizeof (vl.type_instance));
190
191   plugin_dispatch_values (&vl);
192 } /* void submit */
193
194 static int nginx_read (void)
195 {
196   int i;
197
198   char *ptr;
199   char *lines[16];
200   int   lines_num = 0;
201   char *saveptr;
202
203   char *fields[16];
204   int   fields_num;
205
206   if (curl == NULL)
207     return (-1);
208   if (url == NULL)
209     return (-1);
210
211   nginx_buffer_len = 0;
212   if (curl_easy_perform (curl) != 0)
213   {
214     WARNING ("nginx plugin: curl_easy_perform failed: %s", nginx_curl_error);
215     return (-1);
216   }
217
218   ptr = nginx_buffer;
219   saveptr = NULL;
220   while ((lines[lines_num] = strtok_r (ptr, "\n\r", &saveptr)) != NULL)
221   {
222     ptr = NULL;
223     lines_num++;
224
225     if (lines_num >= 16)
226       break;
227   }
228
229   /*
230    * Active connections: 291
231    * server accepts handled requests
232    *  16630948 16630948 31070465
233    * Reading: 6 Writing: 179 Waiting: 106
234    */
235   for (i = 0; i < lines_num; i++)
236   {
237     fields_num = strsplit (lines[i], fields,
238         (sizeof (fields) / sizeof (fields[0])));
239
240     if (fields_num == 3)
241     {
242       if ((strcmp (fields[0], "Active") == 0)
243           && (strcmp (fields[1], "connections:") == 0))
244       {
245         submit ("nginx_connections", "active", atoll (fields[2]));
246       }
247       else if ((atoll (fields[0]) != 0)
248           && (atoll (fields[1]) != 0)
249           && (atoll (fields[2]) != 0))
250       {
251         submit ("nginx_requests", NULL, atoll (fields[2]));
252       }
253     }
254     else if (fields_num == 6)
255     {
256       if ((strcmp (fields[0], "Reading:") == 0)
257           && (strcmp (fields[2], "Writing:") == 0)
258           && (strcmp (fields[4], "Waiting:") == 0))
259       {
260         submit ("nginx_connections", "reading", atoll (fields[1]));
261         submit ("nginx_connections", "writing", atoll (fields[3]));
262         submit ("nginx_connections", "waiting", atoll (fields[5]));
263       }
264     }
265   }
266
267   nginx_buffer_len = 0;
268
269   return (0);
270 } /* int nginx_read */
271
272 void module_register (void)
273 {
274   plugin_register_config ("nginx", config, config_keys, config_keys_num);
275   plugin_register_init ("nginx", init);
276   plugin_register_read ("nginx", nginx_read);
277 } /* void module_register */
278
279 /*
280  * vim: set shiftwidth=2 softtabstop=2 tabstop=8 :
281  */