2 * collectd - src/battery.c
3 * Copyright (C) 2006 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; either version 2 of the License, or (at your
8 * option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * Florian octo Forster <octo at verplant.org>
26 #include "utils_debug.h"
28 #define MODULE_NAME "battery"
31 #if HAVE_MACH_MACH_TYPES_H
32 # include <mach/mach_types.h>
34 #if HAVE_MACH_MACH_INIT_H
35 # include <mach/mach_init.h>
37 #if HAVE_MACH_MACH_ERROR_H
38 # include <mach/mach_error.h>
40 #if HAVE_COREFOUNDATION_COREFOUNDATION_H
41 # include <CoreFoundation/CoreFoundation.h>
43 #if HAVE_IOKIT_IOKITLIB_H
44 # include <IOKit/IOKitLib.h>
46 #if HAVE_IOKIT_IOTYPES_H
47 # include <IOKit/IOTypes.h>
49 #if HAVE_IOKIT_PS_IOPOWERSOURCES_H
50 # include <IOKit/ps/IOPowerSources.h>
52 #if HAVE_IOKIT_PS_IOPSKEYS_H
53 # include <IOKit/ps/IOPSKeys.h>
56 #if HAVE_IOKIT_IOKITLIB_H || HAVE_IOKIT_PS_IOPOWERSOURCES_H || KERNEL_LINUX
57 # define BATTERY_HAVE_READ 1
59 # define BATTERY_HAVE_READ 0
62 #define INVALID_VALUE 47841.29
64 static char *battery_current_file = "battery-%s/current.rrd";
65 static char *battery_voltage_file = "battery-%s/voltage.rrd";
66 static char *battery_charge_file = "battery-%s/charge.rrd";
68 static char *ds_def_current[] =
70 "DS:current:GAUGE:"COLLECTD_HEARTBEAT":U:U",
73 static int ds_num_current = 1;
75 static char *ds_def_voltage[] =
77 "DS:voltage:GAUGE:"COLLECTD_HEARTBEAT":U:U",
80 static int ds_num_voltage = 1;
82 static char *ds_def_charge[] =
84 "DS:charge:GAUGE:"COLLECTD_HEARTBEAT":0:U",
87 static int ds_num_charge = 1;
89 #if HAVE_IOKIT_IOKITLIB_H || HAVE_IOKIT_PS_IOPOWERSOURCES_H
90 /* No global variables */
91 /* #endif HAVE_IOKIT_IOKITLIB_H || HAVE_IOKIT_PS_IOPOWERSOURCES_H */
94 static int battery_pmu_num = 0;
95 static char *battery_pmu_file = "/proc/pmu/battery_%i";
96 #endif /* KERNEL_LINUX */
98 static void battery_init (void)
100 #if HAVE_IOKIT_IOKITLIB_H || HAVE_IOKIT_PS_IOPOWERSOURCES_H
101 /* No init neccessary */
102 /* #endif HAVE_IOKIT_IOKITLIB_H || HAVE_IOKIT_PS_IOPOWERSOURCES_H */
106 char filename[BUFSIZE];
108 for (battery_pmu_num = 0; ; battery_pmu_num++)
110 len = snprintf (filename, BUFSIZE, battery_pmu_file, battery_pmu_num);
112 if ((len >= BUFSIZE) || (len < 0))
115 if (access (filename, R_OK))
118 #endif /* KERNEL_LINUX */
123 static void battery_current_write (char *host, char *inst, char *val)
125 char filename[BUFSIZE];
128 len = snprintf (filename, BUFSIZE, battery_current_file, inst);
129 if ((len >= BUFSIZE) || (len < 0))
132 rrd_update_file (host, filename, val,
133 ds_def_current, ds_num_current);
136 static void battery_voltage_write (char *host, char *inst, char *val)
138 char filename[BUFSIZE];
141 len = snprintf (filename, BUFSIZE, battery_voltage_file, inst);
142 if ((len >= BUFSIZE) || (len < 0))
145 rrd_update_file (host, filename, val,
146 ds_def_voltage, ds_num_voltage);
149 static void battery_charge_write (char *host, char *inst, char *val)
151 char filename[BUFSIZE];
154 len = snprintf (filename, BUFSIZE, battery_charge_file, inst);
155 if ((len >= BUFSIZE) || (len < 0))
158 rrd_update_file (host, filename, val,
159 ds_def_charge, ds_num_charge);
162 #if BATTERY_HAVE_READ
163 static void battery_submit (char *inst, double current, double voltage, double charge)
166 char buffer[BUFSIZE];
168 if (current != INVALID_VALUE)
170 len = snprintf (buffer, BUFSIZE, "N:%.3f", current);
172 if ((len > 0) && (len < BUFSIZE))
173 plugin_submit ("battery_current", inst, buffer);
177 plugin_submit ("battery_current", inst, "N:U");
180 if (voltage != INVALID_VALUE)
182 len = snprintf (buffer, BUFSIZE, "N:%.3f", voltage);
184 if ((len > 0) && (len < BUFSIZE))
185 plugin_submit ("battery_voltage", inst, buffer);
189 plugin_submit ("battery_voltage", inst, "N:U");
192 if (charge != INVALID_VALUE)
194 len = snprintf (buffer, BUFSIZE, "N:%.3f", charge);
196 if ((len > 0) && (len < BUFSIZE))
197 plugin_submit ("battery_charge", inst, buffer);
201 plugin_submit ("battery_charge", inst, "N:U");
205 double dict_get_double (CFDictionaryRef dict, char *key_string)
212 key_obj = CFStringCreateWithCString (kCFAllocatorDefault, key_string,
213 kCFStringEncodingASCII);
216 DBG ("CFStringCreateWithCString (%s) failed.\n", key_string);
217 return (INVALID_VALUE);
220 if ((val_obj = CFDictionaryGetValue (dict, key_obj)) == NULL)
222 DBG ("CFDictionaryGetValue (%s) failed.", key_string);
224 return (INVALID_VALUE);
228 if (CFGetTypeID (val_obj) == CFNumberGetTypeID ())
230 if (CFNumberIsFloatType (val_obj))
232 CFNumberGetValue (val_obj,
238 CFNumberGetValue (val_obj,
239 kCFNumberLongLongType,
241 val_double = val_int;
246 DBG ("CFGetTypeID (val_obj) = %i", (int) CFGetTypeID (val_obj));
247 return (INVALID_VALUE);
253 #if HAVE_IOKIT_PS_IOPOWERSOURCES_H
254 static void get_via_io_power_sources (double *ret_charge,
261 CFDictionaryRef ps_dict;
267 ps_raw = IOPSCopyPowerSourcesInfo ();
268 ps_array = IOPSCopyPowerSourcesList (ps_raw);
269 ps_array_len = CFArrayGetCount (ps_array);
271 DBG ("ps_array_len == %i", ps_array_len);
273 for (i = 0; i < ps_array_len; i++)
275 ps_obj = CFArrayGetValueAtIndex (ps_array, i);
276 ps_dict = IOPSGetPowerSourceDescription (ps_raw, ps_obj);
280 DBG ("IOPSGetPowerSourceDescription failed.");
284 if (CFGetTypeID (ps_dict) != CFDictionaryGetTypeID ())
286 DBG ("IOPSGetPowerSourceDescription did not return a CFDictionaryRef");
290 /* FIXME: Check if this is really an internal battery */
292 if (*ret_charge == INVALID_VALUE)
294 /* This is the charge in percent. */
295 temp_double = dict_get_double (ps_dict,
296 kIOPSCurrentCapacityKey);
297 if ((temp_double != INVALID_VALUE)
298 && (temp_double >= 0.0)
299 && (temp_double <= 100.0))
300 *ret_charge = temp_double;
303 if (*ret_current == INVALID_VALUE)
305 temp_double = dict_get_double (ps_dict,
307 if (temp_double != INVALID_VALUE)
308 *ret_current = temp_double / 1000.0;
311 if (*ret_voltage == INVALID_VALUE)
313 temp_double = dict_get_double (ps_dict,
315 if (temp_double != INVALID_VALUE)
316 *ret_voltage = temp_double / 1000.0;
323 #endif /* HAVE_IOKIT_PS_IOPOWERSOURCES_H */
325 #if HAVE_IOKIT_IOKITLIB_H
326 static void get_via_generic_iokit (double *ret_charge,
330 kern_return_t status;
331 io_iterator_t iterator;
334 CFDictionaryRef bat_root_dict;
335 CFArrayRef bat_info_arry;
336 CFIndex bat_info_arry_len;
337 CFIndex bat_info_arry_pos;
338 CFDictionaryRef bat_info_dict;
342 status = IOServiceGetMatchingServices (kIOMasterPortDefault,
343 IOServiceNameMatching ("battery"),
345 if (status != kIOReturnSuccess)
347 DBG ("IOServiceGetMatchingServices failed.");
351 while ((io_obj = IOIteratorNext (iterator)))
353 status = IORegistryEntryCreateCFProperties (io_obj,
354 (CFMutableDictionaryRef *) &bat_root_dict,
357 if (status != kIOReturnSuccess)
359 DBG ("IORegistryEntryCreateCFProperties failed.");
363 bat_info_arry = (CFArrayRef) CFDictionaryGetValue (bat_root_dict,
364 CFSTR ("IOBatteryInfo"));
365 if (bat_info_arry == NULL)
367 CFRelease (bat_root_dict);
370 bat_info_arry_len = CFArrayGetCount (bat_info_arry);
372 for (bat_info_arry_pos = 0;
373 bat_info_arry_pos < bat_info_arry_len;
376 bat_info_dict = (CFDictionaryRef) CFArrayGetValueAtIndex (bat_info_arry, bat_info_arry_pos);
378 if (*ret_charge == INVALID_VALUE)
380 temp_double = dict_get_double (bat_info_dict,
382 if (temp_double != INVALID_VALUE)
383 *ret_charge = temp_double / 1000.0;
386 if (*ret_current == INVALID_VALUE)
388 temp_double = dict_get_double (bat_info_dict,
390 if (temp_double != INVALID_VALUE)
391 *ret_current = temp_double / 1000.0;
394 if (*ret_voltage == INVALID_VALUE)
396 temp_double = dict_get_double (bat_info_dict,
398 if (temp_double != INVALID_VALUE)
399 *ret_voltage = temp_double / 1000.0;
403 CFRelease (bat_root_dict);
406 IOObjectRelease (iterator);
408 #endif /* HAVE_IOKIT_IOKITLIB_H */
410 static void 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 || (current != INVALID_VALUE)
432 || (voltage != INVALID_VALUE))
433 battery_submit ("0", current, voltage, charge);
434 /* #endif HAVE_IOKIT_IOKITLIB_H || HAVE_IOKIT_PS_IOPOWERSOURCES_H */
438 char buffer[BUFSIZE];
439 char filename[BUFSIZE];
447 for (i = 0; i < battery_pmu_num; i++)
449 char batnum_str[BUFSIZE];
450 double current = INVALID_VALUE;
451 double voltage = INVALID_VALUE;
452 double charge = INVALID_VALUE;
453 double *valptr = NULL;
455 len = snprintf (filename, BUFSIZE, battery_pmu_file, i);
456 if ((len >= BUFSIZE) || (len < 0))
459 len = snprintf (batnum_str, BUFSIZE, "%i", i);
460 if ((len >= BUFSIZE) || (len < 0))
463 if ((fh = fopen (filename, "r")) == NULL)
466 while (fgets (buffer, BUFSIZE, fh) != NULL)
468 numfields = strsplit (buffer, fields, 8);
473 if (strcmp ("current", fields[0]) == 0)
475 else if (strcmp ("voltage", fields[0]) == 0)
477 else if (strcmp ("charge", fields[0]) == 0)
489 *valptr = strtod (fields[2], &endptr) / 1000.0;
491 if ((fields[2] == endptr) || (errno != 0))
492 *valptr = INVALID_VALUE;
496 if ((current != INVALID_VALUE)
497 || (voltage != INVALID_VALUE)
498 || (charge != INVALID_VALUE))
499 battery_submit (batnum_str, current, voltage, charge);
505 if (access ("/proc/acpi/battery", R_OK | X_OK) == 0)
507 double current = INVALID_VALUE;
508 double voltage = INVALID_VALUE;
509 double charge = INVALID_VALUE;
510 double *valptr = NULL;
516 if ((dh = opendir ("/proc/acpi/battery")) == NULL)
518 syslog (LOG_ERR, "Cannot open `/proc/acpi/battery': %s", strerror (errno));
522 while ((ent = readdir (dh)) != NULL)
524 if (ent->d_name[0] == '.')
527 len = snprintf (filename, BUFSIZE, "/proc/acpi/battery/%s/state", ent->d_name);
528 if ((len >= BUFSIZE) || (len < 0))
531 if ((fh = fopen (filename, "r")) == NULL)
533 syslog (LOG_ERR, "Cannot open `%s': %s", filename, strerror (errno));
538 * [11:00] <@tokkee> $ cat /proc/acpi/battery/BAT1/state
539 * [11:00] <@tokkee> present: yes
540 * [11:00] <@tokkee> capacity state: ok
541 * [11:00] <@tokkee> charging state: charging
542 * [11:00] <@tokkee> present rate: 1724 mA
543 * [11:00] <@tokkee> remaining capacity: 4136 mAh
544 * [11:00] <@tokkee> present voltage: 12428 mV
546 while (fgets (buffer, BUFSIZE, fh) != NULL)
548 numfields = strsplit (buffer, fields, 8);
553 if ((strcmp (fields[0], "present") == 0)
554 && (strcmp (fields[1], "rate:") == 0))
556 else if ((strcmp (fields[0], "remaining") == 0)
557 && (strcmp (fields[1], "capacity:") == 0))
559 else if ((strcmp (fields[0], "present") == 0)
560 && (strcmp (fields[1], "voltage:") == 0))
565 if ((strcmp (fields[0], "charging") == 0)
566 && (strcmp (fields[1], "state:") == 0))
568 if (strcmp (fields[2], "charging") == 0)
581 *valptr = strtod (fields[2], &endptr) / 1000.0;
583 if ((fields[2] == endptr) || (errno != 0))
584 *valptr = INVALID_VALUE;
588 if ((current != INVALID_VALUE) && (charging == 0))
591 if ((current != INVALID_VALUE)
592 || (voltage != INVALID_VALUE)
593 || (charge != INVALID_VALUE))
594 battery_submit (ent->d_name, current, voltage, charge);
601 #endif /* KERNEL_LINUX */
604 # define battery_read NULL
605 #endif /* BATTERY_HAVE_READ */
607 void module_register (void)
609 plugin_register (MODULE_NAME, battery_init, battery_read, NULL);
610 plugin_register ("battery_current", NULL, NULL, battery_current_write);
611 plugin_register ("battery_voltage", NULL, NULL, battery_voltage_write);
612 plugin_register ("battery_charge", NULL, NULL, battery_charge_write);