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 sstrncpy (vl.type, type, sizeof (vl.type));
135 if (type_instance != NULL)
136 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
138 plugin_dispatch_values (&vl);
139 return (0);
140 } /* }}} int ascent_submit_gauge */
142 static size_t ascent_curl_callback (void *buf, size_t size, size_t nmemb, /* {{{ */
143 void *stream)
144 {
145 size_t len = size * nmemb;
147 if (len <= 0)
148 return (len);
150 if ((ascent_buffer_fill + len) >= ascent_buffer_size)
151 {
152 char *temp;
154 temp = (char *) realloc (ascent_buffer,
155 ascent_buffer_fill + len + 1);
156 if (temp == NULL)
157 {
158 ERROR ("ascent plugin: realloc failed.");
159 return (0);
160 }
161 ascent_buffer = temp;
162 ascent_buffer_size = ascent_buffer_fill + len + 1;
163 }
165 memcpy (ascent_buffer + ascent_buffer_fill, (char *) buf, len);
166 ascent_buffer_fill += len;
167 ascent_buffer[ascent_buffer_fill] = 0;
169 return (len);
170 } /* }}} size_t ascent_curl_callback */
172 static int ascent_submit_players (player_stats_t *ps) /* {{{ */
173 {
174 int i;
175 gauge_t value;
177 for (i = 0; i < RACES_LIST_LENGTH; i++)
178 if (races_list[i] != NULL)
179 ascent_submit_gauge ("by-race", "players", races_list[i],
180 (gauge_t) ps->races[i]);
182 for (i = 0; i < CLASSES_LIST_LENGTH; i++)
183 if (classes_list[i] != NULL)
184 ascent_submit_gauge ("by-class", "players", classes_list[i],
185 (gauge_t) ps->classes[i]);
187 for (i = 0; i < GENDERS_LIST_LENGTH; i++)
188 if (genders_list[i] != NULL)
189 ascent_submit_gauge ("by-gender", "players", genders_list[i],
190 (gauge_t) ps->genders[i]);
192 if (ps->level_num <= 0)
193 value = NAN;
194 else
195 value = ((double) ps->level_sum) / ((double) ps->level_num);
196 ascent_submit_gauge (NULL, "gauge", "avg-level", value);
198 /* Latency is in ms, but we store seconds. */
199 if (ps->latency_num <= 0)
200 value = NAN;
201 else
202 value = ((double) ps->latency_sum) / (1000.0 * ((double) ps->latency_num));
203 ascent_submit_gauge (NULL, "latency", "average", value);
205 return (0);
206 } /* }}} int ascent_submit_players */
208 static int ascent_account_player (player_stats_t *ps, /* {{{ */
209 player_info_t *pi)
210 {
211 if (pi->race >= 0)
212 {
213 if ((pi->race >= RACES_LIST_LENGTH)
214 || (races_list[pi->race] == NULL))
215 ERROR ("ascent plugin: Ignoring invalid numeric race %i.", pi->race);
216 else
217 ps->races[pi->race]++;
218 }
220 if (pi->class >= 0)
221 {
222 if ((pi->class >= CLASSES_LIST_LENGTH)
223 || (classes_list[pi->class] == NULL))
224 ERROR ("ascent plugin: Ignoring invalid numeric class %i.", pi->class);
225 else
226 ps->classes[pi->class]++;
227 }
229 if (pi->gender >= 0)
230 {
231 if ((pi->gender >= GENDERS_LIST_LENGTH)
232 || (genders_list[pi->gender] == NULL))
233 ERROR ("ascent plugin: Ignoring invalid numeric gender %i.",
234 pi->gender);
235 else
236 ps->genders[pi->gender]++;
237 }
240 if (pi->level > 0)
241 {
242 ps->level_sum += pi->level;
243 ps->level_num++;
244 }
246 if (pi->latency >= 0)
247 {
248 ps->latency_sum += pi->latency;
249 ps->latency_num++;
250 }
252 return (0);
253 } /* }}} int ascent_account_player */
255 static int ascent_xml_submit_gauge (xmlDoc *doc, xmlNode *node, /* {{{ */
256 const char *plugin_instance, const char *type, const char *type_instance)
257 {
258 char *str_ptr;
259 gauge_t value;
261 str_ptr = (char *) xmlNodeListGetString (doc, node->xmlChildrenNode, 1);
262 if (str_ptr == NULL)
263 {
264 ERROR ("ascent plugin: ascent_xml_submit_gauge: xmlNodeListGetString failed.");
265 return (-1);
266 }
268 if (strcasecmp ("N/A", str_ptr) == 0)
269 value = NAN;
270 else
271 {
272 char *end_ptr = NULL;
273 value = strtod (str_ptr, &end_ptr);
274 if (str_ptr == end_ptr)
275 {
276 ERROR ("ascent plugin: ascent_xml_submit_gauge: strtod failed.");
277 return (-1);
278 }
279 }
281 return (ascent_submit_gauge (plugin_instance, type, type_instance, value));
282 } /* }}} int ascent_xml_submit_gauge */
284 static int ascent_xml_read_int (xmlDoc *doc, xmlNode *node, /* {{{ */
285 int *ret_value)
286 {
287 char *str_ptr;
288 int value;
290 str_ptr = (char *) xmlNodeListGetString (doc, node->xmlChildrenNode, 1);
291 if (str_ptr == NULL)
292 {
293 ERROR ("ascent plugin: ascent_xml_read_int: xmlNodeListGetString failed.");
294 return (-1);
295 }
297 if (strcasecmp ("N/A", str_ptr) == 0)
298 value = -1;
299 else
300 {
301 char *end_ptr = NULL;
302 value = strtol (str_ptr, &end_ptr, 0);
303 if (str_ptr == end_ptr)
304 {
305 ERROR ("ascent plugin: ascent_xml_read_int: strtol failed.");
306 return (-1);
307 }
308 }
310 *ret_value = value;
311 return (0);
312 } /* }}} int ascent_xml_read_int */
314 static int ascent_xml_sessions_plr (xmlDoc *doc, xmlNode *node, /* {{{ */
315 player_info_t *pi)
316 {
317 xmlNode *child;
319 for (child = node->xmlChildrenNode; child != NULL; child = child->next)
320 {
321 if ((xmlStrcmp ((const xmlChar *) "comment", child->name) == 0)
322 || (xmlStrcmp ((const xmlChar *) "text", child->name) == 0))
323 /* ignore */;
324 else if (xmlStrcmp ((const xmlChar *) "race", child->name) == 0)
325 ascent_xml_read_int (doc, child, &pi->race);
326 else if (xmlStrcmp ((const xmlChar *) "class", child->name) == 0)
327 ascent_xml_read_int (doc, child, &pi->class);
328 else if (xmlStrcmp ((const xmlChar *) "gender", child->name) == 0)
329 ascent_xml_read_int (doc, child, &pi->gender);
330 else if (xmlStrcmp ((const xmlChar *) "level", child->name) == 0)
331 ascent_xml_read_int (doc, child, &pi->level);
332 else if (xmlStrcmp ((const xmlChar *) "latency", child->name) == 0)
333 ascent_xml_read_int (doc, child, &pi->latency);
334 else if ((xmlStrcmp ((const xmlChar *) "name", child->name) == 0)
335 || (xmlStrcmp ((const xmlChar *) "pvprank", child->name) == 0)
336 || (xmlStrcmp ((const xmlChar *) "map", child->name) == 0)
337 || (xmlStrcmp ((const xmlChar *) "areaid", child->name) == 0)
338 || (xmlStrcmp ((const xmlChar *) "xpos", child->name) == 0)
339 || (xmlStrcmp ((const xmlChar *) "ypos", child->name) == 0)
340 || (xmlStrcmp ((const xmlChar *) "onime", child->name) == 0))
341 /* ignore */;
342 else
343 {
344 WARNING ("ascent plugin: ascent_xml_status: Unknown tag: %s", child->name);
345 }
346 } /* for (child) */
348 return (0);
349 } /* }}} int ascent_xml_sessions_plr */
351 static int ascent_xml_sessions (xmlDoc *doc, xmlNode *node) /* {{{ */
352 {
353 xmlNode *child;
354 player_stats_t ps;
356 memset (&ps, 0, sizeof (ps));
358 for (child = node->xmlChildrenNode; child != NULL; child = child->next)
359 {
360 if ((xmlStrcmp ((const xmlChar *) "comment", child->name) == 0)
361 || (xmlStrcmp ((const xmlChar *) "text", child->name) == 0))
362 /* ignore */;
363 else if (xmlStrcmp ((const xmlChar *) "plr", child->name) == 0)
364 {
365 int status;
366 player_info_t pi = PLAYER_INFO_STATIC_INIT;
368 status = ascent_xml_sessions_plr (doc, child, &pi);
369 if (status == 0)
370 ascent_account_player (&ps, &pi);
371 }
372 else
373 {
374 WARNING ("ascent plugin: ascent_xml_status: Unknown tag: %s", child->name);
375 }
376 } /* for (child) */
378 ascent_submit_players (&ps);
380 return (0);
381 } /* }}} int ascent_xml_sessions */
383 static int ascent_xml_status (xmlDoc *doc, xmlNode *node) /* {{{ */
384 {
385 xmlNode *child;
387 for (child = node->xmlChildrenNode; child != NULL; child = child->next)
388 {
389 if ((xmlStrcmp ((const xmlChar *) "comment", child->name) == 0)
390 || (xmlStrcmp ((const xmlChar *) "text", child->name) == 0))
391 /* ignore */;
392 else if (xmlStrcmp ((const xmlChar *) "alliance", child->name) == 0)
393 ascent_xml_submit_gauge (doc, child, NULL, "players", "alliance");
394 else if (xmlStrcmp ((const xmlChar *) "horde", child->name) == 0)
395 ascent_xml_submit_gauge (doc, child, NULL, "players", "horde");
396 else if (xmlStrcmp ((const xmlChar *) "qplayers", child->name) == 0)
397 ascent_xml_submit_gauge (doc, child, NULL, "players", "queued");
398 else if ((xmlStrcmp ((const xmlChar *) "acceptedconns", child->name) == 0)
399 || (xmlStrcmp ((const xmlChar *) "avglat", child->name) == 0)
400 || (xmlStrcmp ((const xmlChar *) "cdbquerysize", child->name) == 0)
401 || (xmlStrcmp ((const xmlChar *) "cpu", child->name) == 0)
402 || (xmlStrcmp ((const xmlChar *) "fthreads", child->name) == 0)
403 || (xmlStrcmp ((const xmlChar *) "gmcount", child->name) == 0)
404 || (xmlStrcmp ((const xmlChar *) "lastupdate", child->name) == 0)
405 || (xmlStrcmp ((const xmlChar *) "ontime", child->name) == 0)
406 || (xmlStrcmp ((const xmlChar *) "oplayers", child->name) == 0)
407 || (xmlStrcmp ((const xmlChar *) "peakcount", child->name) == 0)
408 || (xmlStrcmp ((const xmlChar *) "platform", child->name) == 0)
409 || (xmlStrcmp ((const xmlChar *) "ram", child->name) == 0)
410 || (xmlStrcmp ((const xmlChar *) "threads", child->name) == 0)
411 || (xmlStrcmp ((const xmlChar *) "uptime", child->name) == 0)
412 || (xmlStrcmp ((const xmlChar *) "wdbquerysize", child->name) == 0))
413 /* ignore */;
414 else
415 {
416 WARNING ("ascent plugin: ascent_xml_status: Unknown tag: %s", child->name);
417 }
418 } /* for (child) */
420 return (0);
421 } /* }}} int ascent_xml_status */
423 static int ascent_xml (const char *data) /* {{{ */
424 {
425 xmlDoc *doc;
426 xmlNode *cur;
427 xmlNode *child;
429 #if 0
430 doc = xmlParseMemory (data, strlen (data),
431 /* URL = */ "ascent.xml",
432 /* encoding = */ NULL,
433 /* options = */ 0);
434 #else
435 doc = xmlParseMemory (data, strlen (data));
436 #endif
437 if (doc == NULL)
438 {
439 ERROR ("ascent plugin: xmlParseMemory failed.");
440 return (-1);
441 }
443 cur = xmlDocGetRootElement (doc);
444 if (cur == NULL)
445 {
446 ERROR ("ascent plugin: XML document is empty.");
447 xmlFreeDoc (doc);
448 return (-1);
449 }
451 if (xmlStrcmp ((const xmlChar *) "serverpage", cur->name) != 0)
452 {
453 ERROR ("ascent plugin: XML root element is not \"serverpage\".");
454 xmlFreeDoc (doc);
455 return (-1);
456 }
458 for (child = cur->xmlChildrenNode; child != NULL; child = child->next)
459 {
460 if ((xmlStrcmp ((const xmlChar *) "comment", child->name) == 0)
461 || (xmlStrcmp ((const xmlChar *) "text", child->name) == 0))
462 /* ignore */;
463 else if (xmlStrcmp ((const xmlChar *) "status", child->name) == 0)
464 ascent_xml_status (doc, child);
465 else if (xmlStrcmp ((const xmlChar *) "instances", child->name) == 0)
466 /* ignore for now */;
467 else if (xmlStrcmp ((const xmlChar *) "gms", child->name) == 0)
468 /* ignore for now */;
469 else if (xmlStrcmp ((const xmlChar *) "sessions", child->name) == 0)
470 ascent_xml_sessions (doc, child);
471 else
472 {
473 WARNING ("ascent plugin: ascent_xml: Unknown tag: %s", child->name);
474 }
475 } /* for (child) */
477 xmlFreeDoc (doc);
478 return (0);
479 } /* }}} int ascent_xml */
481 static int config_set (char **var, const char *value) /* {{{ */
482 {
483 if (*var != NULL)
484 {
485 free (*var);
486 *var = NULL;
487 }
489 if ((*var = strdup (value)) == NULL)
490 return (1);
491 else
492 return (0);
493 } /* }}} int config_set */
495 static int ascent_config (const char *key, const char *value) /* {{{ */
496 {
497 if (strcasecmp (key, "URL") == 0)
498 return (config_set (&url, value));
499 else if (strcasecmp (key, "User") == 0)
500 return (config_set (&user, value));
501 else if (strcasecmp (key, "Password") == 0)
502 return (config_set (&pass, value));
503 else if (strcasecmp (key, "CACert") == 0)
504 return (config_set (&cacert, value));
505 else
506 return (-1);
507 } /* }}} int ascent_config */
509 static int ascent_init (void) /* {{{ */
510 {
511 static char credentials[1024];
513 if (url == NULL)
514 {
515 WARNING ("ascent plugin: ascent_init: No URL configured, "
516 "returning an error.");
517 return (-1);
518 }
520 if (curl != NULL)
521 {
522 curl_easy_cleanup (curl);
523 }
525 if ((curl = curl_easy_init ()) == NULL)
526 {
527 ERROR ("ascent plugin: ascent_init: curl_easy_init failed.");
528 return (-1);
529 }
531 curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, ascent_curl_callback);
532 curl_easy_setopt (curl, CURLOPT_USERAGENT, PACKAGE_NAME"/"PACKAGE_VERSION);
533 curl_easy_setopt (curl, CURLOPT_ERRORBUFFER, ascent_curl_error);
535 if (user != NULL)
536 {
537 int status;
539 status = ssnprintf (credentials, sizeof (credentials), "%s:%s",
540 user, (pass == NULL) ? "" : pass);
541 if (status >= sizeof (credentials))
542 {
543 ERROR ("ascent plugin: ascent_init: Returning an error because the "
544 "credentials have been truncated.");
545 return (-1);
546 }
548 curl_easy_setopt (curl, CURLOPT_USERPWD, credentials);
549 }
551 curl_easy_setopt (curl, CURLOPT_URL, url);
553 if (cacert != NULL)
554 curl_easy_setopt (curl, CURLOPT_CAINFO, cacert);
556 return (0);
557 } /* }}} int ascent_init */
559 static int ascent_read (void) /* {{{ */
560 {
561 int status;
563 if (curl == NULL)
564 {
565 ERROR ("ascent plugin: I don't have a CURL object.");
566 return (-1);
567 }
569 if (url == NULL)
570 {
571 ERROR ("ascent plugin: No URL has been configured.");
572 return (-1);
573 }
575 ascent_buffer_fill = 0;
576 if (curl_easy_perform (curl) != 0)
577 {
578 ERROR ("ascent plugin: curl_easy_perform failed: %s",
579 ascent_curl_error);
580 return (-1);
581 }
583 status = ascent_xml (ascent_buffer);
584 if (status != 0)
585 return (-1);
586 else
587 return (0);
588 } /* }}} int ascent_read */
590 void module_register (void)
591 {
592 plugin_register_config ("ascent", ascent_config, config_keys, config_keys_num);
593 plugin_register_init ("ascent", ascent_init);
594 plugin_register_read ("ascent", ascent_read);
595 } /* void module_register */
597 /* vim: set sw=2 sts=2 ts=8 et fdm=marker : */