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