Various plugins: Use the IS_TRUE and IS_FALSE macros everywhere.
[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,
58     void __attribute__((unused)) *stream)
59 {
60   size_t len = size * nmemb;
61
62   if ((nginx_buffer_len + len) >= ABUFFER_SIZE)
63   {
64     len = (ABUFFER_SIZE - 1) - nginx_buffer_len;
65   }
66
67   if (len <= 0)
68     return (len);
69
70   memcpy (nginx_buffer + nginx_buffer_len, (char *) buf, len);
71   nginx_buffer_len += len;
72   nginx_buffer[nginx_buffer_len] = '\0';
73
74   return (len);
75 }
76
77 static int config_set (char **var, const char *value)
78 {
79   if (*var != NULL)
80   {
81     free (*var);
82     *var = NULL;
83   }
84
85   if ((*var = strdup (value)) == NULL)
86     return (1);
87   else
88     return (0);
89 }
90
91 static int config (const char *key, const char *value)
92 {
93   if (strcasecmp (key, "url") == 0)
94     return (config_set (&url, value));
95   else if (strcasecmp (key, "user") == 0)
96     return (config_set (&user, value));
97   else if (strcasecmp (key, "password") == 0)
98     return (config_set (&pass, value));
99   else if (strcasecmp (key, "verifypeer") == 0)
100     return (config_set (&verify_peer, value));
101   else if (strcasecmp (key, "verifyhost") == 0)
102     return (config_set (&verify_host, value));
103   else if (strcasecmp (key, "cacert") == 0)
104     return (config_set (&cacert, value));
105   else
106     return (-1);
107 } /* int config */
108
109 static int init (void)
110 {
111   static char credentials[1024];
112
113   if (curl != NULL)
114     curl_easy_cleanup (curl);
115
116   if ((curl = curl_easy_init ()) == NULL)
117   {
118     ERROR ("nginx plugin: curl_easy_init failed.");
119     return (-1);
120   }
121
122   curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, nginx_curl_callback);
123   curl_easy_setopt (curl, CURLOPT_USERAGENT, PACKAGE_NAME"/"PACKAGE_VERSION);
124   curl_easy_setopt (curl, CURLOPT_ERRORBUFFER, nginx_curl_error);
125
126   if (user != NULL)
127   {
128     int status = ssnprintf (credentials, sizeof (credentials),
129         "%s:%s", user, pass == NULL ? "" : pass);
130     if ((status < 0) || ((size_t) status >= sizeof (credentials)))
131     {
132       ERROR ("nginx plugin: Credentials would have been truncated.");
133       return (-1);
134     }
135
136     curl_easy_setopt (curl, CURLOPT_USERPWD, credentials);
137   }
138
139   if (url != NULL)
140   {
141     curl_easy_setopt (curl, CURLOPT_URL, url);
142   }
143
144   curl_easy_setopt (curl, CURLOPT_FOLLOWLOCATION, 1);
145
146   if ((verify_peer == NULL) || IS_TRUE (verify_peer))
147   {
148     curl_easy_setopt (curl, CURLOPT_SSL_VERIFYPEER, 1);
149   }
150   else
151   {
152     curl_easy_setopt (curl, CURLOPT_SSL_VERIFYPEER, 0);
153   }
154
155   if ((verify_host == NULL) || IS_TRUE (verify_host))
156   {
157     curl_easy_setopt (curl, CURLOPT_SSL_VERIFYHOST, 2);
158   }
159   else
160   {
161     curl_easy_setopt (curl, CURLOPT_SSL_VERIFYHOST, 0);
162   }
163
164   if (cacert != NULL)
165   {
166     curl_easy_setopt (curl, CURLOPT_CAINFO, cacert);
167   }
168
169   return (0);
170 } /* void init */
171
172 static void submit (char *type, char *inst, long long value)
173 {
174   value_t values[1];
175   value_list_t vl = VALUE_LIST_INIT;
176
177   if (strcmp (type, "nginx_connections") == 0)
178     values[0].gauge = value;
179   else if (strcmp (type, "nginx_requests") == 0)
180     values[0].counter = value;
181   else
182     return;
183
184   vl.values = values;
185   vl.values_len = 1;
186   sstrncpy (vl.host, hostname_g, sizeof (vl.host));
187   sstrncpy (vl.plugin, "nginx", sizeof (vl.plugin));
188   sstrncpy (vl.plugin_instance, "", sizeof (vl.plugin_instance));
189   sstrncpy (vl.type, type, sizeof (vl.type));
190
191   if (inst != NULL)
192     sstrncpy (vl.type_instance, inst, sizeof (vl.type_instance));
193
194   plugin_dispatch_values (&vl);
195 } /* void submit */
196
197 static int nginx_read (void)
198 {
199   int i;
200
201   char *ptr;
202   char *lines[16];
203   int   lines_num = 0;
204   char *saveptr;
205
206   char *fields[16];
207   int   fields_num;
208
209   if (curl == NULL)
210     return (-1);
211   if (url == NULL)
212     return (-1);
213
214   nginx_buffer_len = 0;
215   if (curl_easy_perform (curl) != 0)
216   {
217     WARNING ("nginx plugin: curl_easy_perform failed: %s", nginx_curl_error);
218     return (-1);
219   }
220
221   ptr = nginx_buffer;
222   saveptr = NULL;
223   while ((lines[lines_num] = strtok_r (ptr, "\n\r", &saveptr)) != NULL)
224   {
225     ptr = NULL;
226     lines_num++;
227
228     if (lines_num >= 16)
229       break;
230   }
231
232   /*
233    * Active connections: 291
234    * server accepts handled requests
235    *  16630948 16630948 31070465
236    * Reading: 6 Writing: 179 Waiting: 106
237    */
238   for (i = 0; i < lines_num; i++)
239   {
240     fields_num = strsplit (lines[i], fields,
241         (sizeof (fields) / sizeof (fields[0])));
242
243     if (fields_num == 3)
244     {
245       if ((strcmp (fields[0], "Active") == 0)
246           && (strcmp (fields[1], "connections:") == 0))
247       {
248         submit ("nginx_connections", "active", atoll (fields[2]));
249       }
250       else if ((atoll (fields[0]) != 0)
251           && (atoll (fields[1]) != 0)
252           && (atoll (fields[2]) != 0))
253       {
254         submit ("nginx_requests", NULL, atoll (fields[2]));
255       }
256     }
257     else if (fields_num == 6)
258     {
259       if ((strcmp (fields[0], "Reading:") == 0)
260           && (strcmp (fields[2], "Writing:") == 0)
261           && (strcmp (fields[4], "Waiting:") == 0))
262       {
263         submit ("nginx_connections", "reading", atoll (fields[1]));
264         submit ("nginx_connections", "writing", atoll (fields[3]));
265         submit ("nginx_connections", "waiting", atoll (fields[5]));
266       }
267     }
268   }
269
270   nginx_buffer_len = 0;
271
272   return (0);
273 } /* int nginx_read */
274
275 void module_register (void)
276 {
277   plugin_register_config ("nginx", config, config_keys, config_keys_num);
278   plugin_register_init ("nginx", init);
279   plugin_register_read ("nginx", nginx_read);
280 } /* void module_register */
281
282 /*
283  * vim: set shiftwidth=2 softtabstop=2 tabstop=8 :
284  */