2 * collectd - src/battery.c
3 * Copyright (C) 2006,2007 Florian octo Forster
4 * Copyright (C) 2008 Michał Mirosław
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; only version 2 of the License is applicable.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * Florian octo Forster <octo at verplant.org>
21 * Michał Mirosław <mirq-linux at rere.qmqm.pl>
28 #include "utils_complain.h"
30 #if HAVE_MACH_MACH_TYPES_H
31 # include <mach/mach_types.h>
33 #if HAVE_MACH_MACH_INIT_H
34 # include <mach/mach_init.h>
36 #if HAVE_MACH_MACH_ERROR_H
37 # include <mach/mach_error.h>
39 #if HAVE_COREFOUNDATION_COREFOUNDATION_H
40 # include <CoreFoundation/CoreFoundation.h>
42 #if HAVE_IOKIT_IOKITLIB_H
43 # include <IOKit/IOKitLib.h>
45 #if HAVE_IOKIT_IOTYPES_H
46 # include <IOKit/IOTypes.h>
48 #if HAVE_IOKIT_PS_IOPOWERSOURCES_H
49 # include <IOKit/ps/IOPowerSources.h>
51 #if HAVE_IOKIT_PS_IOPSKEYS_H
52 # include <IOKit/ps/IOPSKeys.h>
55 #if !HAVE_IOKIT_IOKITLIB_H && !HAVE_IOKIT_PS_IOPOWERSOURCES_H && !KERNEL_LINUX
56 # error "No applicable input method."
59 #define INVALID_VALUE 47841.29
61 #if HAVE_IOKIT_IOKITLIB_H || HAVE_IOKIT_PS_IOPOWERSOURCES_H
62 /* No global variables */
63 /* #endif HAVE_IOKIT_IOKITLIB_H || HAVE_IOKIT_PS_IOPOWERSOURCES_H */
66 static int battery_pmu_num = 0;
67 static char *battery_pmu_file = "/proc/pmu/battery_%i";
68 static const char *battery_acpi_dir = "/proc/acpi/battery";
69 #endif /* KERNEL_LINUX */
71 static int battery_init (void)
73 #if HAVE_IOKIT_IOKITLIB_H || HAVE_IOKIT_PS_IOPOWERSOURCES_H
74 /* No init neccessary */
75 /* #endif HAVE_IOKIT_IOKITLIB_H || HAVE_IOKIT_PS_IOPOWERSOURCES_H */
81 for (battery_pmu_num = 0; ; battery_pmu_num++)
83 len = ssnprintf (filename, sizeof (filename), battery_pmu_file, battery_pmu_num);
85 if ((len < 0) || ((unsigned int)len >= sizeof (filename)))
88 if (access (filename, R_OK))
91 #endif /* KERNEL_LINUX */
96 static void battery_submit (const char *plugin_instance, const char *type, double value)
99 value_list_t vl = VALUE_LIST_INIT;
101 values[0].gauge = value;
105 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
106 sstrncpy (vl.plugin, "battery", sizeof (vl.plugin));
107 sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
108 sstrncpy (vl.type, type, sizeof (vl.type));
110 plugin_dispatch_values (&vl);
111 } /* void battery_submit */
113 #if HAVE_IOKIT_PS_IOPOWERSOURCES_H || HAVE_IOKIT_IOKITLIB_H
114 double dict_get_double (CFDictionaryRef dict, char *key_string)
121 key_obj = CFStringCreateWithCString (kCFAllocatorDefault, key_string,
122 kCFStringEncodingASCII);
125 DEBUG ("CFStringCreateWithCString (%s) failed.\n", key_string);
126 return (INVALID_VALUE);
129 if ((val_obj = CFDictionaryGetValue (dict, key_obj)) == NULL)
131 DEBUG ("CFDictionaryGetValue (%s) failed.", key_string);
133 return (INVALID_VALUE);
137 if (CFGetTypeID (val_obj) == CFNumberGetTypeID ())
139 if (CFNumberIsFloatType (val_obj))
141 CFNumberGetValue (val_obj,
147 CFNumberGetValue (val_obj,
148 kCFNumberLongLongType,
150 val_double = val_int;
155 DEBUG ("CFGetTypeID (val_obj) = %i", (int) CFGetTypeID (val_obj));
156 return (INVALID_VALUE);
161 #endif /* HAVE_IOKIT_PS_IOPOWERSOURCES_H || HAVE_IOKIT_IOKITLIB_H */
163 #if HAVE_IOKIT_PS_IOPOWERSOURCES_H
164 static void get_via_io_power_sources (double *ret_charge,
171 CFDictionaryRef ps_dict;
177 ps_raw = IOPSCopyPowerSourcesInfo ();
178 ps_array = IOPSCopyPowerSourcesList (ps_raw);
179 ps_array_len = CFArrayGetCount (ps_array);
181 DEBUG ("ps_array_len == %i", ps_array_len);
183 for (i = 0; i < ps_array_len; i++)
185 ps_obj = CFArrayGetValueAtIndex (ps_array, i);
186 ps_dict = IOPSGetPowerSourceDescription (ps_raw, ps_obj);
190 DEBUG ("IOPSGetPowerSourceDescription failed.");
194 if (CFGetTypeID (ps_dict) != CFDictionaryGetTypeID ())
196 DEBUG ("IOPSGetPowerSourceDescription did not return a CFDictionaryRef");
200 /* FIXME: Check if this is really an internal battery */
202 if (*ret_charge == INVALID_VALUE)
204 /* This is the charge in percent. */
205 temp_double = dict_get_double (ps_dict,
206 kIOPSCurrentCapacityKey);
207 if ((temp_double != INVALID_VALUE)
208 && (temp_double >= 0.0)
209 && (temp_double <= 100.0))
210 *ret_charge = temp_double;
213 if (*ret_current == INVALID_VALUE)
215 temp_double = dict_get_double (ps_dict,
217 if (temp_double != INVALID_VALUE)
218 *ret_current = temp_double / 1000.0;
221 if (*ret_voltage == INVALID_VALUE)
223 temp_double = dict_get_double (ps_dict,
225 if (temp_double != INVALID_VALUE)
226 *ret_voltage = temp_double / 1000.0;
233 #endif /* HAVE_IOKIT_PS_IOPOWERSOURCES_H */
235 #if HAVE_IOKIT_IOKITLIB_H
236 static void get_via_generic_iokit (double *ret_charge,
240 kern_return_t status;
241 io_iterator_t iterator;
244 CFDictionaryRef bat_root_dict;
245 CFArrayRef bat_info_arry;
246 CFIndex bat_info_arry_len;
247 CFIndex bat_info_arry_pos;
248 CFDictionaryRef bat_info_dict;
252 status = IOServiceGetMatchingServices (kIOMasterPortDefault,
253 IOServiceNameMatching ("battery"),
255 if (status != kIOReturnSuccess)
257 DEBUG ("IOServiceGetMatchingServices failed.");
261 while ((io_obj = IOIteratorNext (iterator)))
263 status = IORegistryEntryCreateCFProperties (io_obj,
264 (CFMutableDictionaryRef *) &bat_root_dict,
267 if (status != kIOReturnSuccess)
269 DEBUG ("IORegistryEntryCreateCFProperties failed.");
273 bat_info_arry = (CFArrayRef) CFDictionaryGetValue (bat_root_dict,
274 CFSTR ("IOBatteryInfo"));
275 if (bat_info_arry == NULL)
277 CFRelease (bat_root_dict);
280 bat_info_arry_len = CFArrayGetCount (bat_info_arry);
282 for (bat_info_arry_pos = 0;
283 bat_info_arry_pos < bat_info_arry_len;
286 bat_info_dict = (CFDictionaryRef) CFArrayGetValueAtIndex (bat_info_arry, bat_info_arry_pos);
288 if (*ret_charge == INVALID_VALUE)
290 temp_double = dict_get_double (bat_info_dict,
292 if (temp_double != INVALID_VALUE)
293 *ret_charge = temp_double / 1000.0;
296 if (*ret_current == INVALID_VALUE)
298 temp_double = dict_get_double (bat_info_dict,
300 if (temp_double != INVALID_VALUE)
301 *ret_current = temp_double / 1000.0;
304 if (*ret_voltage == INVALID_VALUE)
306 temp_double = dict_get_double (bat_info_dict,
308 if (temp_double != INVALID_VALUE)
309 *ret_voltage = temp_double / 1000.0;
313 CFRelease (bat_root_dict);
316 IOObjectRelease (iterator);
318 #endif /* HAVE_IOKIT_IOKITLIB_H */
321 static int battery_read_acpi (const char __attribute__((unused)) *dir,
322 const char *name, void __attribute__((unused)) *user_data)
324 double current = INVALID_VALUE;
325 double voltage = INVALID_VALUE;
326 double charge = INVALID_VALUE;
327 double *valptr = NULL;
339 len = ssnprintf (filename, sizeof (filename), "%s/%s/state", battery_acpi_dir, name);
341 if ((len < 0) || ((unsigned int)len >= sizeof (filename)))
344 if ((fh = fopen (filename, "r")) == NULL) {
346 ERROR ("Cannot open `%s': %s", filename,
347 sstrerror (errno, errbuf, sizeof (errbuf)));
352 * [11:00] <@tokkee> $ cat /proc/acpi/battery/BAT1/state
353 * [11:00] <@tokkee> present: yes
354 * [11:00] <@tokkee> capacity state: ok
355 * [11:00] <@tokkee> charging state: charging
356 * [11:00] <@tokkee> present rate: 1724 mA
357 * [11:00] <@tokkee> remaining capacity: 4136 mAh
358 * [11:00] <@tokkee> present voltage: 12428 mV
360 while (fgets (buffer, sizeof (buffer), fh) != NULL)
362 numfields = strsplit (buffer, fields, 8);
367 if ((strcmp (fields[0], "charging") == 0)
368 && (strcmp (fields[1], "state:") == 0))
370 if (strcmp (fields[2], "charging") == 0)
377 if ((strcmp (fields[0], "present") == 0)
378 && (strcmp (fields[1], "rate:") == 0))
380 else if ((strcmp (fields[0], "remaining") == 0)
381 && (strcmp (fields[1], "capacity:") == 0))
383 else if ((strcmp (fields[0], "present") == 0)
384 && (strcmp (fields[1], "voltage:") == 0))
391 *valptr = strtod (fields[2], &endptr) / 1000.0;
393 if ((fields[2] == endptr) || (errno != 0))
394 *valptr = INVALID_VALUE;
395 } /* while (fgets (buffer, sizeof (buffer), fh) != NULL) */
399 if ((current != INVALID_VALUE) && (charging == 0))
402 if (charge != INVALID_VALUE)
403 battery_submit ("0", "charge", charge);
404 if (current != INVALID_VALUE)
405 battery_submit ("0", "current", current);
406 if (voltage != INVALID_VALUE)
407 battery_submit ("0", "voltage", voltage);
411 #endif /* KERNEL_LINUX */
414 static int battery_read (void)
416 #if HAVE_IOKIT_IOKITLIB_H || HAVE_IOKIT_PS_IOPOWERSOURCES_H
417 double charge = INVALID_VALUE; /* Current charge in Ah */
418 double current = INVALID_VALUE; /* Current in A */
419 double voltage = INVALID_VALUE; /* Voltage in V */
421 double charge_rel = INVALID_VALUE; /* Current charge in percent */
422 double charge_abs = INVALID_VALUE; /* Total capacity */
424 #if HAVE_IOKIT_PS_IOPOWERSOURCES_H
425 get_via_io_power_sources (&charge_rel, ¤t, &voltage);
427 #if HAVE_IOKIT_IOKITLIB_H
428 get_via_generic_iokit (&charge_abs, ¤t, &voltage);
431 if ((charge_rel != INVALID_VALUE) && (charge_abs != INVALID_VALUE))
432 charge = charge_abs * charge_rel / 100.0;
434 if (charge != INVALID_VALUE)
435 battery_submit ("0", "charge", charge);
436 if (current != INVALID_VALUE)
437 battery_submit ("0", "current", current);
438 if (voltage != INVALID_VALUE)
439 battery_submit ("0", "voltage", voltage);
440 /* #endif HAVE_IOKIT_IOKITLIB_H || HAVE_IOKIT_PS_IOPOWERSOURCES_H */
443 static c_complain_t acpi_dir_complaint = C_COMPLAIN_INIT_STATIC;
455 for (i = 0; i < battery_pmu_num; i++)
457 char batnum_str[256];
458 double current = INVALID_VALUE;
459 double voltage = INVALID_VALUE;
460 double charge = INVALID_VALUE;
461 double *valptr = NULL;
463 len = ssnprintf (filename, sizeof (filename), battery_pmu_file, i);
464 if ((len < 0) || ((unsigned int)len >= sizeof (filename)))
467 len = ssnprintf (batnum_str, sizeof (batnum_str), "%i", i);
468 if ((len < 0) || ((unsigned int)len >= sizeof (batnum_str)))
471 if ((fh = fopen (filename, "r")) == NULL)
474 while (fgets (buffer, sizeof (buffer), fh) != NULL)
476 numfields = strsplit (buffer, fields, 8);
481 if (strcmp ("current", fields[0]) == 0)
483 else if (strcmp ("voltage", fields[0]) == 0)
485 else if (strcmp ("charge", fields[0]) == 0)
497 *valptr = strtod (fields[2], &endptr) / 1000.0;
499 if ((fields[2] == endptr) || (errno != 0))
500 *valptr = INVALID_VALUE;
507 if (charge != INVALID_VALUE)
508 battery_submit ("0", "charge", charge);
509 if (current != INVALID_VALUE)
510 battery_submit ("0", "current", current);
511 if (voltage != INVALID_VALUE)
512 battery_submit ("0", "voltage", voltage);
515 if (0 == access (battery_acpi_dir, R_OK))
516 walk_directory (battery_acpi_dir, battery_read_acpi,
517 /* user_data = */ NULL,
518 /* include hidden */ 0);
522 c_complain_once (LOG_WARNING, &acpi_dir_complaint,
523 "battery plugin: Failed to access `%s': %s",
525 sstrerror (errno, errbuf, sizeof (errbuf)));
528 #endif /* KERNEL_LINUX */
533 void module_register (void)
535 plugin_register_init ("battery", battery_init);
536 plugin_register_read ("battery", battery_read);
537 } /* void module_register */