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 if (type_instance != NULL)
141 sstrncpy (vl.type_instance, type_instance,
142 sizeof (vl.type_instance));
144 plugin_dispatch_values (type, &vl);
145 } /* void tss2_submit_gauge */
147 static void tss2_submit_io (const char *plugin_instance, const char *type,
148 counter_t rx, counter_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].counter = rx;
157 values[1].counter = tx;
159 vl.values = values;
160 vl.values_len = 2;
161 vl.time = time (NULL);
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 plugin_dispatch_values (type, &vl);
170 } /* void tss2_submit_gauge */
172 static void tss2_close_socket (void)
173 {
174 /*
175 * Closes all sockets
176 */
177 if (global_write_fh != NULL)
178 {
179 fputs ("quit\r\n", global_write_fh);
180 }
182 if (global_read_fh != NULL)
183 {
184 fclose (global_read_fh);
185 global_read_fh = NULL;
186 }
188 if (global_write_fh != NULL)
189 {
190 fclose (global_write_fh);
191 global_write_fh = NULL;
192 }
193 } /* void tss2_close_socket */
195 static int tss2_get_socket (FILE **ret_read_fh, FILE **ret_write_fh)
196 {
197 /*
198 * Returns connected file objects or establishes the connection
199 * if it's not already present
200 */
201 struct addrinfo ai_hints;
202 struct addrinfo *ai_head;
203 struct addrinfo *ai_ptr;
204 int sd = -1;
205 int status;
207 /* Check if we already got opened connections */
208 if ((global_read_fh != NULL) && (global_write_fh != NULL))
209 {
210 /* If so, use them */
211 if (ret_read_fh != NULL)
212 *ret_read_fh = global_read_fh;
213 if (ret_write_fh != NULL)
214 *ret_write_fh = global_write_fh;
215 return (0);
216 }
218 /* Get all addrs for this hostname */
219 memset (&ai_hints, 0, sizeof (ai_hints));
220 #ifdef AI_ADDRCONFIG
221 ai_hints.ai_flags |= AI_ADDRCONFIG;
222 #endif
223 ai_hints.ai_family = AF_UNSPEC;
224 ai_hints.ai_socktype = SOCK_STREAM;
226 status = getaddrinfo ((config_host != NULL) ? config_host : DEFAULT_HOST,
227 (config_port != NULL) ? config_port : DEFAULT_PORT,
228 &ai_hints,
229 &ai_head);
230 if (status != 0)
231 {
232 ERROR ("teamspeak2 plugin: getaddrinfo failed: %s",
233 gai_strerror (status));
234 return (-1);
235 }
237 /* Try all given hosts until we can connect to one */
238 for (ai_ptr = ai_head; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
239 {
240 /* Create socket */
241 sd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype,
242 ai_ptr->ai_protocol);
243 if (sd < 0)
244 {
245 char errbuf[1024];
246 WARNING ("teamspeak2 plugin: socket failed: %s",
247 sstrerror (errno, errbuf, sizeof (errbuf)));
248 continue;
249 }
251 /* Try to connect */
252 status = connect (sd, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
253 if (status != 0)
254 {
255 char errbuf[1024];
256 WARNING ("teamspeak2 plugin: connect failed: %s",
257 sstrerror (errno, errbuf, sizeof (errbuf)));
258 close (sd);
259 continue;
260 }
262 /*
263 * Success, we can break. Don't need more than one connection
264 */
265 break;
266 } /* for (ai_ptr) */
268 freeaddrinfo (ai_head);
270 /* Check if we really got connected */
271 if (sd < 0)
272 return (-1);
274 /* Create file objects from sockets */
275 global_read_fh = fdopen (sd, "r");
276 if (global_read_fh == NULL)
277 {
278 char errbuf[1024];
279 ERROR ("teamspeak2 plugin: fdopen failed: %s",
280 sstrerror (errno, errbuf, sizeof (errbuf)));
281 close (sd);
282 return (-1);
283 }
285 global_write_fh = fdopen (sd, "w");
286 if (global_write_fh == NULL)
287 {
288 char errbuf[1024];
289 ERROR ("teamspeak2 plugin: fdopen failed: %s",
290 sstrerror (errno, errbuf, sizeof (errbuf)));
291 tss2_close_socket ();
292 return (-1);
293 }
295 { /* Check that the server correctly identifies itself. */
296 char buffer[4096];
297 char *buffer_ptr;
299 buffer_ptr = fgets (buffer, sizeof (buffer), global_read_fh);
300 buffer[sizeof (buffer) - 1] = 0;
302 if (memcmp ("[TS]\r\n", buffer, 6) != 0)
303 {
304 ERROR ("teamspeak2 plugin: Unexpected response when connecting "
305 "to server. Expected ``[TS]'', got ``%s''.",
306 buffer);
307 tss2_close_socket ();
308 return (-1);
309 }
310 DEBUG ("teamspeak2 plugin: Server send correct banner, connected!");
311 }
313 /* Copy the new filehandles to the given pointers */
314 if (ret_read_fh != NULL)
315 *ret_read_fh = global_read_fh;
316 if (ret_write_fh != NULL)
317 *ret_write_fh = global_write_fh;
318 return (0);
319 } /* int tss2_get_socket */
321 static int tss2_send_request (FILE *fh, const char *request)
322 {
323 /*
324 * This function puts a request to the server socket
325 */
326 int status;
328 status = fputs (request, fh);
329 if (status < 0)
330 {
331 ERROR ("teamspeak2 plugin: fputs failed.");
332 tss2_close_socket ();
333 return (-1);
334 }
335 fflush (fh);
337 return (0);
338 } /* int tss2_send_request */
340 static int tss2_receive_line (FILE *fh, char *buffer, int buffer_size)
341 {
342 /*
343 * Receive a single line from the given file object
344 */
345 char *temp;
347 /*
348 * fgets is blocking but much easier then doing anything else
349 * TODO: Non-blocking Version would be safer
350 */
351 temp = fgets (buffer, buffer_size, fh);
352 if (temp == NULL)
353 {
354 char errbuf[1024];
355 ERROR ("teamspeak2 plugin: fgets failed: %s",
356 sstrerror (errno, errbuf, sizeof(errbuf)));
357 tss2_close_socket ();
358 return (-1);
359 }
361 buffer[buffer_size - 1] = 0;
362 return (0);
363 } /* int tss2_receive_line */
365 static int tss2_select_vserver (FILE *read_fh, FILE *write_fh, vserver_list_t *vserver)
366 {
367 /*
368 * Tell the server to select the given vserver
369 */
370 char command[128];
371 char response[128];
372 int status;
374 DEBUG("teamspeak2 plugin: Select server %i", vserver->port);
376 /* Send request */
377 snprintf (command, sizeof (command), "sel %i\r\n", vserver->port);
378 command[sizeof (command) - 1] = 0;
380 status = tss2_send_request (write_fh, command);
381 if (status != 0)
382 {
383 ERROR ("teamspeak2 plugin: tss2_send_request (%s) failed.", command);
384 return (-1);
385 }
387 /* Get answer */
388 status = tss2_receive_line (read_fh, response, sizeof (response));
389 if (status != 0)
390 {
391 ERROR ("teamspeak2 plugin: tss2_receive_line failed.");
392 return (-1);
393 }
394 response[sizeof (response)] = 0;
396 /* Check answer */
397 if ((strncmp ("OK", response, 2) == 0)
398 && ((response[2] == 0)
399 || (response[2] == '\n')
400 || (response[2] == '\r')))
401 return (0);
403 ERROR ("teamspeak2 plugin: Command ``%s'' failed. "
404 "Response received from server was: ``%s''.",
405 command, response);
406 return (-1);
407 } /* int tss2_select_vserver */
409 static int tss2_vserver_gapl (FILE *read_fh, FILE *write_fh,
410 vserver_list_t *vserver, gauge_t *ret_value)
411 {
412 /*
413 * Reads the vserver's average packet loss and submits it to collectd.
414 * Be sure to run the tss2_read_vserver function before calling this so
415 * the vserver is selected correctly.
416 */
417 gauge_t packet_loss = NAN;
418 int status;
420 DEBUG("teamspeak2 plugin: Get average packet loss (VServer: %i)",
421 vserver->port);
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 noone 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)] = 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 (strncmp ("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 counter_t rx_octets = 0;
504 counter_t tx_octets = 0;
505 counter_t rx_packets = 0;
506 counter_t tx_packets = 0;
507 gauge_t packet_loss = NAN;
508 int valid = 0;
510 char plugin_instance[DATA_MAX_NAME_LEN];
512 FILE *read_fh;
513 FILE *write_fh;
515 /* Get the send/receive sockets */
516 status = tss2_get_socket (&read_fh, &write_fh);
517 if (status != 0)
518 {
519 ERROR ("teamspeak2 plugin: tss2_get_socket failed.");
520 return (-1);
521 }
523 if (vserver == NULL)
524 {
525 /* Request global information */
526 DEBUG("teamspeak2 plugin: Read global server information");
528 memset (plugin_instance, 0, sizeof (plugin_instance));
530 status = tss2_send_request (write_fh, "gi\r\n");
531 }
532 else
533 {
534 /* Request server information */
535 DEBUG("teamspeak2 plugin: Read vserver's %i information!", vserver->port);
537 snprintf (plugin_instance, sizeof (plugin_instance), "vserver%i",
538 vserver->port);
539 plugin_instance[sizeof (plugin_instance) - 1] = 0;
541 /* Select the server */
542 status = tss2_select_vserver (read_fh, write_fh, vserver);
543 if (status != 0)
544 return (status);
546 status = tss2_send_request (write_fh, "si\r\n");
547 }
549 if (status != 0)
550 {
551 ERROR ("teamspeak2 plugin: tss2_send_request failed.");
552 return (-1);
553 }
555 /* Loop until break */
556 while (42)
557 {
558 char buffer[4096];
559 char *key;
560 char *value;
561 char *endptr = NULL;
563 /* Read one line of the server's answer */
564 status = tss2_receive_line (read_fh, buffer, sizeof (buffer));
565 if (status != 0)
566 {
567 /* Set to NULL just to make sure noone uses these FHs anymore. */
568 read_fh = NULL;
569 write_fh = NULL;
570 ERROR ("teamspeak2 plugin: tss2_receive_line failed.");
571 break;
572 }
574 if (strncmp ("ERROR", buffer, 5) == 0)
575 {
576 ERROR ("teamspeak2 plugin: Server returned an error: %s",
577 buffer);
578 break;
579 }
580 else if (strncmp ("OK", buffer, 2) == 0)
581 {
582 break;
583 }
585 /* Split line into key and value */
586 key = strchr (buffer, '_');
587 if (key == NULL)
588 {
589 DEBUG ("teamspeak2 plugin: Cannot parse line: %s", buffer);
590 continue;
591 }
592 key++;
594 /* Evaluate assignment */
595 value = strchr (key, '=');
596 if (value == NULL)
597 {
598 DEBUG ("teamspeak2 plugin: Cannot parse line: %s", buffer);
599 continue;
600 }
601 *value = 0;
602 value++;
604 /* Check for known key and save the given value */
605 if ((strcmp ("currentusers", key) == 0)
606 || (strcmp ("users_online", key) == 0))
607 {
608 users = strtod (value, &endptr);
609 if (value != endptr)
610 valid |= 0x01;
611 }
612 else if (strcmp ("bytesreceived", key) == 0)
613 {
614 rx_octets = strtoll (value, &endptr, 0);
615 if (value != endptr)
616 valid |= 0x02;
617 }
618 else if (strcmp ("bytessend", key) == 0)
619 {
620 tx_octets = strtoll (value, &endptr, 0);
621 if (value != endptr)
622 valid |= 0x04;
623 }
624 else if (strcmp ("packetsreceived", key) == 0)
625 {
626 rx_packets = strtoll (value, &endptr, 0);
627 if (value != endptr)
628 valid |= 0x08;
629 }
630 else if (strcmp ("packetssend", key) == 0)
631 {
632 tx_packets = strtoll (value, &endptr, 0);
633 if (value != endptr)
634 valid |= 0x10;
635 }
636 else if ((strncmp ("allow_codec_", key, strlen ("allow_codec_")) == 0)
637 || (strncmp ("bwinlast", key, strlen ("bwinlast")) == 0)
638 || (strncmp ("bwoutlast", key, strlen ("bwoutlast")) == 0)
639 || (strncmp ("webpost_", key, strlen ("webpost_")) == 0)
640 || (strcmp ("adminemail", key) == 0)
641 || (strcmp ("clan_server", key) == 0)
642 || (strcmp ("countrynumber", key) == 0)
643 || (strcmp ("id", key) == 0)
644 || (strcmp ("ispname", key) == 0)
645 || (strcmp ("linkurl", key) == 0)
646 || (strcmp ("maxusers", key) == 0)
647 || (strcmp ("name", key) == 0)
648 || (strcmp ("password", key) == 0)
649 || (strcmp ("platform", key) == 0)
650 || (strcmp ("server_platform", key) == 0)
651 || (strcmp ("server_uptime", key) == 0)
652 || (strcmp ("server_version", key) == 0)
653 || (strcmp ("udpport", key) == 0)
654 || (strcmp ("uptime", key) == 0)
655 || (strcmp ("users_maximal", key) == 0)
656 || (strcmp ("welcomemessage", key) == 0))
657 /* ignore */;
658 else
659 {
660 INFO ("teamspeak2 plugin: Unknown key-value-pair: "
661 "key = %s; value = %s;", key, value);
662 }
663 } /* while (42) */
665 /* Collect vserver packet loss rates only if the loop above did not exit
666 * with an error. */
667 if ((status == 0) && (vserver != NULL))
668 {
669 status = tss2_vserver_gapl (read_fh, write_fh, vserver, &packet_loss);
670 if (status == 0)
671 {
672 valid |= 0x20;
673 }
674 else
675 {
676 WARNING ("teamspeak2 plugin: Reading package loss "
677 "for vserver %i failed.", vserver->port);
678 }
679 }
681 if ((valid & 0x01) == 0x01)
682 tss2_submit_gauge (plugin_instance, "users", NULL, users);
684 if ((valid & 0x06) == 0x06)
685 tss2_submit_io (plugin_instance, "io_octets", rx_octets, tx_octets);
687 if ((valid & 0x18) == 0x18)
688 tss2_submit_io (plugin_instance, "io_packets", rx_packets, tx_packets);
690 if ((valid & 0x20) == 0x20)
691 tss2_submit_gauge (plugin_instance, "percent", "packet_loss", packet_loss);
693 if (valid == 0)
694 return (-1);
695 return (0);
696 } /* int tss2_read_vserver */
698 static int tss2_config (const char *key, const char *value)
699 {
700 /*
701 * Interpret configuration values
702 */
703 if (strcasecmp ("Host", key) == 0)
704 {
705 char *temp;
707 temp = strdup (value);
708 if (temp == NULL)
709 {
710 ERROR("teamspeak2 plugin: strdup failed.");
711 return (1);
712 }
713 sfree (config_host);
714 config_host = temp;
715 }
716 else if (strcasecmp ("Port", key) == 0)
717 {
718 char *temp;
720 temp = strdup (value);
721 if (temp == NULL)
722 {
723 ERROR("teamspeak2 plugin: strdup failed.");
724 return (1);
725 }
726 sfree (config_port);
727 config_port = temp;
728 }
729 else if (strcasecmp ("Server", key) == 0)
730 {
731 /* Server variable found */
732 int status;
734 status = tss2_add_vserver (atoi (value));
735 if (status != 0)
736 return (1);
737 }
738 else
739 {
740 /* Unknown variable found */
741 return (-1);
742 }
744 return 0;
745 } /* int tss2_config */
747 static int tss2_read (void)
748 {
749 /*
750 * Poll function which collects global and vserver information
751 * and submits it to collectd
752 */
753 vserver_list_t *vserver;
754 int success = 0;
755 int status;
757 DEBUG("teamspeak2 plugin: Poll everything");
759 /* Handle global server variables */
760 status = tss2_read_vserver (NULL);
761 if (status == 0)
762 {
763 success++;
764 }
765 else
766 {
767 WARNING ("teamspeak2 plugin: Reading global server variables failed.");
768 }
770 /* Handle vservers */
771 for (vserver = server_list; vserver != NULL; vserver = vserver->next)
772 {
773 status = tss2_read_vserver (vserver);
774 if (status == 0)
775 {
776 success++;
777 }
778 else
779 {
780 WARNING ("teamspeak2 plugin: Reading statistics "
781 "for vserver %i failed.", vserver->port);
782 continue;
783 }
784 }
785 DEBUG("teamspeak2 plugin: Poll done");
787 if (success == 0)
788 return (-1);
789 return (0);
790 } /* int tss2_read */
792 static int tss2_shutdown(void)
793 {
794 /*
795 * Shutdown handler
796 */
797 vserver_list_t *entry;
799 tss2_close_socket ();
801 entry = server_list;
802 server_list = NULL;
803 while (entry != NULL)
804 {
805 vserver_list_t *next;
807 next = entry->next;
808 sfree (entry);
809 entry = next;
810 }
812 /* Get rid of the configuration */
813 sfree (config_host);
814 sfree (config_port);
816 return (0);
817 } /* int tss2_shutdown */
819 void module_register(void)
820 {
821 /*
822 * Mandatory module_register function
823 */
824 plugin_register_config ("teamspeak2", tss2_config,
825 config_keys, config_keys_num);
826 plugin_register_read ("teamspeak2", tss2_read);
827 plugin_register_shutdown ("teamspeak2", tss2_shutdown);
828 } /* void module_register */
830 /* vim: set sw=4 ts=4 : */