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_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_PS_IOPOWERSOURCES_H
90 /* No global variables */
91 /* #endif 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_PS_IOPOWERSOURCES_H
101 /* No init neccessary */
102 /* #endif 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 static void battery_read (void)
207 #if HAVE_IOKIT_PS_IOPOWERSOURCES_H
211 CFDictionaryRef ps_dict;
218 double charge = INVALID_VALUE;
219 double current = INVALID_VALUE;
220 double voltage = INVALID_VALUE;
222 ps_raw = IOPSCopyPowerSourcesInfo ();
223 ps_array = IOPSCopyPowerSourcesList (ps_raw);
224 ps_array_len = CFArrayGetCount (ps_array);
226 DBG ("ps_array_len == %i", ps_array_len);
228 for (i = 0; i < ps_array_len; i++)
230 ps_obj = CFArrayGetValueAtIndex (ps_array, i);
231 ps_dict = IOPSGetPowerSourceDescription (ps_raw, ps_obj);
233 if (CFGetTypeID (ps_dict) != CFDictionaryGetTypeID ())
235 DBG ("IOPSGetPowerSourceDescription did not return a CFDictionaryRef");
241 /* Get the current capacity/charge */
243 charge = INVALID_VALUE;
244 if (CFDictionaryGetValueIfPresent (ps_dict,
245 CFSTR (kIOPSCurrentCapacityKey),
248 if (CFGetTypeID (ps_value) != CFNumberGetTypeID ())
249 CFNumberGetValue (ps_value,
253 DBG ("kIOPSCurrentCapacityKey: Not a CFNumber");
255 DBG ("charge = %f", charge);
258 DBG ("`%s' does not exist", kIOPSCurrentCapacityKey);
260 /* Get the current */
262 current = INVALID_VALUE;
263 if (CFDictionaryGetValueIfPresent (ps_dict,
264 CFSTR (kIOPSCurrentKey),
267 if (CFGetTypeID (ps_value) != CFNumberGetTypeID ())
268 CFNumberGetValue (ps_value,
272 DBG ("kIOPSCurrentKey: Not a CFNumber");
273 DBG ("current = %f", current);
276 DBG ("`%s' does not exist", kIOPSCurrentKey);
278 /* Get the voltage */
280 voltage = INVALID_VALUE;
281 if (CFDictionaryGetValueIfPresent (ps_dict,
282 CFSTR (kIOPSVoltageKey),
285 if (CFGetTypeID (ps_value) != CFNumberGetTypeID ())
286 CFNumberGetValue (ps_value,
290 DBG ("kIOPSVoltageKey: Not a CFNumber");
291 DBG ("voltage = %f", voltage);
294 DBG ("`%s' does not exist", kIOPSVoltageKey);
296 /* Get the name of the device.. */
298 if (CFDictionaryGetValueIfPresent (ps_dict,
299 CFSTR (kIOPSNameKey),
302 if (CFGetTypeID (ps_value) != CFStringGetTypeID ())
304 if (!CFStringGetCString (ps_value,
306 kCFStringEncodingASCII))
311 DBG ("kIOPSNameKey: Not a CFStringGetTypeID");
313 DBG ("Original string: `%s'", name);
317 strncpy (name, "unknown", 128);
320 for (i = 0; i < 128; i++)
324 else if (isalnum (name[i]))
325 name[i] = (char) tolower (name[i]);
330 battery_submit (name, current, voltage, charge);
336 /* #endif HAVE_IOKIT_PS_IOPOWERSOURCES_H */
340 char buffer[BUFSIZE];
341 char filename[BUFSIZE];
349 for (i = 0; i < battery_pmu_num; i++)
351 char batnum_str[BUFSIZE];
352 double current = INVALID_VALUE;
353 double voltage = INVALID_VALUE;
354 double charge = INVALID_VALUE;
355 double *valptr = NULL;
357 len = snprintf (filename, BUFSIZE, battery_pmu_file, i);
358 if ((len >= BUFSIZE) || (len < 0))
361 len = snprintf (batnum_str, BUFSIZE, "%i", i);
362 if ((len >= BUFSIZE) || (len < 0))
365 if ((fh = fopen (filename, "r")) == NULL)
368 while (fgets (buffer, BUFSIZE, fh) != NULL)
370 numfields = strsplit (buffer, fields, 8);
375 if (strcmp ("current", fields[0]) == 0)
377 else if (strcmp ("voltage", fields[0]) == 0)
379 else if (strcmp ("charge", fields[0]) == 0)
391 *valptr = strtod (fields[2], &endptr) / 1000.0;
393 if ((fields[2] == endptr) || (errno != 0))
394 *valptr = INVALID_VALUE;
398 if ((current != INVALID_VALUE)
399 || (voltage != INVALID_VALUE)
400 || (charge != INVALID_VALUE))
401 battery_submit (batnum_str, current, voltage, charge);
407 if (access ("/proc/acpi/battery", R_OK | X_OK) == 0)
409 double current = INVALID_VALUE;
410 double voltage = INVALID_VALUE;
411 double charge = INVALID_VALUE;
412 double *valptr = NULL;
418 if ((dh = opendir ("/proc/acpi/battery")) == NULL)
420 syslog (LOG_ERR, "Cannot open `/proc/acpi/battery': %s", strerror (errno));
424 while ((ent = readdir (dh)) != NULL)
426 if (ent->d_name[0] == '.')
429 len = snprintf (filename, BUFSIZE, "/proc/acpi/battery/%s/state", ent->d_name);
430 if ((len >= BUFSIZE) || (len < 0))
433 if ((fh = fopen (filename, "r")) == NULL)
435 syslog (LOG_ERR, "Cannot open `%s': %s", filename, strerror (errno));
440 * [11:00] <@tokkee> $ cat /proc/acpi/battery/BAT1/state
441 * [11:00] <@tokkee> present: yes
442 * [11:00] <@tokkee> capacity state: ok
443 * [11:00] <@tokkee> charging state: charging
444 * [11:00] <@tokkee> present rate: 1724 mA
445 * [11:00] <@tokkee> remaining capacity: 4136 mAh
446 * [11:00] <@tokkee> present voltage: 12428 mV
448 while (fgets (buffer, BUFSIZE, fh) != NULL)
450 numfields = strsplit (buffer, fields, 8);
455 if ((strcmp (fields[0], "present") == 0)
456 && (strcmp (fields[1], "rate:") == 0))
458 else if ((strcmp (fields[0], "remaining") == 0)
459 && (strcmp (fields[1], "capacity:") == 0))
461 else if ((strcmp (fields[0], "present") == 0)
462 && (strcmp (fields[1], "voltage:") == 0))
467 if ((strcmp (fields[0], "charging") == 0)
468 && (strcmp (fields[1], "state:") == 0))
470 if (strcmp (fields[2], "charging") == 0)
483 *valptr = strtod (fields[2], &endptr) / 1000.0;
485 if ((fields[2] == endptr) || (errno != 0))
486 *valptr = INVALID_VALUE;
490 if ((current != INVALID_VALUE) && (charging == 0))
493 if ((current != INVALID_VALUE)
494 || (voltage != INVALID_VALUE)
495 || (charge != INVALID_VALUE))
496 battery_submit (ent->d_name, current, voltage, charge);
503 #endif /* KERNEL_LINUX */
506 # define battery_read NULL
507 #endif /* BATTERY_HAVE_READ */
509 void module_register (void)
511 plugin_register (MODULE_NAME, battery_init, battery_read, NULL);
512 plugin_register ("battery_current", NULL, NULL, battery_current_write);
513 plugin_register ("battery_voltage", NULL, NULL, battery_voltage_write);
514 plugin_register ("battery_charge", NULL, NULL, battery_charge_write);