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