disk plugin: Correct the collection of {read,write}-time.
[collectd.git] / src / unixsock.c
index 82cfc60..8211bf4 100644 (file)
@@ -29,7 +29,6 @@
 
 #include <sys/socket.h>
 #include <sys/un.h>
-#include <sys/poll.h>
 
 #include <grp.h>
 
@@ -67,6 +66,8 @@ static const char *config_keys[] =
 };
 static int config_keys_num = 3;
 
+static int loop = 0;
+
 /* socket configuration */
 static int   sock_fd    = -1;
 static char *sock_file  = NULL;
@@ -142,48 +143,15 @@ static value_cache_t *cache_search (const char *name)
        return (vc);
 } /* value_cache_t *cache_search */
 
-static int cache_alloc_name (char *ret, int ret_len,
-               const char *hostname,
-               const char *plugin, const char *plugin_instance,
-               const char *type, const char *type_instance)
-{
-       int  status;
-
-       assert (plugin != NULL);
-       assert (type != NULL);
-
-       if ((plugin_instance == NULL) || (strlen (plugin_instance) == 0))
-       {
-               if ((type_instance == NULL) || (strlen (type_instance) == 0))
-                       status = snprintf (ret, ret_len, "%s/%s/%s",
-                                       hostname, plugin, type);
-               else
-                       status = snprintf (ret, ret_len, "%s/%s/%s-%s",
-                                       hostname, plugin, type, type_instance);
-       }
-       else
-       {
-               if ((type_instance == NULL) || (strlen (type_instance) == 0))
-                       status = snprintf (ret, ret_len, "%s/%s-%s/%s",
-                                       hostname, plugin, plugin_instance, type);
-               else
-                       status = snprintf (ret, ret_len, "%s/%s-%s/%s-%s",
-                                       hostname, plugin, plugin_instance, type, type_instance);
-       }
-
-       if ((status < 1) || (status >= ret_len))
-               return (-1);
-       return (0);
-} /* int cache_alloc_name */
-
 static int cache_insert (const data_set_t *ds, const value_list_t *vl)
 {
        /* We're called from `cache_update' so we don't need to lock the mutex */
        value_cache_t *vc;
        int i;
 
-       DEBUG ("ds->ds_num = %i; vl->values_len = %i;",
-                       ds->ds_num, vl->values_len);
+       DEBUG ("unixsock plugin: cache_insert: ds->type = %s; ds->ds_num = %i;"
+                       " vl->values_len = %i;",
+                       ds->type, ds->ds_num, vl->values_len);
        assert (ds->ds_num == vl->values_len);
 
        vc = (value_cache_t *) malloc (sizeof (value_cache_t));
@@ -219,12 +187,10 @@ static int cache_insert (const data_set_t *ds, const value_list_t *vl)
                return (-1);
        }
 
-       if (cache_alloc_name (vc->name, sizeof (vc->name),
-                               vl->host, vl->plugin, vl->plugin_instance,
-                               ds->type, vl->type_instance) != 0)
+       if (FORMAT_VL (vc->name, sizeof (vc->name), vl, ds))
        {
                pthread_mutex_unlock (&cache_lock);
-               ERROR ("unixsock plugin: cache_alloc_name failed.");
+               ERROR ("unixsock plugin: FORMAT_VL failed.");
                free (vc->counter);
                free (vc->gauge);
                free (vc);
@@ -269,22 +235,31 @@ static int cache_update (const data_set_t *ds, const value_list_t *vl)
        value_cache_t *vc;
        int i;
 
-       if (cache_alloc_name (name, sizeof (name),
-                               vl->host,
-                               vl->plugin, vl->plugin_instance,
-                               ds->type, vl->type_instance) != 0)
+       if (FORMAT_VL (name, sizeof (name), vl, ds) != 0)
                return (-1);
 
        pthread_mutex_lock (&cache_lock);
 
        vc = cache_search (name);
 
+       /* pthread_mutex_lock is called by cache_insert. */
        if (vc == NULL)
                return (cache_insert (ds, vl));
 
        assert (vc->values_num == ds->ds_num);
        assert (vc->values_num == vl->values_len);
 
+       /* Avoid floating-point exceptions due to division by zero. */
+       if (vc->time >= vl->time)
+       {
+               pthread_mutex_unlock (&cache_lock);
+               ERROR ("unixsock plugin: vc->time >= vl->time. vc->time = %u; "
+                               "vl->time = %u; vl = %s;",
+                               (unsigned int) vc->time, (unsigned int) vl->time,
+                               name);
+               return (-1);
+       } /* if (vc->time >= vl->time) */
+
        /*
         * Update the values. This is possibly a lot more that you'd expect
         * because we honor min and max values and handle counter overflows here.
@@ -487,16 +462,29 @@ static int us_handle_getval (FILE *fh, char **fields, int fields_num)
        int   i;
 
        if (fields_num != 2)
+       {
+               DEBUG ("unixsock plugin: Wrong number of fields: %i", fields_num);
+               fprintf (fh, "-1 Wrong number of fields: Got %i, expected 2.\n",
+                               fields_num);
+               fflush (fh);
                return (-1);
+       }
+       DEBUG ("unixsock plugin: Got query for `%s'", fields[1]);
 
        status = parse_identifier (fields[1], &hostname,
                        &plugin, &plugin_instance,
                        &type, &type_instance);
        if (status != 0)
+       {
+               DEBUG ("unixsock plugin: Cannot parse `%s'", fields[1]);
+               fprintf (fh, "-1 Cannot parse identifier.\n");
+               fflush (fh);
                return (-1);
+       }
 
-       status = cache_alloc_name (name, sizeof (name),
+       status = format_name (name, sizeof (name),
                        hostname, plugin, plugin_instance, type, type_instance);
+       /* FIXME: Send some response */
        if (status != 0)
                return (-1);
 
@@ -549,24 +537,40 @@ static int us_handle_putval (FILE *fh, char **fields, int fields_num)
        char **value_ptr;
 
        if (fields_num != 3)
+       {
+               DEBUG ("unixsock plugin: Wrong number of fields: %i", fields_num);
+               fprintf (fh, "-1 Wrong number of fields: Got %i, expected 3.\n",
+                               fields_num);
+               fflush (fh);
                return (-1);
+       }
 
        status = parse_identifier (fields[1], &hostname,
                        &plugin, &plugin_instance,
                        &type, &type_instance);
        if (status != 0)
+       {
+               DEBUG ("unixsock plugin: Cannot parse `%s'", fields[1]);
+               fprintf (fh, "-1 Cannot parse identifier.\n");
+               fflush (fh);
                return (-1);
+       }
 
+       /* FIXME: Send some response */
        if ((strlen (hostname) > sizeof (vl.host))
                        || (strlen (plugin) > sizeof (vl.plugin))
-                       || (strlen (plugin_instance) > sizeof (vl.plugin_instance))
-                       || (strlen (type_instance) > sizeof (vl.type_instance)))
+                       || ((plugin_instance != NULL)
+                               && (strlen (plugin_instance) > sizeof (vl.plugin_instance)))
+                       || ((type_instance != NULL)
+                               && (strlen (type_instance) > sizeof (vl.type_instance))))
                return (-1);
 
        strcpy (vl.host, hostname);
        strcpy (vl.plugin, plugin);
-       strcpy (vl.plugin_instance, plugin_instance);
-       strcpy (vl.type_instance, type_instance);
+       if (plugin_instance != NULL)
+               strcpy (vl.plugin_instance, plugin_instance);
+       if (type_instance != NULL)
+               strcpy (vl.type_instance, type_instance);
 
        { /* parse the time */
                char *t = fields[2];
@@ -587,6 +591,7 @@ static int us_handle_putval (FILE *fh, char **fields, int fields_num)
                return (-1);
 
        value_ptr = (char **) calloc (ds->ds_num, sizeof (char *));
+       /* FIXME: Send some response */
        if (value_ptr == NULL)
                return (-1);
 
@@ -613,19 +618,20 @@ static int us_handle_putval (FILE *fh, char **fields, int fields_num)
 
                if (i != ds->ds_num)
                {
-                       free (value_ptr);
+                       sfree (value_ptr);
+                       /* FIXME: Send some response */
                        return (-1);
                }
        } /* done parsing the value-list */
 
-       vl.values_len = fields_num - 2;
+       vl.values_len = ds->ds_num;
        vl.values = (value_t *) malloc (vl.values_len * sizeof (value_t));
        if (vl.values == NULL)
        {
-               free (value_ptr);
+               sfree (value_ptr);
                return (-1);
        }
-       vl.values_len = ds->ds_num;
+       DEBUG ("value_ptr = 0x%p; vl.values = 0x%p;", (void *) value_ptr, (void *) vl.values);
 
        for (i = 0; i < ds->ds_num; i++)
        {
@@ -636,12 +642,17 @@ static int us_handle_putval (FILE *fh, char **fields, int fields_num)
                else if (ds->ds[i].type == DS_TYPE_GAUGE)
                        vl.values[i].gauge = atof (value_ptr[i]);
        } /* for (i = 2 .. fields_num) */
-       sfree (value_ptr);
 
        plugin_dispatch_values (type, &vl);
 
+       DEBUG ("value_ptr = 0x%p; vl.values = 0x%p;", (void *) value_ptr, (void *) vl.values);
+
+       sfree (value_ptr);
        sfree (vl.values); 
 
+       fprintf (fh, "0 Success\n");
+       fflush (fh);
+
        return (0);
 } /* int us_handle_putval */
 
@@ -723,7 +734,7 @@ static void *us_server_thread (void *arg)
        if (us_open_socket () != 0)
                pthread_exit ((void *) 1);
 
-       while (42)
+       while (loop != 0)
        {
                DEBUG ("Calling accept..");
                status = accept (sock_fd, NULL, NULL);
@@ -767,7 +778,19 @@ static void *us_server_thread (void *arg)
                        free (remote_fd);
                        continue;
                }
-       } /* while (42) */
+       } /* while (loop) */
+
+       close (sock_fd);
+       sock_fd = -1;
+
+       status = unlink ((sock_file != NULL) ? sock_file : US_DEFAULT_PATH);
+       if (status != 0)
+       {
+               char errbuf[1024];
+               NOTICE ("unixsock plugin: unlink (%s) failed: %s",
+                               (sock_file != NULL) ? sock_file : US_DEFAULT_PATH,
+                               sstrerror (errno, errbuf, sizeof (errbuf)));
+       }
 
        return ((void *) 0);
 } /* void *us_server_thread */
@@ -800,6 +823,8 @@ static int us_init (void)
 {
        int status;
 
+       loop = 1;
+
        status = pthread_create (&listen_thread, NULL, us_server_thread, NULL);
        if (status != 0)
        {
@@ -816,6 +841,8 @@ static int us_shutdown (void)
 {
        void *ret;
 
+       loop = 0;
+
        if (listen_thread != (pthread_t) 0)
        {
                pthread_kill (listen_thread, SIGTERM);
@@ -833,7 +860,7 @@ static int us_shutdown (void)
 static int us_write (const data_set_t *ds, const value_list_t *vl)
 {
        cache_update (ds, vl);
-       cache_flush (2 * atoi (COLLECTD_STEP));
+       cache_flush (2 * interval_g);
 
        return (0);
 }