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 verplant.org>
28 #include <arpa/inet.h>
29 #include <netinet/in.h>
30 #include <sys/types.h>
31 #include <sys/socket.h>
37 /* Default host and port */
38 #define DEFAULT_HOST "127.0.0.1"
39 #define DEFAULT_PORT "51234"
44 /* Server linked list structure */
45 typedef struct vserver_list_s
48 struct vserver_list_s *next;
50 static vserver_list_t *server_list = NULL;
53 static char *config_host = NULL;
54 static char *config_port = NULL;
56 static FILE *global_read_fh = NULL;
57 static FILE *global_write_fh = NULL;
60 static const char *config_keys[] =
66 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
71 static int tss2_add_vserver (int vserver_port)
74 * Adds a new vserver to the linked list
76 vserver_list_t *entry;
78 /* Check port range */
79 if ((vserver_port <= 0) || (vserver_port > 65535))
81 ERROR ("teamspeak2 plugin: VServer port is invalid: %i",
87 entry = (vserver_list_t *) malloc (sizeof (vserver_list_t));
90 ERROR ("teamspeak2 plugin: malloc failed.");
93 memset (entry, 0, sizeof (vserver_list_t));
96 entry->port = vserver_port;
99 if(server_list == NULL) {
100 /* Add the server as the first element */
104 vserver_list_t *prev;
106 /* Add the server to the end of the list */
108 while (prev->next != NULL)
113 INFO ("teamspeak2 plugin: Registered new vserver: %i", vserver_port);
116 } /* int tss2_add_vserver */
118 static void tss2_submit_gauge (const char *plugin_instance,
119 const char *type, const char *type_instance,
123 * Submits a gauge value to the collectd daemon
126 value_list_t vl = VALUE_LIST_INIT;
128 values[0].gauge = value;
132 vl.time = time (NULL);
133 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
134 sstrncpy (vl.plugin, "teamspeak2", sizeof (vl.plugin));
136 if (plugin_instance != NULL)
137 sstrncpy (vl.plugin_instance, plugin_instance,
138 sizeof (vl.plugin_instance));
140 if (type_instance != NULL)
141 sstrncpy (vl.type_instance, type_instance,
142 sizeof (vl.type_instance));
144 plugin_dispatch_values (type, &vl);
145 } /* void tss2_submit_gauge */
147 static void tss2_submit_io (const char *plugin_instance, const char *type,
148 counter_t rx, counter_t tx)
151 * Submits the io rx/tx tuple to the collectd daemon
154 value_list_t vl = VALUE_LIST_INIT;
156 values[0].counter = rx;
157 values[1].counter = tx;
161 vl.time = time (NULL);
162 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
163 sstrncpy (vl.plugin, "teamspeak2", sizeof (vl.plugin));
165 if (plugin_instance != NULL)
166 sstrncpy (vl.plugin_instance, plugin_instance,
167 sizeof (vl.plugin_instance));
169 plugin_dispatch_values (type, &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;
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 memset (&ai_hints, 0, sizeof (ai_hints));
221 ai_hints.ai_flags |= AI_ADDRCONFIG;
223 ai_hints.ai_family = AF_UNSPEC;
224 ai_hints.ai_socktype = SOCK_STREAM;
226 status = getaddrinfo ((config_host != NULL) ? config_host : DEFAULT_HOST,
227 (config_port != NULL) ? config_port : DEFAULT_PORT,
232 ERROR ("teamspeak2 plugin: getaddrinfo failed: %s",
233 gai_strerror (status));
237 /* Try all given hosts until we can connect to one */
238 for (ai_ptr = ai_head; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
241 sd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype,
242 ai_ptr->ai_protocol);
246 WARNING ("teamspeak2 plugin: socket failed: %s",
247 sstrerror (errno, errbuf, sizeof (errbuf)));
252 status = connect (sd, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
256 WARNING ("teamspeak2 plugin: connect failed: %s",
257 sstrerror (errno, errbuf, sizeof (errbuf)));
263 * Success, we can break. Don't need more than one connection
268 freeaddrinfo (ai_head);
270 /* Check if we really got connected */
274 /* Create file objects from sockets */
275 global_read_fh = fdopen (sd, "r");
276 if (global_read_fh == NULL)
279 ERROR ("teamspeak2 plugin: fdopen failed: %s",
280 sstrerror (errno, errbuf, sizeof (errbuf)));
285 global_write_fh = fdopen (sd, "w");
286 if (global_write_fh == NULL)
289 ERROR ("teamspeak2 plugin: fdopen failed: %s",
290 sstrerror (errno, errbuf, sizeof (errbuf)));
291 tss2_close_socket ();
295 { /* Check that the server correctly identifies itself. */
299 buffer_ptr = fgets (buffer, sizeof (buffer), global_read_fh);
300 buffer[sizeof (buffer) - 1] = 0;
302 if (memcmp ("[TS]\r\n", buffer, 6) != 0)
304 ERROR ("teamspeak2 plugin: Unexpected response when connecting "
305 "to server. Expected ``[TS]'', got ``%s''.",
307 tss2_close_socket ();
310 DEBUG ("teamspeak2 plugin: Server send correct banner, connected!");
313 /* Copy the new filehandles to the given pointers */
314 if (ret_read_fh != NULL)
315 *ret_read_fh = global_read_fh;
316 if (ret_write_fh != NULL)
317 *ret_write_fh = global_write_fh;
319 } /* int tss2_get_socket */
321 static int tss2_send_request (FILE *fh, const char *request)
324 * This function puts a request to the server socket
328 status = fputs (request, fh);
331 ERROR ("teamspeak2 plugin: fputs failed.");
332 tss2_close_socket ();
338 } /* int tss2_send_request */
340 static int tss2_receive_line (FILE *fh, char *buffer, int buffer_size)
343 * Receive a single line from the given file object
348 * fgets is blocking but much easier then doing anything else
349 * TODO: Non-blocking Version would be safer
351 temp = fgets (buffer, buffer_size, fh);
355 ERROR ("teamspeak2 plugin: fgets failed: %s",
356 sstrerror (errno, errbuf, sizeof(errbuf)));
357 tss2_close_socket ();
361 buffer[buffer_size - 1] = 0;
363 } /* int tss2_receive_line */
365 static int tss2_select_vserver (FILE *read_fh, FILE *write_fh, vserver_list_t *vserver)
368 * Tell the server to select the given vserver
375 snprintf (command, sizeof (command), "sel %i\r\n", vserver->port);
376 command[sizeof (command) - 1] = 0;
378 status = tss2_send_request (write_fh, command);
381 ERROR ("teamspeak2 plugin: tss2_send_request (%s) failed.", command);
386 status = tss2_receive_line (read_fh, response, sizeof (response));
389 ERROR ("teamspeak2 plugin: tss2_receive_line failed.");
392 response[sizeof (response) - 1] = 0;
395 if ((strncasecmp ("OK", response, 2) == 0)
396 && ((response[2] == 0)
397 || (response[2] == '\n')
398 || (response[2] == '\r')))
401 ERROR ("teamspeak2 plugin: Command ``%s'' failed. "
402 "Response received from server was: ``%s''.",
405 } /* int tss2_select_vserver */
407 static int tss2_vserver_gapl (FILE *read_fh, FILE *write_fh,
408 vserver_list_t *vserver, gauge_t *ret_value)
411 * Reads the vserver's average packet loss and submits it to collectd.
412 * Be sure to run the tss2_read_vserver function before calling this so
413 * the vserver is selected correctly.
415 gauge_t packet_loss = NAN;
418 status = tss2_send_request (write_fh, "gapl\r\n");
421 ERROR("teamspeak2 plugin: tss2_send_request (gapl) failed.");
431 status = tss2_receive_line (read_fh, buffer, sizeof (buffer));
434 /* Set to NULL just to make sure noone uses these FHs anymore. */
437 ERROR ("teamspeak2 plugin: tss2_receive_line failed.");
440 buffer[sizeof (buffer) - 1] = 0;
442 if (strncmp ("average_packet_loss=", buffer,
443 strlen ("average_packet_loss=")) == 0)
445 /* Got average packet loss, now interpret it */
447 /* Replace , with . */
460 packet_loss = strtod (value, &endptr);
464 WARNING ("teamspeak2 plugin: Could not read average package "
465 "loss from string: %s", buffer);
469 else if (strncasecmp ("OK", buffer, 2) == 0)
473 else if (strncasecmp ("ERROR", buffer, 5) == 0)
475 ERROR ("teamspeak2 plugin: Server returned an error: %s", buffer);
480 WARNING ("teamspeak2 plugin: Server returned unexpected string: %s",
485 *ret_value = packet_loss;
487 } /* int tss2_vserver_gapl */
489 static int tss2_read_vserver (vserver_list_t *vserver)
492 * Poll information for the given vserver and submit it to collect.
493 * If vserver is NULL the global server information will be queried.
498 gauge_t channels = NAN;
499 gauge_t servers = NAN;
500 counter_t rx_octets = 0;
501 counter_t tx_octets = 0;
502 counter_t rx_packets = 0;
503 counter_t tx_packets = 0;
504 gauge_t packet_loss = NAN;
507 char plugin_instance[DATA_MAX_NAME_LEN];
512 /* Get the send/receive sockets */
513 status = tss2_get_socket (&read_fh, &write_fh);
516 ERROR ("teamspeak2 plugin: tss2_get_socket failed.");
522 /* Request global information */
523 memset (plugin_instance, 0, sizeof (plugin_instance));
525 status = tss2_send_request (write_fh, "gi\r\n");
529 /* Request server information */
530 snprintf (plugin_instance, sizeof (plugin_instance), "vserver%i",
532 plugin_instance[sizeof (plugin_instance) - 1] = 0;
534 /* Select the server */
535 status = tss2_select_vserver (read_fh, write_fh, vserver);
539 status = tss2_send_request (write_fh, "si\r\n");
544 ERROR ("teamspeak2 plugin: tss2_send_request failed.");
548 /* Loop until break */
556 /* Read one line of the server's answer */
557 status = tss2_receive_line (read_fh, buffer, sizeof (buffer));
560 /* Set to NULL just to make sure noone uses these FHs anymore. */
563 ERROR ("teamspeak2 plugin: tss2_receive_line failed.");
567 if (strncasecmp ("ERROR", buffer, 5) == 0)
569 ERROR ("teamspeak2 plugin: Server returned an error: %s",
573 else if (strncasecmp ("OK", buffer, 2) == 0)
578 /* Split line into key and value */
579 key = strchr (buffer, '_');
582 DEBUG ("teamspeak2 plugin: Cannot parse line: %s", buffer);
587 /* Evaluate assignment */
588 value = strchr (key, '=');
591 DEBUG ("teamspeak2 plugin: Cannot parse line: %s", buffer);
597 /* Check for known key and save the given value */
598 /* global info: users_online,
599 * server info: currentusers. */
600 if ((strcmp ("currentusers", key) == 0)
601 || (strcmp ("users_online", key) == 0))
603 users = strtod (value, &endptr);
607 /* global info: channels,
608 * server info: currentchannels. */
609 else if ((strcmp ("currentchannels", key) == 0)
610 || (strcmp ("channels", key) == 0))
612 channels = strtod (value, &endptr);
617 else if (strcmp ("servers", key) == 0)
619 servers = strtod (value, &endptr);
623 else if (strcmp ("bytesreceived", key) == 0)
625 rx_octets = strtoll (value, &endptr, 0);
629 else if (strcmp ("bytessend", key) == 0)
631 tx_octets = strtoll (value, &endptr, 0);
635 else if (strcmp ("packetsreceived", key) == 0)
637 rx_packets = strtoll (value, &endptr, 0);
641 else if (strcmp ("packetssend", key) == 0)
643 tx_packets = strtoll (value, &endptr, 0);
647 else if ((strncmp ("allow_codec_", key, strlen ("allow_codec_")) == 0)
648 || (strncmp ("bwinlast", key, strlen ("bwinlast")) == 0)
649 || (strncmp ("bwoutlast", key, strlen ("bwoutlast")) == 0)
650 || (strncmp ("webpost_", key, strlen ("webpost_")) == 0)
651 || (strcmp ("adminemail", key) == 0)
652 || (strcmp ("clan_server", key) == 0)
653 || (strcmp ("countrynumber", key) == 0)
654 || (strcmp ("id", key) == 0)
655 || (strcmp ("ispname", key) == 0)
656 || (strcmp ("linkurl", key) == 0)
657 || (strcmp ("maxusers", key) == 0)
658 || (strcmp ("name", key) == 0)
659 || (strcmp ("password", key) == 0)
660 || (strcmp ("platform", key) == 0)
661 || (strcmp ("server_platform", key) == 0)
662 || (strcmp ("server_uptime", key) == 0)
663 || (strcmp ("server_version", key) == 0)
664 || (strcmp ("udpport", key) == 0)
665 || (strcmp ("uptime", key) == 0)
666 || (strcmp ("users_maximal", key) == 0)
667 || (strcmp ("welcomemessage", key) == 0))
671 INFO ("teamspeak2 plugin: Unknown key-value-pair: "
672 "key = %s; value = %s;", key, value);
676 /* Collect vserver packet loss rates only if the loop above did not exit
678 if ((status == 0) && (vserver != NULL))
680 status = tss2_vserver_gapl (read_fh, write_fh, vserver, &packet_loss);
687 WARNING ("teamspeak2 plugin: Reading package loss "
688 "for vserver %i failed.", vserver->port);
692 if ((valid & 0x01) == 0x01)
693 tss2_submit_gauge (plugin_instance, "users", NULL, users);
695 if ((valid & 0x06) == 0x06)
696 tss2_submit_io (plugin_instance, "io_octets", rx_octets, tx_octets);
698 if ((valid & 0x18) == 0x18)
699 tss2_submit_io (plugin_instance, "io_packets", rx_packets, tx_packets);
701 if ((valid & 0x20) == 0x20)
702 tss2_submit_gauge (plugin_instance, "percent", "packet_loss", packet_loss);
704 if ((valid & 0x40) == 0x40)
705 tss2_submit_gauge (plugin_instance, "gauge", "channels", channels);
707 if ((valid & 0x80) == 0x80)
708 tss2_submit_gauge (plugin_instance, "gauge", "servers", servers);
713 } /* int tss2_read_vserver */
715 static int tss2_config (const char *key, const char *value)
718 * Interpret configuration values
720 if (strcasecmp ("Host", key) == 0)
724 temp = strdup (value);
727 ERROR("teamspeak2 plugin: strdup failed.");
733 else if (strcasecmp ("Port", key) == 0)
737 temp = strdup (value);
740 ERROR("teamspeak2 plugin: strdup failed.");
746 else if (strcasecmp ("Server", key) == 0)
748 /* Server variable found */
751 status = tss2_add_vserver (atoi (value));
757 /* Unknown variable found */
762 } /* int tss2_config */
764 static int tss2_read (void)
767 * Poll function which collects global and vserver information
768 * and submits it to collectd
770 vserver_list_t *vserver;
774 /* Handle global server variables */
775 status = tss2_read_vserver (NULL);
782 WARNING ("teamspeak2 plugin: Reading global server variables failed.");
785 /* Handle vservers */
786 for (vserver = server_list; vserver != NULL; vserver = vserver->next)
788 status = tss2_read_vserver (vserver);
795 WARNING ("teamspeak2 plugin: Reading statistics "
796 "for vserver %i failed.", vserver->port);
804 } /* int tss2_read */
806 static int tss2_shutdown(void)
811 vserver_list_t *entry;
813 tss2_close_socket ();
817 while (entry != NULL)
819 vserver_list_t *next;
826 /* Get rid of the configuration */
831 } /* int tss2_shutdown */
833 void module_register(void)
836 * Mandatory module_register function
838 plugin_register_config ("teamspeak2", tss2_config,
839 config_keys, config_keys_num);
840 plugin_register_read ("teamspeak2", tss2_read);
841 plugin_register_shutdown ("teamspeak2", tss2_shutdown);
842 } /* void module_register */
844 /* vim: set sw=4 ts=4 : */