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 # define BATTERY_HAVE_READ 1
54 # define BATTERY_HAVE_READ 0
57 #define INVALID_VALUE 47841.29
60 #if HAVE_IOKIT_IOKITLIB_H || HAVE_IOKIT_PS_IOPOWERSOURCES_H
61 /* No global variables */
62 /* #endif HAVE_IOKIT_IOKITLIB_H || HAVE_IOKIT_PS_IOPOWERSOURCES_H */
65 static int battery_pmu_num = 0;
66 static char *battery_pmu_file = "/proc/pmu/battery_%i";
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 = snprintf (filename, sizeof (filename), battery_pmu_file, battery_pmu_num);
83 if ((len >= sizeof (filename)) || (len < 0))
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 strcpy (vl.host, hostname_g);
105 strcpy (vl.plugin, "battery");
106 strcpy (vl.plugin_instance, plugin_instance);
108 plugin_dispatch_values (type, &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 */
318 static int battery_read (void)
320 #if HAVE_IOKIT_IOKITLIB_H || HAVE_IOKIT_PS_IOPOWERSOURCES_H
321 double charge = INVALID_VALUE; /* Current charge in Ah */
322 double current = INVALID_VALUE; /* Current in A */
323 double voltage = INVALID_VALUE; /* Voltage in V */
325 double charge_rel = INVALID_VALUE; /* Current charge in percent */
326 double charge_abs = INVALID_VALUE; /* Total capacity */
328 #if HAVE_IOKIT_PS_IOPOWERSOURCES_H
329 get_via_io_power_sources (&charge_rel, ¤t, &voltage);
331 #if HAVE_IOKIT_IOKITLIB_H
332 get_via_generic_iokit (&charge_abs, ¤t, &voltage);
335 if ((charge_rel != INVALID_VALUE) && (charge_abs != INVALID_VALUE))
336 charge = charge_abs * charge_rel / 100.0;
338 if (charge != INVALID_VALUE)
339 battery_submit ("0", "charge", charge);
340 if (current != INVALID_VALUE)
341 battery_submit ("0", "current", current);
342 if (voltage != INVALID_VALUE)
343 battery_submit ("0", "voltage", voltage);
344 /* #endif HAVE_IOKIT_IOKITLIB_H || HAVE_IOKIT_PS_IOPOWERSOURCES_H */
357 for (i = 0; i < battery_pmu_num; i++)
359 char batnum_str[256];
360 double current = INVALID_VALUE;
361 double voltage = INVALID_VALUE;
362 double charge = INVALID_VALUE;
363 double *valptr = NULL;
365 len = snprintf (filename, sizeof (filename), battery_pmu_file, i);
366 if ((len >= sizeof (filename)) || (len < 0))
369 len = snprintf (batnum_str, sizeof (batnum_str), "%i", i);
370 if ((len >= sizeof (batnum_str)) || (len < 0))
373 if ((fh = fopen (filename, "r")) == NULL)
376 while (fgets (buffer, sizeof (buffer), fh) != NULL)
378 numfields = strsplit (buffer, fields, 8);
383 if (strcmp ("current", fields[0]) == 0)
385 else if (strcmp ("voltage", fields[0]) == 0)
387 else if (strcmp ("charge", fields[0]) == 0)
399 *valptr = strtod (fields[2], &endptr) / 1000.0;
401 if ((fields[2] == endptr) || (errno != 0))
402 *valptr = INVALID_VALUE;
409 if (charge != INVALID_VALUE)
410 battery_submit ("0", "charge", charge);
411 if (current != INVALID_VALUE)
412 battery_submit ("0", "current", current);
413 if (voltage != INVALID_VALUE)
414 battery_submit ("0", "voltage", voltage);
417 if (access ("/proc/acpi/battery", R_OK | X_OK) == 0)
419 double current = INVALID_VALUE;
420 double voltage = INVALID_VALUE;
421 double charge = INVALID_VALUE;
422 double *valptr = NULL;
428 if ((dh = opendir ("/proc/acpi/battery")) == NULL)
431 ERROR ("Cannot open `/proc/acpi/battery': %s",
432 sstrerror (errno, errbuf, sizeof (errbuf)));
436 while ((ent = readdir (dh)) != NULL)
438 if (ent->d_name[0] == '.')
441 len = snprintf (filename, sizeof (filename),
442 "/proc/acpi/battery/%s/state",
444 if ((len >= sizeof (filename)) || (len < 0))
447 if ((fh = fopen (filename, "r")) == NULL)
450 ERROR ("Cannot open `%s': %s", filename,
451 sstrerror (errno, errbuf,
457 * [11:00] <@tokkee> $ cat /proc/acpi/battery/BAT1/state
458 * [11:00] <@tokkee> present: yes
459 * [11:00] <@tokkee> capacity state: ok
460 * [11:00] <@tokkee> charging state: charging
461 * [11:00] <@tokkee> present rate: 1724 mA
462 * [11:00] <@tokkee> remaining capacity: 4136 mAh
463 * [11:00] <@tokkee> present voltage: 12428 mV
465 while (fgets (buffer, sizeof (buffer), fh) != NULL)
467 numfields = strsplit (buffer, fields, 8);
472 if ((strcmp (fields[0], "present") == 0)
473 && (strcmp (fields[1], "rate:") == 0))
475 else if ((strcmp (fields[0], "remaining") == 0)
476 && (strcmp (fields[1], "capacity:") == 0))
478 else if ((strcmp (fields[0], "present") == 0)
479 && (strcmp (fields[1], "voltage:") == 0))
484 if ((strcmp (fields[0], "charging") == 0)
485 && (strcmp (fields[1], "state:") == 0))
487 if (strcmp (fields[2], "charging") == 0)
500 *valptr = strtod (fields[2], &endptr) / 1000.0;
502 if ((fields[2] == endptr) || (errno != 0))
503 *valptr = INVALID_VALUE;
505 } /* while (fgets (buffer, sizeof (buffer), fh) != NULL) */
509 if ((current != INVALID_VALUE) && (charging == 0))
512 if (charge != INVALID_VALUE)
513 battery_submit ("0", "charge", charge);
514 if (current != INVALID_VALUE)
515 battery_submit ("0", "current", current);
516 if (voltage != INVALID_VALUE)
517 battery_submit ("0", "voltage", voltage);
522 #endif /* KERNEL_LINUX */
526 #endif /* BATTERY_HAVE_READ */
528 void module_register (void)
530 #if BATTERY_HAVE_READ
531 plugin_register_init ("battery", battery_init);
532 plugin_register_read ("battery", battery_read);
533 #endif /* BATTERY_HAVE_READ */
534 } /* void module_register */