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 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 counter_t rx, counter_t tx)
150 {
151 /*
152 * Submits the io rx/tx tuple to the collectd daemon
153 */
154 value_t values[2];
155 value_list_t vl = VALUE_LIST_INIT;
157 values[0].counter = rx;
158 values[1].counter = tx;
160 vl.values = values;
161 vl.values_len = 2;
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)
175 {
176 /*
177 * Closes all sockets
178 */
179 if (global_write_fh != NULL)
180 {
181 fputs ("quit\r\n", global_write_fh);
182 }
184 if (global_read_fh != NULL)
185 {
186 fclose (global_read_fh);
187 global_read_fh = NULL;
188 }
190 if (global_write_fh != NULL)
191 {
192 fclose (global_write_fh);
193 global_write_fh = NULL;
194 }
195 } /* void tss2_close_socket */
197 static int tss2_get_socket (FILE **ret_read_fh, FILE **ret_write_fh)
198 {
199 /*
200 * Returns connected file objects or establishes the connection
201 * if it's not already present
202 */
203 struct addrinfo ai_hints;
204 struct addrinfo *ai_head;
205 struct addrinfo *ai_ptr;
206 int sd = -1;
207 int status;
209 /* Check if we already got opened connections */
210 if ((global_read_fh != NULL) && (global_write_fh != NULL))
211 {
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;
217 return (0);
218 }
220 /* Get all addrs for this hostname */
221 memset (&ai_hints, 0, sizeof (ai_hints));
222 #ifdef AI_ADDRCONFIG
223 ai_hints.ai_flags |= AI_ADDRCONFIG;
224 #endif
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,
230 &ai_hints,
231 &ai_head);
232 if (status != 0)
233 {
234 ERROR ("teamspeak2 plugin: getaddrinfo failed: %s",
235 gai_strerror (status));
236 return (-1);
237 }
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)
241 {
242 /* Create socket */
243 sd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype,
244 ai_ptr->ai_protocol);
245 if (sd < 0)
246 {
247 char errbuf[1024];
248 WARNING ("teamspeak2 plugin: socket failed: %s",
249 sstrerror (errno, errbuf, sizeof (errbuf)));
250 continue;
251 }
253 /* Try to connect */
254 status = connect (sd, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
255 if (status != 0)
256 {
257 char errbuf[1024];
258 WARNING ("teamspeak2 plugin: connect failed: %s",
259 sstrerror (errno, errbuf, sizeof (errbuf)));
260 close (sd);
261 continue;
262 }
264 /*
265 * Success, we can break. Don't need more than one connection
266 */
267 break;
268 } /* for (ai_ptr) */
270 freeaddrinfo (ai_head);
272 /* Check if we really got connected */
273 if (sd < 0)
274 return (-1);
276 /* Create file objects from sockets */
277 global_read_fh = fdopen (sd, "r");
278 if (global_read_fh == NULL)
279 {
280 char errbuf[1024];
281 ERROR ("teamspeak2 plugin: fdopen failed: %s",
282 sstrerror (errno, errbuf, sizeof (errbuf)));
283 close (sd);
284 return (-1);
285 }
287 global_write_fh = fdopen (sd, "w");
288 if (global_write_fh == NULL)
289 {
290 char errbuf[1024];
291 ERROR ("teamspeak2 plugin: fdopen failed: %s",
292 sstrerror (errno, errbuf, sizeof (errbuf)));
293 tss2_close_socket ();
294 return (-1);
295 }
297 { /* Check that the server correctly identifies itself. */
298 char buffer[4096];
299 char *buffer_ptr;
301 buffer_ptr = fgets (buffer, sizeof (buffer), global_read_fh);
302 buffer[sizeof (buffer) - 1] = 0;
304 if (memcmp ("[TS]\r\n", buffer, 6) != 0)
305 {
306 ERROR ("teamspeak2 plugin: Unexpected response when connecting "
307 "to server. Expected ``[TS]'', got ``%s''.",
308 buffer);
309 tss2_close_socket ();
310 return (-1);
311 }
312 DEBUG ("teamspeak2 plugin: Server send correct banner, connected!");
313 }
315 /* Copy the new filehandles to the given pointers */
316 if (ret_read_fh != NULL)
317 *ret_read_fh = global_read_fh;
318 if (ret_write_fh != NULL)
319 *ret_write_fh = global_write_fh;
320 return (0);
321 } /* int tss2_get_socket */
323 static int tss2_send_request (FILE *fh, const char *request)
324 {
325 /*
326 * This function puts a request to the server socket
327 */
328 int status;
330 status = fputs (request, fh);
331 if (status < 0)
332 {
333 ERROR ("teamspeak2 plugin: fputs failed.");
334 tss2_close_socket ();
335 return (-1);
336 }
337 fflush (fh);
339 return (0);
340 } /* int tss2_send_request */
342 static int tss2_receive_line (FILE *fh, char *buffer, int buffer_size)
343 {
344 /*
345 * Receive a single line from the given file object
346 */
347 char *temp;
349 /*
350 * fgets is blocking but much easier then doing anything else
351 * TODO: Non-blocking Version would be safer
352 */
353 temp = fgets (buffer, buffer_size, fh);
354 if (temp == NULL)
355 {
356 char errbuf[1024];
357 ERROR ("teamspeak2 plugin: fgets failed: %s",
358 sstrerror (errno, errbuf, sizeof(errbuf)));
359 tss2_close_socket ();
360 return (-1);
361 }
363 buffer[buffer_size - 1] = 0;
364 return (0);
365 } /* int tss2_receive_line */
367 static int tss2_select_vserver (FILE *read_fh, FILE *write_fh, vserver_list_t *vserver)
368 {
369 /*
370 * Tell the server to select the given vserver
371 */
372 char command[128];
373 char response[128];
374 int status;
376 /* Send request */
377 ssnprintf (command, sizeof (command), "sel %i\r\n", vserver->port);
379 status = tss2_send_request (write_fh, command);
380 if (status != 0)
381 {
382 ERROR ("teamspeak2 plugin: tss2_send_request (%s) failed.", command);
383 return (-1);
384 }
386 /* Get answer */
387 status = tss2_receive_line (read_fh, response, sizeof (response));
388 if (status != 0)
389 {
390 ERROR ("teamspeak2 plugin: tss2_receive_line failed.");
391 return (-1);
392 }
393 response[sizeof (response) - 1] = 0;
395 /* Check answer */
396 if ((strncasecmp ("OK", response, 2) == 0)
397 && ((response[2] == 0)
398 || (response[2] == '\n')
399 || (response[2] == '\r')))
400 return (0);
402 ERROR ("teamspeak2 plugin: Command ``%s'' failed. "
403 "Response received from server was: ``%s''.",
404 command, response);
405 return (-1);
406 } /* int tss2_select_vserver */
408 static int tss2_vserver_gapl (FILE *read_fh, FILE *write_fh,
409 gauge_t *ret_value)
410 {
411 /*
412 * Reads the vserver's average packet loss and submits it to collectd.
413 * Be sure to run the tss2_read_vserver function before calling this so
414 * the vserver is selected correctly.
415 */
416 gauge_t packet_loss = NAN;
417 int status;
419 status = tss2_send_request (write_fh, "gapl\r\n");
420 if (status != 0)
421 {
422 ERROR("teamspeak2 plugin: tss2_send_request (gapl) failed.");
423 return (-1);
424 }
426 while (42)
427 {
428 char buffer[4096];
429 char *value;
430 char *endptr = NULL;
432 status = tss2_receive_line (read_fh, buffer, sizeof (buffer));
433 if (status != 0)
434 {
435 /* Set to NULL just to make sure noone uses these FHs anymore. */
436 read_fh = NULL;
437 write_fh = NULL;
438 ERROR ("teamspeak2 plugin: tss2_receive_line failed.");
439 return (-1);
440 }
441 buffer[sizeof (buffer) - 1] = 0;
443 if (strncmp ("average_packet_loss=", buffer,
444 strlen ("average_packet_loss=")) == 0)
445 {
446 /* Got average packet loss, now interpret it */
447 value = &buffer[20];
448 /* Replace , with . */
449 while (*value != 0)
450 {
451 if (*value == ',')
452 {
453 *value = '.';
454 break;
455 }
456 value++;
457 }
459 value = &buffer[20];
461 packet_loss = strtod (value, &endptr);
462 if (value == endptr)
463 {
464 /* Failed */
465 WARNING ("teamspeak2 plugin: Could not read average package "
466 "loss from string: %s", buffer);
467 continue;
468 }
469 }
470 else if (strncasecmp ("OK", buffer, 2) == 0)
471 {
472 break;
473 }
474 else if (strncasecmp ("ERROR", buffer, 5) == 0)
475 {
476 ERROR ("teamspeak2 plugin: Server returned an error: %s", buffer);
477 return (-1);
478 }
479 else
480 {
481 WARNING ("teamspeak2 plugin: Server returned unexpected string: %s",
482 buffer);
483 }
484 }
486 *ret_value = packet_loss;
487 return (0);
488 } /* int tss2_vserver_gapl */
490 static int tss2_read_vserver (vserver_list_t *vserver)
491 {
492 /*
493 * Poll information for the given vserver and submit it to collect.
494 * If vserver is NULL the global server information will be queried.
495 */
496 int status;
498 gauge_t users = NAN;
499 gauge_t channels = NAN;
500 gauge_t servers = NAN;
501 counter_t rx_octets = 0;
502 counter_t tx_octets = 0;
503 counter_t rx_packets = 0;
504 counter_t tx_packets = 0;
505 gauge_t packet_loss = NAN;
506 int valid = 0;
508 char plugin_instance[DATA_MAX_NAME_LEN];
510 FILE *read_fh;
511 FILE *write_fh;
513 /* Get the send/receive sockets */
514 status = tss2_get_socket (&read_fh, &write_fh);
515 if (status != 0)
516 {
517 ERROR ("teamspeak2 plugin: tss2_get_socket failed.");
518 return (-1);
519 }
521 if (vserver == NULL)
522 {
523 /* Request global information */
524 memset (plugin_instance, 0, sizeof (plugin_instance));
526 status = tss2_send_request (write_fh, "gi\r\n");
527 }
528 else
529 {
530 /* Request server information */
531 ssnprintf (plugin_instance, sizeof (plugin_instance), "vserver%i",
532 vserver->port);
534 /* Select the server */
535 status = tss2_select_vserver (read_fh, write_fh, vserver);
536 if (status != 0)
537 return (status);
539 status = tss2_send_request (write_fh, "si\r\n");
540 }
542 if (status != 0)
543 {
544 ERROR ("teamspeak2 plugin: tss2_send_request failed.");
545 return (-1);
546 }
548 /* Loop until break */
549 while (42)
550 {
551 char buffer[4096];
552 char *key;
553 char *value;
554 char *endptr = NULL;
556 /* Read one line of the server's answer */
557 status = tss2_receive_line (read_fh, buffer, sizeof (buffer));
558 if (status != 0)
559 {
560 /* Set to NULL just to make sure noone uses these FHs anymore. */
561 read_fh = NULL;
562 write_fh = NULL;
563 ERROR ("teamspeak2 plugin: tss2_receive_line failed.");
564 break;
565 }
567 if (strncasecmp ("ERROR", buffer, 5) == 0)
568 {
569 ERROR ("teamspeak2 plugin: Server returned an error: %s",
570 buffer);
571 break;
572 }
573 else if (strncasecmp ("OK", buffer, 2) == 0)
574 {
575 break;
576 }
578 /* Split line into key and value */
579 key = strchr (buffer, '_');
580 if (key == NULL)
581 {
582 DEBUG ("teamspeak2 plugin: Cannot parse line: %s", buffer);
583 continue;
584 }
585 key++;
587 /* Evaluate assignment */
588 value = strchr (key, '=');
589 if (value == NULL)
590 {
591 DEBUG ("teamspeak2 plugin: Cannot parse line: %s", buffer);
592 continue;
593 }
594 *value = 0;
595 value++;
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))
602 {
603 users = strtod (value, &endptr);
604 if (value != endptr)
605 valid |= 0x01;
606 }
607 /* global info: channels,
608 * server info: currentchannels. */
609 else if ((strcmp ("currentchannels", key) == 0)
610 || (strcmp ("channels", key) == 0))
611 {
612 channels = strtod (value, &endptr);
613 if (value != endptr)
614 valid |= 0x40;
615 }
616 /* global only */
617 else if (strcmp ("servers", key) == 0)
618 {
619 servers = strtod (value, &endptr);
620 if (value != endptr)
621 valid |= 0x80;
622 }
623 else if (strcmp ("bytesreceived", key) == 0)
624 {
625 rx_octets = strtoll (value, &endptr, 0);
626 if (value != endptr)
627 valid |= 0x02;
628 }
629 else if (strcmp ("bytessend", key) == 0)
630 {
631 tx_octets = strtoll (value, &endptr, 0);
632 if (value != endptr)
633 valid |= 0x04;
634 }
635 else if (strcmp ("packetsreceived", key) == 0)
636 {
637 rx_packets = strtoll (value, &endptr, 0);
638 if (value != endptr)
639 valid |= 0x08;
640 }
641 else if (strcmp ("packetssend", key) == 0)
642 {
643 tx_packets = strtoll (value, &endptr, 0);
644 if (value != endptr)
645 valid |= 0x10;
646 }
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))
668 /* ignore */;
669 else
670 {
671 INFO ("teamspeak2 plugin: Unknown key-value-pair: "
672 "key = %s; value = %s;", key, value);
673 }
674 } /* while (42) */
676 /* Collect vserver packet loss rates only if the loop above did not exit
677 * with an error. */
678 if ((status == 0) && (vserver != NULL))
679 {
680 status = tss2_vserver_gapl (read_fh, write_fh, &packet_loss);
681 if (status == 0)
682 {
683 valid |= 0x20;
684 }
685 else
686 {
687 WARNING ("teamspeak2 plugin: Reading package loss "
688 "for vserver %i failed.", vserver->port);
689 }
690 }
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);
710 if (valid == 0)
711 return (-1);
712 return (0);
713 } /* int tss2_read_vserver */
715 static int tss2_config (const char *key, const char *value)
716 {
717 /*
718 * Interpret configuration values
719 */
720 if (strcasecmp ("Host", key) == 0)
721 {
722 char *temp;
724 temp = strdup (value);
725 if (temp == NULL)
726 {
727 ERROR("teamspeak2 plugin: strdup failed.");
728 return (1);
729 }
730 sfree (config_host);
731 config_host = temp;
732 }
733 else if (strcasecmp ("Port", key) == 0)
734 {
735 char *temp;
737 temp = strdup (value);
738 if (temp == NULL)
739 {
740 ERROR("teamspeak2 plugin: strdup failed.");
741 return (1);
742 }
743 sfree (config_port);
744 config_port = temp;
745 }
746 else if (strcasecmp ("Server", key) == 0)
747 {
748 /* Server variable found */
749 int status;
751 status = tss2_add_vserver (atoi (value));
752 if (status != 0)
753 return (1);
754 }
755 else
756 {
757 /* Unknown variable found */
758 return (-1);
759 }
761 return 0;
762 } /* int tss2_config */
764 static int tss2_read (void)
765 {
766 /*
767 * Poll function which collects global and vserver information
768 * and submits it to collectd
769 */
770 vserver_list_t *vserver;
771 int success = 0;
772 int status;
774 /* Handle global server variables */
775 status = tss2_read_vserver (NULL);
776 if (status == 0)
777 {
778 success++;
779 }
780 else
781 {
782 WARNING ("teamspeak2 plugin: Reading global server variables failed.");
783 }
785 /* Handle vservers */
786 for (vserver = server_list; vserver != NULL; vserver = vserver->next)
787 {
788 status = tss2_read_vserver (vserver);
789 if (status == 0)
790 {
791 success++;
792 }
793 else
794 {
795 WARNING ("teamspeak2 plugin: Reading statistics "
796 "for vserver %i failed.", vserver->port);
797 continue;
798 }
799 }
801 if (success == 0)
802 return (-1);
803 return (0);
804 } /* int tss2_read */
806 static int tss2_shutdown(void)
807 {
808 /*
809 * Shutdown handler
810 */
811 vserver_list_t *entry;
813 tss2_close_socket ();
815 entry = server_list;
816 server_list = NULL;
817 while (entry != NULL)
818 {
819 vserver_list_t *next;
821 next = entry->next;
822 sfree (entry);
823 entry = next;
824 }
826 /* Get rid of the configuration */
827 sfree (config_host);
828 sfree (config_port);
830 return (0);
831 } /* int tss2_shutdown */
833 void module_register(void)
834 {
835 /*
836 * Mandatory module_register function
837 */
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 : */