unixsock plugin: Remove the socket-file after closing the socket.
[collectd.git] / src / disk.c
index 3cb86e7..31c3ec2 100644 (file)
@@ -22,7 +22,6 @@
 #include "collectd.h"
 #include "common.h"
 #include "plugin.h"
-#include "utils_debug.h"
 
 #if HAVE_MACH_MACH_TYPES_H
 #  include <mach/mach_types.h>
 # define DISK_HAVE_READ 0
 #endif
 
-/* 2^34 = 17179869184 = ~17.2GByte/s */
-static data_source_t octets_dsrc[2] =
-{
-       {"read",  DS_TYPE_COUNTER, 0, 17179869183.0},
-       {"write", DS_TYPE_COUNTER, 0, 17179869183.0}
-};
-
-static data_set_t octets_ds =
-{
-       "disk_octets", 2, octets_dsrc
-};
-
-static data_source_t operations_dsrc[2] =
-{
-       {"read",  DS_TYPE_COUNTER, 0, 4294967295.0},
-       {"write", DS_TYPE_COUNTER, 0, 4294967295.0}
-};
-
-static data_set_t operations_ds =
-{
-       "disk_ops", 2, operations_dsrc
-};
-
-static data_source_t merged_dsrc[2] =
-{
-       {"read",  DS_TYPE_COUNTER, 0, 4294967295.0},
-       {"write", DS_TYPE_COUNTER, 0, 4294967295.0}
-};
-
-static data_set_t merged_ds =
-{
-       "disk_merged", 2, merged_dsrc
-};
-
-/* max is 1000000us per second. */
-static data_source_t time_dsrc[2] =
-{
-       {"read",  DS_TYPE_COUNTER, 0, 1000000.0},
-       {"write", DS_TYPE_COUNTER, 0, 1000000.0}
-};
-
-static data_set_t time_ds =
-{
-       "disk_time", 2, time_dsrc
-};
-
 #if DISK_HAVE_READ
 #if HAVE_IOKIT_IOKITLIB_H
 static mach_port_t io_master_port = MACH_PORT_NULL;
@@ -127,7 +80,6 @@ typedef struct diskstats
 } diskstats_t;
 
 static diskstats_t *disklist;
-static int min_poll_count;
 /* #endif KERNEL_LINUX */
 
 #elif HAVE_LIBKSTAT
@@ -152,7 +104,7 @@ static int disk_init (void)
        status = IOMasterPort (MACH_PORT_NULL, &io_master_port);
        if (status != kIOReturnSuccess)
        {
-               syslog (LOG_ERR, "IOMasterPort failed: %s",
+               ERROR ("IOMasterPort failed: %s",
                                mach_error_string (status));
                io_master_port = MACH_PORT_NULL;
                return (-1);
@@ -160,17 +112,7 @@ static int disk_init (void)
 /* #endif HAVE_IOKIT_IOKITLIB_H */
 
 #elif KERNEL_LINUX
-       int step;
-       int heartbeat;
-
-       step = atoi (COLLECTD_STEP);
-       heartbeat = atoi (COLLECTD_HEARTBEAT);
-
-       assert (step > 0);
-       assert (heartbeat >= step);
-
-       min_poll_count = 1 + (heartbeat / step);
-       DBG ("min_poll_count = %i;", min_poll_count);
+       /* do nothing */
 /* #endif KERNEL_LINUX */
 
 #elif HAVE_LIBKSTAT
@@ -210,7 +152,7 @@ static void disk_submit (const char *plugin_instance,
        vl.values = values;
        vl.values_len = 2;
        vl.time = time (NULL);
-       strcpy (vl.host, hostname);
+       strcpy (vl.host, hostname_g);
        strcpy (vl.plugin, "disk");
        strncpy (vl.plugin_instance, plugin_instance,
                        sizeof (vl.plugin_instance));
@@ -230,7 +172,7 @@ static signed long long dict_get_value (CFDictionaryRef dict, const char *key)
                        kCFStringEncodingASCII);
        if (key_obj == NULL)
        {
-               DBG ("CFStringCreateWithCString (%s) failed.", key);
+               DEBUG ("CFStringCreateWithCString (%s) failed.", key);
                return (-1LL);
        }
        
@@ -241,13 +183,13 @@ static signed long long dict_get_value (CFDictionaryRef dict, const char *key)
 
        if (val_obj == NULL)
        {
-               DBG ("CFDictionaryGetValue (%s) failed.", key);
+               DEBUG ("CFDictionaryGetValue (%s) failed.", key);
                return (-1LL);
        }
 
        if (!CFNumberGetValue (val_obj, kCFNumberSInt64Type, &val_int))
        {
-               DBG ("CFNumberGetValue (%s) failed.", key);
+               DEBUG ("CFNumberGetValue (%s) failed.", key);
                return (-1LL);
        }
 
@@ -305,7 +247,7 @@ static int disk_read (void)
                                != kIOReturnSuccess)
                {
                        /* This fails for example for DVD/CD drives.. */
-                       DBG ("IORegistryEntryGetChildEntry (disk) failed: 0x%08x", status);
+                       DEBUG ("IORegistryEntryGetChildEntry (disk) failed: 0x%08x", status);
                        IOObjectRelease (disk);
                        continue;
                }
@@ -317,7 +259,7 @@ static int disk_read (void)
                                        kNilOptions)
                                != kIOReturnSuccess)
                {
-                       syslog (LOG_ERR, "disk-plugin: IORegistryEntryCreateCFProperties failed.");
+                       ERROR ("disk-plugin: IORegistryEntryCreateCFProperties failed.");
                        IOObjectRelease (disk_child);
                        IOObjectRelease (disk);
                        continue;
@@ -325,7 +267,7 @@ static int disk_read (void)
 
                if (props_dict == NULL)
                {
-                       DBG ("IORegistryEntryCreateCFProperties (disk) failed.");
+                       DEBUG ("IORegistryEntryCreateCFProperties (disk) failed.");
                        IOObjectRelease (disk_child);
                        IOObjectRelease (disk);
                        continue;
@@ -336,7 +278,7 @@ static int disk_read (void)
 
                if (stats_dict == NULL)
                {
-                       DBG ("CFDictionaryGetValue (%s) failed.",
+                       DEBUG ("CFDictionaryGetValue (%s) failed.",
                                        kIOBlockStorageDriverStatisticsKey);
                        CFRelease (props_dict);
                        IOObjectRelease (disk_child);
@@ -350,7 +292,7 @@ static int disk_read (void)
                                        kNilOptions)
                                != kIOReturnSuccess)
                {
-                       DBG ("IORegistryEntryCreateCFProperties (disk_child) failed.");
+                       DEBUG ("IORegistryEntryCreateCFProperties (disk_child) failed.");
                        IOObjectRelease (disk_child);
                        CFRelease (props_dict);
                        IOObjectRelease (disk);
@@ -382,14 +324,14 @@ static int disk_read (void)
 
                if (snprintf (disk_name, 64, "%i-%i", disk_major, disk_minor) >= 64)
                {
-                       DBG ("snprintf (major, minor) failed.");
+                       DEBUG ("snprintf (major, minor) failed.");
                        CFRelease (child_dict);
                        IOObjectRelease (disk_child);
                        CFRelease (props_dict);
                        IOObjectRelease (disk);
                        continue;
                }
-               DBG ("disk_name = %s", disk_name);
+               DEBUG ("disk_name = %s", disk_name);
 
                if ((read_byt != -1LL) || (write_byt != -1LL))
                        disk_submit (disk_name, "disk_octets", read_byt, write_byt);
@@ -516,7 +458,7 @@ static int disk_read (void)
                }
                else
                {
-                       DBG ("numfields = %i; => unknown file format.", numfields);
+                       DEBUG ("numfields = %i; => unknown file format.", numfields);
                        continue;
                }
 
@@ -538,16 +480,16 @@ static int disk_read (void)
 
                /* Don't write to the RRDs if we've just started.. */
                ds->poll_count++;
-               if (ds->poll_count <= min_poll_count)
+               if (ds->poll_count <= 2)
                {
-                       DBG ("(ds->poll_count = %i) <= (min_poll_count = %i); => Not writing.",
-                                       ds->poll_count, min_poll_count);
+                       DEBUG ("(ds->poll_count = %i) <= (min_poll_count = 2); => Not writing.",
+                                       ds->poll_count);
                        continue;
                }
 
                if ((read_count == 0) && (write_count == 0))
                {
-                       DBG ("((read_count == 0) && (write_count == 0)); => Not writing.");
+                       DEBUG ("((read_count == 0) && (write_count == 0)); => Not writing.");
                        continue;
                }
 
@@ -571,6 +513,23 @@ static int disk_read (void)
 /* #endif defined(KERNEL_LINUX) */
 
 #elif HAVE_LIBKSTAT
+# if HAVE_KSTAT_IO_T_WRITES && HAVE_KSTAT_IO_T_NWRITES && HAVE_KSTAT_IO_T_WTIME
+#  define KIO_ROCTETS reads
+#  define KIO_WOCTETS writes
+#  define KIO_ROPS    nreads
+#  define KIO_WOPS    nwrites
+#  define KIO_RTIME   rtime
+#  define KIO_WTIME   wtime
+# elif HAVE_KSTAT_IO_T_NWRITTEN && HAVE_KSTAT_IO_T_WRITES && HAVE_KSTAT_IO_T_WTIME
+#  define KIO_ROCTETS nread
+#  define KIO_WOCTETS nwritten
+#  define KIO_ROPS    reads
+#  define KIO_WOPS    writes
+#  define KIO_RTIME   rtime
+#  define KIO_WTIME   wtime
+# else
+#  error "kstat_io_t does not have the required members"
+# endif
        static kstat_io_t kio;
        int i;
 
@@ -584,15 +543,20 @@ static int disk_read (void)
 
                if (strncmp (ksp[i]->ks_class, "disk", 4) == 0)
                {
-                       disk_submit (ksp[i]->ks_name, "disk_octets", kio.reads, kio.writes);
-                       disk_submit (ksp[i]->ks_name, "disk_ops", kio.nreads, kio.nwrites);
+                       disk_submit (ksp[i]->ks_name, "disk_octets",
+                                       kio.KIO_ROCTETS, kio.KIO_WOCTETS);
+                       disk_submit (ksp[i]->ks_name, "disk_ops",
+                                       kio.KIO_ROPS, kio.KIO_WOPS);
                        /* FIXME: Convert this to microseconds if necessary */
-                       disk_submit (ksp[i]->ks_name, "disk_time", kio.rtime, kio.wtime);
+                       disk_submit (ksp[i]->ks_name, "disk_time",
+                                       kio.KIO_RTIME, kio.KIO_WTIME);
                }
                else if (strncmp (ksp[i]->ks_class, "partition", 9) == 0)
                {
-                       disk_submit (ksp[i]->ks_name, "disk_octets", kio.reads, kio.writes);
-                       disk_submit (ksp[i]->ks_name, "disk_ops", kio.nreads, kio.nwrites);
+                       disk_submit (ksp[i]->ks_name, "disk_octets",
+                                       kio.KIO_ROCTETS, kio.KIO_WOCTETS);
+                       disk_submit (ksp[i]->ks_name, "disk_ops",
+                                       kio.KIO_ROPS, kio.KIO_WOPS);
                }
        }
 #endif /* defined(HAVE_LIBKSTAT) */
@@ -603,13 +567,8 @@ static int disk_read (void)
 
 void module_register (void)
 {
-       plugin_register_data_set (&octets_ds);
-       plugin_register_data_set (&operations_ds);
-       plugin_register_data_set (&merged_ds);
-       plugin_register_data_set (&time_ds);
-
 #if DISK_HAVE_READ
        plugin_register_init ("disk", disk_init);
        plugin_register_read ("disk", disk_read);
 #endif /* DISK_HAVE_READ */
-}
+} /* void module_register */