1 /**
2 * collectd - src/teamspeak2.c
3 * Copyright (C) 2008 Stefan Hacker
4 * Copyright (C) 2008 Florian Forster
5 *
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.
9 *
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.
14 *
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
18 *
19 * Authors:
20 * Stefan Hacker <d0t at dbclan dot de>
21 * Florian Forster <octo at collectd.org>
22 **/
24 #include "collectd.h"
26 #include "common.h"
27 #include "plugin.h"
29 #include <netinet/in.h>
30 #include <arpa/inet.h>
31 #include <sys/types.h>
32 #include <netdb.h>
34 /*
35 * Defines
36 */
37 /* Default host and port */
38 #define DEFAULT_HOST "127.0.0.1"
39 #define DEFAULT_PORT "51234"
41 /*
42 * Variables
43 */
44 /* Server linked list structure */
45 typedef struct vserver_list_s
46 {
47 int port;
48 struct vserver_list_s *next;
49 } vserver_list_t;
50 static vserver_list_t *server_list = NULL;
52 /* Host data */
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;
59 /* Config data */
60 static const char *config_keys[] =
61 {
62 "Host",
63 "Port",
64 "Server"
65 };
66 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
68 /*
69 * Functions
70 */
71 static int tss2_add_vserver (int vserver_port)
72 {
73 /*
74 * Adds a new vserver to the linked list
75 */
76 vserver_list_t *entry;
78 /* Check port range */
79 if ((vserver_port <= 0) || (vserver_port > 65535))
80 {
81 ERROR ("teamspeak2 plugin: VServer port is invalid: %i",
82 vserver_port);
83 return (-1);
84 }
86 /* Allocate memory */
87 entry = calloc (1, sizeof (*entry));
88 if (entry == NULL)
89 {
90 ERROR ("teamspeak2 plugin: calloc failed.");
91 return (-1);
92 }
94 /* Save data */
95 entry->port = vserver_port;
97 /* Insert to list */
98 if(server_list == NULL) {
99 /* Add the server as the first element */
100 server_list = entry;
101 }
102 else {
103 vserver_list_t *prev;
105 /* Add the server to the end of the list */
106 prev = server_list;
107 while (prev->next != NULL)
108 prev = prev->next;
109 prev->next = entry;
110 }
112 INFO ("teamspeak2 plugin: Registered new vserver: %i", vserver_port);
114 return (0);
115 } /* int tss2_add_vserver */
117 static void tss2_submit_gauge (const char *plugin_instance,
118 const char *type, const char *type_instance,
119 gauge_t value)
120 {
121 /*
122 * Submits a gauge value to the collectd daemon
123 */
124 value_list_t vl = VALUE_LIST_INIT;
126 vl.values = &(value_t) { .gauge = value };
127 vl.values_len = 1;
128 sstrncpy (vl.plugin, "teamspeak2", sizeof (vl.plugin));
130 if (plugin_instance != NULL)
131 sstrncpy (vl.plugin_instance, plugin_instance,
132 sizeof (vl.plugin_instance));
134 sstrncpy (vl.type, type, sizeof (vl.type));
136 if (type_instance != NULL)
137 sstrncpy (vl.type_instance, type_instance,
138 sizeof (vl.type_instance));
140 plugin_dispatch_values (&vl);
141 } /* void tss2_submit_gauge */
143 static void tss2_submit_io (const char *plugin_instance, const char *type,
144 derive_t rx, derive_t tx)
145 {
146 /*
147 * Submits the io rx/tx tuple to the collectd daemon
148 */
149 value_list_t vl = VALUE_LIST_INIT;
150 value_t values[] = {
151 { .derive = rx },
152 { .derive = tx },
153 };
155 vl.values = values;
156 vl.values_len = STATIC_ARRAY_SIZE (values);
157 sstrncpy (vl.plugin, "teamspeak2", sizeof (vl.plugin));
159 if (plugin_instance != NULL)
160 sstrncpy (vl.plugin_instance, plugin_instance,
161 sizeof (vl.plugin_instance));
163 sstrncpy (vl.type, type, sizeof (vl.type));
165 plugin_dispatch_values (&vl);
166 } /* void tss2_submit_gauge */
168 static void tss2_close_socket (void)
169 {
170 /*
171 * Closes all sockets
172 */
173 if (global_write_fh != NULL)
174 {
175 fputs ("quit\r\n", global_write_fh);
176 }
178 if (global_read_fh != NULL)
179 {
180 fclose (global_read_fh);
181 global_read_fh = NULL;
182 }
184 if (global_write_fh != NULL)
185 {
186 fclose (global_write_fh);
187 global_write_fh = NULL;
188 }
189 } /* void tss2_close_socket */
191 static int tss2_get_socket (FILE **ret_read_fh, FILE **ret_write_fh)
192 {
193 /*
194 * Returns connected file objects or establishes the connection
195 * if it's not already present
196 */
197 struct addrinfo *ai_head;
198 int sd = -1;
199 int status;
201 /* Check if we already got opened connections */
202 if ((global_read_fh != NULL) && (global_write_fh != NULL))
203 {
204 /* If so, use them */
205 if (ret_read_fh != NULL)
206 *ret_read_fh = global_read_fh;
207 if (ret_write_fh != NULL)
208 *ret_write_fh = global_write_fh;
209 return (0);
210 }
212 /* Get all addrs for this hostname */
213 struct addrinfo ai_hints = {
214 .ai_family = AF_UNSPEC,
215 .ai_flags = AI_ADDRCONFIG,
216 .ai_socktype = SOCK_STREAM
217 };
219 status = getaddrinfo ((config_host != NULL) ? config_host : DEFAULT_HOST,
220 (config_port != NULL) ? config_port : DEFAULT_PORT,
221 &ai_hints,
222 &ai_head);
223 if (status != 0)
224 {
225 ERROR ("teamspeak2 plugin: getaddrinfo failed: %s",
226 gai_strerror (status));
227 return (-1);
228 }
230 /* Try all given hosts until we can connect to one */
231 for (struct addrinfo *ai_ptr = ai_head; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
232 {
233 /* Create socket */
234 sd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype,
235 ai_ptr->ai_protocol);
236 if (sd < 0)
237 {
238 char errbuf[1024];
239 WARNING ("teamspeak2 plugin: socket failed: %s",
240 sstrerror (errno, errbuf, sizeof (errbuf)));
241 continue;
242 }
244 /* Try to connect */
245 status = connect (sd, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
246 if (status != 0)
247 {
248 char errbuf[1024];
249 WARNING ("teamspeak2 plugin: connect failed: %s",
250 sstrerror (errno, errbuf, sizeof (errbuf)));
251 close (sd);
252 sd = -1;
253 continue;
254 }
256 /*
257 * Success, we can break. Don't need more than one connection
258 */
259 break;
260 } /* for (ai_ptr) */
262 freeaddrinfo (ai_head);
264 /* Check if we really got connected */
265 if (sd < 0)
266 return (-1);
268 /* Create file objects from sockets */
269 global_read_fh = fdopen (sd, "r");
270 if (global_read_fh == NULL)
271 {
272 char errbuf[1024];
273 ERROR ("teamspeak2 plugin: fdopen failed: %s",
274 sstrerror (errno, errbuf, sizeof (errbuf)));
275 close (sd);
276 return (-1);
277 }
279 global_write_fh = fdopen (sd, "w");
280 if (global_write_fh == NULL)
281 {
282 char errbuf[1024];
283 ERROR ("teamspeak2 plugin: fdopen failed: %s",
284 sstrerror (errno, errbuf, sizeof (errbuf)));
285 tss2_close_socket ();
286 return (-1);
287 }
289 { /* Check that the server correctly identifies itself. */
290 char buffer[4096];
291 char *buffer_ptr;
293 buffer_ptr = fgets (buffer, sizeof (buffer), global_read_fh);
294 if (buffer_ptr == NULL)
295 {
296 WARNING ("teamspeak2 plugin: Unexpected EOF received "
297 "from remote host %s:%s.",
298 config_host ? config_host : DEFAULT_HOST,
299 config_port ? config_port : DEFAULT_PORT);
300 }
301 buffer[sizeof (buffer) - 1] = 0;
303 if (memcmp ("[TS]\r\n", buffer, 6) != 0)
304 {
305 ERROR ("teamspeak2 plugin: Unexpected response when connecting "
306 "to server. Expected ``[TS]'', got ``%s''.",
307 buffer);
308 tss2_close_socket ();
309 return (-1);
310 }
311 DEBUG ("teamspeak2 plugin: Server send correct banner, connected!");
312 }
314 /* Copy the new filehandles to the given pointers */
315 if (ret_read_fh != NULL)
316 *ret_read_fh = global_read_fh;
317 if (ret_write_fh != NULL)
318 *ret_write_fh = global_write_fh;
319 return (0);
320 } /* int tss2_get_socket */
322 static int tss2_send_request (FILE *fh, const char *request)
323 {
324 /*
325 * This function puts a request to the server socket
326 */
327 int status;
329 status = fputs (request, fh);
330 if (status < 0)
331 {
332 ERROR ("teamspeak2 plugin: fputs failed.");
333 tss2_close_socket ();
334 return (-1);
335 }
336 fflush (fh);
338 return (0);
339 } /* int tss2_send_request */
341 static int tss2_receive_line (FILE *fh, char *buffer, int buffer_size)
342 {
343 /*
344 * Receive a single line from the given file object
345 */
346 char *temp;
348 /*
349 * fgets is blocking but much easier then doing anything else
350 * TODO: Non-blocking Version would be safer
351 */
352 temp = fgets (buffer, buffer_size, fh);
353 if (temp == NULL)
354 {
355 char errbuf[1024];
356 ERROR ("teamspeak2 plugin: fgets failed: %s",
357 sstrerror (errno, errbuf, sizeof(errbuf)));
358 tss2_close_socket ();
359 return (-1);
360 }
362 buffer[buffer_size - 1] = 0;
363 return (0);
364 } /* int tss2_receive_line */
366 static int tss2_select_vserver (FILE *read_fh, FILE *write_fh, vserver_list_t *vserver)
367 {
368 /*
369 * Tell the server to select the given vserver
370 */
371 char command[128];
372 char response[128];
373 int status;
375 /* Send request */
376 ssnprintf (command, sizeof (command), "sel %i\r\n", vserver->port);
378 status = tss2_send_request (write_fh, command);
379 if (status != 0)
380 {
381 ERROR ("teamspeak2 plugin: tss2_send_request (%s) failed.", command);
382 return (-1);
383 }
385 /* Get answer */
386 status = tss2_receive_line (read_fh, response, sizeof (response));
387 if (status != 0)
388 {
389 ERROR ("teamspeak2 plugin: tss2_receive_line failed.");
390 return (-1);
391 }
392 response[sizeof (response) - 1] = 0;
394 /* Check answer */
395 if ((strncasecmp ("OK", response, 2) == 0)
396 && ((response[2] == 0)
397 || (response[2] == '\n')
398 || (response[2] == '\r')))
399 return (0);
401 ERROR ("teamspeak2 plugin: Command ``%s'' failed. "
402 "Response received from server was: ``%s''.",
403 command, response);
404 return (-1);
405 } /* int tss2_select_vserver */
407 static int tss2_vserver_gapl (FILE *read_fh, FILE *write_fh,
408 gauge_t *ret_value)
409 {
410 /*
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.
414 */
415 gauge_t packet_loss = NAN;
416 int status;
418 status = tss2_send_request (write_fh, "gapl\r\n");
419 if (status != 0)
420 {
421 ERROR("teamspeak2 plugin: tss2_send_request (gapl) failed.");
422 return (-1);
423 }
425 while (42)
426 {
427 char buffer[4096];
428 char *value;
429 char *endptr = NULL;
431 status = tss2_receive_line (read_fh, buffer, sizeof (buffer));
432 if (status != 0)
433 {
434 /* Set to NULL just to make sure no one uses these FHs anymore. */
435 read_fh = NULL;
436 write_fh = NULL;
437 ERROR ("teamspeak2 plugin: tss2_receive_line failed.");
438 return (-1);
439 }
440 buffer[sizeof (buffer) - 1] = 0;
442 if (strncmp ("average_packet_loss=", buffer,
443 strlen ("average_packet_loss=")) == 0)
444 {
445 /* Got average packet loss, now interpret it */
446 value = &buffer[20];
447 /* Replace , with . */
448 while (*value != 0)
449 {
450 if (*value == ',')
451 {
452 *value = '.';
453 break;
454 }
455 value++;
456 }
458 value = &buffer[20];
460 packet_loss = strtod (value, &endptr);
461 if (value == endptr)
462 {
463 /* Failed */
464 WARNING ("teamspeak2 plugin: Could not read average package "
465 "loss from string: %s", buffer);
466 continue;
467 }
468 }
469 else if (strncasecmp ("OK", buffer, 2) == 0)
470 {
471 break;
472 }
473 else if (strncasecmp ("ERROR", buffer, 5) == 0)
474 {
475 ERROR ("teamspeak2 plugin: Server returned an error: %s", buffer);
476 return (-1);
477 }
478 else
479 {
480 WARNING ("teamspeak2 plugin: Server returned unexpected string: %s",
481 buffer);
482 }
483 }
485 *ret_value = packet_loss;
486 return (0);
487 } /* int tss2_vserver_gapl */
489 static int tss2_read_vserver (vserver_list_t *vserver)
490 {
491 /*
492 * Poll information for the given vserver and submit it to collect.
493 * If vserver is NULL the global server information will be queried.
494 */
495 int status;
497 gauge_t users = NAN;
498 gauge_t channels = NAN;
499 gauge_t servers = NAN;
500 derive_t rx_octets = 0;
501 derive_t tx_octets = 0;
502 derive_t rx_packets = 0;
503 derive_t tx_packets = 0;
504 gauge_t packet_loss = NAN;
505 int valid = 0;
507 char plugin_instance[DATA_MAX_NAME_LEN] = { 0 };
509 FILE *read_fh;
510 FILE *write_fh;
512 /* Get the send/receive sockets */
513 status = tss2_get_socket (&read_fh, &write_fh);
514 if (status != 0)
515 {
516 ERROR ("teamspeak2 plugin: tss2_get_socket failed.");
517 return (-1);
518 }
520 if (vserver == NULL)
521 {
522 /* Request global information */
523 status = tss2_send_request (write_fh, "gi\r\n");
524 }
525 else
526 {
527 /* Request server information */
528 ssnprintf (plugin_instance, sizeof (plugin_instance), "vserver%i",
529 vserver->port);
531 /* Select the server */
532 status = tss2_select_vserver (read_fh, write_fh, vserver);
533 if (status != 0)
534 return (status);
536 status = tss2_send_request (write_fh, "si\r\n");
537 }
539 if (status != 0)
540 {
541 ERROR ("teamspeak2 plugin: tss2_send_request failed.");
542 return (-1);
543 }
545 /* Loop until break */
546 while (42)
547 {
548 char buffer[4096];
549 char *key;
550 char *value;
551 char *endptr = NULL;
553 /* Read one line of the server's answer */
554 status = tss2_receive_line (read_fh, buffer, sizeof (buffer));
555 if (status != 0)
556 {
557 /* Set to NULL just to make sure no one uses these FHs anymore. */
558 read_fh = NULL;
559 write_fh = NULL;
560 ERROR ("teamspeak2 plugin: tss2_receive_line failed.");
561 break;
562 }
564 if (strncasecmp ("ERROR", buffer, 5) == 0)
565 {
566 ERROR ("teamspeak2 plugin: Server returned an error: %s",
567 buffer);
568 break;
569 }
570 else if (strncasecmp ("OK", buffer, 2) == 0)
571 {
572 break;
573 }
575 /* Split line into key and value */
576 key = strchr (buffer, '_');
577 if (key == NULL)
578 {
579 DEBUG ("teamspeak2 plugin: Cannot parse line: %s", buffer);
580 continue;
581 }
582 key++;
584 /* Evaluate assignment */
585 value = strchr (key, '=');
586 if (value == NULL)
587 {
588 DEBUG ("teamspeak2 plugin: Cannot parse line: %s", buffer);
589 continue;
590 }
591 *value = 0;
592 value++;
594 /* Check for known key and save the given value */
595 /* global info: users_online,
596 * server info: currentusers. */
597 if ((strcmp ("currentusers", key) == 0)
598 || (strcmp ("users_online", key) == 0))
599 {
600 users = strtod (value, &endptr);
601 if (value != endptr)
602 valid |= 0x01;
603 }
604 /* global info: channels,
605 * server info: currentchannels. */
606 else if ((strcmp ("currentchannels", key) == 0)
607 || (strcmp ("channels", key) == 0))
608 {
609 channels = strtod (value, &endptr);
610 if (value != endptr)
611 valid |= 0x40;
612 }
613 /* global only */
614 else if (strcmp ("servers", key) == 0)
615 {
616 servers = strtod (value, &endptr);
617 if (value != endptr)
618 valid |= 0x80;
619 }
620 else if (strcmp ("bytesreceived", key) == 0)
621 {
622 rx_octets = strtoll (value, &endptr, 0);
623 if (value != endptr)
624 valid |= 0x02;
625 }
626 else if (strcmp ("bytessend", key) == 0)
627 {
628 tx_octets = strtoll (value, &endptr, 0);
629 if (value != endptr)
630 valid |= 0x04;
631 }
632 else if (strcmp ("packetsreceived", key) == 0)
633 {
634 rx_packets = strtoll (value, &endptr, 0);
635 if (value != endptr)
636 valid |= 0x08;
637 }
638 else if (strcmp ("packetssend", key) == 0)
639 {
640 tx_packets = strtoll (value, &endptr, 0);
641 if (value != endptr)
642 valid |= 0x10;
643 }
644 else if ((strncmp ("allow_codec_", key, strlen ("allow_codec_")) == 0)
645 || (strncmp ("bwinlast", key, strlen ("bwinlast")) == 0)
646 || (strncmp ("bwoutlast", key, strlen ("bwoutlast")) == 0)
647 || (strncmp ("webpost_", key, strlen ("webpost_")) == 0)
648 || (strcmp ("adminemail", key) == 0)
649 || (strcmp ("clan_server", key) == 0)
650 || (strcmp ("countrynumber", key) == 0)
651 || (strcmp ("id", key) == 0)
652 || (strcmp ("ispname", key) == 0)
653 || (strcmp ("linkurl", key) == 0)
654 || (strcmp ("maxusers", key) == 0)
655 || (strcmp ("name", key) == 0)
656 || (strcmp ("password", key) == 0)
657 || (strcmp ("platform", key) == 0)
658 || (strcmp ("server_platform", key) == 0)
659 || (strcmp ("server_uptime", key) == 0)
660 || (strcmp ("server_version", key) == 0)
661 || (strcmp ("udpport", key) == 0)
662 || (strcmp ("uptime", key) == 0)
663 || (strcmp ("users_maximal", key) == 0)
664 || (strcmp ("welcomemessage", key) == 0))
665 /* ignore */;
666 else
667 {
668 INFO ("teamspeak2 plugin: Unknown key-value-pair: "
669 "key = %s; value = %s;", key, value);
670 }
671 } /* while (42) */
673 /* Collect vserver packet loss rates only if the loop above did not exit
674 * with an error. */
675 if ((status == 0) && (vserver != NULL))
676 {
677 status = tss2_vserver_gapl (read_fh, write_fh, &packet_loss);
678 if (status == 0)
679 {
680 valid |= 0x20;
681 }
682 else
683 {
684 WARNING ("teamspeak2 plugin: Reading package loss "
685 "for vserver %i failed.", vserver->port);
686 }
687 }
689 if ((valid & 0x01) == 0x01)
690 tss2_submit_gauge (plugin_instance, "users", NULL, users);
692 if ((valid & 0x06) == 0x06)
693 tss2_submit_io (plugin_instance, "io_octets", rx_octets, tx_octets);
695 if ((valid & 0x18) == 0x18)
696 tss2_submit_io (plugin_instance, "io_packets", rx_packets, tx_packets);
698 if ((valid & 0x20) == 0x20)
699 tss2_submit_gauge (plugin_instance, "percent", "packet_loss", packet_loss);
701 if ((valid & 0x40) == 0x40)
702 tss2_submit_gauge (plugin_instance, "gauge", "channels", channels);
704 if ((valid & 0x80) == 0x80)
705 tss2_submit_gauge (plugin_instance, "gauge", "servers", servers);
707 if (valid == 0)
708 return (-1);
709 return (0);
710 } /* int tss2_read_vserver */
712 static int tss2_config (const char *key, const char *value)
713 {
714 /*
715 * Interpret configuration values
716 */
717 if (strcasecmp ("Host", key) == 0)
718 {
719 char *temp;
721 temp = strdup (value);
722 if (temp == NULL)
723 {
724 ERROR("teamspeak2 plugin: strdup failed.");
725 return (1);
726 }
727 sfree (config_host);
728 config_host = temp;
729 }
730 else if (strcasecmp ("Port", key) == 0)
731 {
732 char *temp;
734 temp = strdup (value);
735 if (temp == NULL)
736 {
737 ERROR("teamspeak2 plugin: strdup failed.");
738 return (1);
739 }
740 sfree (config_port);
741 config_port = temp;
742 }
743 else if (strcasecmp ("Server", key) == 0)
744 {
745 /* Server variable found */
746 int status;
748 status = tss2_add_vserver (atoi (value));
749 if (status != 0)
750 return (1);
751 }
752 else
753 {
754 /* Unknown variable found */
755 return (-1);
756 }
758 return 0;
759 } /* int tss2_config */
761 static int tss2_read (void)
762 {
763 /*
764 * Poll function which collects global and vserver information
765 * and submits it to collectd
766 */
767 int success = 0;
768 int status;
770 /* Handle global server variables */
771 status = tss2_read_vserver (NULL);
772 if (status == 0)
773 {
774 success++;
775 }
776 else
777 {
778 WARNING ("teamspeak2 plugin: Reading global server variables failed.");
779 }
781 /* Handle vservers */
782 for (vserver_list_t *vserver = server_list; vserver != NULL; vserver = vserver->next)
783 {
784 status = tss2_read_vserver (vserver);
785 if (status == 0)
786 {
787 success++;
788 }
789 else
790 {
791 WARNING ("teamspeak2 plugin: Reading statistics "
792 "for vserver %i failed.", vserver->port);
793 continue;
794 }
795 }
797 if (success == 0)
798 return (-1);
799 return (0);
800 } /* int tss2_read */
802 static int tss2_shutdown(void)
803 {
804 /*
805 * Shutdown handler
806 */
807 vserver_list_t *entry;
809 tss2_close_socket ();
811 entry = server_list;
812 server_list = NULL;
813 while (entry != NULL)
814 {
815 vserver_list_t *next;
817 next = entry->next;
818 sfree (entry);
819 entry = next;
820 }
822 /* Get rid of the configuration */
823 sfree (config_host);
824 sfree (config_port);
826 return (0);
827 } /* int tss2_shutdown */
829 void module_register(void)
830 {
831 /*
832 * Mandatory module_register function
833 */
834 plugin_register_config ("teamspeak2", tss2_config,
835 config_keys, config_keys_num);
836 plugin_register_read ("teamspeak2", tss2_read);
837 plugin_register_shutdown ("teamspeak2", tss2_shutdown);
838 } /* void module_register */
840 /* vim: set sw=4 ts=4 : */