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