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 verplant.org>
22 **/
24 #include "collectd.h"
25 #include "common.h"
26 #include "plugin.h"
28 #include <arpa/inet.h>
29 #include <netinet/in.h>
30 #include <sys/types.h>
31 #include <sys/socket.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 = (vserver_list_t *) malloc (sizeof (vserver_list_t));
88 if (entry == NULL)
89 {
90 ERROR ("teamspeak2 plugin: malloc failed.");
91 return (-1);
92 }
93 memset (entry, 0, sizeof (vserver_list_t));
95 /* Save data */
96 entry->port = vserver_port;
98 /* Insert to list */
99 if(server_list == NULL) {
100 /* Add the server as the first element */
101 server_list = entry;
102 }
103 else {
104 vserver_list_t *prev;
106 /* Add the server to the end of the list */
107 prev = server_list;
108 while (prev->next != NULL)
109 prev = prev->next;
110 prev->next = entry;
111 }
113 INFO ("teamspeak2 plugin: Registered new vserver: %i", vserver_port);
115 return (0);
116 } /* int tss2_add_vserver */
118 static void tss2_submit_gauge (const char *plugin_instance,
119 const char *type, const char *type_instance,
120 gauge_t value)
121 {
122 /*
123 * Submits a gauge value to the collectd daemon
124 */
125 value_t values[1];
126 value_list_t vl = VALUE_LIST_INIT;
128 values[0].gauge = value;
130 vl.values = values;
131 vl.values_len = 1;
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 sstrncpy (vl.type, type, sizeof (vl.type));
142 if (type_instance != NULL)
143 sstrncpy (vl.type_instance, type_instance,
144 sizeof (vl.type_instance));
146 plugin_dispatch_values (&vl);
147 } /* void tss2_submit_gauge */
149 static void tss2_submit_io (const char *plugin_instance, const char *type,
150 counter_t rx, counter_t tx)
151 {
152 /*
153 * Submits the io rx/tx tuple to the collectd daemon
154 */
155 value_t values[2];
156 value_list_t vl = VALUE_LIST_INIT;
158 values[0].counter = rx;
159 values[1].counter = tx;
161 vl.values = values;
162 vl.values_len = 2;
163 vl.time = time (NULL);
164 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
165 sstrncpy (vl.plugin, "teamspeak2", sizeof (vl.plugin));
167 if (plugin_instance != NULL)
168 sstrncpy (vl.plugin_instance, plugin_instance,
169 sizeof (vl.plugin_instance));
171 sstrncpy (vl.type, type, sizeof (vl.type));
173 plugin_dispatch_values (&vl);
174 } /* void tss2_submit_gauge */
176 static void tss2_close_socket (void)
177 {
178 /*
179 * Closes all sockets
180 */
181 if (global_write_fh != NULL)
182 {
183 fputs ("quit\r\n", global_write_fh);
184 }
186 if (global_read_fh != NULL)
187 {
188 fclose (global_read_fh);
189 global_read_fh = NULL;
190 }
192 if (global_write_fh != NULL)
193 {
194 fclose (global_write_fh);
195 global_write_fh = NULL;
196 }
197 } /* void tss2_close_socket */
199 static int tss2_get_socket (FILE **ret_read_fh, FILE **ret_write_fh)
200 {
201 /*
202 * Returns connected file objects or establishes the connection
203 * if it's not already present
204 */
205 struct addrinfo ai_hints;
206 struct addrinfo *ai_head;
207 struct addrinfo *ai_ptr;
208 int sd = -1;
209 int status;
211 /* Check if we already got opened connections */
212 if ((global_read_fh != NULL) && (global_write_fh != NULL))
213 {
214 /* If so, use them */
215 if (ret_read_fh != NULL)
216 *ret_read_fh = global_read_fh;
217 if (ret_write_fh != NULL)
218 *ret_write_fh = global_write_fh;
219 return (0);
220 }
222 /* Get all addrs for this hostname */
223 memset (&ai_hints, 0, sizeof (ai_hints));
224 #ifdef AI_ADDRCONFIG
225 ai_hints.ai_flags |= AI_ADDRCONFIG;
226 #endif
227 ai_hints.ai_family = AF_UNSPEC;
228 ai_hints.ai_socktype = SOCK_STREAM;
230 status = getaddrinfo ((config_host != NULL) ? config_host : DEFAULT_HOST,
231 (config_port != NULL) ? config_port : DEFAULT_PORT,
232 &ai_hints,
233 &ai_head);
234 if (status != 0)
235 {
236 ERROR ("teamspeak2 plugin: getaddrinfo failed: %s",
237 gai_strerror (status));
238 return (-1);
239 }
241 /* Try all given hosts until we can connect to one */
242 for (ai_ptr = ai_head; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
243 {
244 /* Create socket */
245 sd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype,
246 ai_ptr->ai_protocol);
247 if (sd < 0)
248 {
249 char errbuf[1024];
250 WARNING ("teamspeak2 plugin: socket failed: %s",
251 sstrerror (errno, errbuf, sizeof (errbuf)));
252 continue;
253 }
255 /* Try to connect */
256 status = connect (sd, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
257 if (status != 0)
258 {
259 char errbuf[1024];
260 WARNING ("teamspeak2 plugin: connect failed: %s",
261 sstrerror (errno, errbuf, sizeof (errbuf)));
262 close (sd);
263 continue;
264 }
266 /*
267 * Success, we can break. Don't need more than one connection
268 */
269 break;
270 } /* for (ai_ptr) */
272 freeaddrinfo (ai_head);
274 /* Check if we really got connected */
275 if (sd < 0)
276 return (-1);
278 /* Create file objects from sockets */
279 global_read_fh = fdopen (sd, "r");
280 if (global_read_fh == NULL)
281 {
282 char errbuf[1024];
283 ERROR ("teamspeak2 plugin: fdopen failed: %s",
284 sstrerror (errno, errbuf, sizeof (errbuf)));
285 close (sd);
286 return (-1);
287 }
289 global_write_fh = fdopen (sd, "w");
290 if (global_write_fh == NULL)
291 {
292 char errbuf[1024];
293 ERROR ("teamspeak2 plugin: fdopen failed: %s",
294 sstrerror (errno, errbuf, sizeof (errbuf)));
295 tss2_close_socket ();
296 return (-1);
297 }
299 { /* Check that the server correctly identifies itself. */
300 char buffer[4096];
301 char *buffer_ptr;
303 buffer_ptr = fgets (buffer, sizeof (buffer), global_read_fh);
304 buffer[sizeof (buffer) - 1] = 0;
306 if (memcmp ("[TS]\r\n", buffer, 6) != 0)
307 {
308 ERROR ("teamspeak2 plugin: Unexpected response when connecting "
309 "to server. Expected ``[TS]'', got ``%s''.",
310 buffer);
311 tss2_close_socket ();
312 return (-1);
313 }
314 DEBUG ("teamspeak2 plugin: Server send correct banner, connected!");
315 }
317 /* Copy the new filehandles to the given pointers */
318 if (ret_read_fh != NULL)
319 *ret_read_fh = global_read_fh;
320 if (ret_write_fh != NULL)
321 *ret_write_fh = global_write_fh;
322 return (0);
323 } /* int tss2_get_socket */
325 static int tss2_send_request (FILE *fh, const char *request)
326 {
327 /*
328 * This function puts a request to the server socket
329 */
330 int status;
332 status = fputs (request, fh);
333 if (status < 0)
334 {
335 ERROR ("teamspeak2 plugin: fputs failed.");
336 tss2_close_socket ();
337 return (-1);
338 }
339 fflush (fh);
341 return (0);
342 } /* int tss2_send_request */
344 static int tss2_receive_line (FILE *fh, char *buffer, int buffer_size)
345 {
346 /*
347 * Receive a single line from the given file object
348 */
349 char *temp;
351 /*
352 * fgets is blocking but much easier then doing anything else
353 * TODO: Non-blocking Version would be safer
354 */
355 temp = fgets (buffer, buffer_size, fh);
356 if (temp == NULL)
357 {
358 char errbuf[1024];
359 ERROR ("teamspeak2 plugin: fgets failed: %s",
360 sstrerror (errno, errbuf, sizeof(errbuf)));
361 tss2_close_socket ();
362 return (-1);
363 }
365 buffer[buffer_size - 1] = 0;
366 return (0);
367 } /* int tss2_receive_line */
369 static int tss2_select_vserver (FILE *read_fh, FILE *write_fh, vserver_list_t *vserver)
370 {
371 /*
372 * Tell the server to select the given vserver
373 */
374 char command[128];
375 char response[128];
376 int status;
378 /* Send request */
379 snprintf (command, sizeof (command), "sel %i\r\n", vserver->port);
380 command[sizeof (command) - 1] = 0;
382 status = tss2_send_request (write_fh, command);
383 if (status != 0)
384 {
385 ERROR ("teamspeak2 plugin: tss2_send_request (%s) failed.", command);
386 return (-1);
387 }
389 /* Get answer */
390 status = tss2_receive_line (read_fh, response, sizeof (response));
391 if (status != 0)
392 {
393 ERROR ("teamspeak2 plugin: tss2_receive_line failed.");
394 return (-1);
395 }
396 response[sizeof (response)] = 0;
398 /* Check answer */
399 if ((strncasecmp ("OK", response, 2) == 0)
400 && ((response[2] == 0)
401 || (response[2] == '\n')
402 || (response[2] == '\r')))
403 return (0);
405 ERROR ("teamspeak2 plugin: Command ``%s'' failed. "
406 "Response received from server was: ``%s''.",
407 command, response);
408 return (-1);
409 } /* int tss2_select_vserver */
411 static int tss2_vserver_gapl (FILE *read_fh, FILE *write_fh,
412 vserver_list_t *vserver, gauge_t *ret_value)
413 {
414 /*
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.
418 */
419 gauge_t packet_loss = NAN;
420 int status;
422 status = tss2_send_request (write_fh, "gapl\r\n");
423 if (status != 0)
424 {
425 ERROR("teamspeak2 plugin: tss2_send_request (gapl) failed.");
426 return (-1);
427 }
429 while (42)
430 {
431 char buffer[4096];
432 char *value;
433 char *endptr = NULL;
435 status = tss2_receive_line (read_fh, buffer, sizeof (buffer));
436 if (status != 0)
437 {
438 /* Set to NULL just to make sure noone uses these FHs anymore. */
439 read_fh = NULL;
440 write_fh = NULL;
441 ERROR ("teamspeak2 plugin: tss2_receive_line failed.");
442 return (-1);
443 }
444 buffer[sizeof (buffer)] = 0;
446 if (strncmp ("average_packet_loss=", buffer,
447 strlen ("average_packet_loss=")) == 0)
448 {
449 /* Got average packet loss, now interpret it */
450 value = &buffer[20];
451 /* Replace , with . */
452 while (*value != 0)
453 {
454 if (*value == ',')
455 {
456 *value = '.';
457 break;
458 }
459 value++;
460 }
462 value = &buffer[20];
464 packet_loss = strtod (value, &endptr);
465 if (value == endptr)
466 {
467 /* Failed */
468 WARNING ("teamspeak2 plugin: Could not read average package "
469 "loss from string: %s", buffer);
470 continue;
471 }
472 }
473 else if (strncasecmp ("OK", buffer, 2) == 0)
474 {
475 break;
476 }
477 else if (strncasecmp ("ERROR", buffer, 5) == 0)
478 {
479 ERROR ("teamspeak2 plugin: Server returned an error: %s", buffer);
480 return (-1);
481 }
482 else
483 {
484 WARNING ("teamspeak2 plugin: Server returned unexpected string: %s",
485 buffer);
486 }
487 }
489 *ret_value = packet_loss;
490 return (0);
491 } /* int tss2_vserver_gapl */
493 static int tss2_read_vserver (vserver_list_t *vserver)
494 {
495 /*
496 * Poll information for the given vserver and submit it to collect.
497 * If vserver is NULL the global server information will be queried.
498 */
499 int status;
501 gauge_t users = NAN;
502 gauge_t channels = NAN;
503 gauge_t servers = NAN;
504 counter_t rx_octets = 0;
505 counter_t tx_octets = 0;
506 counter_t rx_packets = 0;
507 counter_t tx_packets = 0;
508 gauge_t packet_loss = NAN;
509 int valid = 0;
511 char plugin_instance[DATA_MAX_NAME_LEN];
513 FILE *read_fh;
514 FILE *write_fh;
516 /* Get the send/receive sockets */
517 status = tss2_get_socket (&read_fh, &write_fh);
518 if (status != 0)
519 {
520 ERROR ("teamspeak2 plugin: tss2_get_socket failed.");
521 return (-1);
522 }
524 if (vserver == NULL)
525 {
526 /* Request global information */
527 memset (plugin_instance, 0, sizeof (plugin_instance));
529 status = tss2_send_request (write_fh, "gi\r\n");
530 }
531 else
532 {
533 /* Request server information */
534 snprintf (plugin_instance, sizeof (plugin_instance), "vserver%i",
535 vserver->port);
536 plugin_instance[sizeof (plugin_instance) - 1] = 0;
538 /* Select the server */
539 status = tss2_select_vserver (read_fh, write_fh, vserver);
540 if (status != 0)
541 return (status);
543 status = tss2_send_request (write_fh, "si\r\n");
544 }
546 if (status != 0)
547 {
548 ERROR ("teamspeak2 plugin: tss2_send_request failed.");
549 return (-1);
550 }
552 /* Loop until break */
553 while (42)
554 {
555 char buffer[4096];
556 char *key;
557 char *value;
558 char *endptr = NULL;
560 /* Read one line of the server's answer */
561 status = tss2_receive_line (read_fh, buffer, sizeof (buffer));
562 if (status != 0)
563 {
564 /* Set to NULL just to make sure noone uses these FHs anymore. */
565 read_fh = NULL;
566 write_fh = NULL;
567 ERROR ("teamspeak2 plugin: tss2_receive_line failed.");
568 break;
569 }
571 if (strncasecmp ("ERROR", buffer, 5) == 0)
572 {
573 ERROR ("teamspeak2 plugin: Server returned an error: %s",
574 buffer);
575 break;
576 }
577 else if (strncasecmp ("OK", buffer, 2) == 0)
578 {
579 break;
580 }
582 /* Split line into key and value */
583 key = strchr (buffer, '_');
584 if (key == NULL)
585 {
586 DEBUG ("teamspeak2 plugin: Cannot parse line: %s", buffer);
587 continue;
588 }
589 key++;
591 /* Evaluate assignment */
592 value = strchr (key, '=');
593 if (value == NULL)
594 {
595 DEBUG ("teamspeak2 plugin: Cannot parse line: %s", buffer);
596 continue;
597 }
598 *value = 0;
599 value++;
601 /* Check for known key and save the given value */
602 /* global info: users_online,
603 * server info: currentusers. */
604 if ((strcmp ("currentusers", key) == 0)
605 || (strcmp ("users_online", key) == 0))
606 {
607 users = strtod (value, &endptr);
608 if (value != endptr)
609 valid |= 0x01;
610 }
611 /* global info: channels,
612 * server info: currentchannels. */
613 else if ((strcmp ("currentchannels", key) == 0)
614 || (strcmp ("channels", key) == 0))
615 {
616 channels = strtod (value, &endptr);
617 if (value != endptr)
618 valid |= 0x40;
619 }
620 /* global only */
621 else if (strcmp ("servers", key) == 0)
622 {
623 servers = strtod (value, &endptr);
624 if (value != endptr)
625 valid |= 0x80;
626 }
627 else if (strcmp ("bytesreceived", key) == 0)
628 {
629 rx_octets = strtoll (value, &endptr, 0);
630 if (value != endptr)
631 valid |= 0x02;
632 }
633 else if (strcmp ("bytessend", key) == 0)
634 {
635 tx_octets = strtoll (value, &endptr, 0);
636 if (value != endptr)
637 valid |= 0x04;
638 }
639 else if (strcmp ("packetsreceived", key) == 0)
640 {
641 rx_packets = strtoll (value, &endptr, 0);
642 if (value != endptr)
643 valid |= 0x08;
644 }
645 else if (strcmp ("packetssend", key) == 0)
646 {
647 tx_packets = strtoll (value, &endptr, 0);
648 if (value != endptr)
649 valid |= 0x10;
650 }
651 else if ((strncmp ("allow_codec_", key, strlen ("allow_codec_")) == 0)
652 || (strncmp ("bwinlast", key, strlen ("bwinlast")) == 0)
653 || (strncmp ("bwoutlast", key, strlen ("bwoutlast")) == 0)
654 || (strncmp ("webpost_", key, strlen ("webpost_")) == 0)
655 || (strcmp ("adminemail", key) == 0)
656 || (strcmp ("clan_server", key) == 0)
657 || (strcmp ("countrynumber", key) == 0)
658 || (strcmp ("id", key) == 0)
659 || (strcmp ("ispname", key) == 0)
660 || (strcmp ("linkurl", key) == 0)
661 || (strcmp ("maxusers", key) == 0)
662 || (strcmp ("name", key) == 0)
663 || (strcmp ("password", key) == 0)
664 || (strcmp ("platform", key) == 0)
665 || (strcmp ("server_platform", key) == 0)
666 || (strcmp ("server_uptime", key) == 0)
667 || (strcmp ("server_version", key) == 0)
668 || (strcmp ("udpport", key) == 0)
669 || (strcmp ("uptime", key) == 0)
670 || (strcmp ("users_maximal", key) == 0)
671 || (strcmp ("welcomemessage", key) == 0))
672 /* ignore */;
673 else
674 {
675 INFO ("teamspeak2 plugin: Unknown key-value-pair: "
676 "key = %s; value = %s;", key, value);
677 }
678 } /* while (42) */
680 /* Collect vserver packet loss rates only if the loop above did not exit
681 * with an error. */
682 if ((status == 0) && (vserver != NULL))
683 {
684 status = tss2_vserver_gapl (read_fh, write_fh, vserver, &packet_loss);
685 if (status == 0)
686 {
687 valid |= 0x20;
688 }
689 else
690 {
691 WARNING ("teamspeak2 plugin: Reading package loss "
692 "for vserver %i failed.", vserver->port);
693 }
694 }
696 if ((valid & 0x01) == 0x01)
697 tss2_submit_gauge (plugin_instance, "users", NULL, users);
699 if ((valid & 0x06) == 0x06)
700 tss2_submit_io (plugin_instance, "io_octets", rx_octets, tx_octets);
702 if ((valid & 0x18) == 0x18)
703 tss2_submit_io (plugin_instance, "io_packets", rx_packets, tx_packets);
705 if ((valid & 0x20) == 0x20)
706 tss2_submit_gauge (plugin_instance, "percent", "packet_loss", packet_loss);
708 if ((valid & 0x40) == 0x40)
709 tss2_submit_gauge (plugin_instance, "gauge", "channels", channels);
711 if ((valid & 0x80) == 0x80)
712 tss2_submit_gauge (plugin_instance, "gauge", "servers", servers);
714 if (valid == 0)
715 return (-1);
716 return (0);
717 } /* int tss2_read_vserver */
719 static int tss2_config (const char *key, const char *value)
720 {
721 /*
722 * Interpret configuration values
723 */
724 if (strcasecmp ("Host", key) == 0)
725 {
726 char *temp;
728 temp = strdup (value);
729 if (temp == NULL)
730 {
731 ERROR("teamspeak2 plugin: strdup failed.");
732 return (1);
733 }
734 sfree (config_host);
735 config_host = temp;
736 }
737 else if (strcasecmp ("Port", key) == 0)
738 {
739 char *temp;
741 temp = strdup (value);
742 if (temp == NULL)
743 {
744 ERROR("teamspeak2 plugin: strdup failed.");
745 return (1);
746 }
747 sfree (config_port);
748 config_port = temp;
749 }
750 else if (strcasecmp ("Server", key) == 0)
751 {
752 /* Server variable found */
753 int status;
755 status = tss2_add_vserver (atoi (value));
756 if (status != 0)
757 return (1);
758 }
759 else
760 {
761 /* Unknown variable found */
762 return (-1);
763 }
765 return 0;
766 } /* int tss2_config */
768 static int tss2_read (void)
769 {
770 /*
771 * Poll function which collects global and vserver information
772 * and submits it to collectd
773 */
774 vserver_list_t *vserver;
775 int success = 0;
776 int status;
778 /* Handle global server variables */
779 status = tss2_read_vserver (NULL);
780 if (status == 0)
781 {
782 success++;
783 }
784 else
785 {
786 WARNING ("teamspeak2 plugin: Reading global server variables failed.");
787 }
789 /* Handle vservers */
790 for (vserver = server_list; vserver != NULL; vserver = vserver->next)
791 {
792 status = tss2_read_vserver (vserver);
793 if (status == 0)
794 {
795 success++;
796 }
797 else
798 {
799 WARNING ("teamspeak2 plugin: Reading statistics "
800 "for vserver %i failed.", vserver->port);
801 continue;
802 }
803 }
805 if (success == 0)
806 return (-1);
807 return (0);
808 } /* int tss2_read */
810 static int tss2_shutdown(void)
811 {
812 /*
813 * Shutdown handler
814 */
815 vserver_list_t *entry;
817 tss2_close_socket ();
819 entry = server_list;
820 server_list = NULL;
821 while (entry != NULL)
822 {
823 vserver_list_t *next;
825 next = entry->next;
826 sfree (entry);
827 entry = next;
828 }
830 /* Get rid of the configuration */
831 sfree (config_host);
832 sfree (config_port);
834 return (0);
835 } /* int tss2_shutdown */
837 void module_register(void)
838 {
839 /*
840 * Mandatory module_register function
841 */
842 plugin_register_config ("teamspeak2", tss2_config,
843 config_keys, config_keys_num);
844 plugin_register_read ("teamspeak2", tss2_read);
845 plugin_register_shutdown ("teamspeak2", tss2_shutdown);
846 } /* void module_register */
848 /* vim: set sw=4 ts=4 : */