1 /**
2 * collectd - src/battery.c
3 * Copyright (C) 2006,2007 Florian octo Forster
4 *
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.
8 *
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.
13 *
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
17 *
18 * Authors:
19 * Florian octo Forster <octo at verplant.org>
20 **/
22 #include "collectd.h"
23 #include "common.h"
24 #include "plugin.h"
26 #if HAVE_MACH_MACH_TYPES_H
27 # include <mach/mach_types.h>
28 #endif
29 #if HAVE_MACH_MACH_INIT_H
30 # include <mach/mach_init.h>
31 #endif
32 #if HAVE_MACH_MACH_ERROR_H
33 # include <mach/mach_error.h>
34 #endif
35 #if HAVE_COREFOUNDATION_COREFOUNDATION_H
36 # include <CoreFoundation/CoreFoundation.h>
37 #endif
38 #if HAVE_IOKIT_IOKITLIB_H
39 # include <IOKit/IOKitLib.h>
40 #endif
41 #if HAVE_IOKIT_IOTYPES_H
42 # include <IOKit/IOTypes.h>
43 #endif
44 #if HAVE_IOKIT_PS_IOPOWERSOURCES_H
45 # include <IOKit/ps/IOPowerSources.h>
46 #endif
47 #if HAVE_IOKIT_PS_IOPSKEYS_H
48 # include <IOKit/ps/IOPSKeys.h>
49 #endif
51 #if !HAVE_IOKIT_IOKITLIB_H && !HAVE_IOKIT_PS_IOPOWERSOURCES_H && !KERNEL_LINUX
52 # error "No applicable input method."
53 #endif
55 #define INVALID_VALUE 47841.29
57 #if HAVE_IOKIT_IOKITLIB_H || HAVE_IOKIT_PS_IOPOWERSOURCES_H
58 /* No global variables */
59 /* #endif HAVE_IOKIT_IOKITLIB_H || HAVE_IOKIT_PS_IOPOWERSOURCES_H */
61 #elif KERNEL_LINUX
62 static int battery_pmu_num = 0;
63 static char *battery_pmu_file = "/proc/pmu/battery_%i";
64 static const char *battery_acpi_dir = "/proc/acpi/battery";
65 #endif /* KERNEL_LINUX */
67 static int battery_init (void)
68 {
69 #if HAVE_IOKIT_IOKITLIB_H || HAVE_IOKIT_PS_IOPOWERSOURCES_H
70 /* No init neccessary */
71 /* #endif HAVE_IOKIT_IOKITLIB_H || HAVE_IOKIT_PS_IOPOWERSOURCES_H */
73 #elif KERNEL_LINUX
74 int len;
75 char filename[128];
77 for (battery_pmu_num = 0; ; battery_pmu_num++)
78 {
79 len = ssnprintf (filename, sizeof (filename), battery_pmu_file, battery_pmu_num);
81 if ((len < 0) || ((unsigned int)len >= sizeof (filename)))
82 break;
84 if (access (filename, R_OK))
85 break;
86 }
87 #endif /* KERNEL_LINUX */
89 return (0);
90 }
92 static void battery_submit (const char *plugin_instance, const char *type, double value)
93 {
94 value_t values[1];
95 value_list_t vl = VALUE_LIST_INIT;
97 values[0].gauge = value;
99 vl.values = values;
100 vl.values_len = 1;
101 vl.time = time (NULL);
102 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
103 sstrncpy (vl.plugin, "battery", sizeof (vl.plugin));
104 sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
105 sstrncpy (vl.type, type, sizeof (vl.type));
107 plugin_dispatch_values (&vl);
108 } /* void battery_submit */
110 #if HAVE_IOKIT_PS_IOPOWERSOURCES_H || HAVE_IOKIT_IOKITLIB_H
111 double dict_get_double (CFDictionaryRef dict, char *key_string)
112 {
113 double val_double;
114 long long val_int;
115 CFNumberRef val_obj;
116 CFStringRef key_obj;
118 key_obj = CFStringCreateWithCString (kCFAllocatorDefault, key_string,
119 kCFStringEncodingASCII);
120 if (key_obj == NULL)
121 {
122 DEBUG ("CFStringCreateWithCString (%s) failed.\n", key_string);
123 return (INVALID_VALUE);
124 }
126 if ((val_obj = CFDictionaryGetValue (dict, key_obj)) == NULL)
127 {
128 DEBUG ("CFDictionaryGetValue (%s) failed.", key_string);
129 CFRelease (key_obj);
130 return (INVALID_VALUE);
131 }
132 CFRelease (key_obj);
134 if (CFGetTypeID (val_obj) == CFNumberGetTypeID ())
135 {
136 if (CFNumberIsFloatType (val_obj))
137 {
138 CFNumberGetValue (val_obj,
139 kCFNumberDoubleType,
140 &val_double);
141 }
142 else
143 {
144 CFNumberGetValue (val_obj,
145 kCFNumberLongLongType,
146 &val_int);
147 val_double = val_int;
148 }
149 }
150 else
151 {
152 DEBUG ("CFGetTypeID (val_obj) = %i", (int) CFGetTypeID (val_obj));
153 return (INVALID_VALUE);
154 }
156 return (val_double);
157 }
158 #endif /* HAVE_IOKIT_PS_IOPOWERSOURCES_H || HAVE_IOKIT_IOKITLIB_H */
160 #if HAVE_IOKIT_PS_IOPOWERSOURCES_H
161 static void get_via_io_power_sources (double *ret_charge,
162 double *ret_current,
163 double *ret_voltage)
164 {
165 CFTypeRef ps_raw;
166 CFArrayRef ps_array;
167 int ps_array_len;
168 CFDictionaryRef ps_dict;
169 CFTypeRef ps_obj;
171 double temp_double;
172 int i;
174 ps_raw = IOPSCopyPowerSourcesInfo ();
175 ps_array = IOPSCopyPowerSourcesList (ps_raw);
176 ps_array_len = CFArrayGetCount (ps_array);
178 DEBUG ("ps_array_len == %i", ps_array_len);
180 for (i = 0; i < ps_array_len; i++)
181 {
182 ps_obj = CFArrayGetValueAtIndex (ps_array, i);
183 ps_dict = IOPSGetPowerSourceDescription (ps_raw, ps_obj);
185 if (ps_dict == NULL)
186 {
187 DEBUG ("IOPSGetPowerSourceDescription failed.");
188 continue;
189 }
191 if (CFGetTypeID (ps_dict) != CFDictionaryGetTypeID ())
192 {
193 DEBUG ("IOPSGetPowerSourceDescription did not return a CFDictionaryRef");
194 continue;
195 }
197 /* FIXME: Check if this is really an internal battery */
199 if (*ret_charge == INVALID_VALUE)
200 {
201 /* This is the charge in percent. */
202 temp_double = dict_get_double (ps_dict,
203 kIOPSCurrentCapacityKey);
204 if ((temp_double != INVALID_VALUE)
205 && (temp_double >= 0.0)
206 && (temp_double <= 100.0))
207 *ret_charge = temp_double;
208 }
210 if (*ret_current == INVALID_VALUE)
211 {
212 temp_double = dict_get_double (ps_dict,
213 kIOPSCurrentKey);
214 if (temp_double != INVALID_VALUE)
215 *ret_current = temp_double / 1000.0;
216 }
218 if (*ret_voltage == INVALID_VALUE)
219 {
220 temp_double = dict_get_double (ps_dict,
221 kIOPSVoltageKey);
222 if (temp_double != INVALID_VALUE)
223 *ret_voltage = temp_double / 1000.0;
224 }
225 }
227 CFRelease(ps_array);
228 CFRelease(ps_raw);
229 }
230 #endif /* HAVE_IOKIT_PS_IOPOWERSOURCES_H */
232 #if HAVE_IOKIT_IOKITLIB_H
233 static void get_via_generic_iokit (double *ret_charge,
234 double *ret_current,
235 double *ret_voltage)
236 {
237 kern_return_t status;
238 io_iterator_t iterator;
239 io_object_t io_obj;
241 CFDictionaryRef bat_root_dict;
242 CFArrayRef bat_info_arry;
243 CFIndex bat_info_arry_len;
244 CFIndex bat_info_arry_pos;
245 CFDictionaryRef bat_info_dict;
247 double temp_double;
249 status = IOServiceGetMatchingServices (kIOMasterPortDefault,
250 IOServiceNameMatching ("battery"),
251 &iterator);
252 if (status != kIOReturnSuccess)
253 {
254 DEBUG ("IOServiceGetMatchingServices failed.");
255 return;
256 }
258 while ((io_obj = IOIteratorNext (iterator)))
259 {
260 status = IORegistryEntryCreateCFProperties (io_obj,
261 (CFMutableDictionaryRef *) &bat_root_dict,
262 kCFAllocatorDefault,
263 kNilOptions);
264 if (status != kIOReturnSuccess)
265 {
266 DEBUG ("IORegistryEntryCreateCFProperties failed.");
267 continue;
268 }
270 bat_info_arry = (CFArrayRef) CFDictionaryGetValue (bat_root_dict,
271 CFSTR ("IOBatteryInfo"));
272 if (bat_info_arry == NULL)
273 {
274 CFRelease (bat_root_dict);
275 continue;
276 }
277 bat_info_arry_len = CFArrayGetCount (bat_info_arry);
279 for (bat_info_arry_pos = 0;
280 bat_info_arry_pos < bat_info_arry_len;
281 bat_info_arry_pos++)
282 {
283 bat_info_dict = (CFDictionaryRef) CFArrayGetValueAtIndex (bat_info_arry, bat_info_arry_pos);
285 if (*ret_charge == INVALID_VALUE)
286 {
287 temp_double = dict_get_double (bat_info_dict,
288 "Capacity");
289 if (temp_double != INVALID_VALUE)
290 *ret_charge = temp_double / 1000.0;
291 }
293 if (*ret_current == INVALID_VALUE)
294 {
295 temp_double = dict_get_double (bat_info_dict,
296 "Current");
297 if (temp_double != INVALID_VALUE)
298 *ret_current = temp_double / 1000.0;
299 }
301 if (*ret_voltage == INVALID_VALUE)
302 {
303 temp_double = dict_get_double (bat_info_dict,
304 "Voltage");
305 if (temp_double != INVALID_VALUE)
306 *ret_voltage = temp_double / 1000.0;
307 }
308 }
310 CFRelease (bat_root_dict);
311 }
313 IOObjectRelease (iterator);
314 }
315 #endif /* HAVE_IOKIT_IOKITLIB_H */
317 #if KERNEL_LINUX
318 static int battery_read_acpi (const char *name)
319 {
320 double current = INVALID_VALUE;
321 double voltage = INVALID_VALUE;
322 double charge = INVALID_VALUE;
323 double *valptr = NULL;
324 int charging = 0;
326 char filename[256];
327 FILE *fh;
329 char buffer[1024];
330 char *fields[8];
331 int numfields;
332 char *endptr;
333 int len;
335 len = ssnprintf (filename, sizeof (filename), "%s/%s/state", battery_acpi_dir, name);
337 if ((len < 0) || ((unsigned int)len >= sizeof (filename)))
338 return -1;
340 if ((fh = fopen (filename, "r")) == NULL) {
341 char errbuf[1024];
342 ERROR ("Cannot open `%s': %s", filename,
343 sstrerror (errno, errbuf, sizeof (errbuf)));
344 return -1;
345 }
347 /*
348 * [11:00] <@tokkee> $ cat /proc/acpi/battery/BAT1/state
349 * [11:00] <@tokkee> present: yes
350 * [11:00] <@tokkee> capacity state: ok
351 * [11:00] <@tokkee> charging state: charging
352 * [11:00] <@tokkee> present rate: 1724 mA
353 * [11:00] <@tokkee> remaining capacity: 4136 mAh
354 * [11:00] <@tokkee> present voltage: 12428 mV
355 */
356 while (fgets (buffer, sizeof (buffer), fh) != NULL)
357 {
358 numfields = strsplit (buffer, fields, 8);
360 if (numfields < 3)
361 continue;
363 if ((strcmp (fields[0], "charging") == 0)
364 && (strcmp (fields[1], "state:") == 0))
365 {
366 if (strcmp (fields[2], "charging") == 0)
367 charging = 1;
368 else
369 charging = 0;
370 continue;
371 }
373 if ((strcmp (fields[0], "present") == 0)
374 && (strcmp (fields[1], "rate:") == 0))
375 valptr = ¤t;
376 else if ((strcmp (fields[0], "remaining") == 0)
377 && (strcmp (fields[1], "capacity:") == 0))
378 valptr = &charge;
379 else if ((strcmp (fields[0], "present") == 0)
380 && (strcmp (fields[1], "voltage:") == 0))
381 valptr = &voltage;
382 else
383 continue;
385 endptr = NULL;
386 errno = 0;
387 *valptr = strtod (fields[2], &endptr) / 1000.0;
389 if ((fields[2] == endptr) || (errno != 0))
390 *valptr = INVALID_VALUE;
391 } /* while (fgets (buffer, sizeof (buffer), fh) != NULL) */
393 fclose (fh);
395 if ((current != INVALID_VALUE) && (charging == 0))
396 current *= -1;
398 if (charge != INVALID_VALUE)
399 battery_submit ("0", "charge", charge);
400 if (current != INVALID_VALUE)
401 battery_submit ("0", "current", current);
402 if (voltage != INVALID_VALUE)
403 battery_submit ("0", "voltage", voltage);
405 return 0;
406 }
407 #endif /* KERNEL_LINUX */
410 static int battery_read (void)
411 {
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);
422 #endif
423 #if HAVE_IOKIT_IOKITLIB_H
424 get_via_generic_iokit (&charge_abs, ¤t, &voltage);
425 #endif
427 if ((charge_rel != INVALID_VALUE) && (charge_abs != INVALID_VALUE))
428 charge = charge_abs * charge_rel / 100.0;
430 if (charge != INVALID_VALUE)
431 battery_submit ("0", "charge", charge);
432 if (current != INVALID_VALUE)
433 battery_submit ("0", "current", current);
434 if (voltage != INVALID_VALUE)
435 battery_submit ("0", "voltage", voltage);
436 /* #endif HAVE_IOKIT_IOKITLIB_H || HAVE_IOKIT_PS_IOPOWERSOURCES_H */
438 #elif KERNEL_LINUX
439 FILE *fh;
440 char buffer[1024];
441 char filename[256];
443 char *fields[8];
444 int numfields;
446 int i;
447 int len;
449 for (i = 0; i < battery_pmu_num; i++)
450 {
451 char batnum_str[256];
452 double current = INVALID_VALUE;
453 double voltage = INVALID_VALUE;
454 double charge = INVALID_VALUE;
455 double *valptr = NULL;
457 len = ssnprintf (filename, sizeof (filename), battery_pmu_file, i);
458 if ((len < 0) || ((unsigned int)len >= sizeof (filename)))
459 continue;
461 len = ssnprintf (batnum_str, sizeof (batnum_str), "%i", i);
462 if ((len < 0) || ((unsigned int)len >= sizeof (batnum_str)))
463 continue;
465 if ((fh = fopen (filename, "r")) == NULL)
466 continue;
468 while (fgets (buffer, sizeof (buffer), fh) != NULL)
469 {
470 numfields = strsplit (buffer, fields, 8);
472 if (numfields < 3)
473 continue;
475 if (strcmp ("current", fields[0]) == 0)
476 valptr = ¤t;
477 else if (strcmp ("voltage", fields[0]) == 0)
478 valptr = &voltage;
479 else if (strcmp ("charge", fields[0]) == 0)
480 valptr = &charge;
481 else
482 valptr = NULL;
484 if (valptr != NULL)
485 {
486 char *endptr;
488 endptr = NULL;
489 errno = 0;
491 *valptr = strtod (fields[2], &endptr) / 1000.0;
493 if ((fields[2] == endptr) || (errno != 0))
494 *valptr = INVALID_VALUE;
495 }
496 }
498 fclose (fh);
499 fh = NULL;
501 if (charge != INVALID_VALUE)
502 battery_submit ("0", "charge", charge);
503 if (current != INVALID_VALUE)
504 battery_submit ("0", "current", current);
505 if (voltage != INVALID_VALUE)
506 battery_submit ("0", "voltage", voltage);
507 }
509 walk_directory (battery_acpi_dir, battery_read_acpi);
511 #endif /* KERNEL_LINUX */
513 return (0);
514 }
516 void module_register (void)
517 {
518 plugin_register_init ("battery", battery_init);
519 plugin_register_read ("battery", battery_read);
520 } /* void module_register */