Code

teamspeak2 plugin: Call `tss2_vserver_gapl' from `tss2_read_vserver'.
authorFlorian Forster <octo@leeloo.lan.home.verplant.org>
Sun, 13 Apr 2008 09:57:02 +0000 (11:57 +0200)
committerFlorian Forster <octo@leeloo.lan.home.verplant.org>
Sun, 13 Apr 2008 09:57:02 +0000 (11:57 +0200)
This way the file descriptors can be reused and we can assure that the vserver
has been selected.

src/teamspeak2.c

index 726bf7e303968e49111c51c54ad2fac7c574477b..aa10742cf637923140843a2ef3fec1d8f57d5de4 100644 (file)
@@ -115,7 +115,8 @@ static int tss2_add_vserver (int vserver_port)
        return (0);
 } /* int tss2_add_vserver */
 
-static void tss2_submit_gauge (const char *plugin_instance, const char *type,
+static void tss2_submit_gauge (const char *plugin_instance,
+               const char *type, const char *type_instance,
                gauge_t value)
 {
        /*
@@ -136,6 +137,10 @@ static void tss2_submit_gauge (const char *plugin_instance, const char *type,
                sstrncpy (vl.plugin_instance, plugin_instance,
                                sizeof (vl.plugin_instance));
        
+       if (type_instance != NULL)
+               sstrncpy (vl.type_instance, type_instance,
+                               sizeof (vl.type_instance));
+       
        plugin_dispatch_values (type, &vl);
 } /* void tss2_submit_gauge */
 
@@ -401,36 +406,24 @@ static int tss2_select_vserver (FILE *read_fh, FILE *write_fh, vserver_list_t *v
        return (-1);
 } /* int tss2_select_vserver */
 
-static int tss2_vserver_gapl(vserver_list_t *vserver)
+static int tss2_vserver_gapl (FILE *read_fh, FILE *write_fh,
+               vserver_list_t *vserver, gauge_t *ret_value)
 {
        /*
         * Reads the vserver's average packet loss and submits it to collectd.
         * Be sure to run the tss2_read_vserver function before calling this so
         * the vserver is selected correctly.
         */
-       int status;
-       
-       FILE *read_fh;
-       FILE *write_fh;
-
-       char plugin_instance[DATA_MAX_NAME_LEN];
-       
        gauge_t packet_loss = NAN;
+       int status;
 
-       DEBUG("teamspeak2 plugin: Get average packet loss (VServer: %i)", vserver->port);
-       
-       /* Get the send/receive sockets */
-       status = tss2_get_socket (&read_fh, &write_fh);
-       if (status != 0)
-       {
-               ERROR ("teamspeak2 plugin: tss2_get_socket failed.");
-               return (-1);
-       }
+       DEBUG("teamspeak2 plugin: Get average packet loss (VServer: %i)",
+                       vserver->port);
        
        status = tss2_send_request (write_fh, "gapl\r\n");
        if (status != 0)
        {
-               ERROR("teamspeak2 plugin: tss2_send_request failed. (gapl)");
+               ERROR("teamspeak2 plugin: tss2_send_request (gapl) failed.");
                return (-1);
        }
 
@@ -451,7 +444,8 @@ static int tss2_vserver_gapl(vserver_list_t *vserver)
                }
                buffer[sizeof (buffer)] = 0;
                
-               if (strncmp ("average_packet_loss=", buffer, 20) == 0)
+               if (strncmp ("average_packet_loss=", buffer,
+                                       strlen ("average_packet_loss=")) == 0)
                {
                        /* Got average packet loss, now interpret it */
                        value = &buffer[20];
@@ -468,11 +462,12 @@ static int tss2_vserver_gapl(vserver_list_t *vserver)
                        
                        value = &buffer[20];
                        
-                       packet_loss = strtod(value, &endptr);
+                       packet_loss = strtod (value, &endptr);
                        if (value == endptr)
                        {
                                /* Failed */
-                               WARNING ("teamspeak2 plugin: Could not read average package loss from string: %s", buffer);
+                               WARNING ("teamspeak2 plugin: Could not read average package "
+                                               "loss from string: %s", buffer);
                                continue;
                        }
                }
@@ -480,7 +475,7 @@ static int tss2_vserver_gapl(vserver_list_t *vserver)
                {
                        break;
                }
-               else if(strncasecmp ("ERROR", buffer, 5) == 0)
+               else if (strncasecmp ("ERROR", buffer, 5) == 0)
                {
                        ERROR ("teamspeak2 plugin: Server returned an error: %s", buffer);
                        return (-1);
@@ -492,19 +487,7 @@ static int tss2_vserver_gapl(vserver_list_t *vserver)
                }
        }
        
-       if (packet_loss != NAN)
-       {
-               snprintf (plugin_instance, sizeof (plugin_instance), "vserver%i",
-                               vserver->port);
-               plugin_instance[sizeof (plugin_instance) - 1] = 0;
-               
-               tss2_submit_gauge (plugin_instance, "percent", packet_loss);
-       }
-       else
-       {
-               return (-1);
-       }
-       
+       *ret_value = packet_loss;
        return (0);
 } /* int tss2_vserver_gapl */
 
@@ -521,6 +504,7 @@ static int tss2_read_vserver (vserver_list_t *vserver)
        counter_t tx_octets = 0;
        counter_t rx_packets = 0;
        counter_t tx_packets = 0;
+       gauge_t packet_loss = NAN;
        int valid = 0;
 
        char plugin_instance[DATA_MAX_NAME_LEN];
@@ -678,8 +662,24 @@ static int tss2_read_vserver (vserver_list_t *vserver)
                }
        } /* while (42) */
 
+       /* Collect vserver packet loss rates only if the loop above did not exit
+        * with an error. */
+       if ((status == 0) && (vserver != NULL))
+       {
+               status = tss2_vserver_gapl (read_fh, write_fh, vserver, &packet_loss);
+               if (status == 0)
+               {
+                       valid |= 0x20;
+               }
+               else
+               {
+                       WARNING ("teamspeak2 plugin: Reading package loss "
+                                       "for vserver %i failed.", vserver->port);
+               }
+       }
+
        if ((valid & 0x01) == 0x01)
-               tss2_submit_gauge (plugin_instance, "users", users);
+               tss2_submit_gauge (plugin_instance, "users", NULL, users);
 
        if ((valid & 0x06) == 0x06)
                tss2_submit_io (plugin_instance, "io_octets", rx_octets, tx_octets);
@@ -687,6 +687,9 @@ static int tss2_read_vserver (vserver_list_t *vserver)
        if ((valid & 0x18) == 0x18)
                tss2_submit_io (plugin_instance, "io_packets", rx_packets, tx_packets);
 
+       if ((valid & 0x20) == 0x20)
+               tss2_submit_gauge (plugin_instance, "percent", "packet_loss", packet_loss);
+
        if (valid == 0)
                return (-1);
        return (0);
@@ -778,13 +781,6 @@ static int tss2_read (void)
                                        "for vserver %i failed.", vserver->port);
                        continue;
                }
-               
-               status = tss2_vserver_gapl (vserver);
-               if (status != 0)
-               {
-                       WARNING ("teamspeak2 plugin: Reading package loss "
-                                       "for vserver %i failed.", vserver->port);
-               }
        }
        DEBUG("teamspeak2 plugin: Poll done");