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 vl.time = time (NULL);
104 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
105 sstrncpy (vl.plugin, "battery", sizeof (vl.plugin));
106 sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
107 sstrncpy (vl.type, type, sizeof (vl.type));
109 plugin_dispatch_values (&vl);
110 } /* void battery_submit */
112 #if HAVE_IOKIT_PS_IOPOWERSOURCES_H || HAVE_IOKIT_IOKITLIB_H
113 double dict_get_double (CFDictionaryRef dict, char *key_string)
120 key_obj = CFStringCreateWithCString (kCFAllocatorDefault, key_string,
121 kCFStringEncodingASCII);
124 DEBUG ("CFStringCreateWithCString (%s) failed.\n", key_string);
125 return (INVALID_VALUE);
128 if ((val_obj = CFDictionaryGetValue (dict, key_obj)) == NULL)
130 DEBUG ("CFDictionaryGetValue (%s) failed.", key_string);
132 return (INVALID_VALUE);
136 if (CFGetTypeID (val_obj) == CFNumberGetTypeID ())
138 if (CFNumberIsFloatType (val_obj))
140 CFNumberGetValue (val_obj,
146 CFNumberGetValue (val_obj,
147 kCFNumberLongLongType,
149 val_double = val_int;
154 DEBUG ("CFGetTypeID (val_obj) = %i", (int) CFGetTypeID (val_obj));
155 return (INVALID_VALUE);
160 #endif /* HAVE_IOKIT_PS_IOPOWERSOURCES_H || HAVE_IOKIT_IOKITLIB_H */
162 #if HAVE_IOKIT_PS_IOPOWERSOURCES_H
163 static void get_via_io_power_sources (double *ret_charge,
170 CFDictionaryRef ps_dict;
176 ps_raw = IOPSCopyPowerSourcesInfo ();
177 ps_array = IOPSCopyPowerSourcesList (ps_raw);
178 ps_array_len = CFArrayGetCount (ps_array);
180 DEBUG ("ps_array_len == %i", ps_array_len);
182 for (i = 0; i < ps_array_len; i++)
184 ps_obj = CFArrayGetValueAtIndex (ps_array, i);
185 ps_dict = IOPSGetPowerSourceDescription (ps_raw, ps_obj);
189 DEBUG ("IOPSGetPowerSourceDescription failed.");
193 if (CFGetTypeID (ps_dict) != CFDictionaryGetTypeID ())
195 DEBUG ("IOPSGetPowerSourceDescription did not return a CFDictionaryRef");
199 /* FIXME: Check if this is really an internal battery */
201 if (*ret_charge == INVALID_VALUE)
203 /* This is the charge in percent. */
204 temp_double = dict_get_double (ps_dict,
205 kIOPSCurrentCapacityKey);
206 if ((temp_double != INVALID_VALUE)
207 && (temp_double >= 0.0)
208 && (temp_double <= 100.0))
209 *ret_charge = temp_double;
212 if (*ret_current == INVALID_VALUE)
214 temp_double = dict_get_double (ps_dict,
216 if (temp_double != INVALID_VALUE)
217 *ret_current = temp_double / 1000.0;
220 if (*ret_voltage == INVALID_VALUE)
222 temp_double = dict_get_double (ps_dict,
224 if (temp_double != INVALID_VALUE)
225 *ret_voltage = temp_double / 1000.0;
232 #endif /* HAVE_IOKIT_PS_IOPOWERSOURCES_H */
234 #if HAVE_IOKIT_IOKITLIB_H
235 static void get_via_generic_iokit (double *ret_charge,
239 kern_return_t status;
240 io_iterator_t iterator;
243 CFDictionaryRef bat_root_dict;
244 CFArrayRef bat_info_arry;
245 CFIndex bat_info_arry_len;
246 CFIndex bat_info_arry_pos;
247 CFDictionaryRef bat_info_dict;
251 status = IOServiceGetMatchingServices (kIOMasterPortDefault,
252 IOServiceNameMatching ("battery"),
254 if (status != kIOReturnSuccess)
256 DEBUG ("IOServiceGetMatchingServices failed.");
260 while ((io_obj = IOIteratorNext (iterator)))
262 status = IORegistryEntryCreateCFProperties (io_obj,
263 (CFMutableDictionaryRef *) &bat_root_dict,
266 if (status != kIOReturnSuccess)
268 DEBUG ("IORegistryEntryCreateCFProperties failed.");
272 bat_info_arry = (CFArrayRef) CFDictionaryGetValue (bat_root_dict,
273 CFSTR ("IOBatteryInfo"));
274 if (bat_info_arry == NULL)
276 CFRelease (bat_root_dict);
279 bat_info_arry_len = CFArrayGetCount (bat_info_arry);
281 for (bat_info_arry_pos = 0;
282 bat_info_arry_pos < bat_info_arry_len;
285 bat_info_dict = (CFDictionaryRef) CFArrayGetValueAtIndex (bat_info_arry, bat_info_arry_pos);
287 if (*ret_charge == INVALID_VALUE)
289 temp_double = dict_get_double (bat_info_dict,
291 if (temp_double != INVALID_VALUE)
292 *ret_charge = temp_double / 1000.0;
295 if (*ret_current == INVALID_VALUE)
297 temp_double = dict_get_double (bat_info_dict,
299 if (temp_double != INVALID_VALUE)
300 *ret_current = temp_double / 1000.0;
303 if (*ret_voltage == INVALID_VALUE)
305 temp_double = dict_get_double (bat_info_dict,
307 if (temp_double != INVALID_VALUE)
308 *ret_voltage = temp_double / 1000.0;
312 CFRelease (bat_root_dict);
315 IOObjectRelease (iterator);
317 #endif /* HAVE_IOKIT_IOKITLIB_H */
320 static int battery_read_acpi (const char *dir, const char *name,
323 double current = INVALID_VALUE;
324 double voltage = INVALID_VALUE;
325 double charge = INVALID_VALUE;
326 double *valptr = NULL;
338 len = ssnprintf (filename, sizeof (filename), "%s/%s/state", battery_acpi_dir, name);
340 if ((len < 0) || ((unsigned int)len >= sizeof (filename)))
343 if ((fh = fopen (filename, "r")) == NULL) {
345 ERROR ("Cannot open `%s': %s", filename,
346 sstrerror (errno, errbuf, sizeof (errbuf)));
351 * [11:00] <@tokkee> $ cat /proc/acpi/battery/BAT1/state
352 * [11:00] <@tokkee> present: yes
353 * [11:00] <@tokkee> capacity state: ok
354 * [11:00] <@tokkee> charging state: charging
355 * [11:00] <@tokkee> present rate: 1724 mA
356 * [11:00] <@tokkee> remaining capacity: 4136 mAh
357 * [11:00] <@tokkee> present voltage: 12428 mV
359 while (fgets (buffer, sizeof (buffer), fh) != NULL)
361 numfields = strsplit (buffer, fields, 8);
366 if ((strcmp (fields[0], "charging") == 0)
367 && (strcmp (fields[1], "state:") == 0))
369 if (strcmp (fields[2], "charging") == 0)
376 if ((strcmp (fields[0], "present") == 0)
377 && (strcmp (fields[1], "rate:") == 0))
379 else if ((strcmp (fields[0], "remaining") == 0)
380 && (strcmp (fields[1], "capacity:") == 0))
382 else if ((strcmp (fields[0], "present") == 0)
383 && (strcmp (fields[1], "voltage:") == 0))
390 *valptr = strtod (fields[2], &endptr) / 1000.0;
392 if ((fields[2] == endptr) || (errno != 0))
393 *valptr = INVALID_VALUE;
394 } /* while (fgets (buffer, sizeof (buffer), fh) != NULL) */
398 if ((current != INVALID_VALUE) && (charging == 0))
401 if (charge != INVALID_VALUE)
402 battery_submit ("0", "charge", charge);
403 if (current != INVALID_VALUE)
404 battery_submit ("0", "current", current);
405 if (voltage != INVALID_VALUE)
406 battery_submit ("0", "voltage", voltage);
410 #endif /* KERNEL_LINUX */
413 static int battery_read (void)
415 #if HAVE_IOKIT_IOKITLIB_H || HAVE_IOKIT_PS_IOPOWERSOURCES_H
416 double charge = INVALID_VALUE; /* Current charge in Ah */
417 double current = INVALID_VALUE; /* Current in A */
418 double voltage = INVALID_VALUE; /* Voltage in V */
420 double charge_rel = INVALID_VALUE; /* Current charge in percent */
421 double charge_abs = INVALID_VALUE; /* Total capacity */
423 #if HAVE_IOKIT_PS_IOPOWERSOURCES_H
424 get_via_io_power_sources (&charge_rel, ¤t, &voltage);
426 #if HAVE_IOKIT_IOKITLIB_H
427 get_via_generic_iokit (&charge_abs, ¤t, &voltage);
430 if ((charge_rel != INVALID_VALUE) && (charge_abs != INVALID_VALUE))
431 charge = charge_abs * charge_rel / 100.0;
433 if (charge != INVALID_VALUE)
434 battery_submit ("0", "charge", charge);
435 if (current != INVALID_VALUE)
436 battery_submit ("0", "current", current);
437 if (voltage != INVALID_VALUE)
438 battery_submit ("0", "voltage", voltage);
439 /* #endif HAVE_IOKIT_IOKITLIB_H || HAVE_IOKIT_PS_IOPOWERSOURCES_H */
442 static c_complain_t acpi_dir_complaint = C_COMPLAIN_INIT_STATIC;
454 for (i = 0; i < battery_pmu_num; i++)
456 char batnum_str[256];
457 double current = INVALID_VALUE;
458 double voltage = INVALID_VALUE;
459 double charge = INVALID_VALUE;
460 double *valptr = NULL;
462 len = ssnprintf (filename, sizeof (filename), battery_pmu_file, i);
463 if ((len < 0) || ((unsigned int)len >= sizeof (filename)))
466 len = ssnprintf (batnum_str, sizeof (batnum_str), "%i", i);
467 if ((len < 0) || ((unsigned int)len >= sizeof (batnum_str)))
470 if ((fh = fopen (filename, "r")) == NULL)
473 while (fgets (buffer, sizeof (buffer), fh) != NULL)
475 numfields = strsplit (buffer, fields, 8);
480 if (strcmp ("current", fields[0]) == 0)
482 else if (strcmp ("voltage", fields[0]) == 0)
484 else if (strcmp ("charge", fields[0]) == 0)
496 *valptr = strtod (fields[2], &endptr) / 1000.0;
498 if ((fields[2] == endptr) || (errno != 0))
499 *valptr = INVALID_VALUE;
506 if (charge != INVALID_VALUE)
507 battery_submit ("0", "charge", charge);
508 if (current != INVALID_VALUE)
509 battery_submit ("0", "current", current);
510 if (voltage != INVALID_VALUE)
511 battery_submit ("0", "voltage", voltage);
514 if (0 == access (battery_acpi_dir, R_OK))
515 walk_directory (battery_acpi_dir, battery_read_acpi,
516 /* user_data = */ NULL);
520 c_complain_once (LOG_WARNING, &acpi_dir_complaint,
521 "battery plugin: Failed to access `%s': %s",
523 sstrerror (errno, errbuf, sizeof (errbuf)));
526 #endif /* KERNEL_LINUX */
531 void module_register (void)
533 plugin_register_init ("battery", battery_init);
534 plugin_register_read ("battery", battery_read);
535 } /* void module_register */