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"
28 #include "common.h"
29 #include "plugin.h"
30 #include "configfile.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 = (char *) 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 size_t i;
185 gauge_t value;
187 for (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 (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 (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)
203 value = NAN;
204 else
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)
210 value = NAN;
211 else
212 value = ((double) ps->latency_sum) / (1000.0 * ((double) ps->latency_num));
213 ascent_submit_gauge (NULL, "latency", "average", value);
215 return (0);
216 } /* }}} int ascent_submit_players */
218 static int ascent_account_player (player_stats_t *ps, /* {{{ */
219 player_info_t *pi)
220 {
221 if (pi->race >= 0)
222 {
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);
226 else
227 ps->races[pi->race]++;
228 }
230 if (pi->class >= 0)
231 {
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);
235 else
236 ps->classes[pi->class]++;
237 }
239 if (pi->gender >= 0)
240 {
241 if (((size_t) pi->gender >= GENDERS_LIST_LENGTH)
242 || (genders_list[pi->gender] == NULL))
243 ERROR ("ascent plugin: Ignoring invalid numeric gender %i.",
244 pi->gender);
245 else
246 ps->genders[pi->gender]++;
247 }
250 if (pi->level > 0)
251 {
252 ps->level_sum += pi->level;
253 ps->level_num++;
254 }
256 if (pi->latency >= 0)
257 {
258 ps->latency_sum += pi->latency;
259 ps->latency_num++;
260 }
262 return (0);
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)
267 {
268 char *str_ptr;
269 gauge_t value;
271 str_ptr = (char *) xmlNodeListGetString (doc, node->xmlChildrenNode, 1);
272 if (str_ptr == NULL)
273 {
274 ERROR ("ascent plugin: ascent_xml_submit_gauge: xmlNodeListGetString failed.");
275 return (-1);
276 }
278 if (strcasecmp ("N/A", str_ptr) == 0)
279 value = NAN;
280 else
281 {
282 char *end_ptr = NULL;
283 value = strtod (str_ptr, &end_ptr);
284 if (str_ptr == end_ptr)
285 {
286 xmlFree(str_ptr);
287 ERROR ("ascent plugin: ascent_xml_submit_gauge: strtod failed.");
288 return (-1);
289 }
290 }
291 xmlFree(str_ptr);
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, /* {{{ */
297 int *ret_value)
298 {
299 char *str_ptr;
300 int value;
302 str_ptr = (char *) xmlNodeListGetString (doc, node->xmlChildrenNode, 1);
303 if (str_ptr == NULL)
304 {
305 ERROR ("ascent plugin: ascent_xml_read_int: xmlNodeListGetString failed.");
306 return (-1);
307 }
309 if (strcasecmp ("N/A", str_ptr) == 0)
310 value = -1;
311 else
312 {
313 char *end_ptr = NULL;
314 value = strtol (str_ptr, &end_ptr, 0);
315 if (str_ptr == end_ptr)
316 {
317 xmlFree(str_ptr);
318 ERROR ("ascent plugin: ascent_xml_read_int: strtol failed.");
319 return (-1);
320 }
321 }
322 xmlFree(str_ptr);
324 *ret_value = value;
325 return (0);
326 } /* }}} int ascent_xml_read_int */
328 static int ascent_xml_sessions_plr (xmlDoc *doc, xmlNode *node, /* {{{ */
329 player_info_t *pi)
330 {
331 xmlNode *child;
333 for (child = node->xmlChildrenNode; child != NULL; child = child->next)
334 {
335 if ((xmlStrcmp ((const xmlChar *) "comment", child->name) == 0)
336 || (xmlStrcmp ((const xmlChar *) "text", child->name) == 0))
337 /* ignore */;
338 else if (xmlStrcmp ((const xmlChar *) "race", child->name) == 0)
339 ascent_xml_read_int (doc, child, &pi->race);
340 else if (xmlStrcmp ((const xmlChar *) "class", child->name) == 0)
341 ascent_xml_read_int (doc, child, &pi->class);
342 else if (xmlStrcmp ((const xmlChar *) "gender", child->name) == 0)
343 ascent_xml_read_int (doc, child, &pi->gender);
344 else if (xmlStrcmp ((const xmlChar *) "level", child->name) == 0)
345 ascent_xml_read_int (doc, child, &pi->level);
346 else if (xmlStrcmp ((const xmlChar *) "latency", child->name) == 0)
347 ascent_xml_read_int (doc, child, &pi->latency);
348 else if ((xmlStrcmp ((const xmlChar *) "name", child->name) == 0)
349 || (xmlStrcmp ((const xmlChar *) "pvprank", child->name) == 0)
350 || (xmlStrcmp ((const xmlChar *) "map", child->name) == 0)
351 || (xmlStrcmp ((const xmlChar *) "areaid", child->name) == 0)
352 || (xmlStrcmp ((const xmlChar *) "xpos", child->name) == 0)
353 || (xmlStrcmp ((const xmlChar *) "ypos", child->name) == 0)
354 || (xmlStrcmp ((const xmlChar *) "onime", child->name) == 0))
355 /* ignore */;
356 else
357 {
358 WARNING ("ascent plugin: ascent_xml_status: Unknown tag: %s", child->name);
359 }
360 } /* for (child) */
362 return (0);
363 } /* }}} int ascent_xml_sessions_plr */
365 static int ascent_xml_sessions (xmlDoc *doc, xmlNode *node) /* {{{ */
366 {
367 xmlNode *child;
368 player_stats_t ps;
370 memset (&ps, 0, sizeof (ps));
372 for (child = node->xmlChildrenNode; child != NULL; child = child->next)
373 {
374 if ((xmlStrcmp ((const xmlChar *) "comment", child->name) == 0)
375 || (xmlStrcmp ((const xmlChar *) "text", child->name) == 0))
376 /* ignore */;
377 else if (xmlStrcmp ((const xmlChar *) "plr", child->name) == 0)
378 {
379 int status;
380 player_info_t pi = PLAYER_INFO_STATIC_INIT;
382 status = ascent_xml_sessions_plr (doc, child, &pi);
383 if (status == 0)
384 ascent_account_player (&ps, &pi);
385 }
386 else
387 {
388 WARNING ("ascent plugin: ascent_xml_status: Unknown tag: %s", child->name);
389 }
390 } /* for (child) */
392 ascent_submit_players (&ps);
394 return (0);
395 } /* }}} int ascent_xml_sessions */
397 static int ascent_xml_status (xmlDoc *doc, xmlNode *node) /* {{{ */
398 {
399 xmlNode *child;
401 for (child = node->xmlChildrenNode; child != NULL; child = child->next)
402 {
403 if ((xmlStrcmp ((const xmlChar *) "comment", child->name) == 0)
404 || (xmlStrcmp ((const xmlChar *) "text", child->name) == 0))
405 /* ignore */;
406 else if (xmlStrcmp ((const xmlChar *) "alliance", child->name) == 0)
407 ascent_xml_submit_gauge (doc, child, NULL, "players", "alliance");
408 else if (xmlStrcmp ((const xmlChar *) "horde", child->name) == 0)
409 ascent_xml_submit_gauge (doc, child, NULL, "players", "horde");
410 else if (xmlStrcmp ((const xmlChar *) "qplayers", child->name) == 0)
411 ascent_xml_submit_gauge (doc, child, NULL, "players", "queued");
412 else if ((xmlStrcmp ((const xmlChar *) "acceptedconns", child->name) == 0)
413 || (xmlStrcmp ((const xmlChar *) "avglat", child->name) == 0)
414 || (xmlStrcmp ((const xmlChar *) "cdbquerysize", child->name) == 0)
415 || (xmlStrcmp ((const xmlChar *) "cpu", child->name) == 0)
416 || (xmlStrcmp ((const xmlChar *) "fthreads", child->name) == 0)
417 || (xmlStrcmp ((const xmlChar *) "gmcount", child->name) == 0)
418 || (xmlStrcmp ((const xmlChar *) "lastupdate", child->name) == 0)
419 || (xmlStrcmp ((const xmlChar *) "ontime", child->name) == 0)
420 || (xmlStrcmp ((const xmlChar *) "oplayers", child->name) == 0)
421 || (xmlStrcmp ((const xmlChar *) "peakcount", child->name) == 0)
422 || (xmlStrcmp ((const xmlChar *) "platform", child->name) == 0)
423 || (xmlStrcmp ((const xmlChar *) "ram", child->name) == 0)
424 || (xmlStrcmp ((const xmlChar *) "threads", child->name) == 0)
425 || (xmlStrcmp ((const xmlChar *) "uptime", child->name) == 0)
426 || (xmlStrcmp ((const xmlChar *) "wdbquerysize", child->name) == 0))
427 /* ignore */;
428 else
429 {
430 WARNING ("ascent plugin: ascent_xml_status: Unknown tag: %s", child->name);
431 }
432 } /* for (child) */
434 return (0);
435 } /* }}} int ascent_xml_status */
437 static int ascent_xml (const char *data) /* {{{ */
438 {
439 xmlDoc *doc;
440 xmlNode *cur;
441 xmlNode *child;
443 #if 0
444 doc = xmlParseMemory (data, strlen (data),
445 /* URL = */ "ascent.xml",
446 /* encoding = */ NULL,
447 /* options = */ 0);
448 #else
449 doc = xmlParseMemory (data, strlen (data));
450 #endif
451 if (doc == NULL)
452 {
453 ERROR ("ascent plugin: xmlParseMemory failed.");
454 return (-1);
455 }
457 cur = xmlDocGetRootElement (doc);
458 if (cur == NULL)
459 {
460 ERROR ("ascent plugin: XML document is empty.");
461 xmlFreeDoc (doc);
462 return (-1);
463 }
465 if (xmlStrcmp ((const xmlChar *) "serverpage", cur->name) != 0)
466 {
467 ERROR ("ascent plugin: XML root element is not \"serverpage\".");
468 xmlFreeDoc (doc);
469 return (-1);
470 }
472 for (child = cur->xmlChildrenNode; child != NULL; child = child->next)
473 {
474 if ((xmlStrcmp ((const xmlChar *) "comment", child->name) == 0)
475 || (xmlStrcmp ((const xmlChar *) "text", child->name) == 0))
476 /* ignore */;
477 else if (xmlStrcmp ((const xmlChar *) "status", child->name) == 0)
478 ascent_xml_status (doc, child);
479 else if (xmlStrcmp ((const xmlChar *) "instances", child->name) == 0)
480 /* ignore for now */;
481 else if (xmlStrcmp ((const xmlChar *) "gms", child->name) == 0)
482 /* ignore for now */;
483 else if (xmlStrcmp ((const xmlChar *) "sessions", child->name) == 0)
484 ascent_xml_sessions (doc, child);
485 else
486 {
487 WARNING ("ascent plugin: ascent_xml: Unknown tag: %s", child->name);
488 }
489 } /* for (child) */
491 xmlFreeDoc (doc);
492 return (0);
493 } /* }}} int ascent_xml */
495 static int config_set (char **var, const char *value) /* {{{ */
496 {
497 if (*var != NULL)
498 {
499 free (*var);
500 *var = NULL;
501 }
503 if ((*var = strdup (value)) == NULL)
504 return (1);
505 else
506 return (0);
507 } /* }}} int config_set */
509 static int ascent_config (const char *key, const char *value) /* {{{ */
510 {
511 if (strcasecmp (key, "URL") == 0)
512 return (config_set (&url, value));
513 else if (strcasecmp (key, "User") == 0)
514 return (config_set (&user, value));
515 else if (strcasecmp (key, "Password") == 0)
516 return (config_set (&pass, value));
517 else if (strcasecmp (key, "VerifyPeer") == 0)
518 return (config_set (&verify_peer, value));
519 else if (strcasecmp (key, "VerifyHost") == 0)
520 return (config_set (&verify_host, value));
521 else if (strcasecmp (key, "CACert") == 0)
522 return (config_set (&cacert, value));
523 else if (strcasecmp (key, "Timeout") == 0)
524 return (config_set (&timeout, value));
525 else
526 return (-1);
527 } /* }}} int ascent_config */
529 static int ascent_init (void) /* {{{ */
530 {
531 if (url == NULL)
532 {
533 WARNING ("ascent plugin: ascent_init: No URL configured, "
534 "returning an error.");
535 return (-1);
536 }
538 if (curl != NULL)
539 {
540 curl_easy_cleanup (curl);
541 }
543 if ((curl = curl_easy_init ()) == NULL)
544 {
545 ERROR ("ascent plugin: ascent_init: curl_easy_init failed.");
546 return (-1);
547 }
549 curl_easy_setopt (curl, CURLOPT_NOSIGNAL, 1L);
550 curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, ascent_curl_callback);
551 curl_easy_setopt (curl, CURLOPT_USERAGENT, COLLECTD_USERAGENT);
552 curl_easy_setopt (curl, CURLOPT_ERRORBUFFER, ascent_curl_error);
554 if (user != NULL)
555 {
556 #ifdef HAVE_CURLOPT_USERNAME
557 curl_easy_setopt (curl, CURLOPT_USERNAME, user);
558 curl_easy_setopt (curl, CURLOPT_PASSWORD, (pass == NULL) ? "" : pass);
559 #else
560 static char credentials[1024];
561 int status;
563 status = ssnprintf (credentials, sizeof (credentials), "%s:%s",
564 user, (pass == NULL) ? "" : pass);
565 if ((status < 0) || ((size_t) status >= sizeof (credentials)))
566 {
567 ERROR ("ascent plugin: ascent_init: Returning an error because the "
568 "credentials have been truncated.");
569 return (-1);
570 }
572 curl_easy_setopt (curl, CURLOPT_USERPWD, credentials);
573 #endif
574 }
576 curl_easy_setopt (curl, CURLOPT_URL, url);
577 curl_easy_setopt (curl, CURLOPT_FOLLOWLOCATION, 1L);
578 curl_easy_setopt (curl, CURLOPT_MAXREDIRS, 50L);
580 if ((verify_peer == NULL) || IS_TRUE (verify_peer))
581 curl_easy_setopt (curl, CURLOPT_SSL_VERIFYPEER, 1L);
582 else
583 curl_easy_setopt (curl, CURLOPT_SSL_VERIFYPEER, 0L);
585 if ((verify_host == NULL) || IS_TRUE (verify_host))
586 curl_easy_setopt (curl, CURLOPT_SSL_VERIFYHOST, 2L);
587 else
588 curl_easy_setopt (curl, CURLOPT_SSL_VERIFYHOST, 0L);
590 if (cacert != NULL)
591 curl_easy_setopt (curl, CURLOPT_CAINFO, cacert);
593 #ifdef HAVE_CURLOPT_TIMEOUT_MS
594 if (timeout != NULL)
595 curl_easy_setopt (curl, CURLOPT_TIMEOUT_MS, atol(timeout));
596 else
597 curl_easy_setopt (curl, CURLOPT_TIMEOUT_MS, (long) CDTIME_T_TO_MS(plugin_get_interval()));
598 #endif
600 return (0);
601 } /* }}} int ascent_init */
603 static int ascent_read (void) /* {{{ */
604 {
605 int status;
607 if (curl == NULL)
608 {
609 ERROR ("ascent plugin: I don't have a CURL object.");
610 return (-1);
611 }
613 if (url == NULL)
614 {
615 ERROR ("ascent plugin: No URL has been configured.");
616 return (-1);
617 }
619 ascent_buffer_fill = 0;
620 if (curl_easy_perform (curl) != CURLE_OK)
621 {
622 ERROR ("ascent plugin: curl_easy_perform failed: %s",
623 ascent_curl_error);
624 return (-1);
625 }
627 status = ascent_xml (ascent_buffer);
628 if (status != 0)
629 return (-1);
630 else
631 return (0);
632 } /* }}} int ascent_read */
634 void module_register (void)
635 {
636 plugin_register_config ("ascent", ascent_config, config_keys, config_keys_num);
637 plugin_register_init ("ascent", ascent_init);
638 plugin_register_read ("ascent", ascent_read);
639 } /* void module_register */
641 /* vim: set sw=2 sts=2 ts=8 et fdm=marker : */