2 * collectd - src/teamspeak2.c
3 * Copyright (C) 2008 Stefan Hacker
4 * Copyright (C) 2008 Florian Forster
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; only version 2 of the License is applicable.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * Stefan Hacker <d0t at dbclan dot de>
21 * Florian Forster <octo at collectd.org>
28 #include <netinet/in.h>
29 #include <arpa/inet.h>
30 #include <sys/types.h>
36 /* Default host and port */
37 #define DEFAULT_HOST "127.0.0.1"
38 #define DEFAULT_PORT "51234"
43 /* Server linked list structure */
44 typedef struct vserver_list_s
47 struct vserver_list_s *next;
49 static vserver_list_t *server_list = NULL;
52 static char *config_host = NULL;
53 static char *config_port = NULL;
55 static FILE *global_read_fh = NULL;
56 static FILE *global_write_fh = NULL;
59 static const char *config_keys[] =
65 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
70 static int tss2_add_vserver (int vserver_port)
73 * Adds a new vserver to the linked list
75 vserver_list_t *entry;
77 /* Check port range */
78 if ((vserver_port <= 0) || (vserver_port > 65535))
80 ERROR ("teamspeak2 plugin: VServer port is invalid: %i",
86 entry = calloc (1, sizeof (*entry));
89 ERROR ("teamspeak2 plugin: calloc failed.");
94 entry->port = vserver_port;
97 if(server_list == NULL) {
98 /* Add the server as the first element */
102 vserver_list_t *prev;
104 /* Add the server to the end of the list */
106 while (prev->next != NULL)
111 INFO ("teamspeak2 plugin: Registered new vserver: %i", vserver_port);
114 } /* int tss2_add_vserver */
116 static void tss2_submit_gauge (const char *plugin_instance,
117 const char *type, const char *type_instance,
121 * Submits a gauge value to the collectd daemon
124 value_list_t vl = VALUE_LIST_INIT;
126 values[0].gauge = value;
130 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
131 sstrncpy (vl.plugin, "teamspeak2", 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,
141 sizeof (vl.type_instance));
143 plugin_dispatch_values (&vl);
144 } /* void tss2_submit_gauge */
146 static void tss2_submit_io (const char *plugin_instance, const char *type,
147 derive_t rx, derive_t tx)
150 * Submits the io rx/tx tuple to the collectd daemon
153 value_list_t vl = VALUE_LIST_INIT;
155 values[0].derive = rx;
156 values[1].derive = tx;
160 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
161 sstrncpy (vl.plugin, "teamspeak2", sizeof (vl.plugin));
163 if (plugin_instance != NULL)
164 sstrncpy (vl.plugin_instance, plugin_instance,
165 sizeof (vl.plugin_instance));
167 sstrncpy (vl.type, type, sizeof (vl.type));
169 plugin_dispatch_values (&vl);
170 } /* void tss2_submit_gauge */
172 static void tss2_close_socket (void)
177 if (global_write_fh != NULL)
179 fputs ("quit\r\n", global_write_fh);
182 if (global_read_fh != NULL)
184 fclose (global_read_fh);
185 global_read_fh = NULL;
188 if (global_write_fh != NULL)
190 fclose (global_write_fh);
191 global_write_fh = NULL;
193 } /* void tss2_close_socket */
195 static int tss2_get_socket (FILE **ret_read_fh, FILE **ret_write_fh)
198 * Returns connected file objects or establishes the connection
199 * if it's not already present
201 struct addrinfo ai_hints = { 0 };
202 struct addrinfo *ai_head;
203 struct addrinfo *ai_ptr;
207 /* Check if we already got opened connections */
208 if ((global_read_fh != NULL) && (global_write_fh != NULL))
210 /* If so, use them */
211 if (ret_read_fh != NULL)
212 *ret_read_fh = global_read_fh;
213 if (ret_write_fh != NULL)
214 *ret_write_fh = global_write_fh;
218 /* Get all addrs for this hostname */
219 ai_hints.ai_flags = AI_ADDRCONFIG;
220 ai_hints.ai_family = AF_UNSPEC;
221 ai_hints.ai_socktype = SOCK_STREAM;
223 status = getaddrinfo ((config_host != NULL) ? config_host : DEFAULT_HOST,
224 (config_port != NULL) ? config_port : DEFAULT_PORT,
229 ERROR ("teamspeak2 plugin: getaddrinfo failed: %s",
230 gai_strerror (status));
234 /* Try all given hosts until we can connect to one */
235 for (ai_ptr = ai_head; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
238 sd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype,
239 ai_ptr->ai_protocol);
243 WARNING ("teamspeak2 plugin: socket failed: %s",
244 sstrerror (errno, errbuf, sizeof (errbuf)));
249 status = connect (sd, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
253 WARNING ("teamspeak2 plugin: connect failed: %s",
254 sstrerror (errno, errbuf, sizeof (errbuf)));
261 * Success, we can break. Don't need more than one connection
266 freeaddrinfo (ai_head);
268 /* Check if we really got connected */
272 /* Create file objects from sockets */
273 global_read_fh = fdopen (sd, "r");
274 if (global_read_fh == NULL)
277 ERROR ("teamspeak2 plugin: fdopen failed: %s",
278 sstrerror (errno, errbuf, sizeof (errbuf)));
283 global_write_fh = fdopen (sd, "w");
284 if (global_write_fh == NULL)
287 ERROR ("teamspeak2 plugin: fdopen failed: %s",
288 sstrerror (errno, errbuf, sizeof (errbuf)));
289 tss2_close_socket ();
293 { /* Check that the server correctly identifies itself. */
297 buffer_ptr = fgets (buffer, sizeof (buffer), global_read_fh);
298 if (buffer_ptr == NULL)
300 WARNING ("teamspeak2 plugin: Unexpected EOF received "
301 "from remote host %s:%s.",
302 config_host ? config_host : DEFAULT_HOST,
303 config_port ? config_port : DEFAULT_PORT);
305 buffer[sizeof (buffer) - 1] = 0;
307 if (memcmp ("[TS]\r\n", buffer, 6) != 0)
309 ERROR ("teamspeak2 plugin: Unexpected response when connecting "
310 "to server. Expected ``[TS]'', got ``%s''.",
312 tss2_close_socket ();
315 DEBUG ("teamspeak2 plugin: Server send correct banner, connected!");
318 /* Copy the new filehandles to the given pointers */
319 if (ret_read_fh != NULL)
320 *ret_read_fh = global_read_fh;
321 if (ret_write_fh != NULL)
322 *ret_write_fh = global_write_fh;
324 } /* int tss2_get_socket */
326 static int tss2_send_request (FILE *fh, const char *request)
329 * This function puts a request to the server socket
333 status = fputs (request, fh);
336 ERROR ("teamspeak2 plugin: fputs failed.");
337 tss2_close_socket ();
343 } /* int tss2_send_request */
345 static int tss2_receive_line (FILE *fh, char *buffer, int buffer_size)
348 * Receive a single line from the given file object
353 * fgets is blocking but much easier then doing anything else
354 * TODO: Non-blocking Version would be safer
356 temp = fgets (buffer, buffer_size, fh);
360 ERROR ("teamspeak2 plugin: fgets failed: %s",
361 sstrerror (errno, errbuf, sizeof(errbuf)));
362 tss2_close_socket ();
366 buffer[buffer_size - 1] = 0;
368 } /* int tss2_receive_line */
370 static int tss2_select_vserver (FILE *read_fh, FILE *write_fh, vserver_list_t *vserver)
373 * Tell the server to select the given vserver
380 ssnprintf (command, sizeof (command), "sel %i\r\n", vserver->port);
382 status = tss2_send_request (write_fh, command);
385 ERROR ("teamspeak2 plugin: tss2_send_request (%s) failed.", command);
390 status = tss2_receive_line (read_fh, response, sizeof (response));
393 ERROR ("teamspeak2 plugin: tss2_receive_line failed.");
396 response[sizeof (response) - 1] = 0;
399 if ((strncasecmp ("OK", response, 2) == 0)
400 && ((response[2] == 0)
401 || (response[2] == '\n')
402 || (response[2] == '\r')))
405 ERROR ("teamspeak2 plugin: Command ``%s'' failed. "
406 "Response received from server was: ``%s''.",
409 } /* int tss2_select_vserver */
411 static int tss2_vserver_gapl (FILE *read_fh, FILE *write_fh,
415 * Reads the vserver's average packet loss and submits it to collectd.
416 * Be sure to run the tss2_read_vserver function before calling this so
417 * the vserver is selected correctly.
419 gauge_t packet_loss = NAN;
422 status = tss2_send_request (write_fh, "gapl\r\n");
425 ERROR("teamspeak2 plugin: tss2_send_request (gapl) failed.");
435 status = tss2_receive_line (read_fh, buffer, sizeof (buffer));
438 /* Set to NULL just to make sure no one uses these FHs anymore. */
441 ERROR ("teamspeak2 plugin: tss2_receive_line failed.");
444 buffer[sizeof (buffer) - 1] = 0;
446 if (strncmp ("average_packet_loss=", buffer,
447 strlen ("average_packet_loss=")) == 0)
449 /* Got average packet loss, now interpret it */
451 /* Replace , with . */
464 packet_loss = strtod (value, &endptr);
468 WARNING ("teamspeak2 plugin: Could not read average package "
469 "loss from string: %s", buffer);
473 else if (strncasecmp ("OK", buffer, 2) == 0)
477 else if (strncasecmp ("ERROR", buffer, 5) == 0)
479 ERROR ("teamspeak2 plugin: Server returned an error: %s", buffer);
484 WARNING ("teamspeak2 plugin: Server returned unexpected string: %s",
489 *ret_value = packet_loss;
491 } /* int tss2_vserver_gapl */
493 static int tss2_read_vserver (vserver_list_t *vserver)
496 * Poll information for the given vserver and submit it to collect.
497 * If vserver is NULL the global server information will be queried.
502 gauge_t channels = NAN;
503 gauge_t servers = NAN;
504 derive_t rx_octets = 0;
505 derive_t tx_octets = 0;
506 derive_t rx_packets = 0;
507 derive_t tx_packets = 0;
508 gauge_t packet_loss = NAN;
511 char plugin_instance[DATA_MAX_NAME_LEN] = { 0 };
516 /* Get the send/receive sockets */
517 status = tss2_get_socket (&read_fh, &write_fh);
520 ERROR ("teamspeak2 plugin: tss2_get_socket failed.");
526 /* Request global information */
527 status = tss2_send_request (write_fh, "gi\r\n");
531 /* Request server information */
532 ssnprintf (plugin_instance, sizeof (plugin_instance), "vserver%i",
535 /* Select the server */
536 status = tss2_select_vserver (read_fh, write_fh, vserver);
540 status = tss2_send_request (write_fh, "si\r\n");
545 ERROR ("teamspeak2 plugin: tss2_send_request failed.");
549 /* Loop until break */
557 /* Read one line of the server's answer */
558 status = tss2_receive_line (read_fh, buffer, sizeof (buffer));
561 /* Set to NULL just to make sure no one uses these FHs anymore. */
564 ERROR ("teamspeak2 plugin: tss2_receive_line failed.");
568 if (strncasecmp ("ERROR", buffer, 5) == 0)
570 ERROR ("teamspeak2 plugin: Server returned an error: %s",
574 else if (strncasecmp ("OK", buffer, 2) == 0)
579 /* Split line into key and value */
580 key = strchr (buffer, '_');
583 DEBUG ("teamspeak2 plugin: Cannot parse line: %s", buffer);
588 /* Evaluate assignment */
589 value = strchr (key, '=');
592 DEBUG ("teamspeak2 plugin: Cannot parse line: %s", buffer);
598 /* Check for known key and save the given value */
599 /* global info: users_online,
600 * server info: currentusers. */
601 if ((strcmp ("currentusers", key) == 0)
602 || (strcmp ("users_online", key) == 0))
604 users = strtod (value, &endptr);
608 /* global info: channels,
609 * server info: currentchannels. */
610 else if ((strcmp ("currentchannels", key) == 0)
611 || (strcmp ("channels", key) == 0))
613 channels = strtod (value, &endptr);
618 else if (strcmp ("servers", key) == 0)
620 servers = strtod (value, &endptr);
624 else if (strcmp ("bytesreceived", key) == 0)
626 rx_octets = strtoll (value, &endptr, 0);
630 else if (strcmp ("bytessend", key) == 0)
632 tx_octets = strtoll (value, &endptr, 0);
636 else if (strcmp ("packetsreceived", key) == 0)
638 rx_packets = strtoll (value, &endptr, 0);
642 else if (strcmp ("packetssend", key) == 0)
644 tx_packets = strtoll (value, &endptr, 0);
648 else if ((strncmp ("allow_codec_", key, strlen ("allow_codec_")) == 0)
649 || (strncmp ("bwinlast", key, strlen ("bwinlast")) == 0)
650 || (strncmp ("bwoutlast", key, strlen ("bwoutlast")) == 0)
651 || (strncmp ("webpost_", key, strlen ("webpost_")) == 0)
652 || (strcmp ("adminemail", key) == 0)
653 || (strcmp ("clan_server", key) == 0)
654 || (strcmp ("countrynumber", key) == 0)
655 || (strcmp ("id", key) == 0)
656 || (strcmp ("ispname", key) == 0)
657 || (strcmp ("linkurl", key) == 0)
658 || (strcmp ("maxusers", key) == 0)
659 || (strcmp ("name", key) == 0)
660 || (strcmp ("password", key) == 0)
661 || (strcmp ("platform", key) == 0)
662 || (strcmp ("server_platform", key) == 0)
663 || (strcmp ("server_uptime", key) == 0)
664 || (strcmp ("server_version", key) == 0)
665 || (strcmp ("udpport", key) == 0)
666 || (strcmp ("uptime", key) == 0)
667 || (strcmp ("users_maximal", key) == 0)
668 || (strcmp ("welcomemessage", key) == 0))
672 INFO ("teamspeak2 plugin: Unknown key-value-pair: "
673 "key = %s; value = %s;", key, value);
677 /* Collect vserver packet loss rates only if the loop above did not exit
679 if ((status == 0) && (vserver != NULL))
681 status = tss2_vserver_gapl (read_fh, write_fh, &packet_loss);
688 WARNING ("teamspeak2 plugin: Reading package loss "
689 "for vserver %i failed.", vserver->port);
693 if ((valid & 0x01) == 0x01)
694 tss2_submit_gauge (plugin_instance, "users", NULL, users);
696 if ((valid & 0x06) == 0x06)
697 tss2_submit_io (plugin_instance, "io_octets", rx_octets, tx_octets);
699 if ((valid & 0x18) == 0x18)
700 tss2_submit_io (plugin_instance, "io_packets", rx_packets, tx_packets);
702 if ((valid & 0x20) == 0x20)
703 tss2_submit_gauge (plugin_instance, "percent", "packet_loss", packet_loss);
705 if ((valid & 0x40) == 0x40)
706 tss2_submit_gauge (plugin_instance, "gauge", "channels", channels);
708 if ((valid & 0x80) == 0x80)
709 tss2_submit_gauge (plugin_instance, "gauge", "servers", servers);
714 } /* int tss2_read_vserver */
716 static int tss2_config (const char *key, const char *value)
719 * Interpret configuration values
721 if (strcasecmp ("Host", key) == 0)
725 temp = strdup (value);
728 ERROR("teamspeak2 plugin: strdup failed.");
734 else if (strcasecmp ("Port", key) == 0)
738 temp = strdup (value);
741 ERROR("teamspeak2 plugin: strdup failed.");
747 else if (strcasecmp ("Server", key) == 0)
749 /* Server variable found */
752 status = tss2_add_vserver (atoi (value));
758 /* Unknown variable found */
763 } /* int tss2_config */
765 static int tss2_read (void)
768 * Poll function which collects global and vserver information
769 * and submits it to collectd
771 vserver_list_t *vserver;
775 /* Handle global server variables */
776 status = tss2_read_vserver (NULL);
783 WARNING ("teamspeak2 plugin: Reading global server variables failed.");
786 /* Handle vservers */
787 for (vserver = server_list; vserver != NULL; vserver = vserver->next)
789 status = tss2_read_vserver (vserver);
796 WARNING ("teamspeak2 plugin: Reading statistics "
797 "for vserver %i failed.", vserver->port);
805 } /* int tss2_read */
807 static int tss2_shutdown(void)
812 vserver_list_t *entry;
814 tss2_close_socket ();
818 while (entry != NULL)
820 vserver_list_t *next;
827 /* Get rid of the configuration */
832 } /* int tss2_shutdown */
834 void module_register(void)
837 * Mandatory module_register function
839 plugin_register_config ("teamspeak2", tss2_config,
840 config_keys, config_keys_num);
841 plugin_register_read ("teamspeak2", tss2_read);
842 plugin_register_shutdown ("teamspeak2", tss2_shutdown);
843 } /* void module_register */
845 /* vim: set sw=4 ts=4 : */