| 1 |
/** |
| 2 |
* collectd - src/ascent.c |
| 3 |
* Copyright (C) 2008 Florian octo Forster |
| 4 |
* |
| 5 |
* Permission is hereby granted, free of charge, to any person obtaining a |
| 6 |
* copy of this software and associated documentation files (the "Software"), |
| 7 |
* to deal in the Software without restriction, including without limitation |
| 8 |
* the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 9 |
* and/or sell copies of the Software, and to permit persons to whom the |
| 10 |
* Software is furnished to do so, subject to the following conditions: |
| 11 |
* |
| 12 |
* The above copyright notice and this permission notice shall be included in |
| 13 |
* all copies or substantial portions of the Software. |
| 14 |
* |
| 15 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 20 |
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 21 |
* DEALINGS IN THE SOFTWARE. |
| 22 |
* |
| 23 |
* Authors: |
| 24 |
* Florian octo Forster <octo at collectd.org> |
| 25 |
**/ |
| 26 |
|
| 27 |
#include "collectd.h" |
| 28 |
|
| 29 |
#include "plugin.h" |
| 30 |
#include "utils/common/common.h" |
| 31 |
|
| 32 |
#include <curl/curl.h> |
| 33 |
#include <libxml/parser.h> |
| 34 |
|
| 35 |
static const char *races_list[] = /* {{{ */ |
| 36 |
{ |
| 37 |
NULL, "Human", /* 1 */ |
| 38 |
"Orc", /* 2 */ |
| 39 |
"Dwarf", /* 3 */ |
| 40 |
"Nightelf", /* 4 */ |
| 41 |
"Undead", /* 5 */ |
| 42 |
"Tauren", /* 6 */ |
| 43 |
"Gnome", /* 7 */ |
| 44 |
"Troll", /* 8 */ |
| 45 |
NULL, "Bloodelf", /* 10 */ |
| 46 |
"Draenei" /* 11 */ |
| 47 |
}; /* }}} */ |
| 48 |
#define RACES_LIST_LENGTH STATIC_ARRAY_SIZE(races_list) |
| 49 |
|
| 50 |
static const char *classes_list[] = /* {{{ */ |
| 51 |
{ |
| 52 |
NULL, "Warrior", /* 1 */ |
| 53 |
"Paladin", /* 2 */ |
| 54 |
"Hunter", /* 3 */ |
| 55 |
"Rogue", /* 4 */ |
| 56 |
"Priest", /* 5 */ |
| 57 |
NULL, "Shaman", /* 7 */ |
| 58 |
"Mage", /* 8 */ |
| 59 |
"Warlock", /* 9 */ |
| 60 |
NULL, "Druid" /* 11 */ |
| 61 |
}; /* }}} */ |
| 62 |
#define CLASSES_LIST_LENGTH STATIC_ARRAY_SIZE(classes_list) |
| 63 |
|
| 64 |
static const char *genders_list[] = /* {{{ */ |
| 65 |
{"Male", "Female"}; /* }}} */ |
| 66 |
#define GENDERS_LIST_LENGTH STATIC_ARRAY_SIZE(genders_list) |
| 67 |
|
| 68 |
struct player_stats_s { |
| 69 |
int races[RACES_LIST_LENGTH]; |
| 70 |
int classes[CLASSES_LIST_LENGTH]; |
| 71 |
int genders[GENDERS_LIST_LENGTH]; |
| 72 |
int level_sum; |
| 73 |
int level_num; |
| 74 |
int latency_sum; |
| 75 |
int latency_num; |
| 76 |
}; |
| 77 |
typedef struct player_stats_s player_stats_t; |
| 78 |
|
| 79 |
struct player_info_s { |
| 80 |
int race; |
| 81 |
int class; |
| 82 |
int gender; |
| 83 |
int level; |
| 84 |
int latency; |
| 85 |
}; |
| 86 |
typedef struct player_info_s player_info_t; |
| 87 |
#define PLAYER_INFO_STATIC_INIT \ |
| 88 |
{ -1, -1, -1, -1, -1 } |
| 89 |
|
| 90 |
static char *url; |
| 91 |
static char *user; |
| 92 |
static char *pass; |
| 93 |
static char *verify_peer; |
| 94 |
static char *verify_host; |
| 95 |
static char *cacert; |
| 96 |
static char *timeout; |
| 97 |
|
| 98 |
static CURL *curl; |
| 99 |
|
| 100 |
static char *ascent_buffer; |
| 101 |
static size_t ascent_buffer_size; |
| 102 |
static size_t ascent_buffer_fill; |
| 103 |
static char ascent_curl_error[CURL_ERROR_SIZE]; |
| 104 |
|
| 105 |
static const char *config_keys[] = { |
| 106 |
"URL", "User", "Password", "VerifyPeer", "VerifyHost", "CACert", "Timeout", |
| 107 |
}; |
| 108 |
static int config_keys_num = STATIC_ARRAY_SIZE(config_keys); |
| 109 |
|
| 110 |
static int ascent_submit_gauge(const char *plugin_instance, /* {{{ */ |
| 111 |
const char *type, const char *type_instance, |
| 112 |
gauge_t value) { |
| 113 |
value_list_t vl = VALUE_LIST_INIT; |
| 114 |
|
| 115 |
vl.values = &(value_t){.gauge = value}; |
| 116 |
vl.values_len = 1; |
| 117 |
sstrncpy(vl.plugin, "ascent", sizeof(vl.plugin)); |
| 118 |
|
| 119 |
if (plugin_instance != NULL) |
| 120 |
sstrncpy(vl.plugin_instance, plugin_instance, sizeof(vl.plugin_instance)); |
| 121 |
|
| 122 |
sstrncpy(vl.type, type, sizeof(vl.type)); |
| 123 |
|
| 124 |
if (type_instance != NULL) |
| 125 |
sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance)); |
| 126 |
|
| 127 |
plugin_dispatch_values(&vl); |
| 128 |
return 0; |
| 129 |
} /* }}} int ascent_submit_gauge */ |
| 130 |
|
| 131 |
static size_t ascent_curl_callback(void *buf, size_t size, |
| 132 |
size_t nmemb, /* {{{ */ |
| 133 |
void __attribute__((unused)) * stream) { |
| 134 |
size_t len = size * nmemb; |
| 135 |
|
| 136 |
if (len == 0) |
| 137 |
return len; |
| 138 |
|
| 139 |
if ((ascent_buffer_fill + len) >= ascent_buffer_size) { |
| 140 |
char *temp; |
| 141 |
|
| 142 |
temp = realloc(ascent_buffer, ascent_buffer_fill + len + 1); |
| 143 |
if (temp == NULL) { |
| 144 |
ERROR("ascent plugin: realloc failed."); |
| 145 |
return 0; |
| 146 |
} |
| 147 |
ascent_buffer = temp; |
| 148 |
ascent_buffer_size = ascent_buffer_fill + len + 1; |
| 149 |
} |
| 150 |
|
| 151 |
memcpy(ascent_buffer + ascent_buffer_fill, (char *)buf, len); |
| 152 |
ascent_buffer_fill += len; |
| 153 |
ascent_buffer[ascent_buffer_fill] = 0; |
| 154 |
|
| 155 |
return len; |
| 156 |
} /* }}} size_t ascent_curl_callback */ |
| 157 |
|
| 158 |
static int ascent_submit_players(player_stats_t *ps) /* {{{ */ |
| 159 |
{ |
| 160 |
gauge_t value; |
| 161 |
|
| 162 |
for (size_t i = 0; i < RACES_LIST_LENGTH; i++) |
| 163 |
if (races_list[i] != NULL) |
| 164 |
ascent_submit_gauge("by-race", "players", races_list[i], |
| 165 |
(gauge_t)ps->races[i]); |
| 166 |
|
| 167 |
for (size_t i = 0; i < CLASSES_LIST_LENGTH; i++) |
| 168 |
if (classes_list[i] != NULL) |
| 169 |
ascent_submit_gauge("by-class", "players", classes_list[i], |
| 170 |
(gauge_t)ps->classes[i]); |
| 171 |
|
| 172 |
for (size_t i = 0; i < GENDERS_LIST_LENGTH; i++) |
| 173 |
if (genders_list[i] != NULL) |
| 174 |
ascent_submit_gauge("by-gender", "players", genders_list[i], |
| 175 |
(gauge_t)ps->genders[i]); |
| 176 |
|
| 177 |
if (ps->level_num <= 0) |
| 178 |
value = NAN; |
| 179 |
else |
| 180 |
value = ((double)ps->level_sum) / ((double)ps->level_num); |
| 181 |
ascent_submit_gauge(NULL, "gauge", "avg-level", value); |
| 182 |
|
| 183 |
/* Latency is in ms, but we store seconds. */ |
| 184 |
if (ps->latency_num <= 0) |
| 185 |
value = NAN; |
| 186 |
else |
| 187 |
value = ((double)ps->latency_sum) / (1000.0 * ((double)ps->latency_num)); |
| 188 |
ascent_submit_gauge(NULL, "latency", "average", value); |
| 189 |
|
| 190 |
return 0; |
| 191 |
} /* }}} int ascent_submit_players */ |
| 192 |
|
| 193 |
static int ascent_account_player(player_stats_t *ps, /* {{{ */ |
| 194 |
player_info_t *pi) { |
| 195 |
if (pi->race >= 0) { |
| 196 |
if (((size_t)pi->race >= RACES_LIST_LENGTH) || |
| 197 |
(races_list[pi->race] == NULL)) |
| 198 |
ERROR("ascent plugin: Ignoring invalid numeric race %i.", pi->race); |
| 199 |
else |
| 200 |
ps->races[pi->race]++; |
| 201 |
} |
| 202 |
|
| 203 |
if (pi->class >= 0) { |
| 204 |
if (((size_t)pi->class >= CLASSES_LIST_LENGTH) || |
| 205 |
(classes_list[pi->class] == NULL)) |
| 206 |
ERROR("ascent plugin: Ignoring invalid numeric class %i.", pi->class); |
| 207 |
else |
| 208 |
ps->classes[pi->class]++; |
| 209 |
} |
| 210 |
|
| 211 |
if (pi->gender >= 0) { |
| 212 |
if (((size_t)pi->gender >= GENDERS_LIST_LENGTH) || |
| 213 |
(genders_list[pi->gender] == NULL)) |
| 214 |
ERROR("ascent plugin: Ignoring invalid numeric gender %i.", pi->gender); |
| 215 |
else |
| 216 |
ps->genders[pi->gender]++; |
| 217 |
} |
| 218 |
|
| 219 |
if (pi->level > 0) { |
| 220 |
ps->level_sum += pi->level; |
| 221 |
ps->level_num++; |
| 222 |
} |
| 223 |
|
| 224 |
if (pi->latency >= 0) { |
| 225 |
ps->latency_sum += pi->latency; |
| 226 |
ps->latency_num++; |
| 227 |
} |
| 228 |
|
| 229 |
return 0; |
| 230 |
} /* }}} int ascent_account_player */ |
| 231 |
|
| 232 |
static int ascent_xml_submit_gauge(xmlDoc *doc, xmlNode *node, /* {{{ */ |
| 233 |
const char *plugin_instance, |
| 234 |
const char *type, |
| 235 |
const char *type_instance) { |
| 236 |
char *str_ptr; |
| 237 |
gauge_t value; |
| 238 |
|
| 239 |
str_ptr = (char *)xmlNodeListGetString(doc, node->xmlChildrenNode, 1); |
| 240 |
if (str_ptr == NULL) { |
| 241 |
ERROR( |
| 242 |
"ascent plugin: ascent_xml_submit_gauge: xmlNodeListGetString failed."); |
| 243 |
return -1; |
| 244 |
} |
| 245 |
|
| 246 |
if (strcasecmp("N/A", str_ptr) == 0) |
| 247 |
value = NAN; |
| 248 |
else { |
| 249 |
char *end_ptr = NULL; |
| 250 |
value = strtod(str_ptr, &end_ptr); |
| 251 |
if (str_ptr == end_ptr) { |
| 252 |
xmlFree(str_ptr); |
| 253 |
ERROR("ascent plugin: ascent_xml_submit_gauge: strtod failed."); |
| 254 |
return -1; |
| 255 |
} |
| 256 |
} |
| 257 |
xmlFree(str_ptr); |
| 258 |
|
| 259 |
return ascent_submit_gauge(plugin_instance, type, type_instance, value); |
| 260 |
} /* }}} int ascent_xml_submit_gauge */ |
| 261 |
|
| 262 |
static int ascent_xml_read_int(xmlDoc *doc, xmlNode *node, /* {{{ */ |
| 263 |
int *ret_value) { |
| 264 |
char *str_ptr; |
| 265 |
int value; |
| 266 |
|
| 267 |
str_ptr = (char *)xmlNodeListGetString(doc, node->xmlChildrenNode, 1); |
| 268 |
if (str_ptr == NULL) { |
| 269 |
ERROR("ascent plugin: ascent_xml_read_int: xmlNodeListGetString failed."); |
| 270 |
return -1; |
| 271 |
} |
| 272 |
|
| 273 |
if (strcasecmp("N/A", str_ptr) == 0) |
| 274 |
value = -1; |
| 275 |
else { |
| 276 |
char *end_ptr = NULL; |
| 277 |
value = strtol(str_ptr, &end_ptr, 0); |
| 278 |
if (str_ptr == end_ptr) { |
| 279 |
xmlFree(str_ptr); |
| 280 |
ERROR("ascent plugin: ascent_xml_read_int: strtol failed."); |
| 281 |
return -1; |
| 282 |
} |
| 283 |
} |
| 284 |
xmlFree(str_ptr); |
| 285 |
|
| 286 |
*ret_value = value; |
| 287 |
return 0; |
| 288 |
} /* }}} int ascent_xml_read_int */ |
| 289 |
|
| 290 |
static int ascent_xml_sessions_plr(xmlDoc *doc, xmlNode *node, /* {{{ */ |
| 291 |
player_info_t *pi) { |
| 292 |
for (xmlNode *child = node->xmlChildrenNode; child != NULL; |
| 293 |
child = child->next) { |
| 294 |
if ((xmlStrcmp((const xmlChar *)"comment", child->name) == 0) || |
| 295 |
(xmlStrcmp((const xmlChar *)"text", child->name) == 0)) |
| 296 |
/* ignore */; |
| 297 |
else if (xmlStrcmp((const xmlChar *)"race", child->name) == 0) |
| 298 |
ascent_xml_read_int(doc, child, &pi->race); |
| 299 |
else if (xmlStrcmp((const xmlChar *)"class", child->name) == 0) |
| 300 |
ascent_xml_read_int(doc, child, &pi->class); |
| 301 |
else if (xmlStrcmp((const xmlChar *)"gender", child->name) == 0) |
| 302 |
ascent_xml_read_int(doc, child, &pi->gender); |
| 303 |
else if (xmlStrcmp((const xmlChar *)"level", child->name) == 0) |
| 304 |
ascent_xml_read_int(doc, child, &pi->level); |
| 305 |
else if (xmlStrcmp((const xmlChar *)"latency", child->name) == 0) |
| 306 |
ascent_xml_read_int(doc, child, &pi->latency); |
| 307 |
else if ((xmlStrcmp((const xmlChar *)"name", child->name) == 0) || |
| 308 |
(xmlStrcmp((const xmlChar *)"pvprank", child->name) == 0) || |
| 309 |
(xmlStrcmp((const xmlChar *)"map", child->name) == 0) || |
| 310 |
(xmlStrcmp((const xmlChar *)"areaid", child->name) == 0) || |
| 311 |
(xmlStrcmp((const xmlChar *)"xpos", child->name) == 0) || |
| 312 |
(xmlStrcmp((const xmlChar *)"ypos", child->name) == 0) || |
| 313 |
(xmlStrcmp((const xmlChar *)"onime", child->name) == 0)) |
| 314 |
/* ignore */; |
| 315 |
else { |
| 316 |
WARNING("ascent plugin: ascent_xml_status: Unknown tag: %s", child->name); |
| 317 |
} |
| 318 |
} /* for (child) */ |
| 319 |
|
| 320 |
return 0; |
| 321 |
} /* }}} int ascent_xml_sessions_plr */ |
| 322 |
|
| 323 |
static int ascent_xml_sessions(xmlDoc *doc, xmlNode *node) /* {{{ */ |
| 324 |
{ |
| 325 |
player_stats_t ps = {.level_sum = 0}; |
| 326 |
|
| 327 |
for (xmlNode *child = node->xmlChildrenNode; child != NULL; |
| 328 |
child = child->next) { |
| 329 |
if ((xmlStrcmp((const xmlChar *)"comment", child->name) == 0) || |
| 330 |
(xmlStrcmp((const xmlChar *)"text", child->name) == 0)) |
| 331 |
/* ignore */; |
| 332 |
else if (xmlStrcmp((const xmlChar *)"plr", child->name) == 0) { |
| 333 |
int status; |
| 334 |
player_info_t pi = PLAYER_INFO_STATIC_INIT; |
| 335 |
|
| 336 |
status = ascent_xml_sessions_plr(doc, child, &pi); |
| 337 |
if (status == 0) |
| 338 |
ascent_account_player(&ps, &pi); |
| 339 |
} else { |
| 340 |
WARNING("ascent plugin: ascent_xml_status: Unknown tag: %s", child->name); |
| 341 |
} |
| 342 |
} /* for (child) */ |
| 343 |
|
| 344 |
ascent_submit_players(&ps); |
| 345 |
|
| 346 |
return 0; |
| 347 |
} /* }}} int ascent_xml_sessions */ |
| 348 |
|
| 349 |
static int ascent_xml_status(xmlDoc *doc, xmlNode *node) /* {{{ */ |
| 350 |
{ |
| 351 |
for (xmlNode *child = node->xmlChildrenNode; child != NULL; |
| 352 |
child = child->next) { |
| 353 |
if ((xmlStrcmp((const xmlChar *)"comment", child->name) == 0) || |
| 354 |
(xmlStrcmp((const xmlChar *)"text", child->name) == 0)) |
| 355 |
/* ignore */; |
| 356 |
else if (xmlStrcmp((const xmlChar *)"alliance", child->name) == 0) |
| 357 |
ascent_xml_submit_gauge(doc, child, NULL, "players", "alliance"); |
| 358 |
else if (xmlStrcmp((const xmlChar *)"horde", child->name) == 0) |
| 359 |
ascent_xml_submit_gauge(doc, child, NULL, "players", "horde"); |
| 360 |
else if (xmlStrcmp((const xmlChar *)"qplayers", child->name) == 0) |
| 361 |
ascent_xml_submit_gauge(doc, child, NULL, "players", "queued"); |
| 362 |
else if ((xmlStrcmp((const xmlChar *)"acceptedconns", child->name) == 0) || |
| 363 |
(xmlStrcmp((const xmlChar *)"avglat", child->name) == 0) || |
| 364 |
(xmlStrcmp((const xmlChar *)"cdbquerysize", child->name) == 0) || |
| 365 |
(xmlStrcmp((const xmlChar *)"cpu", child->name) == 0) || |
| 366 |
(xmlStrcmp((const xmlChar *)"fthreads", child->name) == 0) || |
| 367 |
(xmlStrcmp((const xmlChar *)"gmcount", child->name) == 0) || |
| 368 |
(xmlStrcmp((const xmlChar *)"lastupdate", child->name) == 0) || |
| 369 |
(xmlStrcmp((const xmlChar *)"ontime", child->name) == 0) || |
| 370 |
(xmlStrcmp((const xmlChar *)"oplayers", child->name) == 0) || |
| 371 |
(xmlStrcmp((const xmlChar *)"peakcount", child->name) == 0) || |
| 372 |
(xmlStrcmp((const xmlChar *)"platform", child->name) == 0) || |
| 373 |
(xmlStrcmp((const xmlChar *)"ram", child->name) == 0) || |
| 374 |
(xmlStrcmp((const xmlChar *)"threads", child->name) == 0) || |
| 375 |
(xmlStrcmp((const xmlChar *)"uptime", child->name) == 0) || |
| 376 |
(xmlStrcmp((const xmlChar *)"wdbquerysize", child->name) == 0)) |
| 377 |
/* ignore */; |
| 378 |
else { |
| 379 |
WARNING("ascent plugin: ascent_xml_status: Unknown tag: %s", child->name); |
| 380 |
} |
| 381 |
} /* for (child) */ |
| 382 |
|
| 383 |
return 0; |
| 384 |
} /* }}} int ascent_xml_status */ |
| 385 |
|
| 386 |
static int ascent_xml(const char *data) /* {{{ */ |
| 387 |
{ |
| 388 |
xmlDoc *doc; |
| 389 |
xmlNode *cur; |
| 390 |
|
| 391 |
#if 0 |
| 392 |
doc = xmlParseMemory (data, strlen (data), |
| 393 |
/* URL = */ "ascent.xml", |
| 394 |
/* encoding = */ NULL, |
| 395 |
/* options = */ 0); |
| 396 |
#else |
| 397 |
doc = xmlParseMemory(data, strlen(data)); |
| 398 |
#endif |
| 399 |
if (doc == NULL) { |
| 400 |
ERROR("ascent plugin: xmlParseMemory failed."); |
| 401 |
return -1; |
| 402 |
} |
| 403 |
|
| 404 |
cur = xmlDocGetRootElement(doc); |
| 405 |
if (cur == NULL) { |
| 406 |
ERROR("ascent plugin: XML document is empty."); |
| 407 |
xmlFreeDoc(doc); |
| 408 |
return -1; |
| 409 |
} |
| 410 |
|
| 411 |
if (xmlStrcmp((const xmlChar *)"serverpage", cur->name) != 0) { |
| 412 |
ERROR("ascent plugin: XML root element is not \"serverpage\"."); |
| 413 |
xmlFreeDoc(doc); |
| 414 |
return -1; |
| 415 |
} |
| 416 |
|
| 417 |
for (xmlNode *child = cur->xmlChildrenNode; child != NULL; |
| 418 |
child = child->next) { |
| 419 |
if ((xmlStrcmp((const xmlChar *)"comment", child->name) == 0) || |
| 420 |
(xmlStrcmp((const xmlChar *)"text", child->name) == 0)) |
| 421 |
/* ignore */; |
| 422 |
else if (xmlStrcmp((const xmlChar *)"status", child->name) == 0) |
| 423 |
ascent_xml_status(doc, child); |
| 424 |
else if (xmlStrcmp((const xmlChar *)"instances", child->name) == 0) |
| 425 |
/* ignore for now */; |
| 426 |
else if (xmlStrcmp((const xmlChar *)"gms", child->name) == 0) |
| 427 |
/* ignore for now */; |
| 428 |
else if (xmlStrcmp((const xmlChar *)"sessions", child->name) == 0) |
| 429 |
ascent_xml_sessions(doc, child); |
| 430 |
else { |
| 431 |
WARNING("ascent plugin: ascent_xml: Unknown tag: %s", child->name); |
| 432 |
} |
| 433 |
} /* for (child) */ |
| 434 |
|
| 435 |
xmlFreeDoc(doc); |
| 436 |
return 0; |
| 437 |
} /* }}} int ascent_xml */ |
| 438 |
|
| 439 |
static int config_set(char **var, const char *value) /* {{{ */ |
| 440 |
{ |
| 441 |
if (*var != NULL) { |
| 442 |
free(*var); |
| 443 |
*var = NULL; |
| 444 |
} |
| 445 |
|
| 446 |
if ((*var = strdup(value)) == NULL) |
| 447 |
return 1; |
| 448 |
else |
| 449 |
return 0; |
| 450 |
} /* }}} int config_set */ |
| 451 |
|
| 452 |
static int ascent_config(const char *key, const char *value) /* {{{ */ |
| 453 |
{ |
| 454 |
if (strcasecmp(key, "URL") == 0) |
| 455 |
return config_set(&url, value); |
| 456 |
else if (strcasecmp(key, "User") == 0) |
| 457 |
return config_set(&user, value); |
| 458 |
else if (strcasecmp(key, "Password") == 0) |
| 459 |
return config_set(&pass, value); |
| 460 |
else if (strcasecmp(key, "VerifyPeer") == 0) |
| 461 |
return config_set(&verify_peer, value); |
| 462 |
else if (strcasecmp(key, "VerifyHost") == 0) |
| 463 |
return config_set(&verify_host, value); |
| 464 |
else if (strcasecmp(key, "CACert") == 0) |
| 465 |
return config_set(&cacert, value); |
| 466 |
else if (strcasecmp(key, "Timeout") == 0) |
| 467 |
return config_set(&timeout, value); |
| 468 |
else |
| 469 |
return -1; |
| 470 |
} /* }}} int ascent_config */ |
| 471 |
|
| 472 |
static int ascent_init(void) /* {{{ */ |
| 473 |
{ |
| 474 |
if (url == NULL) { |
| 475 |
WARNING("ascent plugin: ascent_init: No URL configured, " |
| 476 |
"returning an error."); |
| 477 |
return -1; |
| 478 |
} |
| 479 |
|
| 480 |
if (curl != NULL) { |
| 481 |
curl_easy_cleanup(curl); |
| 482 |
} |
| 483 |
|
| 484 |
if ((curl = curl_easy_init()) == NULL) { |
| 485 |
ERROR("ascent plugin: ascent_init: curl_easy_init failed."); |
| 486 |
return -1; |
| 487 |
} |
| 488 |
|
| 489 |
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1L); |
| 490 |
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, ascent_curl_callback); |
| 491 |
curl_easy_setopt(curl, CURLOPT_USERAGENT, COLLECTD_USERAGENT); |
| 492 |
curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, ascent_curl_error); |
| 493 |
|
| 494 |
if (user != NULL) { |
| 495 |
#ifdef HAVE_CURLOPT_USERNAME |
| 496 |
curl_easy_setopt(curl, CURLOPT_USERNAME, user); |
| 497 |
curl_easy_setopt(curl, CURLOPT_PASSWORD, (pass == NULL) ? "" : pass); |
| 498 |
#else |
| 499 |
static char credentials[1024]; |
| 500 |
int status; |
| 501 |
|
| 502 |
status = ssnprintf(credentials, sizeof(credentials), "%s:%s", user, |
| 503 |
(pass == NULL) ? "" : pass); |
| 504 |
if ((status < 0) || ((size_t)status >= sizeof(credentials))) { |
| 505 |
ERROR("ascent plugin: ascent_init: Returning an error because the " |
| 506 |
"credentials have been truncated."); |
| 507 |
return -1; |
| 508 |
} |
| 509 |
|
| 510 |
curl_easy_setopt(curl, CURLOPT_USERPWD, credentials); |
| 511 |
#endif |
| 512 |
} |
| 513 |
|
| 514 |
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); |
| 515 |
curl_easy_setopt(curl, CURLOPT_MAXREDIRS, 50L); |
| 516 |
|
| 517 |
if ((verify_peer == NULL) || IS_TRUE(verify_peer)) |
| 518 |
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1L); |
| 519 |
else |
| 520 |
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L); |
| 521 |
|
| 522 |
if ((verify_host == NULL) || IS_TRUE(verify_host)) |
| 523 |
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 2L); |
| 524 |
else |
| 525 |
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L); |
| 526 |
|
| 527 |
if (cacert != NULL) |
| 528 |
curl_easy_setopt(curl, CURLOPT_CAINFO, cacert); |
| 529 |
|
| 530 |
#ifdef HAVE_CURLOPT_TIMEOUT_MS |
| 531 |
if (timeout != NULL) |
| 532 |
curl_easy_setopt(curl, CURLOPT_TIMEOUT_MS, atol(timeout)); |
| 533 |
else |
| 534 |
curl_easy_setopt(curl, CURLOPT_TIMEOUT_MS, |
| 535 |
(long)CDTIME_T_TO_MS(plugin_get_interval())); |
| 536 |
#endif |
| 537 |
|
| 538 |
return 0; |
| 539 |
} /* }}} int ascent_init */ |
| 540 |
|
| 541 |
static int ascent_read(void) /* {{{ */ |
| 542 |
{ |
| 543 |
int status; |
| 544 |
|
| 545 |
if (curl == NULL) { |
| 546 |
ERROR("ascent plugin: I don't have a CURL object."); |
| 547 |
return -1; |
| 548 |
} |
| 549 |
|
| 550 |
if (url == NULL) { |
| 551 |
ERROR("ascent plugin: No URL has been configured."); |
| 552 |
return -1; |
| 553 |
} |
| 554 |
|
| 555 |
ascent_buffer_fill = 0; |
| 556 |
|
| 557 |
curl_easy_setopt(curl, CURLOPT_URL, url); |
| 558 |
|
| 559 |
if (curl_easy_perform(curl) != CURLE_OK) { |
| 560 |
ERROR("ascent plugin: curl_easy_perform failed: %s", ascent_curl_error); |
| 561 |
return -1; |
| 562 |
} |
| 563 |
|
| 564 |
status = ascent_xml(ascent_buffer); |
| 565 |
if (status != 0) |
| 566 |
return -1; |
| 567 |
else |
| 568 |
return 0; |
| 569 |
} /* }}} int ascent_read */ |
| 570 |
|
| 571 |
void module_register(void) { |
| 572 |
plugin_register_config("ascent", ascent_config, config_keys, config_keys_num); |
| 573 |
plugin_register_init("ascent", ascent_init); |
| 574 |
plugin_register_read("ascent", ascent_read); |
| 575 |
} /* void module_register */ |