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_t values[1];
125 value_list_t vl = VALUE_LIST_INIT;
127 values[0].gauge = value;
129 vl.values = values;
130 vl.values_len = 1;
131 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
132 sstrncpy (vl.plugin, "teamspeak2", sizeof (vl.plugin));
134 if (plugin_instance != NULL)
135 sstrncpy (vl.plugin_instance, plugin_instance,
136 sizeof (vl.plugin_instance));
138 sstrncpy (vl.type, type, sizeof (vl.type));
140 if (type_instance != NULL)
141 sstrncpy (vl.type_instance, type_instance,
142 sizeof (vl.type_instance));
144 plugin_dispatch_values (&vl);
145 } /* void tss2_submit_gauge */
147 static void tss2_submit_io (const char *plugin_instance, const char *type,
148 derive_t rx, derive_t tx)
149 {
150 /*
151 * Submits the io rx/tx tuple to the collectd daemon
152 */
153 value_t values[2];
154 value_list_t vl = VALUE_LIST_INIT;
156 values[0].derive = rx;
157 values[1].derive = tx;
159 vl.values = values;
160 vl.values_len = 2;
161 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
162 sstrncpy (vl.plugin, "teamspeak2", sizeof (vl.plugin));
164 if (plugin_instance != NULL)
165 sstrncpy (vl.plugin_instance, plugin_instance,
166 sizeof (vl.plugin_instance));
168 sstrncpy (vl.type, type, sizeof (vl.type));
170 plugin_dispatch_values (&vl);
171 } /* void tss2_submit_gauge */
173 static void tss2_close_socket (void)
174 {
175 /*
176 * Closes all sockets
177 */
178 if (global_write_fh != NULL)
179 {
180 fputs ("quit\r\n", global_write_fh);
181 }
183 if (global_read_fh != NULL)
184 {
185 fclose (global_read_fh);
186 global_read_fh = NULL;
187 }
189 if (global_write_fh != NULL)
190 {
191 fclose (global_write_fh);
192 global_write_fh = NULL;
193 }
194 } /* void tss2_close_socket */
196 static int tss2_get_socket (FILE **ret_read_fh, FILE **ret_write_fh)
197 {
198 /*
199 * Returns connected file objects or establishes the connection
200 * if it's not already present
201 */
202 struct addrinfo *ai_head;
203 int sd = -1;
204 int status;
206 /* Check if we already got opened connections */
207 if ((global_read_fh != NULL) && (global_write_fh != NULL))
208 {
209 /* If so, use them */
210 if (ret_read_fh != NULL)
211 *ret_read_fh = global_read_fh;
212 if (ret_write_fh != NULL)
213 *ret_write_fh = global_write_fh;
214 return (0);
215 }
217 /* Get all addrs for this hostname */
218 struct addrinfo ai_hints = {
219 .ai_family = AF_UNSPEC,
220 .ai_flags = AI_ADDRCONFIG,
221 .ai_socktype = SOCK_STREAM
222 };
224 status = getaddrinfo ((config_host != NULL) ? config_host : DEFAULT_HOST,
225 (config_port != NULL) ? config_port : DEFAULT_PORT,
226 &ai_hints,
227 &ai_head);
228 if (status != 0)
229 {
230 ERROR ("teamspeak2 plugin: getaddrinfo failed: %s",
231 gai_strerror (status));
232 return (-1);
233 }
235 /* Try all given hosts until we can connect to one */
236 for (struct addrinfo *ai_ptr = ai_head; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
237 {
238 /* Create socket */
239 sd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype,
240 ai_ptr->ai_protocol);
241 if (sd < 0)
242 {
243 char errbuf[1024];
244 WARNING ("teamspeak2 plugin: socket failed: %s",
245 sstrerror (errno, errbuf, sizeof (errbuf)));
246 continue;
247 }
249 /* Try to connect */
250 status = connect (sd, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
251 if (status != 0)
252 {
253 char errbuf[1024];
254 WARNING ("teamspeak2 plugin: connect failed: %s",
255 sstrerror (errno, errbuf, sizeof (errbuf)));
256 close (sd);
257 sd = -1;
258 continue;
259 }
261 /*
262 * Success, we can break. Don't need more than one connection
263 */
264 break;
265 } /* for (ai_ptr) */
267 freeaddrinfo (ai_head);
269 /* Check if we really got connected */
270 if (sd < 0)
271 return (-1);
273 /* Create file objects from sockets */
274 global_read_fh = fdopen (sd, "r");
275 if (global_read_fh == NULL)
276 {
277 char errbuf[1024];
278 ERROR ("teamspeak2 plugin: fdopen failed: %s",
279 sstrerror (errno, errbuf, sizeof (errbuf)));
280 close (sd);
281 return (-1);
282 }
284 global_write_fh = fdopen (sd, "w");
285 if (global_write_fh == NULL)
286 {
287 char errbuf[1024];
288 ERROR ("teamspeak2 plugin: fdopen failed: %s",
289 sstrerror (errno, errbuf, sizeof (errbuf)));
290 tss2_close_socket ();
291 return (-1);
292 }
294 { /* Check that the server correctly identifies itself. */
295 char buffer[4096];
296 char *buffer_ptr;
298 buffer_ptr = fgets (buffer, sizeof (buffer), global_read_fh);
299 if (buffer_ptr == NULL)
300 {
301 WARNING ("teamspeak2 plugin: Unexpected EOF received "
302 "from remote host %s:%s.",
303 config_host ? config_host : DEFAULT_HOST,
304 config_port ? config_port : DEFAULT_PORT);
305 }
306 buffer[sizeof (buffer) - 1] = 0;
308 if (memcmp ("[TS]\r\n", buffer, 6) != 0)
309 {
310 ERROR ("teamspeak2 plugin: Unexpected response when connecting "
311 "to server. Expected ``[TS]'', got ``%s''.",
312 buffer);
313 tss2_close_socket ();
314 return (-1);
315 }
316 DEBUG ("teamspeak2 plugin: Server send correct banner, connected!");
317 }
319 /* Copy the new filehandles to the given pointers */
320 if (ret_read_fh != NULL)
321 *ret_read_fh = global_read_fh;
322 if (ret_write_fh != NULL)
323 *ret_write_fh = global_write_fh;
324 return (0);
325 } /* int tss2_get_socket */
327 static int tss2_send_request (FILE *fh, const char *request)
328 {
329 /*
330 * This function puts a request to the server socket
331 */
332 int status;
334 status = fputs (request, fh);
335 if (status < 0)
336 {
337 ERROR ("teamspeak2 plugin: fputs failed.");
338 tss2_close_socket ();
339 return (-1);
340 }
341 fflush (fh);
343 return (0);
344 } /* int tss2_send_request */
346 static int tss2_receive_line (FILE *fh, char *buffer, int buffer_size)
347 {
348 /*
349 * Receive a single line from the given file object
350 */
351 char *temp;
353 /*
354 * fgets is blocking but much easier then doing anything else
355 * TODO: Non-blocking Version would be safer
356 */
357 temp = fgets (buffer, buffer_size, fh);
358 if (temp == NULL)
359 {
360 char errbuf[1024];
361 ERROR ("teamspeak2 plugin: fgets failed: %s",
362 sstrerror (errno, errbuf, sizeof(errbuf)));
363 tss2_close_socket ();
364 return (-1);
365 }
367 buffer[buffer_size - 1] = 0;
368 return (0);
369 } /* int tss2_receive_line */
371 static int tss2_select_vserver (FILE *read_fh, FILE *write_fh, vserver_list_t *vserver)
372 {
373 /*
374 * Tell the server to select the given vserver
375 */
376 char command[128];
377 char response[128];
378 int status;
380 /* Send request */
381 ssnprintf (command, sizeof (command), "sel %i\r\n", vserver->port);
383 status = tss2_send_request (write_fh, command);
384 if (status != 0)
385 {
386 ERROR ("teamspeak2 plugin: tss2_send_request (%s) failed.", command);
387 return (-1);
388 }
390 /* Get answer */
391 status = tss2_receive_line (read_fh, response, sizeof (response));
392 if (status != 0)
393 {
394 ERROR ("teamspeak2 plugin: tss2_receive_line failed.");
395 return (-1);
396 }
397 response[sizeof (response) - 1] = 0;
399 /* Check answer */
400 if ((strncasecmp ("OK", response, 2) == 0)
401 && ((response[2] == 0)
402 || (response[2] == '\n')
403 || (response[2] == '\r')))
404 return (0);
406 ERROR ("teamspeak2 plugin: Command ``%s'' failed. "
407 "Response received from server was: ``%s''.",
408 command, response);
409 return (-1);
410 } /* int tss2_select_vserver */
412 static int tss2_vserver_gapl (FILE *read_fh, FILE *write_fh,
413 gauge_t *ret_value)
414 {
415 /*
416 * Reads the vserver's average packet loss and submits it to collectd.
417 * Be sure to run the tss2_read_vserver function before calling this so
418 * the vserver is selected correctly.
419 */
420 gauge_t packet_loss = NAN;
421 int status;
423 status = tss2_send_request (write_fh, "gapl\r\n");
424 if (status != 0)
425 {
426 ERROR("teamspeak2 plugin: tss2_send_request (gapl) failed.");
427 return (-1);
428 }
430 while (42)
431 {
432 char buffer[4096];
433 char *value;
434 char *endptr = NULL;
436 status = tss2_receive_line (read_fh, buffer, sizeof (buffer));
437 if (status != 0)
438 {
439 /* Set to NULL just to make sure no one uses these FHs anymore. */
440 read_fh = NULL;
441 write_fh = NULL;
442 ERROR ("teamspeak2 plugin: tss2_receive_line failed.");
443 return (-1);
444 }
445 buffer[sizeof (buffer) - 1] = 0;
447 if (strncmp ("average_packet_loss=", buffer,
448 strlen ("average_packet_loss=")) == 0)
449 {
450 /* Got average packet loss, now interpret it */
451 value = &buffer[20];
452 /* Replace , with . */
453 while (*value != 0)
454 {
455 if (*value == ',')
456 {
457 *value = '.';
458 break;
459 }
460 value++;
461 }
463 value = &buffer[20];
465 packet_loss = strtod (value, &endptr);
466 if (value == endptr)
467 {
468 /* Failed */
469 WARNING ("teamspeak2 plugin: Could not read average package "
470 "loss from string: %s", buffer);
471 continue;
472 }
473 }
474 else if (strncasecmp ("OK", buffer, 2) == 0)
475 {
476 break;
477 }
478 else if (strncasecmp ("ERROR", buffer, 5) == 0)
479 {
480 ERROR ("teamspeak2 plugin: Server returned an error: %s", buffer);
481 return (-1);
482 }
483 else
484 {
485 WARNING ("teamspeak2 plugin: Server returned unexpected string: %s",
486 buffer);
487 }
488 }
490 *ret_value = packet_loss;
491 return (0);
492 } /* int tss2_vserver_gapl */
494 static int tss2_read_vserver (vserver_list_t *vserver)
495 {
496 /*
497 * Poll information for the given vserver and submit it to collect.
498 * If vserver is NULL the global server information will be queried.
499 */
500 int status;
502 gauge_t users = NAN;
503 gauge_t channels = NAN;
504 gauge_t servers = NAN;
505 derive_t rx_octets = 0;
506 derive_t tx_octets = 0;
507 derive_t rx_packets = 0;
508 derive_t tx_packets = 0;
509 gauge_t packet_loss = NAN;
510 int valid = 0;
512 char plugin_instance[DATA_MAX_NAME_LEN] = { 0 };
514 FILE *read_fh;
515 FILE *write_fh;
517 /* Get the send/receive sockets */
518 status = tss2_get_socket (&read_fh, &write_fh);
519 if (status != 0)
520 {
521 ERROR ("teamspeak2 plugin: tss2_get_socket failed.");
522 return (-1);
523 }
525 if (vserver == NULL)
526 {
527 /* Request global information */
528 status = tss2_send_request (write_fh, "gi\r\n");
529 }
530 else
531 {
532 /* Request server information */
533 ssnprintf (plugin_instance, sizeof (plugin_instance), "vserver%i",
534 vserver->port);
536 /* Select the server */
537 status = tss2_select_vserver (read_fh, write_fh, vserver);
538 if (status != 0)
539 return (status);
541 status = tss2_send_request (write_fh, "si\r\n");
542 }
544 if (status != 0)
545 {
546 ERROR ("teamspeak2 plugin: tss2_send_request failed.");
547 return (-1);
548 }
550 /* Loop until break */
551 while (42)
552 {
553 char buffer[4096];
554 char *key;
555 char *value;
556 char *endptr = NULL;
558 /* Read one line of the server's answer */
559 status = tss2_receive_line (read_fh, buffer, sizeof (buffer));
560 if (status != 0)
561 {
562 /* Set to NULL just to make sure no one uses these FHs anymore. */
563 read_fh = NULL;
564 write_fh = NULL;
565 ERROR ("teamspeak2 plugin: tss2_receive_line failed.");
566 break;
567 }
569 if (strncasecmp ("ERROR", buffer, 5) == 0)
570 {
571 ERROR ("teamspeak2 plugin: Server returned an error: %s",
572 buffer);
573 break;
574 }
575 else if (strncasecmp ("OK", buffer, 2) == 0)
576 {
577 break;
578 }
580 /* Split line into key and value */
581 key = strchr (buffer, '_');
582 if (key == NULL)
583 {
584 DEBUG ("teamspeak2 plugin: Cannot parse line: %s", buffer);
585 continue;
586 }
587 key++;
589 /* Evaluate assignment */
590 value = strchr (key, '=');
591 if (value == NULL)
592 {
593 DEBUG ("teamspeak2 plugin: Cannot parse line: %s", buffer);
594 continue;
595 }
596 *value = 0;
597 value++;
599 /* Check for known key and save the given value */
600 /* global info: users_online,
601 * server info: currentusers. */
602 if ((strcmp ("currentusers", key) == 0)
603 || (strcmp ("users_online", key) == 0))
604 {
605 users = strtod (value, &endptr);
606 if (value != endptr)
607 valid |= 0x01;
608 }
609 /* global info: channels,
610 * server info: currentchannels. */
611 else if ((strcmp ("currentchannels", key) == 0)
612 || (strcmp ("channels", key) == 0))
613 {
614 channels = strtod (value, &endptr);
615 if (value != endptr)
616 valid |= 0x40;
617 }
618 /* global only */
619 else if (strcmp ("servers", key) == 0)
620 {
621 servers = strtod (value, &endptr);
622 if (value != endptr)
623 valid |= 0x80;
624 }
625 else if (strcmp ("bytesreceived", key) == 0)
626 {
627 rx_octets = strtoll (value, &endptr, 0);
628 if (value != endptr)
629 valid |= 0x02;
630 }
631 else if (strcmp ("bytessend", key) == 0)
632 {
633 tx_octets = strtoll (value, &endptr, 0);
634 if (value != endptr)
635 valid |= 0x04;
636 }
637 else if (strcmp ("packetsreceived", key) == 0)
638 {
639 rx_packets = strtoll (value, &endptr, 0);
640 if (value != endptr)
641 valid |= 0x08;
642 }
643 else if (strcmp ("packetssend", key) == 0)
644 {
645 tx_packets = strtoll (value, &endptr, 0);
646 if (value != endptr)
647 valid |= 0x10;
648 }
649 else if ((strncmp ("allow_codec_", key, strlen ("allow_codec_")) == 0)
650 || (strncmp ("bwinlast", key, strlen ("bwinlast")) == 0)
651 || (strncmp ("bwoutlast", key, strlen ("bwoutlast")) == 0)
652 || (strncmp ("webpost_", key, strlen ("webpost_")) == 0)
653 || (strcmp ("adminemail", key) == 0)
654 || (strcmp ("clan_server", key) == 0)
655 || (strcmp ("countrynumber", key) == 0)
656 || (strcmp ("id", key) == 0)
657 || (strcmp ("ispname", key) == 0)
658 || (strcmp ("linkurl", key) == 0)
659 || (strcmp ("maxusers", key) == 0)
660 || (strcmp ("name", key) == 0)
661 || (strcmp ("password", key) == 0)
662 || (strcmp ("platform", key) == 0)
663 || (strcmp ("server_platform", key) == 0)
664 || (strcmp ("server_uptime", key) == 0)
665 || (strcmp ("server_version", key) == 0)
666 || (strcmp ("udpport", key) == 0)
667 || (strcmp ("uptime", key) == 0)
668 || (strcmp ("users_maximal", key) == 0)
669 || (strcmp ("welcomemessage", key) == 0))
670 /* ignore */;
671 else
672 {
673 INFO ("teamspeak2 plugin: Unknown key-value-pair: "
674 "key = %s; value = %s;", key, value);
675 }
676 } /* while (42) */
678 /* Collect vserver packet loss rates only if the loop above did not exit
679 * with an error. */
680 if ((status == 0) && (vserver != NULL))
681 {
682 status = tss2_vserver_gapl (read_fh, write_fh, &packet_loss);
683 if (status == 0)
684 {
685 valid |= 0x20;
686 }
687 else
688 {
689 WARNING ("teamspeak2 plugin: Reading package loss "
690 "for vserver %i failed.", vserver->port);
691 }
692 }
694 if ((valid & 0x01) == 0x01)
695 tss2_submit_gauge (plugin_instance, "users", NULL, users);
697 if ((valid & 0x06) == 0x06)
698 tss2_submit_io (plugin_instance, "io_octets", rx_octets, tx_octets);
700 if ((valid & 0x18) == 0x18)
701 tss2_submit_io (plugin_instance, "io_packets", rx_packets, tx_packets);
703 if ((valid & 0x20) == 0x20)
704 tss2_submit_gauge (plugin_instance, "percent", "packet_loss", packet_loss);
706 if ((valid & 0x40) == 0x40)
707 tss2_submit_gauge (plugin_instance, "gauge", "channels", channels);
709 if ((valid & 0x80) == 0x80)
710 tss2_submit_gauge (plugin_instance, "gauge", "servers", servers);
712 if (valid == 0)
713 return (-1);
714 return (0);
715 } /* int tss2_read_vserver */
717 static int tss2_config (const char *key, const char *value)
718 {
719 /*
720 * Interpret configuration values
721 */
722 if (strcasecmp ("Host", key) == 0)
723 {
724 char *temp;
726 temp = strdup (value);
727 if (temp == NULL)
728 {
729 ERROR("teamspeak2 plugin: strdup failed.");
730 return (1);
731 }
732 sfree (config_host);
733 config_host = temp;
734 }
735 else if (strcasecmp ("Port", key) == 0)
736 {
737 char *temp;
739 temp = strdup (value);
740 if (temp == NULL)
741 {
742 ERROR("teamspeak2 plugin: strdup failed.");
743 return (1);
744 }
745 sfree (config_port);
746 config_port = temp;
747 }
748 else if (strcasecmp ("Server", key) == 0)
749 {
750 /* Server variable found */
751 int status;
753 status = tss2_add_vserver (atoi (value));
754 if (status != 0)
755 return (1);
756 }
757 else
758 {
759 /* Unknown variable found */
760 return (-1);
761 }
763 return 0;
764 } /* int tss2_config */
766 static int tss2_read (void)
767 {
768 /*
769 * Poll function which collects global and vserver information
770 * and submits it to collectd
771 */
772 int success = 0;
773 int status;
775 /* Handle global server variables */
776 status = tss2_read_vserver (NULL);
777 if (status == 0)
778 {
779 success++;
780 }
781 else
782 {
783 WARNING ("teamspeak2 plugin: Reading global server variables failed.");
784 }
786 /* Handle vservers */
787 for (vserver_list_t *vserver = server_list; vserver != NULL; vserver = vserver->next)
788 {
789 status = tss2_read_vserver (vserver);
790 if (status == 0)
791 {
792 success++;
793 }
794 else
795 {
796 WARNING ("teamspeak2 plugin: Reading statistics "
797 "for vserver %i failed.", vserver->port);
798 continue;
799 }
800 }
802 if (success == 0)
803 return (-1);
804 return (0);
805 } /* int tss2_read */
807 static int tss2_shutdown(void)
808 {
809 /*
810 * Shutdown handler
811 */
812 vserver_list_t *entry;
814 tss2_close_socket ();
816 entry = server_list;
817 server_list = NULL;
818 while (entry != NULL)
819 {
820 vserver_list_t *next;
822 next = entry->next;
823 sfree (entry);
824 entry = next;
825 }
827 /* Get rid of the configuration */
828 sfree (config_host);
829 sfree (config_port);
831 return (0);
832 } /* int tss2_shutdown */
834 void module_register(void)
835 {
836 /*
837 * Mandatory module_register function
838 */
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 : */