2 * collectd - src/tss2.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 */
57 #define INVALID_SOCKET 0
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>
81 /* Server linked list structure */
82 typedef struct server_s {
84 struct server_s *next;
86 static server_t *pserver = NULL;
90 static char *host = DEFAULT_HOST;
91 static int port = DEFAULT_PORT;
93 static SOCKET telnet = INVALID_SOCKET;
94 static FILE *telnet_in = NULL;
98 static const char *config_keys[] =
105 static int config_keys_num = 3;
112 static void add_server(server_t *new_server)
115 * Adds a new server to the linked list
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;
125 /* Add the server to the end of the list */
127 while(tmp->next != NULL) {
130 tmp->next = new_server;
133 DEBUG("Registered new server '%d'", new_server->port);
134 } /* void add_server */
137 static int do_connect(void)
140 * Tries to establish a connection to the server
142 struct sockaddr_in addr;
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(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("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("tss2 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("tss2 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("tss2 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("tss2 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("tss2 plugin: fgets failed: %s",
275 sstrerror(errno, errbuf,
279 DEBUG("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("tss2 plugin: strdup failed: %s",
296 sstrerror(errno, errbuf,
302 else if (strcasecmp(key, "port") == 0) {
303 /* Port variable found */
306 else if (strcasecmp(key, "server") == 0) {
307 /* Server variable found */
308 server_t *new_server = NULL;
310 if ((new_server = (server_t *)malloc(sizeof(server_t))) == NULL) {
312 ERROR("tss2 plugin: malloc failed: %s",
313 sstrerror (errno, errbuf,
318 new_server->port = atoi(value);
319 add_server((struct server_s*)new_server);
322 /* Unknow variable found */
330 static int tss2_init(void)
333 * Function to initialize the plugin
335 char buff[TELNET_BANNER_LENGTH + 1]; /*Prepare banner buffer*/
337 /*Connect to telnet*/
338 DEBUG("tss2 plugin: Connecting to '%s:%d'", host, port);
339 if (do_connect()!=0) {
342 ERROR("tss2 plugin: connect to %s:%d failed: %s",
345 sstrerror(errno, errbuf,
350 DEBUG("tss2 plugin: connection established!")
353 /*Check if this is the real thing*/
354 if (do_recv(buff, sizeof(buff), 1) == -1) {
358 DEBUG("tss2 plugin: received banner '%s'", buff);
360 if (strcmp(buff, TELNET_BANNER)!=0) {
361 /* Received unexpected banner string */
362 ERROR("tss2 plugin: host %s:%d is no teamspeak2 query port",
367 /*Alright, we are connected now get a file descriptor*/
368 if ((telnet_in = fdopen(telnet, "r")) == NULL) {
371 ERROR("tss2 plugin: fdopen failed",
372 sstrerror(errno, errbuf,
376 DEBUG("tss2 plugin: Connection established", host, port);
378 } /* int tss2_init */
381 static void tss2_submit (gauge_t users,
382 counter_t bytes_send, counter_t bytes_received,
383 counter_t packets_send, counter_t packets_received,
387 * Function to submit values to collectd
391 value_t v_packets[2];
393 value_list_t vl_users = VALUE_LIST_INIT;
394 value_list_t vl_octets = VALUE_LIST_INIT;
395 value_list_t vl_packets = VALUE_LIST_INIT;
398 * Dispatch users gauge
400 v_users[0].gauge = users;
402 vl_users.values = v_users;
403 vl_users.values_len = STATIC_ARRAY_SIZE (v_users);
404 vl_users.time = time (NULL);
407 strcpy(vl_users.host, hostname_g);
408 strcpy(vl_users.plugin, "tss2");
410 if (server != NULL) {
412 strcpy(vl_users.plugin_instance, "");
413 strncpy(vl_users.type_instance, server, sizeof(vl_users.type_instance));
416 plugin_dispatch_values ("users", &vl_users);
419 * Dispatch octets counter
421 v_octets[0].counter = bytes_send;
422 v_octets[1].counter = bytes_received;
424 vl_octets.values = v_octets;
425 vl_octets.values_len = STATIC_ARRAY_SIZE (v_octets);
426 vl_octets.time = time (NULL);
428 strcpy(vl_octets.host, hostname_g);
429 strcpy(vl_octets.plugin, "tss2");
431 if (server != NULL) {
433 strcpy(vl_octets.plugin_instance, "");
434 strncpy(vl_octets.type_instance, server, sizeof(vl_octets.type_instance));
437 plugin_dispatch_values ("octets", &vl_octets);
440 * Dispatch packets counter
442 v_packets[0].counter = packets_send;
443 v_packets[1].counter = packets_send;
445 vl_packets.values = v_packets;
446 vl_packets.values_len = STATIC_ARRAY_SIZE (v_packets);
447 vl_packets.time = time (NULL);
449 strcpy(vl_packets.host, hostname_g);
450 strcpy(vl_packets.plugin, "tss2");
452 if (server != NULL) {
454 strcpy(vl_packets.plugin_instance, "");
455 strncpy(vl_packets.type_instance, server, sizeof(vl_packets.type_instance));
458 plugin_dispatch_values("packets", &vl_packets);
459 } /* void tss2_submit */
462 static int tss2_read(void)
465 * Tries to read the current values from all servers and to submit them
467 char buff[TELNET_BUFFSIZE];
470 /* Variables for received values */
472 int users_online = 0;
474 long int bytes_received = 0;
475 long int bytes_send = 0;
476 long int packets_received = 0;
477 long int packets_send = 0;
479 /*Check if we are connected*/
480 if ((telnet == INVALID_SOCKET) && (do_connect() != 0)) {
481 /* Disconnected and reconnect failed */
483 ERROR("tss2 plugin: reconnect to %s:%d failed: %s",
486 sstrerror(errno, errbuf,
491 /* Request global server variables */
492 if (do_request(T_REQUEST) == -1) {
493 /* Collect global info failed */
494 ERROR("tss2 plugin: Collect global information request failed");
498 collected = 0; /* Counts the number of variables found in the reply */
501 /* Request a line with a timeout of 200ms */
502 if (do_recv_line(buff, TELNET_BUFFSIZE, 200000) != 0) {
504 ERROR("tss2 plugin: Collect global information failed");
509 * Collect the received data
511 if (is_eq(T_USERS_ONLINE, buff) == 0) {
512 /* Number of users online */
513 users_online = (int)eval_eq(T_USERS_ONLINE, buff);
514 DEBUG("users_online: %d", users_online);
517 else if (is_eq(T_PACKETS_SEND, buff) == 0) {
518 /* Number of packets send */
519 packets_send = eval_eq(T_PACKETS_SEND, buff);
520 DEBUG("packets_send: %ld", packets_send);
523 else if (is_eq(T_PACKETS_REC, buff) == 0) {
524 /* Number of packets received */
525 packets_received = eval_eq(T_PACKETS_REC, buff);
526 DEBUG("packets_received: %ld", packets_received);
529 else if (is_eq(T_BYTES_SEND, buff) == 0) {
530 /* Number of bytes send */
531 bytes_send = eval_eq(T_BYTES_SEND, buff);
532 DEBUG("bytes_send: %ld", bytes_send);
535 else if (is_eq(T_BYTES_REC, buff) == 0) {
536 /* Number of bytes received */
537 bytes_received = eval_eq(T_BYTES_REC, buff);
538 DEBUG("byte_received: %ld", bytes_received);
541 else if (is_eq(TELNET_OK, buff) == 0) {
542 /* Received end of transmission flag */
544 /* Not all expected values were received */
545 ERROR("tss2 plugin: Couldn't collect all values (%d)", collected);
549 * Everything is fine, let's break out of the loop
553 else if (is_eq(TELNET_ERROR, buff) == 0) {
554 /* An error occured on the servers' side */
555 ERROR("tss2 plugin: host reported error '%s'", buff);
560 /* Forward values to collectd */
561 DEBUG("Full global dataset received");
562 tss2_submit(users_online, bytes_send, bytes_received, packets_send, packets_received, NULL);
564 /* Collect values of servers */
567 /* Try to select server */
568 sprintf(buff, "sel %d\r\n", tmp->port);
570 if (do_request(buff) == -1) return -1; /* Send the request */
571 if (do_recv_line(buff, TELNET_BUFFSIZE, 200000)!=0) return -1; /* Receive the first line */
573 if (is_eq(buff,TELNET_ERROR) == 0) {
574 /*Could not select server, go to the next one*/
575 WARNING("tss2 plugin: Could not select server '%d'", tmp->port);
579 else if (is_eq(TELNET_OK, buff) == 0) {
581 * VServer selected, now request its information
583 collected = 0; /* Counts the number of variables found in the reply */
585 if (do_request(S_REQUEST) == -1) {
587 WARNING("tss2 plugin: Collect info about server '%d' failed", tmp->port);
593 /* Request a line with a timeout of 200ms */
594 if (do_recv_line(buff, TELNET_BUFFSIZE, 200000) !=0 ) {
595 ERROR("tss2 plugin: Connection error");
600 * Collect the received data
602 if (is_eq(S_USERS_ONLINE, buff) == 0) {
603 /* Number of users online */
604 users_online = (int)eval_eq(S_USERS_ONLINE, buff);
607 else if (is_eq(S_PACKETS_SEND, buff) == 0) {
608 /* Number of packets send */
609 packets_send = eval_eq(S_PACKETS_SEND, buff);
612 else if (is_eq(S_PACKETS_REC, buff) == 0) {
613 /* Number of packets received */
614 packets_received = eval_eq(S_PACKETS_REC, buff);
617 else if (is_eq(S_BYTES_SEND, buff) == 0) {
618 /* Number of bytes send */
619 bytes_send = eval_eq(S_BYTES_SEND, buff);
622 else if (is_eq(S_BYTES_REC, buff) == 0) {
623 /* Number of bytes received */
624 bytes_received = eval_eq(S_BYTES_REC, buff);
627 else if (is_eq(TELNET_OK, buff) == 0) {
629 * Received end of transmission flag, break the loop
633 else if (is_eq(TELNET_ERROR, buff) == 0) {
634 /* Error, not good */
635 ERROR("tss2 plugin: server '%d' reported error '%s'", tmp->port, buff);
641 /* Not all expected values were received */
642 ERROR("tss2 plugin: Couldn't collect all values of server '%d' (%d)", tmp->port, collected);
644 continue; /* Continue with the next VServer */
647 /* Forward values to connectd */
648 sprintf(buff,"%d",tmp->port);
649 tss2_submit(users_online, bytes_send, bytes_received, packets_send, packets_received, buff);
653 /*The server send us garbage? wtf???*/
654 ERROR("tss2 plugin: Server send garbage");
661 } /* int tss2_read */
664 static int tss2_shutdown(void)
669 DEBUG("tss2 plugin: Shutdown");
670 server_t *tmp = NULL;
672 /* Close our telnet socket */
673 if (telnet != INVALID_SOCKET) {
674 do_request(TELNET_QUIT);
677 telnet = INVALID_SOCKET;
680 /* Release all allocated memory */
681 while(pserver != NULL) {
683 pserver = pserver->next;
687 /* Get rid of the rest */
688 if (host != DEFAULT_HOST) {
690 host = (char*)DEFAULT_HOST;
694 } /* int tss2_shutdown */
697 void module_register(void)
702 plugin_register_config("tss2",
707 plugin_register_init("tss2", tss2_init);
708 plugin_register_read("tss2", tss2_read);
709 plugin_register_shutdown("tss2", tss2_shutdown);
710 } /* void module_register */