f5eb26877fe170c57ed6940342595aede5595fe6
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_list_t vl = VALUE_LIST_INIT;
126 vl.values = &(value_t) { .gauge = value };
127 vl.values_len = 1;
128 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
129 sstrncpy (vl.plugin, "teamspeak2", sizeof (vl.plugin));
131 if (plugin_instance != NULL)
132 sstrncpy (vl.plugin_instance, plugin_instance,
133 sizeof (vl.plugin_instance));
135 sstrncpy (vl.type, type, sizeof (vl.type));
137 if (type_instance != NULL)
138 sstrncpy (vl.type_instance, type_instance,
139 sizeof (vl.type_instance));
141 plugin_dispatch_values (&vl);
142 } /* void tss2_submit_gauge */
144 static void tss2_submit_io (const char *plugin_instance, const char *type,
145 derive_t rx, derive_t tx)
146 {
147 /*
148 * Submits the io rx/tx tuple to the collectd daemon
149 */
150 value_t values[2];
151 value_list_t vl = VALUE_LIST_INIT;
153 values[0].derive = rx;
154 values[1].derive = tx;
156 vl.values = values;
157 vl.values_len = 2;
158 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
159 sstrncpy (vl.plugin, "teamspeak2", sizeof (vl.plugin));
161 if (plugin_instance != NULL)
162 sstrncpy (vl.plugin_instance, plugin_instance,
163 sizeof (vl.plugin_instance));
165 sstrncpy (vl.type, type, sizeof (vl.type));
167 plugin_dispatch_values (&vl);
168 } /* void tss2_submit_gauge */
170 static void tss2_close_socket (void)
171 {
172 /*
173 * Closes all sockets
174 */
175 if (global_write_fh != NULL)
176 {
177 fputs ("quit\r\n", global_write_fh);
178 }
180 if (global_read_fh != NULL)
181 {
182 fclose (global_read_fh);
183 global_read_fh = NULL;
184 }
186 if (global_write_fh != NULL)
187 {
188 fclose (global_write_fh);
189 global_write_fh = NULL;
190 }
191 } /* void tss2_close_socket */
193 static int tss2_get_socket (FILE **ret_read_fh, FILE **ret_write_fh)
194 {
195 /*
196 * Returns connected file objects or establishes the connection
197 * if it's not already present
198 */
199 struct addrinfo *ai_head;
200 int sd = -1;
201 int status;
203 /* Check if we already got opened connections */
204 if ((global_read_fh != NULL) && (global_write_fh != NULL))
205 {
206 /* If so, use them */
207 if (ret_read_fh != NULL)
208 *ret_read_fh = global_read_fh;
209 if (ret_write_fh != NULL)
210 *ret_write_fh = global_write_fh;
211 return (0);
212 }
214 /* Get all addrs for this hostname */
215 struct addrinfo ai_hints = {
216 .ai_family = AF_UNSPEC,
217 .ai_flags = AI_ADDRCONFIG,
218 .ai_socktype = SOCK_STREAM
219 };
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 (struct addrinfo *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 sd = -1;
255 continue;
256 }
258 /*
259 * Success, we can break. Don't need more than one connection
260 */
261 break;
262 } /* for (ai_ptr) */
264 freeaddrinfo (ai_head);
266 /* Check if we really got connected */
267 if (sd < 0)
268 return (-1);
270 /* Create file objects from sockets */
271 global_read_fh = fdopen (sd, "r");
272 if (global_read_fh == NULL)
273 {
274 char errbuf[1024];
275 ERROR ("teamspeak2 plugin: fdopen failed: %s",
276 sstrerror (errno, errbuf, sizeof (errbuf)));
277 close (sd);
278 return (-1);
279 }
281 global_write_fh = fdopen (sd, "w");
282 if (global_write_fh == NULL)
283 {
284 char errbuf[1024];
285 ERROR ("teamspeak2 plugin: fdopen failed: %s",
286 sstrerror (errno, errbuf, sizeof (errbuf)));
287 tss2_close_socket ();
288 return (-1);
289 }
291 { /* Check that the server correctly identifies itself. */
292 char buffer[4096];
293 char *buffer_ptr;
295 buffer_ptr = fgets (buffer, sizeof (buffer), global_read_fh);
296 if (buffer_ptr == NULL)
297 {
298 WARNING ("teamspeak2 plugin: Unexpected EOF received "
299 "from remote host %s:%s.",
300 config_host ? config_host : DEFAULT_HOST,
301 config_port ? config_port : DEFAULT_PORT);
302 }
303 buffer[sizeof (buffer) - 1] = 0;
305 if (memcmp ("[TS]\r\n", buffer, 6) != 0)
306 {
307 ERROR ("teamspeak2 plugin: Unexpected response when connecting "
308 "to server. Expected ``[TS]'', got ``%s''.",
309 buffer);
310 tss2_close_socket ();
311 return (-1);
312 }
313 DEBUG ("teamspeak2 plugin: Server send correct banner, connected!");
314 }
316 /* Copy the new filehandles to the given pointers */
317 if (ret_read_fh != NULL)
318 *ret_read_fh = global_read_fh;
319 if (ret_write_fh != NULL)
320 *ret_write_fh = global_write_fh;
321 return (0);
322 } /* int tss2_get_socket */
324 static int tss2_send_request (FILE *fh, const char *request)
325 {
326 /*
327 * This function puts a request to the server socket
328 */
329 int status;
331 status = fputs (request, fh);
332 if (status < 0)
333 {
334 ERROR ("teamspeak2 plugin: fputs failed.");
335 tss2_close_socket ();
336 return (-1);
337 }
338 fflush (fh);
340 return (0);
341 } /* int tss2_send_request */
343 static int tss2_receive_line (FILE *fh, char *buffer, int buffer_size)
344 {
345 /*
346 * Receive a single line from the given file object
347 */
348 char *temp;
350 /*
351 * fgets is blocking but much easier then doing anything else
352 * TODO: Non-blocking Version would be safer
353 */
354 temp = fgets (buffer, buffer_size, fh);
355 if (temp == NULL)
356 {
357 char errbuf[1024];
358 ERROR ("teamspeak2 plugin: fgets failed: %s",
359 sstrerror (errno, errbuf, sizeof(errbuf)));
360 tss2_close_socket ();
361 return (-1);
362 }
364 buffer[buffer_size - 1] = 0;
365 return (0);
366 } /* int tss2_receive_line */
368 static int tss2_select_vserver (FILE *read_fh, FILE *write_fh, vserver_list_t *vserver)
369 {
370 /*
371 * Tell the server to select the given vserver
372 */
373 char command[128];
374 char response[128];
375 int status;
377 /* Send request */
378 ssnprintf (command, sizeof (command), "sel %i\r\n", vserver->port);
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) - 1] = 0;
396 /* Check answer */
397 if ((strncasecmp ("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 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 status = tss2_send_request (write_fh, "gapl\r\n");
421 if (status != 0)
422 {
423 ERROR("teamspeak2 plugin: tss2_send_request (gapl) failed.");
424 return (-1);
425 }
427 while (42)
428 {
429 char buffer[4096];
430 char *value;
431 char *endptr = NULL;
433 status = tss2_receive_line (read_fh, buffer, sizeof (buffer));
434 if (status != 0)
435 {
436 /* Set to NULL just to make sure no one uses these FHs anymore. */
437 read_fh = NULL;
438 write_fh = NULL;
439 ERROR ("teamspeak2 plugin: tss2_receive_line failed.");
440 return (-1);
441 }
442 buffer[sizeof (buffer) - 1] = 0;
444 if (strncmp ("average_packet_loss=", buffer,
445 strlen ("average_packet_loss=")) == 0)
446 {
447 /* Got average packet loss, now interpret it */
448 value = &buffer[20];
449 /* Replace , with . */
450 while (*value != 0)
451 {
452 if (*value == ',')
453 {
454 *value = '.';
455 break;
456 }
457 value++;
458 }
460 value = &buffer[20];
462 packet_loss = strtod (value, &endptr);
463 if (value == endptr)
464 {
465 /* Failed */
466 WARNING ("teamspeak2 plugin: Could not read average package "
467 "loss from string: %s", buffer);
468 continue;
469 }
470 }
471 else if (strncasecmp ("OK", buffer, 2) == 0)
472 {
473 break;
474 }
475 else if (strncasecmp ("ERROR", buffer, 5) == 0)
476 {
477 ERROR ("teamspeak2 plugin: Server returned an error: %s", buffer);
478 return (-1);
479 }
480 else
481 {
482 WARNING ("teamspeak2 plugin: Server returned unexpected string: %s",
483 buffer);
484 }
485 }
487 *ret_value = packet_loss;
488 return (0);
489 } /* int tss2_vserver_gapl */
491 static int tss2_read_vserver (vserver_list_t *vserver)
492 {
493 /*
494 * Poll information for the given vserver and submit it to collect.
495 * If vserver is NULL the global server information will be queried.
496 */
497 int status;
499 gauge_t users = NAN;
500 gauge_t channels = NAN;
501 gauge_t servers = NAN;
502 derive_t rx_octets = 0;
503 derive_t tx_octets = 0;
504 derive_t rx_packets = 0;
505 derive_t tx_packets = 0;
506 gauge_t packet_loss = NAN;
507 int valid = 0;
509 char plugin_instance[DATA_MAX_NAME_LEN] = { 0 };
511 FILE *read_fh;
512 FILE *write_fh;
514 /* Get the send/receive sockets */
515 status = tss2_get_socket (&read_fh, &write_fh);
516 if (status != 0)
517 {
518 ERROR ("teamspeak2 plugin: tss2_get_socket failed.");
519 return (-1);
520 }
522 if (vserver == NULL)
523 {
524 /* Request global information */
525 status = tss2_send_request (write_fh, "gi\r\n");
526 }
527 else
528 {
529 /* Request server information */
530 ssnprintf (plugin_instance, sizeof (plugin_instance), "vserver%i",
531 vserver->port);
533 /* Select the server */
534 status = tss2_select_vserver (read_fh, write_fh, vserver);
535 if (status != 0)
536 return (status);
538 status = tss2_send_request (write_fh, "si\r\n");
539 }
541 if (status != 0)
542 {
543 ERROR ("teamspeak2 plugin: tss2_send_request failed.");
544 return (-1);
545 }
547 /* Loop until break */
548 while (42)
549 {
550 char buffer[4096];
551 char *key;
552 char *value;
553 char *endptr = NULL;
555 /* Read one line of the server's answer */
556 status = tss2_receive_line (read_fh, buffer, sizeof (buffer));
557 if (status != 0)
558 {
559 /* Set to NULL just to make sure no one uses these FHs anymore. */
560 read_fh = NULL;
561 write_fh = NULL;
562 ERROR ("teamspeak2 plugin: tss2_receive_line failed.");
563 break;
564 }
566 if (strncasecmp ("ERROR", buffer, 5) == 0)
567 {
568 ERROR ("teamspeak2 plugin: Server returned an error: %s",
569 buffer);
570 break;
571 }
572 else if (strncasecmp ("OK", buffer, 2) == 0)
573 {
574 break;
575 }
577 /* Split line into key and value */
578 key = strchr (buffer, '_');
579 if (key == NULL)
580 {
581 DEBUG ("teamspeak2 plugin: Cannot parse line: %s", buffer);
582 continue;
583 }
584 key++;
586 /* Evaluate assignment */
587 value = strchr (key, '=');
588 if (value == NULL)
589 {
590 DEBUG ("teamspeak2 plugin: Cannot parse line: %s", buffer);
591 continue;
592 }
593 *value = 0;
594 value++;
596 /* Check for known key and save the given value */
597 /* global info: users_online,
598 * server info: currentusers. */
599 if ((strcmp ("currentusers", key) == 0)
600 || (strcmp ("users_online", key) == 0))
601 {
602 users = strtod (value, &endptr);
603 if (value != endptr)
604 valid |= 0x01;
605 }
606 /* global info: channels,
607 * server info: currentchannels. */
608 else if ((strcmp ("currentchannels", key) == 0)
609 || (strcmp ("channels", key) == 0))
610 {
611 channels = strtod (value, &endptr);
612 if (value != endptr)
613 valid |= 0x40;
614 }
615 /* global only */
616 else if (strcmp ("servers", key) == 0)
617 {
618 servers = strtod (value, &endptr);
619 if (value != endptr)
620 valid |= 0x80;
621 }
622 else if (strcmp ("bytesreceived", key) == 0)
623 {
624 rx_octets = strtoll (value, &endptr, 0);
625 if (value != endptr)
626 valid |= 0x02;
627 }
628 else if (strcmp ("bytessend", key) == 0)
629 {
630 tx_octets = strtoll (value, &endptr, 0);
631 if (value != endptr)
632 valid |= 0x04;
633 }
634 else if (strcmp ("packetsreceived", key) == 0)
635 {
636 rx_packets = strtoll (value, &endptr, 0);
637 if (value != endptr)
638 valid |= 0x08;
639 }
640 else if (strcmp ("packetssend", key) == 0)
641 {
642 tx_packets = strtoll (value, &endptr, 0);
643 if (value != endptr)
644 valid |= 0x10;
645 }
646 else if ((strncmp ("allow_codec_", key, strlen ("allow_codec_")) == 0)
647 || (strncmp ("bwinlast", key, strlen ("bwinlast")) == 0)
648 || (strncmp ("bwoutlast", key, strlen ("bwoutlast")) == 0)
649 || (strncmp ("webpost_", key, strlen ("webpost_")) == 0)
650 || (strcmp ("adminemail", key) == 0)
651 || (strcmp ("clan_server", key) == 0)
652 || (strcmp ("countrynumber", key) == 0)
653 || (strcmp ("id", key) == 0)
654 || (strcmp ("ispname", key) == 0)
655 || (strcmp ("linkurl", key) == 0)
656 || (strcmp ("maxusers", key) == 0)
657 || (strcmp ("name", key) == 0)
658 || (strcmp ("password", key) == 0)
659 || (strcmp ("platform", key) == 0)
660 || (strcmp ("server_platform", key) == 0)
661 || (strcmp ("server_uptime", key) == 0)
662 || (strcmp ("server_version", key) == 0)
663 || (strcmp ("udpport", key) == 0)
664 || (strcmp ("uptime", key) == 0)
665 || (strcmp ("users_maximal", key) == 0)
666 || (strcmp ("welcomemessage", key) == 0))
667 /* ignore */;
668 else
669 {
670 INFO ("teamspeak2 plugin: Unknown key-value-pair: "
671 "key = %s; value = %s;", key, value);
672 }
673 } /* while (42) */
675 /* Collect vserver packet loss rates only if the loop above did not exit
676 * with an error. */
677 if ((status == 0) && (vserver != NULL))
678 {
679 status = tss2_vserver_gapl (read_fh, write_fh, &packet_loss);
680 if (status == 0)
681 {
682 valid |= 0x20;
683 }
684 else
685 {
686 WARNING ("teamspeak2 plugin: Reading package loss "
687 "for vserver %i failed.", vserver->port);
688 }
689 }
691 if ((valid & 0x01) == 0x01)
692 tss2_submit_gauge (plugin_instance, "users", NULL, users);
694 if ((valid & 0x06) == 0x06)
695 tss2_submit_io (plugin_instance, "io_octets", rx_octets, tx_octets);
697 if ((valid & 0x18) == 0x18)
698 tss2_submit_io (plugin_instance, "io_packets", rx_packets, tx_packets);
700 if ((valid & 0x20) == 0x20)
701 tss2_submit_gauge (plugin_instance, "percent", "packet_loss", packet_loss);
703 if ((valid & 0x40) == 0x40)
704 tss2_submit_gauge (plugin_instance, "gauge", "channels", channels);
706 if ((valid & 0x80) == 0x80)
707 tss2_submit_gauge (plugin_instance, "gauge", "servers", servers);
709 if (valid == 0)
710 return (-1);
711 return (0);
712 } /* int tss2_read_vserver */
714 static int tss2_config (const char *key, const char *value)
715 {
716 /*
717 * Interpret configuration values
718 */
719 if (strcasecmp ("Host", key) == 0)
720 {
721 char *temp;
723 temp = strdup (value);
724 if (temp == NULL)
725 {
726 ERROR("teamspeak2 plugin: strdup failed.");
727 return (1);
728 }
729 sfree (config_host);
730 config_host = temp;
731 }
732 else if (strcasecmp ("Port", key) == 0)
733 {
734 char *temp;
736 temp = strdup (value);
737 if (temp == NULL)
738 {
739 ERROR("teamspeak2 plugin: strdup failed.");
740 return (1);
741 }
742 sfree (config_port);
743 config_port = temp;
744 }
745 else if (strcasecmp ("Server", key) == 0)
746 {
747 /* Server variable found */
748 int status;
750 status = tss2_add_vserver (atoi (value));
751 if (status != 0)
752 return (1);
753 }
754 else
755 {
756 /* Unknown variable found */
757 return (-1);
758 }
760 return 0;
761 } /* int tss2_config */
763 static int tss2_read (void)
764 {
765 /*
766 * Poll function which collects global and vserver information
767 * and submits it to collectd
768 */
769 int success = 0;
770 int status;
772 /* Handle global server variables */
773 status = tss2_read_vserver (NULL);
774 if (status == 0)
775 {
776 success++;
777 }
778 else
779 {
780 WARNING ("teamspeak2 plugin: Reading global server variables failed.");
781 }
783 /* Handle vservers */
784 for (vserver_list_t *vserver = server_list; vserver != NULL; vserver = vserver->next)
785 {
786 status = tss2_read_vserver (vserver);
787 if (status == 0)
788 {
789 success++;
790 }
791 else
792 {
793 WARNING ("teamspeak2 plugin: Reading statistics "
794 "for vserver %i failed.", vserver->port);
795 continue;
796 }
797 }
799 if (success == 0)
800 return (-1);
801 return (0);
802 } /* int tss2_read */
804 static int tss2_shutdown(void)
805 {
806 /*
807 * Shutdown handler
808 */
809 vserver_list_t *entry;
811 tss2_close_socket ();
813 entry = server_list;
814 server_list = NULL;
815 while (entry != NULL)
816 {
817 vserver_list_t *next;
819 next = entry->next;
820 sfree (entry);
821 entry = next;
822 }
824 /* Get rid of the configuration */
825 sfree (config_host);
826 sfree (config_port);
828 return (0);
829 } /* int tss2_shutdown */
831 void module_register(void)
832 {
833 /*
834 * Mandatory module_register function
835 */
836 plugin_register_config ("teamspeak2", tss2_config,
837 config_keys, config_keys_num);
838 plugin_register_read ("teamspeak2", tss2_read);
839 plugin_register_shutdown ("teamspeak2", tss2_shutdown);
840 } /* void module_register */
842 /* vim: set sw=4 ts=4 : */