collectd.c: Added '-t' command line option.
[collectd.git] / src / unixsock.c
index bea8a4f..9793659 100644 (file)
@@ -29,7 +29,6 @@
 
 #include <sys/socket.h>
 #include <sys/un.h>
-#include <sys/poll.h>
 
 #include <grp.h>
 
@@ -144,49 +143,26 @@ 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);
+#if COLLECT_DEBUG
        assert (ds->ds_num == vl->values_len);
+#else
+       if (ds->ds_num != vl->values_len)
+       {
+               ERROR ("unixsock plugin: ds->type = %s: (ds->ds_num = %i) != "
+                               "(vl->values_len = %i)",
+                               ds->type, ds->ds_num, vl->values_len);
+               return (-1);
+       }
+#endif
 
        vc = (value_cache_t *) malloc (sizeof (value_cache_t));
        if (vc == NULL)
@@ -221,12 +197,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);
@@ -271,22 +245,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.
@@ -509,11 +492,13 @@ static int us_handle_getval (FILE *fh, char **fields, int fields_num)
                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)
+       {
+               fprintf (fh, "-1 format_name failed.\n");
                return (-1);
+       }
 
        pthread_mutex_lock (&cache_lock);
 
@@ -583,14 +568,16 @@ static int us_handle_putval (FILE *fh, char **fields, int fields_num)
                return (-1);
        }
 
-       /* FIXME: Send some response */
        if ((strlen (hostname) > sizeof (vl.host))
                        || (strlen (plugin) > sizeof (vl.plugin))
                        || ((plugin_instance != NULL)
                                && (strlen (plugin_instance) > sizeof (vl.plugin_instance)))
                        || ((type_instance != NULL)
                                && (strlen (type_instance) > sizeof (vl.type_instance))))
+       {
+               fprintf (fh, "-1 Identifier too long.");
                return (-1);
+       }
 
        strcpy (vl.host, hostname);
        strcpy (vl.plugin, plugin);
@@ -603,7 +590,10 @@ static int us_handle_putval (FILE *fh, char **fields, int fields_num)
                char *t = fields[2];
                char *v = strchr (t, ':');
                if (v == NULL)
+               {
+                       fprintf (fh, "-1 No time found.");
                        return (-1);
+               }
                *v = '\0'; v++;
 
                vl.time = (time_t) atoi (t);
@@ -618,10 +608,11 @@ 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)
+       {
+               fprintf (fh, "-1 calloc failed.");
                return (-1);
-
+       }
 
        { /* parse the value-list. It's colon-separated. */
                char *dummy;
@@ -646,7 +637,8 @@ static int us_handle_putval (FILE *fh, char **fields, int fields_num)
                if (i != ds->ds_num)
                {
                        sfree (value_ptr);
-                       /* FIXME: Send some response */
+                       fprintf (fh, "-1 Number of values incorrect: Got %i, "
+                                       "expected %i.", i, ds->ds_num);
                        return (-1);
                }
        } /* done parsing the value-list */
@@ -656,6 +648,7 @@ static int us_handle_putval (FILE *fh, char **fields, int fields_num)
        if (vl.values == NULL)
        {
                sfree (value_ptr);
+               fprintf (fh, "-1 malloc failed.");
                return (-1);
        }
        DEBUG ("value_ptr = 0x%p; vl.values = 0x%p;", (void *) value_ptr, (void *) vl.values);
@@ -740,7 +733,7 @@ static void *us_handle_client (void *arg)
                }
                else
                {
-                       fprintf (fh, "Unknown command: %s\n", fields[0]);
+                       fprintf (fh, "-1 Unknown command: %s\n", fields[0]);
                        fflush (fh);
                }
        } /* while (fgets) */
@@ -810,6 +803,15 @@ static void *us_server_thread (void *arg)
        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 */
 
@@ -863,9 +865,7 @@ static int us_shutdown (void)
 
        if (listen_thread != (pthread_t) 0)
        {
-               DEBUG ("unixsock plugin: Sending SIGTERM to listening thread");
                pthread_kill (listen_thread, SIGTERM);
-               DEBUG ("unixsock plugin: Waiting for thread to terminate");
                pthread_join (listen_thread, &ret);
                listen_thread = (pthread_t) 0;
        }
@@ -880,7 +880,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);
 }