email plugin: Don't print `pthread_t'.
[collectd.git] / src / nginx.c
1 /**
2  * collectd - src/nginx.c
3  * Copyright (C) 2006,2007  Florian octo Forster
4  *
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.
9  *
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.
14  *
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
18  *
19  * Authors:
20  *   Florian octo Forster <octo at verplant.org>
21  **/
22
23 #include "collectd.h"
24 #include "common.h"
25 #include "plugin.h"
26 #include "configfile.h"
27
28 #include <curl/curl.h>
29
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;
36
37 static CURL *curl = NULL;
38
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];
43
44 static const char *config_keys[] =
45 {
46   "URL",
47   "User",
48   "Password",
49   "VerifyPeer",
50   "VerifyHost",
51   "CACert"
52 };
53 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
54
55 static size_t nginx_curl_callback (void *buf, size_t size, size_t nmemb, void *stream)
56 {
57   size_t len = size * nmemb;
58
59   if ((nginx_buffer_len + len) >= ABUFFER_SIZE)
60   {
61     len = (ABUFFER_SIZE - 1) - nginx_buffer_len;
62   }
63
64   if (len <= 0)
65     return (len);
66
67   memcpy (nginx_buffer + nginx_buffer_len, (char *) buf, len);
68   nginx_buffer_len += len;
69   nginx_buffer[nginx_buffer_len] = '\0';
70
71   return (len);
72 }
73
74 static int config_set (char **var, const char *value)
75 {
76   if (*var != NULL)
77   {
78     free (*var);
79     *var = NULL;
80   }
81
82   if ((*var = strdup (value)) == NULL)
83     return (1);
84   else
85     return (0);
86 }
87
88 static int config (const char *key, const char *value)
89 {
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));
102   else
103     return (-1);
104 } /* int config */
105
106 static int init (void)
107 {
108   static char credentials[1024];
109
110   if (curl != NULL)
111     curl_easy_cleanup (curl);
112
113   if ((curl = curl_easy_init ()) == NULL)
114   {
115     ERROR ("nginx plugin: curl_easy_init failed.");
116     return (-1);
117   }
118
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);
122
123   if (user != NULL)
124   {
125     if (snprintf (credentials, 1024, "%s:%s", user, pass == NULL ? "" : pass) >= 1024)
126     {
127       ERROR ("nginx plugin: Credentials would have been truncated.");
128       return (-1);
129     }
130
131     curl_easy_setopt (curl, CURLOPT_USERPWD, credentials);
132   }
133
134   if (url != NULL)
135   {
136     curl_easy_setopt (curl, CURLOPT_URL, url);
137   }
138
139   if ((verify_peer == NULL) || (strcmp (verify_peer, "true") == 0))
140   {
141     curl_easy_setopt (curl, CURLOPT_SSL_VERIFYPEER, 1);
142   }
143   else
144   {
145     curl_easy_setopt (curl, CURLOPT_SSL_VERIFYPEER, 0);
146   }
147
148   if ((verify_host == NULL) || (strcmp (verify_host, "true") == 0))
149   {
150     curl_easy_setopt (curl, CURLOPT_SSL_VERIFYHOST, 2);
151   }
152   else
153   {
154     curl_easy_setopt (curl, CURLOPT_SSL_VERIFYHOST, 0);
155   }
156
157   if (cacert != NULL)
158   {
159     curl_easy_setopt (curl, CURLOPT_CAINFO, cacert);
160   }
161
162   return (0);
163 } /* void init */
164
165 static void submit (char *type, char *inst, long long value)
166 {
167   value_t values[1];
168   value_list_t vl = VALUE_LIST_INIT;
169
170   if (strcmp (type, "nginx_connections") == 0)
171     values[0].gauge = value;
172   else if (strcmp (type, "nginx_requests") == 0)
173     values[0].counter = value;
174   else
175     return;
176
177   vl.values = values;
178   vl.values_len = 1;
179   vl.time = time (NULL);
180   sstrncpy (vl.host, hostname_g, sizeof (vl.host));
181   sstrncpy (vl.plugin, "nginx", sizeof (vl.plugin));
182   sstrncpy (vl.plugin_instance, "", sizeof (vl.plugin_instance));
183
184   if (inst != NULL)
185   {
186     strncpy (vl.type_instance, inst, sizeof (vl.type_instance));
187     vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
188   }
189
190   plugin_dispatch_values (type, &vl);
191 } /* void submit */
192
193 static int nginx_read (void)
194 {
195   int i;
196
197   char *ptr;
198   char *lines[16];
199   int   lines_num = 0;
200   char *saveptr;
201
202   char *fields[16];
203   int   fields_num;
204
205   if (curl == NULL)
206     return (-1);
207   if (url == NULL)
208     return (-1);
209
210   nginx_buffer_len = 0;
211   if (curl_easy_perform (curl) != 0)
212   {
213     WARNING ("nginx plugin: curl_easy_perform failed: %s", nginx_curl_error);
214     return (-1);
215   }
216
217   ptr = nginx_buffer;
218   saveptr = NULL;
219   while ((lines[lines_num] = strtok_r (ptr, "\n\r", &saveptr)) != NULL)
220   {
221     ptr = NULL;
222     lines_num++;
223
224     if (lines_num >= 16)
225       break;
226   }
227
228   /*
229    * Active connections: 291
230    * server accepts handled requests
231    *  16630948 16630948 31070465
232    * Reading: 6 Writing: 179 Waiting: 106
233    */
234   for (i = 0; i < lines_num; i++)
235   {
236     fields_num = strsplit (lines[i], fields,
237         (sizeof (fields) / sizeof (fields[0])));
238
239     if (fields_num == 3)
240     {
241       if ((strcmp (fields[0], "Active") == 0)
242           && (strcmp (fields[1], "connections:") == 0))
243       {
244         submit ("nginx_connections", "active", atoll (fields[2]));
245       }
246       else if ((atoll (fields[0]) != 0)
247           && (atoll (fields[1]) != 0)
248           && (atoll (fields[2]) != 0))
249       {
250         submit ("nginx_requests", NULL, atoll (fields[2]));
251       }
252     }
253     else if (fields_num == 6)
254     {
255       if ((strcmp (fields[0], "Reading:") == 0)
256           && (strcmp (fields[2], "Writing:") == 0)
257           && (strcmp (fields[4], "Waiting:") == 0))
258       {
259         submit ("nginx_connections", "reading", atoll (fields[1]));
260         submit ("nginx_connections", "writing", atoll (fields[3]));
261         submit ("nginx_connections", "waiting", atoll (fields[5]));
262       }
263     }
264   }
265
266   nginx_buffer_len = 0;
267
268   return (0);
269 } /* int nginx_read */
270
271 void module_register (void)
272 {
273   plugin_register_config ("nginx", config, config_keys, config_keys_num);
274   plugin_register_init ("nginx", init);
275   plugin_register_read ("nginx", nginx_read);
276 } /* void module_register */
277
278 /*
279  * vim: set shiftwidth=2 softtabstop=2 tabstop=8 :
280  */