Code

Bugfix that made collectd inflooping if no openvpn connection
[collectd.git] / src / openvpn.c
index 1ac50830ceea5fb8f7cc4bdc5c21f68a5781a104..c3d5916ea849ab84c0b138212ec66d2c0c5e879f 100644 (file)
@@ -52,12 +52,16 @@ static int vpn_num = 0;
 
 static int store_compression = 1;
 static int new_naming_schema = 0;
+static int number_connectedusers  = 0;
+static int number_connectedusers_only  = 0;
 
 static const char *config_keys[] =
 {
        "StatusFile",
        "Compression",
-       "ImprovedNamingSchema"
+       "ImprovedNamingSchema",
+ "AggregateUsers",
+ "OnlyAggregateUsers"
 };
 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
 
@@ -85,6 +89,27 @@ static int openvpn_strsplit (char *string, char **fields, size_t size)
        return (i);
 } /* int openvpn_strsplit */
 
+/* dispatches number of users */
+static void numusers_submit (char *pinst, char *tinst, gauge_t value)
+{
+       value_t values[1];
+       value_list_t vl = VALUE_LIST_INIT;
+
+       values[0].gauge = value;
+
+       vl.values = values;
+       vl.values_len = STATIC_ARRAY_SIZE (values);
+       sstrncpy (vl.host, hostname_g, sizeof (vl.host));
+       sstrncpy (vl.plugin, "openvpn", sizeof (vl.plugin));
+       sstrncpy (vl.type, "users", sizeof (vl.type));
+       if (pinst != NULL)
+               sstrncpy (vl.plugin_instance, pinst, sizeof (vl.plugin_instance));
+       if (tinst != NULL)
+               sstrncpy (vl.type_instance, tinst, sizeof (vl.type_instance));
+
+       plugin_dispatch_values (&vl);
+} /* void numusers_submit */
+
 /* dispatches stats about traffic (TCP or UDP) generated by the tunnel per single endpoint */
 static void iostats_submit (char *pinst, char *tinst, counter_t rx, counter_t tx)
 {
@@ -238,6 +263,7 @@ static int multi1_read (char *name, FILE *fh)
        char buffer[1024];
        char *fields[10];
        int  fields_num, read = 0, found_header = 0;
+       long long sum_users = 0;
 
        /* read the file until the "ROUTING TABLE" line is found (no more info after) */
        while (fgets (buffer, sizeof (buffer), fh) != NULL)
@@ -261,24 +287,38 @@ static int multi1_read (char *name, FILE *fh)
                if (fields_num < 4)
                        continue;
 
-               if (new_naming_schema)
+               if (number_connectedusers)
+               /* If so, sum all users, ignore the individuals*/
                {
-                       iostats_submit (fields[0],          /* "Common Name" */
-                                       NULL,               /* unused when in multimode */
-                                       atoll (fields[2]),  /* "Bytes Received" */
-                                       atoll (fields[3])); /* "Bytes Sent" */
+                sum_users += 1;
                }
-               else
+  if (number_connectedusers_only==0) 
                {
-                       iostats_submit (name,               /* vpn instance */
-                                       fields[0],          /* "Common Name" */
-                                       atoll (fields[2]),  /* "Bytes Received" */
-                                       atoll (fields[3])); /* "Bytes Sent" */
+                       if (new_naming_schema)
+                       {
+                               iostats_submit (fields[0],          /* "Common Name" */
+                                               NULL,               /* unused when in multimode */
+                                               atoll (fields[2]),  /* "Bytes Received" */
+                                               atoll (fields[3])); /* "Bytes Sent" */
+                       }
+                       else
+                       {
+                               iostats_submit (name,               /* vpn instance */
+                                               fields[0],          /* "Common Name" */
+                                               atoll (fields[2]),  /* "Bytes Received" */
+                                               atoll (fields[3])); /* "Bytes Sent" */
+                       }
                }
 
                read = 1;
        }
 
+       if (number_connectedusers)
+       {
+                       numusers_submit(name, name, sum_users);
+                       read = 1;
+       }
+
        return (read);
 } /* int multi1_read */
 
@@ -289,6 +329,7 @@ static int multi2_read (char *name, FILE *fh)
        char *fields[10];
        const int max_fields = STATIC_ARRAY_SIZE (fields);
        int  fields_num, read = 0;
+       long long sum_users    = 0;
 
        while (fgets (buffer, sizeof (buffer), fh) != NULL)
        {
@@ -306,26 +347,40 @@ static int multi2_read (char *name, FILE *fh)
                if (strcmp (fields[0], "CLIENT_LIST") != 0)
                        continue;
 
-               if (new_naming_schema)
+               if (number_connectedusers)
+               /* If so, sum all users, ignore the individuals*/
                {
-                       /* plugin inst = file name, type inst = fields[1] */
-                       iostats_submit (name,               /* vpn instance */
-                                       fields[1],          /* "Common Name" */
-                                       atoll (fields[4]),  /* "Bytes Received" */
-                                       atoll (fields[5])); /* "Bytes Sent" */
+                sum_users += 1;
                }
-               else
+  if (number_connectedusers_only==0) 
                {
-                       /* plugin inst = fields[1], type inst = "" */
-                       iostats_submit (fields[1],          /* "Common Name" */
-                                       NULL,               /* unused when in multimode */
-                                       atoll (fields[4]),  /* "Bytes Received" */
-                                       atoll (fields[5])); /* "Bytes Sent" */
+                       if (new_naming_schema)
+                       {
+                               /* plugin inst = file name, type inst = fields[1] */
+                               iostats_submit (name,               /* vpn instance */
+                                               fields[1],          /* "Common Name" */
+                                               atoll (fields[4]),  /* "Bytes Received" */
+                                               atoll (fields[5])); /* "Bytes Sent" */
+                       }
+                       else
+                       {
+                               /* plugin inst = fields[1], type inst = "" */
+                               iostats_submit (fields[1],          /* "Common Name" */
+                                               NULL,               /* unused when in multimode */
+                                               atoll (fields[4]),  /* "Bytes Received" */
+                                               atoll (fields[5])); /* "Bytes Sent" */
+                       }
                }
 
                read = 1;
        }
 
+       if (number_connectedusers)
+       {
+               numusers_submit(name, name, sum_users);
+               read = 1;
+       }
+
        return (read);
 } /* int multi2_read */
 
@@ -336,6 +391,7 @@ static int multi3_read (char *name, FILE *fh)
        char *fields[15];
        const int max_fields = STATIC_ARRAY_SIZE (fields);
        int  fields_num, read = 0;
+       long long sum_users    = 0;
 
        while (fgets (buffer, sizeof (buffer), fh) != NULL)
        {
@@ -356,25 +412,40 @@ static int multi3_read (char *name, FILE *fh)
                        if (strcmp (fields[0], "CLIENT_LIST") != 0)
                                continue;
 
-                       if (new_naming_schema)
+                       if (number_connectedusers)
+                /* If so, sum all users, ignore the individuals*/
                        {
-                               iostats_submit (name,               /* vpn instance */
-                                               fields[1],          /* "Common Name" */
-                                               atoll (fields[4]),  /* "Bytes Received" */
-                                               atoll (fields[5])); /* "Bytes Sent" */
+                 sum_users += 1;
                        }
-                       else
+
+   if (number_connectedusers_only==0) 
                        {
-                               iostats_submit (fields[1],          /* "Common Name" */
-                                               NULL,               /* unused when in multimode */
-                                               atoll (fields[4]),  /* "Bytes Received" */
-                                               atoll (fields[5])); /* "Bytes Sent" */
+                               if (new_naming_schema)
+                               {
+                                       iostats_submit (name,               /* vpn instance */
+                                                       fields[1],          /* "Common Name" */
+                                                       atoll (fields[4]),  /* "Bytes Received" */
+                                                       atoll (fields[5])); /* "Bytes Sent" */
+                               }
+                               else
+                               {
+                                       iostats_submit (fields[1],          /* "Common Name" */
+                                                       NULL,               /* unused when in multimode */
+                                                       atoll (fields[4]),  /* "Bytes Received" */
+                                                       atoll (fields[5])); /* "Bytes Sent" */
+                               }
                        }
 
-                       read = 1;
+                read = 1;
                }
        }
 
+       if (number_connectedusers)
+       {
+                       numusers_submit(name, name, sum_users);
+                       read = 1;
+       }
+
        return (read);
 } /* int multi3_read */
 
@@ -591,6 +662,31 @@ static int openvpn_config (const char *key, const char *value)
                        new_naming_schema = 0;
                }
        } /* if (strcasecmp ("ImprovedNamingSchema", key) == 0) */
+       else if (strcasecmp("AggregateUsers", key) == 0)
+       {
+               if (IS_TRUE(value))
+               {
+                       DEBUG ("openvpn plugin: Summing up all users");
+                number_connectedusers = 1;
+               }
+               else
+               {
+                number_connectedusers = 0;
+               }
+       } /* if (strcasecmp("AggregateUsers", key) == 0) */
+       else if (strcasecmp("OnlyAggregateUsers", key) == 0)
+       {
+               if (IS_TRUE(value))
+               {
+                       DEBUG ("openvpn plugin: Summing up all users");
+                number_connectedusers_only = 1;
+                number_connectedusers = 1;
+               }
+               else
+               {
+                number_connectedusers_only = 0;
+               }
+       } /* if (strcasecmp("OnlyAggregateUsers", key) == 0) */
        else
        {
                return (-1);