command parser: Set command type to UNKNOWN upon failure.
[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.host, hostname_g, sizeof (vl.host));
134   sstrncpy (vl.plugin, "ascent", sizeof (vl.plugin));
135
136   if (plugin_instance != NULL)
137     sstrncpy (vl.plugin_instance, plugin_instance,
138         sizeof (vl.plugin_instance));
139
140   sstrncpy (vl.type, type, sizeof (vl.type));
141
142   if (type_instance != NULL)
143     sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
144
145   plugin_dispatch_values (&vl);
146   return (0);
147 } /* }}} int ascent_submit_gauge */
148
149 static size_t ascent_curl_callback (void *buf, size_t size, size_t nmemb, /* {{{ */
150     void __attribute__((unused)) *stream)
151 {
152   size_t len = size * nmemb;
153
154   if (len == 0)
155     return (len);
156
157   if ((ascent_buffer_fill + len) >= ascent_buffer_size)
158   {
159     char *temp;
160
161     temp = realloc (ascent_buffer,
162         ascent_buffer_fill + len + 1);
163     if (temp == NULL)
164     {
165       ERROR ("ascent plugin: realloc failed.");
166       return (0);
167     }
168     ascent_buffer = temp;
169     ascent_buffer_size = ascent_buffer_fill + len + 1;
170   }
171
172   memcpy (ascent_buffer + ascent_buffer_fill, (char *) buf, len);
173   ascent_buffer_fill += len;
174   ascent_buffer[ascent_buffer_fill] = 0;
175
176   return (len);
177 } /* }}} size_t ascent_curl_callback */
178
179 static int ascent_submit_players (player_stats_t *ps) /* {{{ */
180 {
181   gauge_t value;
182
183   for (size_t i = 0; i < RACES_LIST_LENGTH; i++)
184     if (races_list[i] != NULL)
185       ascent_submit_gauge ("by-race", "players", races_list[i],
186           (gauge_t) ps->races[i]);
187
188   for (size_t i = 0; i < CLASSES_LIST_LENGTH; i++)
189     if (classes_list[i] != NULL)
190       ascent_submit_gauge ("by-class", "players", classes_list[i],
191           (gauge_t) ps->classes[i]);
192
193   for (size_t i = 0; i < GENDERS_LIST_LENGTH; i++)
194     if (genders_list[i] != NULL)
195       ascent_submit_gauge ("by-gender", "players", genders_list[i],
196           (gauge_t) ps->genders[i]);
197
198   if (ps->level_num <= 0)
199     value = NAN;
200   else
201     value = ((double) ps->level_sum) / ((double) ps->level_num);
202   ascent_submit_gauge (NULL, "gauge", "avg-level", value);
203
204   /* Latency is in ms, but we store seconds. */
205   if (ps->latency_num <= 0)
206     value = NAN;
207   else
208     value = ((double) ps->latency_sum) / (1000.0 * ((double) ps->latency_num));
209   ascent_submit_gauge (NULL, "latency", "average", value);
210
211   return (0);
212 } /* }}} int ascent_submit_players */
213
214 static int ascent_account_player (player_stats_t *ps, /* {{{ */
215     player_info_t *pi)
216 {
217   if (pi->race >= 0)
218   {
219     if (((size_t) pi->race >= RACES_LIST_LENGTH)
220         || (races_list[pi->race] == NULL))
221       ERROR ("ascent plugin: Ignoring invalid numeric race %i.", pi->race);
222     else
223       ps->races[pi->race]++;
224   }
225
226   if (pi->class >= 0)
227   {
228     if (((size_t) pi->class >= CLASSES_LIST_LENGTH)
229         || (classes_list[pi->class] == NULL))
230       ERROR ("ascent plugin: Ignoring invalid numeric class %i.", pi->class);
231     else
232       ps->classes[pi->class]++;
233   }
234
235   if (pi->gender >= 0)
236   {
237     if (((size_t) pi->gender >= GENDERS_LIST_LENGTH)
238         || (genders_list[pi->gender] == NULL))
239       ERROR ("ascent plugin: Ignoring invalid numeric gender %i.",
240           pi->gender);
241     else
242       ps->genders[pi->gender]++;
243   }
244
245
246   if (pi->level > 0)
247   {
248     ps->level_sum += pi->level;
249     ps->level_num++;
250   }
251
252   if (pi->latency >= 0)
253   {
254     ps->latency_sum += pi->latency;
255     ps->latency_num++;
256   }
257
258   return (0);
259 } /* }}} int ascent_account_player */
260
261 static int ascent_xml_submit_gauge (xmlDoc *doc, xmlNode *node, /* {{{ */
262     const char *plugin_instance, const char *type, const char *type_instance)
263 {
264   char *str_ptr;
265   gauge_t value;
266
267   str_ptr = (char *) xmlNodeListGetString (doc, node->xmlChildrenNode, 1);
268   if (str_ptr == NULL)
269   {
270     ERROR ("ascent plugin: ascent_xml_submit_gauge: xmlNodeListGetString failed.");
271     return (-1);
272   }
273
274   if (strcasecmp ("N/A", str_ptr) == 0)
275     value = NAN;
276   else
277   {
278     char *end_ptr = NULL;
279     value = strtod (str_ptr, &end_ptr);
280     if (str_ptr == end_ptr)
281     {
282       xmlFree(str_ptr);
283       ERROR ("ascent plugin: ascent_xml_submit_gauge: strtod failed.");
284       return (-1);
285     }
286   }
287   xmlFree(str_ptr);
288
289   return (ascent_submit_gauge (plugin_instance, type, type_instance, value));
290 } /* }}} int ascent_xml_submit_gauge */
291
292 static int ascent_xml_read_int (xmlDoc *doc, xmlNode *node, /* {{{ */
293     int *ret_value)
294 {
295   char *str_ptr;
296   int value;
297
298   str_ptr = (char *) xmlNodeListGetString (doc, node->xmlChildrenNode, 1);
299   if (str_ptr == NULL)
300   {
301     ERROR ("ascent plugin: ascent_xml_read_int: xmlNodeListGetString failed.");
302     return (-1);
303   }
304
305   if (strcasecmp ("N/A", str_ptr) == 0)
306     value = -1;
307   else
308   {
309     char *end_ptr = NULL;
310     value = strtol (str_ptr, &end_ptr, 0);
311     if (str_ptr == end_ptr)
312     {
313       xmlFree(str_ptr);
314       ERROR ("ascent plugin: ascent_xml_read_int: strtol failed.");
315       return (-1);
316     }
317   }
318   xmlFree(str_ptr);
319
320   *ret_value = value;
321   return (0);
322 } /* }}} int ascent_xml_read_int */
323
324 static int ascent_xml_sessions_plr (xmlDoc *doc, xmlNode *node, /* {{{ */
325     player_info_t *pi)
326 {
327   for (xmlNode *child = node->xmlChildrenNode; child != NULL; child = child->next)
328   {
329     if ((xmlStrcmp ((const xmlChar *) "comment", child->name) == 0)
330         || (xmlStrcmp ((const xmlChar *) "text", child->name) == 0))
331       /* ignore */;
332     else if (xmlStrcmp ((const xmlChar *) "race", child->name) == 0)
333       ascent_xml_read_int (doc, child, &pi->race);
334     else if (xmlStrcmp ((const xmlChar *) "class", child->name) == 0)
335       ascent_xml_read_int (doc, child, &pi->class);
336     else if (xmlStrcmp ((const xmlChar *) "gender", child->name) == 0)
337       ascent_xml_read_int (doc, child, &pi->gender);
338     else if (xmlStrcmp ((const xmlChar *) "level", child->name) == 0)
339       ascent_xml_read_int (doc, child, &pi->level);
340     else if (xmlStrcmp ((const xmlChar *) "latency", child->name) == 0)
341       ascent_xml_read_int (doc, child, &pi->latency);
342     else if ((xmlStrcmp ((const xmlChar *) "name", child->name) == 0)
343         || (xmlStrcmp ((const xmlChar *) "pvprank", child->name) == 0)
344         || (xmlStrcmp ((const xmlChar *) "map", child->name) == 0)
345         || (xmlStrcmp ((const xmlChar *) "areaid", child->name) == 0)
346         || (xmlStrcmp ((const xmlChar *) "xpos", child->name) == 0)
347         || (xmlStrcmp ((const xmlChar *) "ypos", child->name) == 0)
348         || (xmlStrcmp ((const xmlChar *) "onime", child->name) == 0))
349       /* ignore */;
350     else
351     {
352       WARNING ("ascent plugin: ascent_xml_status: Unknown tag: %s", child->name);
353     }
354   } /* for (child) */
355
356   return (0);
357 } /* }}} int ascent_xml_sessions_plr */
358
359 static int ascent_xml_sessions (xmlDoc *doc, xmlNode *node) /* {{{ */
360 {
361   player_stats_t ps = {
362     .level_sum = 0
363   };
364
365   for (xmlNode *child = node->xmlChildrenNode; child != NULL; child = child->next)
366   {
367     if ((xmlStrcmp ((const xmlChar *) "comment", child->name) == 0)
368         || (xmlStrcmp ((const xmlChar *) "text", child->name) == 0))
369       /* ignore */;
370     else if (xmlStrcmp ((const xmlChar *) "plr", child->name) == 0)
371     {
372       int status;
373       player_info_t pi = PLAYER_INFO_STATIC_INIT;
374
375       status = ascent_xml_sessions_plr (doc, child, &pi);
376       if (status == 0)
377         ascent_account_player (&ps, &pi);
378     }
379     else
380     {
381       WARNING ("ascent plugin: ascent_xml_status: Unknown tag: %s", child->name);
382     }
383   } /* for (child) */
384
385   ascent_submit_players (&ps);
386
387   return (0);
388 } /* }}} int ascent_xml_sessions */
389
390 static int ascent_xml_status (xmlDoc *doc, xmlNode *node) /* {{{ */
391 {
392   for (xmlNode *child = node->xmlChildrenNode; child != NULL; child = child->next)
393   {
394     if ((xmlStrcmp ((const xmlChar *) "comment", child->name) == 0)
395         || (xmlStrcmp ((const xmlChar *) "text", child->name) == 0))
396       /* ignore */;
397     else if (xmlStrcmp ((const xmlChar *) "alliance", child->name) == 0)
398       ascent_xml_submit_gauge (doc, child, NULL, "players", "alliance");
399     else if (xmlStrcmp ((const xmlChar *) "horde", child->name) == 0)
400       ascent_xml_submit_gauge (doc, child, NULL, "players", "horde");
401     else if (xmlStrcmp ((const xmlChar *) "qplayers", child->name) == 0)
402       ascent_xml_submit_gauge (doc, child, NULL, "players", "queued");
403     else if ((xmlStrcmp ((const xmlChar *) "acceptedconns", child->name) == 0)
404         || (xmlStrcmp ((const xmlChar *) "avglat", child->name) == 0)
405         || (xmlStrcmp ((const xmlChar *) "cdbquerysize", child->name) == 0)
406         || (xmlStrcmp ((const xmlChar *) "cpu", child->name) == 0)
407         || (xmlStrcmp ((const xmlChar *) "fthreads", child->name) == 0)
408         || (xmlStrcmp ((const xmlChar *) "gmcount", child->name) == 0)
409         || (xmlStrcmp ((const xmlChar *) "lastupdate", child->name) == 0)
410         || (xmlStrcmp ((const xmlChar *) "ontime", child->name) == 0)
411         || (xmlStrcmp ((const xmlChar *) "oplayers", child->name) == 0)
412         || (xmlStrcmp ((const xmlChar *) "peakcount", child->name) == 0)
413         || (xmlStrcmp ((const xmlChar *) "platform", child->name) == 0)
414         || (xmlStrcmp ((const xmlChar *) "ram", child->name) == 0)
415         || (xmlStrcmp ((const xmlChar *) "threads", child->name) == 0)
416         || (xmlStrcmp ((const xmlChar *) "uptime", child->name) == 0)
417         || (xmlStrcmp ((const xmlChar *) "wdbquerysize", child->name) == 0))
418       /* ignore */;
419     else
420     {
421       WARNING ("ascent plugin: ascent_xml_status: Unknown tag: %s", child->name);
422     }
423   } /* for (child) */
424
425   return (0);
426 } /* }}} int ascent_xml_status */
427
428 static int ascent_xml (const char *data) /* {{{ */
429 {
430   xmlDoc *doc;
431   xmlNode *cur;
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 (xmlNode *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 if (strcasecmp (key, "Timeout") == 0)
514     return (config_set (&timeout, value));
515   else
516     return (-1);
517 } /* }}} int ascent_config */
518
519 static int ascent_init (void) /* {{{ */
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_NOSIGNAL, 1L);
540   curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, ascent_curl_callback);
541   curl_easy_setopt (curl, CURLOPT_USERAGENT, COLLECTD_USERAGENT);
542   curl_easy_setopt (curl, CURLOPT_ERRORBUFFER, ascent_curl_error);
543
544   if (user != NULL)
545   {
546 #ifdef HAVE_CURLOPT_USERNAME
547     curl_easy_setopt (curl, CURLOPT_USERNAME, user);
548     curl_easy_setopt (curl, CURLOPT_PASSWORD, (pass == NULL) ? "" : pass);
549 #else
550     static char credentials[1024];
551     int status;
552
553     status = ssnprintf (credentials, sizeof (credentials), "%s:%s",
554         user, (pass == NULL) ? "" : pass);
555     if ((status < 0) || ((size_t) status >= sizeof (credentials)))
556     {
557       ERROR ("ascent plugin: ascent_init: Returning an error because the "
558           "credentials have been truncated.");
559       return (-1);
560     }
561
562     curl_easy_setopt (curl, CURLOPT_USERPWD, credentials);
563 #endif
564   }
565
566   curl_easy_setopt (curl, CURLOPT_URL, url);
567   curl_easy_setopt (curl, CURLOPT_FOLLOWLOCATION, 1L);
568   curl_easy_setopt (curl, CURLOPT_MAXREDIRS, 50L);
569
570   if ((verify_peer == NULL) || IS_TRUE (verify_peer))
571     curl_easy_setopt (curl, CURLOPT_SSL_VERIFYPEER, 1L);
572   else
573     curl_easy_setopt (curl, CURLOPT_SSL_VERIFYPEER, 0L);
574
575   if ((verify_host == NULL) || IS_TRUE (verify_host))
576     curl_easy_setopt (curl, CURLOPT_SSL_VERIFYHOST, 2L);
577   else
578     curl_easy_setopt (curl, CURLOPT_SSL_VERIFYHOST, 0L);
579
580   if (cacert != NULL)
581     curl_easy_setopt (curl, CURLOPT_CAINFO, cacert);
582
583 #ifdef HAVE_CURLOPT_TIMEOUT_MS
584   if (timeout != NULL)
585     curl_easy_setopt (curl, CURLOPT_TIMEOUT_MS, atol(timeout));
586   else
587     curl_easy_setopt (curl, CURLOPT_TIMEOUT_MS, (long) CDTIME_T_TO_MS(plugin_get_interval()));
588 #endif
589
590   return (0);
591 } /* }}} int ascent_init */
592
593 static int ascent_read (void) /* {{{ */
594 {
595   int status;
596
597   if (curl == NULL)
598   {
599     ERROR ("ascent plugin: I don't have a CURL object.");
600     return (-1);
601   }
602
603   if (url == NULL)
604   {
605     ERROR ("ascent plugin: No URL has been configured.");
606     return (-1);
607   }
608
609   ascent_buffer_fill = 0;
610   if (curl_easy_perform (curl) != CURLE_OK)
611   {
612     ERROR ("ascent plugin: curl_easy_perform failed: %s",
613         ascent_curl_error);
614     return (-1);
615   }
616
617   status = ascent_xml (ascent_buffer);
618   if (status != 0)
619     return (-1);
620   else
621     return (0);
622 } /* }}} int ascent_read */
623
624 void module_register (void)
625 {
626   plugin_register_config ("ascent", ascent_config, config_keys, config_keys_num);
627   plugin_register_init ("ascent", ascent_init);
628   plugin_register_read ("ascent", ascent_read);
629 } /* void module_register */
630
631 /* vim: set sw=2 sts=2 ts=8 et fdm=marker : */