Various plugins: Convert more plugins to use "derive" instead of "counter".
[collectd.git] / src / teamspeak2.c
index aa10742..7528406 100644 (file)
@@ -129,23 +129,24 @@ static void tss2_submit_gauge (const char *plugin_instance,
 
        vl.values     = values;
        vl.values_len = 1;
-       vl.time       = time (NULL);
        sstrncpy (vl.host, hostname_g, sizeof (vl.host));
        sstrncpy (vl.plugin, "teamspeak2", sizeof (vl.plugin));
 
        if (plugin_instance != NULL)
                sstrncpy (vl.plugin_instance, plugin_instance,
                                sizeof (vl.plugin_instance));
-       
+
+       sstrncpy (vl.type, type, sizeof (vl.type));
+
        if (type_instance != NULL)
                sstrncpy (vl.type_instance, type_instance,
                                sizeof (vl.type_instance));
        
-       plugin_dispatch_values (type, &vl);
+       plugin_dispatch_values (&vl);
 } /* void tss2_submit_gauge */
 
 static void tss2_submit_io (const char *plugin_instance, const char *type,
-               counter_t rx, counter_t tx)
+               derive_t rx, derive_t tx)
 {
        /*
         * Submits the io rx/tx tuple to the collectd daemon
@@ -153,20 +154,21 @@ static void tss2_submit_io (const char *plugin_instance, const char *type,
        value_t values[2];
        value_list_t vl = VALUE_LIST_INIT;
 
-       values[0].counter = rx;
-       values[1].counter = tx;
+       values[0].derive = rx;
+       values[1].derive = tx;
 
        vl.values     = values;
        vl.values_len = 2;
-       vl.time       = time (NULL);
        sstrncpy (vl.host, hostname_g, sizeof (vl.host));
        sstrncpy (vl.plugin, "teamspeak2", sizeof (vl.plugin));
 
        if (plugin_instance != NULL)
                sstrncpy (vl.plugin_instance, plugin_instance,
                                sizeof (vl.plugin_instance));
-       
-       plugin_dispatch_values (type, &vl);
+
+       sstrncpy (vl.type, type, sizeof (vl.type));
+
+       plugin_dispatch_values (&vl);
 } /* void tss2_submit_gauge */
 
 static void tss2_close_socket (void)
@@ -371,11 +373,8 @@ static int tss2_select_vserver (FILE *read_fh, FILE *write_fh, vserver_list_t *v
        char response[128];
        int status;
 
-       DEBUG("teamspeak2 plugin: Select server %i", vserver->port);
-       
        /* Send request */
-       snprintf (command, sizeof (command), "sel %i\r\n", vserver->port);
-       command[sizeof (command) - 1] = 0;
+       ssnprintf (command, sizeof (command), "sel %i\r\n", vserver->port);
 
        status = tss2_send_request (write_fh, command);
        if (status != 0)
@@ -391,10 +390,10 @@ static int tss2_select_vserver (FILE *read_fh, FILE *write_fh, vserver_list_t *v
                ERROR ("teamspeak2 plugin: tss2_receive_line failed.");
                return (-1);
        }
-       response[sizeof (response)] = 0;
+       response[sizeof (response) - 1] = 0;
 
        /* Check answer */
-       if ((strncmp ("OK", response, 2) == 0)
+       if ((strncasecmp ("OK", response, 2) == 0)
                        && ((response[2] == 0)
                                || (response[2] == '\n')
                                || (response[2] == '\r')))
@@ -407,7 +406,7 @@ static int tss2_select_vserver (FILE *read_fh, FILE *write_fh, vserver_list_t *v
 } /* int tss2_select_vserver */
 
 static int tss2_vserver_gapl (FILE *read_fh, FILE *write_fh,
-               vserver_list_t *vserver, gauge_t *ret_value)
+               gauge_t *ret_value)
 {
        /*
         * Reads the vserver's average packet loss and submits it to collectd.
@@ -417,9 +416,6 @@ static int tss2_vserver_gapl (FILE *read_fh, FILE *write_fh,
        gauge_t packet_loss = NAN;
        int status;
 
-       DEBUG("teamspeak2 plugin: Get average packet loss (VServer: %i)",
-                       vserver->port);
-       
        status = tss2_send_request (write_fh, "gapl\r\n");
        if (status != 0)
        {
@@ -442,7 +438,7 @@ static int tss2_vserver_gapl (FILE *read_fh, FILE *write_fh,
                        ERROR ("teamspeak2 plugin: tss2_receive_line failed.");
                        return (-1);
                }
-               buffer[sizeof (buffer)] = 0;
+               buffer[sizeof (buffer) - 1] = 0;
                
                if (strncmp ("average_packet_loss=", buffer,
                                        strlen ("average_packet_loss=")) == 0)
@@ -471,7 +467,7 @@ static int tss2_vserver_gapl (FILE *read_fh, FILE *write_fh,
                                continue;
                        }
                }
-               else if (strncmp ("OK", buffer, 2) == 0)
+               else if (strncasecmp ("OK", buffer, 2) == 0)
                {
                        break;
                }
@@ -500,10 +496,12 @@ static int tss2_read_vserver (vserver_list_t *vserver)
        int status;
 
        gauge_t users = NAN;
-       counter_t rx_octets = 0;
-       counter_t tx_octets = 0;
-       counter_t rx_packets = 0;
-       counter_t tx_packets = 0;
+       gauge_t channels = NAN;
+       gauge_t servers = NAN;
+       derive_t rx_octets = 0;
+       derive_t tx_octets = 0;
+       derive_t rx_packets = 0;
+       derive_t tx_packets = 0;
        gauge_t packet_loss = NAN;
        int valid = 0;
 
@@ -523,8 +521,6 @@ static int tss2_read_vserver (vserver_list_t *vserver)
        if (vserver == NULL)
        {
                /* Request global information */
-               DEBUG("teamspeak2 plugin: Read global server information");
-       
                memset (plugin_instance, 0, sizeof (plugin_instance));
 
                status = tss2_send_request (write_fh, "gi\r\n");
@@ -532,11 +528,8 @@ static int tss2_read_vserver (vserver_list_t *vserver)
        else
        {
                /* Request server information */
-               DEBUG("teamspeak2 plugin: Read vserver's %i information!", vserver->port);
-       
-               snprintf (plugin_instance, sizeof (plugin_instance), "vserver%i",
+               ssnprintf (plugin_instance, sizeof (plugin_instance), "vserver%i",
                                vserver->port);
-               plugin_instance[sizeof (plugin_instance) - 1] = 0;
 
                /* Select the server */
                status = tss2_select_vserver (read_fh, write_fh, vserver);
@@ -571,13 +564,13 @@ static int tss2_read_vserver (vserver_list_t *vserver)
                        break;
                }
 
-               if (strncmp ("ERROR", buffer, 5) == 0)
+               if (strncasecmp ("ERROR", buffer, 5) == 0)
                {
                        ERROR ("teamspeak2 plugin: Server returned an error: %s",
                                        buffer);
                        break;
                }
-               else if (strncmp ("OK", buffer, 2) == 0)
+               else if (strncasecmp ("OK", buffer, 2) == 0)
                {
                        break;
                }
@@ -602,6 +595,8 @@ static int tss2_read_vserver (vserver_list_t *vserver)
                value++;
 
                /* Check for known key and save the given value */
+               /* global info: users_online,
+                * server info: currentusers. */
                if ((strcmp ("currentusers", key) == 0)
                                || (strcmp ("users_online", key) == 0))
                {
@@ -609,6 +604,22 @@ static int tss2_read_vserver (vserver_list_t *vserver)
                        if (value != endptr)
                                valid |= 0x01;
                }
+               /* global info: channels,
+                * server info: currentchannels. */
+               else if ((strcmp ("currentchannels", key) == 0)
+                               || (strcmp ("channels", key) == 0))
+               {
+                       channels = strtod (value, &endptr);
+                       if (value != endptr)
+                               valid |= 0x40;
+               }
+               /* global only */
+               else if (strcmp ("servers", key) == 0)
+               {
+                       servers = strtod (value, &endptr);
+                       if (value != endptr)
+                               valid |= 0x80;
+               }
                else if (strcmp ("bytesreceived", key) == 0)
                {
                        rx_octets = strtoll (value, &endptr, 0);
@@ -666,7 +677,7 @@ static int tss2_read_vserver (vserver_list_t *vserver)
         * with an error. */
        if ((status == 0) && (vserver != NULL))
        {
-               status = tss2_vserver_gapl (read_fh, write_fh, vserver, &packet_loss);
+               status = tss2_vserver_gapl (read_fh, write_fh, &packet_loss);
                if (status == 0)
                {
                        valid |= 0x20;
@@ -690,6 +701,12 @@ static int tss2_read_vserver (vserver_list_t *vserver)
        if ((valid & 0x20) == 0x20)
                tss2_submit_gauge (plugin_instance, "percent", "packet_loss", packet_loss);
 
+       if ((valid & 0x40) == 0x40)
+               tss2_submit_gauge (plugin_instance, "gauge", "channels", channels);
+
+       if ((valid & 0x80) == 0x80)
+               tss2_submit_gauge (plugin_instance, "gauge", "servers", servers);
+
        if (valid == 0)
                return (-1);
        return (0);
@@ -754,8 +771,6 @@ static int tss2_read (void)
        int success = 0;
        int status;
 
-       DEBUG("teamspeak2 plugin: Poll everything");
-       
        /* Handle global server variables */
        status = tss2_read_vserver (NULL);
        if (status == 0)
@@ -782,7 +797,6 @@ static int tss2_read (void)
                        continue;
                }
        }
-       DEBUG("teamspeak2 plugin: Poll done");
        
        if (success == 0)
                return (-1);