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>
31 #include "configfile.h"
33 #include <curl/curl.h>
34 #include <libxml/parser.h>
36 static const char *races_list[] = /* {{{ */
51 #define RACES_LIST_LENGTH STATIC_ARRAY_SIZE (races_list)
53 static const char *classes_list[] = /* {{{ */
68 #define CLASSES_LIST_LENGTH STATIC_ARRAY_SIZE (classes_list)
70 static const char *genders_list[] = /* {{{ */
75 #define GENDERS_LIST_LENGTH STATIC_ARRAY_SIZE (genders_list)
79 int races[RACES_LIST_LENGTH];
80 int classes[CLASSES_LIST_LENGTH];
81 int genders[GENDERS_LIST_LENGTH];
87 typedef struct player_stats_s player_stats_t;
97 typedef struct player_info_s player_info_t;
98 #define PLAYER_INFO_STATIC_INIT { -1, -1, -1, -1, -1 }
100 static char *url = NULL;
101 static char *user = NULL;
102 static char *pass = NULL;
103 static char *verify_peer = NULL;
104 static char *verify_host = NULL;
105 static char *cacert = NULL;
106 static char *timeout = NULL;
108 static CURL *curl = NULL;
110 static char *ascent_buffer = NULL;
111 static size_t ascent_buffer_size = 0;
112 static size_t ascent_buffer_fill = 0;
113 static char ascent_curl_error[CURL_ERROR_SIZE];
115 static const char *config_keys[] =
125 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
127 static int ascent_submit_gauge (const char *plugin_instance, /* {{{ */
128 const char *type, const char *type_instance, gauge_t value)
131 value_list_t vl = VALUE_LIST_INIT;
133 values[0].gauge = value;
137 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
138 sstrncpy (vl.plugin, "ascent", sizeof (vl.plugin));
140 if (plugin_instance != NULL)
141 sstrncpy (vl.plugin_instance, plugin_instance,
142 sizeof (vl.plugin_instance));
144 sstrncpy (vl.type, type, sizeof (vl.type));
146 if (type_instance != NULL)
147 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
149 plugin_dispatch_values (&vl);
151 } /* }}} int ascent_submit_gauge */
153 static size_t ascent_curl_callback (void *buf, size_t size, size_t nmemb, /* {{{ */
154 void __attribute__((unused)) *stream)
156 size_t len = size * nmemb;
161 if ((ascent_buffer_fill + len) >= ascent_buffer_size)
165 temp = realloc (ascent_buffer,
166 ascent_buffer_fill + len + 1);
169 ERROR ("ascent plugin: realloc failed.");
172 ascent_buffer = temp;
173 ascent_buffer_size = ascent_buffer_fill + len + 1;
176 memcpy (ascent_buffer + ascent_buffer_fill, (char *) buf, len);
177 ascent_buffer_fill += len;
178 ascent_buffer[ascent_buffer_fill] = 0;
181 } /* }}} size_t ascent_curl_callback */
183 static int ascent_submit_players (player_stats_t *ps) /* {{{ */
187 for (size_t i = 0; i < RACES_LIST_LENGTH; i++)
188 if (races_list[i] != NULL)
189 ascent_submit_gauge ("by-race", "players", races_list[i],
190 (gauge_t) ps->races[i]);
192 for (size_t i = 0; i < CLASSES_LIST_LENGTH; i++)
193 if (classes_list[i] != NULL)
194 ascent_submit_gauge ("by-class", "players", classes_list[i],
195 (gauge_t) ps->classes[i]);
197 for (size_t i = 0; i < GENDERS_LIST_LENGTH; i++)
198 if (genders_list[i] != NULL)
199 ascent_submit_gauge ("by-gender", "players", genders_list[i],
200 (gauge_t) ps->genders[i]);
202 if (ps->level_num <= 0)
205 value = ((double) ps->level_sum) / ((double) ps->level_num);
206 ascent_submit_gauge (NULL, "gauge", "avg-level", value);
208 /* Latency is in ms, but we store seconds. */
209 if (ps->latency_num <= 0)
212 value = ((double) ps->latency_sum) / (1000.0 * ((double) ps->latency_num));
213 ascent_submit_gauge (NULL, "latency", "average", value);
216 } /* }}} int ascent_submit_players */
218 static int ascent_account_player (player_stats_t *ps, /* {{{ */
223 if (((size_t) pi->race >= RACES_LIST_LENGTH)
224 || (races_list[pi->race] == NULL))
225 ERROR ("ascent plugin: Ignoring invalid numeric race %i.", pi->race);
227 ps->races[pi->race]++;
232 if (((size_t) pi->class >= CLASSES_LIST_LENGTH)
233 || (classes_list[pi->class] == NULL))
234 ERROR ("ascent plugin: Ignoring invalid numeric class %i.", pi->class);
236 ps->classes[pi->class]++;
241 if (((size_t) pi->gender >= GENDERS_LIST_LENGTH)
242 || (genders_list[pi->gender] == NULL))
243 ERROR ("ascent plugin: Ignoring invalid numeric gender %i.",
246 ps->genders[pi->gender]++;
252 ps->level_sum += pi->level;
256 if (pi->latency >= 0)
258 ps->latency_sum += pi->latency;
263 } /* }}} int ascent_account_player */
265 static int ascent_xml_submit_gauge (xmlDoc *doc, xmlNode *node, /* {{{ */
266 const char *plugin_instance, const char *type, const char *type_instance)
271 str_ptr = (char *) xmlNodeListGetString (doc, node->xmlChildrenNode, 1);
274 ERROR ("ascent plugin: ascent_xml_submit_gauge: xmlNodeListGetString failed.");
278 if (strcasecmp ("N/A", str_ptr) == 0)
282 char *end_ptr = NULL;
283 value = strtod (str_ptr, &end_ptr);
284 if (str_ptr == end_ptr)
287 ERROR ("ascent plugin: ascent_xml_submit_gauge: strtod failed.");
293 return (ascent_submit_gauge (plugin_instance, type, type_instance, value));
294 } /* }}} int ascent_xml_submit_gauge */
296 static int ascent_xml_read_int (xmlDoc *doc, xmlNode *node, /* {{{ */
302 str_ptr = (char *) xmlNodeListGetString (doc, node->xmlChildrenNode, 1);
305 ERROR ("ascent plugin: ascent_xml_read_int: xmlNodeListGetString failed.");
309 if (strcasecmp ("N/A", str_ptr) == 0)
313 char *end_ptr = NULL;
314 value = strtol (str_ptr, &end_ptr, 0);
315 if (str_ptr == end_ptr)
318 ERROR ("ascent plugin: ascent_xml_read_int: strtol failed.");
326 } /* }}} int ascent_xml_read_int */
328 static int ascent_xml_sessions_plr (xmlDoc *doc, xmlNode *node, /* {{{ */
331 for (xmlNode *child = node->xmlChildrenNode; child != NULL; child = child->next)
333 if ((xmlStrcmp ((const xmlChar *) "comment", child->name) == 0)
334 || (xmlStrcmp ((const xmlChar *) "text", child->name) == 0))
336 else if (xmlStrcmp ((const xmlChar *) "race", child->name) == 0)
337 ascent_xml_read_int (doc, child, &pi->race);
338 else if (xmlStrcmp ((const xmlChar *) "class", child->name) == 0)
339 ascent_xml_read_int (doc, child, &pi->class);
340 else if (xmlStrcmp ((const xmlChar *) "gender", child->name) == 0)
341 ascent_xml_read_int (doc, child, &pi->gender);
342 else if (xmlStrcmp ((const xmlChar *) "level", child->name) == 0)
343 ascent_xml_read_int (doc, child, &pi->level);
344 else if (xmlStrcmp ((const xmlChar *) "latency", child->name) == 0)
345 ascent_xml_read_int (doc, child, &pi->latency);
346 else if ((xmlStrcmp ((const xmlChar *) "name", child->name) == 0)
347 || (xmlStrcmp ((const xmlChar *) "pvprank", child->name) == 0)
348 || (xmlStrcmp ((const xmlChar *) "map", child->name) == 0)
349 || (xmlStrcmp ((const xmlChar *) "areaid", child->name) == 0)
350 || (xmlStrcmp ((const xmlChar *) "xpos", child->name) == 0)
351 || (xmlStrcmp ((const xmlChar *) "ypos", child->name) == 0)
352 || (xmlStrcmp ((const xmlChar *) "onime", child->name) == 0))
356 WARNING ("ascent plugin: ascent_xml_status: Unknown tag: %s", child->name);
361 } /* }}} int ascent_xml_sessions_plr */
363 static int ascent_xml_sessions (xmlDoc *doc, xmlNode *node) /* {{{ */
365 player_stats_t ps = {
369 for (xmlNode *child = node->xmlChildrenNode; child != NULL; child = child->next)
371 if ((xmlStrcmp ((const xmlChar *) "comment", child->name) == 0)
372 || (xmlStrcmp ((const xmlChar *) "text", child->name) == 0))
374 else if (xmlStrcmp ((const xmlChar *) "plr", child->name) == 0)
377 player_info_t pi = PLAYER_INFO_STATIC_INIT;
379 status = ascent_xml_sessions_plr (doc, child, &pi);
381 ascent_account_player (&ps, &pi);
385 WARNING ("ascent plugin: ascent_xml_status: Unknown tag: %s", child->name);
389 ascent_submit_players (&ps);
392 } /* }}} int ascent_xml_sessions */
394 static int ascent_xml_status (xmlDoc *doc, xmlNode *node) /* {{{ */
396 for (xmlNode *child = node->xmlChildrenNode; child != NULL; child = child->next)
398 if ((xmlStrcmp ((const xmlChar *) "comment", child->name) == 0)
399 || (xmlStrcmp ((const xmlChar *) "text", child->name) == 0))
401 else if (xmlStrcmp ((const xmlChar *) "alliance", child->name) == 0)
402 ascent_xml_submit_gauge (doc, child, NULL, "players", "alliance");
403 else if (xmlStrcmp ((const xmlChar *) "horde", child->name) == 0)
404 ascent_xml_submit_gauge (doc, child, NULL, "players", "horde");
405 else if (xmlStrcmp ((const xmlChar *) "qplayers", child->name) == 0)
406 ascent_xml_submit_gauge (doc, child, NULL, "players", "queued");
407 else if ((xmlStrcmp ((const xmlChar *) "acceptedconns", child->name) == 0)
408 || (xmlStrcmp ((const xmlChar *) "avglat", child->name) == 0)
409 || (xmlStrcmp ((const xmlChar *) "cdbquerysize", child->name) == 0)
410 || (xmlStrcmp ((const xmlChar *) "cpu", child->name) == 0)
411 || (xmlStrcmp ((const xmlChar *) "fthreads", child->name) == 0)
412 || (xmlStrcmp ((const xmlChar *) "gmcount", child->name) == 0)
413 || (xmlStrcmp ((const xmlChar *) "lastupdate", child->name) == 0)
414 || (xmlStrcmp ((const xmlChar *) "ontime", child->name) == 0)
415 || (xmlStrcmp ((const xmlChar *) "oplayers", child->name) == 0)
416 || (xmlStrcmp ((const xmlChar *) "peakcount", child->name) == 0)
417 || (xmlStrcmp ((const xmlChar *) "platform", child->name) == 0)
418 || (xmlStrcmp ((const xmlChar *) "ram", child->name) == 0)
419 || (xmlStrcmp ((const xmlChar *) "threads", child->name) == 0)
420 || (xmlStrcmp ((const xmlChar *) "uptime", child->name) == 0)
421 || (xmlStrcmp ((const xmlChar *) "wdbquerysize", child->name) == 0))
425 WARNING ("ascent plugin: ascent_xml_status: Unknown tag: %s", child->name);
430 } /* }}} int ascent_xml_status */
432 static int ascent_xml (const char *data) /* {{{ */
438 doc = xmlParseMemory (data, strlen (data),
439 /* URL = */ "ascent.xml",
440 /* encoding = */ NULL,
443 doc = xmlParseMemory (data, strlen (data));
447 ERROR ("ascent plugin: xmlParseMemory failed.");
451 cur = xmlDocGetRootElement (doc);
454 ERROR ("ascent plugin: XML document is empty.");
459 if (xmlStrcmp ((const xmlChar *) "serverpage", cur->name) != 0)
461 ERROR ("ascent plugin: XML root element is not \"serverpage\".");
466 for (xmlNode *child = cur->xmlChildrenNode; child != NULL; child = child->next)
468 if ((xmlStrcmp ((const xmlChar *) "comment", child->name) == 0)
469 || (xmlStrcmp ((const xmlChar *) "text", child->name) == 0))
471 else if (xmlStrcmp ((const xmlChar *) "status", child->name) == 0)
472 ascent_xml_status (doc, child);
473 else if (xmlStrcmp ((const xmlChar *) "instances", child->name) == 0)
474 /* ignore for now */;
475 else if (xmlStrcmp ((const xmlChar *) "gms", child->name) == 0)
476 /* ignore for now */;
477 else if (xmlStrcmp ((const xmlChar *) "sessions", child->name) == 0)
478 ascent_xml_sessions (doc, child);
481 WARNING ("ascent plugin: ascent_xml: Unknown tag: %s", child->name);
487 } /* }}} int ascent_xml */
489 static int config_set (char **var, const char *value) /* {{{ */
497 if ((*var = strdup (value)) == NULL)
501 } /* }}} int config_set */
503 static int ascent_config (const char *key, const char *value) /* {{{ */
505 if (strcasecmp (key, "URL") == 0)
506 return (config_set (&url, value));
507 else if (strcasecmp (key, "User") == 0)
508 return (config_set (&user, value));
509 else if (strcasecmp (key, "Password") == 0)
510 return (config_set (&pass, value));
511 else if (strcasecmp (key, "VerifyPeer") == 0)
512 return (config_set (&verify_peer, value));
513 else if (strcasecmp (key, "VerifyHost") == 0)
514 return (config_set (&verify_host, value));
515 else if (strcasecmp (key, "CACert") == 0)
516 return (config_set (&cacert, value));
517 else if (strcasecmp (key, "Timeout") == 0)
518 return (config_set (&timeout, value));
521 } /* }}} int ascent_config */
523 static int ascent_init (void) /* {{{ */
527 WARNING ("ascent plugin: ascent_init: No URL configured, "
528 "returning an error.");
534 curl_easy_cleanup (curl);
537 if ((curl = curl_easy_init ()) == NULL)
539 ERROR ("ascent plugin: ascent_init: curl_easy_init failed.");
543 curl_easy_setopt (curl, CURLOPT_NOSIGNAL, 1L);
544 curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, ascent_curl_callback);
545 curl_easy_setopt (curl, CURLOPT_USERAGENT, COLLECTD_USERAGENT);
546 curl_easy_setopt (curl, CURLOPT_ERRORBUFFER, ascent_curl_error);
550 #ifdef HAVE_CURLOPT_USERNAME
551 curl_easy_setopt (curl, CURLOPT_USERNAME, user);
552 curl_easy_setopt (curl, CURLOPT_PASSWORD, (pass == NULL) ? "" : pass);
554 static char credentials[1024];
557 status = ssnprintf (credentials, sizeof (credentials), "%s:%s",
558 user, (pass == NULL) ? "" : pass);
559 if ((status < 0) || ((size_t) status >= sizeof (credentials)))
561 ERROR ("ascent plugin: ascent_init: Returning an error because the "
562 "credentials have been truncated.");
566 curl_easy_setopt (curl, CURLOPT_USERPWD, credentials);
570 curl_easy_setopt (curl, CURLOPT_URL, url);
571 curl_easy_setopt (curl, CURLOPT_FOLLOWLOCATION, 1L);
572 curl_easy_setopt (curl, CURLOPT_MAXREDIRS, 50L);
574 if ((verify_peer == NULL) || IS_TRUE (verify_peer))
575 curl_easy_setopt (curl, CURLOPT_SSL_VERIFYPEER, 1L);
577 curl_easy_setopt (curl, CURLOPT_SSL_VERIFYPEER, 0L);
579 if ((verify_host == NULL) || IS_TRUE (verify_host))
580 curl_easy_setopt (curl, CURLOPT_SSL_VERIFYHOST, 2L);
582 curl_easy_setopt (curl, CURLOPT_SSL_VERIFYHOST, 0L);
585 curl_easy_setopt (curl, CURLOPT_CAINFO, cacert);
587 #ifdef HAVE_CURLOPT_TIMEOUT_MS
589 curl_easy_setopt (curl, CURLOPT_TIMEOUT_MS, atol(timeout));
591 curl_easy_setopt (curl, CURLOPT_TIMEOUT_MS, (long) CDTIME_T_TO_MS(plugin_get_interval()));
595 } /* }}} int ascent_init */
597 static int ascent_read (void) /* {{{ */
603 ERROR ("ascent plugin: I don't have a CURL object.");
609 ERROR ("ascent plugin: No URL has been configured.");
613 ascent_buffer_fill = 0;
614 if (curl_easy_perform (curl) != CURLE_OK)
616 ERROR ("ascent plugin: curl_easy_perform failed: %s",
621 status = ascent_xml (ascent_buffer);
626 } /* }}} int ascent_read */
628 void module_register (void)
630 plugin_register_config ("ascent", ascent_config, config_keys, config_keys_num);
631 plugin_register_init ("ascent", ascent_init);
632 plugin_register_read ("ascent", ascent_read);
633 } /* void module_register */
635 /* vim: set sw=2 sts=2 ts=8 et fdm=marker : */