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 *cacert = NULL;
99 static CURL *curl = NULL;
101 static char *ascent_buffer = NULL;
102 static size_t ascent_buffer_size = 0;
103 static size_t ascent_buffer_fill = 0;
104 static char ascent_curl_error[CURL_ERROR_SIZE];
106 static const char *config_keys[] =
107 {
108 "URL",
109 "User",
110 "Password",
111 "CACert"
112 };
113 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
115 static int ascent_submit_gauge (const char *plugin_instance, /* {{{ */
116 const char *type, const char *type_instance, gauge_t value)
117 {
118 value_t values[1];
119 value_list_t vl = VALUE_LIST_INIT;
121 values[0].gauge = value;
123 vl.values = values;
124 vl.values_len = 1;
125 vl.time = time (NULL);
126 strcpy (vl.host, hostname_g);
127 strcpy (vl.plugin, "ascent");
129 if (plugin_instance != NULL)
130 sstrncpy (vl.plugin_instance, plugin_instance,
131 sizeof (vl.plugin_instance));
133 if (type_instance != NULL)
134 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
136 plugin_dispatch_values (type, &vl);
137 return (0);
138 } /* }}} int ascent_submit_gauge */
140 static size_t ascent_curl_callback (void *buf, size_t size, size_t nmemb, /* {{{ */
141 void *stream)
142 {
143 size_t len = size * nmemb;
145 if (len <= 0)
146 return (len);
148 if ((ascent_buffer_fill + len) >= ascent_buffer_size)
149 {
150 char *temp;
152 temp = (char *) realloc (ascent_buffer,
153 ascent_buffer_fill + len + 1);
154 if (temp == NULL)
155 {
156 ERROR ("ascent plugin: realloc failed.");
157 return (0);
158 }
159 ascent_buffer = temp;
160 ascent_buffer_size = ascent_buffer_fill + len + 1;
161 }
163 memcpy (ascent_buffer + ascent_buffer_fill, (char *) buf, len);
164 ascent_buffer_fill += len;
165 ascent_buffer[ascent_buffer_fill] = 0;
167 return (len);
168 } /* }}} size_t ascent_curl_callback */
170 static int ascent_submit_players (player_stats_t *ps) /* {{{ */
171 {
172 int i;
173 gauge_t value;
175 for (i = 0; i < RACES_LIST_LENGTH; i++)
176 if (races_list[i] != NULL)
177 ascent_submit_gauge ("by-race", "players", races_list[i],
178 (gauge_t) ps->races[i]);
180 for (i = 0; i < CLASSES_LIST_LENGTH; i++)
181 if (classes_list[i] != NULL)
182 ascent_submit_gauge ("by-class", "players", classes_list[i],
183 (gauge_t) ps->classes[i]);
185 for (i = 0; i < GENDERS_LIST_LENGTH; i++)
186 if (genders_list[i] != NULL)
187 ascent_submit_gauge ("by-gender", "players", genders_list[i],
188 (gauge_t) ps->genders[i]);
190 if (ps->level_num <= 0)
191 value = NAN;
192 else
193 value = ((double) ps->level_sum) / ((double) ps->level_num);
194 ascent_submit_gauge (NULL, "gauge", "avg-level", value);
196 /* Latency is in ms, but we store seconds. */
197 if (ps->latency_num <= 0)
198 value = NAN;
199 else
200 value = ((double) ps->latency_sum) / (1000.0 * ((double) ps->latency_num));
201 ascent_submit_gauge (NULL, "latency", "average", value);
203 return (0);
204 } /* }}} int ascent_submit_players */
206 static int ascent_account_player (player_stats_t *ps, /* {{{ */
207 player_info_t *pi)
208 {
209 if (pi->race >= 0)
210 {
211 if ((pi->race >= RACES_LIST_LENGTH)
212 || (races_list[pi->race] == NULL))
213 ERROR ("ascent plugin: Ignoring invalid numeric race %i.", pi->race);
214 else
215 ps->races[pi->race]++;
216 }
218 if (pi->class >= 0)
219 {
220 if ((pi->class >= CLASSES_LIST_LENGTH)
221 || (classes_list[pi->class] == NULL))
222 ERROR ("ascent plugin: Ignoring invalid numeric class %i.", pi->class);
223 else
224 ps->classes[pi->class]++;
225 }
227 if (pi->gender >= 0)
228 {
229 if ((pi->gender >= GENDERS_LIST_LENGTH)
230 || (genders_list[pi->gender] == NULL))
231 ERROR ("ascent plugin: Ignoring invalid numeric gender %i.",
232 pi->gender);
233 else
234 ps->genders[pi->gender]++;
235 }
238 if (pi->level > 0)
239 {
240 ps->level_sum += pi->level;
241 ps->level_num++;
242 }
244 if (pi->latency >= 0)
245 {
246 ps->latency_sum += pi->latency;
247 ps->latency_num++;
248 }
250 return (0);
251 } /* }}} int ascent_account_player */
253 static int ascent_xml_submit_gauge (xmlDoc *doc, xmlNode *node, /* {{{ */
254 const char *plugin_instance, const char *type, const char *type_instance)
255 {
256 char *str_ptr;
257 gauge_t value;
259 str_ptr = (char *) xmlNodeListGetString (doc, node->xmlChildrenNode, 1);
260 if (str_ptr == NULL)
261 {
262 ERROR ("ascent plugin: ascent_xml_submit_gauge: xmlNodeListGetString failed.");
263 return (-1);
264 }
266 if (strcasecmp ("N/A", str_ptr) == 0)
267 value = NAN;
268 else
269 {
270 char *end_ptr = NULL;
271 value = strtod (str_ptr, &end_ptr);
272 if (str_ptr == end_ptr)
273 {
274 ERROR ("ascent plugin: ascent_xml_submit_gauge: strtod failed.");
275 return (-1);
276 }
277 }
279 return (ascent_submit_gauge (plugin_instance, type, type_instance, value));
280 } /* }}} int ascent_xml_submit_gauge */
282 static int ascent_xml_read_int (xmlDoc *doc, xmlNode *node, /* {{{ */
283 int *ret_value)
284 {
285 char *str_ptr;
286 int value;
288 str_ptr = (char *) xmlNodeListGetString (doc, node->xmlChildrenNode, 1);
289 if (str_ptr == NULL)
290 {
291 ERROR ("ascent plugin: ascent_xml_read_int: xmlNodeListGetString failed.");
292 return (-1);
293 }
295 if (strcasecmp ("N/A", str_ptr) == 0)
296 value = -1;
297 else
298 {
299 char *end_ptr = NULL;
300 value = strtol (str_ptr, &end_ptr, 0);
301 if (str_ptr == end_ptr)
302 {
303 ERROR ("ascent plugin: ascent_xml_read_int: strtol failed.");
304 return (-1);
305 }
306 }
308 *ret_value = value;
309 return (0);
310 } /* }}} int ascent_xml_read_int */
312 static int ascent_xml_sessions_plr (xmlDoc *doc, xmlNode *node, /* {{{ */
313 player_info_t *pi)
314 {
315 xmlNode *child;
317 for (child = node->xmlChildrenNode; child != NULL; child = child->next)
318 {
319 if ((xmlStrcmp ((const xmlChar *) "comment", child->name) == 0)
320 || (xmlStrcmp ((const xmlChar *) "text", child->name) == 0))
321 /* ignore */;
322 else if (xmlStrcmp ((const xmlChar *) "race", child->name) == 0)
323 ascent_xml_read_int (doc, child, &pi->race);
324 else if (xmlStrcmp ((const xmlChar *) "class", child->name) == 0)
325 ascent_xml_read_int (doc, child, &pi->class);
326 else if (xmlStrcmp ((const xmlChar *) "gender", child->name) == 0)
327 ascent_xml_read_int (doc, child, &pi->gender);
328 else if (xmlStrcmp ((const xmlChar *) "level", child->name) == 0)
329 ascent_xml_read_int (doc, child, &pi->level);
330 else if (xmlStrcmp ((const xmlChar *) "latency", child->name) == 0)
331 ascent_xml_read_int (doc, child, &pi->latency);
332 else if ((xmlStrcmp ((const xmlChar *) "name", child->name) == 0)
333 || (xmlStrcmp ((const xmlChar *) "pvprank", child->name) == 0)
334 || (xmlStrcmp ((const xmlChar *) "map", child->name) == 0)
335 || (xmlStrcmp ((const xmlChar *) "areaid", child->name) == 0)
336 || (xmlStrcmp ((const xmlChar *) "xpos", child->name) == 0)
337 || (xmlStrcmp ((const xmlChar *) "ypos", child->name) == 0)
338 || (xmlStrcmp ((const xmlChar *) "onime", child->name) == 0))
339 /* ignore */;
340 else
341 {
342 WARNING ("ascent plugin: ascent_xml_status: Unknown tag: %s", child->name);
343 }
344 } /* for (child) */
346 return (0);
347 } /* }}} int ascent_xml_sessions_plr */
349 static int ascent_xml_sessions (xmlDoc *doc, xmlNode *node) /* {{{ */
350 {
351 xmlNode *child;
352 player_stats_t ps;
354 memset (&ps, 0, sizeof (ps));
356 for (child = node->xmlChildrenNode; child != NULL; child = child->next)
357 {
358 if ((xmlStrcmp ((const xmlChar *) "comment", child->name) == 0)
359 || (xmlStrcmp ((const xmlChar *) "text", child->name) == 0))
360 /* ignore */;
361 else if (xmlStrcmp ((const xmlChar *) "plr", child->name) == 0)
362 {
363 int status;
364 player_info_t pi = PLAYER_INFO_STATIC_INIT;
366 status = ascent_xml_sessions_plr (doc, child, &pi);
367 if (status == 0)
368 ascent_account_player (&ps, &pi);
369 }
370 else
371 {
372 WARNING ("ascent plugin: ascent_xml_status: Unknown tag: %s", child->name);
373 }
374 } /* for (child) */
376 ascent_submit_players (&ps);
378 return (0);
379 } /* }}} int ascent_xml_sessions */
381 static int ascent_xml_status (xmlDoc *doc, xmlNode *node) /* {{{ */
382 {
383 xmlNode *child;
385 for (child = node->xmlChildrenNode; child != NULL; child = child->next)
386 {
387 if ((xmlStrcmp ((const xmlChar *) "comment", child->name) == 0)
388 || (xmlStrcmp ((const xmlChar *) "text", child->name) == 0))
389 /* ignore */;
390 else if (xmlStrcmp ((const xmlChar *) "alliance", child->name) == 0)
391 ascent_xml_submit_gauge (doc, child, NULL, "players", "alliance");
392 else if (xmlStrcmp ((const xmlChar *) "horde", child->name) == 0)
393 ascent_xml_submit_gauge (doc, child, NULL, "players", "horde");
394 else if (xmlStrcmp ((const xmlChar *) "qplayers", child->name) == 0)
395 ascent_xml_submit_gauge (doc, child, NULL, "players", "queued");
396 else if ((xmlStrcmp ((const xmlChar *) "acceptedconns", child->name) == 0)
397 || (xmlStrcmp ((const xmlChar *) "avglat", child->name) == 0)
398 || (xmlStrcmp ((const xmlChar *) "cdbquerysize", child->name) == 0)
399 || (xmlStrcmp ((const xmlChar *) "cpu", child->name) == 0)
400 || (xmlStrcmp ((const xmlChar *) "fthreads", child->name) == 0)
401 || (xmlStrcmp ((const xmlChar *) "gmcount", child->name) == 0)
402 || (xmlStrcmp ((const xmlChar *) "lastupdate", child->name) == 0)
403 || (xmlStrcmp ((const xmlChar *) "ontime", child->name) == 0)
404 || (xmlStrcmp ((const xmlChar *) "oplayers", child->name) == 0)
405 || (xmlStrcmp ((const xmlChar *) "peakcount", child->name) == 0)
406 || (xmlStrcmp ((const xmlChar *) "platform", child->name) == 0)
407 || (xmlStrcmp ((const xmlChar *) "ram", child->name) == 0)
408 || (xmlStrcmp ((const xmlChar *) "threads", child->name) == 0)
409 || (xmlStrcmp ((const xmlChar *) "uptime", child->name) == 0)
410 || (xmlStrcmp ((const xmlChar *) "wdbquerysize", child->name) == 0))
411 /* ignore */;
412 else
413 {
414 WARNING ("ascent plugin: ascent_xml_status: Unknown tag: %s", child->name);
415 }
416 } /* for (child) */
418 return (0);
419 } /* }}} int ascent_xml_status */
421 static int ascent_xml (const char *data) /* {{{ */
422 {
423 xmlDoc *doc;
424 xmlNode *cur;
425 xmlNode *child;
427 #if 0
428 doc = xmlParseMemory (data, strlen (data),
429 /* URL = */ "ascent.xml",
430 /* encoding = */ NULL,
431 /* options = */ 0);
432 #else
433 doc = xmlParseMemory (data, strlen (data));
434 #endif
435 if (doc == NULL)
436 {
437 ERROR ("ascent plugin: xmlParseMemory failed.");
438 return (-1);
439 }
441 cur = xmlDocGetRootElement (doc);
442 if (cur == NULL)
443 {
444 ERROR ("ascent plugin: XML document is empty.");
445 xmlFreeDoc (doc);
446 return (-1);
447 }
449 if (xmlStrcmp ((const xmlChar *) "serverpage", cur->name) != 0)
450 {
451 ERROR ("ascent plugin: XML root element is not \"serverpage\".");
452 xmlFreeDoc (doc);
453 return (-1);
454 }
456 for (child = cur->xmlChildrenNode; child != NULL; child = child->next)
457 {
458 if ((xmlStrcmp ((const xmlChar *) "comment", child->name) == 0)
459 || (xmlStrcmp ((const xmlChar *) "text", child->name) == 0))
460 /* ignore */;
461 else if (xmlStrcmp ((const xmlChar *) "status", child->name) == 0)
462 ascent_xml_status (doc, child);
463 else if (xmlStrcmp ((const xmlChar *) "instances", child->name) == 0)
464 /* ignore for now */;
465 else if (xmlStrcmp ((const xmlChar *) "gms", child->name) == 0)
466 /* ignore for now */;
467 else if (xmlStrcmp ((const xmlChar *) "sessions", child->name) == 0)
468 ascent_xml_sessions (doc, child);
469 else
470 {
471 WARNING ("ascent plugin: ascent_xml: Unknown tag: %s", child->name);
472 }
473 } /* for (child) */
475 xmlFreeDoc (doc);
476 return (0);
477 } /* }}} int ascent_xml */
479 static int config_set (char **var, const char *value) /* {{{ */
480 {
481 if (*var != NULL)
482 {
483 free (*var);
484 *var = NULL;
485 }
487 if ((*var = strdup (value)) == NULL)
488 return (1);
489 else
490 return (0);
491 } /* }}} int config_set */
493 static int ascent_config (const char *key, const char *value) /* {{{ */
494 {
495 if (strcasecmp (key, "URL") == 0)
496 return (config_set (&url, value));
497 else if (strcasecmp (key, "User") == 0)
498 return (config_set (&user, value));
499 else if (strcasecmp (key, "Password") == 0)
500 return (config_set (&pass, value));
501 else if (strcasecmp (key, "CACert") == 0)
502 return (config_set (&cacert, value));
503 else
504 return (-1);
505 } /* }}} int ascent_config */
507 static int ascent_init (void) /* {{{ */
508 {
509 static char credentials[1024];
511 if (url == NULL)
512 {
513 WARNING ("ascent plugin: ascent_init: No URL configured, "
514 "returning an error.");
515 return (-1);
516 }
518 if (curl != NULL)
519 {
520 curl_easy_cleanup (curl);
521 }
523 if ((curl = curl_easy_init ()) == NULL)
524 {
525 ERROR ("ascent plugin: ascent_init: curl_easy_init failed.");
526 return (-1);
527 }
529 curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, ascent_curl_callback);
530 curl_easy_setopt (curl, CURLOPT_USERAGENT, PACKAGE_NAME"/"PACKAGE_VERSION);
531 curl_easy_setopt (curl, CURLOPT_ERRORBUFFER, ascent_curl_error);
533 if (user != NULL)
534 {
535 int status;
537 status = snprintf (credentials, sizeof (credentials), "%s:%s",
538 user, (pass == NULL) ? "" : pass);
539 if (status >= sizeof (credentials))
540 {
541 ERROR ("ascent plugin: ascent_init: Returning an error because the "
542 "credentials have been truncated.");
543 return (-1);
544 }
545 credentials[sizeof (credentials) - 1] = '\0';
547 curl_easy_setopt (curl, CURLOPT_USERPWD, credentials);
548 }
550 curl_easy_setopt (curl, CURLOPT_URL, url);
552 if (cacert != NULL)
553 curl_easy_setopt (curl, CURLOPT_CAINFO, cacert);
555 return (0);
556 } /* }}} int ascent_init */
558 static int ascent_read (void) /* {{{ */
559 {
560 int status;
562 if (curl == NULL)
563 {
564 ERROR ("ascent plugin: I don't have a CURL object.");
565 return (-1);
566 }
568 if (url == NULL)
569 {
570 ERROR ("ascent plugin: No URL has been configured.");
571 return (-1);
572 }
574 ascent_buffer_fill = 0;
575 if (curl_easy_perform (curl) != 0)
576 {
577 ERROR ("ascent plugin: curl_easy_perform failed: %s",
578 ascent_curl_error);
579 return (-1);
580 }
582 status = ascent_xml (ascent_buffer);
583 if (status != 0)
584 return (-1);
585 else
586 return (0);
587 } /* }}} int ascent_read */
589 void module_register (void)
590 {
591 plugin_register_config ("ascent", ascent_config, config_keys, config_keys_num);
592 plugin_register_init ("ascent", ascent_init);
593 plugin_register_read ("ascent", ascent_read);
594 } /* void module_register */
596 /* vim: set sw=2 sts=2 ts=8 et fdm=marker : */