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