Added "type" to the value_list_t struct.
[collectd.git] / src / battery.c
index 8a74b3d..0522015 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>
 #  include <IOKit/ps/IOPSKeys.h>
 #endif
 
-#if HAVE_IOKIT_IOKITLIB_H || HAVE_IOKIT_PS_IOPOWERSOURCES_H || KERNEL_LINUX
-# define BATTERY_HAVE_READ 1
-#else
-# define BATTERY_HAVE_READ 0
+#if !HAVE_IOKIT_IOKITLIB_H && !HAVE_IOKIT_PS_IOPOWERSOURCES_H && !KERNEL_LINUX
+# error "No applicable input method."
 #endif
 
 #define INVALID_VALUE 47841.29
 
-static data_source_t data_source_charge[1] =
-{
-       {"value", DS_TYPE_GAUGE, 0, NAN}
-};
-
-static data_set_t charge_ds =
-{
-       "charge", 1, data_source_charge
-};
-
-static data_source_t data_source_current[1] =
-{
-       {"value", DS_TYPE_GAUGE, NAN, NAN}
-};
-
-static data_set_t current_ds =
-{
-       "current", 1, data_source_current
-};
-
-static data_source_t data_source_voltage[1] =
-{
-       {"value", DS_TYPE_GAUGE, NAN, NAN}
-};
-
-static data_set_t voltage_ds =
-{
-       "voltage", 1, data_source_voltage
-};
-
-#if BATTERY_HAVE_READ
 #if HAVE_IOKIT_IOKITLIB_H || HAVE_IOKIT_PS_IOPOWERSOURCES_H
        /* No global variables */
 /* #endif HAVE_IOKIT_IOKITLIB_H || HAVE_IOKIT_PS_IOPOWERSOURCES_H */
@@ -111,7 +77,7 @@ static int battery_init (void)
        {
                len = snprintf (filename, sizeof (filename), battery_pmu_file, battery_pmu_num);
 
-               if ((len >= sizeof (filename)) || (len < 0))
+               if ((len < 0) || ((unsigned int)len >= sizeof (filename)))
                        break;
 
                if (access (filename, R_OK))
@@ -134,9 +100,10 @@ static void battery_submit (const char *plugin_instance, const char *type, doubl
        vl.time = time (NULL);
        strcpy (vl.host, hostname_g);
        strcpy (vl.plugin, "battery");
-       strcpy (vl.plugin_instance, plugin_instance);
+       strncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
+       strncpy (vl.type, type, sizeof (vl.type));
 
-       plugin_dispatch_values (type, &vl);
+       plugin_dispatch_values (&vl);
 } /* void battery_submit */
 
 #if HAVE_IOKIT_PS_IOPOWERSOURCES_H || HAVE_IOKIT_IOKITLIB_H
@@ -151,13 +118,13 @@ double dict_get_double (CFDictionaryRef dict, char *key_string)
                        kCFStringEncodingASCII);
        if (key_obj == NULL)
        {
-               DBG ("CFStringCreateWithCString (%s) failed.\n", key_string);
+               DEBUG ("CFStringCreateWithCString (%s) failed.\n", key_string);
                return (INVALID_VALUE);
        }
 
        if ((val_obj = CFDictionaryGetValue (dict, key_obj)) == NULL)
        {
-               DBG ("CFDictionaryGetValue (%s) failed.", key_string);
+               DEBUG ("CFDictionaryGetValue (%s) failed.", key_string);
                CFRelease (key_obj);
                return (INVALID_VALUE);
        }
@@ -181,7 +148,7 @@ double dict_get_double (CFDictionaryRef dict, char *key_string)
        }
        else
        {
-               DBG ("CFGetTypeID (val_obj) = %i", (int) CFGetTypeID (val_obj));
+               DEBUG ("CFGetTypeID (val_obj) = %i", (int) CFGetTypeID (val_obj));
                return (INVALID_VALUE);
        }
 
@@ -207,7 +174,7 @@ static void get_via_io_power_sources (double *ret_charge,
        ps_array     = IOPSCopyPowerSourcesList (ps_raw);
        ps_array_len = CFArrayGetCount (ps_array);
 
-       DBG ("ps_array_len == %i", ps_array_len);
+       DEBUG ("ps_array_len == %i", ps_array_len);
 
        for (i = 0; i < ps_array_len; i++)
        {
@@ -216,13 +183,13 @@ static void get_via_io_power_sources (double *ret_charge,
 
                if (ps_dict == NULL)
                {
-                       DBG ("IOPSGetPowerSourceDescription failed.");
+                       DEBUG ("IOPSGetPowerSourceDescription failed.");
                        continue;
                }
 
                if (CFGetTypeID (ps_dict) != CFDictionaryGetTypeID ())
                {
-                       DBG ("IOPSGetPowerSourceDescription did not return a CFDictionaryRef");
+                       DEBUG ("IOPSGetPowerSourceDescription did not return a CFDictionaryRef");
                        continue;
                }
 
@@ -283,7 +250,7 @@ static void get_via_generic_iokit (double *ret_charge,
                        &iterator);
        if (status != kIOReturnSuccess)
        {
-               DBG ("IOServiceGetMatchingServices failed.");
+               DEBUG ("IOServiceGetMatchingServices failed.");
                return;
        }
 
@@ -295,7 +262,7 @@ static void get_via_generic_iokit (double *ret_charge,
                                kNilOptions);
                if (status != kIOReturnSuccess)
                {
-                       DBG ("IORegistryEntryCreateCFProperties failed.");
+                       DEBUG ("IORegistryEntryCreateCFProperties failed.");
                        continue;
                }
 
@@ -394,11 +361,11 @@ static int battery_read (void)
                double *valptr = NULL;
 
                len = snprintf (filename, sizeof (filename), battery_pmu_file, i);
-               if ((len >= sizeof (filename)) || (len < 0))
+               if ((len < 0) || ((unsigned int)len >= sizeof (filename)))
                        continue;
 
                len = snprintf (batnum_str, sizeof (batnum_str), "%i", i);
-               if ((len >= sizeof (batnum_str)) || (len < 0))
+               if ((len < 0) || ((unsigned int)len >= sizeof (batnum_str)))
                        continue;
 
                if ((fh = fopen (filename, "r")) == NULL)
@@ -458,7 +425,9 @@ static int battery_read (void)
 
                if ((dh = opendir ("/proc/acpi/battery")) == NULL)
                {
-                       syslog (LOG_ERR, "Cannot open `/proc/acpi/battery': %s", strerror (errno));
+                       char errbuf[1024];
+                       ERROR ("Cannot open `/proc/acpi/battery': %s",
+                                       sstrerror (errno, errbuf, sizeof (errbuf)));
                        return (-1);
                }
 
@@ -470,12 +439,15 @@ static int battery_read (void)
                        len = snprintf (filename, sizeof (filename),
                                        "/proc/acpi/battery/%s/state",
                                        ent->d_name);
-                       if ((len >= sizeof (filename)) || (len < 0))
+                       if ((len < 0) || ((unsigned int)len >= sizeof (filename)))
                                continue;
 
                        if ((fh = fopen (filename, "r")) == NULL)
                        {
-                               syslog (LOG_ERR, "Cannot open `%s': %s", filename, strerror (errno));
+                               char errbuf[1024];
+                               ERROR ("Cannot open `%s': %s", filename,
+                                               sstrerror (errno, errbuf,
+                                                       sizeof (errbuf)));
                                continue;
                        }
 
@@ -549,16 +521,9 @@ static int battery_read (void)
 
        return (0);
 }
-#endif /* BATTERY_HAVE_READ */
 
 void module_register (void)
 {
-       plugin_register_data_set (&charge_ds);
-       plugin_register_data_set (&current_ds);
-       plugin_register_data_set (&voltage_ds);
-
-#if BATTERY_HAVE_READ
        plugin_register_init ("battery", battery_init);
        plugin_register_read ("battery", battery_read);
-#endif /* BATTERY_HAVE_READ */
-}
+} /* void module_register */