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>
25 #include "utils_debug.h"
27 #if HAVE_MACH_MACH_TYPES_H
28 # include <mach/mach_types.h>
30 #if HAVE_MACH_MACH_INIT_H
31 # include <mach/mach_init.h>
33 #if HAVE_MACH_MACH_ERROR_H
34 # include <mach/mach_error.h>
36 #if HAVE_COREFOUNDATION_COREFOUNDATION_H
37 # include <CoreFoundation/CoreFoundation.h>
39 #if HAVE_IOKIT_IOKITLIB_H
40 # include <IOKit/IOKitLib.h>
42 #if HAVE_IOKIT_IOTYPES_H
43 # include <IOKit/IOTypes.h>
45 #if HAVE_IOKIT_PS_IOPOWERSOURCES_H
46 # include <IOKit/ps/IOPowerSources.h>
48 #if HAVE_IOKIT_PS_IOPSKEYS_H
49 # include <IOKit/ps/IOPSKeys.h>
52 #if HAVE_IOKIT_IOKITLIB_H || HAVE_IOKIT_PS_IOPOWERSOURCES_H || KERNEL_LINUX
53 # define BATTERY_HAVE_READ 1
55 # define BATTERY_HAVE_READ 0
58 #define INVALID_VALUE 47841.29
60 static data_source_t data_source_charge[1] =
62 {"charge", DS_TYPE_GAUGE, 0, NAN}
65 static data_set_t charge_ds =
67 "charge", 1, data_source_charge
70 static data_source_t data_source_current[1] =
72 {"current", DS_TYPE_GAUGE, NAN, NAN}
75 static data_set_t current_ds =
77 "current", 1, data_source_current
80 static data_source_t data_source_voltage[1] =
82 {"voltage", DS_TYPE_GAUGE, NAN, NAN}
85 static data_set_t voltage_ds =
87 "voltage", 1, data_source_voltage
91 #if HAVE_IOKIT_IOKITLIB_H || HAVE_IOKIT_PS_IOPOWERSOURCES_H
92 /* No global variables */
93 /* #endif HAVE_IOKIT_IOKITLIB_H || HAVE_IOKIT_PS_IOPOWERSOURCES_H */
96 static int battery_pmu_num = 0;
97 static char *battery_pmu_file = "/proc/pmu/battery_%i";
98 #endif /* KERNEL_LINUX */
100 static int battery_init (void)
102 #if HAVE_IOKIT_IOKITLIB_H || HAVE_IOKIT_PS_IOPOWERSOURCES_H
103 /* No init neccessary */
104 /* #endif HAVE_IOKIT_IOKITLIB_H || HAVE_IOKIT_PS_IOPOWERSOURCES_H */
110 for (battery_pmu_num = 0; ; battery_pmu_num++)
112 len = snprintf (filename, sizeof (filename), battery_pmu_file, battery_pmu_num);
114 if ((len >= sizeof (filename)) || (len < 0))
117 if (access (filename, R_OK))
120 #endif /* KERNEL_LINUX */
125 static void battery_submit (const char *plugin_instance, const char *type, double value)
128 value_list_t vl = VALUE_LIST_INIT;
130 values[0].gauge = value;
134 vl.time = time (NULL);
135 strcpy (vl.host, hostname);
136 strcpy (vl.plugin, "battery");
137 strcpy (vl.plugin_instance, plugin_instance);
139 plugin_dispatch_values (type, &vl);
140 } /* void battery_submit */
142 #if HAVE_IOKIT_PS_IOPOWERSOURCES_H || HAVE_IOKIT_IOKITLIB_H
143 double dict_get_double (CFDictionaryRef dict, char *key_string)
150 key_obj = CFStringCreateWithCString (kCFAllocatorDefault, key_string,
151 kCFStringEncodingASCII);
154 DBG ("CFStringCreateWithCString (%s) failed.\n", key_string);
155 return (INVALID_VALUE);
158 if ((val_obj = CFDictionaryGetValue (dict, key_obj)) == NULL)
160 DBG ("CFDictionaryGetValue (%s) failed.", key_string);
162 return (INVALID_VALUE);
166 if (CFGetTypeID (val_obj) == CFNumberGetTypeID ())
168 if (CFNumberIsFloatType (val_obj))
170 CFNumberGetValue (val_obj,
176 CFNumberGetValue (val_obj,
177 kCFNumberLongLongType,
179 val_double = val_int;
184 DBG ("CFGetTypeID (val_obj) = %i", (int) CFGetTypeID (val_obj));
185 return (INVALID_VALUE);
190 #endif /* HAVE_IOKIT_PS_IOPOWERSOURCES_H || HAVE_IOKIT_IOKITLIB_H */
192 #if HAVE_IOKIT_PS_IOPOWERSOURCES_H
193 static void get_via_io_power_sources (double *ret_charge,
200 CFDictionaryRef ps_dict;
206 ps_raw = IOPSCopyPowerSourcesInfo ();
207 ps_array = IOPSCopyPowerSourcesList (ps_raw);
208 ps_array_len = CFArrayGetCount (ps_array);
210 DBG ("ps_array_len == %i", ps_array_len);
212 for (i = 0; i < ps_array_len; i++)
214 ps_obj = CFArrayGetValueAtIndex (ps_array, i);
215 ps_dict = IOPSGetPowerSourceDescription (ps_raw, ps_obj);
219 DBG ("IOPSGetPowerSourceDescription failed.");
223 if (CFGetTypeID (ps_dict) != CFDictionaryGetTypeID ())
225 DBG ("IOPSGetPowerSourceDescription did not return a CFDictionaryRef");
229 /* FIXME: Check if this is really an internal battery */
231 if (*ret_charge == INVALID_VALUE)
233 /* This is the charge in percent. */
234 temp_double = dict_get_double (ps_dict,
235 kIOPSCurrentCapacityKey);
236 if ((temp_double != INVALID_VALUE)
237 && (temp_double >= 0.0)
238 && (temp_double <= 100.0))
239 *ret_charge = temp_double;
242 if (*ret_current == INVALID_VALUE)
244 temp_double = dict_get_double (ps_dict,
246 if (temp_double != INVALID_VALUE)
247 *ret_current = temp_double / 1000.0;
250 if (*ret_voltage == INVALID_VALUE)
252 temp_double = dict_get_double (ps_dict,
254 if (temp_double != INVALID_VALUE)
255 *ret_voltage = temp_double / 1000.0;
262 #endif /* HAVE_IOKIT_PS_IOPOWERSOURCES_H */
264 #if HAVE_IOKIT_IOKITLIB_H
265 static void get_via_generic_iokit (double *ret_charge,
269 kern_return_t status;
270 io_iterator_t iterator;
273 CFDictionaryRef bat_root_dict;
274 CFArrayRef bat_info_arry;
275 CFIndex bat_info_arry_len;
276 CFIndex bat_info_arry_pos;
277 CFDictionaryRef bat_info_dict;
281 status = IOServiceGetMatchingServices (kIOMasterPortDefault,
282 IOServiceNameMatching ("battery"),
284 if (status != kIOReturnSuccess)
286 DBG ("IOServiceGetMatchingServices failed.");
290 while ((io_obj = IOIteratorNext (iterator)))
292 status = IORegistryEntryCreateCFProperties (io_obj,
293 (CFMutableDictionaryRef *) &bat_root_dict,
296 if (status != kIOReturnSuccess)
298 DBG ("IORegistryEntryCreateCFProperties failed.");
302 bat_info_arry = (CFArrayRef) CFDictionaryGetValue (bat_root_dict,
303 CFSTR ("IOBatteryInfo"));
304 if (bat_info_arry == NULL)
306 CFRelease (bat_root_dict);
309 bat_info_arry_len = CFArrayGetCount (bat_info_arry);
311 for (bat_info_arry_pos = 0;
312 bat_info_arry_pos < bat_info_arry_len;
315 bat_info_dict = (CFDictionaryRef) CFArrayGetValueAtIndex (bat_info_arry, bat_info_arry_pos);
317 if (*ret_charge == INVALID_VALUE)
319 temp_double = dict_get_double (bat_info_dict,
321 if (temp_double != INVALID_VALUE)
322 *ret_charge = temp_double / 1000.0;
325 if (*ret_current == INVALID_VALUE)
327 temp_double = dict_get_double (bat_info_dict,
329 if (temp_double != INVALID_VALUE)
330 *ret_current = temp_double / 1000.0;
333 if (*ret_voltage == INVALID_VALUE)
335 temp_double = dict_get_double (bat_info_dict,
337 if (temp_double != INVALID_VALUE)
338 *ret_voltage = temp_double / 1000.0;
342 CFRelease (bat_root_dict);
345 IOObjectRelease (iterator);
347 #endif /* HAVE_IOKIT_IOKITLIB_H */
349 static int battery_read (void)
351 #if HAVE_IOKIT_IOKITLIB_H || HAVE_IOKIT_PS_IOPOWERSOURCES_H
352 double charge = INVALID_VALUE; /* Current charge in Ah */
353 double current = INVALID_VALUE; /* Current in A */
354 double voltage = INVALID_VALUE; /* Voltage in V */
356 double charge_rel = INVALID_VALUE; /* Current charge in percent */
357 double charge_abs = INVALID_VALUE; /* Total capacity */
359 #if HAVE_IOKIT_PS_IOPOWERSOURCES_H
360 get_via_io_power_sources (&charge_rel, ¤t, &voltage);
362 #if HAVE_IOKIT_IOKITLIB_H
363 get_via_generic_iokit (&charge_abs, ¤t, &voltage);
366 if ((charge_rel != INVALID_VALUE) && (charge_abs != INVALID_VALUE))
367 charge = charge_abs * charge_rel / 100.0;
369 if (charge != INVALID_VALUE)
370 battery_submit ("0", "charge", charge);
371 if (current != INVALID_VALUE)
372 battery_submit ("0", "current", current);
373 if (voltage != INVALID_VALUE)
374 battery_submit ("0", "voltage", voltage);
375 /* #endif HAVE_IOKIT_IOKITLIB_H || HAVE_IOKIT_PS_IOPOWERSOURCES_H */
388 for (i = 0; i < battery_pmu_num; i++)
390 char batnum_str[256];
391 double current = INVALID_VALUE;
392 double voltage = INVALID_VALUE;
393 double charge = INVALID_VALUE;
394 double *valptr = NULL;
396 len = snprintf (filename, sizeof (filename), battery_pmu_file, i);
397 if ((len >= sizeof (filename)) || (len < 0))
400 len = snprintf (batnum_str, sizeof (batnum_str), "%i", i);
401 if ((len >= sizeof (batnum_str)) || (len < 0))
404 if ((fh = fopen (filename, "r")) == NULL)
407 while (fgets (buffer, sizeof (buffer), fh) != NULL)
409 numfields = strsplit (buffer, fields, 8);
414 if (strcmp ("current", fields[0]) == 0)
416 else if (strcmp ("voltage", fields[0]) == 0)
418 else if (strcmp ("charge", fields[0]) == 0)
430 *valptr = strtod (fields[2], &endptr) / 1000.0;
432 if ((fields[2] == endptr) || (errno != 0))
433 *valptr = INVALID_VALUE;
440 if (charge != INVALID_VALUE)
441 battery_submit ("0", "charge", charge);
442 if (current != INVALID_VALUE)
443 battery_submit ("0", "current", current);
444 if (voltage != INVALID_VALUE)
445 battery_submit ("0", "voltage", voltage);
448 if (access ("/proc/acpi/battery", R_OK | X_OK) == 0)
450 double current = INVALID_VALUE;
451 double voltage = INVALID_VALUE;
452 double charge = INVALID_VALUE;
453 double *valptr = NULL;
459 if ((dh = opendir ("/proc/acpi/battery")) == NULL)
461 syslog (LOG_ERR, "Cannot open `/proc/acpi/battery': %s", strerror (errno));
465 while ((ent = readdir (dh)) != NULL)
467 if (ent->d_name[0] == '.')
470 len = snprintf (filename, sizeof (filename),
471 "/proc/acpi/battery/%s/state",
473 if ((len >= sizeof (filename)) || (len < 0))
476 if ((fh = fopen (filename, "r")) == NULL)
478 syslog (LOG_ERR, "Cannot open `%s': %s", filename, strerror (errno));
483 * [11:00] <@tokkee> $ cat /proc/acpi/battery/BAT1/state
484 * [11:00] <@tokkee> present: yes
485 * [11:00] <@tokkee> capacity state: ok
486 * [11:00] <@tokkee> charging state: charging
487 * [11:00] <@tokkee> present rate: 1724 mA
488 * [11:00] <@tokkee> remaining capacity: 4136 mAh
489 * [11:00] <@tokkee> present voltage: 12428 mV
491 while (fgets (buffer, sizeof (buffer), fh) != NULL)
493 numfields = strsplit (buffer, fields, 8);
498 if ((strcmp (fields[0], "present") == 0)
499 && (strcmp (fields[1], "rate:") == 0))
501 else if ((strcmp (fields[0], "remaining") == 0)
502 && (strcmp (fields[1], "capacity:") == 0))
504 else if ((strcmp (fields[0], "present") == 0)
505 && (strcmp (fields[1], "voltage:") == 0))
510 if ((strcmp (fields[0], "charging") == 0)
511 && (strcmp (fields[1], "state:") == 0))
513 if (strcmp (fields[2], "charging") == 0)
526 *valptr = strtod (fields[2], &endptr) / 1000.0;
528 if ((fields[2] == endptr) || (errno != 0))
529 *valptr = INVALID_VALUE;
531 } /* while (fgets (buffer, sizeof (buffer), fh) != NULL) */
535 if ((current != INVALID_VALUE) && (charging == 0))
538 if (charge != INVALID_VALUE)
539 battery_submit ("0", "charge", charge);
540 if (current != INVALID_VALUE)
541 battery_submit ("0", "current", current);
542 if (voltage != INVALID_VALUE)
543 battery_submit ("0", "voltage", voltage);
548 #endif /* KERNEL_LINUX */
552 #endif /* BATTERY_HAVE_READ */
554 void module_register (void)
556 plugin_register_data_set (&charge_ds);
557 plugin_register_data_set (¤t_ds);
558 plugin_register_data_set (&voltage_ds);
560 #if BATTERY_HAVE_READ
561 plugin_register_init ("battery", battery_init);
562 plugin_register_read ("battery", battery_read);
563 #endif /* BATTERY_HAVE_READ */