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>
30 #include "configfile.h"
32 #include <curl/curl.h>
33 #include <libxml/parser.h>
35 static char *races_list[] = /* {{{ */
50 #define RACES_LIST_LENGTH STATIC_ARRAY_SIZE (races_list)
52 static char *classes_list[] = /* {{{ */
67 #define CLASSES_LIST_LENGTH STATIC_ARRAY_SIZE (classes_list)
69 static char *genders_list[] = /* {{{ */
74 #define GENDERS_LIST_LENGTH STATIC_ARRAY_SIZE (genders_list)
78 int races[RACES_LIST_LENGTH];
79 int classes[CLASSES_LIST_LENGTH];
80 int genders[GENDERS_LIST_LENGTH];
86 typedef struct player_stats_s player_stats_t;
96 typedef struct player_info_s player_info_t;
97 #define PLAYER_INFO_STATIC_INIT { -1, -1, -1, -1, -1 }
99 static char *url = NULL;
100 static char *user = NULL;
101 static char *pass = NULL;
102 static char *verify_peer = NULL;
103 static char *verify_host = NULL;
104 static char *cacert = NULL;
106 static CURL *curl = NULL;
108 static char *ascent_buffer = NULL;
109 static size_t ascent_buffer_size = 0;
110 static size_t ascent_buffer_fill = 0;
111 static char ascent_curl_error[CURL_ERROR_SIZE];
113 static const char *config_keys[] =
122 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
124 static int ascent_submit_gauge (const char *plugin_instance, /* {{{ */
125 const char *type, const char *type_instance, gauge_t value)
128 value_list_t vl = VALUE_LIST_INIT;
130 values[0].gauge = value;
134 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
135 sstrncpy (vl.plugin, "ascent", sizeof (vl.plugin));
137 if (plugin_instance != NULL)
138 sstrncpy (vl.plugin_instance, plugin_instance,
139 sizeof (vl.plugin_instance));
141 sstrncpy (vl.type, type, sizeof (vl.type));
143 if (type_instance != NULL)
144 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
146 plugin_dispatch_values (&vl);
148 } /* }}} int ascent_submit_gauge */
150 static size_t ascent_curl_callback (void *buf, size_t size, size_t nmemb, /* {{{ */
151 void __attribute__((unused)) *stream)
153 size_t len = size * nmemb;
158 if ((ascent_buffer_fill + len) >= ascent_buffer_size)
162 temp = (char *) realloc (ascent_buffer,
163 ascent_buffer_fill + len + 1);
166 ERROR ("ascent plugin: realloc failed.");
169 ascent_buffer = temp;
170 ascent_buffer_size = ascent_buffer_fill + len + 1;
173 memcpy (ascent_buffer + ascent_buffer_fill, (char *) buf, len);
174 ascent_buffer_fill += len;
175 ascent_buffer[ascent_buffer_fill] = 0;
178 } /* }}} size_t ascent_curl_callback */
180 static int ascent_submit_players (player_stats_t *ps) /* {{{ */
185 for (i = 0; i < RACES_LIST_LENGTH; i++)
186 if (races_list[i] != NULL)
187 ascent_submit_gauge ("by-race", "players", races_list[i],
188 (gauge_t) ps->races[i]);
190 for (i = 0; i < CLASSES_LIST_LENGTH; i++)
191 if (classes_list[i] != NULL)
192 ascent_submit_gauge ("by-class", "players", classes_list[i],
193 (gauge_t) ps->classes[i]);
195 for (i = 0; i < GENDERS_LIST_LENGTH; i++)
196 if (genders_list[i] != NULL)
197 ascent_submit_gauge ("by-gender", "players", genders_list[i],
198 (gauge_t) ps->genders[i]);
200 if (ps->level_num <= 0)
203 value = ((double) ps->level_sum) / ((double) ps->level_num);
204 ascent_submit_gauge (NULL, "gauge", "avg-level", value);
206 /* Latency is in ms, but we store seconds. */
207 if (ps->latency_num <= 0)
210 value = ((double) ps->latency_sum) / (1000.0 * ((double) ps->latency_num));
211 ascent_submit_gauge (NULL, "latency", "average", value);
214 } /* }}} int ascent_submit_players */
216 static int ascent_account_player (player_stats_t *ps, /* {{{ */
221 if (((size_t) pi->race >= RACES_LIST_LENGTH)
222 || (races_list[pi->race] == NULL))
223 ERROR ("ascent plugin: Ignoring invalid numeric race %i.", pi->race);
225 ps->races[pi->race]++;
230 if (((size_t) pi->class >= CLASSES_LIST_LENGTH)
231 || (classes_list[pi->class] == NULL))
232 ERROR ("ascent plugin: Ignoring invalid numeric class %i.", pi->class);
234 ps->classes[pi->class]++;
239 if (((size_t) pi->gender >= GENDERS_LIST_LENGTH)
240 || (genders_list[pi->gender] == NULL))
241 ERROR ("ascent plugin: Ignoring invalid numeric gender %i.",
244 ps->genders[pi->gender]++;
250 ps->level_sum += pi->level;
254 if (pi->latency >= 0)
256 ps->latency_sum += pi->latency;
261 } /* }}} int ascent_account_player */
263 static int ascent_xml_submit_gauge (xmlDoc *doc, xmlNode *node, /* {{{ */
264 const char *plugin_instance, const char *type, const char *type_instance)
269 str_ptr = (char *) xmlNodeListGetString (doc, node->xmlChildrenNode, 1);
272 ERROR ("ascent plugin: ascent_xml_submit_gauge: xmlNodeListGetString failed.");
276 if (strcasecmp ("N/A", str_ptr) == 0)
280 char *end_ptr = NULL;
281 value = strtod (str_ptr, &end_ptr);
282 if (str_ptr == end_ptr)
285 ERROR ("ascent plugin: ascent_xml_submit_gauge: strtod failed.");
291 return (ascent_submit_gauge (plugin_instance, type, type_instance, value));
292 } /* }}} int ascent_xml_submit_gauge */
294 static int ascent_xml_read_int (xmlDoc *doc, xmlNode *node, /* {{{ */
300 str_ptr = (char *) xmlNodeListGetString (doc, node->xmlChildrenNode, 1);
303 ERROR ("ascent plugin: ascent_xml_read_int: xmlNodeListGetString failed.");
307 if (strcasecmp ("N/A", str_ptr) == 0)
311 char *end_ptr = NULL;
312 value = strtol (str_ptr, &end_ptr, 0);
313 if (str_ptr == end_ptr)
316 ERROR ("ascent plugin: ascent_xml_read_int: strtol failed.");
324 } /* }}} int ascent_xml_read_int */
326 static int ascent_xml_sessions_plr (xmlDoc *doc, xmlNode *node, /* {{{ */
331 for (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) /* {{{ */
368 memset (&ps, 0, sizeof (ps));
370 for (child = node->xmlChildrenNode; child != NULL; child = child->next)
372 if ((xmlStrcmp ((const xmlChar *) "comment", child->name) == 0)
373 || (xmlStrcmp ((const xmlChar *) "text", child->name) == 0))
375 else if (xmlStrcmp ((const xmlChar *) "plr", child->name) == 0)
378 player_info_t pi = PLAYER_INFO_STATIC_INIT;
380 status = ascent_xml_sessions_plr (doc, child, &pi);
382 ascent_account_player (&ps, &pi);
386 WARNING ("ascent plugin: ascent_xml_status: Unknown tag: %s", child->name);
390 ascent_submit_players (&ps);
393 } /* }}} int ascent_xml_sessions */
395 static int ascent_xml_status (xmlDoc *doc, xmlNode *node) /* {{{ */
399 for (child = node->xmlChildrenNode; child != NULL; child = child->next)
401 if ((xmlStrcmp ((const xmlChar *) "comment", child->name) == 0)
402 || (xmlStrcmp ((const xmlChar *) "text", child->name) == 0))
404 else if (xmlStrcmp ((const xmlChar *) "alliance", child->name) == 0)
405 ascent_xml_submit_gauge (doc, child, NULL, "players", "alliance");
406 else if (xmlStrcmp ((const xmlChar *) "horde", child->name) == 0)
407 ascent_xml_submit_gauge (doc, child, NULL, "players", "horde");
408 else if (xmlStrcmp ((const xmlChar *) "qplayers", child->name) == 0)
409 ascent_xml_submit_gauge (doc, child, NULL, "players", "queued");
410 else if ((xmlStrcmp ((const xmlChar *) "acceptedconns", child->name) == 0)
411 || (xmlStrcmp ((const xmlChar *) "avglat", child->name) == 0)
412 || (xmlStrcmp ((const xmlChar *) "cdbquerysize", child->name) == 0)
413 || (xmlStrcmp ((const xmlChar *) "cpu", child->name) == 0)
414 || (xmlStrcmp ((const xmlChar *) "fthreads", child->name) == 0)
415 || (xmlStrcmp ((const xmlChar *) "gmcount", child->name) == 0)
416 || (xmlStrcmp ((const xmlChar *) "lastupdate", child->name) == 0)
417 || (xmlStrcmp ((const xmlChar *) "ontime", child->name) == 0)
418 || (xmlStrcmp ((const xmlChar *) "oplayers", child->name) == 0)
419 || (xmlStrcmp ((const xmlChar *) "peakcount", child->name) == 0)
420 || (xmlStrcmp ((const xmlChar *) "platform", child->name) == 0)
421 || (xmlStrcmp ((const xmlChar *) "ram", child->name) == 0)
422 || (xmlStrcmp ((const xmlChar *) "threads", child->name) == 0)
423 || (xmlStrcmp ((const xmlChar *) "uptime", child->name) == 0)
424 || (xmlStrcmp ((const xmlChar *) "wdbquerysize", child->name) == 0))
428 WARNING ("ascent plugin: ascent_xml_status: Unknown tag: %s", child->name);
433 } /* }}} int ascent_xml_status */
435 static int ascent_xml (const char *data) /* {{{ */
442 doc = xmlParseMemory (data, strlen (data),
443 /* URL = */ "ascent.xml",
444 /* encoding = */ NULL,
447 doc = xmlParseMemory (data, strlen (data));
451 ERROR ("ascent plugin: xmlParseMemory failed.");
455 cur = xmlDocGetRootElement (doc);
458 ERROR ("ascent plugin: XML document is empty.");
463 if (xmlStrcmp ((const xmlChar *) "serverpage", cur->name) != 0)
465 ERROR ("ascent plugin: XML root element is not \"serverpage\".");
470 for (child = cur->xmlChildrenNode; child != NULL; child = child->next)
472 if ((xmlStrcmp ((const xmlChar *) "comment", child->name) == 0)
473 || (xmlStrcmp ((const xmlChar *) "text", child->name) == 0))
475 else if (xmlStrcmp ((const xmlChar *) "status", child->name) == 0)
476 ascent_xml_status (doc, child);
477 else if (xmlStrcmp ((const xmlChar *) "instances", child->name) == 0)
478 /* ignore for now */;
479 else if (xmlStrcmp ((const xmlChar *) "gms", child->name) == 0)
480 /* ignore for now */;
481 else if (xmlStrcmp ((const xmlChar *) "sessions", child->name) == 0)
482 ascent_xml_sessions (doc, child);
485 WARNING ("ascent plugin: ascent_xml: Unknown tag: %s", child->name);
491 } /* }}} int ascent_xml */
493 static int config_set (char **var, const char *value) /* {{{ */
501 if ((*var = strdup (value)) == NULL)
505 } /* }}} int config_set */
507 static int ascent_config (const char *key, const char *value) /* {{{ */
509 if (strcasecmp (key, "URL") == 0)
510 return (config_set (&url, value));
511 else if (strcasecmp (key, "User") == 0)
512 return (config_set (&user, value));
513 else if (strcasecmp (key, "Password") == 0)
514 return (config_set (&pass, value));
515 else if (strcasecmp (key, "VerifyPeer") == 0)
516 return (config_set (&verify_peer, value));
517 else if (strcasecmp (key, "VerifyHost") == 0)
518 return (config_set (&verify_host, value));
519 else if (strcasecmp (key, "CACert") == 0)
520 return (config_set (&cacert, value));
523 } /* }}} int ascent_config */
525 static int ascent_init (void) /* {{{ */
527 static char credentials[1024];
531 WARNING ("ascent plugin: ascent_init: No URL configured, "
532 "returning an error.");
538 curl_easy_cleanup (curl);
541 if ((curl = curl_easy_init ()) == NULL)
543 ERROR ("ascent plugin: ascent_init: curl_easy_init failed.");
547 curl_easy_setopt (curl, CURLOPT_NOSIGNAL, 1L);
548 curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, ascent_curl_callback);
549 curl_easy_setopt (curl, CURLOPT_USERAGENT, COLLECTD_USERAGENT);
550 curl_easy_setopt (curl, CURLOPT_ERRORBUFFER, ascent_curl_error);
556 status = ssnprintf (credentials, sizeof (credentials), "%s:%s",
557 user, (pass == NULL) ? "" : pass);
558 if ((status < 0) || ((size_t) status >= sizeof (credentials)))
560 ERROR ("ascent plugin: ascent_init: Returning an error because the "
561 "credentials have been truncated.");
565 curl_easy_setopt (curl, CURLOPT_USERPWD, credentials);
568 curl_easy_setopt (curl, CURLOPT_URL, url);
569 curl_easy_setopt (curl, CURLOPT_FOLLOWLOCATION, 1L);
570 curl_easy_setopt (curl, CURLOPT_MAXREDIRS, 50L);
572 if ((verify_peer == NULL) || IS_TRUE (verify_peer))
573 curl_easy_setopt (curl, CURLOPT_SSL_VERIFYPEER, 1L);
575 curl_easy_setopt (curl, CURLOPT_SSL_VERIFYPEER, 0L);
577 if ((verify_host == NULL) || IS_TRUE (verify_host))
578 curl_easy_setopt (curl, CURLOPT_SSL_VERIFYHOST, 2L);
580 curl_easy_setopt (curl, CURLOPT_SSL_VERIFYHOST, 0L);
583 curl_easy_setopt (curl, CURLOPT_CAINFO, cacert);
586 } /* }}} int ascent_init */
588 static int ascent_read (void) /* {{{ */
594 ERROR ("ascent plugin: I don't have a CURL object.");
600 ERROR ("ascent plugin: No URL has been configured.");
604 ascent_buffer_fill = 0;
605 if (curl_easy_perform (curl) != CURLE_OK)
607 ERROR ("ascent plugin: curl_easy_perform failed: %s",
612 status = ascent_xml (ascent_buffer);
617 } /* }}} int ascent_read */
619 void module_register (void)
621 plugin_register_config ("ascent", ascent_config, config_keys, config_keys_num);
622 plugin_register_init ("ascent", ascent_init);
623 plugin_register_read ("ascent", ascent_read);
624 } /* void module_register */
626 /* vim: set sw=2 sts=2 ts=8 et fdm=marker : */