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 vl.time = time (NULL);
130 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
131 sstrncpy (vl.plugin, "ascent", sizeof (vl.plugin));
133 if (plugin_instance != NULL)
134 sstrncpy (vl.plugin_instance, plugin_instance,
135 sizeof (vl.plugin_instance));
137 sstrncpy (vl.type, type, sizeof (vl.type));
139 if (type_instance != NULL)
140 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
142 plugin_dispatch_values (&vl);
144 } /* }}} int ascent_submit_gauge */
146 static size_t ascent_curl_callback (void *buf, size_t size, size_t nmemb, /* {{{ */
149 size_t len = size * nmemb;
154 if ((ascent_buffer_fill + len) >= ascent_buffer_size)
158 temp = (char *) realloc (ascent_buffer,
159 ascent_buffer_fill + len + 1);
162 ERROR ("ascent plugin: realloc failed.");
165 ascent_buffer = temp;
166 ascent_buffer_size = ascent_buffer_fill + len + 1;
169 memcpy (ascent_buffer + ascent_buffer_fill, (char *) buf, len);
170 ascent_buffer_fill += len;
171 ascent_buffer[ascent_buffer_fill] = 0;
174 } /* }}} size_t ascent_curl_callback */
176 static int ascent_submit_players (player_stats_t *ps) /* {{{ */
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]);
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]);
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]);
196 if (ps->level_num <= 0)
199 value = ((double) ps->level_sum) / ((double) ps->level_num);
200 ascent_submit_gauge (NULL, "gauge", "avg-level", value);
202 /* Latency is in ms, but we store seconds. */
203 if (ps->latency_num <= 0)
206 value = ((double) ps->latency_sum) / (1000.0 * ((double) ps->latency_num));
207 ascent_submit_gauge (NULL, "latency", "average", value);
210 } /* }}} int ascent_submit_players */
212 static int ascent_account_player (player_stats_t *ps, /* {{{ */
217 if ((pi->race >= RACES_LIST_LENGTH)
218 || (races_list[pi->race] == NULL))
219 ERROR ("ascent plugin: Ignoring invalid numeric race %i.", pi->race);
221 ps->races[pi->race]++;
226 if ((pi->class >= CLASSES_LIST_LENGTH)
227 || (classes_list[pi->class] == NULL))
228 ERROR ("ascent plugin: Ignoring invalid numeric class %i.", pi->class);
230 ps->classes[pi->class]++;
235 if ((pi->gender >= GENDERS_LIST_LENGTH)
236 || (genders_list[pi->gender] == NULL))
237 ERROR ("ascent plugin: Ignoring invalid numeric gender %i.",
240 ps->genders[pi->gender]++;
246 ps->level_sum += pi->level;
250 if (pi->latency >= 0)
252 ps->latency_sum += pi->latency;
257 } /* }}} int ascent_account_player */
259 static int ascent_xml_submit_gauge (xmlDoc *doc, xmlNode *node, /* {{{ */
260 const char *plugin_instance, const char *type, const char *type_instance)
265 str_ptr = (char *) xmlNodeListGetString (doc, node->xmlChildrenNode, 1);
268 ERROR ("ascent plugin: ascent_xml_submit_gauge: xmlNodeListGetString failed.");
272 if (strcasecmp ("N/A", str_ptr) == 0)
276 char *end_ptr = NULL;
277 value = strtod (str_ptr, &end_ptr);
278 if (str_ptr == end_ptr)
281 ERROR ("ascent plugin: ascent_xml_submit_gauge: strtod failed.");
287 return (ascent_submit_gauge (plugin_instance, type, type_instance, value));
288 } /* }}} int ascent_xml_submit_gauge */
290 static int ascent_xml_read_int (xmlDoc *doc, xmlNode *node, /* {{{ */
296 str_ptr = (char *) xmlNodeListGetString (doc, node->xmlChildrenNode, 1);
299 ERROR ("ascent plugin: ascent_xml_read_int: xmlNodeListGetString failed.");
303 if (strcasecmp ("N/A", str_ptr) == 0)
307 char *end_ptr = NULL;
308 value = strtol (str_ptr, &end_ptr, 0);
309 if (str_ptr == end_ptr)
312 ERROR ("ascent plugin: ascent_xml_read_int: strtol failed.");
320 } /* }}} int ascent_xml_read_int */
322 static int ascent_xml_sessions_plr (xmlDoc *doc, xmlNode *node, /* {{{ */
327 for (child = node->xmlChildrenNode; child != NULL; child = child->next)
329 if ((xmlStrcmp ((const xmlChar *) "comment", child->name) == 0)
330 || (xmlStrcmp ((const xmlChar *) "text", child->name) == 0))
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))
352 WARNING ("ascent plugin: ascent_xml_status: Unknown tag: %s", child->name);
357 } /* }}} int ascent_xml_sessions_plr */
359 static int ascent_xml_sessions (xmlDoc *doc, xmlNode *node) /* {{{ */
364 memset (&ps, 0, sizeof (ps));
366 for (child = node->xmlChildrenNode; child != NULL; child = child->next)
368 if ((xmlStrcmp ((const xmlChar *) "comment", child->name) == 0)
369 || (xmlStrcmp ((const xmlChar *) "text", child->name) == 0))
371 else if (xmlStrcmp ((const xmlChar *) "plr", child->name) == 0)
374 player_info_t pi = PLAYER_INFO_STATIC_INIT;
376 status = ascent_xml_sessions_plr (doc, child, &pi);
378 ascent_account_player (&ps, &pi);
382 WARNING ("ascent plugin: ascent_xml_status: Unknown tag: %s", child->name);
386 ascent_submit_players (&ps);
389 } /* }}} int ascent_xml_sessions */
391 static int ascent_xml_status (xmlDoc *doc, xmlNode *node) /* {{{ */
395 for (child = node->xmlChildrenNode; child != NULL; child = child->next)
397 if ((xmlStrcmp ((const xmlChar *) "comment", child->name) == 0)
398 || (xmlStrcmp ((const xmlChar *) "text", child->name) == 0))
400 else if (xmlStrcmp ((const xmlChar *) "alliance", child->name) == 0)
401 ascent_xml_submit_gauge (doc, child, NULL, "players", "alliance");
402 else if (xmlStrcmp ((const xmlChar *) "horde", child->name) == 0)
403 ascent_xml_submit_gauge (doc, child, NULL, "players", "horde");
404 else if (xmlStrcmp ((const xmlChar *) "qplayers", child->name) == 0)
405 ascent_xml_submit_gauge (doc, child, NULL, "players", "queued");
406 else if ((xmlStrcmp ((const xmlChar *) "acceptedconns", child->name) == 0)
407 || (xmlStrcmp ((const xmlChar *) "avglat", child->name) == 0)
408 || (xmlStrcmp ((const xmlChar *) "cdbquerysize", child->name) == 0)
409 || (xmlStrcmp ((const xmlChar *) "cpu", child->name) == 0)
410 || (xmlStrcmp ((const xmlChar *) "fthreads", child->name) == 0)
411 || (xmlStrcmp ((const xmlChar *) "gmcount", child->name) == 0)
412 || (xmlStrcmp ((const xmlChar *) "lastupdate", child->name) == 0)
413 || (xmlStrcmp ((const xmlChar *) "ontime", child->name) == 0)
414 || (xmlStrcmp ((const xmlChar *) "oplayers", child->name) == 0)
415 || (xmlStrcmp ((const xmlChar *) "peakcount", child->name) == 0)
416 || (xmlStrcmp ((const xmlChar *) "platform", child->name) == 0)
417 || (xmlStrcmp ((const xmlChar *) "ram", child->name) == 0)
418 || (xmlStrcmp ((const xmlChar *) "threads", child->name) == 0)
419 || (xmlStrcmp ((const xmlChar *) "uptime", child->name) == 0)
420 || (xmlStrcmp ((const xmlChar *) "wdbquerysize", child->name) == 0))
424 WARNING ("ascent plugin: ascent_xml_status: Unknown tag: %s", child->name);
429 } /* }}} int ascent_xml_status */
431 static int ascent_xml (const char *data) /* {{{ */
438 doc = xmlParseMemory (data, strlen (data),
439 /* URL = */ "ascent.xml",
440 /* encoding = */ NULL,
443 doc = xmlParseMemory (data, strlen (data));
447 ERROR ("ascent plugin: xmlParseMemory failed.");
451 cur = xmlDocGetRootElement (doc);
454 ERROR ("ascent plugin: XML document is empty.");
459 if (xmlStrcmp ((const xmlChar *) "serverpage", cur->name) != 0)
461 ERROR ("ascent plugin: XML root element is not \"serverpage\".");
466 for (child = cur->xmlChildrenNode; child != NULL; child = child->next)
468 if ((xmlStrcmp ((const xmlChar *) "comment", child->name) == 0)
469 || (xmlStrcmp ((const xmlChar *) "text", child->name) == 0))
471 else if (xmlStrcmp ((const xmlChar *) "status", child->name) == 0)
472 ascent_xml_status (doc, child);
473 else if (xmlStrcmp ((const xmlChar *) "instances", child->name) == 0)
474 /* ignore for now */;
475 else if (xmlStrcmp ((const xmlChar *) "gms", child->name) == 0)
476 /* ignore for now */;
477 else if (xmlStrcmp ((const xmlChar *) "sessions", child->name) == 0)
478 ascent_xml_sessions (doc, child);
481 WARNING ("ascent plugin: ascent_xml: Unknown tag: %s", child->name);
487 } /* }}} int ascent_xml */
489 static int config_set (char **var, const char *value) /* {{{ */
497 if ((*var = strdup (value)) == NULL)
501 } /* }}} int config_set */
503 static int ascent_config (const char *key, const char *value) /* {{{ */
505 if (strcasecmp (key, "URL") == 0)
506 return (config_set (&url, value));
507 else if (strcasecmp (key, "User") == 0)
508 return (config_set (&user, value));
509 else if (strcasecmp (key, "Password") == 0)
510 return (config_set (&pass, value));
511 else if (strcasecmp (key, "VerifyPeer") == 0)
512 return (config_set (&verify_peer, value));
513 else if (strcasecmp (key, "VerifyHost") == 0)
514 return (config_set (&verify_host, value));
515 else if (strcasecmp (key, "CACert") == 0)
516 return (config_set (&cacert, value));
519 } /* }}} int ascent_config */
521 static int ascent_init (void) /* {{{ */
523 static char credentials[1024];
527 WARNING ("ascent plugin: ascent_init: No URL configured, "
528 "returning an error.");
534 curl_easy_cleanup (curl);
537 if ((curl = curl_easy_init ()) == NULL)
539 ERROR ("ascent plugin: ascent_init: curl_easy_init failed.");
543 curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, ascent_curl_callback);
544 curl_easy_setopt (curl, CURLOPT_USERAGENT, PACKAGE_NAME"/"PACKAGE_VERSION);
545 curl_easy_setopt (curl, CURLOPT_ERRORBUFFER, ascent_curl_error);
551 status = ssnprintf (credentials, sizeof (credentials), "%s:%s",
552 user, (pass == NULL) ? "" : pass);
553 if (status >= sizeof (credentials))
555 ERROR ("ascent plugin: ascent_init: Returning an error because the "
556 "credentials have been truncated.");
560 curl_easy_setopt (curl, CURLOPT_USERPWD, credentials);
563 curl_easy_setopt (curl, CURLOPT_URL, url);
565 if ((verify_peer == NULL) || (strcmp (verify_peer, "true") == 0))
566 curl_easy_setopt (curl, CURLOPT_SSL_VERIFYPEER, 1);
568 curl_easy_setopt (curl, CURLOPT_SSL_VERIFYPEER, 0);
570 if ((verify_host == NULL) || (strcmp (verify_host, "true") == 0))
571 curl_easy_setopt (curl, CURLOPT_SSL_VERIFYHOST, 2);
573 curl_easy_setopt (curl, CURLOPT_SSL_VERIFYHOST, 0);
576 curl_easy_setopt (curl, CURLOPT_CAINFO, cacert);
579 } /* }}} int ascent_init */
581 static int ascent_read (void) /* {{{ */
587 ERROR ("ascent plugin: I don't have a CURL object.");
593 ERROR ("ascent plugin: No URL has been configured.");
597 ascent_buffer_fill = 0;
598 if (curl_easy_perform (curl) != 0)
600 ERROR ("ascent plugin: curl_easy_perform failed: %s",
605 status = ascent_xml (ascent_buffer);
610 } /* }}} int ascent_read */
612 void module_register (void)
614 plugin_register_config ("ascent", ascent_config, config_keys, config_keys_num);
615 plugin_register_init ("ascent", ascent_init);
616 plugin_register_read ("ascent", ascent_read);
617 } /* void module_register */
619 /* vim: set sw=2 sts=2 ts=8 et fdm=marker : */