2 * collectd - src/battery.c
3 * Copyright (C) 2006,2007 Florian octo Forster
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; only version 2 of the License is applicable.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Florian octo Forster <octo at verplant.org>
26 #if HAVE_MACH_MACH_TYPES_H
27 # include <mach/mach_types.h>
29 #if HAVE_MACH_MACH_INIT_H
30 # include <mach/mach_init.h>
32 #if HAVE_MACH_MACH_ERROR_H
33 # include <mach/mach_error.h>
35 #if HAVE_COREFOUNDATION_COREFOUNDATION_H
36 # include <CoreFoundation/CoreFoundation.h>
38 #if HAVE_IOKIT_IOKITLIB_H
39 # include <IOKit/IOKitLib.h>
41 #if HAVE_IOKIT_IOTYPES_H
42 # include <IOKit/IOTypes.h>
44 #if HAVE_IOKIT_PS_IOPOWERSOURCES_H
45 # include <IOKit/ps/IOPowerSources.h>
47 #if HAVE_IOKIT_PS_IOPSKEYS_H
48 # include <IOKit/ps/IOPSKeys.h>
51 #if !HAVE_IOKIT_IOKITLIB_H && !HAVE_IOKIT_PS_IOPOWERSOURCES_H && !KERNEL_LINUX
52 # error "No applicable input method."
55 #define INVALID_VALUE 47841.29
57 #if HAVE_IOKIT_IOKITLIB_H || HAVE_IOKIT_PS_IOPOWERSOURCES_H
58 /* No global variables */
59 /* #endif HAVE_IOKIT_IOKITLIB_H || HAVE_IOKIT_PS_IOPOWERSOURCES_H */
62 static int battery_pmu_num = 0;
63 static char *battery_pmu_file = "/proc/pmu/battery_%i";
64 #endif /* KERNEL_LINUX */
66 static int battery_init (void)
68 #if HAVE_IOKIT_IOKITLIB_H || HAVE_IOKIT_PS_IOPOWERSOURCES_H
69 /* No init neccessary */
70 /* #endif HAVE_IOKIT_IOKITLIB_H || HAVE_IOKIT_PS_IOPOWERSOURCES_H */
76 for (battery_pmu_num = 0; ; battery_pmu_num++)
78 len = snprintf (filename, sizeof (filename), battery_pmu_file, battery_pmu_num);
80 if ((len < 0) || ((unsigned int)len >= sizeof (filename)))
83 if (access (filename, R_OK))
86 #endif /* KERNEL_LINUX */
91 static void battery_submit (const char *plugin_instance, const char *type, double value)
94 value_list_t vl = VALUE_LIST_INIT;
96 values[0].gauge = value;
100 vl.time = time (NULL);
101 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
102 sstrncpy (vl.plugin, "battery", sizeof (vl.plugin));
103 sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
105 plugin_dispatch_values (type, &vl);
106 } /* void battery_submit */
108 #if HAVE_IOKIT_PS_IOPOWERSOURCES_H || HAVE_IOKIT_IOKITLIB_H
109 double dict_get_double (CFDictionaryRef dict, char *key_string)
116 key_obj = CFStringCreateWithCString (kCFAllocatorDefault, key_string,
117 kCFStringEncodingASCII);
120 DEBUG ("CFStringCreateWithCString (%s) failed.\n", key_string);
121 return (INVALID_VALUE);
124 if ((val_obj = CFDictionaryGetValue (dict, key_obj)) == NULL)
126 DEBUG ("CFDictionaryGetValue (%s) failed.", key_string);
128 return (INVALID_VALUE);
132 if (CFGetTypeID (val_obj) == CFNumberGetTypeID ())
134 if (CFNumberIsFloatType (val_obj))
136 CFNumberGetValue (val_obj,
142 CFNumberGetValue (val_obj,
143 kCFNumberLongLongType,
145 val_double = val_int;
150 DEBUG ("CFGetTypeID (val_obj) = %i", (int) CFGetTypeID (val_obj));
151 return (INVALID_VALUE);
156 #endif /* HAVE_IOKIT_PS_IOPOWERSOURCES_H || HAVE_IOKIT_IOKITLIB_H */
158 #if HAVE_IOKIT_PS_IOPOWERSOURCES_H
159 static void get_via_io_power_sources (double *ret_charge,
166 CFDictionaryRef ps_dict;
172 ps_raw = IOPSCopyPowerSourcesInfo ();
173 ps_array = IOPSCopyPowerSourcesList (ps_raw);
174 ps_array_len = CFArrayGetCount (ps_array);
176 DEBUG ("ps_array_len == %i", ps_array_len);
178 for (i = 0; i < ps_array_len; i++)
180 ps_obj = CFArrayGetValueAtIndex (ps_array, i);
181 ps_dict = IOPSGetPowerSourceDescription (ps_raw, ps_obj);
185 DEBUG ("IOPSGetPowerSourceDescription failed.");
189 if (CFGetTypeID (ps_dict) != CFDictionaryGetTypeID ())
191 DEBUG ("IOPSGetPowerSourceDescription did not return a CFDictionaryRef");
195 /* FIXME: Check if this is really an internal battery */
197 if (*ret_charge == INVALID_VALUE)
199 /* This is the charge in percent. */
200 temp_double = dict_get_double (ps_dict,
201 kIOPSCurrentCapacityKey);
202 if ((temp_double != INVALID_VALUE)
203 && (temp_double >= 0.0)
204 && (temp_double <= 100.0))
205 *ret_charge = temp_double;
208 if (*ret_current == INVALID_VALUE)
210 temp_double = dict_get_double (ps_dict,
212 if (temp_double != INVALID_VALUE)
213 *ret_current = temp_double / 1000.0;
216 if (*ret_voltage == INVALID_VALUE)
218 temp_double = dict_get_double (ps_dict,
220 if (temp_double != INVALID_VALUE)
221 *ret_voltage = temp_double / 1000.0;
228 #endif /* HAVE_IOKIT_PS_IOPOWERSOURCES_H */
230 #if HAVE_IOKIT_IOKITLIB_H
231 static void get_via_generic_iokit (double *ret_charge,
235 kern_return_t status;
236 io_iterator_t iterator;
239 CFDictionaryRef bat_root_dict;
240 CFArrayRef bat_info_arry;
241 CFIndex bat_info_arry_len;
242 CFIndex bat_info_arry_pos;
243 CFDictionaryRef bat_info_dict;
247 status = IOServiceGetMatchingServices (kIOMasterPortDefault,
248 IOServiceNameMatching ("battery"),
250 if (status != kIOReturnSuccess)
252 DEBUG ("IOServiceGetMatchingServices failed.");
256 while ((io_obj = IOIteratorNext (iterator)))
258 status = IORegistryEntryCreateCFProperties (io_obj,
259 (CFMutableDictionaryRef *) &bat_root_dict,
262 if (status != kIOReturnSuccess)
264 DEBUG ("IORegistryEntryCreateCFProperties failed.");
268 bat_info_arry = (CFArrayRef) CFDictionaryGetValue (bat_root_dict,
269 CFSTR ("IOBatteryInfo"));
270 if (bat_info_arry == NULL)
272 CFRelease (bat_root_dict);
275 bat_info_arry_len = CFArrayGetCount (bat_info_arry);
277 for (bat_info_arry_pos = 0;
278 bat_info_arry_pos < bat_info_arry_len;
281 bat_info_dict = (CFDictionaryRef) CFArrayGetValueAtIndex (bat_info_arry, bat_info_arry_pos);
283 if (*ret_charge == INVALID_VALUE)
285 temp_double = dict_get_double (bat_info_dict,
287 if (temp_double != INVALID_VALUE)
288 *ret_charge = temp_double / 1000.0;
291 if (*ret_current == INVALID_VALUE)
293 temp_double = dict_get_double (bat_info_dict,
295 if (temp_double != INVALID_VALUE)
296 *ret_current = temp_double / 1000.0;
299 if (*ret_voltage == INVALID_VALUE)
301 temp_double = dict_get_double (bat_info_dict,
303 if (temp_double != INVALID_VALUE)
304 *ret_voltage = temp_double / 1000.0;
308 CFRelease (bat_root_dict);
311 IOObjectRelease (iterator);
313 #endif /* HAVE_IOKIT_IOKITLIB_H */
315 static int battery_read (void)
317 #if HAVE_IOKIT_IOKITLIB_H || HAVE_IOKIT_PS_IOPOWERSOURCES_H
318 double charge = INVALID_VALUE; /* Current charge in Ah */
319 double current = INVALID_VALUE; /* Current in A */
320 double voltage = INVALID_VALUE; /* Voltage in V */
322 double charge_rel = INVALID_VALUE; /* Current charge in percent */
323 double charge_abs = INVALID_VALUE; /* Total capacity */
325 #if HAVE_IOKIT_PS_IOPOWERSOURCES_H
326 get_via_io_power_sources (&charge_rel, ¤t, &voltage);
328 #if HAVE_IOKIT_IOKITLIB_H
329 get_via_generic_iokit (&charge_abs, ¤t, &voltage);
332 if ((charge_rel != INVALID_VALUE) && (charge_abs != INVALID_VALUE))
333 charge = charge_abs * charge_rel / 100.0;
335 if (charge != INVALID_VALUE)
336 battery_submit ("0", "charge", charge);
337 if (current != INVALID_VALUE)
338 battery_submit ("0", "current", current);
339 if (voltage != INVALID_VALUE)
340 battery_submit ("0", "voltage", voltage);
341 /* #endif HAVE_IOKIT_IOKITLIB_H || HAVE_IOKIT_PS_IOPOWERSOURCES_H */
354 for (i = 0; i < battery_pmu_num; i++)
356 char batnum_str[256];
357 double current = INVALID_VALUE;
358 double voltage = INVALID_VALUE;
359 double charge = INVALID_VALUE;
360 double *valptr = NULL;
362 len = snprintf (filename, sizeof (filename), battery_pmu_file, i);
363 if ((len < 0) || ((unsigned int)len >= sizeof (filename)))
366 len = snprintf (batnum_str, sizeof (batnum_str), "%i", i);
367 if ((len < 0) || ((unsigned int)len >= sizeof (batnum_str)))
370 if ((fh = fopen (filename, "r")) == NULL)
373 while (fgets (buffer, sizeof (buffer), fh) != NULL)
375 numfields = strsplit (buffer, fields, 8);
380 if (strcmp ("current", fields[0]) == 0)
382 else if (strcmp ("voltage", fields[0]) == 0)
384 else if (strcmp ("charge", fields[0]) == 0)
396 *valptr = strtod (fields[2], &endptr) / 1000.0;
398 if ((fields[2] == endptr) || (errno != 0))
399 *valptr = INVALID_VALUE;
406 if (charge != INVALID_VALUE)
407 battery_submit ("0", "charge", charge);
408 if (current != INVALID_VALUE)
409 battery_submit ("0", "current", current);
410 if (voltage != INVALID_VALUE)
411 battery_submit ("0", "voltage", voltage);
414 if (access ("/proc/acpi/battery", R_OK | X_OK) == 0)
416 double current = INVALID_VALUE;
417 double voltage = INVALID_VALUE;
418 double charge = INVALID_VALUE;
419 double *valptr = NULL;
425 if ((dh = opendir ("/proc/acpi/battery")) == NULL)
428 ERROR ("Cannot open `/proc/acpi/battery': %s",
429 sstrerror (errno, errbuf, sizeof (errbuf)));
433 while ((ent = readdir (dh)) != NULL)
435 if (ent->d_name[0] == '.')
438 len = snprintf (filename, sizeof (filename),
439 "/proc/acpi/battery/%s/state",
441 if ((len < 0) || ((unsigned int)len >= sizeof (filename)))
444 if ((fh = fopen (filename, "r")) == NULL)
447 ERROR ("Cannot open `%s': %s", filename,
448 sstrerror (errno, errbuf,
454 * [11:00] <@tokkee> $ cat /proc/acpi/battery/BAT1/state
455 * [11:00] <@tokkee> present: yes
456 * [11:00] <@tokkee> capacity state: ok
457 * [11:00] <@tokkee> charging state: charging
458 * [11:00] <@tokkee> present rate: 1724 mA
459 * [11:00] <@tokkee> remaining capacity: 4136 mAh
460 * [11:00] <@tokkee> present voltage: 12428 mV
462 while (fgets (buffer, sizeof (buffer), fh) != NULL)
464 numfields = strsplit (buffer, fields, 8);
469 if ((strcmp (fields[0], "present") == 0)
470 && (strcmp (fields[1], "rate:") == 0))
472 else if ((strcmp (fields[0], "remaining") == 0)
473 && (strcmp (fields[1], "capacity:") == 0))
475 else if ((strcmp (fields[0], "present") == 0)
476 && (strcmp (fields[1], "voltage:") == 0))
481 if ((strcmp (fields[0], "charging") == 0)
482 && (strcmp (fields[1], "state:") == 0))
484 if (strcmp (fields[2], "charging") == 0)
497 *valptr = strtod (fields[2], &endptr) / 1000.0;
499 if ((fields[2] == endptr) || (errno != 0))
500 *valptr = INVALID_VALUE;
502 } /* while (fgets (buffer, sizeof (buffer), fh) != NULL) */
506 if ((current != INVALID_VALUE) && (charging == 0))
509 if (charge != INVALID_VALUE)
510 battery_submit ("0", "charge", charge);
511 if (current != INVALID_VALUE)
512 battery_submit ("0", "current", current);
513 if (voltage != INVALID_VALUE)
514 battery_submit ("0", "voltage", voltage);
519 #endif /* KERNEL_LINUX */
524 void module_register (void)
526 plugin_register_init ("battery", battery_init);
527 plugin_register_read ("battery", battery_read);
528 } /* void module_register */