ac8ae0b37c11aaf78356ab43a2000dacc7ef8159
1 /**
2 * collectd - src/teamspeak2.c
3 * Copyright (C) 2008 Stefan Hacker
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; only version 2 of the License is applicable.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 *
18 * Authors:
19 * Stefan Hacker <d0t at dbclan dot de>
20 **/
23 /*
24 * Defines
25 */
27 /* Teamspeak query protocol defines */
28 #define TELNET_BANNER "[TS]\r\n"
29 #define TELNET_BANNER_LENGTH 5
30 #define TELNET_ERROR "ERROR"
31 #define TELNET_OK "OK"
32 #define TELNET_QUIT "quit\r\n"
34 /* Predefined settings */
35 #define TELNET_BUFFSIZE 512
36 #define DEFAULT_HOST "127.0.0.1"
37 #define DEFAULT_PORT 51234
39 /* VServer request defines */
40 #define S_REQUEST "si\r\n"
41 #define S_USERS_ONLINE "server_currentusers="
42 #define S_PACKETS_SEND "server_packetssend="
43 #define S_PACKETS_REC "server_packetsreceived="
44 #define S_BYTES_SEND "server_bytessend="
45 #define S_BYTES_REC "server_bytesreceived="
47 /* Global request defines */
48 #define T_REQUEST "gi\r\n"
49 #define T_USERS_ONLINE "total_users_online="
50 #define T_PACKETS_SEND "total_packetssend="
51 #define T_PACKETS_REC "total_packetsreceived="
52 #define T_BYTES_SEND "total_bytessend="
53 #define T_BYTES_REC "total_bytesreceived="
55 /* Convinience defines */
56 #define SOCKET int
57 #define INVALID_SOCKET 0
60 /*
61 * Includes
62 */
64 #include "collectd.h"
65 #include "common.h"
66 #include "plugin.h"
68 #include <stdio.h>
69 #include <arpa/inet.h>
70 #include <netinet/in.h>
71 #include <sys/socket.h>
72 #include <sys/select.h>
73 #include <sys/types.h>
74 #include <unistd.h>
75 #include <stdlib.h>
77 /*
78 * Variables
79 */
81 /* Server linked list structure */
82 typedef struct server_s {
83 int port;
84 struct server_s *next;
85 } server_t;
86 static server_t *pserver = NULL;
89 /* Host data */
90 static char *config_host = NULL;
91 static int port = DEFAULT_PORT;
93 static SOCKET telnet = INVALID_SOCKET;
94 static FILE *telnet_in = NULL;
97 /* Config data */
98 static const char *config_keys[] =
99 {
100 "Host",
101 "Port",
102 "Server",
103 NULL
104 };
105 static int config_keys_num = 3;
108 /*
109 * Functions
110 */
112 static void add_server(server_t *new_server)
113 {
114 /*
115 * Adds a new server to the linked list
116 */
117 server_t *tmp = NULL;
118 new_server->next = NULL;
120 if(pserver == NULL) {
121 /* Add the server as the first element */
122 pserver = new_server;
123 }
124 else {
125 /* Add the server to the end of the list */
126 tmp = pserver;
127 while(tmp->next != NULL) {
128 tmp = tmp->next;
129 }
130 tmp->next = new_server;
131 }
133 DEBUG("Registered new server '%d'", new_server->port);
134 } /* void add_server */
137 static int do_connect(void)
138 {
139 /*
140 * Tries to establish a connection to the server
141 */
142 struct sockaddr_in addr;
143 char *host;
145 host = (config_host != NULL) ? config_host : DEFAULT_HOST;
147 /* Establish telnet connection */
148 telnet = socket(AF_INET, SOCK_STREAM, 0);
150 addr.sin_family = AF_INET;
151 addr.sin_addr.s_addr = inet_addr(host);
152 addr.sin_port = htons(port);
154 if(connect(telnet, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
155 /* Connection failed */
156 return -1;
157 }
158 return 0;
159 } /* int do_connect */
162 static int do_request(char *request)
163 {
164 /*
165 * Pushes a request
166 */
167 int ret = 0;
168 DEBUG("Send Request: '%s'", request);
170 /* Send the request */
171 if((ret = send(telnet, request, strlen(request), 0))==-1) {
172 /* Send data failed */
173 if (telnet!=INVALID_SOCKET) {
174 close(telnet);
175 telnet = INVALID_SOCKET;
176 }
177 char errbuf[1024];
178 ERROR("teamspeak2: send data to host '%s' failed: %s",
179 config_host,
180 sstrerror(errno, errbuf,
181 sizeof(errbuf)));
182 return -1;
183 }
184 return ret;
185 } /* int do_request */
188 static int do_recv(char *buffer, int buffer_size, long int usecs)
189 {
190 /*
191 * Tries to receive from the connection 'timeout' seconds
192 */
193 int ret = 0;
194 fd_set rset;
195 struct timeval timeout;
197 timeout.tv_sec = 0;
198 timeout.tv_usec = usecs;
200 FD_ZERO(&rset);
201 FD_SET(telnet, &rset);
203 if (select(FD_SETSIZE, &rset, NULL, NULL, &timeout) == -1) {
204 /* Select failed */
205 if (telnet!=INVALID_SOCKET) {
206 close(telnet);
207 telnet = INVALID_SOCKET;
208 }
210 char errbuf[1024];
211 ERROR("teamspeak2: select failed: %s",
212 sstrerror(errno, errbuf,
213 sizeof(errbuf)));
214 return -1;
215 }
216 if (!FD_ISSET(telnet, &rset)) {
217 /* Timeout for answer reached --> disconnect */
218 if (telnet!=INVALID_SOCKET) {
219 close(telnet);
220 telnet = INVALID_SOCKET;
221 }
222 WARNING("teamspeak2: request timed out (closed connection)");
223 return -1;
224 }
225 if ((ret = recv(telnet, buffer, buffer_size, 0)) == -1) {
226 /* Recv failed */
227 if (telnet!=INVALID_SOCKET) {
228 close(telnet);
229 telnet = INVALID_SOCKET;
230 }
232 char errbuf[1024];
233 ERROR("teamspeak2: recv failed: %s",
234 sstrerror(errno, errbuf,
235 sizeof(errbuf)));
236 return -1;
237 }
238 return ret;
239 } /* int do_recv */
242 static int is_eq(char *eq, char *str) {
243 /*
244 * Checks if the given str starts with eq
245 */
246 if (strlen(eq) > strlen(str)) return -1;
247 return strncmp(eq, str, strlen(eq));
248 }
251 static long int eval_eq(char *eq, char *str) {
252 /*
253 * Returns the value written behind the eq string in str as a long int
254 */
255 return strtol((char*)&str[strlen(eq)], NULL, 10);
256 }
259 static int do_recv_line(char *buffer, int buffer_size, long int usecs)
260 {
261 /*
262 * Receives a line from the socket
263 */
265 /*
266 * fgets is blocking but much easier then doing anything else
267 * TODO: Non-blocking Version would be safer
268 */
269 if ((fgets(buffer, buffer_size, telnet_in)) == NULL) {
270 /* Receive line failed */
271 if (telnet != INVALID_SOCKET) {
272 close(telnet);
273 telnet = INVALID_SOCKET;
274 }
276 char errbuf[1024];
277 ERROR("teamspeak2: fgets failed: %s",
278 sstrerror(errno, errbuf,
279 sizeof(errbuf)));
280 return -1;
281 }
282 DEBUG("Line: %s", buffer);
283 return 0;
284 }
287 static int tss2_config(const char *key, const char *value)
288 {
289 /*
290 * Configuration interpreter function
291 */
292 char *phost = NULL;
294 if (strcasecmp(key, "host") == 0) {
295 /* Host variable found*/
296 if ((phost = strdup(value)) == NULL) {
297 char errbuf[1024];
298 ERROR("teamspeak2: strdup failed: %s",
299 sstrerror(errno, errbuf,
300 sizeof(errbuf)));
301 return 1;
302 }
303 sfree (config_host);
304 config_host = phost;
305 }
306 else if (strcasecmp(key, "port") == 0) {
307 /* Port variable found */
308 port = atoi(value);
309 }
310 else if (strcasecmp(key, "server") == 0) {
311 /* Server variable found */
312 server_t *new_server = NULL;
314 if ((new_server = (server_t *)malloc(sizeof(server_t))) == NULL) {
315 char errbuf[1024];
316 ERROR("teamspeak2: malloc failed: %s",
317 sstrerror (errno, errbuf,
318 sizeof (errbuf)));
319 return 1;
320 }
322 new_server->port = atoi(value);
323 add_server((struct server_s*)new_server);
324 }
325 else {
326 /* Unknow variable found */
327 return 1;
328 }
330 return 0;
331 }
334 static int tss2_init(void)
335 {
336 /*
337 * Function to initialize the plugin
338 */
339 char buff[TELNET_BANNER_LENGTH + 1]; /*Prepare banner buffer*/
341 /*Connect to telnet*/
342 DEBUG("teamspeak2: Connecting to '%s:%d'", config_host, port);
343 if (do_connect()!=0) {
344 /* Failed */
345 char errbuf[1024];
346 ERROR("teamspeak2: connect to %s:%d failed: %s",
347 config_host,
348 port,
349 sstrerror(errno, errbuf,
350 sizeof(errbuf)));
351 return 1;
352 }
353 else {
354 DEBUG("teamspeak2: connection established!");
355 }
357 /*Check if this is the real thing*/
358 if (do_recv(buff, sizeof(buff), 1) == -1) {
359 /* Failed */
360 return 1;
361 }
362 DEBUG("teamspeak2: received banner '%s'", buff);
364 if (strcmp(buff, TELNET_BANNER)!=0) {
365 /* Received unexpected banner string */
366 ERROR("teamspeak2: host %s:%d is no teamspeak2 query port",
367 config_host, port);
368 return 1;
369 }
371 /*Alright, we are connected now get a file descriptor*/
372 if ((telnet_in = fdopen(telnet, "r")) == NULL) {
373 /* Failed */
374 char errbuf[1024];
375 ERROR("teamspeak2: fdopen failed",
376 sstrerror(errno, errbuf,
377 sizeof(errbuf)));
378 return 1;
379 }
380 DEBUG("teamspeak2: Connection established");
381 return 0;
382 } /* int tss2_init */
385 static void tss2_submit (gauge_t users,
386 counter_t bytes_send, counter_t bytes_received,
387 counter_t packets_send, counter_t packets_received,
388 char *server)
389 {
390 /*
391 * Function to submit values to collectd
392 */
393 value_t v_users[1];
394 value_t v_octets[2];
395 value_t v_packets[2];
397 value_list_t vl_users = VALUE_LIST_INIT;
398 value_list_t vl_octets = VALUE_LIST_INIT;
399 value_list_t vl_packets = VALUE_LIST_INIT;
401 /*
402 * Dispatch users gauge
403 */
404 v_users[0].gauge = users;
406 vl_users.values = v_users;
407 vl_users.values_len = STATIC_ARRAY_SIZE (v_users);
408 vl_users.time = time (NULL);
411 strcpy(vl_users.host, hostname_g);
412 strcpy(vl_users.plugin, "teamspeak2");
414 if (server != NULL) {
415 /* VServer values */
416 strcpy(vl_users.plugin_instance, "");
417 strncpy(vl_users.type_instance, server, sizeof(vl_users.type_instance));
418 }
420 plugin_dispatch_values ("users", &vl_users);
422 /*
423 * Dispatch octets counter
424 */
425 v_octets[0].counter = bytes_send;
426 v_octets[1].counter = bytes_received;
428 vl_octets.values = v_octets;
429 vl_octets.values_len = STATIC_ARRAY_SIZE (v_octets);
430 vl_octets.time = time (NULL);
432 strcpy(vl_octets.host, hostname_g);
433 strcpy(vl_octets.plugin, "teamspeak2");
435 if (server != NULL) {
436 /* VServer values */
437 strcpy(vl_octets.plugin_instance, "");
438 strncpy(vl_octets.type_instance, server, sizeof(vl_octets.type_instance));
439 }
441 plugin_dispatch_values ("octets", &vl_octets);
443 /*
444 * Dispatch packets counter
445 */
446 v_packets[0].counter = packets_send;
447 v_packets[1].counter = packets_send;
449 vl_packets.values = v_packets;
450 vl_packets.values_len = STATIC_ARRAY_SIZE (v_packets);
451 vl_packets.time = time (NULL);
453 strcpy(vl_packets.host, hostname_g);
454 strcpy(vl_packets.plugin, "teamspeak2");
456 if (server != NULL) {
457 /* VServer values */
458 strcpy(vl_packets.plugin_instance, "");
459 strncpy(vl_packets.type_instance, server, sizeof(vl_packets.type_instance));
460 }
462 plugin_dispatch_values("packets", &vl_packets);
463 } /* void tss2_submit */
466 static int tss2_read(void)
467 {
468 /*
469 * Tries to read the current values from all servers and to submit them
470 */
471 char buff[TELNET_BUFFSIZE];
472 server_t *tmp;
474 /* Variables for received values */
475 int collected = 0;
476 int users_online = 0;
478 long int bytes_received = 0;
479 long int bytes_send = 0;
480 long int packets_received = 0;
481 long int packets_send = 0;
483 /*Check if we are connected*/
484 if ((telnet == INVALID_SOCKET) && (do_connect() != 0)) {
485 /* Disconnected and reconnect failed */
486 char errbuf[1024];
487 ERROR("teamspeak2: reconnect to %s:%d failed: %s",
488 config_host,
489 port,
490 sstrerror(errno, errbuf,
491 sizeof(errbuf)));
492 return -1;
493 }
495 /* Request global server variables */
496 if (do_request(T_REQUEST) == -1) {
497 /* Collect global info failed */
498 ERROR("teamspeak2: Collect global information request failed");
499 return -1;
500 }
502 collected = 0; /* Counts the number of variables found in the reply */
504 for(;;) {
505 /* Request a line with a timeout of 200ms */
506 if (do_recv_line(buff, TELNET_BUFFSIZE, 200000) != 0) {
507 /* Failed */
508 ERROR("teamspeak2: Collect global information failed");
509 return -1;
510 }
512 /*
513 * Collect the received data
514 */
515 if (is_eq(T_USERS_ONLINE, buff) == 0) {
516 /* Number of users online */
517 users_online = (int)eval_eq(T_USERS_ONLINE, buff);
518 DEBUG("users_online: %d", users_online);
519 collected += 1;
520 }
521 else if (is_eq(T_PACKETS_SEND, buff) == 0) {
522 /* Number of packets send */
523 packets_send = eval_eq(T_PACKETS_SEND, buff);
524 DEBUG("packets_send: %ld", packets_send);
525 collected += 1;
526 }
527 else if (is_eq(T_PACKETS_REC, buff) == 0) {
528 /* Number of packets received */
529 packets_received = eval_eq(T_PACKETS_REC, buff);
530 DEBUG("packets_received: %ld", packets_received);
531 collected += 1;
532 }
533 else if (is_eq(T_BYTES_SEND, buff) == 0) {
534 /* Number of bytes send */
535 bytes_send = eval_eq(T_BYTES_SEND, buff);
536 DEBUG("bytes_send: %ld", bytes_send);
537 collected += 1;
538 }
539 else if (is_eq(T_BYTES_REC, buff) == 0) {
540 /* Number of bytes received */
541 bytes_received = eval_eq(T_BYTES_REC, buff);
542 DEBUG("byte_received: %ld", bytes_received);
543 collected += 1;
544 }
545 else if (is_eq(TELNET_OK, buff) == 0) {
546 /* Received end of transmission flag */
547 if (collected < 5) {
548 /* Not all expected values were received */
549 ERROR("teamspeak2: Couldn't collect all values (%d)", collected);
550 return -1;
551 }
552 /*
553 * Everything is fine, let's break out of the loop
554 */
555 break;
556 }
557 else if (is_eq(TELNET_ERROR, buff) == 0) {
558 /* An error occured on the servers' side */
559 ERROR("teamspeak2: host reported error '%s'", buff);
560 return -1;
561 }
562 }
564 /* Forward values to collectd */
565 DEBUG("Full global dataset received");
566 tss2_submit(users_online, bytes_send, bytes_received, packets_send, packets_received, NULL);
568 /* Collect values of servers */
569 tmp = pserver;
570 while(tmp != NULL) {
571 /* Try to select server */
572 sprintf(buff, "sel %d\r\n", tmp->port);
574 if (do_request(buff) == -1) return -1; /* Send the request */
575 if (do_recv_line(buff, TELNET_BUFFSIZE, 200000)!=0) return -1; /* Receive the first line */
577 if (is_eq(buff,TELNET_ERROR) == 0) {
578 /*Could not select server, go to the next one*/
579 WARNING("teamspeak2: Could not select server '%d'", tmp->port);
580 tmp = tmp->next;
581 continue;
582 }
583 else if (is_eq(TELNET_OK, buff) == 0) {
584 /*
585 * VServer selected, now request its information
586 */
587 collected = 0; /* Counts the number of variables found in the reply */
589 if (do_request(S_REQUEST) == -1) {
590 /* Failed */
591 WARNING("teamspeak2: Collect info about server '%d' failed", tmp->port);
592 tmp = tmp->next;
593 continue;
594 }
596 for(;;) {
597 /* Request a line with a timeout of 200ms */
598 if (do_recv_line(buff, TELNET_BUFFSIZE, 200000) !=0 ) {
599 ERROR("teamspeak2: Connection error");
600 return -1;
601 }
603 /*
604 * Collect the received data
605 */
606 if (is_eq(S_USERS_ONLINE, buff) == 0) {
607 /* Number of users online */
608 users_online = (int)eval_eq(S_USERS_ONLINE, buff);
609 collected += 1;
610 }
611 else if (is_eq(S_PACKETS_SEND, buff) == 0) {
612 /* Number of packets send */
613 packets_send = eval_eq(S_PACKETS_SEND, buff);
614 collected += 1;
615 }
616 else if (is_eq(S_PACKETS_REC, buff) == 0) {
617 /* Number of packets received */
618 packets_received = eval_eq(S_PACKETS_REC, buff);
619 collected += 1;
620 }
621 else if (is_eq(S_BYTES_SEND, buff) == 0) {
622 /* Number of bytes send */
623 bytes_send = eval_eq(S_BYTES_SEND, buff);
624 collected += 1;
625 }
626 else if (is_eq(S_BYTES_REC, buff) == 0) {
627 /* Number of bytes received */
628 bytes_received = eval_eq(S_BYTES_REC, buff);
629 collected += 1;
630 }
631 else if (is_eq(TELNET_OK, buff) == 0) {
632 /*
633 * Received end of transmission flag, break the loop
634 */
635 break;
636 }
637 else if (is_eq(TELNET_ERROR, buff) == 0) {
638 /* Error, not good */
639 ERROR("teamspeak2: server '%d' reported error '%s'", tmp->port, buff);
640 return -1;
641 }
642 }
644 if (collected < 5) {
645 /* Not all expected values were received */
646 ERROR("teamspeak2: Couldn't collect all values of server '%d' (%d)", tmp->port, collected);
647 tmp = tmp->next;
648 continue; /* Continue with the next VServer */
649 }
651 /* Forward values to connectd */
652 sprintf(buff,"%d",tmp->port);
653 tss2_submit(users_online, bytes_send, bytes_received, packets_send, packets_received, buff);
655 }
656 else {
657 /*The server send us garbage? wtf???*/
658 ERROR("teamspeak2: Server send garbage");
659 return -1;
660 }
661 tmp = tmp->next;
662 }
664 return 0;
665 } /* int tss2_read */
668 static int tss2_shutdown(void)
669 {
670 /*
671 * Shutdown handler
672 */
673 DEBUG("teamspeak2: Shutdown");
674 server_t *tmp = NULL;
676 /* Close our telnet socket */
677 if (telnet != INVALID_SOCKET) {
678 do_request(TELNET_QUIT);
679 fclose(telnet_in);
680 telnet_in = NULL;
681 telnet = INVALID_SOCKET;
682 }
684 /* Release all allocated memory */
685 while(pserver != NULL) {
686 tmp = pserver;
687 pserver = pserver->next;
688 free(tmp);
689 }
691 /* Get rid of the rest */
692 sfree (config_host);
694 return 0;
695 } /* int tss2_shutdown */
698 void module_register(void)
699 {
700 /*
701 * Module registrator
702 */
703 plugin_register_config("teamspeak2",
704 tss2_config,
705 config_keys,
706 config_keys_num);
708 plugin_register_init("teamspeak2", tss2_init);
709 plugin_register_read("teamspeak2", tss2_read);
710 plugin_register_shutdown("teamspeak2", tss2_shutdown);
711 } /* void module_register */
713 /* vim: set ts=4 : */