2 * collectd - src/teamspeak2.c
3 * Copyright (C) 2008 Stefan Hacker
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.
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.
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
19 * Stefan Hacker <d0t at dbclan dot de>
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 INVALID_SOCKET -1
68 #include <arpa/inet.h>
69 #include <netinet/in.h>
70 #include <sys/socket.h>
71 #include <sys/select.h>
72 #include <sys/types.h>
80 /* Server linked list structure */
81 typedef struct vserver_list_s {
83 struct vserver_list_s *next;
85 static vserver_list_t *server_list = NULL;
89 static char *config_host = NULL;
90 static int config_port = DEFAULT_PORT;
92 static int telnet = INVALID_SOCKET;
93 static FILE *telnet_in = NULL;
97 static const char *config_keys[] =
103 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
109 static void add_server(vserver_list_t *new_server)
112 * Adds a new server to the linked list
114 vserver_list_t *tmp = NULL;
115 new_server->next = NULL;
117 if(server_list == NULL) {
118 /* Add the server as the first element */
119 server_list = new_server;
122 /* Add the server to the end of the list */
124 while(tmp->next != NULL) {
127 tmp->next = new_server;
130 DEBUG("teamspeak2 plugin: Registered new server '%d'", new_server->port);
131 } /* void add_server */
134 static int do_connect(void)
137 * Tries to establish a connection to the server
139 struct sockaddr_in addr;
142 host = (config_host != NULL) ? config_host : DEFAULT_HOST;
144 /* Establish telnet connection */
145 telnet = socket(AF_INET, SOCK_STREAM, 0);
147 addr.sin_family = AF_INET;
148 addr.sin_addr.s_addr = inet_addr(host);
149 addr.sin_port = htons(config_port);
151 if(connect(telnet, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
152 /* Connection failed */
156 } /* int do_connect */
159 static int do_request(char *request)
165 DEBUG("teamspeak2 plugin: Send Request: '%s'", request);
167 /* Send the request */
168 if((ret = send(telnet, request, strlen(request), 0))==-1) {
169 /* Send data failed */
170 if (telnet!=INVALID_SOCKET) {
172 telnet = INVALID_SOCKET;
175 ERROR("teamspeak2 plugin: send data to host '%s' failed: %s",
177 sstrerror(errno, errbuf,
182 } /* int do_request */
185 static int do_recv(char *buffer, int buffer_size, long int usecs)
188 * Tries to receive from the connection 'timeout' seconds
192 struct timeval timeout;
195 timeout.tv_usec = usecs;
198 FD_SET(telnet, &rset);
200 if (select(FD_SETSIZE, &rset, NULL, NULL, &timeout) == -1) {
202 if (telnet!=INVALID_SOCKET) {
204 telnet = INVALID_SOCKET;
208 ERROR("teamspeak2 plugin: select failed: %s",
209 sstrerror(errno, errbuf,
213 if (!FD_ISSET(telnet, &rset)) {
214 /* Timeout for answer reached --> disconnect */
215 if (telnet!=INVALID_SOCKET) {
217 telnet = INVALID_SOCKET;
219 WARNING("teamspeak2 plugin: request timed out (closed connection)");
222 if ((ret = recv(telnet, buffer, buffer_size, 0)) == -1) {
224 if (telnet!=INVALID_SOCKET) {
226 telnet = INVALID_SOCKET;
230 ERROR("teamspeak2 plugin: recv failed: %s",
231 sstrerror(errno, errbuf,
239 static int is_eq(char *eq, char *str) {
241 * Checks if the given str starts with eq
243 if (strlen(eq) > strlen(str)) return -1;
244 return strncmp(eq, str, strlen(eq));
248 static long int eval_eq(char *eq, char *str) {
250 * Returns the value written behind the eq string in str as a long int
252 return strtol((char*)&str[strlen(eq)], NULL, 10);
256 static int do_recv_line(char *buffer, int buffer_size, long int usecs)
259 * Receives a line from the socket
263 * fgets is blocking but much easier then doing anything else
264 * TODO: Non-blocking Version would be safer
266 if ((fgets(buffer, buffer_size, telnet_in)) == NULL) {
267 /* Receive line failed */
268 if (telnet != INVALID_SOCKET) {
270 telnet = INVALID_SOCKET;
274 ERROR("teamspeak2 plugin: fgets failed: %s",
275 sstrerror(errno, errbuf,
279 DEBUG("teamspeak2 plugin: Line: %s", buffer);
284 static int tss2_config(const char *key, const char *value)
287 * Configuration interpreter function
291 if (strcasecmp(key, "host") == 0) {
292 /* Host variable found*/
293 if ((phost = strdup(value)) == NULL) {
295 ERROR("teamspeak2 plugin: strdup failed: %s",
296 sstrerror(errno, errbuf,
303 else if (strcasecmp(key, "port") == 0) {
304 /* Port variable found */
305 config_port = atoi(value);
307 else if (strcasecmp(key, "server") == 0) {
308 /* Server variable found */
309 vserver_list_t *new_server = NULL;
311 if ((new_server = (vserver_list_t *)malloc(sizeof(vserver_list_t))) == NULL) {
313 ERROR("teamspeak2 plugin: malloc failed: %s",
314 sstrerror (errno, errbuf,
319 new_server->port = atoi(value);
320 add_server((vserver_list_t *)new_server);
323 /* Unknow variable found */
331 static int tss2_init(void)
334 * Function to initialize the plugin
336 char buff[TELNET_BANNER_LENGTH + 1]; /*Prepare banner buffer*/
338 /*Connect to telnet*/
339 DEBUG("teamspeak2 plugin: Connecting to '%s:%d'", config_host, config_port);
340 if (do_connect()!=0) {
343 ERROR("teamspeak2 plugin: connect to %s:%d failed: %s",
346 sstrerror(errno, errbuf,
351 DEBUG("teamspeak2 plugin: connection established!");
354 /*Check if this is the real thing*/
355 if (do_recv(buff, sizeof(buff), 1) == -1) {
359 DEBUG("teamspeak2 plugin: received banner '%s'", buff);
361 if (strcmp(buff, TELNET_BANNER)!=0) {
362 /* Received unexpected banner string */
363 ERROR("teamspeak2 plugin: host %s:%d is no teamspeak2 query port",
364 config_host, config_port);
368 /*Alright, we are connected now get a file descriptor*/
369 if ((telnet_in = fdopen(telnet, "r")) == NULL) {
372 ERROR("teamspeak2 plugin: fdopen failed",
373 sstrerror(errno, errbuf,
377 DEBUG("teamspeak2 plugin: Connection established");
379 } /* int tss2_init */
382 static void tss2_submit (gauge_t users,
383 counter_t bytes_send, counter_t bytes_received,
384 counter_t packets_send, counter_t packets_received,
388 * Function to submit values to collectd
392 value_t v_packets[2];
394 value_list_t vl_users = VALUE_LIST_INIT;
395 value_list_t vl_octets = VALUE_LIST_INIT;
396 value_list_t vl_packets = VALUE_LIST_INIT;
399 * Dispatch users gauge
401 v_users[0].gauge = users;
403 vl_users.values = v_users;
404 vl_users.values_len = STATIC_ARRAY_SIZE (v_users);
405 vl_users.time = time (NULL);
408 strcpy(vl_users.host, hostname_g);
409 strcpy(vl_users.plugin, "teamspeak2");
411 if (server != NULL) {
413 strcpy(vl_users.plugin_instance, "");
414 strncpy(vl_users.type_instance, server, sizeof(vl_users.type_instance));
417 plugin_dispatch_values ("users", &vl_users);
420 * Dispatch octets counter
422 v_octets[0].counter = bytes_send;
423 v_octets[1].counter = bytes_received;
425 vl_octets.values = v_octets;
426 vl_octets.values_len = STATIC_ARRAY_SIZE (v_octets);
427 vl_octets.time = time (NULL);
429 strcpy(vl_octets.host, hostname_g);
430 strcpy(vl_octets.plugin, "teamspeak2");
432 if (server != NULL) {
434 strcpy(vl_octets.plugin_instance, "");
435 strncpy(vl_octets.type_instance, server, sizeof(vl_octets.type_instance));
438 plugin_dispatch_values ("octets", &vl_octets);
441 * Dispatch packets counter
443 v_packets[0].counter = packets_send;
444 v_packets[1].counter = packets_send;
446 vl_packets.values = v_packets;
447 vl_packets.values_len = STATIC_ARRAY_SIZE (v_packets);
448 vl_packets.time = time (NULL);
450 strcpy(vl_packets.host, hostname_g);
451 strcpy(vl_packets.plugin, "teamspeak2");
453 if (server != NULL) {
455 strcpy(vl_packets.plugin_instance, "");
456 strncpy(vl_packets.type_instance, server, sizeof(vl_packets.type_instance));
459 plugin_dispatch_values("packets", &vl_packets);
460 } /* void tss2_submit */
463 static int tss2_read(void)
466 * Tries to read the current values from all servers and to submit them
468 char buff[TELNET_BUFFSIZE];
471 /* Variables for received values */
473 int users_online = 0;
475 long int bytes_received = 0;
476 long int bytes_send = 0;
477 long int packets_received = 0;
478 long int packets_send = 0;
480 /*Check if we are connected*/
481 if ((telnet == INVALID_SOCKET) && (do_connect() != 0)) {
482 /* Disconnected and reconnect failed */
484 ERROR("teamspeak2 plugin: reconnect to %s:%d failed: %s",
487 sstrerror(errno, errbuf,
492 /* Request global server variables */
493 if (do_request(T_REQUEST) == -1) {
494 /* Collect global info failed */
495 ERROR("teamspeak2 plugin: Collect global information request failed");
499 collected = 0; /* Counts the number of variables found in the reply */
502 /* Request a line with a timeout of 200ms */
503 if (do_recv_line(buff, TELNET_BUFFSIZE, 200000) != 0) {
505 ERROR("teamspeak2 plugin: Collect global information failed");
510 * Collect the received data
512 if (is_eq(T_USERS_ONLINE, buff) == 0) {
513 /* Number of users online */
514 users_online = (int)eval_eq(T_USERS_ONLINE, buff);
515 DEBUG("teamspeak2 plugin: users_online: %d", users_online);
518 else if (is_eq(T_PACKETS_SEND, buff) == 0) {
519 /* Number of packets send */
520 packets_send = eval_eq(T_PACKETS_SEND, buff);
521 DEBUG("teamspeak2 plugin: packets_send: %ld", packets_send);
524 else if (is_eq(T_PACKETS_REC, buff) == 0) {
525 /* Number of packets received */
526 packets_received = eval_eq(T_PACKETS_REC, buff);
527 DEBUG("teamspeak2 plugin: packets_received: %ld", packets_received);
530 else if (is_eq(T_BYTES_SEND, buff) == 0) {
531 /* Number of bytes send */
532 bytes_send = eval_eq(T_BYTES_SEND, buff);
533 DEBUG("teamspeak2 plugin: bytes_send: %ld", bytes_send);
536 else if (is_eq(T_BYTES_REC, buff) == 0) {
537 /* Number of bytes received */
538 bytes_received = eval_eq(T_BYTES_REC, buff);
539 DEBUG("teamspeak2 plugin: byte_received: %ld", bytes_received);
542 else if (is_eq(TELNET_OK, buff) == 0) {
543 /* Received end of transmission flag */
545 /* Not all expected values were received */
546 ERROR("teamspeak2 plugin: Couldn't collect all values (%d)", collected);
550 * Everything is fine, let's break out of the loop
554 else if (is_eq(TELNET_ERROR, buff) == 0) {
555 /* An error occured on the servers' side */
556 ERROR("teamspeak2 plugin: host reported error '%s'", buff);
561 /* Forward values to collectd */
562 DEBUG("teamspeak2 plugin: Full global dataset received");
563 tss2_submit(users_online, bytes_send, bytes_received, packets_send, packets_received, NULL);
565 /* Collect values of servers */
568 /* Try to select server */
569 sprintf(buff, "sel %d\r\n", tmp->port);
571 if (do_request(buff) == -1) return -1; /* Send the request */
572 if (do_recv_line(buff, TELNET_BUFFSIZE, 200000)!=0) return -1; /* Receive the first line */
574 if (is_eq(buff,TELNET_ERROR) == 0) {
575 /*Could not select server, go to the next one*/
576 WARNING("teamspeak2 plugin: Could not select server '%d'", tmp->port);
580 else if (is_eq(TELNET_OK, buff) == 0) {
582 * VServer selected, now request its information
584 collected = 0; /* Counts the number of variables found in the reply */
586 if (do_request(S_REQUEST) == -1) {
588 WARNING("teamspeak2 plugin: Collect info about server '%d' failed", tmp->port);
594 /* Request a line with a timeout of 200ms */
595 if (do_recv_line(buff, TELNET_BUFFSIZE, 200000) !=0 ) {
596 ERROR("teamspeak2 plugin: Connection error");
601 * Collect the received data
603 if (is_eq(S_USERS_ONLINE, buff) == 0) {
604 /* Number of users online */
605 users_online = (int)eval_eq(S_USERS_ONLINE, buff);
608 else if (is_eq(S_PACKETS_SEND, buff) == 0) {
609 /* Number of packets send */
610 packets_send = eval_eq(S_PACKETS_SEND, buff);
613 else if (is_eq(S_PACKETS_REC, buff) == 0) {
614 /* Number of packets received */
615 packets_received = eval_eq(S_PACKETS_REC, buff);
618 else if (is_eq(S_BYTES_SEND, buff) == 0) {
619 /* Number of bytes send */
620 bytes_send = eval_eq(S_BYTES_SEND, buff);
623 else if (is_eq(S_BYTES_REC, buff) == 0) {
624 /* Number of bytes received */
625 bytes_received = eval_eq(S_BYTES_REC, buff);
628 else if (is_eq(TELNET_OK, buff) == 0) {
630 * Received end of transmission flag, break the loop
634 else if (is_eq(TELNET_ERROR, buff) == 0) {
635 /* Error, not good */
636 ERROR("teamspeak2 plugin: server '%d' reported error '%s'", tmp->port, buff);
642 /* Not all expected values were received */
643 ERROR("teamspeak2 plugin: Couldn't collect all values of server '%d' (%d)", tmp->port, collected);
645 continue; /* Continue with the next VServer */
648 /* Forward values to connectd */
649 sprintf(buff,"%d",tmp->port);
650 tss2_submit(users_online, bytes_send, bytes_received, packets_send, packets_received, buff);
654 /*The server send us garbage? wtf???*/
655 ERROR("teamspeak2 plugin: Server send garbage");
662 } /* int tss2_read */
665 static int tss2_shutdown(void)
670 DEBUG("teamspeak2 plugin: Shutdown");
671 vserver_list_t *tmp = NULL;
673 /* Close our telnet socket */
674 if (telnet != INVALID_SOCKET) {
675 do_request(TELNET_QUIT);
678 telnet = INVALID_SOCKET;
681 /* Release all allocated memory */
682 while(server_list != NULL) {
684 server_list = server_list->next;
688 /* Get rid of the rest */
692 } /* int tss2_shutdown */
695 void module_register(void)
700 plugin_register_config("teamspeak2",
705 plugin_register_init("teamspeak2", tss2_init);
706 plugin_register_read("teamspeak2", tss2_read);
707 plugin_register_shutdown("teamspeak2", tss2_shutdown);
708 } /* void module_register */
710 /* vim: set sw=4 ts=4 : */