{GPL, other}: Relicense to MIT license.
[collectd.git] / src / ascent.c
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  **/
26
27 #include "collectd.h"
28 #include "common.h"
29 #include "plugin.h"
30 #include "configfile.h"
31
32 #include <curl/curl.h>
33 #include <libxml/parser.h>
34
35 static 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)
51
52 static 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)
68
69 static char *genders_list[] = /* {{{ */
70 {
71   "Male",
72   "Female"
73 }; /* }}} */
74 #define GENDERS_LIST_LENGTH STATIC_ARRAY_SIZE (genders_list)
75
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;
87
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 }
98
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
106 static CURL *curl = NULL;
107
108 static char  *ascent_buffer = NULL;
109 static size_t ascent_buffer_size = 0;
110 static size_t ascent_buffer_fill = 0;
111 static char   ascent_curl_error[CURL_ERROR_SIZE];
112
113 static const char *config_keys[] =
114 {
115   "URL",
116   "User",
117   "Password",
118   "VerifyPeer",
119   "VerifyHost",
120   "CACert"
121 };
122 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
123
124 static int ascent_submit_gauge (const char *plugin_instance, /* {{{ */
125     const char *type, const char *type_instance, gauge_t value)
126 {
127   value_t values[1];
128   value_list_t vl = VALUE_LIST_INIT;
129
130   values[0].gauge = value;
131
132   vl.values = values;
133   vl.values_len = 1;
134   sstrncpy (vl.host, hostname_g, sizeof (vl.host));
135   sstrncpy (vl.plugin, "ascent", sizeof (vl.plugin));
136
137   if (plugin_instance != NULL)
138     sstrncpy (vl.plugin_instance, plugin_instance,
139         sizeof (vl.plugin_instance));
140
141   sstrncpy (vl.type, type, sizeof (vl.type));
142
143   if (type_instance != NULL)
144     sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
145
146   plugin_dispatch_values (&vl);
147   return (0);
148 } /* }}} int ascent_submit_gauge */
149
150 static size_t ascent_curl_callback (void *buf, size_t size, size_t nmemb, /* {{{ */
151     void __attribute__((unused)) *stream)
152 {
153   size_t len = size * nmemb;
154
155   if (len <= 0)
156     return (len);
157
158   if ((ascent_buffer_fill + len) >= ascent_buffer_size)
159   {
160     char *temp;
161
162     temp = (char *) realloc (ascent_buffer,
163         ascent_buffer_fill + len + 1);
164     if (temp == NULL)
165     {
166       ERROR ("ascent plugin: realloc failed.");
167       return (0);
168     }
169     ascent_buffer = temp;
170     ascent_buffer_size = ascent_buffer_fill + len + 1;
171   }
172
173   memcpy (ascent_buffer + ascent_buffer_fill, (char *) buf, len);
174   ascent_buffer_fill += len;
175   ascent_buffer[ascent_buffer_fill] = 0;
176
177   return (len);
178 } /* }}} size_t ascent_curl_callback */
179
180 static int ascent_submit_players (player_stats_t *ps) /* {{{ */
181 {
182   size_t i;
183   gauge_t value;
184
185   for (i = 0; i < RACES_LIST_LENGTH; i++)
186     if (races_list[i] != NULL)
187       ascent_submit_gauge ("by-race", "players", races_list[i],
188           (gauge_t) ps->races[i]);
189
190   for (i = 0; i < CLASSES_LIST_LENGTH; i++)
191     if (classes_list[i] != NULL)
192       ascent_submit_gauge ("by-class", "players", classes_list[i],
193           (gauge_t) ps->classes[i]);
194
195   for (i = 0; i < GENDERS_LIST_LENGTH; i++)
196     if (genders_list[i] != NULL)
197       ascent_submit_gauge ("by-gender", "players", genders_list[i],
198           (gauge_t) ps->genders[i]);
199
200   if (ps->level_num <= 0)
201     value = NAN;
202   else
203     value = ((double) ps->level_sum) / ((double) ps->level_num);
204   ascent_submit_gauge (NULL, "gauge", "avg-level", value);
205
206   /* Latency is in ms, but we store seconds. */
207   if (ps->latency_num <= 0)
208     value = NAN;
209   else
210     value = ((double) ps->latency_sum) / (1000.0 * ((double) ps->latency_num));
211   ascent_submit_gauge (NULL, "latency", "average", value);
212
213   return (0);
214 } /* }}} int ascent_submit_players */
215
216 static int ascent_account_player (player_stats_t *ps, /* {{{ */
217     player_info_t *pi)
218 {
219   if (pi->race >= 0)
220   {
221     if (((size_t) pi->race >= RACES_LIST_LENGTH)
222         || (races_list[pi->race] == NULL))
223       ERROR ("ascent plugin: Ignoring invalid numeric race %i.", pi->race);
224     else
225       ps->races[pi->race]++;
226   }
227
228   if (pi->class >= 0)
229   {
230     if (((size_t) pi->class >= CLASSES_LIST_LENGTH)
231         || (classes_list[pi->class] == NULL))
232       ERROR ("ascent plugin: Ignoring invalid numeric class %i.", pi->class);
233     else
234       ps->classes[pi->class]++;
235   }
236
237   if (pi->gender >= 0)
238   {
239     if (((size_t) pi->gender >= GENDERS_LIST_LENGTH)
240         || (genders_list[pi->gender] == NULL))
241       ERROR ("ascent plugin: Ignoring invalid numeric gender %i.",
242           pi->gender);
243     else
244       ps->genders[pi->gender]++;
245   }
246
247
248   if (pi->level > 0)
249   {
250     ps->level_sum += pi->level;
251     ps->level_num++;
252   }
253
254   if (pi->latency >= 0)
255   {
256     ps->latency_sum += pi->latency;
257     ps->latency_num++;
258   }
259
260   return (0);
261 } /* }}} int ascent_account_player */
262
263 static int ascent_xml_submit_gauge (xmlDoc *doc, xmlNode *node, /* {{{ */
264     const char *plugin_instance, const char *type, const char *type_instance)
265 {
266   char *str_ptr;
267   gauge_t value;
268
269   str_ptr = (char *) xmlNodeListGetString (doc, node->xmlChildrenNode, 1);
270   if (str_ptr == NULL)
271   {
272     ERROR ("ascent plugin: ascent_xml_submit_gauge: xmlNodeListGetString failed.");
273     return (-1);
274   }
275
276   if (strcasecmp ("N/A", str_ptr) == 0)
277     value = NAN;
278   else
279   {
280     char *end_ptr = NULL;
281     value = strtod (str_ptr, &end_ptr);
282     if (str_ptr == end_ptr)
283     {
284       xmlFree(str_ptr);
285       ERROR ("ascent plugin: ascent_xml_submit_gauge: strtod failed.");
286       return (-1);
287     }
288   }
289   xmlFree(str_ptr);
290
291   return (ascent_submit_gauge (plugin_instance, type, type_instance, value));
292 } /* }}} int ascent_xml_submit_gauge */
293
294 static int ascent_xml_read_int (xmlDoc *doc, xmlNode *node, /* {{{ */
295     int *ret_value)
296 {
297   char *str_ptr;
298   int value;
299
300   str_ptr = (char *) xmlNodeListGetString (doc, node->xmlChildrenNode, 1);
301   if (str_ptr == NULL)
302   {
303     ERROR ("ascent plugin: ascent_xml_read_int: xmlNodeListGetString failed.");
304     return (-1);
305   }
306
307   if (strcasecmp ("N/A", str_ptr) == 0)
308     value = -1;
309   else
310   {
311     char *end_ptr = NULL;
312     value = strtol (str_ptr, &end_ptr, 0);
313     if (str_ptr == end_ptr)
314     {
315       xmlFree(str_ptr);
316       ERROR ("ascent plugin: ascent_xml_read_int: strtol failed.");
317       return (-1);
318     }
319   }
320   xmlFree(str_ptr);
321
322   *ret_value = value;
323   return (0);
324 } /* }}} int ascent_xml_read_int */
325
326 static int ascent_xml_sessions_plr (xmlDoc *doc, xmlNode *node, /* {{{ */
327     player_info_t *pi)
328 {
329   xmlNode *child;
330
331   for (child = node->xmlChildrenNode; child != NULL; child = child->next)
332   {
333     if ((xmlStrcmp ((const xmlChar *) "comment", child->name) == 0)
334         || (xmlStrcmp ((const xmlChar *) "text", child->name) == 0))
335       /* ignore */;
336     else if (xmlStrcmp ((const xmlChar *) "race", child->name) == 0)
337       ascent_xml_read_int (doc, child, &pi->race);
338     else if (xmlStrcmp ((const xmlChar *) "class", child->name) == 0)
339       ascent_xml_read_int (doc, child, &pi->class);
340     else if (xmlStrcmp ((const xmlChar *) "gender", child->name) == 0)
341       ascent_xml_read_int (doc, child, &pi->gender);
342     else if (xmlStrcmp ((const xmlChar *) "level", child->name) == 0)
343       ascent_xml_read_int (doc, child, &pi->level);
344     else if (xmlStrcmp ((const xmlChar *) "latency", child->name) == 0)
345       ascent_xml_read_int (doc, child, &pi->latency);
346     else if ((xmlStrcmp ((const xmlChar *) "name", child->name) == 0)
347         || (xmlStrcmp ((const xmlChar *) "pvprank", child->name) == 0)
348         || (xmlStrcmp ((const xmlChar *) "map", child->name) == 0)
349         || (xmlStrcmp ((const xmlChar *) "areaid", child->name) == 0)
350         || (xmlStrcmp ((const xmlChar *) "xpos", child->name) == 0)
351         || (xmlStrcmp ((const xmlChar *) "ypos", child->name) == 0)
352         || (xmlStrcmp ((const xmlChar *) "onime", child->name) == 0))
353       /* ignore */;
354     else
355     {
356       WARNING ("ascent plugin: ascent_xml_status: Unknown tag: %s", child->name);
357     }
358   } /* for (child) */
359
360   return (0);
361 } /* }}} int ascent_xml_sessions_plr */
362
363 static int ascent_xml_sessions (xmlDoc *doc, xmlNode *node) /* {{{ */
364 {
365   xmlNode *child;
366   player_stats_t ps;
367
368   memset (&ps, 0, sizeof (ps));
369
370   for (child = node->xmlChildrenNode; child != NULL; child = child->next)
371   {
372     if ((xmlStrcmp ((const xmlChar *) "comment", child->name) == 0)
373         || (xmlStrcmp ((const xmlChar *) "text", child->name) == 0))
374       /* ignore */;
375     else if (xmlStrcmp ((const xmlChar *) "plr", child->name) == 0)
376     {
377       int status;
378       player_info_t pi = PLAYER_INFO_STATIC_INIT;
379
380       status = ascent_xml_sessions_plr (doc, child, &pi);
381       if (status == 0)
382         ascent_account_player (&ps, &pi);
383     }
384     else
385     {
386       WARNING ("ascent plugin: ascent_xml_status: Unknown tag: %s", child->name);
387     }
388   } /* for (child) */
389
390   ascent_submit_players (&ps);
391
392   return (0);
393 } /* }}} int ascent_xml_sessions */
394
395 static int ascent_xml_status (xmlDoc *doc, xmlNode *node) /* {{{ */
396 {
397   xmlNode *child;
398
399   for (child = node->xmlChildrenNode; child != NULL; child = child->next)
400   {
401     if ((xmlStrcmp ((const xmlChar *) "comment", child->name) == 0)
402         || (xmlStrcmp ((const xmlChar *) "text", child->name) == 0))
403       /* ignore */;
404     else if (xmlStrcmp ((const xmlChar *) "alliance", child->name) == 0)
405       ascent_xml_submit_gauge (doc, child, NULL, "players", "alliance");
406     else if (xmlStrcmp ((const xmlChar *) "horde", child->name) == 0)
407       ascent_xml_submit_gauge (doc, child, NULL, "players", "horde");
408     else if (xmlStrcmp ((const xmlChar *) "qplayers", child->name) == 0)
409       ascent_xml_submit_gauge (doc, child, NULL, "players", "queued");
410     else if ((xmlStrcmp ((const xmlChar *) "acceptedconns", child->name) == 0)
411         || (xmlStrcmp ((const xmlChar *) "avglat", child->name) == 0)
412         || (xmlStrcmp ((const xmlChar *) "cdbquerysize", child->name) == 0)
413         || (xmlStrcmp ((const xmlChar *) "cpu", child->name) == 0)
414         || (xmlStrcmp ((const xmlChar *) "fthreads", child->name) == 0)
415         || (xmlStrcmp ((const xmlChar *) "gmcount", child->name) == 0)
416         || (xmlStrcmp ((const xmlChar *) "lastupdate", child->name) == 0)
417         || (xmlStrcmp ((const xmlChar *) "ontime", child->name) == 0)
418         || (xmlStrcmp ((const xmlChar *) "oplayers", child->name) == 0)
419         || (xmlStrcmp ((const xmlChar *) "peakcount", child->name) == 0)
420         || (xmlStrcmp ((const xmlChar *) "platform", child->name) == 0)
421         || (xmlStrcmp ((const xmlChar *) "ram", child->name) == 0)
422         || (xmlStrcmp ((const xmlChar *) "threads", child->name) == 0)
423         || (xmlStrcmp ((const xmlChar *) "uptime", child->name) == 0)
424         || (xmlStrcmp ((const xmlChar *) "wdbquerysize", child->name) == 0))
425       /* ignore */;
426     else
427     {
428       WARNING ("ascent plugin: ascent_xml_status: Unknown tag: %s", child->name);
429     }
430   } /* for (child) */
431
432   return (0);
433 } /* }}} int ascent_xml_status */
434
435 static int ascent_xml (const char *data) /* {{{ */
436 {
437   xmlDoc *doc;
438   xmlNode *cur;
439   xmlNode *child;
440
441 #if 0
442   doc = xmlParseMemory (data, strlen (data),
443       /* URL = */ "ascent.xml",
444       /* encoding = */ NULL,
445       /* options = */ 0);
446 #else
447   doc = xmlParseMemory (data, strlen (data));
448 #endif
449   if (doc == NULL)
450   {
451     ERROR ("ascent plugin: xmlParseMemory failed.");
452     return (-1);
453   }
454
455   cur = xmlDocGetRootElement (doc);
456   if (cur == NULL)
457   {
458     ERROR ("ascent plugin: XML document is empty.");
459     xmlFreeDoc (doc);
460     return (-1);
461   }
462
463   if (xmlStrcmp ((const xmlChar *) "serverpage", cur->name) != 0)
464   {
465     ERROR ("ascent plugin: XML root element is not \"serverpage\".");
466     xmlFreeDoc (doc);
467     return (-1);
468   }
469
470   for (child = cur->xmlChildrenNode; child != NULL; child = child->next)
471   {
472     if ((xmlStrcmp ((const xmlChar *) "comment", child->name) == 0)
473         || (xmlStrcmp ((const xmlChar *) "text", child->name) == 0))
474       /* ignore */;
475     else if (xmlStrcmp ((const xmlChar *) "status", child->name) == 0)
476       ascent_xml_status (doc, child);
477     else if (xmlStrcmp ((const xmlChar *) "instances", child->name) == 0)
478       /* ignore for now */;
479     else if (xmlStrcmp ((const xmlChar *) "gms", child->name) == 0)
480       /* ignore for now */;
481     else if (xmlStrcmp ((const xmlChar *) "sessions", child->name) == 0)
482       ascent_xml_sessions (doc, child);
483     else
484     {
485       WARNING ("ascent plugin: ascent_xml: Unknown tag: %s", child->name);
486     }
487   } /* for (child) */
488
489   xmlFreeDoc (doc);
490   return (0);
491 } /* }}} int ascent_xml */
492
493 static int config_set (char **var, const char *value) /* {{{ */
494 {
495   if (*var != NULL)
496   {
497     free (*var);
498     *var = NULL;
499   }
500
501   if ((*var = strdup (value)) == NULL)
502     return (1);
503   else
504     return (0);
505 } /* }}} int config_set */
506
507 static int ascent_config (const char *key, const char *value) /* {{{ */
508 {
509   if (strcasecmp (key, "URL") == 0)
510     return (config_set (&url, value));
511   else if (strcasecmp (key, "User") == 0)
512     return (config_set (&user, value));
513   else if (strcasecmp (key, "Password") == 0)
514     return (config_set (&pass, value));
515   else if (strcasecmp (key, "VerifyPeer") == 0)
516     return (config_set (&verify_peer, value));
517   else if (strcasecmp (key, "VerifyHost") == 0)
518     return (config_set (&verify_host, value));
519   else if (strcasecmp (key, "CACert") == 0)
520     return (config_set (&cacert, value));
521   else
522     return (-1);
523 } /* }}} int ascent_config */
524
525 static int ascent_init (void) /* {{{ */
526 {
527   static char credentials[1024];
528
529   if (url == NULL)
530   {
531     WARNING ("ascent plugin: ascent_init: No URL configured, "
532         "returning an error.");
533     return (-1);
534   }
535
536   if (curl != NULL)
537   {
538     curl_easy_cleanup (curl);
539   }
540
541   if ((curl = curl_easy_init ()) == NULL)
542   {
543     ERROR ("ascent plugin: ascent_init: curl_easy_init failed.");
544     return (-1);
545   }
546
547   curl_easy_setopt (curl, CURLOPT_NOSIGNAL, 1L);
548   curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, ascent_curl_callback);
549   curl_easy_setopt (curl, CURLOPT_USERAGENT, COLLECTD_USERAGENT);
550   curl_easy_setopt (curl, CURLOPT_ERRORBUFFER, ascent_curl_error);
551
552   if (user != NULL)
553   {
554     int status;
555
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     }
564
565     curl_easy_setopt (curl, CURLOPT_USERPWD, credentials);
566   }
567
568   curl_easy_setopt (curl, CURLOPT_URL, url);
569   curl_easy_setopt (curl, CURLOPT_FOLLOWLOCATION, 1L);
570   curl_easy_setopt (curl, CURLOPT_MAXREDIRS, 50L);
571
572   if ((verify_peer == NULL) || IS_TRUE (verify_peer))
573     curl_easy_setopt (curl, CURLOPT_SSL_VERIFYPEER, 1L);
574   else
575     curl_easy_setopt (curl, CURLOPT_SSL_VERIFYPEER, 0L);
576
577   if ((verify_host == NULL) || IS_TRUE (verify_host))
578     curl_easy_setopt (curl, CURLOPT_SSL_VERIFYHOST, 2L);
579   else
580     curl_easy_setopt (curl, CURLOPT_SSL_VERIFYHOST, 0L);
581
582   if (cacert != NULL)
583     curl_easy_setopt (curl, CURLOPT_CAINFO, cacert);
584
585   return (0);
586 } /* }}} int ascent_init */
587
588 static int ascent_read (void) /* {{{ */
589 {
590   int status;
591
592   if (curl == NULL)
593   {
594     ERROR ("ascent plugin: I don't have a CURL object.");
595     return (-1);
596   }
597
598   if (url == NULL)
599   {
600     ERROR ("ascent plugin: No URL has been configured.");
601     return (-1);
602   }
603
604   ascent_buffer_fill = 0;
605   if (curl_easy_perform (curl) != CURLE_OK)
606   {
607     ERROR ("ascent plugin: curl_easy_perform failed: %s",
608         ascent_curl_error);
609     return (-1);
610   }
611
612   status = ascent_xml (ascent_buffer);
613   if (status != 0)
614     return (-1);
615   else
616     return (0);
617 } /* }}} int ascent_read */
618
619 void module_register (void)
620 {
621   plugin_register_config ("ascent", ascent_config, config_keys, config_keys_num);
622   plugin_register_init ("ascent", ascent_init);
623   plugin_register_read ("ascent", ascent_read);
624 } /* void module_register */
625
626 /* vim: set sw=2 sts=2 ts=8 et fdm=marker : */