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>
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 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
133 sstrncpy (vl.plugin, "teamspeak2", sizeof (vl.plugin));
135 if (plugin_instance != NULL)
136 sstrncpy (vl.plugin_instance, plugin_instance,
137 sizeof (vl.plugin_instance));
139 sstrncpy (vl.type, type, sizeof (vl.type));
141 if (type_instance != NULL)
142 sstrncpy (vl.type_instance, type_instance,
143 sizeof (vl.type_instance));
145 plugin_dispatch_values (&vl);
146 } /* void tss2_submit_gauge */
148 static void tss2_submit_io (const char *plugin_instance, const char *type,
149 derive_t rx, derive_t tx)
152 * Submits the io rx/tx tuple to the collectd daemon
155 value_list_t vl = VALUE_LIST_INIT;
157 values[0].derive = rx;
158 values[1].derive = tx;
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 sstrncpy (vl.type, type, sizeof (vl.type));
171 plugin_dispatch_values (&vl);
172 } /* void tss2_submit_gauge */
174 static void tss2_close_socket (void)
179 if (global_write_fh != NULL)
181 fputs ("quit\r\n", global_write_fh);
184 if (global_read_fh != NULL)
186 fclose (global_read_fh);
187 global_read_fh = NULL;
190 if (global_write_fh != NULL)
192 fclose (global_write_fh);
193 global_write_fh = NULL;
195 } /* void tss2_close_socket */
197 static int tss2_get_socket (FILE **ret_read_fh, FILE **ret_write_fh)
200 * Returns connected file objects or establishes the connection
201 * if it's not already present
203 struct addrinfo ai_hints;
204 struct addrinfo *ai_head;
205 struct addrinfo *ai_ptr;
209 /* Check if we already got opened connections */
210 if ((global_read_fh != NULL) && (global_write_fh != NULL))
212 /* If so, use them */
213 if (ret_read_fh != NULL)
214 *ret_read_fh = global_read_fh;
215 if (ret_write_fh != NULL)
216 *ret_write_fh = global_write_fh;
220 /* Get all addrs for this hostname */
221 memset (&ai_hints, 0, sizeof (ai_hints));
223 ai_hints.ai_flags |= AI_ADDRCONFIG;
225 ai_hints.ai_family = AF_UNSPEC;
226 ai_hints.ai_socktype = SOCK_STREAM;
228 status = getaddrinfo ((config_host != NULL) ? config_host : DEFAULT_HOST,
229 (config_port != NULL) ? config_port : DEFAULT_PORT,
234 ERROR ("teamspeak2 plugin: getaddrinfo failed: %s",
235 gai_strerror (status));
239 /* Try all given hosts until we can connect to one */
240 for (ai_ptr = ai_head; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
243 sd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype,
244 ai_ptr->ai_protocol);
248 WARNING ("teamspeak2 plugin: socket failed: %s",
249 sstrerror (errno, errbuf, sizeof (errbuf)));
254 status = connect (sd, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
258 WARNING ("teamspeak2 plugin: connect failed: %s",
259 sstrerror (errno, errbuf, sizeof (errbuf)));
266 * Success, we can break. Don't need more than one connection
271 freeaddrinfo (ai_head);
273 /* Check if we really got connected */
277 /* Create file objects from sockets */
278 global_read_fh = fdopen (sd, "r");
279 if (global_read_fh == NULL)
282 ERROR ("teamspeak2 plugin: fdopen failed: %s",
283 sstrerror (errno, errbuf, sizeof (errbuf)));
288 global_write_fh = fdopen (sd, "w");
289 if (global_write_fh == NULL)
292 ERROR ("teamspeak2 plugin: fdopen failed: %s",
293 sstrerror (errno, errbuf, sizeof (errbuf)));
294 tss2_close_socket ();
298 { /* Check that the server correctly identifies itself. */
302 buffer_ptr = fgets (buffer, sizeof (buffer), global_read_fh);
303 if (buffer_ptr == NULL)
305 WARNING ("teamspeak2 plugin: Unexpected EOF received "
306 "from remote host %s:%s.",
307 config_host ? config_host : DEFAULT_HOST,
308 config_port ? config_port : DEFAULT_PORT);
310 buffer[sizeof (buffer) - 1] = 0;
312 if (memcmp ("[TS]\r\n", buffer, 6) != 0)
314 ERROR ("teamspeak2 plugin: Unexpected response when connecting "
315 "to server. Expected ``[TS]'', got ``%s''.",
317 tss2_close_socket ();
320 DEBUG ("teamspeak2 plugin: Server send correct banner, connected!");
323 /* Copy the new filehandles to the given pointers */
324 if (ret_read_fh != NULL)
325 *ret_read_fh = global_read_fh;
326 if (ret_write_fh != NULL)
327 *ret_write_fh = global_write_fh;
329 } /* int tss2_get_socket */
331 static int tss2_send_request (FILE *fh, const char *request)
334 * This function puts a request to the server socket
338 status = fputs (request, fh);
341 ERROR ("teamspeak2 plugin: fputs failed.");
342 tss2_close_socket ();
348 } /* int tss2_send_request */
350 static int tss2_receive_line (FILE *fh, char *buffer, int buffer_size)
353 * Receive a single line from the given file object
358 * fgets is blocking but much easier then doing anything else
359 * TODO: Non-blocking Version would be safer
361 temp = fgets (buffer, buffer_size, fh);
365 ERROR ("teamspeak2 plugin: fgets failed: %s",
366 sstrerror (errno, errbuf, sizeof(errbuf)));
367 tss2_close_socket ();
371 buffer[buffer_size - 1] = 0;
373 } /* int tss2_receive_line */
375 static int tss2_select_vserver (FILE *read_fh, FILE *write_fh, vserver_list_t *vserver)
378 * Tell the server to select the given vserver
385 ssnprintf (command, sizeof (command), "sel %i\r\n", vserver->port);
387 status = tss2_send_request (write_fh, command);
390 ERROR ("teamspeak2 plugin: tss2_send_request (%s) failed.", command);
395 status = tss2_receive_line (read_fh, response, sizeof (response));
398 ERROR ("teamspeak2 plugin: tss2_receive_line failed.");
401 response[sizeof (response) - 1] = 0;
404 if ((strncasecmp ("OK", response, 2) == 0)
405 && ((response[2] == 0)
406 || (response[2] == '\n')
407 || (response[2] == '\r')))
410 ERROR ("teamspeak2 plugin: Command ``%s'' failed. "
411 "Response received from server was: ``%s''.",
414 } /* int tss2_select_vserver */
416 static int tss2_vserver_gapl (FILE *read_fh, FILE *write_fh,
420 * Reads the vserver's average packet loss and submits it to collectd.
421 * Be sure to run the tss2_read_vserver function before calling this so
422 * the vserver is selected correctly.
424 gauge_t packet_loss = NAN;
427 status = tss2_send_request (write_fh, "gapl\r\n");
430 ERROR("teamspeak2 plugin: tss2_send_request (gapl) failed.");
440 status = tss2_receive_line (read_fh, buffer, sizeof (buffer));
443 /* Set to NULL just to make sure no one uses these FHs anymore. */
446 ERROR ("teamspeak2 plugin: tss2_receive_line failed.");
449 buffer[sizeof (buffer) - 1] = 0;
451 if (strncmp ("average_packet_loss=", buffer,
452 strlen ("average_packet_loss=")) == 0)
454 /* Got average packet loss, now interpret it */
456 /* Replace , with . */
469 packet_loss = strtod (value, &endptr);
473 WARNING ("teamspeak2 plugin: Could not read average package "
474 "loss from string: %s", buffer);
478 else if (strncasecmp ("OK", buffer, 2) == 0)
482 else if (strncasecmp ("ERROR", buffer, 5) == 0)
484 ERROR ("teamspeak2 plugin: Server returned an error: %s", buffer);
489 WARNING ("teamspeak2 plugin: Server returned unexpected string: %s",
494 *ret_value = packet_loss;
496 } /* int tss2_vserver_gapl */
498 static int tss2_read_vserver (vserver_list_t *vserver)
501 * Poll information for the given vserver and submit it to collect.
502 * If vserver is NULL the global server information will be queried.
507 gauge_t channels = NAN;
508 gauge_t servers = NAN;
509 derive_t rx_octets = 0;
510 derive_t tx_octets = 0;
511 derive_t rx_packets = 0;
512 derive_t tx_packets = 0;
513 gauge_t packet_loss = NAN;
516 char plugin_instance[DATA_MAX_NAME_LEN];
521 /* Get the send/receive sockets */
522 status = tss2_get_socket (&read_fh, &write_fh);
525 ERROR ("teamspeak2 plugin: tss2_get_socket failed.");
531 /* Request global information */
532 memset (plugin_instance, 0, sizeof (plugin_instance));
534 status = tss2_send_request (write_fh, "gi\r\n");
538 /* Request server information */
539 ssnprintf (plugin_instance, sizeof (plugin_instance), "vserver%i",
542 /* Select the server */
543 status = tss2_select_vserver (read_fh, write_fh, vserver);
547 status = tss2_send_request (write_fh, "si\r\n");
552 ERROR ("teamspeak2 plugin: tss2_send_request failed.");
556 /* Loop until break */
564 /* Read one line of the server's answer */
565 status = tss2_receive_line (read_fh, buffer, sizeof (buffer));
568 /* Set to NULL just to make sure no one uses these FHs anymore. */
571 ERROR ("teamspeak2 plugin: tss2_receive_line failed.");
575 if (strncasecmp ("ERROR", buffer, 5) == 0)
577 ERROR ("teamspeak2 plugin: Server returned an error: %s",
581 else if (strncasecmp ("OK", buffer, 2) == 0)
586 /* Split line into key and value */
587 key = strchr (buffer, '_');
590 DEBUG ("teamspeak2 plugin: Cannot parse line: %s", buffer);
595 /* Evaluate assignment */
596 value = strchr (key, '=');
599 DEBUG ("teamspeak2 plugin: Cannot parse line: %s", buffer);
605 /* Check for known key and save the given value */
606 /* global info: users_online,
607 * server info: currentusers. */
608 if ((strcmp ("currentusers", key) == 0)
609 || (strcmp ("users_online", key) == 0))
611 users = strtod (value, &endptr);
615 /* global info: channels,
616 * server info: currentchannels. */
617 else if ((strcmp ("currentchannels", key) == 0)
618 || (strcmp ("channels", key) == 0))
620 channels = strtod (value, &endptr);
625 else if (strcmp ("servers", key) == 0)
627 servers = strtod (value, &endptr);
631 else if (strcmp ("bytesreceived", key) == 0)
633 rx_octets = strtoll (value, &endptr, 0);
637 else if (strcmp ("bytessend", key) == 0)
639 tx_octets = strtoll (value, &endptr, 0);
643 else if (strcmp ("packetsreceived", key) == 0)
645 rx_packets = strtoll (value, &endptr, 0);
649 else if (strcmp ("packetssend", key) == 0)
651 tx_packets = strtoll (value, &endptr, 0);
655 else if ((strncmp ("allow_codec_", key, strlen ("allow_codec_")) == 0)
656 || (strncmp ("bwinlast", key, strlen ("bwinlast")) == 0)
657 || (strncmp ("bwoutlast", key, strlen ("bwoutlast")) == 0)
658 || (strncmp ("webpost_", key, strlen ("webpost_")) == 0)
659 || (strcmp ("adminemail", key) == 0)
660 || (strcmp ("clan_server", key) == 0)
661 || (strcmp ("countrynumber", key) == 0)
662 || (strcmp ("id", key) == 0)
663 || (strcmp ("ispname", key) == 0)
664 || (strcmp ("linkurl", key) == 0)
665 || (strcmp ("maxusers", key) == 0)
666 || (strcmp ("name", key) == 0)
667 || (strcmp ("password", key) == 0)
668 || (strcmp ("platform", key) == 0)
669 || (strcmp ("server_platform", key) == 0)
670 || (strcmp ("server_uptime", key) == 0)
671 || (strcmp ("server_version", key) == 0)
672 || (strcmp ("udpport", key) == 0)
673 || (strcmp ("uptime", key) == 0)
674 || (strcmp ("users_maximal", key) == 0)
675 || (strcmp ("welcomemessage", key) == 0))
679 INFO ("teamspeak2 plugin: Unknown key-value-pair: "
680 "key = %s; value = %s;", key, value);
684 /* Collect vserver packet loss rates only if the loop above did not exit
686 if ((status == 0) && (vserver != NULL))
688 status = tss2_vserver_gapl (read_fh, write_fh, &packet_loss);
695 WARNING ("teamspeak2 plugin: Reading package loss "
696 "for vserver %i failed.", vserver->port);
700 if ((valid & 0x01) == 0x01)
701 tss2_submit_gauge (plugin_instance, "users", NULL, users);
703 if ((valid & 0x06) == 0x06)
704 tss2_submit_io (plugin_instance, "io_octets", rx_octets, tx_octets);
706 if ((valid & 0x18) == 0x18)
707 tss2_submit_io (plugin_instance, "io_packets", rx_packets, tx_packets);
709 if ((valid & 0x20) == 0x20)
710 tss2_submit_gauge (plugin_instance, "percent", "packet_loss", packet_loss);
712 if ((valid & 0x40) == 0x40)
713 tss2_submit_gauge (plugin_instance, "gauge", "channels", channels);
715 if ((valid & 0x80) == 0x80)
716 tss2_submit_gauge (plugin_instance, "gauge", "servers", servers);
721 } /* int tss2_read_vserver */
723 static int tss2_config (const char *key, const char *value)
726 * Interpret configuration values
728 if (strcasecmp ("Host", key) == 0)
732 temp = strdup (value);
735 ERROR("teamspeak2 plugin: strdup failed.");
741 else if (strcasecmp ("Port", key) == 0)
745 temp = strdup (value);
748 ERROR("teamspeak2 plugin: strdup failed.");
754 else if (strcasecmp ("Server", key) == 0)
756 /* Server variable found */
759 status = tss2_add_vserver (atoi (value));
765 /* Unknown variable found */
770 } /* int tss2_config */
772 static int tss2_read (void)
775 * Poll function which collects global and vserver information
776 * and submits it to collectd
778 vserver_list_t *vserver;
782 /* Handle global server variables */
783 status = tss2_read_vserver (NULL);
790 WARNING ("teamspeak2 plugin: Reading global server variables failed.");
793 /* Handle vservers */
794 for (vserver = server_list; vserver != NULL; vserver = vserver->next)
796 status = tss2_read_vserver (vserver);
803 WARNING ("teamspeak2 plugin: Reading statistics "
804 "for vserver %i failed.", vserver->port);
812 } /* int tss2_read */
814 static int tss2_shutdown(void)
819 vserver_list_t *entry;
821 tss2_close_socket ();
825 while (entry != NULL)
827 vserver_list_t *next;
834 /* Get rid of the configuration */
839 } /* int tss2_shutdown */
841 void module_register(void)
844 * Mandatory module_register function
846 plugin_register_config ("teamspeak2", tss2_config,
847 config_keys, config_keys_num);
848 plugin_register_read ("teamspeak2", tss2_read);
849 plugin_register_shutdown ("teamspeak2", tss2_shutdown);
850 } /* void module_register */
852 /* vim: set sw=4 ts=4 : */