Tree wide: Use compound literals when dealing with value_t.
[collectd.git] / src / battery.c
1 /**
2  * collectd - src/battery.c
3  * Copyright (C) 2006-2014  Florian octo Forster
4  * Copyright (C) 2008       Michał Mirosław
5  * Copyright (C) 2014       Andy Parkins
6  *
7  * This program is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License as published by the
9  * Free Software Foundation; only version 2 of the License is applicable.
10  *
11  * This program is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this program; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
19  *
20  * Authors:
21  *   Florian octo Forster <octo at collectd.org>
22  *   Michał Mirosław <mirq-linux at rere.qmqm.pl>
23  *   Andy Parkins <andyp at fussylogic.co.uk>
24  **/
25
26 #include "collectd.h"
27
28 #include "common.h"
29 #include "plugin.h"
30
31 #if HAVE_MACH_MACH_TYPES_H
32 #  include <mach/mach_types.h>
33 #endif
34 #if HAVE_MACH_MACH_INIT_H
35 #  include <mach/mach_init.h>
36 #endif
37 #if HAVE_MACH_MACH_ERROR_H
38 #  include <mach/mach_error.h>
39 #endif
40 #if HAVE_COREFOUNDATION_COREFOUNDATION_H
41 #  include <CoreFoundation/CoreFoundation.h>
42 #endif
43 #if HAVE_IOKIT_IOKITLIB_H
44 #  include <IOKit/IOKitLib.h>
45 #endif
46 #if HAVE_IOKIT_IOTYPES_H
47 #  include <IOKit/IOTypes.h>
48 #endif
49 #if HAVE_IOKIT_PS_IOPOWERSOURCES_H
50 #  include <IOKit/ps/IOPowerSources.h>
51 #endif
52 #if HAVE_IOKIT_PS_IOPSKEYS_H
53 #  include <IOKit/ps/IOPSKeys.h>
54 #endif
55
56 #if !HAVE_IOKIT_IOKITLIB_H && !HAVE_IOKIT_PS_IOPOWERSOURCES_H && !KERNEL_LINUX
57 # error "No applicable input method."
58 #endif
59
60 #if HAVE_IOKIT_IOKITLIB_H || HAVE_IOKIT_PS_IOPOWERSOURCES_H
61         /* No global variables */
62 /* #endif HAVE_IOKIT_IOKITLIB_H || HAVE_IOKIT_PS_IOPOWERSOURCES_H */
63
64 #elif KERNEL_LINUX
65 # define PROC_PMU_PATH_FORMAT "/proc/pmu/battery_%i"
66 # define PROC_ACPI_PATH "/proc/acpi/battery"
67 # define PROC_ACPI_FACTOR 0.001
68 # define SYSFS_PATH "/sys/class/power_supply"
69 # define SYSFS_FACTOR 0.000001
70 #endif /* KERNEL_LINUX */
71
72 int battery_read_statefs (void); /* defined in battery_statefs; used by StateFS backend */
73
74 static _Bool report_percent = 0;
75 static _Bool report_degraded = 0;
76 static _Bool query_statefs = 0;
77
78 static void battery_submit2 (char const *plugin_instance, /* {{{ */
79                 char const *type, char const *type_instance, gauge_t value)
80 {
81         value_list_t vl = VALUE_LIST_INIT;
82
83         vl.values = &(value_t) { .gauge = value };
84         vl.values_len = 1;
85         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
86         sstrncpy (vl.plugin, "battery", sizeof (vl.plugin));
87         sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
88         sstrncpy (vl.type, type, sizeof (vl.type));
89         if (type_instance != NULL)
90                 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
91
92         plugin_dispatch_values (&vl);
93 } /* }}} void battery_submit2 */
94
95 static void battery_submit (char const *plugin_instance, /* {{{ */
96                 char const *type, gauge_t value)
97 {
98         battery_submit2 (plugin_instance, type, NULL, value);
99 } /* }}} void battery_submit */
100
101 static void submit_capacity (char const *plugin_instance, /* {{{ */
102                 gauge_t capacity_charged,
103                 gauge_t capacity_full,
104                 gauge_t capacity_design)
105 {
106         if (report_percent && (capacity_charged > capacity_full))
107                 return;
108         if (report_degraded && (capacity_full > capacity_design))
109                 return;
110
111         if (report_percent)
112         {
113                 gauge_t capacity_max;
114
115                 if (report_degraded)
116                         capacity_max = capacity_design;
117                 else
118                         capacity_max = capacity_full;
119
120                 battery_submit2 (plugin_instance, "percent", "charged",
121                                 100.0 * capacity_charged / capacity_max);
122                 battery_submit2 (plugin_instance, "percent", "discharged",
123                                 100.0 * (capacity_full - capacity_charged) / capacity_max);
124
125                 if (report_degraded)
126                         battery_submit2 (plugin_instance, "percent", "degraded",
127                                         100.0 * (capacity_design - capacity_full) / capacity_max);
128         }
129         else if (report_degraded) /* && !report_percent */
130         {
131                 battery_submit2 (plugin_instance, "capacity", "charged",
132                                 capacity_charged);
133                 battery_submit2 (plugin_instance, "capacity", "discharged",
134                                 (capacity_full - capacity_charged));
135                 battery_submit2 (plugin_instance, "capacity", "degraded",
136                                 (capacity_design - capacity_full));
137         }
138         else /* !report_percent && !report_degraded */
139         {
140                 battery_submit (plugin_instance, "capacity", capacity_charged);
141         }
142 } /* }}} void submit_capacity */
143
144 #if HAVE_IOKIT_PS_IOPOWERSOURCES_H || HAVE_IOKIT_IOKITLIB_H
145 static double dict_get_double (CFDictionaryRef dict, const char *key_string) /* {{{ */
146 {
147         double      val_double;
148         long long   val_int;
149         CFNumberRef val_obj;
150         CFStringRef key_obj;
151
152         key_obj = CFStringCreateWithCString (kCFAllocatorDefault, key_string,
153                         kCFStringEncodingASCII);
154         if (key_obj == NULL)
155         {
156                 DEBUG ("CFStringCreateWithCString (%s) failed.\n", key_string);
157                 return (NAN);
158         }
159
160         if ((val_obj = CFDictionaryGetValue (dict, key_obj)) == NULL)
161         {
162                 DEBUG ("CFDictionaryGetValue (%s) failed.", key_string);
163                 CFRelease (key_obj);
164                 return (NAN);
165         }
166         CFRelease (key_obj);
167
168         if (CFGetTypeID (val_obj) == CFNumberGetTypeID ())
169         {
170                 if (CFNumberIsFloatType (val_obj))
171                 {
172                         CFNumberGetValue (val_obj,
173                                         kCFNumberDoubleType,
174                                         &val_double);
175                 }
176                 else
177                 {
178                         CFNumberGetValue (val_obj,
179                                         kCFNumberLongLongType,
180                                         &val_int);
181                         val_double = val_int;
182                 }
183         }
184         else
185         {
186                 DEBUG ("CFGetTypeID (val_obj) = %i", (int) CFGetTypeID (val_obj));
187                 return (NAN);
188         }
189
190         return (val_double);
191 } /* }}} double dict_get_double */
192
193 # if HAVE_IOKIT_PS_IOPOWERSOURCES_H
194 static void get_via_io_power_sources (double *ret_charge, /* {{{ */
195                 double *ret_current,
196                 double *ret_voltage)
197 {
198         CFTypeRef       ps_raw;
199         CFArrayRef      ps_array;
200         int             ps_array_len;
201         CFDictionaryRef ps_dict;
202         CFTypeRef       ps_obj;
203
204         double temp_double;
205
206         ps_raw       = IOPSCopyPowerSourcesInfo ();
207         ps_array     = IOPSCopyPowerSourcesList (ps_raw);
208         ps_array_len = CFArrayGetCount (ps_array);
209
210         DEBUG ("ps_array_len == %i", ps_array_len);
211
212         for (int i = 0; i < ps_array_len; i++)
213         {
214                 ps_obj  = CFArrayGetValueAtIndex (ps_array, i);
215                 ps_dict = IOPSGetPowerSourceDescription (ps_raw, ps_obj);
216
217                 if (ps_dict == NULL)
218                 {
219                         DEBUG ("IOPSGetPowerSourceDescription failed.");
220                         continue;
221                 }
222
223                 if (CFGetTypeID (ps_dict) != CFDictionaryGetTypeID ())
224                 {
225                         DEBUG ("IOPSGetPowerSourceDescription did not return a CFDictionaryRef");
226                         continue;
227                 }
228
229                 /* FIXME: Check if this is really an internal battery */
230
231                 if (isnan (*ret_charge))
232                 {
233                         /* This is the charge in percent. */
234                         temp_double = dict_get_double (ps_dict,
235                                         kIOPSCurrentCapacityKey);
236                         if (!isnan ((temp_double))
237                                         && (temp_double >= 0.0)
238                                         && (temp_double <= 100.0))
239                                 *ret_charge = temp_double;
240                 }
241
242                 if (isnan (*ret_current))
243                 {
244                         temp_double = dict_get_double (ps_dict,
245                                         kIOPSCurrentKey);
246                         if (!isnan (temp_double))
247                                 *ret_current = temp_double / 1000.0;
248                 }
249
250                 if (isnan (*ret_voltage))
251                 {
252                         temp_double = dict_get_double (ps_dict,
253                                         kIOPSVoltageKey);
254                         if (!isnan (temp_double))
255                                 *ret_voltage = temp_double / 1000.0;
256                 }
257         }
258
259         CFRelease(ps_array);
260         CFRelease(ps_raw);
261 } /* }}} void get_via_io_power_sources */
262 # endif /* HAVE_IOKIT_PS_IOPOWERSOURCES_H */
263
264 # if HAVE_IOKIT_IOKITLIB_H
265 static void get_via_generic_iokit (double *ret_capacity_full, /* {{{ */
266                 double *ret_capacity_design,
267                 double *ret_current,
268                 double *ret_voltage)
269 {
270         kern_return_t   status;
271         io_iterator_t   iterator;
272         io_object_t     io_obj;
273
274         CFDictionaryRef bat_root_dict;
275         CFArrayRef      bat_info_arry;
276         CFIndex         bat_info_arry_len;
277         CFDictionaryRef bat_info_dict;
278
279         double temp_double;
280
281         status = IOServiceGetMatchingServices (kIOMasterPortDefault,
282                         IOServiceNameMatching ("battery"),
283                         &iterator);
284         if (status != kIOReturnSuccess)
285         {
286                 DEBUG ("IOServiceGetMatchingServices failed.");
287                 return;
288         }
289
290         while ((io_obj = IOIteratorNext (iterator)))
291         {
292                 status = IORegistryEntryCreateCFProperties (io_obj,
293                                 (CFMutableDictionaryRef *) &bat_root_dict,
294                                 kCFAllocatorDefault,
295                                 kNilOptions);
296                 if (status != kIOReturnSuccess)
297                 {
298                         DEBUG ("IORegistryEntryCreateCFProperties failed.");
299                         continue;
300                 }
301
302                 bat_info_arry = (CFArrayRef) CFDictionaryGetValue (bat_root_dict,
303                                 CFSTR ("IOBatteryInfo"));
304                 if (bat_info_arry == NULL)
305                 {
306                         CFRelease (bat_root_dict);
307                         continue;
308                 }
309                 bat_info_arry_len = CFArrayGetCount (bat_info_arry);
310
311                 for (CFIndex bat_info_arry_pos = 0;
312                                 bat_info_arry_pos < bat_info_arry_len;
313                                 bat_info_arry_pos++)
314                 {
315                         bat_info_dict = (CFDictionaryRef) CFArrayGetValueAtIndex (bat_info_arry, bat_info_arry_pos);
316
317                         if (isnan (*ret_capacity_full))
318                         {
319                                 temp_double = dict_get_double (bat_info_dict, "Capacity");
320                                 *ret_capacity_full = temp_double / 1000.0;
321                         }
322
323                         if (isnan (*ret_capacity_design))
324                         {
325                                 temp_double = dict_get_double (bat_info_dict, "AbsoluteMaxCapacity");
326                                 *ret_capacity_design = temp_double / 1000.0;
327                         }
328
329                         if (isnan (*ret_current))
330                         {
331                                 temp_double = dict_get_double (bat_info_dict, "Current");
332                                 *ret_current = temp_double / 1000.0;
333                         }
334
335                         if (isnan (*ret_voltage))
336                         {
337                                 temp_double = dict_get_double (bat_info_dict, "Voltage");
338                                 *ret_voltage = temp_double / 1000.0;
339                         }
340                 }
341
342                 CFRelease (bat_root_dict);
343         }
344
345         IOObjectRelease (iterator);
346 } /* }}} void get_via_generic_iokit */
347 # endif /* HAVE_IOKIT_IOKITLIB_H */
348
349 static int battery_read (void) /* {{{ */
350 {
351         gauge_t current = NAN; /* Current in A */
352         gauge_t voltage = NAN; /* Voltage in V */
353
354         /* We only get the charged capacity as a percentage from
355          * IOPowerSources. IOKit, on the other hand, only reports the full
356          * capacity. We use the two to calculate the current charged capacity. */
357         gauge_t charge_rel = NAN; /* Current charge in percent */
358         gauge_t capacity_charged; /* Charged capacity */
359         gauge_t capacity_full = NAN; /* Total capacity */
360         gauge_t capacity_design = NAN; /* Full design capacity */
361
362         if (query_statefs)
363                 return battery_read_statefs ();
364
365 #if HAVE_IOKIT_PS_IOPOWERSOURCES_H
366         get_via_io_power_sources (&charge_rel, &current, &voltage);
367 #endif
368 #if HAVE_IOKIT_IOKITLIB_H
369         get_via_generic_iokit (&capacity_full, &capacity_design, &current, &voltage);
370 #endif
371
372         capacity_charged = charge_rel * capacity_full / 100.0;
373         submit_capacity ("0", capacity_charged, capacity_full, capacity_design);
374
375         if (!isnan (current))
376                 battery_submit ("0", "current", current);
377         if (!isnan (voltage))
378                 battery_submit ("0", "voltage", voltage);
379
380         return (0);
381 } /* }}} int battery_read */
382 /* #endif HAVE_IOKIT_IOKITLIB_H || HAVE_IOKIT_PS_IOPOWERSOURCES_H */
383
384 #elif KERNEL_LINUX
385 /* Reads a file which contains only a number (and optionally a trailing
386  * newline) and parses that number. */
387 static int sysfs_file_to_buffer(char const *dir, /* {{{ */
388                 char const *power_supply,
389                 char const *basename,
390                 char *buffer, size_t buffer_size)
391 {
392         char filename[PATH_MAX];
393         int status;
394
395         ssnprintf (filename, sizeof (filename), "%s/%s/%s",
396                         dir, power_supply, basename);
397
398         status = (int) read_file_contents (filename, buffer, buffer_size);
399         if (status < 0)
400                 return status;
401
402         strstripnewline (buffer);
403         return 0;
404 } /* }}} int sysfs_file_to_buffer */
405
406 /* Reads a file which contains only a number (and optionally a trailing
407  * newline) and parses that number. */
408 static int sysfs_file_to_gauge(char const *dir, /* {{{ */
409                 char const *power_supply,
410                 char const *basename, gauge_t *ret_value)
411 {
412         int status;
413         char buffer[32] = "";
414
415         status = sysfs_file_to_buffer (dir, power_supply, basename, buffer, sizeof (buffer));
416         if (status != 0)
417                 return (status);
418
419         return (strtogauge (buffer, ret_value));
420 } /* }}} sysfs_file_to_gauge */
421
422 static int read_sysfs_capacity (char const *dir, /* {{{ */
423                 char const *power_supply,
424                 char const *plugin_instance)
425 {
426         gauge_t capacity_charged = NAN;
427         gauge_t capacity_full = NAN;
428         gauge_t capacity_design = NAN;
429         int status;
430
431         status = sysfs_file_to_gauge (dir, power_supply, "energy_now", &capacity_charged);
432         if (status != 0)
433                 return (status);
434
435         status = sysfs_file_to_gauge (dir, power_supply, "energy_full", &capacity_full);
436         if (status != 0)
437                 return (status);
438
439         status = sysfs_file_to_gauge (dir, power_supply, "energy_full_design", &capacity_design);
440         if (status != 0)
441                 return (status);
442
443         submit_capacity (plugin_instance,
444                         capacity_charged * SYSFS_FACTOR,
445                         capacity_full * SYSFS_FACTOR,
446                         capacity_design * SYSFS_FACTOR);
447         return (0);
448 } /* }}} int read_sysfs_capacity */
449
450 static int read_sysfs_callback (char const *dir, /* {{{ */
451                 char const *power_supply,
452                 void *user_data)
453 {
454         int *battery_index = user_data;
455
456         char const *plugin_instance;
457         char buffer[32];
458         gauge_t v = NAN;
459         _Bool discharging = 0;
460         int status;
461
462         /* Ignore non-battery directories, such as AC power. */
463         status = sysfs_file_to_buffer (dir, power_supply, "type", buffer, sizeof (buffer));
464         if (status != 0)
465                 return (0);
466         if (strcasecmp ("Battery", buffer) != 0)
467                 return (0);
468
469         (void) sysfs_file_to_buffer (dir, power_supply, "status", buffer, sizeof (buffer));
470         if (strcasecmp ("Discharging", buffer) == 0)
471                 discharging = 1;
472
473         /* FIXME: This is a dirty hack for backwards compatibility: The battery
474          * plugin, for a very long time, has had the plugin_instance
475          * hard-coded to "0". So, to keep backwards compatibility, we'll use
476          * "0" for the first battery we find and the power_supply name for all
477          * following. This should be reverted in a future major version. */
478         plugin_instance = (*battery_index == 0) ? "0" : power_supply;
479         (*battery_index)++;
480
481         read_sysfs_capacity (dir, power_supply, plugin_instance);
482
483         if (sysfs_file_to_gauge (dir, power_supply, "power_now", &v) == 0)
484         {
485                 if (discharging)
486                         v *= -1.0;
487                 battery_submit (plugin_instance, "power", v * SYSFS_FACTOR);
488         }
489         if (sysfs_file_to_gauge (dir, power_supply, "current_now", &v) == 0)
490         {
491                 if (discharging)
492                         v *= -1.0;
493                 battery_submit (plugin_instance, "current", v * SYSFS_FACTOR);
494         }
495
496         if (sysfs_file_to_gauge (dir, power_supply, "voltage_now", &v) == 0)
497                 battery_submit (plugin_instance, "voltage", v * SYSFS_FACTOR);
498
499         return (0);
500 } /* }}} int read_sysfs_callback */
501
502 static int read_sysfs (void) /* {{{ */
503 {
504         int status;
505         int battery_counter = 0;
506
507         if (access (SYSFS_PATH, R_OK) != 0)
508                 return (ENOENT);
509
510         status = walk_directory (SYSFS_PATH, read_sysfs_callback,
511                         /* user_data = */ &battery_counter,
512                         /* include hidden */ 0);
513         return (status);
514 } /* }}} int read_sysfs */
515
516 static int read_acpi_full_capacity (char const *dir, /* {{{ */
517                 char const *power_supply,
518                 gauge_t *ret_capacity_full,
519                 gauge_t *ret_capacity_design)
520
521 {
522         char filename[PATH_MAX];
523         char buffer[1024];
524
525         FILE *fh;
526
527         ssnprintf (filename, sizeof (filename), "%s/%s/info", dir, power_supply);
528         fh = fopen (filename, "r");
529         if (fh == NULL)
530                 return (errno);
531
532         /* last full capacity:      40090 mWh */
533         while (fgets (buffer, sizeof (buffer), fh) != NULL)
534         {
535                 gauge_t *value_ptr;
536                 int fields_num;
537                 char *fields[8];
538                 int index;
539
540                 if (strncmp ("last full capacity:", buffer, strlen ("last full capacity:")) == 0)
541                 {
542                         value_ptr = ret_capacity_full;
543                         index = 3;
544                 }
545                 else if (strncmp ("design capacity:", buffer, strlen ("design capacity:")) == 0)
546                 {
547                         value_ptr = ret_capacity_design;
548                         index = 2;
549                 }
550                 else
551                 {
552                         continue;
553                 }
554
555                 fields_num = strsplit (buffer, fields, STATIC_ARRAY_SIZE (fields));
556                 if (fields_num <= index)
557                         continue;
558
559                 strtogauge (fields[index], value_ptr);
560         }
561
562         fclose (fh);
563         return (0);
564 } /* }}} int read_acpi_full_capacity */
565
566 static int read_acpi_callback (char const *dir, /* {{{ */
567                 char const *power_supply,
568                 void *user_data)
569 {
570         int *battery_index = user_data;
571
572         gauge_t power = NAN;
573         gauge_t voltage = NAN;
574         gauge_t capacity_charged = NAN;
575         gauge_t capacity_full = NAN;
576         gauge_t capacity_design = NAN;
577         _Bool charging = 0;
578         _Bool is_current = 0;
579
580         char const *plugin_instance;
581         char filename[PATH_MAX];
582         char buffer[1024];
583
584         FILE *fh;
585
586         ssnprintf (filename, sizeof (filename), "%s/%s/state", dir, power_supply);
587         fh = fopen (filename, "r");
588         if (fh == NULL)
589         {
590                 if ((errno == EAGAIN) || (errno == EINTR) || (errno == ENOENT))
591                         return (0);
592                 else
593                         return (errno);
594         }
595
596         /*
597          * [11:00] <@tokkee> $ cat /proc/acpi/battery/BAT1/state
598          * [11:00] <@tokkee> present:                 yes
599          * [11:00] <@tokkee> capacity state:          ok
600          * [11:00] <@tokkee> charging state:          charging
601          * [11:00] <@tokkee> present rate:            1724 mA
602          * [11:00] <@tokkee> remaining capacity:      4136 mAh
603          * [11:00] <@tokkee> present voltage:         12428 mV
604          */
605         while (fgets (buffer, sizeof (buffer), fh) != NULL)
606         {
607                 char *fields[8];
608                 int numfields;
609
610                 numfields = strsplit (buffer, fields, STATIC_ARRAY_SIZE (fields));
611                 if (numfields < 3)
612                         continue;
613
614                 if ((strcmp (fields[0], "charging") == 0)
615                                 && (strcmp (fields[1], "state:") == 0))
616                 {
617                         if (strcmp (fields[2], "charging") == 0)
618                                 charging = 1;
619                         else
620                                 charging = 0;
621                         continue;
622                 }
623
624                 /* The unit of "present rate" depends on the battery. Modern
625                  * batteries export power (watts), older batteries (used to)
626                  * export current (amperes). We check the fourth column and try
627                  * to find old batteries this way. */
628                 if ((strcmp (fields[0], "present") == 0)
629                                 && (strcmp (fields[1], "rate:") == 0))
630                 {
631                         strtogauge (fields[2], &power);
632
633                         if ((numfields >= 4) && (strcmp ("mA", fields[3]) == 0))
634                                 is_current = 1;
635                 }
636                 else if ((strcmp (fields[0], "remaining") == 0)
637                                 && (strcmp (fields[1], "capacity:") == 0))
638                         strtogauge (fields[2], &capacity_charged);
639                 else if ((strcmp (fields[0], "present") == 0)
640                                 && (strcmp (fields[1], "voltage:") == 0))
641                         strtogauge (fields[2], &voltage);
642         } /* while (fgets (buffer, sizeof (buffer), fh) != NULL) */
643
644         fclose (fh);
645
646         if (!charging)
647                 power *= -1.0;
648
649         /* FIXME: This is a dirty hack for backwards compatibility: The battery
650          * plugin, for a very long time, has had the plugin_instance
651          * hard-coded to "0". So, to keep backwards compatibility, we'll use
652          * "0" for the first battery we find and the power_supply name for all
653          * following. This should be reverted in a future major version. */
654         plugin_instance = (*battery_index == 0) ? "0" : power_supply;
655         (*battery_index)++;
656
657         read_acpi_full_capacity (dir, power_supply, &capacity_full, &capacity_design);
658
659         submit_capacity (plugin_instance,
660                         capacity_charged * PROC_ACPI_FACTOR,
661                         capacity_full * PROC_ACPI_FACTOR,
662                         capacity_design * PROC_ACPI_FACTOR);
663
664         battery_submit (plugin_instance,
665                         is_current ? "current" : "power",
666                         power * PROC_ACPI_FACTOR);
667         battery_submit (plugin_instance, "voltage", voltage * PROC_ACPI_FACTOR);
668
669         return 0;
670 } /* }}} int read_acpi_callback */
671
672 static int read_acpi (void) /* {{{ */
673 {
674         int status;
675         int battery_counter = 0;
676
677         if (access (PROC_ACPI_PATH, R_OK) != 0)
678                 return (ENOENT);
679
680         status = walk_directory (PROC_ACPI_PATH, read_acpi_callback,
681                         /* user_data = */ &battery_counter,
682                         /* include hidden */ 0);
683         return (status);
684 } /* }}} int read_acpi */
685
686 static int read_pmu (void) /* {{{ */
687 {
688         int i = 0;
689         /* The upper limit here is just a safeguard. If there is a system with
690          * more than 100 batteries, this can easily be increased. */
691         for (; i < 100; i++)
692         {
693                 FILE *fh;
694
695                 char buffer[1024];
696                 char filename[PATH_MAX];
697                 char plugin_instance[DATA_MAX_NAME_LEN];
698
699                 gauge_t current = NAN;
700                 gauge_t voltage = NAN;
701                 gauge_t charge  = NAN;
702
703                 ssnprintf (filename, sizeof (filename), PROC_PMU_PATH_FORMAT, i);
704                 if (access (filename, R_OK) != 0)
705                         break;
706
707                 ssnprintf (plugin_instance, sizeof (plugin_instance), "%i", i);
708
709                 fh = fopen (filename, "r");
710                 if (fh == NULL)
711                 {
712                         if (errno == ENOENT)
713                                 break;
714                         else if ((errno == EAGAIN) || (errno == EINTR))
715                                 continue;
716                         else
717                                 return (errno);
718                 }
719
720                 while (fgets (buffer, sizeof (buffer), fh) != NULL)
721                 {
722                         char *fields[8];
723                         int numfields;
724
725                         numfields = strsplit (buffer, fields, STATIC_ARRAY_SIZE (fields));
726                         if (numfields < 3)
727                                 continue;
728
729                         if (strcmp ("current", fields[0]) == 0)
730                                 strtogauge (fields[2], &current);
731                         else if (strcmp ("voltage", fields[0]) == 0)
732                                 strtogauge (fields[2], &voltage);
733                         else if (strcmp ("charge", fields[0]) == 0)
734                                 strtogauge (fields[2], &charge);
735                 }
736
737                 fclose (fh);
738                 fh = NULL;
739
740                 battery_submit (plugin_instance, "charge", charge / 1000.0);
741                 battery_submit (plugin_instance, "current", current / 1000.0);
742                 battery_submit (plugin_instance, "voltage", voltage / 1000.0);
743         }
744
745         if (i == 0)
746                 return (ENOENT);
747         return (0);
748 } /* }}} int read_pmu */
749
750 static int battery_read (void) /* {{{ */
751 {
752         int status;
753
754         if (query_statefs)
755                 return battery_read_statefs ();
756
757         DEBUG ("battery plugin: Trying sysfs ...");
758         status = read_sysfs ();
759         if (status == 0)
760                 return (0);
761
762         DEBUG ("battery plugin: Trying acpi ...");
763         status = read_acpi ();
764         if (status == 0)
765                 return (0);
766
767         DEBUG ("battery plugin: Trying pmu ...");
768         status = read_pmu ();
769         if (status == 0)
770                 return (0);
771
772         ERROR ("battery plugin: Add available input methods failed.");
773         return (-1);
774 } /* }}} int battery_read */
775 #endif /* KERNEL_LINUX */
776
777 static int battery_config (oconfig_item_t *ci)
778 {
779         for (int i = 0; i < ci->children_num; i++)
780         {
781                 oconfig_item_t *child = ci->children + i;
782
783                 if (strcasecmp ("ValuesPercentage", child->key) == 0)
784                         cf_util_get_boolean (child, &report_percent);
785                 else if (strcasecmp ("ReportDegraded", child->key) == 0)
786                         cf_util_get_boolean (child, &report_degraded);
787                 else if (strcasecmp ("QueryStateFS", child->key) == 0)
788                         cf_util_get_boolean (child, &query_statefs);
789                 else
790                         WARNING ("battery plugin: Ignoring unknown "
791                                         "configuration option \"%s\".",
792                                         child->key);
793         }
794
795         return (0);
796 } /* }}} int battery_config */
797
798 void module_register (void)
799 {
800         plugin_register_complex_config ("battery", battery_config);
801         plugin_register_read ("battery", battery_read);
802 } /* void module_register */