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 static const char *battery_acpi_dir = "/proc/acpi/battery";
65 #endif /* KERNEL_LINUX */
67 static int battery_init (void)
69 #if HAVE_IOKIT_IOKITLIB_H || HAVE_IOKIT_PS_IOPOWERSOURCES_H
70 /* No init neccessary */
71 /* #endif HAVE_IOKIT_IOKITLIB_H || HAVE_IOKIT_PS_IOPOWERSOURCES_H */
77 for (battery_pmu_num = 0; ; battery_pmu_num++)
79 len = ssnprintf (filename, sizeof (filename), battery_pmu_file, battery_pmu_num);
81 if ((len < 0) || ((unsigned int)len >= sizeof (filename)))
84 if (access (filename, R_OK))
87 #endif /* KERNEL_LINUX */
92 static void battery_submit (const char *plugin_instance, const char *type, double value)
95 value_list_t vl = VALUE_LIST_INIT;
97 values[0].gauge = value;
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));
104 sstrncpy (vl.type, type, sizeof (vl.type));
106 plugin_dispatch_values (&vl);
107 } /* void battery_submit */
109 #if HAVE_IOKIT_PS_IOPOWERSOURCES_H || HAVE_IOKIT_IOKITLIB_H
110 double dict_get_double (CFDictionaryRef dict, char *key_string)
117 key_obj = CFStringCreateWithCString (kCFAllocatorDefault, key_string,
118 kCFStringEncodingASCII);
121 DEBUG ("CFStringCreateWithCString (%s) failed.\n", key_string);
122 return (INVALID_VALUE);
125 if ((val_obj = CFDictionaryGetValue (dict, key_obj)) == NULL)
127 DEBUG ("CFDictionaryGetValue (%s) failed.", key_string);
129 return (INVALID_VALUE);
133 if (CFGetTypeID (val_obj) == CFNumberGetTypeID ())
135 if (CFNumberIsFloatType (val_obj))
137 CFNumberGetValue (val_obj,
143 CFNumberGetValue (val_obj,
144 kCFNumberLongLongType,
146 val_double = val_int;
151 DEBUG ("CFGetTypeID (val_obj) = %i", (int) CFGetTypeID (val_obj));
152 return (INVALID_VALUE);
157 #endif /* HAVE_IOKIT_PS_IOPOWERSOURCES_H || HAVE_IOKIT_IOKITLIB_H */
159 #if HAVE_IOKIT_PS_IOPOWERSOURCES_H
160 static void get_via_io_power_sources (double *ret_charge,
167 CFDictionaryRef ps_dict;
173 ps_raw = IOPSCopyPowerSourcesInfo ();
174 ps_array = IOPSCopyPowerSourcesList (ps_raw);
175 ps_array_len = CFArrayGetCount (ps_array);
177 DEBUG ("ps_array_len == %i", ps_array_len);
179 for (i = 0; i < ps_array_len; i++)
181 ps_obj = CFArrayGetValueAtIndex (ps_array, i);
182 ps_dict = IOPSGetPowerSourceDescription (ps_raw, ps_obj);
186 DEBUG ("IOPSGetPowerSourceDescription failed.");
190 if (CFGetTypeID (ps_dict) != CFDictionaryGetTypeID ())
192 DEBUG ("IOPSGetPowerSourceDescription did not return a CFDictionaryRef");
196 /* FIXME: Check if this is really an internal battery */
198 if (*ret_charge == INVALID_VALUE)
200 /* This is the charge in percent. */
201 temp_double = dict_get_double (ps_dict,
202 kIOPSCurrentCapacityKey);
203 if ((temp_double != INVALID_VALUE)
204 && (temp_double >= 0.0)
205 && (temp_double <= 100.0))
206 *ret_charge = temp_double;
209 if (*ret_current == INVALID_VALUE)
211 temp_double = dict_get_double (ps_dict,
213 if (temp_double != INVALID_VALUE)
214 *ret_current = temp_double / 1000.0;
217 if (*ret_voltage == INVALID_VALUE)
219 temp_double = dict_get_double (ps_dict,
221 if (temp_double != INVALID_VALUE)
222 *ret_voltage = temp_double / 1000.0;
229 #endif /* HAVE_IOKIT_PS_IOPOWERSOURCES_H */
231 #if HAVE_IOKIT_IOKITLIB_H
232 static void get_via_generic_iokit (double *ret_charge,
236 kern_return_t status;
237 io_iterator_t iterator;
240 CFDictionaryRef bat_root_dict;
241 CFArrayRef bat_info_arry;
242 CFIndex bat_info_arry_len;
243 CFIndex bat_info_arry_pos;
244 CFDictionaryRef bat_info_dict;
248 status = IOServiceGetMatchingServices (kIOMasterPortDefault,
249 IOServiceNameMatching ("battery"),
251 if (status != kIOReturnSuccess)
253 DEBUG ("IOServiceGetMatchingServices failed.");
257 while ((io_obj = IOIteratorNext (iterator)))
259 status = IORegistryEntryCreateCFProperties (io_obj,
260 (CFMutableDictionaryRef *) &bat_root_dict,
263 if (status != kIOReturnSuccess)
265 DEBUG ("IORegistryEntryCreateCFProperties failed.");
269 bat_info_arry = (CFArrayRef) CFDictionaryGetValue (bat_root_dict,
270 CFSTR ("IOBatteryInfo"));
271 if (bat_info_arry == NULL)
273 CFRelease (bat_root_dict);
276 bat_info_arry_len = CFArrayGetCount (bat_info_arry);
278 for (bat_info_arry_pos = 0;
279 bat_info_arry_pos < bat_info_arry_len;
282 bat_info_dict = (CFDictionaryRef) CFArrayGetValueAtIndex (bat_info_arry, bat_info_arry_pos);
284 if (*ret_charge == INVALID_VALUE)
286 temp_double = dict_get_double (bat_info_dict,
288 if (temp_double != INVALID_VALUE)
289 *ret_charge = temp_double / 1000.0;
292 if (*ret_current == INVALID_VALUE)
294 temp_double = dict_get_double (bat_info_dict,
296 if (temp_double != INVALID_VALUE)
297 *ret_current = temp_double / 1000.0;
300 if (*ret_voltage == INVALID_VALUE)
302 temp_double = dict_get_double (bat_info_dict,
304 if (temp_double != INVALID_VALUE)
305 *ret_voltage = temp_double / 1000.0;
309 CFRelease (bat_root_dict);
312 IOObjectRelease (iterator);
314 #endif /* HAVE_IOKIT_IOKITLIB_H */
317 static int battery_read_acpi (const char __attribute__((unused)) *dir,
318 const char *name, void __attribute__((unused)) *user_data)
320 double current = INVALID_VALUE;
321 double voltage = INVALID_VALUE;
322 double charge = INVALID_VALUE;
323 double *valptr = NULL;
335 len = ssnprintf (filename, sizeof (filename), "%s/%s/state", battery_acpi_dir, name);
337 if ((len < 0) || ((unsigned int)len >= sizeof (filename)))
340 if ((fh = fopen (filename, "r")) == NULL) {
342 ERROR ("Cannot open `%s': %s", filename,
343 sstrerror (errno, errbuf, sizeof (errbuf)));
348 * [11:00] <@tokkee> $ cat /proc/acpi/battery/BAT1/state
349 * [11:00] <@tokkee> present: yes
350 * [11:00] <@tokkee> capacity state: ok
351 * [11:00] <@tokkee> charging state: charging
352 * [11:00] <@tokkee> present rate: 1724 mA
353 * [11:00] <@tokkee> remaining capacity: 4136 mAh
354 * [11:00] <@tokkee> present voltage: 12428 mV
356 while (fgets (buffer, sizeof (buffer), fh) != NULL)
358 numfields = strsplit (buffer, fields, 8);
363 if ((strcmp (fields[0], "charging") == 0)
364 && (strcmp (fields[1], "state:") == 0))
366 if (strcmp (fields[2], "charging") == 0)
373 if ((strcmp (fields[0], "present") == 0)
374 && (strcmp (fields[1], "rate:") == 0))
376 else if ((strcmp (fields[0], "remaining") == 0)
377 && (strcmp (fields[1], "capacity:") == 0))
379 else if ((strcmp (fields[0], "present") == 0)
380 && (strcmp (fields[1], "voltage:") == 0))
387 *valptr = strtod (fields[2], &endptr) / 1000.0;
389 if ((fields[2] == endptr) || (errno != 0))
390 *valptr = INVALID_VALUE;
391 } /* while (fgets (buffer, sizeof (buffer), fh) != NULL) */
395 if ((current != INVALID_VALUE) && (charging == 0))
398 if (charge != INVALID_VALUE)
399 battery_submit ("0", "charge", charge);
400 if (current != INVALID_VALUE)
401 battery_submit ("0", "current", current);
402 if (voltage != INVALID_VALUE)
403 battery_submit ("0", "voltage", voltage);
407 #endif /* KERNEL_LINUX */
410 static int battery_read (void)
412 #if HAVE_IOKIT_IOKITLIB_H || HAVE_IOKIT_PS_IOPOWERSOURCES_H
413 double charge = INVALID_VALUE; /* Current charge in Ah */
414 double current = INVALID_VALUE; /* Current in A */
415 double voltage = INVALID_VALUE; /* Voltage in V */
417 double charge_rel = INVALID_VALUE; /* Current charge in percent */
418 double charge_abs = INVALID_VALUE; /* Total capacity */
420 #if HAVE_IOKIT_PS_IOPOWERSOURCES_H
421 get_via_io_power_sources (&charge_rel, ¤t, &voltage);
423 #if HAVE_IOKIT_IOKITLIB_H
424 get_via_generic_iokit (&charge_abs, ¤t, &voltage);
427 if ((charge_rel != INVALID_VALUE) && (charge_abs != INVALID_VALUE))
428 charge = charge_abs * charge_rel / 100.0;
430 if (charge != INVALID_VALUE)
431 battery_submit ("0", "charge", charge);
432 if (current != INVALID_VALUE)
433 battery_submit ("0", "current", current);
434 if (voltage != INVALID_VALUE)
435 battery_submit ("0", "voltage", voltage);
436 /* #endif HAVE_IOKIT_IOKITLIB_H || HAVE_IOKIT_PS_IOPOWERSOURCES_H */
449 for (i = 0; i < battery_pmu_num; i++)
451 char batnum_str[256];
452 double current = INVALID_VALUE;
453 double voltage = INVALID_VALUE;
454 double charge = INVALID_VALUE;
455 double *valptr = NULL;
457 len = ssnprintf (filename, sizeof (filename), battery_pmu_file, i);
458 if ((len < 0) || ((unsigned int)len >= sizeof (filename)))
461 len = ssnprintf (batnum_str, sizeof (batnum_str), "%i", i);
462 if ((len < 0) || ((unsigned int)len >= sizeof (batnum_str)))
465 if ((fh = fopen (filename, "r")) == NULL)
468 while (fgets (buffer, sizeof (buffer), fh) != NULL)
470 numfields = strsplit (buffer, fields, 8);
475 if (strcmp ("current", fields[0]) == 0)
477 else if (strcmp ("voltage", fields[0]) == 0)
479 else if (strcmp ("charge", fields[0]) == 0)
491 *valptr = strtod (fields[2], &endptr) / 1000.0;
493 if ((fields[2] == endptr) || (errno != 0))
494 *valptr = INVALID_VALUE;
501 if (charge != INVALID_VALUE)
502 battery_submit ("0", "charge", charge);
503 if (current != INVALID_VALUE)
504 battery_submit ("0", "current", current);
505 if (voltage != INVALID_VALUE)
506 battery_submit ("0", "voltage", voltage);
509 walk_directory (battery_acpi_dir, battery_read_acpi,
510 /* user_data = */ NULL);
512 #endif /* KERNEL_LINUX */
517 void module_register (void)
519 plugin_register_init ("battery", battery_init);
520 plugin_register_read ("battery", battery_read);
521 } /* void module_register */