X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fbattery.c;h=4178d8b5145f2b728e5c7c949d72ebd8d126a89e;hb=619f181d3d5f4f6c90cb8ae228527302ddd35819;hp=2141a58c725f3515963f71e29566ac0315b0fca8;hpb=fdf203eed0a91818db735105a4cc85effd7971d9;p=collectd.git diff --git a/src/battery.c b/src/battery.c index 2141a58c..4178d8b5 100644 --- a/src/battery.c +++ b/src/battery.c @@ -1,11 +1,11 @@ /** * collectd - src/battery.c - * Copyright (C) 2006 Florian octo Forster + * Copyright (C) 2006,2007 Florian octo Forster + * Copyright (C) 2008 Michał Mirosław * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the - * Free Software Foundation; either version 2 of the License, or (at your - * option) any later version. + * Free Software Foundation; only version 2 of the License is applicable. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of @@ -18,15 +18,14 @@ * * Authors: * Florian octo Forster + * Michał Mirosław **/ #include "collectd.h" #include "common.h" #include "plugin.h" -#include "utils_debug.h" -#define MODULE_NAME "battery" -#define BUFSIZE 512 +#include "utils_complain.h" #if HAVE_MACH_MACH_TYPES_H # include @@ -53,39 +52,12 @@ # include #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 char *battery_current_file = "battery-%s/current.rrd"; -static char *battery_voltage_file = "battery-%s/voltage.rrd"; -static char *battery_charge_file = "battery-%s/charge.rrd"; - -static char *ds_def_current[] = -{ - "DS:current:GAUGE:"COLLECTD_HEARTBEAT":U:U", - NULL -}; -static int ds_num_current = 1; - -static char *ds_def_voltage[] = -{ - "DS:voltage:GAUGE:"COLLECTD_HEARTBEAT":U:U", - NULL -}; -static int ds_num_voltage = 1; - -static char *ds_def_charge[] = -{ - "DS:charge:GAUGE:"COLLECTD_HEARTBEAT":0:U", - NULL -}; -static int ds_num_charge = 1; - #if HAVE_IOKIT_IOKITLIB_H || HAVE_IOKIT_PS_IOPOWERSOURCES_H /* No global variables */ /* #endif HAVE_IOKIT_IOKITLIB_H || HAVE_IOKIT_PS_IOPOWERSOURCES_H */ @@ -93,9 +65,10 @@ static int ds_num_charge = 1; #elif KERNEL_LINUX static int battery_pmu_num = 0; static char *battery_pmu_file = "/proc/pmu/battery_%i"; +static const char *battery_acpi_dir = "/proc/acpi/battery"; #endif /* KERNEL_LINUX */ -static void battery_init (void) +static int battery_init (void) { #if HAVE_IOKIT_IOKITLIB_H || HAVE_IOKIT_PS_IOPOWERSOURCES_H /* No init neccessary */ @@ -103,13 +76,13 @@ static void battery_init (void) #elif KERNEL_LINUX int len; - char filename[BUFSIZE]; + char filename[128]; for (battery_pmu_num = 0; ; battery_pmu_num++) { - len = snprintf (filename, BUFSIZE, battery_pmu_file, battery_pmu_num); + len = ssnprintf (filename, sizeof (filename), battery_pmu_file, battery_pmu_num); - if ((len >= BUFSIZE) || (len < 0)) + if ((len < 0) || ((unsigned int)len >= sizeof (filename))) break; if (access (filename, R_OK)) @@ -117,90 +90,25 @@ static void battery_init (void) } #endif /* KERNEL_LINUX */ - return; -} - -static void battery_current_write (char *host, char *inst, char *val) -{ - char filename[BUFSIZE]; - int len; - - len = snprintf (filename, BUFSIZE, battery_current_file, inst); - if ((len >= BUFSIZE) || (len < 0)) - return; - - rrd_update_file (host, filename, val, - ds_def_current, ds_num_current); + return (0); } -static void battery_voltage_write (char *host, char *inst, char *val) +static void battery_submit (const char *plugin_instance, const char *type, double value) { - char filename[BUFSIZE]; - int len; - - len = snprintf (filename, BUFSIZE, battery_voltage_file, inst); - if ((len >= BUFSIZE) || (len < 0)) - return; - - rrd_update_file (host, filename, val, - ds_def_voltage, ds_num_voltage); -} - -static void battery_charge_write (char *host, char *inst, char *val) -{ - char filename[BUFSIZE]; - int len; - - len = snprintf (filename, BUFSIZE, battery_charge_file, inst); - if ((len >= BUFSIZE) || (len < 0)) - return; - - rrd_update_file (host, filename, val, - ds_def_charge, ds_num_charge); -} - -#if BATTERY_HAVE_READ -static void battery_submit (char *inst, double current, double voltage, double charge) -{ - int len; - char buffer[BUFSIZE]; - - if (current != INVALID_VALUE) - { - len = snprintf (buffer, BUFSIZE, "N:%.3f", current); - - if ((len > 0) && (len < BUFSIZE)) - plugin_submit ("battery_current", inst, buffer); - } - else - { - plugin_submit ("battery_current", inst, "N:U"); - } + value_t values[1]; + value_list_t vl = VALUE_LIST_INIT; - if (voltage != INVALID_VALUE) - { - len = snprintf (buffer, BUFSIZE, "N:%.3f", voltage); + values[0].gauge = value; - if ((len > 0) && (len < BUFSIZE)) - plugin_submit ("battery_voltage", inst, buffer); - } - else - { - plugin_submit ("battery_voltage", inst, "N:U"); - } + vl.values = values; + vl.values_len = 1; + sstrncpy (vl.host, hostname_g, sizeof (vl.host)); + sstrncpy (vl.plugin, "battery", sizeof (vl.plugin)); + sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance)); + sstrncpy (vl.type, type, sizeof (vl.type)); - if (charge != INVALID_VALUE) - { - len = snprintf (buffer, BUFSIZE, "N:%.3f", charge); - - if ((len > 0) && (len < BUFSIZE)) - plugin_submit ("battery_charge", inst, buffer); - } - else - { - plugin_submit ("battery_charge", inst, "N:U"); - } -} + plugin_dispatch_values (&vl); +} /* void battery_submit */ #if HAVE_IOKIT_PS_IOPOWERSOURCES_H || HAVE_IOKIT_IOKITLIB_H double dict_get_double (CFDictionaryRef dict, char *key_string) @@ -214,13 +122,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); } @@ -244,7 +152,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); } @@ -270,7 +178,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++) { @@ -279,13 +187,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; } @@ -346,7 +254,7 @@ static void get_via_generic_iokit (double *ret_charge, &iterator); if (status != kIOReturnSuccess) { - DBG ("IOServiceGetMatchingServices failed."); + DEBUG ("IOServiceGetMatchingServices failed."); return; } @@ -358,7 +266,7 @@ static void get_via_generic_iokit (double *ret_charge, kNilOptions); if (status != kIOReturnSuccess) { - DBG ("IORegistryEntryCreateCFProperties failed."); + DEBUG ("IORegistryEntryCreateCFProperties failed."); continue; } @@ -409,7 +317,101 @@ static void get_via_generic_iokit (double *ret_charge, } #endif /* HAVE_IOKIT_IOKITLIB_H */ -static void battery_read (void) +#if KERNEL_LINUX +static int battery_read_acpi (const char __attribute__((unused)) *dir, + const char *name, void __attribute__((unused)) *user_data) +{ + double current = INVALID_VALUE; + double voltage = INVALID_VALUE; + double charge = INVALID_VALUE; + double *valptr = NULL; + int charging = 0; + + char filename[256]; + FILE *fh; + + char buffer[1024]; + char *fields[8]; + int numfields; + char *endptr; + int len; + + len = ssnprintf (filename, sizeof (filename), "%s/%s/state", battery_acpi_dir, name); + + if ((len < 0) || ((unsigned int)len >= sizeof (filename))) + return -1; + + if ((fh = fopen (filename, "r")) == NULL) { + char errbuf[1024]; + ERROR ("Cannot open `%s': %s", filename, + sstrerror (errno, errbuf, sizeof (errbuf))); + return -1; + } + + /* + * [11:00] <@tokkee> $ cat /proc/acpi/battery/BAT1/state + * [11:00] <@tokkee> present: yes + * [11:00] <@tokkee> capacity state: ok + * [11:00] <@tokkee> charging state: charging + * [11:00] <@tokkee> present rate: 1724 mA + * [11:00] <@tokkee> remaining capacity: 4136 mAh + * [11:00] <@tokkee> present voltage: 12428 mV + */ + while (fgets (buffer, sizeof (buffer), fh) != NULL) + { + numfields = strsplit (buffer, fields, 8); + + if (numfields < 3) + continue; + + if ((strcmp (fields[0], "charging") == 0) + && (strcmp (fields[1], "state:") == 0)) + { + if (strcmp (fields[2], "charging") == 0) + charging = 1; + else + charging = 0; + continue; + } + + if ((strcmp (fields[0], "present") == 0) + && (strcmp (fields[1], "rate:") == 0)) + valptr = ¤t; + else if ((strcmp (fields[0], "remaining") == 0) + && (strcmp (fields[1], "capacity:") == 0)) + valptr = &charge; + else if ((strcmp (fields[0], "present") == 0) + && (strcmp (fields[1], "voltage:") == 0)) + valptr = &voltage; + else + continue; + + endptr = NULL; + errno = 0; + *valptr = strtod (fields[2], &endptr) / 1000.0; + + if ((fields[2] == endptr) || (errno != 0)) + *valptr = INVALID_VALUE; + } /* while (fgets (buffer, sizeof (buffer), fh) != NULL) */ + + fclose (fh); + + if ((current != INVALID_VALUE) && (charging == 0)) + current *= -1; + + if (charge != INVALID_VALUE) + battery_submit ("0", "charge", charge); + if (current != INVALID_VALUE) + battery_submit ("0", "current", current); + if (voltage != INVALID_VALUE) + battery_submit ("0", "voltage", voltage); + + return 0; +} +#endif /* KERNEL_LINUX */ + + +static int battery_read (void) { #if HAVE_IOKIT_IOKITLIB_H || HAVE_IOKIT_PS_IOPOWERSOURCES_H double charge = INVALID_VALUE; /* Current charge in Ah */ @@ -429,16 +431,20 @@ static void battery_read (void) if ((charge_rel != INVALID_VALUE) && (charge_abs != INVALID_VALUE)) charge = charge_abs * charge_rel / 100.0; - if ((charge != INVALID_VALUE) - || (current != INVALID_VALUE) - || (voltage != INVALID_VALUE)) - battery_submit ("0", current, voltage, charge); + if (charge != INVALID_VALUE) + battery_submit ("0", "charge", charge); + if (current != INVALID_VALUE) + battery_submit ("0", "current", current); + if (voltage != INVALID_VALUE) + battery_submit ("0", "voltage", voltage); /* #endif HAVE_IOKIT_IOKITLIB_H || HAVE_IOKIT_PS_IOPOWERSOURCES_H */ #elif KERNEL_LINUX + static c_complain_t acpi_dir_complaint = C_COMPLAIN_INIT_STATIC; + FILE *fh; - char buffer[BUFSIZE]; - char filename[BUFSIZE]; + char buffer[1024]; + char filename[256]; char *fields[8]; int numfields; @@ -448,24 +454,24 @@ static void battery_read (void) for (i = 0; i < battery_pmu_num; i++) { - char batnum_str[BUFSIZE]; + char batnum_str[256]; double current = INVALID_VALUE; double voltage = INVALID_VALUE; double charge = INVALID_VALUE; double *valptr = NULL; - len = snprintf (filename, BUFSIZE, battery_pmu_file, i); - if ((len >= BUFSIZE) || (len < 0)) + len = ssnprintf (filename, sizeof (filename), battery_pmu_file, i); + if ((len < 0) || ((unsigned int)len >= sizeof (filename))) continue; - len = snprintf (batnum_str, BUFSIZE, "%i", i); - if ((len >= BUFSIZE) || (len < 0)) + len = ssnprintf (batnum_str, sizeof (batnum_str), "%i", i); + if ((len < 0) || ((unsigned int)len >= sizeof (batnum_str))) continue; if ((fh = fopen (filename, "r")) == NULL) continue; - while (fgets (buffer, BUFSIZE, fh) != NULL) + while (fgets (buffer, sizeof (buffer), fh) != NULL) { numfields = strsplit (buffer, fields, 8); @@ -495,124 +501,37 @@ static void battery_read (void) } } - if ((current != INVALID_VALUE) - || (voltage != INVALID_VALUE) - || (charge != INVALID_VALUE)) - battery_submit (batnum_str, current, voltage, charge); - fclose (fh); fh = NULL; + + if (charge != INVALID_VALUE) + battery_submit ("0", "charge", charge); + if (current != INVALID_VALUE) + battery_submit ("0", "current", current); + if (voltage != INVALID_VALUE) + battery_submit ("0", "voltage", voltage); } - if (access ("/proc/acpi/battery", R_OK | X_OK) == 0) + if (0 == access (battery_acpi_dir, R_OK)) + walk_directory (battery_acpi_dir, battery_read_acpi, + /* user_data = */ NULL, + /* include hidden */ 0); + else { - double current = INVALID_VALUE; - double voltage = INVALID_VALUE; - double charge = INVALID_VALUE; - double *valptr = NULL; - int charging = 0; - - struct dirent *ent; - DIR *dh; - - if ((dh = opendir ("/proc/acpi/battery")) == NULL) - { - syslog (LOG_ERR, "Cannot open `/proc/acpi/battery': %s", strerror (errno)); - return; - } - - while ((ent = readdir (dh)) != NULL) - { - if (ent->d_name[0] == '.') - continue; - - len = snprintf (filename, BUFSIZE, "/proc/acpi/battery/%s/state", ent->d_name); - if ((len >= BUFSIZE) || (len < 0)) - continue; - - if ((fh = fopen (filename, "r")) == NULL) - { - syslog (LOG_ERR, "Cannot open `%s': %s", filename, strerror (errno)); - continue; - } - - /* - * [11:00] <@tokkee> $ cat /proc/acpi/battery/BAT1/state - * [11:00] <@tokkee> present: yes - * [11:00] <@tokkee> capacity state: ok - * [11:00] <@tokkee> charging state: charging - * [11:00] <@tokkee> present rate: 1724 mA - * [11:00] <@tokkee> remaining capacity: 4136 mAh - * [11:00] <@tokkee> present voltage: 12428 mV - */ - while (fgets (buffer, BUFSIZE, fh) != NULL) - { - numfields = strsplit (buffer, fields, 8); - - if (numfields < 3) - continue; - - if ((strcmp (fields[0], "present") == 0) - && (strcmp (fields[1], "rate:") == 0)) - valptr = ¤t; - else if ((strcmp (fields[0], "remaining") == 0) - && (strcmp (fields[1], "capacity:") == 0)) - valptr = &charge; - else if ((strcmp (fields[0], "present") == 0) - && (strcmp (fields[1], "voltage:") == 0)) - valptr = &voltage; - else - valptr = NULL; - - if ((strcmp (fields[0], "charging") == 0) - && (strcmp (fields[1], "state:") == 0)) - { - if (strcmp (fields[2], "charging") == 0) - charging = 1; - else - charging = 0; - } - - if (valptr != NULL) - { - char *endptr; - - endptr = NULL; - errno = 0; - - *valptr = strtod (fields[2], &endptr) / 1000.0; - - if ((fields[2] == endptr) || (errno != 0)) - *valptr = INVALID_VALUE; - } - } - - if ((current != INVALID_VALUE) && (charging == 0)) - current *= -1; - - if ((current != INVALID_VALUE) - || (voltage != INVALID_VALUE) - || (charge != INVALID_VALUE)) - battery_submit (ent->d_name, current, voltage, charge); - - fclose (fh); - } - - closedir (dh); + char errbuf[1024]; + c_complain_once (LOG_WARNING, &acpi_dir_complaint, + "battery plugin: Failed to access `%s': %s", + battery_acpi_dir, + sstrerror (errno, errbuf, sizeof (errbuf))); } + #endif /* KERNEL_LINUX */ + + return (0); } -#else -# define battery_read NULL -#endif /* BATTERY_HAVE_READ */ void module_register (void) { - plugin_register (MODULE_NAME, battery_init, battery_read, NULL); - plugin_register ("battery_current", NULL, NULL, battery_current_write); - plugin_register ("battery_voltage", NULL, NULL, battery_voltage_write); - plugin_register ("battery_charge", NULL, NULL, battery_charge_write); -} - -#undef BUFSIZE -#undef MODULE_NAME + plugin_register_init ("battery", battery_init); + plugin_register_read ("battery", battery_read); +} /* void module_register */