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 #include "utils_complain.h"
28 #if HAVE_MACH_MACH_TYPES_H
29 # include <mach/mach_types.h>
31 #if HAVE_MACH_MACH_INIT_H
32 # include <mach/mach_init.h>
34 #if HAVE_MACH_MACH_ERROR_H
35 # include <mach/mach_error.h>
37 #if HAVE_COREFOUNDATION_COREFOUNDATION_H
38 # include <CoreFoundation/CoreFoundation.h>
40 #if HAVE_IOKIT_IOKITLIB_H
41 # include <IOKit/IOKitLib.h>
43 #if HAVE_IOKIT_IOTYPES_H
44 # include <IOKit/IOTypes.h>
46 #if HAVE_IOKIT_PS_IOPOWERSOURCES_H
47 # include <IOKit/ps/IOPowerSources.h>
49 #if HAVE_IOKIT_PS_IOPSKEYS_H
50 # include <IOKit/ps/IOPSKeys.h>
53 #if !HAVE_IOKIT_IOKITLIB_H && !HAVE_IOKIT_PS_IOPOWERSOURCES_H && !KERNEL_LINUX
54 # error "No applicable input method."
57 #define INVALID_VALUE 47841.29
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 */
64 static int battery_pmu_num = 0;
65 static char *battery_pmu_file = "/proc/pmu/battery_%i";
66 static const char *battery_acpi_dir = "/proc/acpi/battery";
67 #endif /* KERNEL_LINUX */
69 static int battery_init (void)
71 #if HAVE_IOKIT_IOKITLIB_H || HAVE_IOKIT_PS_IOPOWERSOURCES_H
72 /* No init neccessary */
73 /* #endif HAVE_IOKIT_IOKITLIB_H || HAVE_IOKIT_PS_IOPOWERSOURCES_H */
79 for (battery_pmu_num = 0; ; battery_pmu_num++)
81 len = ssnprintf (filename, sizeof (filename), battery_pmu_file, battery_pmu_num);
83 if ((len < 0) || ((unsigned int)len >= sizeof (filename)))
86 if (access (filename, R_OK))
89 #endif /* KERNEL_LINUX */
94 static void battery_submit (const char *plugin_instance, const char *type, double value)
97 value_list_t vl = VALUE_LIST_INIT;
99 values[0].gauge = value;
103 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
104 sstrncpy (vl.plugin, "battery", sizeof (vl.plugin));
105 sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
106 sstrncpy (vl.type, type, sizeof (vl.type));
108 plugin_dispatch_values (&vl);
109 } /* void battery_submit */
111 #if HAVE_IOKIT_PS_IOPOWERSOURCES_H || HAVE_IOKIT_IOKITLIB_H
112 double dict_get_double (CFDictionaryRef dict, char *key_string)
119 key_obj = CFStringCreateWithCString (kCFAllocatorDefault, key_string,
120 kCFStringEncodingASCII);
123 DEBUG ("CFStringCreateWithCString (%s) failed.\n", key_string);
124 return (INVALID_VALUE);
127 if ((val_obj = CFDictionaryGetValue (dict, key_obj)) == NULL)
129 DEBUG ("CFDictionaryGetValue (%s) failed.", key_string);
131 return (INVALID_VALUE);
135 if (CFGetTypeID (val_obj) == CFNumberGetTypeID ())
137 if (CFNumberIsFloatType (val_obj))
139 CFNumberGetValue (val_obj,
145 CFNumberGetValue (val_obj,
146 kCFNumberLongLongType,
148 val_double = val_int;
153 DEBUG ("CFGetTypeID (val_obj) = %i", (int) CFGetTypeID (val_obj));
154 return (INVALID_VALUE);
159 #endif /* HAVE_IOKIT_PS_IOPOWERSOURCES_H || HAVE_IOKIT_IOKITLIB_H */
161 #if HAVE_IOKIT_PS_IOPOWERSOURCES_H
162 static void get_via_io_power_sources (double *ret_charge,
169 CFDictionaryRef ps_dict;
175 ps_raw = IOPSCopyPowerSourcesInfo ();
176 ps_array = IOPSCopyPowerSourcesList (ps_raw);
177 ps_array_len = CFArrayGetCount (ps_array);
179 DEBUG ("ps_array_len == %i", ps_array_len);
181 for (i = 0; i < ps_array_len; i++)
183 ps_obj = CFArrayGetValueAtIndex (ps_array, i);
184 ps_dict = IOPSGetPowerSourceDescription (ps_raw, ps_obj);
188 DEBUG ("IOPSGetPowerSourceDescription failed.");
192 if (CFGetTypeID (ps_dict) != CFDictionaryGetTypeID ())
194 DEBUG ("IOPSGetPowerSourceDescription did not return a CFDictionaryRef");
198 /* FIXME: Check if this is really an internal battery */
200 if (*ret_charge == INVALID_VALUE)
202 /* This is the charge in percent. */
203 temp_double = dict_get_double (ps_dict,
204 kIOPSCurrentCapacityKey);
205 if ((temp_double != INVALID_VALUE)
206 && (temp_double >= 0.0)
207 && (temp_double <= 100.0))
208 *ret_charge = temp_double;
211 if (*ret_current == INVALID_VALUE)
213 temp_double = dict_get_double (ps_dict,
215 if (temp_double != INVALID_VALUE)
216 *ret_current = temp_double / 1000.0;
219 if (*ret_voltage == INVALID_VALUE)
221 temp_double = dict_get_double (ps_dict,
223 if (temp_double != INVALID_VALUE)
224 *ret_voltage = temp_double / 1000.0;
231 #endif /* HAVE_IOKIT_PS_IOPOWERSOURCES_H */
233 #if HAVE_IOKIT_IOKITLIB_H
234 static void get_via_generic_iokit (double *ret_charge,
238 kern_return_t status;
239 io_iterator_t iterator;
242 CFDictionaryRef bat_root_dict;
243 CFArrayRef bat_info_arry;
244 CFIndex bat_info_arry_len;
245 CFIndex bat_info_arry_pos;
246 CFDictionaryRef bat_info_dict;
250 status = IOServiceGetMatchingServices (kIOMasterPortDefault,
251 IOServiceNameMatching ("battery"),
253 if (status != kIOReturnSuccess)
255 DEBUG ("IOServiceGetMatchingServices failed.");
259 while ((io_obj = IOIteratorNext (iterator)))
261 status = IORegistryEntryCreateCFProperties (io_obj,
262 (CFMutableDictionaryRef *) &bat_root_dict,
265 if (status != kIOReturnSuccess)
267 DEBUG ("IORegistryEntryCreateCFProperties failed.");
271 bat_info_arry = (CFArrayRef) CFDictionaryGetValue (bat_root_dict,
272 CFSTR ("IOBatteryInfo"));
273 if (bat_info_arry == NULL)
275 CFRelease (bat_root_dict);
278 bat_info_arry_len = CFArrayGetCount (bat_info_arry);
280 for (bat_info_arry_pos = 0;
281 bat_info_arry_pos < bat_info_arry_len;
284 bat_info_dict = (CFDictionaryRef) CFArrayGetValueAtIndex (bat_info_arry, bat_info_arry_pos);
286 if (*ret_charge == INVALID_VALUE)
288 temp_double = dict_get_double (bat_info_dict,
290 if (temp_double != INVALID_VALUE)
291 *ret_charge = temp_double / 1000.0;
294 if (*ret_current == INVALID_VALUE)
296 temp_double = dict_get_double (bat_info_dict,
298 if (temp_double != INVALID_VALUE)
299 *ret_current = temp_double / 1000.0;
302 if (*ret_voltage == INVALID_VALUE)
304 temp_double = dict_get_double (bat_info_dict,
306 if (temp_double != INVALID_VALUE)
307 *ret_voltage = temp_double / 1000.0;
311 CFRelease (bat_root_dict);
314 IOObjectRelease (iterator);
316 #endif /* HAVE_IOKIT_IOKITLIB_H */
319 static int battery_read_acpi (const char __attribute__((unused)) *dir,
320 const char *name, void __attribute__((unused)) *user_data)
322 double current = INVALID_VALUE;
323 double voltage = INVALID_VALUE;
324 double charge = INVALID_VALUE;
325 double *valptr = NULL;
337 len = ssnprintf (filename, sizeof (filename), "%s/%s/state", battery_acpi_dir, name);
339 if ((len < 0) || ((unsigned int)len >= sizeof (filename)))
342 if ((fh = fopen (filename, "r")) == NULL) {
344 ERROR ("Cannot open `%s': %s", filename,
345 sstrerror (errno, errbuf, sizeof (errbuf)));
350 * [11:00] <@tokkee> $ cat /proc/acpi/battery/BAT1/state
351 * [11:00] <@tokkee> present: yes
352 * [11:00] <@tokkee> capacity state: ok
353 * [11:00] <@tokkee> charging state: charging
354 * [11:00] <@tokkee> present rate: 1724 mA
355 * [11:00] <@tokkee> remaining capacity: 4136 mAh
356 * [11:00] <@tokkee> present voltage: 12428 mV
358 while (fgets (buffer, sizeof (buffer), fh) != NULL)
360 numfields = strsplit (buffer, fields, 8);
365 if ((strcmp (fields[0], "charging") == 0)
366 && (strcmp (fields[1], "state:") == 0))
368 if (strcmp (fields[2], "charging") == 0)
375 if ((strcmp (fields[0], "present") == 0)
376 && (strcmp (fields[1], "rate:") == 0))
378 else if ((strcmp (fields[0], "remaining") == 0)
379 && (strcmp (fields[1], "capacity:") == 0))
381 else if ((strcmp (fields[0], "present") == 0)
382 && (strcmp (fields[1], "voltage:") == 0))
389 *valptr = strtod (fields[2], &endptr) / 1000.0;
391 if ((fields[2] == endptr) || (errno != 0))
392 *valptr = INVALID_VALUE;
393 } /* while (fgets (buffer, sizeof (buffer), fh) != NULL) */
397 if ((current != INVALID_VALUE) && (charging == 0))
400 if (charge != INVALID_VALUE)
401 battery_submit ("0", "charge", charge);
402 if (current != INVALID_VALUE)
403 battery_submit ("0", "current", current);
404 if (voltage != INVALID_VALUE)
405 battery_submit ("0", "voltage", voltage);
409 #endif /* KERNEL_LINUX */
412 static int battery_read (void)
414 #if HAVE_IOKIT_IOKITLIB_H || HAVE_IOKIT_PS_IOPOWERSOURCES_H
415 double charge = INVALID_VALUE; /* Current charge in Ah */
416 double current = INVALID_VALUE; /* Current in A */
417 double voltage = INVALID_VALUE; /* Voltage in V */
419 double charge_rel = INVALID_VALUE; /* Current charge in percent */
420 double charge_abs = INVALID_VALUE; /* Total capacity */
422 #if HAVE_IOKIT_PS_IOPOWERSOURCES_H
423 get_via_io_power_sources (&charge_rel, ¤t, &voltage);
425 #if HAVE_IOKIT_IOKITLIB_H
426 get_via_generic_iokit (&charge_abs, ¤t, &voltage);
429 if ((charge_rel != INVALID_VALUE) && (charge_abs != INVALID_VALUE))
430 charge = charge_abs * charge_rel / 100.0;
432 if (charge != INVALID_VALUE)
433 battery_submit ("0", "charge", charge);
434 if (current != INVALID_VALUE)
435 battery_submit ("0", "current", current);
436 if (voltage != INVALID_VALUE)
437 battery_submit ("0", "voltage", voltage);
438 /* #endif HAVE_IOKIT_IOKITLIB_H || HAVE_IOKIT_PS_IOPOWERSOURCES_H */
441 static c_complain_t acpi_dir_complaint = C_COMPLAIN_INIT_STATIC;
453 for (i = 0; i < battery_pmu_num; i++)
455 char batnum_str[256];
456 double current = INVALID_VALUE;
457 double voltage = INVALID_VALUE;
458 double charge = INVALID_VALUE;
459 double *valptr = NULL;
461 len = ssnprintf (filename, sizeof (filename), battery_pmu_file, i);
462 if ((len < 0) || ((unsigned int)len >= sizeof (filename)))
465 len = ssnprintf (batnum_str, sizeof (batnum_str), "%i", i);
466 if ((len < 0) || ((unsigned int)len >= sizeof (batnum_str)))
469 if ((fh = fopen (filename, "r")) == NULL)
472 while (fgets (buffer, sizeof (buffer), fh) != NULL)
474 numfields = strsplit (buffer, fields, 8);
479 if (strcmp ("current", fields[0]) == 0)
481 else if (strcmp ("voltage", fields[0]) == 0)
483 else if (strcmp ("charge", fields[0]) == 0)
495 *valptr = strtod (fields[2], &endptr) / 1000.0;
497 if ((fields[2] == endptr) || (errno != 0))
498 *valptr = INVALID_VALUE;
505 if (charge != INVALID_VALUE)
506 battery_submit ("0", "charge", charge);
507 if (current != INVALID_VALUE)
508 battery_submit ("0", "current", current);
509 if (voltage != INVALID_VALUE)
510 battery_submit ("0", "voltage", voltage);
513 if (0 == access (battery_acpi_dir, R_OK))
514 walk_directory (battery_acpi_dir, battery_read_acpi,
515 /* user_data = */ NULL);
519 c_complain_once (LOG_WARNING, &acpi_dir_complaint,
520 "battery plugin: Failed to access `%s': %s",
522 sstrerror (errno, errbuf, sizeof (errbuf)));
525 #endif /* KERNEL_LINUX */
530 void module_register (void)
532 plugin_register_init ("battery", battery_init);
533 plugin_register_read ("battery", battery_read);
534 } /* void module_register */