2 * collectd - src/ascent.c
3 * Copyright (C) 2008 Florian octo Forster
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:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
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.
24 * Florian octo Forster <octo at collectd.org>
32 #include <curl/curl.h>
33 #include <libxml/parser.h>
35 static const char *races_list[] = /* {{{ */
37 NULL, "Human", /* 1 */
45 NULL, "Bloodelf", /* 10 */
48 #define RACES_LIST_LENGTH STATIC_ARRAY_SIZE(races_list)
50 static const char *classes_list[] = /* {{{ */
52 NULL, "Warrior", /* 1 */
57 NULL, "Shaman", /* 7 */
60 NULL, "Druid" /* 11 */
62 #define CLASSES_LIST_LENGTH STATIC_ARRAY_SIZE(classes_list)
64 static const char *genders_list[] = /* {{{ */
65 {"Male", "Female"}; /* }}} */
66 #define GENDERS_LIST_LENGTH STATIC_ARRAY_SIZE(genders_list)
68 struct player_stats_s {
69 int races[RACES_LIST_LENGTH];
70 int classes[CLASSES_LIST_LENGTH];
71 int genders[GENDERS_LIST_LENGTH];
77 typedef struct player_stats_s player_stats_t;
79 struct player_info_s {
86 typedef struct player_info_s player_info_t;
87 #define PLAYER_INFO_STATIC_INIT \
88 { -1, -1, -1, -1, -1 }
90 static char *url = NULL;
91 static char *user = NULL;
92 static char *pass = NULL;
93 static char *verify_peer = NULL;
94 static char *verify_host = NULL;
95 static char *cacert = NULL;
96 static char *timeout = NULL;
98 static CURL *curl = NULL;
100 static char *ascent_buffer = NULL;
101 static size_t ascent_buffer_size = 0;
102 static size_t ascent_buffer_fill = 0;
103 static char ascent_curl_error[CURL_ERROR_SIZE];
105 static const char *config_keys[] = {
106 "URL", "User", "Password", "VerifyPeer", "VerifyHost", "CACert", "Timeout",
108 static int config_keys_num = STATIC_ARRAY_SIZE(config_keys);
110 static int ascent_submit_gauge(const char *plugin_instance, /* {{{ */
111 const char *type, const char *type_instance,
113 value_list_t vl = VALUE_LIST_INIT;
115 vl.values = &(value_t){.gauge = value};
117 sstrncpy(vl.plugin, "ascent", sizeof(vl.plugin));
119 if (plugin_instance != NULL)
120 sstrncpy(vl.plugin_instance, plugin_instance, sizeof(vl.plugin_instance));
122 sstrncpy(vl.type, type, sizeof(vl.type));
124 if (type_instance != NULL)
125 sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
127 plugin_dispatch_values(&vl);
129 } /* }}} int ascent_submit_gauge */
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;
139 if ((ascent_buffer_fill + len) >= ascent_buffer_size) {
142 temp = realloc(ascent_buffer, ascent_buffer_fill + len + 1);
144 ERROR("ascent plugin: realloc failed.");
147 ascent_buffer = temp;
148 ascent_buffer_size = ascent_buffer_fill + len + 1;
151 memcpy(ascent_buffer + ascent_buffer_fill, (char *)buf, len);
152 ascent_buffer_fill += len;
153 ascent_buffer[ascent_buffer_fill] = 0;
156 } /* }}} size_t ascent_curl_callback */
158 static int ascent_submit_players(player_stats_t *ps) /* {{{ */
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]);
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]);
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]);
177 if (ps->level_num <= 0)
180 value = ((double)ps->level_sum) / ((double)ps->level_num);
181 ascent_submit_gauge(NULL, "gauge", "avg-level", value);
183 /* Latency is in ms, but we store seconds. */
184 if (ps->latency_num <= 0)
187 value = ((double)ps->latency_sum) / (1000.0 * ((double)ps->latency_num));
188 ascent_submit_gauge(NULL, "latency", "average", value);
191 } /* }}} int ascent_submit_players */
193 static int ascent_account_player(player_stats_t *ps, /* {{{ */
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);
200 ps->races[pi->race]++;
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);
208 ps->classes[pi->class]++;
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);
216 ps->genders[pi->gender]++;
220 ps->level_sum += pi->level;
224 if (pi->latency >= 0) {
225 ps->latency_sum += pi->latency;
230 } /* }}} int ascent_account_player */
232 static int ascent_xml_submit_gauge(xmlDoc *doc, xmlNode *node, /* {{{ */
233 const char *plugin_instance,
235 const char *type_instance) {
239 str_ptr = (char *)xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
240 if (str_ptr == NULL) {
242 "ascent plugin: ascent_xml_submit_gauge: xmlNodeListGetString failed.");
246 if (strcasecmp("N/A", str_ptr) == 0)
249 char *end_ptr = NULL;
250 value = strtod(str_ptr, &end_ptr);
251 if (str_ptr == end_ptr) {
253 ERROR("ascent plugin: ascent_xml_submit_gauge: strtod failed.");
259 return ascent_submit_gauge(plugin_instance, type, type_instance, value);
260 } /* }}} int ascent_xml_submit_gauge */
262 static int ascent_xml_read_int(xmlDoc *doc, xmlNode *node, /* {{{ */
267 str_ptr = (char *)xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
268 if (str_ptr == NULL) {
269 ERROR("ascent plugin: ascent_xml_read_int: xmlNodeListGetString failed.");
273 if (strcasecmp("N/A", str_ptr) == 0)
276 char *end_ptr = NULL;
277 value = strtol(str_ptr, &end_ptr, 0);
278 if (str_ptr == end_ptr) {
280 ERROR("ascent plugin: ascent_xml_read_int: strtol failed.");
288 } /* }}} int ascent_xml_read_int */
290 static int ascent_xml_sessions_plr(xmlDoc *doc, xmlNode *node, /* {{{ */
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))
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))
316 WARNING("ascent plugin: ascent_xml_status: Unknown tag: %s", child->name);
321 } /* }}} int ascent_xml_sessions_plr */
323 static int ascent_xml_sessions(xmlDoc *doc, xmlNode *node) /* {{{ */
325 player_stats_t ps = {.level_sum = 0};
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))
332 else if (xmlStrcmp((const xmlChar *)"plr", child->name) == 0) {
334 player_info_t pi = PLAYER_INFO_STATIC_INIT;
336 status = ascent_xml_sessions_plr(doc, child, &pi);
338 ascent_account_player(&ps, &pi);
340 WARNING("ascent plugin: ascent_xml_status: Unknown tag: %s", child->name);
344 ascent_submit_players(&ps);
347 } /* }}} int ascent_xml_sessions */
349 static int ascent_xml_status(xmlDoc *doc, xmlNode *node) /* {{{ */
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))
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))
379 WARNING("ascent plugin: ascent_xml_status: Unknown tag: %s", child->name);
384 } /* }}} int ascent_xml_status */
386 static int ascent_xml(const char *data) /* {{{ */
392 doc = xmlParseMemory (data, strlen (data),
393 /* URL = */ "ascent.xml",
394 /* encoding = */ NULL,
397 doc = xmlParseMemory(data, strlen(data));
400 ERROR("ascent plugin: xmlParseMemory failed.");
404 cur = xmlDocGetRootElement(doc);
406 ERROR("ascent plugin: XML document is empty.");
411 if (xmlStrcmp((const xmlChar *)"serverpage", cur->name) != 0) {
412 ERROR("ascent plugin: XML root element is not \"serverpage\".");
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))
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);
431 WARNING("ascent plugin: ascent_xml: Unknown tag: %s", child->name);
437 } /* }}} int ascent_xml */
439 static int config_set(char **var, const char *value) /* {{{ */
446 if ((*var = strdup(value)) == NULL)
450 } /* }}} int config_set */
452 static int ascent_config(const char *key, const char *value) /* {{{ */
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);
470 } /* }}} int ascent_config */
472 static int ascent_init(void) /* {{{ */
475 WARNING("ascent plugin: ascent_init: No URL configured, "
476 "returning an error.");
481 curl_easy_cleanup(curl);
484 if ((curl = curl_easy_init()) == NULL) {
485 ERROR("ascent plugin: ascent_init: curl_easy_init failed.");
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);
495 #ifdef HAVE_CURLOPT_USERNAME
496 curl_easy_setopt(curl, CURLOPT_USERNAME, user);
497 curl_easy_setopt(curl, CURLOPT_PASSWORD, (pass == NULL) ? "" : pass);
499 static char credentials[1024];
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.");
510 curl_easy_setopt(curl, CURLOPT_USERPWD, credentials);
514 curl_easy_setopt(curl, CURLOPT_URL, url);
515 curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
516 curl_easy_setopt(curl, CURLOPT_MAXREDIRS, 50L);
518 if ((verify_peer == NULL) || IS_TRUE(verify_peer))
519 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1L);
521 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
523 if ((verify_host == NULL) || IS_TRUE(verify_host))
524 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 2L);
526 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
529 curl_easy_setopt(curl, CURLOPT_CAINFO, cacert);
531 #ifdef HAVE_CURLOPT_TIMEOUT_MS
533 curl_easy_setopt(curl, CURLOPT_TIMEOUT_MS, atol(timeout));
535 curl_easy_setopt(curl, CURLOPT_TIMEOUT_MS,
536 (long)CDTIME_T_TO_MS(plugin_get_interval()));
540 } /* }}} int ascent_init */
542 static int ascent_read(void) /* {{{ */
547 ERROR("ascent plugin: I don't have a CURL object.");
552 ERROR("ascent plugin: No URL has been configured.");
556 ascent_buffer_fill = 0;
557 if (curl_easy_perform(curl) != CURLE_OK) {
558 ERROR("ascent plugin: curl_easy_perform failed: %s", ascent_curl_error);
562 status = ascent_xml(ascent_buffer);
567 } /* }}} int ascent_read */
569 void module_register(void) {
570 plugin_register_config("ascent", ascent_config, config_keys, config_keys_num);
571 plugin_register_init("ascent", ascent_init);
572 plugin_register_read("ascent", ascent_read);
573 } /* void module_register */