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