2 * collectd - src/ascent.c
3 * Copyright (C) 2008 Florian octo Forster
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.
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.
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
19 * Florian octo Forster <octo at verplant.org>
25 #include "configfile.h"
27 #include <curl/curl.h>
28 #include <libxml/parser.h>
30 static char *races_list[] = /* {{{ */
45 #define RACES_LIST_LENGTH STATIC_ARRAY_SIZE (races_list)
47 static char *classes_list[] = /* {{{ */
62 #define CLASSES_LIST_LENGTH STATIC_ARRAY_SIZE (classes_list)
64 static char *genders_list[] = /* {{{ */
69 #define GENDERS_LIST_LENGTH STATIC_ARRAY_SIZE (genders_list)
73 int races[RACES_LIST_LENGTH];
74 int classes[CLASSES_LIST_LENGTH];
75 int genders[GENDERS_LIST_LENGTH];
81 typedef struct player_stats_s player_stats_t;
91 typedef struct player_info_s player_info_t;
92 #define PLAYER_INFO_STATIC_INIT { -1, -1, -1, -1, -1 }
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;
101 static CURL *curl = NULL;
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];
108 static const char *config_keys[] =
117 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
119 static int ascent_submit_gauge (const char *plugin_instance, /* {{{ */
120 const char *type, const char *type_instance, gauge_t value)
123 value_list_t vl = VALUE_LIST_INIT;
125 values[0].gauge = value;
129 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
130 sstrncpy (vl.plugin, "ascent", sizeof (vl.plugin));
132 if (plugin_instance != NULL)
133 sstrncpy (vl.plugin_instance, plugin_instance,
134 sizeof (vl.plugin_instance));
136 sstrncpy (vl.type, type, sizeof (vl.type));
138 if (type_instance != NULL)
139 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
141 plugin_dispatch_values (&vl);
143 } /* }}} int ascent_submit_gauge */
145 static size_t ascent_curl_callback (void *buf, size_t size, size_t nmemb, /* {{{ */
146 void __attribute__((unused)) *stream)
148 size_t len = size * nmemb;
153 if ((ascent_buffer_fill + len) >= ascent_buffer_size)
157 temp = (char *) realloc (ascent_buffer,
158 ascent_buffer_fill + len + 1);
161 ERROR ("ascent plugin: realloc failed.");
164 ascent_buffer = temp;
165 ascent_buffer_size = ascent_buffer_fill + len + 1;
168 memcpy (ascent_buffer + ascent_buffer_fill, (char *) buf, len);
169 ascent_buffer_fill += len;
170 ascent_buffer[ascent_buffer_fill] = 0;
173 } /* }}} size_t ascent_curl_callback */
175 static int ascent_submit_players (player_stats_t *ps) /* {{{ */
180 for (i = 0; i < RACES_LIST_LENGTH; i++)
181 if (races_list[i] != NULL)
182 ascent_submit_gauge ("by-race", "players", races_list[i],
183 (gauge_t) ps->races[i]);
185 for (i = 0; i < CLASSES_LIST_LENGTH; i++)
186 if (classes_list[i] != NULL)
187 ascent_submit_gauge ("by-class", "players", classes_list[i],
188 (gauge_t) ps->classes[i]);
190 for (i = 0; i < GENDERS_LIST_LENGTH; i++)
191 if (genders_list[i] != NULL)
192 ascent_submit_gauge ("by-gender", "players", genders_list[i],
193 (gauge_t) ps->genders[i]);
195 if (ps->level_num <= 0)
198 value = ((double) ps->level_sum) / ((double) ps->level_num);
199 ascent_submit_gauge (NULL, "gauge", "avg-level", value);
201 /* Latency is in ms, but we store seconds. */
202 if (ps->latency_num <= 0)
205 value = ((double) ps->latency_sum) / (1000.0 * ((double) ps->latency_num));
206 ascent_submit_gauge (NULL, "latency", "average", value);
209 } /* }}} int ascent_submit_players */
211 static int ascent_account_player (player_stats_t *ps, /* {{{ */
216 if (((size_t) pi->race >= RACES_LIST_LENGTH)
217 || (races_list[pi->race] == NULL))
218 ERROR ("ascent plugin: Ignoring invalid numeric race %i.", pi->race);
220 ps->races[pi->race]++;
225 if (((size_t) pi->class >= CLASSES_LIST_LENGTH)
226 || (classes_list[pi->class] == NULL))
227 ERROR ("ascent plugin: Ignoring invalid numeric class %i.", pi->class);
229 ps->classes[pi->class]++;
234 if (((size_t) pi->gender >= GENDERS_LIST_LENGTH)
235 || (genders_list[pi->gender] == NULL))
236 ERROR ("ascent plugin: Ignoring invalid numeric gender %i.",
239 ps->genders[pi->gender]++;
245 ps->level_sum += pi->level;
249 if (pi->latency >= 0)
251 ps->latency_sum += pi->latency;
256 } /* }}} int ascent_account_player */
258 static int ascent_xml_submit_gauge (xmlDoc *doc, xmlNode *node, /* {{{ */
259 const char *plugin_instance, const char *type, const char *type_instance)
264 str_ptr = (char *) xmlNodeListGetString (doc, node->xmlChildrenNode, 1);
267 ERROR ("ascent plugin: ascent_xml_submit_gauge: xmlNodeListGetString failed.");
271 if (strcasecmp ("N/A", str_ptr) == 0)
275 char *end_ptr = NULL;
276 value = strtod (str_ptr, &end_ptr);
277 if (str_ptr == end_ptr)
280 ERROR ("ascent plugin: ascent_xml_submit_gauge: strtod failed.");
286 return (ascent_submit_gauge (plugin_instance, type, type_instance, value));
287 } /* }}} int ascent_xml_submit_gauge */
289 static int ascent_xml_read_int (xmlDoc *doc, xmlNode *node, /* {{{ */
295 str_ptr = (char *) xmlNodeListGetString (doc, node->xmlChildrenNode, 1);
298 ERROR ("ascent plugin: ascent_xml_read_int: xmlNodeListGetString failed.");
302 if (strcasecmp ("N/A", str_ptr) == 0)
306 char *end_ptr = NULL;
307 value = strtol (str_ptr, &end_ptr, 0);
308 if (str_ptr == end_ptr)
311 ERROR ("ascent plugin: ascent_xml_read_int: strtol failed.");
319 } /* }}} int ascent_xml_read_int */
321 static int ascent_xml_sessions_plr (xmlDoc *doc, xmlNode *node, /* {{{ */
326 for (child = node->xmlChildrenNode; child != NULL; child = child->next)
328 if ((xmlStrcmp ((const xmlChar *) "comment", child->name) == 0)
329 || (xmlStrcmp ((const xmlChar *) "text", child->name) == 0))
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))
351 WARNING ("ascent plugin: ascent_xml_status: Unknown tag: %s", child->name);
356 } /* }}} int ascent_xml_sessions_plr */
358 static int ascent_xml_sessions (xmlDoc *doc, xmlNode *node) /* {{{ */
363 memset (&ps, 0, sizeof (ps));
365 for (child = node->xmlChildrenNode; child != NULL; child = child->next)
367 if ((xmlStrcmp ((const xmlChar *) "comment", child->name) == 0)
368 || (xmlStrcmp ((const xmlChar *) "text", child->name) == 0))
370 else if (xmlStrcmp ((const xmlChar *) "plr", child->name) == 0)
373 player_info_t pi = PLAYER_INFO_STATIC_INIT;
375 status = ascent_xml_sessions_plr (doc, child, &pi);
377 ascent_account_player (&ps, &pi);
381 WARNING ("ascent plugin: ascent_xml_status: Unknown tag: %s", child->name);
385 ascent_submit_players (&ps);
388 } /* }}} int ascent_xml_sessions */
390 static int ascent_xml_status (xmlDoc *doc, xmlNode *node) /* {{{ */
394 for (child = node->xmlChildrenNode; child != NULL; child = child->next)
396 if ((xmlStrcmp ((const xmlChar *) "comment", child->name) == 0)
397 || (xmlStrcmp ((const xmlChar *) "text", child->name) == 0))
399 else if (xmlStrcmp ((const xmlChar *) "alliance", child->name) == 0)
400 ascent_xml_submit_gauge (doc, child, NULL, "players", "alliance");
401 else if (xmlStrcmp ((const xmlChar *) "horde", child->name) == 0)
402 ascent_xml_submit_gauge (doc, child, NULL, "players", "horde");
403 else if (xmlStrcmp ((const xmlChar *) "qplayers", child->name) == 0)
404 ascent_xml_submit_gauge (doc, child, NULL, "players", "queued");
405 else if ((xmlStrcmp ((const xmlChar *) "acceptedconns", child->name) == 0)
406 || (xmlStrcmp ((const xmlChar *) "avglat", child->name) == 0)
407 || (xmlStrcmp ((const xmlChar *) "cdbquerysize", child->name) == 0)
408 || (xmlStrcmp ((const xmlChar *) "cpu", child->name) == 0)
409 || (xmlStrcmp ((const xmlChar *) "fthreads", child->name) == 0)
410 || (xmlStrcmp ((const xmlChar *) "gmcount", child->name) == 0)
411 || (xmlStrcmp ((const xmlChar *) "lastupdate", child->name) == 0)
412 || (xmlStrcmp ((const xmlChar *) "ontime", child->name) == 0)
413 || (xmlStrcmp ((const xmlChar *) "oplayers", child->name) == 0)
414 || (xmlStrcmp ((const xmlChar *) "peakcount", child->name) == 0)
415 || (xmlStrcmp ((const xmlChar *) "platform", child->name) == 0)
416 || (xmlStrcmp ((const xmlChar *) "ram", child->name) == 0)
417 || (xmlStrcmp ((const xmlChar *) "threads", child->name) == 0)
418 || (xmlStrcmp ((const xmlChar *) "uptime", child->name) == 0)
419 || (xmlStrcmp ((const xmlChar *) "wdbquerysize", child->name) == 0))
423 WARNING ("ascent plugin: ascent_xml_status: Unknown tag: %s", child->name);
428 } /* }}} int ascent_xml_status */
430 static int ascent_xml (const char *data) /* {{{ */
437 doc = xmlParseMemory (data, strlen (data),
438 /* URL = */ "ascent.xml",
439 /* encoding = */ NULL,
442 doc = xmlParseMemory (data, strlen (data));
446 ERROR ("ascent plugin: xmlParseMemory failed.");
450 cur = xmlDocGetRootElement (doc);
453 ERROR ("ascent plugin: XML document is empty.");
458 if (xmlStrcmp ((const xmlChar *) "serverpage", cur->name) != 0)
460 ERROR ("ascent plugin: XML root element is not \"serverpage\".");
465 for (child = cur->xmlChildrenNode; child != NULL; child = child->next)
467 if ((xmlStrcmp ((const xmlChar *) "comment", child->name) == 0)
468 || (xmlStrcmp ((const xmlChar *) "text", child->name) == 0))
470 else if (xmlStrcmp ((const xmlChar *) "status", child->name) == 0)
471 ascent_xml_status (doc, child);
472 else if (xmlStrcmp ((const xmlChar *) "instances", child->name) == 0)
473 /* ignore for now */;
474 else if (xmlStrcmp ((const xmlChar *) "gms", child->name) == 0)
475 /* ignore for now */;
476 else if (xmlStrcmp ((const xmlChar *) "sessions", child->name) == 0)
477 ascent_xml_sessions (doc, child);
480 WARNING ("ascent plugin: ascent_xml: Unknown tag: %s", child->name);
486 } /* }}} int ascent_xml */
488 static int config_set (char **var, const char *value) /* {{{ */
496 if ((*var = strdup (value)) == NULL)
500 } /* }}} int config_set */
502 static int ascent_config (const char *key, const char *value) /* {{{ */
504 if (strcasecmp (key, "URL") == 0)
505 return (config_set (&url, value));
506 else if (strcasecmp (key, "User") == 0)
507 return (config_set (&user, value));
508 else if (strcasecmp (key, "Password") == 0)
509 return (config_set (&pass, value));
510 else if (strcasecmp (key, "VerifyPeer") == 0)
511 return (config_set (&verify_peer, value));
512 else if (strcasecmp (key, "VerifyHost") == 0)
513 return (config_set (&verify_host, value));
514 else if (strcasecmp (key, "CACert") == 0)
515 return (config_set (&cacert, value));
518 } /* }}} int ascent_config */
520 static int ascent_init (void) /* {{{ */
522 static char credentials[1024];
526 WARNING ("ascent plugin: ascent_init: No URL configured, "
527 "returning an error.");
533 curl_easy_cleanup (curl);
536 if ((curl = curl_easy_init ()) == NULL)
538 ERROR ("ascent plugin: ascent_init: curl_easy_init failed.");
542 curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, ascent_curl_callback);
543 curl_easy_setopt (curl, CURLOPT_USERAGENT, PACKAGE_NAME"/"PACKAGE_VERSION);
544 curl_easy_setopt (curl, CURLOPT_ERRORBUFFER, ascent_curl_error);
550 status = ssnprintf (credentials, sizeof (credentials), "%s:%s",
551 user, (pass == NULL) ? "" : pass);
552 if ((status < 0) || ((size_t) status >= sizeof (credentials)))
554 ERROR ("ascent plugin: ascent_init: Returning an error because the "
555 "credentials have been truncated.");
559 curl_easy_setopt (curl, CURLOPT_USERPWD, credentials);
562 curl_easy_setopt (curl, CURLOPT_URL, url);
564 if ((verify_peer == NULL) || (strcmp (verify_peer, "true") == 0))
565 curl_easy_setopt (curl, CURLOPT_SSL_VERIFYPEER, 1);
567 curl_easy_setopt (curl, CURLOPT_SSL_VERIFYPEER, 0);
569 if ((verify_host == NULL) || (strcmp (verify_host, "true") == 0))
570 curl_easy_setopt (curl, CURLOPT_SSL_VERIFYHOST, 2);
572 curl_easy_setopt (curl, CURLOPT_SSL_VERIFYHOST, 0);
575 curl_easy_setopt (curl, CURLOPT_CAINFO, cacert);
578 } /* }}} int ascent_init */
580 static int ascent_read (void) /* {{{ */
586 ERROR ("ascent plugin: I don't have a CURL object.");
592 ERROR ("ascent plugin: No URL has been configured.");
596 ascent_buffer_fill = 0;
597 if (curl_easy_perform (curl) != 0)
599 ERROR ("ascent plugin: curl_easy_perform failed: %s",
604 status = ascent_xml (ascent_buffer);
609 } /* }}} int ascent_read */
611 void module_register (void)
613 plugin_register_config ("ascent", ascent_config, config_keys, config_keys_num);
614 plugin_register_init ("ascent", ascent_init);
615 plugin_register_read ("ascent", ascent_read);
616 } /* void module_register */
618 /* vim: set sw=2 sts=2 ts=8 et fdm=marker : */