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 **/
27 #include "collectd.h"
29 #include "common.h"
30 #include "plugin.h"
31 #include "configfile.h"
33 #include <curl/curl.h>
34 #include <libxml/parser.h>
36 static const char *races_list[] = /* {{{ */
37 {
38 NULL,
39 "Human", /* 1 */
40 "Orc", /* 2 */
41 "Dwarf", /* 3 */
42 "Nightelf", /* 4 */
43 "Undead", /* 5 */
44 "Tauren", /* 6 */
45 "Gnome", /* 7 */
46 "Troll", /* 8 */
47 NULL,
48 "Bloodelf", /* 10 */
49 "Draenei" /* 11 */
50 }; /* }}} */
51 #define RACES_LIST_LENGTH STATIC_ARRAY_SIZE (races_list)
53 static const char *classes_list[] = /* {{{ */
54 {
55 NULL,
56 "Warrior", /* 1 */
57 "Paladin", /* 2 */
58 "Hunter", /* 3 */
59 "Rogue", /* 4 */
60 "Priest", /* 5 */
61 NULL,
62 "Shaman", /* 7 */
63 "Mage", /* 8 */
64 "Warlock", /* 9 */
65 NULL,
66 "Druid" /* 11 */
67 }; /* }}} */
68 #define CLASSES_LIST_LENGTH STATIC_ARRAY_SIZE (classes_list)
70 static const char *genders_list[] = /* {{{ */
71 {
72 "Male",
73 "Female"
74 }; /* }}} */
75 #define GENDERS_LIST_LENGTH STATIC_ARRAY_SIZE (genders_list)
77 struct player_stats_s
78 {
79 int races[RACES_LIST_LENGTH];
80 int classes[CLASSES_LIST_LENGTH];
81 int genders[GENDERS_LIST_LENGTH];
82 int level_sum;
83 int level_num;
84 int latency_sum;
85 int latency_num;
86 };
87 typedef struct player_stats_s player_stats_t;
89 struct player_info_s
90 {
91 int race;
92 int class;
93 int gender;
94 int level;
95 int latency;
96 };
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[] =
116 {
117 "URL",
118 "User",
119 "Password",
120 "VerifyPeer",
121 "VerifyHost",
122 "CACert",
123 "Timeout",
124 };
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)
129 {
130 value_t values[1];
131 value_list_t vl = VALUE_LIST_INIT;
133 values[0].gauge = value;
135 vl.values = values;
136 vl.values_len = 1;
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);
150 return (0);
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)
155 {
156 size_t len = size * nmemb;
158 if (len == 0)
159 return (len);
161 if ((ascent_buffer_fill + len) >= ascent_buffer_size)
162 {
163 char *temp;
165 temp = realloc (ascent_buffer,
166 ascent_buffer_fill + len + 1);
167 if (temp == NULL)
168 {
169 ERROR ("ascent plugin: realloc failed.");
170 return (0);
171 }
172 ascent_buffer = temp;
173 ascent_buffer_size = ascent_buffer_fill + len + 1;
174 }
176 memcpy (ascent_buffer + ascent_buffer_fill, (char *) buf, len);
177 ascent_buffer_fill += len;
178 ascent_buffer[ascent_buffer_fill] = 0;
180 return (len);
181 } /* }}} size_t ascent_curl_callback */
183 static int ascent_submit_players (player_stats_t *ps) /* {{{ */
184 {
185 size_t i;
186 gauge_t value;
188 for (i = 0; i < RACES_LIST_LENGTH; i++)
189 if (races_list[i] != NULL)
190 ascent_submit_gauge ("by-race", "players", races_list[i],
191 (gauge_t) ps->races[i]);
193 for (i = 0; i < CLASSES_LIST_LENGTH; i++)
194 if (classes_list[i] != NULL)
195 ascent_submit_gauge ("by-class", "players", classes_list[i],
196 (gauge_t) ps->classes[i]);
198 for (i = 0; i < GENDERS_LIST_LENGTH; i++)
199 if (genders_list[i] != NULL)
200 ascent_submit_gauge ("by-gender", "players", genders_list[i],
201 (gauge_t) ps->genders[i]);
203 if (ps->level_num <= 0)
204 value = NAN;
205 else
206 value = ((double) ps->level_sum) / ((double) ps->level_num);
207 ascent_submit_gauge (NULL, "gauge", "avg-level", value);
209 /* Latency is in ms, but we store seconds. */
210 if (ps->latency_num <= 0)
211 value = NAN;
212 else
213 value = ((double) ps->latency_sum) / (1000.0 * ((double) ps->latency_num));
214 ascent_submit_gauge (NULL, "latency", "average", value);
216 return (0);
217 } /* }}} int ascent_submit_players */
219 static int ascent_account_player (player_stats_t *ps, /* {{{ */
220 player_info_t *pi)
221 {
222 if (pi->race >= 0)
223 {
224 if (((size_t) pi->race >= RACES_LIST_LENGTH)
225 || (races_list[pi->race] == NULL))
226 ERROR ("ascent plugin: Ignoring invalid numeric race %i.", pi->race);
227 else
228 ps->races[pi->race]++;
229 }
231 if (pi->class >= 0)
232 {
233 if (((size_t) pi->class >= CLASSES_LIST_LENGTH)
234 || (classes_list[pi->class] == NULL))
235 ERROR ("ascent plugin: Ignoring invalid numeric class %i.", pi->class);
236 else
237 ps->classes[pi->class]++;
238 }
240 if (pi->gender >= 0)
241 {
242 if (((size_t) pi->gender >= GENDERS_LIST_LENGTH)
243 || (genders_list[pi->gender] == NULL))
244 ERROR ("ascent plugin: Ignoring invalid numeric gender %i.",
245 pi->gender);
246 else
247 ps->genders[pi->gender]++;
248 }
251 if (pi->level > 0)
252 {
253 ps->level_sum += pi->level;
254 ps->level_num++;
255 }
257 if (pi->latency >= 0)
258 {
259 ps->latency_sum += pi->latency;
260 ps->latency_num++;
261 }
263 return (0);
264 } /* }}} int ascent_account_player */
266 static int ascent_xml_submit_gauge (xmlDoc *doc, xmlNode *node, /* {{{ */
267 const char *plugin_instance, const char *type, const char *type_instance)
268 {
269 char *str_ptr;
270 gauge_t value;
272 str_ptr = (char *) xmlNodeListGetString (doc, node->xmlChildrenNode, 1);
273 if (str_ptr == NULL)
274 {
275 ERROR ("ascent plugin: ascent_xml_submit_gauge: xmlNodeListGetString failed.");
276 return (-1);
277 }
279 if (strcasecmp ("N/A", str_ptr) == 0)
280 value = NAN;
281 else
282 {
283 char *end_ptr = NULL;
284 value = strtod (str_ptr, &end_ptr);
285 if (str_ptr == end_ptr)
286 {
287 xmlFree(str_ptr);
288 ERROR ("ascent plugin: ascent_xml_submit_gauge: strtod failed.");
289 return (-1);
290 }
291 }
292 xmlFree(str_ptr);
294 return (ascent_submit_gauge (plugin_instance, type, type_instance, value));
295 } /* }}} int ascent_xml_submit_gauge */
297 static int ascent_xml_read_int (xmlDoc *doc, xmlNode *node, /* {{{ */
298 int *ret_value)
299 {
300 char *str_ptr;
301 int value;
303 str_ptr = (char *) xmlNodeListGetString (doc, node->xmlChildrenNode, 1);
304 if (str_ptr == NULL)
305 {
306 ERROR ("ascent plugin: ascent_xml_read_int: xmlNodeListGetString failed.");
307 return (-1);
308 }
310 if (strcasecmp ("N/A", str_ptr) == 0)
311 value = -1;
312 else
313 {
314 char *end_ptr = NULL;
315 value = strtol (str_ptr, &end_ptr, 0);
316 if (str_ptr == end_ptr)
317 {
318 xmlFree(str_ptr);
319 ERROR ("ascent plugin: ascent_xml_read_int: strtol failed.");
320 return (-1);
321 }
322 }
323 xmlFree(str_ptr);
325 *ret_value = value;
326 return (0);
327 } /* }}} int ascent_xml_read_int */
329 static int ascent_xml_sessions_plr (xmlDoc *doc, xmlNode *node, /* {{{ */
330 player_info_t *pi)
331 {
332 xmlNode *child;
334 for (child = node->xmlChildrenNode; child != NULL; child = child->next)
335 {
336 if ((xmlStrcmp ((const xmlChar *) "comment", child->name) == 0)
337 || (xmlStrcmp ((const xmlChar *) "text", child->name) == 0))
338 /* ignore */;
339 else if (xmlStrcmp ((const xmlChar *) "race", child->name) == 0)
340 ascent_xml_read_int (doc, child, &pi->race);
341 else if (xmlStrcmp ((const xmlChar *) "class", child->name) == 0)
342 ascent_xml_read_int (doc, child, &pi->class);
343 else if (xmlStrcmp ((const xmlChar *) "gender", child->name) == 0)
344 ascent_xml_read_int (doc, child, &pi->gender);
345 else if (xmlStrcmp ((const xmlChar *) "level", child->name) == 0)
346 ascent_xml_read_int (doc, child, &pi->level);
347 else if (xmlStrcmp ((const xmlChar *) "latency", child->name) == 0)
348 ascent_xml_read_int (doc, child, &pi->latency);
349 else if ((xmlStrcmp ((const xmlChar *) "name", child->name) == 0)
350 || (xmlStrcmp ((const xmlChar *) "pvprank", child->name) == 0)
351 || (xmlStrcmp ((const xmlChar *) "map", child->name) == 0)
352 || (xmlStrcmp ((const xmlChar *) "areaid", child->name) == 0)
353 || (xmlStrcmp ((const xmlChar *) "xpos", child->name) == 0)
354 || (xmlStrcmp ((const xmlChar *) "ypos", child->name) == 0)
355 || (xmlStrcmp ((const xmlChar *) "onime", child->name) == 0))
356 /* ignore */;
357 else
358 {
359 WARNING ("ascent plugin: ascent_xml_status: Unknown tag: %s", child->name);
360 }
361 } /* for (child) */
363 return (0);
364 } /* }}} int ascent_xml_sessions_plr */
366 static int ascent_xml_sessions (xmlDoc *doc, xmlNode *node) /* {{{ */
367 {
368 xmlNode *child;
369 player_stats_t ps = {
370 .level_sum = 0
371 };
373 for (child = node->xmlChildrenNode; child != NULL; child = child->next)
374 {
375 if ((xmlStrcmp ((const xmlChar *) "comment", child->name) == 0)
376 || (xmlStrcmp ((const xmlChar *) "text", child->name) == 0))
377 /* ignore */;
378 else if (xmlStrcmp ((const xmlChar *) "plr", child->name) == 0)
379 {
380 int status;
381 player_info_t pi = PLAYER_INFO_STATIC_INIT;
383 status = ascent_xml_sessions_plr (doc, child, &pi);
384 if (status == 0)
385 ascent_account_player (&ps, &pi);
386 }
387 else
388 {
389 WARNING ("ascent plugin: ascent_xml_status: Unknown tag: %s", child->name);
390 }
391 } /* for (child) */
393 ascent_submit_players (&ps);
395 return (0);
396 } /* }}} int ascent_xml_sessions */
398 static int ascent_xml_status (xmlDoc *doc, xmlNode *node) /* {{{ */
399 {
400 xmlNode *child;
402 for (child = node->xmlChildrenNode; child != NULL; child = child->next)
403 {
404 if ((xmlStrcmp ((const xmlChar *) "comment", child->name) == 0)
405 || (xmlStrcmp ((const xmlChar *) "text", child->name) == 0))
406 /* ignore */;
407 else if (xmlStrcmp ((const xmlChar *) "alliance", child->name) == 0)
408 ascent_xml_submit_gauge (doc, child, NULL, "players", "alliance");
409 else if (xmlStrcmp ((const xmlChar *) "horde", child->name) == 0)
410 ascent_xml_submit_gauge (doc, child, NULL, "players", "horde");
411 else if (xmlStrcmp ((const xmlChar *) "qplayers", child->name) == 0)
412 ascent_xml_submit_gauge (doc, child, NULL, "players", "queued");
413 else if ((xmlStrcmp ((const xmlChar *) "acceptedconns", child->name) == 0)
414 || (xmlStrcmp ((const xmlChar *) "avglat", child->name) == 0)
415 || (xmlStrcmp ((const xmlChar *) "cdbquerysize", child->name) == 0)
416 || (xmlStrcmp ((const xmlChar *) "cpu", child->name) == 0)
417 || (xmlStrcmp ((const xmlChar *) "fthreads", child->name) == 0)
418 || (xmlStrcmp ((const xmlChar *) "gmcount", child->name) == 0)
419 || (xmlStrcmp ((const xmlChar *) "lastupdate", child->name) == 0)
420 || (xmlStrcmp ((const xmlChar *) "ontime", child->name) == 0)
421 || (xmlStrcmp ((const xmlChar *) "oplayers", child->name) == 0)
422 || (xmlStrcmp ((const xmlChar *) "peakcount", child->name) == 0)
423 || (xmlStrcmp ((const xmlChar *) "platform", child->name) == 0)
424 || (xmlStrcmp ((const xmlChar *) "ram", child->name) == 0)
425 || (xmlStrcmp ((const xmlChar *) "threads", child->name) == 0)
426 || (xmlStrcmp ((const xmlChar *) "uptime", child->name) == 0)
427 || (xmlStrcmp ((const xmlChar *) "wdbquerysize", child->name) == 0))
428 /* ignore */;
429 else
430 {
431 WARNING ("ascent plugin: ascent_xml_status: Unknown tag: %s", child->name);
432 }
433 } /* for (child) */
435 return (0);
436 } /* }}} int ascent_xml_status */
438 static int ascent_xml (const char *data) /* {{{ */
439 {
440 xmlDoc *doc;
441 xmlNode *cur;
442 xmlNode *child;
444 #if 0
445 doc = xmlParseMemory (data, strlen (data),
446 /* URL = */ "ascent.xml",
447 /* encoding = */ NULL,
448 /* options = */ 0);
449 #else
450 doc = xmlParseMemory (data, strlen (data));
451 #endif
452 if (doc == NULL)
453 {
454 ERROR ("ascent plugin: xmlParseMemory failed.");
455 return (-1);
456 }
458 cur = xmlDocGetRootElement (doc);
459 if (cur == NULL)
460 {
461 ERROR ("ascent plugin: XML document is empty.");
462 xmlFreeDoc (doc);
463 return (-1);
464 }
466 if (xmlStrcmp ((const xmlChar *) "serverpage", cur->name) != 0)
467 {
468 ERROR ("ascent plugin: XML root element is not \"serverpage\".");
469 xmlFreeDoc (doc);
470 return (-1);
471 }
473 for (child = cur->xmlChildrenNode; child != NULL; child = child->next)
474 {
475 if ((xmlStrcmp ((const xmlChar *) "comment", child->name) == 0)
476 || (xmlStrcmp ((const xmlChar *) "text", child->name) == 0))
477 /* ignore */;
478 else if (xmlStrcmp ((const xmlChar *) "status", child->name) == 0)
479 ascent_xml_status (doc, child);
480 else if (xmlStrcmp ((const xmlChar *) "instances", child->name) == 0)
481 /* ignore for now */;
482 else if (xmlStrcmp ((const xmlChar *) "gms", child->name) == 0)
483 /* ignore for now */;
484 else if (xmlStrcmp ((const xmlChar *) "sessions", child->name) == 0)
485 ascent_xml_sessions (doc, child);
486 else
487 {
488 WARNING ("ascent plugin: ascent_xml: Unknown tag: %s", child->name);
489 }
490 } /* for (child) */
492 xmlFreeDoc (doc);
493 return (0);
494 } /* }}} int ascent_xml */
496 static int config_set (char **var, const char *value) /* {{{ */
497 {
498 if (*var != NULL)
499 {
500 free (*var);
501 *var = NULL;
502 }
504 if ((*var = strdup (value)) == NULL)
505 return (1);
506 else
507 return (0);
508 } /* }}} int config_set */
510 static int ascent_config (const char *key, const char *value) /* {{{ */
511 {
512 if (strcasecmp (key, "URL") == 0)
513 return (config_set (&url, value));
514 else if (strcasecmp (key, "User") == 0)
515 return (config_set (&user, value));
516 else if (strcasecmp (key, "Password") == 0)
517 return (config_set (&pass, value));
518 else if (strcasecmp (key, "VerifyPeer") == 0)
519 return (config_set (&verify_peer, value));
520 else if (strcasecmp (key, "VerifyHost") == 0)
521 return (config_set (&verify_host, value));
522 else if (strcasecmp (key, "CACert") == 0)
523 return (config_set (&cacert, value));
524 else if (strcasecmp (key, "Timeout") == 0)
525 return (config_set (&timeout, value));
526 else
527 return (-1);
528 } /* }}} int ascent_config */
530 static int ascent_init (void) /* {{{ */
531 {
532 if (url == NULL)
533 {
534 WARNING ("ascent plugin: ascent_init: No URL configured, "
535 "returning an error.");
536 return (-1);
537 }
539 if (curl != NULL)
540 {
541 curl_easy_cleanup (curl);
542 }
544 if ((curl = curl_easy_init ()) == NULL)
545 {
546 ERROR ("ascent plugin: ascent_init: curl_easy_init failed.");
547 return (-1);
548 }
550 curl_easy_setopt (curl, CURLOPT_NOSIGNAL, 1L);
551 curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, ascent_curl_callback);
552 curl_easy_setopt (curl, CURLOPT_USERAGENT, COLLECTD_USERAGENT);
553 curl_easy_setopt (curl, CURLOPT_ERRORBUFFER, ascent_curl_error);
555 if (user != NULL)
556 {
557 #ifdef HAVE_CURLOPT_USERNAME
558 curl_easy_setopt (curl, CURLOPT_USERNAME, user);
559 curl_easy_setopt (curl, CURLOPT_PASSWORD, (pass == NULL) ? "" : pass);
560 #else
561 static char credentials[1024];
562 int status;
564 status = ssnprintf (credentials, sizeof (credentials), "%s:%s",
565 user, (pass == NULL) ? "" : pass);
566 if ((status < 0) || ((size_t) status >= sizeof (credentials)))
567 {
568 ERROR ("ascent plugin: ascent_init: Returning an error because the "
569 "credentials have been truncated.");
570 return (-1);
571 }
573 curl_easy_setopt (curl, CURLOPT_USERPWD, credentials);
574 #endif
575 }
577 curl_easy_setopt (curl, CURLOPT_URL, url);
578 curl_easy_setopt (curl, CURLOPT_FOLLOWLOCATION, 1L);
579 curl_easy_setopt (curl, CURLOPT_MAXREDIRS, 50L);
581 if ((verify_peer == NULL) || IS_TRUE (verify_peer))
582 curl_easy_setopt (curl, CURLOPT_SSL_VERIFYPEER, 1L);
583 else
584 curl_easy_setopt (curl, CURLOPT_SSL_VERIFYPEER, 0L);
586 if ((verify_host == NULL) || IS_TRUE (verify_host))
587 curl_easy_setopt (curl, CURLOPT_SSL_VERIFYHOST, 2L);
588 else
589 curl_easy_setopt (curl, CURLOPT_SSL_VERIFYHOST, 0L);
591 if (cacert != NULL)
592 curl_easy_setopt (curl, CURLOPT_CAINFO, cacert);
594 #ifdef HAVE_CURLOPT_TIMEOUT_MS
595 if (timeout != NULL)
596 curl_easy_setopt (curl, CURLOPT_TIMEOUT_MS, atol(timeout));
597 else
598 curl_easy_setopt (curl, CURLOPT_TIMEOUT_MS, (long) CDTIME_T_TO_MS(plugin_get_interval()));
599 #endif
601 return (0);
602 } /* }}} int ascent_init */
604 static int ascent_read (void) /* {{{ */
605 {
606 int status;
608 if (curl == NULL)
609 {
610 ERROR ("ascent plugin: I don't have a CURL object.");
611 return (-1);
612 }
614 if (url == NULL)
615 {
616 ERROR ("ascent plugin: No URL has been configured.");
617 return (-1);
618 }
620 ascent_buffer_fill = 0;
621 if (curl_easy_perform (curl) != CURLE_OK)
622 {
623 ERROR ("ascent plugin: curl_easy_perform failed: %s",
624 ascent_curl_error);
625 return (-1);
626 }
628 status = ascent_xml (ascent_buffer);
629 if (status != 0)
630 return (-1);
631 else
632 return (0);
633 } /* }}} int ascent_read */
635 void module_register (void)
636 {
637 plugin_register_config ("ascent", ascent_config, config_keys, config_keys_num);
638 plugin_register_init ("ascent", ascent_init);
639 plugin_register_read ("ascent", ascent_read);
640 } /* void module_register */
642 /* vim: set sw=2 sts=2 ts=8 et fdm=marker : */