| 1 |
/** |
| 2 |
* collectd - src/nginx.c |
| 3 |
* Copyright (C) 2006-2010 Florian octo Forster |
| 4 |
* Copyright (C) 2008 Sebastian Harl |
| 5 |
* |
| 6 |
* Permission is hereby granted, free of charge, to any person obtaining a |
| 7 |
* copy of this software and associated documentation files (the "Software"), |
| 8 |
* to deal in the Software without restriction, including without limitation |
| 9 |
* the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 10 |
* and/or sell copies of the Software, and to permit persons to whom the |
| 11 |
* Software is furnished to do so, subject to the following conditions: |
| 12 |
* |
| 13 |
* The above copyright notice and this permission notice shall be included in |
| 14 |
* all copies or substantial portions of the Software. |
| 15 |
* |
| 16 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 21 |
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 22 |
* DEALINGS IN THE SOFTWARE. |
| 23 |
* |
| 24 |
* Authors: |
| 25 |
* Florian octo Forster <octo at collectd.org> |
| 26 |
* Sebastian Harl <sh at tokkee.org> |
| 27 |
**/ |
| 28 |
|
| 29 |
#include "collectd.h" |
| 30 |
|
| 31 |
#include "plugin.h" |
| 32 |
#include "utils/common/common.h" |
| 33 |
|
| 34 |
#include <curl/curl.h> |
| 35 |
|
| 36 |
static char *url; |
| 37 |
static char *user; |
| 38 |
static char *pass; |
| 39 |
static char *verify_peer; |
| 40 |
static char *verify_host; |
| 41 |
static char *cacert; |
| 42 |
static char *timeout; |
| 43 |
|
| 44 |
static CURL *curl; |
| 45 |
|
| 46 |
static char nginx_buffer[16384]; |
| 47 |
static size_t nginx_buffer_len; |
| 48 |
static char nginx_curl_error[CURL_ERROR_SIZE]; |
| 49 |
|
| 50 |
static const char *config_keys[] = { |
| 51 |
"URL", "User", "Password", "VerifyPeer", "VerifyHost", "CACert", "Timeout"}; |
| 52 |
static int config_keys_num = STATIC_ARRAY_SIZE(config_keys); |
| 53 |
|
| 54 |
static size_t nginx_curl_callback(void *buf, size_t size, size_t nmemb, |
| 55 |
void __attribute__((unused)) * stream) { |
| 56 |
size_t len = size * nmemb; |
| 57 |
|
| 58 |
/* Check if the data fits into the memory. If not, truncate it. */ |
| 59 |
if ((nginx_buffer_len + len) >= sizeof(nginx_buffer)) { |
| 60 |
assert(sizeof(nginx_buffer) > nginx_buffer_len); |
| 61 |
len = (sizeof(nginx_buffer) - 1) - nginx_buffer_len; |
| 62 |
} |
| 63 |
|
| 64 |
if (len == 0) |
| 65 |
return len; |
| 66 |
|
| 67 |
memcpy(&nginx_buffer[nginx_buffer_len], 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 |
if (*var != NULL) { |
| 76 |
free(*var); |
| 77 |
*var = NULL; |
| 78 |
} |
| 79 |
|
| 80 |
if ((*var = strdup(value)) == NULL) |
| 81 |
return 1; |
| 82 |
else |
| 83 |
return 0; |
| 84 |
} |
| 85 |
|
| 86 |
static int config(const char *key, const char *value) { |
| 87 |
if (strcasecmp(key, "url") == 0) |
| 88 |
return config_set(&url, value); |
| 89 |
else if (strcasecmp(key, "user") == 0) |
| 90 |
return config_set(&user, value); |
| 91 |
else if (strcasecmp(key, "password") == 0) |
| 92 |
return config_set(&pass, value); |
| 93 |
else if (strcasecmp(key, "verifypeer") == 0) |
| 94 |
return config_set(&verify_peer, value); |
| 95 |
else if (strcasecmp(key, "verifyhost") == 0) |
| 96 |
return config_set(&verify_host, value); |
| 97 |
else if (strcasecmp(key, "cacert") == 0) |
| 98 |
return config_set(&cacert, value); |
| 99 |
else if (strcasecmp(key, "timeout") == 0) |
| 100 |
return config_set(&timeout, value); |
| 101 |
else |
| 102 |
return -1; |
| 103 |
} /* int config */ |
| 104 |
|
| 105 |
static int init(void) { |
| 106 |
if (curl != NULL) |
| 107 |
curl_easy_cleanup(curl); |
| 108 |
|
| 109 |
if ((curl = curl_easy_init()) == NULL) { |
| 110 |
ERROR("nginx plugin: curl_easy_init failed."); |
| 111 |
return -1; |
| 112 |
} |
| 113 |
|
| 114 |
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1L); |
| 115 |
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, nginx_curl_callback); |
| 116 |
curl_easy_setopt(curl, CURLOPT_USERAGENT, COLLECTD_USERAGENT); |
| 117 |
curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, nginx_curl_error); |
| 118 |
|
| 119 |
if (user != NULL) { |
| 120 |
#ifdef HAVE_CURLOPT_USERNAME |
| 121 |
curl_easy_setopt(curl, CURLOPT_USERNAME, user); |
| 122 |
curl_easy_setopt(curl, CURLOPT_PASSWORD, (pass == NULL) ? "" : pass); |
| 123 |
#else |
| 124 |
static char credentials[1024]; |
| 125 |
int status = ssnprintf(credentials, sizeof(credentials), "%s:%s", user, |
| 126 |
pass == NULL ? "" : pass); |
| 127 |
if ((status < 0) || ((size_t)status >= sizeof(credentials))) { |
| 128 |
ERROR("nginx plugin: Credentials would have been truncated."); |
| 129 |
return -1; |
| 130 |
} |
| 131 |
|
| 132 |
curl_easy_setopt(curl, CURLOPT_USERPWD, credentials); |
| 133 |
#endif |
| 134 |
} |
| 135 |
|
| 136 |
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); |
| 137 |
curl_easy_setopt(curl, CURLOPT_MAXREDIRS, 50L); |
| 138 |
|
| 139 |
if ((verify_peer == NULL) || IS_TRUE(verify_peer)) { |
| 140 |
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1L); |
| 141 |
} else { |
| 142 |
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L); |
| 143 |
} |
| 144 |
|
| 145 |
if ((verify_host == NULL) || IS_TRUE(verify_host)) { |
| 146 |
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 2L); |
| 147 |
} else { |
| 148 |
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L); |
| 149 |
} |
| 150 |
|
| 151 |
if (cacert != NULL) { |
| 152 |
curl_easy_setopt(curl, CURLOPT_CAINFO, cacert); |
| 153 |
} |
| 154 |
|
| 155 |
#ifdef HAVE_CURLOPT_TIMEOUT_MS |
| 156 |
if (timeout != NULL) { |
| 157 |
curl_easy_setopt(curl, CURLOPT_TIMEOUT_MS, atol(timeout)); |
| 158 |
} else { |
| 159 |
curl_easy_setopt(curl, CURLOPT_TIMEOUT_MS, |
| 160 |
(long)CDTIME_T_TO_MS(plugin_get_interval())); |
| 161 |
} |
| 162 |
#endif |
| 163 |
|
| 164 |
return 0; |
| 165 |
} /* void init */ |
| 166 |
|
| 167 |
static void submit(const char *type, const char *inst, long long value) { |
| 168 |
value_t values[1]; |
| 169 |
value_list_t vl = VALUE_LIST_INIT; |
| 170 |
|
| 171 |
if (strcmp(type, "nginx_connections") == 0) |
| 172 |
values[0].gauge = value; |
| 173 |
else if (strcmp(type, "nginx_requests") == 0) |
| 174 |
values[0].derive = value; |
| 175 |
else if (strcmp(type, "connections") == 0) |
| 176 |
values[0].derive = value; |
| 177 |
else |
| 178 |
return; |
| 179 |
|
| 180 |
vl.values = values; |
| 181 |
vl.values_len = STATIC_ARRAY_SIZE(values); |
| 182 |
sstrncpy(vl.plugin, "nginx", sizeof(vl.plugin)); |
| 183 |
sstrncpy(vl.type, type, sizeof(vl.type)); |
| 184 |
|
| 185 |
if (inst != NULL) |
| 186 |
sstrncpy(vl.type_instance, inst, sizeof(vl.type_instance)); |
| 187 |
|
| 188 |
plugin_dispatch_values(&vl); |
| 189 |
} /* void submit */ |
| 190 |
|
| 191 |
static int nginx_read(void) { |
| 192 |
char *ptr; |
| 193 |
char *lines[16]; |
| 194 |
int lines_num = 0; |
| 195 |
char *saveptr; |
| 196 |
|
| 197 |
char *fields[16]; |
| 198 |
int fields_num; |
| 199 |
|
| 200 |
if (curl == NULL) |
| 201 |
return -1; |
| 202 |
if (url == NULL) |
| 203 |
return -1; |
| 204 |
|
| 205 |
nginx_buffer_len = 0; |
| 206 |
|
| 207 |
curl_easy_setopt(curl, CURLOPT_URL, url); |
| 208 |
|
| 209 |
if (curl_easy_perform(curl) != CURLE_OK) { |
| 210 |
WARNING("nginx plugin: curl_easy_perform failed: %s", nginx_curl_error); |
| 211 |
return -1; |
| 212 |
} |
| 213 |
|
| 214 |
ptr = nginx_buffer; |
| 215 |
saveptr = NULL; |
| 216 |
while ((lines[lines_num] = strtok_r(ptr, "\n\r", &saveptr)) != NULL) { |
| 217 |
ptr = NULL; |
| 218 |
lines_num++; |
| 219 |
|
| 220 |
if (lines_num >= 16) |
| 221 |
break; |
| 222 |
} |
| 223 |
|
| 224 |
/* |
| 225 |
* Active connections: 291 |
| 226 |
* server accepts handled requests |
| 227 |
* 101059015 100422216 347910649 |
| 228 |
* Reading: 6 Writing: 179 Waiting: 106 |
| 229 |
*/ |
| 230 |
for (int i = 0; i < lines_num; i++) { |
| 231 |
fields_num = |
| 232 |
strsplit(lines[i], fields, (sizeof(fields) / sizeof(fields[0]))); |
| 233 |
|
| 234 |
if (fields_num == 3) { |
| 235 |
if ((strcmp(fields[0], "Active") == 0) && |
| 236 |
(strcmp(fields[1], "connections:") == 0)) { |
| 237 |
submit("nginx_connections", "active", atoll(fields[2])); |
| 238 |
} else if ((atoll(fields[0]) != 0) && (atoll(fields[1]) != 0) && |
| 239 |
(atoll(fields[2]) != 0)) { |
| 240 |
submit("connections", "accepted", atoll(fields[0])); |
| 241 |
/* TODO: The legacy metric "handled", which is the sum of "accepted" and |
| 242 |
* "failed", is reported for backwards compatibility only. Remove in the |
| 243 |
* next major version. */ |
| 244 |
submit("connections", "handled", atoll(fields[1])); |
| 245 |
submit("connections", "failed", (atoll(fields[0]) - atoll(fields[1]))); |
| 246 |
submit("nginx_requests", NULL, atoll(fields[2])); |
| 247 |
} |
| 248 |
} else if (fields_num == 6) { |
| 249 |
if ((strcmp(fields[0], "Reading:") == 0) && |
| 250 |
(strcmp(fields[2], "Writing:") == 0) && |
| 251 |
(strcmp(fields[4], "Waiting:") == 0)) { |
| 252 |
submit("nginx_connections", "reading", atoll(fields[1])); |
| 253 |
submit("nginx_connections", "writing", atoll(fields[3])); |
| 254 |
submit("nginx_connections", "waiting", atoll(fields[5])); |
| 255 |
} |
| 256 |
} |
| 257 |
} |
| 258 |
|
| 259 |
nginx_buffer_len = 0; |
| 260 |
|
| 261 |
return 0; |
| 262 |
} /* int nginx_read */ |
| 263 |
|
| 264 |
void module_register(void) { |
| 265 |
plugin_register_config("nginx", config, config_keys, config_keys_num); |
| 266 |
plugin_register_init("nginx", init); |
| 267 |
plugin_register_read("nginx", nginx_read); |
| 268 |
} /* void module_register */ |