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 **/
26 #include "collectd.h"
27 #include "common.h"
28 #include "plugin.h"
30 #if HAVE_MACH_MACH_TYPES_H
31 # include <mach/mach_types.h>
32 #endif
33 #if HAVE_MACH_MACH_INIT_H
34 # include <mach/mach_init.h>
35 #endif
36 #if HAVE_MACH_MACH_ERROR_H
37 # include <mach/mach_error.h>
38 #endif
39 #if HAVE_COREFOUNDATION_COREFOUNDATION_H
40 # include <CoreFoundation/CoreFoundation.h>
41 #endif
42 #if HAVE_IOKIT_IOKITLIB_H
43 # include <IOKit/IOKitLib.h>
44 #endif
45 #if HAVE_IOKIT_IOTYPES_H
46 # include <IOKit/IOTypes.h>
47 #endif
48 #if HAVE_IOKIT_PS_IOPOWERSOURCES_H
49 # include <IOKit/ps/IOPowerSources.h>
50 #endif
51 #if HAVE_IOKIT_PS_IOPSKEYS_H
52 # include <IOKit/ps/IOPSKeys.h>
53 #endif
55 #if !HAVE_IOKIT_IOKITLIB_H && !HAVE_IOKIT_PS_IOPOWERSOURCES_H && !KERNEL_LINUX
56 # error "No applicable input method."
57 #endif
59 #if HAVE_IOKIT_IOKITLIB_H || HAVE_IOKIT_PS_IOPOWERSOURCES_H
60 /* No global variables */
61 /* #endif HAVE_IOKIT_IOKITLIB_H || HAVE_IOKIT_PS_IOPOWERSOURCES_H */
63 #elif KERNEL_LINUX
64 # define PROC_PMU_PATH_FORMAT "/proc/pmu/battery_%i"
65 # define PROC_ACPI_PATH "/proc/acpi/battery"
66 # define PROC_ACPI_FACTOR 0.001
67 # define SYSFS_PATH "/sys/class/power_supply"
68 # define SYSFS_FACTOR 0.000001
69 #endif /* KERNEL_LINUX */
71 static _Bool report_percent = 0;
72 static _Bool report_degraded = 0;
74 static void battery_submit2 (char const *plugin_instance, /* {{{ */
75 char const *type, char const *type_instance, gauge_t value)
76 {
77 value_t values[1];
78 value_list_t vl = VALUE_LIST_INIT;
80 values[0].gauge = value;
82 vl.values = values;
83 vl.values_len = 1;
84 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
85 sstrncpy (vl.plugin, "battery", sizeof (vl.plugin));
86 sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
87 sstrncpy (vl.type, type, sizeof (vl.type));
88 if (type_instance != NULL)
89 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
91 plugin_dispatch_values (&vl);
92 } /* }}} void battery_submit2 */
94 static void battery_submit (char const *plugin_instance, /* {{{ */
95 char const *type, gauge_t value)
96 {
97 battery_submit2 (plugin_instance, type, NULL, value);
98 } /* }}} void battery_submit */
100 static void submit_capacity (char const *plugin_instance, /* {{{ */
101 gauge_t capacity_charged,
102 gauge_t capacity_full,
103 gauge_t capacity_design)
104 {
105 if (report_percent && (capacity_charged > capacity_full))
106 return;
107 if (report_degraded && (capacity_full > capacity_design))
108 return;
110 if (report_percent)
111 {
112 gauge_t capacity_max;
114 if (report_degraded)
115 capacity_max = capacity_design;
116 else
117 capacity_max = capacity_full;
119 battery_submit2 (plugin_instance, "percent", "charged",
120 100.0 * capacity_charged / capacity_max);
121 battery_submit2 (plugin_instance, "percent", "discharged",
122 100.0 * (capacity_full - capacity_charged) / capacity_max);
124 if (report_degraded)
125 battery_submit2 (plugin_instance, "percent", "degraded",
126 100.0 * (capacity_design - capacity_full) / capacity_max);
127 }
128 else if (report_degraded) /* && !report_percent */
129 {
130 battery_submit2 (plugin_instance, "capacity", "charged",
131 capacity_charged);
132 battery_submit2 (plugin_instance, "capacity", "discharged",
133 (capacity_full - capacity_charged));
134 battery_submit2 (plugin_instance, "capacity", "degraded",
135 (capacity_design - capacity_full));
136 }
137 else /* !report_percent && !report_degraded */
138 {
139 battery_submit (plugin_instance, "capacity", capacity_charged);
140 }
141 } /* }}} void submit_capacity */
143 #if HAVE_IOKIT_PS_IOPOWERSOURCES_H || HAVE_IOKIT_IOKITLIB_H
144 static double dict_get_double (CFDictionaryRef dict, const char *key_string) /* {{{ */
145 {
146 double val_double;
147 long long val_int;
148 CFNumberRef val_obj;
149 CFStringRef key_obj;
151 key_obj = CFStringCreateWithCString (kCFAllocatorDefault, key_string,
152 kCFStringEncodingASCII);
153 if (key_obj == NULL)
154 {
155 DEBUG ("CFStringCreateWithCString (%s) failed.\n", key_string);
156 return (NAN);
157 }
159 if ((val_obj = CFDictionaryGetValue (dict, key_obj)) == NULL)
160 {
161 DEBUG ("CFDictionaryGetValue (%s) failed.", key_string);
162 CFRelease (key_obj);
163 return (NAN);
164 }
165 CFRelease (key_obj);
167 if (CFGetTypeID (val_obj) == CFNumberGetTypeID ())
168 {
169 if (CFNumberIsFloatType (val_obj))
170 {
171 CFNumberGetValue (val_obj,
172 kCFNumberDoubleType,
173 &val_double);
174 }
175 else
176 {
177 CFNumberGetValue (val_obj,
178 kCFNumberLongLongType,
179 &val_int);
180 val_double = val_int;
181 }
182 }
183 else
184 {
185 DEBUG ("CFGetTypeID (val_obj) = %i", (int) CFGetTypeID (val_obj));
186 return (NAN);
187 }
189 return (val_double);
190 } /* }}} double dict_get_double */
192 # if HAVE_IOKIT_PS_IOPOWERSOURCES_H
193 static void get_via_io_power_sources (double *ret_charge, /* {{{ */
194 double *ret_current,
195 double *ret_voltage)
196 {
197 CFTypeRef ps_raw;
198 CFArrayRef ps_array;
199 int ps_array_len;
200 CFDictionaryRef ps_dict;
201 CFTypeRef ps_obj;
203 double temp_double;
204 int i;
206 ps_raw = IOPSCopyPowerSourcesInfo ();
207 ps_array = IOPSCopyPowerSourcesList (ps_raw);
208 ps_array_len = CFArrayGetCount (ps_array);
210 DEBUG ("ps_array_len == %i", ps_array_len);
212 for (i = 0; i < ps_array_len; i++)
213 {
214 ps_obj = CFArrayGetValueAtIndex (ps_array, i);
215 ps_dict = IOPSGetPowerSourceDescription (ps_raw, ps_obj);
217 if (ps_dict == NULL)
218 {
219 DEBUG ("IOPSGetPowerSourceDescription failed.");
220 continue;
221 }
223 if (CFGetTypeID (ps_dict) != CFDictionaryGetTypeID ())
224 {
225 DEBUG ("IOPSGetPowerSourceDescription did not return a CFDictionaryRef");
226 continue;
227 }
229 /* FIXME: Check if this is really an internal battery */
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 }
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 }
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 }
259 CFRelease(ps_array);
260 CFRelease(ps_raw);
261 } /* }}} void get_via_io_power_sources */
262 # endif /* HAVE_IOKIT_PS_IOPOWERSOURCES_H */
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;
274 CFDictionaryRef bat_root_dict;
275 CFArrayRef bat_info_arry;
276 CFIndex bat_info_arry_len;
277 CFIndex bat_info_arry_pos;
278 CFDictionaryRef bat_info_dict;
280 double temp_double;
282 status = IOServiceGetMatchingServices (kIOMasterPortDefault,
283 IOServiceNameMatching ("battery"),
284 &iterator);
285 if (status != kIOReturnSuccess)
286 {
287 DEBUG ("IOServiceGetMatchingServices failed.");
288 return;
289 }
291 while ((io_obj = IOIteratorNext (iterator)))
292 {
293 status = IORegistryEntryCreateCFProperties (io_obj,
294 (CFMutableDictionaryRef *) &bat_root_dict,
295 kCFAllocatorDefault,
296 kNilOptions);
297 if (status != kIOReturnSuccess)
298 {
299 DEBUG ("IORegistryEntryCreateCFProperties failed.");
300 continue;
301 }
303 bat_info_arry = (CFArrayRef) CFDictionaryGetValue (bat_root_dict,
304 CFSTR ("IOBatteryInfo"));
305 if (bat_info_arry == NULL)
306 {
307 CFRelease (bat_root_dict);
308 continue;
309 }
310 bat_info_arry_len = CFArrayGetCount (bat_info_arry);
312 for (bat_info_arry_pos = 0;
313 bat_info_arry_pos < bat_info_arry_len;
314 bat_info_arry_pos++)
315 {
316 bat_info_dict = (CFDictionaryRef) CFArrayGetValueAtIndex (bat_info_arry, bat_info_arry_pos);
318 if (isnan (*ret_capacity_full))
319 {
320 temp_double = dict_get_double (bat_info_dict, "Capacity");
321 *ret_capacity_full = temp_double / 1000.0;
322 }
324 if (isnan (*ret_capacity_design))
325 {
326 temp_double = dict_get_double (bat_info_dict, "AbsoluteMaxCapacity");
327 *ret_capacity_design = temp_double / 1000.0;
328 }
330 if (isnan (*ret_current))
331 {
332 temp_double = dict_get_double (bat_info_dict, "Current");
333 *ret_current = temp_double / 1000.0;
334 }
336 if (isnan (*ret_voltage))
337 {
338 temp_double = dict_get_double (bat_info_dict, "Voltage");
339 *ret_voltage = temp_double / 1000.0;
340 }
341 }
343 CFRelease (bat_root_dict);
344 }
346 IOObjectRelease (iterator);
347 } /* }}} void get_via_generic_iokit */
348 # endif /* HAVE_IOKIT_IOKITLIB_H */
350 static int battery_read (void) /* {{{ */
351 {
352 gauge_t current = NAN; /* Current in A */
353 gauge_t voltage = NAN; /* Voltage in V */
355 /* We only get the charged capacity as a percentage from
356 * IOPowerSources. IOKit, on the other hand, only reports the full
357 * capacity. We use the two to calculate the current charged capacity. */
358 gauge_t charge_rel = NAN; /* Current charge in percent */
359 gauge_t capacity_charged; /* Charged capacity */
360 gauge_t capacity_full = NAN; /* Total capacity */
361 gauge_t capacity_design = NAN; /* Full design capacity */
363 #if HAVE_IOKIT_PS_IOPOWERSOURCES_H
364 get_via_io_power_sources (&charge_rel, ¤t, &voltage);
365 #endif
366 #if HAVE_IOKIT_IOKITLIB_H
367 get_via_generic_iokit (&capacity_full, &capacity_design, ¤t, &voltage);
368 #endif
370 capacity_charged = charge_rel * capacity_full / 100.0;
371 submit_capacity ("0", capacity_charged, capacity_full, capacity_design);
373 if (!isnan (current))
374 battery_submit ("0", "current", current);
375 if (!isnan (voltage))
376 battery_submit ("0", "voltage", voltage);
378 return (0);
379 } /* }}} int battery_read */
380 /* #endif HAVE_IOKIT_IOKITLIB_H || HAVE_IOKIT_PS_IOPOWERSOURCES_H */
382 #elif KERNEL_LINUX
383 /* Reads a file which contains only a number (and optionally a trailing
384 * newline) and parses that number. */
385 static int sysfs_file_to_buffer(char const *dir, /* {{{ */
386 char const *power_supply,
387 char const *basename,
388 char *buffer, size_t buffer_size)
389 {
390 int status;
391 FILE *fp;
392 char filename[PATH_MAX];
394 ssnprintf (filename, sizeof (filename), "%s/%s/%s",
395 dir, power_supply, basename);
397 /* No file isn't the end of the world -- not every system will be
398 * reporting the same set of statistics */
399 if (access (filename, R_OK) != 0)
400 return ENOENT;
402 fp = fopen (filename, "r");
403 if (fp == NULL)
404 {
405 status = errno;
406 if (status != ENOENT)
407 {
408 char errbuf[1024];
409 WARNING ("battery plugin: fopen (%s) failed: %s", filename,
410 sstrerror (status, errbuf, sizeof (errbuf)));
411 }
412 return status;
413 }
415 if (fgets (buffer, buffer_size, fp) == NULL)
416 {
417 status = errno;
418 if (status != ENODEV)
419 {
420 char errbuf[1024];
421 WARNING ("battery plugin: fgets (%s) failed: %s", filename,
422 sstrerror (status, errbuf, sizeof (errbuf)));
423 }
424 fclose (fp);
425 return status;
426 }
428 strstripnewline (buffer);
430 fclose (fp);
431 return 0;
432 } /* }}} int sysfs_file_to_buffer */
434 /* Reads a file which contains only a number (and optionally a trailing
435 * newline) and parses that number. */
436 static int sysfs_file_to_gauge(char const *dir, /* {{{ */
437 char const *power_supply,
438 char const *basename, gauge_t *ret_value)
439 {
440 int status;
441 char buffer[32] = "";
443 status = sysfs_file_to_buffer (dir, power_supply, basename, buffer, sizeof (buffer));
444 if (status != 0)
445 return (status);
447 return (strtogauge (buffer, ret_value));
448 } /* }}} sysfs_file_to_gauge */
450 static int read_sysfs_capacity (char const *dir, /* {{{ */
451 char const *power_supply,
452 char const *plugin_instance)
453 {
454 gauge_t capacity_charged = NAN;
455 gauge_t capacity_full = NAN;
456 gauge_t capacity_design = NAN;
457 int status;
459 status = sysfs_file_to_gauge (dir, power_supply, "energy_now", &capacity_charged);
460 if (status != 0)
461 return (status);
463 status = sysfs_file_to_gauge (dir, power_supply, "energy_full", &capacity_full);
464 if (status != 0)
465 return (status);
467 status = sysfs_file_to_gauge (dir, power_supply, "energy_full_design", &capacity_design);
468 if (status != 0)
469 return (status);
471 submit_capacity (plugin_instance,
472 capacity_charged * SYSFS_FACTOR,
473 capacity_full * SYSFS_FACTOR,
474 capacity_design * SYSFS_FACTOR);
475 return (0);
476 } /* }}} int read_sysfs_capacity */
478 static int read_sysfs_callback (char const *dir, /* {{{ */
479 char const *power_supply,
480 void *user_data)
481 {
482 int *battery_index = user_data;
484 char const *plugin_instance;
485 char buffer[32];
486 gauge_t v = NAN;
487 _Bool discharging = 0;
488 int status;
490 /* Ignore non-battery directories, such as AC power. */
491 status = sysfs_file_to_buffer (dir, power_supply, "type", buffer, sizeof (buffer));
492 if (status != 0)
493 return (0);
494 if (strcasecmp ("Battery", buffer) != 0)
495 return (0);
497 (void) sysfs_file_to_buffer (dir, power_supply, "status", buffer, sizeof (buffer));
498 if (strcasecmp ("Discharging", buffer) == 0)
499 discharging = 1;
501 /* FIXME: This is a dirty hack for backwards compatibility: The battery
502 * plugin, for a very long time, has had the plugin_instance
503 * hard-coded to "0". So, to keep backwards compatibility, we'll use
504 * "0" for the first battery we find and the power_supply name for all
505 * following. This should be reverted in a future major version. */
506 plugin_instance = (*battery_index == 0) ? "0" : power_supply;
507 (*battery_index)++;
509 read_sysfs_capacity (dir, power_supply, plugin_instance);
511 if (sysfs_file_to_gauge (dir, power_supply, "power_now", &v) == 0)
512 {
513 if (discharging)
514 v *= -1.0;
515 battery_submit (plugin_instance, "power", v * SYSFS_FACTOR);
516 }
517 if (sysfs_file_to_gauge (dir, power_supply, "current_now", &v) == 0)
518 {
519 if (discharging)
520 v *= -1.0;
521 battery_submit (plugin_instance, "current", v * SYSFS_FACTOR);
522 }
524 if (sysfs_file_to_gauge (dir, power_supply, "voltage_now", &v) == 0)
525 battery_submit (plugin_instance, "voltage", v * SYSFS_FACTOR);
527 return (0);
528 } /* }}} int read_sysfs_callback */
530 static int read_sysfs (void) /* {{{ */
531 {
532 int status;
533 int battery_counter = 0;
535 if (access (SYSFS_PATH, R_OK) != 0)
536 return (ENOENT);
538 status = walk_directory (SYSFS_PATH, read_sysfs_callback,
539 /* user_data = */ &battery_counter,
540 /* include hidden */ 0);
541 return (status);
542 } /* }}} int read_sysfs */
544 static int read_acpi_full_capacity (char const *dir, /* {{{ */
545 char const *power_supply,
546 gauge_t *ret_capacity_full,
547 gauge_t *ret_capacity_design)
549 {
550 char filename[PATH_MAX];
551 char buffer[1024];
553 FILE *fh;
555 ssnprintf (filename, sizeof (filename), "%s/%s/info", dir, power_supply);
556 fh = fopen (filename, "r");
557 if (fh == NULL)
558 return (errno);
560 /* last full capacity: 40090 mWh */
561 while (fgets (buffer, sizeof (buffer), fh) != NULL)
562 {
563 gauge_t *value_ptr;
564 int fields_num;
565 char *fields[8];
566 int index;
568 if (strncmp ("last full capacity:", buffer, strlen ("last full capacity:")) == 0)
569 {
570 value_ptr = ret_capacity_full;
571 index = 3;
572 }
573 else if (strncmp ("design capacity:", buffer, strlen ("design capacity:")) == 0)
574 {
575 value_ptr = ret_capacity_design;
576 index = 2;
577 }
578 else
579 {
580 continue;
581 }
583 fields_num = strsplit (buffer, fields, STATIC_ARRAY_SIZE (fields));
584 if (fields_num <= index)
585 continue;
587 strtogauge (fields[index], value_ptr);
588 }
590 fclose (fh);
591 return (0);
592 } /* }}} int read_acpi_full_capacity */
594 static int read_acpi_callback (char const *dir, /* {{{ */
595 char const *power_supply,
596 void *user_data)
597 {
598 int *battery_index = user_data;
600 gauge_t power = NAN;
601 gauge_t voltage = NAN;
602 gauge_t capacity_charged = NAN;
603 gauge_t capacity_full = NAN;
604 gauge_t capacity_design = NAN;
605 _Bool charging = 0;
606 _Bool is_current = 0;
608 char const *plugin_instance;
609 char filename[PATH_MAX];
610 char buffer[1024];
612 FILE *fh;
614 ssnprintf (filename, sizeof (filename), "%s/%s/state", dir, power_supply);
615 fh = fopen (filename, "r");
616 if (fh == NULL)
617 {
618 if ((errno == EAGAIN) || (errno == EINTR) || (errno == ENOENT))
619 return (0);
620 else
621 return (errno);
622 }
624 /*
625 * [11:00] <@tokkee> $ cat /proc/acpi/battery/BAT1/state
626 * [11:00] <@tokkee> present: yes
627 * [11:00] <@tokkee> capacity state: ok
628 * [11:00] <@tokkee> charging state: charging
629 * [11:00] <@tokkee> present rate: 1724 mA
630 * [11:00] <@tokkee> remaining capacity: 4136 mAh
631 * [11:00] <@tokkee> present voltage: 12428 mV
632 */
633 while (fgets (buffer, sizeof (buffer), fh) != NULL)
634 {
635 char *fields[8];
636 int numfields;
638 numfields = strsplit (buffer, fields, STATIC_ARRAY_SIZE (fields));
639 if (numfields < 3)
640 continue;
642 if ((strcmp (fields[0], "charging") == 0)
643 && (strcmp (fields[1], "state:") == 0))
644 {
645 if (strcmp (fields[2], "charging") == 0)
646 charging = 1;
647 else
648 charging = 0;
649 continue;
650 }
652 /* The unit of "present rate" depends on the battery. Modern
653 * batteries export power (watts), older batteries (used to)
654 * export current (amperes). We check the fourth column and try
655 * to find old batteries this way. */
656 if ((strcmp (fields[0], "present") == 0)
657 && (strcmp (fields[1], "rate:") == 0))
658 {
659 strtogauge (fields[2], &power);
661 if ((numfields >= 4) && (strcmp ("mA", fields[3]) == 0))
662 is_current = 1;
663 }
664 else if ((strcmp (fields[0], "remaining") == 0)
665 && (strcmp (fields[1], "capacity:") == 0))
666 strtogauge (fields[2], &capacity_charged);
667 else if ((strcmp (fields[0], "present") == 0)
668 && (strcmp (fields[1], "voltage:") == 0))
669 strtogauge (fields[2], &voltage);
670 } /* while (fgets (buffer, sizeof (buffer), fh) != NULL) */
672 fclose (fh);
674 if (!charging)
675 power *= -1.0;
677 /* FIXME: This is a dirty hack for backwards compatibility: The battery
678 * plugin, for a very long time, has had the plugin_instance
679 * hard-coded to "0". So, to keep backwards compatibility, we'll use
680 * "0" for the first battery we find and the power_supply name for all
681 * following. This should be reverted in a future major version. */
682 plugin_instance = (*battery_index == 0) ? "0" : power_supply;
683 (*battery_index)++;
685 read_acpi_full_capacity (dir, power_supply, &capacity_full, &capacity_design);
687 submit_capacity (plugin_instance,
688 capacity_charged * PROC_ACPI_FACTOR,
689 capacity_full * PROC_ACPI_FACTOR,
690 capacity_design * PROC_ACPI_FACTOR);
692 battery_submit (plugin_instance,
693 is_current ? "current" : "power",
694 power * PROC_ACPI_FACTOR);
695 battery_submit (plugin_instance, "voltage", voltage * PROC_ACPI_FACTOR);
697 return 0;
698 } /* }}} int read_acpi_callback */
700 static int read_acpi (void) /* {{{ */
701 {
702 int status;
703 int battery_counter = 0;
705 if (access (PROC_ACPI_PATH, R_OK) != 0)
706 return (ENOENT);
708 status = walk_directory (PROC_ACPI_PATH, read_acpi_callback,
709 /* user_data = */ &battery_counter,
710 /* include hidden */ 0);
711 return (status);
712 } /* }}} int read_acpi */
714 static int read_pmu (void) /* {{{ */
715 {
716 int i;
718 /* The upper limit here is just a safeguard. If there is a system with
719 * more than 100 batteries, this can easily be increased. */
720 for (i = 0; i < 100; i++)
721 {
722 FILE *fh;
724 char buffer[1024];
725 char filename[PATH_MAX];
726 char plugin_instance[DATA_MAX_NAME_LEN];
728 gauge_t current = NAN;
729 gauge_t voltage = NAN;
730 gauge_t charge = NAN;
732 ssnprintf (filename, sizeof (filename), PROC_PMU_PATH_FORMAT, i);
733 if (access (filename, R_OK) != 0)
734 break;
736 ssnprintf (plugin_instance, sizeof (plugin_instance), "%i", i);
738 fh = fopen (filename, "r");
739 if (fh == NULL)
740 {
741 if (errno == ENOENT)
742 break;
743 else if ((errno == EAGAIN) || (errno == EINTR))
744 continue;
745 else
746 return (errno);
747 }
749 while (fgets (buffer, sizeof (buffer), fh) != NULL)
750 {
751 char *fields[8];
752 int numfields;
754 numfields = strsplit (buffer, fields, STATIC_ARRAY_SIZE (fields));
755 if (numfields < 3)
756 continue;
758 if (strcmp ("current", fields[0]) == 0)
759 strtogauge (fields[2], ¤t);
760 else if (strcmp ("voltage", fields[0]) == 0)
761 strtogauge (fields[2], &voltage);
762 else if (strcmp ("charge", fields[0]) == 0)
763 strtogauge (fields[2], &charge);
764 }
766 fclose (fh);
767 fh = NULL;
769 battery_submit (plugin_instance, "charge", charge / 1000.0);
770 battery_submit (plugin_instance, "current", current / 1000.0);
771 battery_submit (plugin_instance, "voltage", voltage / 1000.0);
772 }
774 if (i == 0)
775 return (ENOENT);
776 return (0);
777 } /* }}} int read_pmu */
779 static int battery_read (void) /* {{{ */
780 {
781 int status;
783 DEBUG ("battery plugin: Trying sysfs ...");
784 status = read_sysfs ();
785 if (status == 0)
786 return (0);
788 DEBUG ("battery plugin: Trying acpi ...");
789 status = read_acpi ();
790 if (status == 0)
791 return (0);
793 DEBUG ("battery plugin: Trying pmu ...");
794 status = read_pmu ();
795 if (status == 0)
796 return (0);
798 ERROR ("battery plugin: Add available input methods failed.");
799 return (-1);
800 } /* }}} int battery_read */
801 #endif /* KERNEL_LINUX */
803 static int battery_config (oconfig_item_t *ci)
804 {
805 int i;
807 for (i = 0; i < ci->children_num; i++)
808 {
809 oconfig_item_t *child = ci->children + i;
811 if (strcasecmp ("ValuesPercentage", child->key) == 0)
812 cf_util_get_boolean (child, &report_percent);
813 else if (strcasecmp ("ReportDegraded", child->key) == 0)
814 cf_util_get_boolean (child, &report_degraded);
815 else
816 WARNING ("battery plugin: Ignoring unknown "
817 "configuration option \"%s\".",
818 child->key);
819 }
821 return (0);
822 } /* }}} int battery_config */
824 void module_register (void)
825 {
826 plugin_register_complex_config ("battery", battery_config);
827 plugin_register_read ("battery", battery_read);
828 } /* void module_register */