1 /**
2 * collectd - src/ascent.c
3 * Copyright (C) 2008 Florian octo Forster
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; only version 2 of the License is applicable.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 *
18 * Authors:
19 * Florian octo Forster <octo at verplant.org>
20 **/
22 #include "collectd.h"
23 #include "common.h"
24 #include "plugin.h"
25 #include "configfile.h"
27 #include <curl/curl.h>
28 #include <libxml/parser.h>
30 static char *races_list[] = /* {{{ */
31 {
32 NULL,
33 "Human", /* 1 */
34 "Orc", /* 2 */
35 "Dwarf", /* 3 */
36 "Nightelf", /* 4 */
37 "Undead", /* 5 */
38 "Tauren", /* 6 */
39 "Gnome", /* 7 */
40 "Troll", /* 8 */
41 NULL,
42 "Bloodelf", /* 10 */
43 "Draenei" /* 11 */
44 }; /* }}} */
45 #define RACES_LIST_LENGTH STATIC_ARRAY_SIZE (races_list)
47 static char *classes_list[] = /* {{{ */
48 {
49 NULL,
50 "Warrior", /* 1 */
51 "Paladin", /* 2 */
52 "Hunter", /* 3 */
53 "Rogue", /* 4 */
54 "Priest", /* 5 */
55 NULL,
56 "Shaman", /* 7 */
57 "Mage", /* 8 */
58 "Warlock", /* 9 */
59 NULL,
60 "Druid" /* 11 */
61 }; /* }}} */
62 #define CLASSES_LIST_LENGTH STATIC_ARRAY_SIZE (classes_list)
64 static char *genders_list[] = /* {{{ */
65 {
66 "Male",
67 "Female"
68 }; /* }}} */
69 #define GENDERS_LIST_LENGTH STATIC_ARRAY_SIZE (genders_list)
71 struct player_stats_s
72 {
73 int races[RACES_LIST_LENGTH];
74 int classes[CLASSES_LIST_LENGTH];
75 int genders[GENDERS_LIST_LENGTH];
76 int level_sum;
77 int level_num;
78 int latency_sum;
79 int latency_num;
80 };
81 typedef struct player_stats_s player_stats_t;
83 struct player_info_s
84 {
85 int race;
86 int class;
87 int gender;
88 int level;
89 int latency;
90 };
91 typedef struct player_info_s player_info_t;
92 #define PLAYER_INFO_STATIC_INIT { -1, -1, -1, -1, -1 }
94 static char *url = NULL;
95 static char *user = NULL;
96 static char *pass = NULL;
97 static char *verify_peer = NULL;
98 static char *verify_host = NULL;
99 static char *cacert = NULL;
101 static CURL *curl = NULL;
103 static char *ascent_buffer = NULL;
104 static size_t ascent_buffer_size = 0;
105 static size_t ascent_buffer_fill = 0;
106 static char ascent_curl_error[CURL_ERROR_SIZE];
108 static const char *config_keys[] =
109 {
110 "URL",
111 "User",
112 "Password",
113 "VerifyPeer",
114 "VerifyHost",
115 "CACert"
116 };
117 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
119 static int ascent_submit_gauge (const char *plugin_instance, /* {{{ */
120 const char *type, const char *type_instance, gauge_t value)
121 {
122 value_t values[1];
123 value_list_t vl = VALUE_LIST_INIT;
125 values[0].gauge = value;
127 vl.values = values;
128 vl.values_len = 1;
129 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
130 sstrncpy (vl.plugin, "ascent", sizeof (vl.plugin));
132 if (plugin_instance != NULL)
133 sstrncpy (vl.plugin_instance, plugin_instance,
134 sizeof (vl.plugin_instance));
136 sstrncpy (vl.type, type, sizeof (vl.type));
138 if (type_instance != NULL)
139 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
141 plugin_dispatch_values (&vl);
142 return (0);
143 } /* }}} int ascent_submit_gauge */
145 static size_t ascent_curl_callback (void *buf, size_t size, size_t nmemb, /* {{{ */
146 void __attribute__((unused)) *stream)
147 {
148 size_t len = size * nmemb;
150 if (len <= 0)
151 return (len);
153 if ((ascent_buffer_fill + len) >= ascent_buffer_size)
154 {
155 char *temp;
157 temp = (char *) realloc (ascent_buffer,
158 ascent_buffer_fill + len + 1);
159 if (temp == NULL)
160 {
161 ERROR ("ascent plugin: realloc failed.");
162 return (0);
163 }
164 ascent_buffer = temp;
165 ascent_buffer_size = ascent_buffer_fill + len + 1;
166 }
168 memcpy (ascent_buffer + ascent_buffer_fill, (char *) buf, len);
169 ascent_buffer_fill += len;
170 ascent_buffer[ascent_buffer_fill] = 0;
172 return (len);
173 } /* }}} size_t ascent_curl_callback */
175 static int ascent_submit_players (player_stats_t *ps) /* {{{ */
176 {
177 size_t i;
178 gauge_t value;
180 for (i = 0; i < RACES_LIST_LENGTH; i++)
181 if (races_list[i] != NULL)
182 ascent_submit_gauge ("by-race", "players", races_list[i],
183 (gauge_t) ps->races[i]);
185 for (i = 0; i < CLASSES_LIST_LENGTH; i++)
186 if (classes_list[i] != NULL)
187 ascent_submit_gauge ("by-class", "players", classes_list[i],
188 (gauge_t) ps->classes[i]);
190 for (i = 0; i < GENDERS_LIST_LENGTH; i++)
191 if (genders_list[i] != NULL)
192 ascent_submit_gauge ("by-gender", "players", genders_list[i],
193 (gauge_t) ps->genders[i]);
195 if (ps->level_num <= 0)
196 value = NAN;
197 else
198 value = ((double) ps->level_sum) / ((double) ps->level_num);
199 ascent_submit_gauge (NULL, "gauge", "avg-level", value);
201 /* Latency is in ms, but we store seconds. */
202 if (ps->latency_num <= 0)
203 value = NAN;
204 else
205 value = ((double) ps->latency_sum) / (1000.0 * ((double) ps->latency_num));
206 ascent_submit_gauge (NULL, "latency", "average", value);
208 return (0);
209 } /* }}} int ascent_submit_players */
211 static int ascent_account_player (player_stats_t *ps, /* {{{ */
212 player_info_t *pi)
213 {
214 if (pi->race >= 0)
215 {
216 if (((size_t) pi->race >= RACES_LIST_LENGTH)
217 || (races_list[pi->race] == NULL))
218 ERROR ("ascent plugin: Ignoring invalid numeric race %i.", pi->race);
219 else
220 ps->races[pi->race]++;
221 }
223 if (pi->class >= 0)
224 {
225 if (((size_t) pi->class >= CLASSES_LIST_LENGTH)
226 || (classes_list[pi->class] == NULL))
227 ERROR ("ascent plugin: Ignoring invalid numeric class %i.", pi->class);
228 else
229 ps->classes[pi->class]++;
230 }
232 if (pi->gender >= 0)
233 {
234 if (((size_t) pi->gender >= GENDERS_LIST_LENGTH)
235 || (genders_list[pi->gender] == NULL))
236 ERROR ("ascent plugin: Ignoring invalid numeric gender %i.",
237 pi->gender);
238 else
239 ps->genders[pi->gender]++;
240 }
243 if (pi->level > 0)
244 {
245 ps->level_sum += pi->level;
246 ps->level_num++;
247 }
249 if (pi->latency >= 0)
250 {
251 ps->latency_sum += pi->latency;
252 ps->latency_num++;
253 }
255 return (0);
256 } /* }}} int ascent_account_player */
258 static int ascent_xml_submit_gauge (xmlDoc *doc, xmlNode *node, /* {{{ */
259 const char *plugin_instance, const char *type, const char *type_instance)
260 {
261 char *str_ptr;
262 gauge_t value;
264 str_ptr = (char *) xmlNodeListGetString (doc, node->xmlChildrenNode, 1);
265 if (str_ptr == NULL)
266 {
267 ERROR ("ascent plugin: ascent_xml_submit_gauge: xmlNodeListGetString failed.");
268 return (-1);
269 }
271 if (strcasecmp ("N/A", str_ptr) == 0)
272 value = NAN;
273 else
274 {
275 char *end_ptr = NULL;
276 value = strtod (str_ptr, &end_ptr);
277 if (str_ptr == end_ptr)
278 {
279 xmlFree(str_ptr);
280 ERROR ("ascent plugin: ascent_xml_submit_gauge: strtod failed.");
281 return (-1);
282 }
283 }
284 xmlFree(str_ptr);
286 return (ascent_submit_gauge (plugin_instance, type, type_instance, value));
287 } /* }}} int ascent_xml_submit_gauge */
289 static int ascent_xml_read_int (xmlDoc *doc, xmlNode *node, /* {{{ */
290 int *ret_value)
291 {
292 char *str_ptr;
293 int value;
295 str_ptr = (char *) xmlNodeListGetString (doc, node->xmlChildrenNode, 1);
296 if (str_ptr == NULL)
297 {
298 ERROR ("ascent plugin: ascent_xml_read_int: xmlNodeListGetString failed.");
299 return (-1);
300 }
302 if (strcasecmp ("N/A", str_ptr) == 0)
303 value = -1;
304 else
305 {
306 char *end_ptr = NULL;
307 value = strtol (str_ptr, &end_ptr, 0);
308 if (str_ptr == end_ptr)
309 {
310 xmlFree(str_ptr);
311 ERROR ("ascent plugin: ascent_xml_read_int: strtol failed.");
312 return (-1);
313 }
314 }
315 xmlFree(str_ptr);
317 *ret_value = value;
318 return (0);
319 } /* }}} int ascent_xml_read_int */
321 static int ascent_xml_sessions_plr (xmlDoc *doc, xmlNode *node, /* {{{ */
322 player_info_t *pi)
323 {
324 xmlNode *child;
326 for (child = node->xmlChildrenNode; child != NULL; child = child->next)
327 {
328 if ((xmlStrcmp ((const xmlChar *) "comment", child->name) == 0)
329 || (xmlStrcmp ((const xmlChar *) "text", child->name) == 0))
330 /* ignore */;
331 else if (xmlStrcmp ((const xmlChar *) "race", child->name) == 0)
332 ascent_xml_read_int (doc, child, &pi->race);
333 else if (xmlStrcmp ((const xmlChar *) "class", child->name) == 0)
334 ascent_xml_read_int (doc, child, &pi->class);
335 else if (xmlStrcmp ((const xmlChar *) "gender", child->name) == 0)
336 ascent_xml_read_int (doc, child, &pi->gender);
337 else if (xmlStrcmp ((const xmlChar *) "level", child->name) == 0)
338 ascent_xml_read_int (doc, child, &pi->level);
339 else if (xmlStrcmp ((const xmlChar *) "latency", child->name) == 0)
340 ascent_xml_read_int (doc, child, &pi->latency);
341 else if ((xmlStrcmp ((const xmlChar *) "name", child->name) == 0)
342 || (xmlStrcmp ((const xmlChar *) "pvprank", child->name) == 0)
343 || (xmlStrcmp ((const xmlChar *) "map", child->name) == 0)
344 || (xmlStrcmp ((const xmlChar *) "areaid", child->name) == 0)
345 || (xmlStrcmp ((const xmlChar *) "xpos", child->name) == 0)
346 || (xmlStrcmp ((const xmlChar *) "ypos", child->name) == 0)
347 || (xmlStrcmp ((const xmlChar *) "onime", child->name) == 0))
348 /* ignore */;
349 else
350 {
351 WARNING ("ascent plugin: ascent_xml_status: Unknown tag: %s", child->name);
352 }
353 } /* for (child) */
355 return (0);
356 } /* }}} int ascent_xml_sessions_plr */
358 static int ascent_xml_sessions (xmlDoc *doc, xmlNode *node) /* {{{ */
359 {
360 xmlNode *child;
361 player_stats_t ps;
363 memset (&ps, 0, sizeof (ps));
365 for (child = node->xmlChildrenNode; child != NULL; child = child->next)
366 {
367 if ((xmlStrcmp ((const xmlChar *) "comment", child->name) == 0)
368 || (xmlStrcmp ((const xmlChar *) "text", child->name) == 0))
369 /* ignore */;
370 else if (xmlStrcmp ((const xmlChar *) "plr", child->name) == 0)
371 {
372 int status;
373 player_info_t pi = PLAYER_INFO_STATIC_INIT;
375 status = ascent_xml_sessions_plr (doc, child, &pi);
376 if (status == 0)
377 ascent_account_player (&ps, &pi);
378 }
379 else
380 {
381 WARNING ("ascent plugin: ascent_xml_status: Unknown tag: %s", child->name);
382 }
383 } /* for (child) */
385 ascent_submit_players (&ps);
387 return (0);
388 } /* }}} int ascent_xml_sessions */
390 static int ascent_xml_status (xmlDoc *doc, xmlNode *node) /* {{{ */
391 {
392 xmlNode *child;
394 for (child = node->xmlChildrenNode; child != NULL; child = child->next)
395 {
396 if ((xmlStrcmp ((const xmlChar *) "comment", child->name) == 0)
397 || (xmlStrcmp ((const xmlChar *) "text", child->name) == 0))
398 /* ignore */;
399 else if (xmlStrcmp ((const xmlChar *) "alliance", child->name) == 0)
400 ascent_xml_submit_gauge (doc, child, NULL, "players", "alliance");
401 else if (xmlStrcmp ((const xmlChar *) "horde", child->name) == 0)
402 ascent_xml_submit_gauge (doc, child, NULL, "players", "horde");
403 else if (xmlStrcmp ((const xmlChar *) "qplayers", child->name) == 0)
404 ascent_xml_submit_gauge (doc, child, NULL, "players", "queued");
405 else if ((xmlStrcmp ((const xmlChar *) "acceptedconns", child->name) == 0)
406 || (xmlStrcmp ((const xmlChar *) "avglat", child->name) == 0)
407 || (xmlStrcmp ((const xmlChar *) "cdbquerysize", child->name) == 0)
408 || (xmlStrcmp ((const xmlChar *) "cpu", child->name) == 0)
409 || (xmlStrcmp ((const xmlChar *) "fthreads", child->name) == 0)
410 || (xmlStrcmp ((const xmlChar *) "gmcount", child->name) == 0)
411 || (xmlStrcmp ((const xmlChar *) "lastupdate", child->name) == 0)
412 || (xmlStrcmp ((const xmlChar *) "ontime", child->name) == 0)
413 || (xmlStrcmp ((const xmlChar *) "oplayers", child->name) == 0)
414 || (xmlStrcmp ((const xmlChar *) "peakcount", child->name) == 0)
415 || (xmlStrcmp ((const xmlChar *) "platform", child->name) == 0)
416 || (xmlStrcmp ((const xmlChar *) "ram", child->name) == 0)
417 || (xmlStrcmp ((const xmlChar *) "threads", child->name) == 0)
418 || (xmlStrcmp ((const xmlChar *) "uptime", child->name) == 0)
419 || (xmlStrcmp ((const xmlChar *) "wdbquerysize", child->name) == 0))
420 /* ignore */;
421 else
422 {
423 WARNING ("ascent plugin: ascent_xml_status: Unknown tag: %s", child->name);
424 }
425 } /* for (child) */
427 return (0);
428 } /* }}} int ascent_xml_status */
430 static int ascent_xml (const char *data) /* {{{ */
431 {
432 xmlDoc *doc;
433 xmlNode *cur;
434 xmlNode *child;
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 (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
517 return (-1);
518 } /* }}} int ascent_config */
520 static int ascent_init (void) /* {{{ */
521 {
522 static char credentials[1024];
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, PACKAGE_NAME"/"PACKAGE_VERSION);
545 curl_easy_setopt (curl, CURLOPT_ERRORBUFFER, ascent_curl_error);
547 if (user != NULL)
548 {
549 int status;
551 status = ssnprintf (credentials, sizeof (credentials), "%s:%s",
552 user, (pass == NULL) ? "" : pass);
553 if ((status < 0) || ((size_t) status >= sizeof (credentials)))
554 {
555 ERROR ("ascent plugin: ascent_init: Returning an error because the "
556 "credentials have been truncated.");
557 return (-1);
558 }
560 curl_easy_setopt (curl, CURLOPT_USERPWD, credentials);
561 }
563 curl_easy_setopt (curl, CURLOPT_URL, url);
564 curl_easy_setopt (curl, CURLOPT_FOLLOWLOCATION, 1L);
565 curl_easy_setopt (curl, CURLOPT_MAXREDIRS, 50L);
567 if ((verify_peer == NULL) || IS_TRUE (verify_peer))
568 curl_easy_setopt (curl, CURLOPT_SSL_VERIFYPEER, 1L);
569 else
570 curl_easy_setopt (curl, CURLOPT_SSL_VERIFYPEER, 0L);
572 if ((verify_host == NULL) || IS_TRUE (verify_host))
573 curl_easy_setopt (curl, CURLOPT_SSL_VERIFYHOST, 2L);
574 else
575 curl_easy_setopt (curl, CURLOPT_SSL_VERIFYHOST, 0L);
577 if (cacert != NULL)
578 curl_easy_setopt (curl, CURLOPT_CAINFO, cacert);
580 return (0);
581 } /* }}} int ascent_init */
583 static int ascent_read (void) /* {{{ */
584 {
585 int status;
587 if (curl == NULL)
588 {
589 ERROR ("ascent plugin: I don't have a CURL object.");
590 return (-1);
591 }
593 if (url == NULL)
594 {
595 ERROR ("ascent plugin: No URL has been configured.");
596 return (-1);
597 }
599 ascent_buffer_fill = 0;
600 if (curl_easy_perform (curl) != CURLE_OK)
601 {
602 ERROR ("ascent plugin: curl_easy_perform failed: %s",
603 ascent_curl_error);
604 return (-1);
605 }
607 status = ascent_xml (ascent_buffer);
608 if (status != 0)
609 return (-1);
610 else
611 return (0);
612 } /* }}} int ascent_read */
614 void module_register (void)
615 {
616 plugin_register_config ("ascent", ascent_config, config_keys, config_keys_num);
617 plugin_register_init ("ascent", ascent_init);
618 plugin_register_read ("ascent", ascent_read);
619 } /* void module_register */
621 /* vim: set sw=2 sts=2 ts=8 et fdm=marker : */