Merge branch 'collectd-5.5' into 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,       "Human",    /*  1 */
38         "Orc",                  /*  2 */
39         "Dwarf",                /*  3 */
40         "Nightelf",             /*  4 */
41         "Undead",               /*  5 */
42         "Tauren",               /*  6 */
43         "Gnome",                /*  7 */
44         "Troll",                /*  8 */
45         NULL,       "Bloodelf", /* 10 */
46         "Draenei"               /* 11 */
47 };                              /* }}} */
48 #define RACES_LIST_LENGTH STATIC_ARRAY_SIZE(races_list)
49
50 static const char *classes_list[] = /* {{{ */
51     {
52         NULL,      "Warrior", /*  1 */
53         "Paladin",            /*  2 */
54         "Hunter",             /*  3 */
55         "Rogue",              /*  4 */
56         "Priest",             /*  5 */
57         NULL,      "Shaman",  /*  7 */
58         "Mage",               /*  8 */
59         "Warlock",            /*  9 */
60         NULL,      "Druid"    /* 11 */
61 };                            /* }}} */
62 #define CLASSES_LIST_LENGTH STATIC_ARRAY_SIZE(classes_list)
63
64 static const char *genders_list[] = /* {{{ */
65     {"Male", "Female"};             /* }}} */
66 #define GENDERS_LIST_LENGTH STATIC_ARRAY_SIZE(genders_list)
67
68 struct player_stats_s {
69   int races[RACES_LIST_LENGTH];
70   int classes[CLASSES_LIST_LENGTH];
71   int genders[GENDERS_LIST_LENGTH];
72   int level_sum;
73   int level_num;
74   int latency_sum;
75   int latency_num;
76 };
77 typedef struct player_stats_s player_stats_t;
78
79 struct player_info_s {
80   int race;
81   int class;
82   int gender;
83   int level;
84   int latency;
85 };
86 typedef struct player_info_s player_info_t;
87 #define PLAYER_INFO_STATIC_INIT                                                \
88   { -1, -1, -1, -1, -1 }
89
90 static char *url = NULL;
91 static char *user = NULL;
92 static char *pass = NULL;
93 static char *verify_peer = NULL;
94 static char *verify_host = NULL;
95 static char *cacert = NULL;
96 static char *timeout = NULL;
97
98 static CURL *curl = NULL;
99
100 static char *ascent_buffer = NULL;
101 static size_t ascent_buffer_size = 0;
102 static size_t ascent_buffer_fill = 0;
103 static char ascent_curl_error[CURL_ERROR_SIZE];
104
105 static const char *config_keys[] = {
106     "URL", "User", "Password", "VerifyPeer", "VerifyHost", "CACert", "Timeout",
107 };
108 static int config_keys_num = STATIC_ARRAY_SIZE(config_keys);
109
110 static int ascent_submit_gauge(const char *plugin_instance, /* {{{ */
111                                const char *type, const char *type_instance,
112                                gauge_t value) {
113   value_t values[1];
114   value_list_t vl = VALUE_LIST_INIT;
115
116   values[0].gauge = value;
117
118   vl.values = values;
119   vl.values_len = 1;
120   sstrncpy(vl.host, hostname_g, sizeof(vl.host));
121   sstrncpy(vl.plugin, "ascent", sizeof(vl.plugin));
122
123   if (plugin_instance != NULL)
124     sstrncpy(vl.plugin_instance, plugin_instance, sizeof(vl.plugin_instance));
125
126   sstrncpy(vl.type, type, sizeof(vl.type));
127
128   if (type_instance != NULL)
129     sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
130
131   plugin_dispatch_values(&vl);
132   return (0);
133 } /* }}} int ascent_submit_gauge */
134
135 static size_t ascent_curl_callback(void *buf, size_t size,
136                                    size_t nmemb, /* {{{ */
137                                    void __attribute__((unused)) * stream) {
138   size_t len = size * nmemb;
139
140   if (len == 0)
141     return (len);
142
143   if ((ascent_buffer_fill + len) >= ascent_buffer_size) {
144     char *temp;
145
146     temp = realloc(ascent_buffer, ascent_buffer_fill + len + 1);
147     if (temp == NULL) {
148       ERROR("ascent plugin: realloc failed.");
149       return (0);
150     }
151     ascent_buffer = temp;
152     ascent_buffer_size = ascent_buffer_fill + len + 1;
153   }
154
155   memcpy(ascent_buffer + ascent_buffer_fill, (char *)buf, len);
156   ascent_buffer_fill += len;
157   ascent_buffer[ascent_buffer_fill] = 0;
158
159   return (len);
160 } /* }}} size_t ascent_curl_callback */
161
162 static int ascent_submit_players(player_stats_t *ps) /* {{{ */
163 {
164   gauge_t value;
165
166   for (size_t i = 0; i < RACES_LIST_LENGTH; i++)
167     if (races_list[i] != NULL)
168       ascent_submit_gauge("by-race", "players", races_list[i],
169                           (gauge_t)ps->races[i]);
170
171   for (size_t i = 0; i < CLASSES_LIST_LENGTH; i++)
172     if (classes_list[i] != NULL)
173       ascent_submit_gauge("by-class", "players", classes_list[i],
174                           (gauge_t)ps->classes[i]);
175
176   for (size_t i = 0; i < GENDERS_LIST_LENGTH; i++)
177     if (genders_list[i] != NULL)
178       ascent_submit_gauge("by-gender", "players", genders_list[i],
179                           (gauge_t)ps->genders[i]);
180
181   if (ps->level_num <= 0)
182     value = NAN;
183   else
184     value = ((double)ps->level_sum) / ((double)ps->level_num);
185   ascent_submit_gauge(NULL, "gauge", "avg-level", value);
186
187   /* Latency is in ms, but we store seconds. */
188   if (ps->latency_num <= 0)
189     value = NAN;
190   else
191     value = ((double)ps->latency_sum) / (1000.0 * ((double)ps->latency_num));
192   ascent_submit_gauge(NULL, "latency", "average", value);
193
194   return (0);
195 } /* }}} int ascent_submit_players */
196
197 static int ascent_account_player(player_stats_t *ps, /* {{{ */
198                                  player_info_t *pi) {
199   if (pi->race >= 0) {
200     if (((size_t)pi->race >= RACES_LIST_LENGTH) ||
201         (races_list[pi->race] == NULL))
202       ERROR("ascent plugin: Ignoring invalid numeric race %i.", pi->race);
203     else
204       ps->races[pi->race]++;
205   }
206
207   if (pi->class >= 0) {
208     if (((size_t)pi->class >= CLASSES_LIST_LENGTH) ||
209         (classes_list[pi->class] == NULL))
210       ERROR("ascent plugin: Ignoring invalid numeric class %i.", pi->class);
211     else
212       ps->classes[pi->class]++;
213   }
214
215   if (pi->gender >= 0) {
216     if (((size_t)pi->gender >= GENDERS_LIST_LENGTH) ||
217         (genders_list[pi->gender] == NULL))
218       ERROR("ascent plugin: Ignoring invalid numeric gender %i.", pi->gender);
219     else
220       ps->genders[pi->gender]++;
221   }
222
223   if (pi->level > 0) {
224     ps->level_sum += pi->level;
225     ps->level_num++;
226   }
227
228   if (pi->latency >= 0) {
229     ps->latency_sum += pi->latency;
230     ps->latency_num++;
231   }
232
233   return (0);
234 } /* }}} int ascent_account_player */
235
236 static int ascent_xml_submit_gauge(xmlDoc *doc, xmlNode *node, /* {{{ */
237                                    const char *plugin_instance,
238                                    const char *type,
239                                    const char *type_instance) {
240   char *str_ptr;
241   gauge_t value;
242
243   str_ptr = (char *)xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
244   if (str_ptr == NULL) {
245     ERROR(
246         "ascent plugin: ascent_xml_submit_gauge: xmlNodeListGetString failed.");
247     return (-1);
248   }
249
250   if (strcasecmp("N/A", str_ptr) == 0)
251     value = NAN;
252   else {
253     char *end_ptr = NULL;
254     value = strtod(str_ptr, &end_ptr);
255     if (str_ptr == end_ptr) {
256       xmlFree(str_ptr);
257       ERROR("ascent plugin: ascent_xml_submit_gauge: strtod failed.");
258       return (-1);
259     }
260   }
261   xmlFree(str_ptr);
262
263   return (ascent_submit_gauge(plugin_instance, type, type_instance, value));
264 } /* }}} int ascent_xml_submit_gauge */
265
266 static int ascent_xml_read_int(xmlDoc *doc, xmlNode *node, /* {{{ */
267                                int *ret_value) {
268   char *str_ptr;
269   int value;
270
271   str_ptr = (char *)xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
272   if (str_ptr == NULL) {
273     ERROR("ascent plugin: ascent_xml_read_int: xmlNodeListGetString failed.");
274     return (-1);
275   }
276
277   if (strcasecmp("N/A", str_ptr) == 0)
278     value = -1;
279   else {
280     char *end_ptr = NULL;
281     value = strtol(str_ptr, &end_ptr, 0);
282     if (str_ptr == end_ptr) {
283       xmlFree(str_ptr);
284       ERROR("ascent plugin: ascent_xml_read_int: strtol failed.");
285       return (-1);
286     }
287   }
288   xmlFree(str_ptr);
289
290   *ret_value = value;
291   return (0);
292 } /* }}} int ascent_xml_read_int */
293
294 static int ascent_xml_sessions_plr(xmlDoc *doc, xmlNode *node, /* {{{ */
295                                    player_info_t *pi) {
296   for (xmlNode *child = node->xmlChildrenNode; child != NULL;
297        child = child->next) {
298     if ((xmlStrcmp((const xmlChar *)"comment", child->name) == 0) ||
299         (xmlStrcmp((const xmlChar *)"text", child->name) == 0))
300       /* ignore */;
301     else if (xmlStrcmp((const xmlChar *)"race", child->name) == 0)
302       ascent_xml_read_int(doc, child, &pi->race);
303     else if (xmlStrcmp((const xmlChar *)"class", child->name) == 0)
304       ascent_xml_read_int(doc, child, &pi->class);
305     else if (xmlStrcmp((const xmlChar *)"gender", child->name) == 0)
306       ascent_xml_read_int(doc, child, &pi->gender);
307     else if (xmlStrcmp((const xmlChar *)"level", child->name) == 0)
308       ascent_xml_read_int(doc, child, &pi->level);
309     else if (xmlStrcmp((const xmlChar *)"latency", child->name) == 0)
310       ascent_xml_read_int(doc, child, &pi->latency);
311     else if ((xmlStrcmp((const xmlChar *)"name", child->name) == 0) ||
312              (xmlStrcmp((const xmlChar *)"pvprank", child->name) == 0) ||
313              (xmlStrcmp((const xmlChar *)"map", child->name) == 0) ||
314              (xmlStrcmp((const xmlChar *)"areaid", child->name) == 0) ||
315              (xmlStrcmp((const xmlChar *)"xpos", child->name) == 0) ||
316              (xmlStrcmp((const xmlChar *)"ypos", child->name) == 0) ||
317              (xmlStrcmp((const xmlChar *)"onime", child->name) == 0))
318       /* ignore */;
319     else {
320       WARNING("ascent plugin: ascent_xml_status: Unknown tag: %s", child->name);
321     }
322   } /* for (child) */
323
324   return (0);
325 } /* }}} int ascent_xml_sessions_plr */
326
327 static int ascent_xml_sessions(xmlDoc *doc, xmlNode *node) /* {{{ */
328 {
329   player_stats_t ps = {.level_sum = 0};
330
331   for (xmlNode *child = node->xmlChildrenNode; child != NULL;
332        child = child->next) {
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 *)"plr", child->name) == 0) {
337       int status;
338       player_info_t pi = PLAYER_INFO_STATIC_INIT;
339
340       status = ascent_xml_sessions_plr(doc, child, &pi);
341       if (status == 0)
342         ascent_account_player(&ps, &pi);
343     } else {
344       WARNING("ascent plugin: ascent_xml_status: Unknown tag: %s", child->name);
345     }
346   } /* for (child) */
347
348   ascent_submit_players(&ps);
349
350   return (0);
351 } /* }}} int ascent_xml_sessions */
352
353 static int ascent_xml_status(xmlDoc *doc, xmlNode *node) /* {{{ */
354 {
355   for (xmlNode *child = node->xmlChildrenNode; child != NULL;
356        child = child->next) {
357     if ((xmlStrcmp((const xmlChar *)"comment", child->name) == 0) ||
358         (xmlStrcmp((const xmlChar *)"text", child->name) == 0))
359       /* ignore */;
360     else if (xmlStrcmp((const xmlChar *)"alliance", child->name) == 0)
361       ascent_xml_submit_gauge(doc, child, NULL, "players", "alliance");
362     else if (xmlStrcmp((const xmlChar *)"horde", child->name) == 0)
363       ascent_xml_submit_gauge(doc, child, NULL, "players", "horde");
364     else if (xmlStrcmp((const xmlChar *)"qplayers", child->name) == 0)
365       ascent_xml_submit_gauge(doc, child, NULL, "players", "queued");
366     else if ((xmlStrcmp((const xmlChar *)"acceptedconns", child->name) == 0) ||
367              (xmlStrcmp((const xmlChar *)"avglat", child->name) == 0) ||
368              (xmlStrcmp((const xmlChar *)"cdbquerysize", child->name) == 0) ||
369              (xmlStrcmp((const xmlChar *)"cpu", child->name) == 0) ||
370              (xmlStrcmp((const xmlChar *)"fthreads", child->name) == 0) ||
371              (xmlStrcmp((const xmlChar *)"gmcount", child->name) == 0) ||
372              (xmlStrcmp((const xmlChar *)"lastupdate", child->name) == 0) ||
373              (xmlStrcmp((const xmlChar *)"ontime", child->name) == 0) ||
374              (xmlStrcmp((const xmlChar *)"oplayers", child->name) == 0) ||
375              (xmlStrcmp((const xmlChar *)"peakcount", child->name) == 0) ||
376              (xmlStrcmp((const xmlChar *)"platform", child->name) == 0) ||
377              (xmlStrcmp((const xmlChar *)"ram", child->name) == 0) ||
378              (xmlStrcmp((const xmlChar *)"threads", child->name) == 0) ||
379              (xmlStrcmp((const xmlChar *)"uptime", child->name) == 0) ||
380              (xmlStrcmp((const xmlChar *)"wdbquerysize", child->name) == 0))
381       /* ignore */;
382     else {
383       WARNING("ascent plugin: ascent_xml_status: Unknown tag: %s", child->name);
384     }
385   } /* for (child) */
386
387   return (0);
388 } /* }}} int ascent_xml_status */
389
390 static int ascent_xml(const char *data) /* {{{ */
391 {
392   xmlDoc *doc;
393   xmlNode *cur;
394
395 #if 0
396   doc = xmlParseMemory (data, strlen (data),
397       /* URL = */ "ascent.xml",
398       /* encoding = */ NULL,
399       /* options = */ 0);
400 #else
401   doc = xmlParseMemory(data, strlen(data));
402 #endif
403   if (doc == NULL) {
404     ERROR("ascent plugin: xmlParseMemory failed.");
405     return (-1);
406   }
407
408   cur = xmlDocGetRootElement(doc);
409   if (cur == NULL) {
410     ERROR("ascent plugin: XML document is empty.");
411     xmlFreeDoc(doc);
412     return (-1);
413   }
414
415   if (xmlStrcmp((const xmlChar *)"serverpage", cur->name) != 0) {
416     ERROR("ascent plugin: XML root element is not \"serverpage\".");
417     xmlFreeDoc(doc);
418     return (-1);
419   }
420
421   for (xmlNode *child = cur->xmlChildrenNode; child != NULL;
422        child = child->next) {
423     if ((xmlStrcmp((const xmlChar *)"comment", child->name) == 0) ||
424         (xmlStrcmp((const xmlChar *)"text", child->name) == 0))
425       /* ignore */;
426     else if (xmlStrcmp((const xmlChar *)"status", child->name) == 0)
427       ascent_xml_status(doc, child);
428     else if (xmlStrcmp((const xmlChar *)"instances", child->name) == 0)
429       /* ignore for now */;
430     else if (xmlStrcmp((const xmlChar *)"gms", child->name) == 0)
431       /* ignore for now */;
432     else if (xmlStrcmp((const xmlChar *)"sessions", child->name) == 0)
433       ascent_xml_sessions(doc, child);
434     else {
435       WARNING("ascent plugin: ascent_xml: Unknown tag: %s", child->name);
436     }
437   } /* for (child) */
438
439   xmlFreeDoc(doc);
440   return (0);
441 } /* }}} int ascent_xml */
442
443 static int config_set(char **var, const char *value) /* {{{ */
444 {
445   if (*var != NULL) {
446     free(*var);
447     *var = NULL;
448   }
449
450   if ((*var = strdup(value)) == NULL)
451     return (1);
452   else
453     return (0);
454 } /* }}} int config_set */
455
456 static int ascent_config(const char *key, const char *value) /* {{{ */
457 {
458   if (strcasecmp(key, "URL") == 0)
459     return (config_set(&url, value));
460   else if (strcasecmp(key, "User") == 0)
461     return (config_set(&user, value));
462   else if (strcasecmp(key, "Password") == 0)
463     return (config_set(&pass, value));
464   else if (strcasecmp(key, "VerifyPeer") == 0)
465     return (config_set(&verify_peer, value));
466   else if (strcasecmp(key, "VerifyHost") == 0)
467     return (config_set(&verify_host, value));
468   else if (strcasecmp(key, "CACert") == 0)
469     return (config_set(&cacert, value));
470   else if (strcasecmp(key, "Timeout") == 0)
471     return (config_set(&timeout, value));
472   else
473     return (-1);
474 } /* }}} int ascent_config */
475
476 static int ascent_init(void) /* {{{ */
477 {
478   if (url == NULL) {
479     WARNING("ascent plugin: ascent_init: No URL configured, "
480             "returning an error.");
481     return (-1);
482   }
483
484   if (curl != NULL) {
485     curl_easy_cleanup(curl);
486   }
487
488   if ((curl = curl_easy_init()) == NULL) {
489     ERROR("ascent plugin: ascent_init: curl_easy_init failed.");
490     return (-1);
491   }
492
493   curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1L);
494   curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, ascent_curl_callback);
495   curl_easy_setopt(curl, CURLOPT_USERAGENT, COLLECTD_USERAGENT);
496   curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, ascent_curl_error);
497
498   if (user != NULL) {
499 #ifdef HAVE_CURLOPT_USERNAME
500     curl_easy_setopt(curl, CURLOPT_USERNAME, user);
501     curl_easy_setopt(curl, CURLOPT_PASSWORD, (pass == NULL) ? "" : pass);
502 #else
503     static char credentials[1024];
504     int status;
505
506     status = ssnprintf(credentials, sizeof(credentials), "%s:%s", user,
507                        (pass == NULL) ? "" : pass);
508     if ((status < 0) || ((size_t)status >= sizeof(credentials))) {
509       ERROR("ascent plugin: ascent_init: Returning an error because the "
510             "credentials have been truncated.");
511       return (-1);
512     }
513
514     curl_easy_setopt(curl, CURLOPT_USERPWD, credentials);
515 #endif
516   }
517
518   curl_easy_setopt(curl, CURLOPT_URL, url);
519   curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
520   curl_easy_setopt(curl, CURLOPT_MAXREDIRS, 50L);
521
522   if ((verify_peer == NULL) || IS_TRUE(verify_peer))
523     curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1L);
524   else
525     curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
526
527   if ((verify_host == NULL) || IS_TRUE(verify_host))
528     curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 2L);
529   else
530     curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
531
532   if (cacert != NULL)
533     curl_easy_setopt(curl, CURLOPT_CAINFO, cacert);
534
535 #ifdef HAVE_CURLOPT_TIMEOUT_MS
536   if (timeout != NULL)
537     curl_easy_setopt(curl, CURLOPT_TIMEOUT_MS, atol(timeout));
538   else
539     curl_easy_setopt(curl, CURLOPT_TIMEOUT_MS,
540                      (long)CDTIME_T_TO_MS(plugin_get_interval()));
541 #endif
542
543   return (0);
544 } /* }}} int ascent_init */
545
546 static int ascent_read(void) /* {{{ */
547 {
548   int status;
549
550   if (curl == NULL) {
551     ERROR("ascent plugin: I don't have a CURL object.");
552     return (-1);
553   }
554
555   if (url == NULL) {
556     ERROR("ascent plugin: No URL has been configured.");
557     return (-1);
558   }
559
560   ascent_buffer_fill = 0;
561   if (curl_easy_perform(curl) != CURLE_OK) {
562     ERROR("ascent plugin: curl_easy_perform failed: %s", ascent_curl_error);
563     return (-1);
564   }
565
566   status = ascent_xml(ascent_buffer);
567   if (status != 0)
568     return (-1);
569   else
570     return (0);
571 } /* }}} int ascent_read */
572
573 void module_register(void) {
574   plugin_register_config("ascent", ascent_config, config_keys, config_keys_num);
575   plugin_register_init("ascent", ascent_init);
576   plugin_register_read("ascent", ascent_read);
577 } /* void module_register */
578
579 /* vim: set sw=2 sts=2 ts=8 et fdm=marker : */