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>
27 #define MODULE_NAME "battery"
30 #if defined(KERNEL_LINUX)
31 # define BATTERY_HAVE_READ 1
33 # define BATTERY_HAVE_READ 0
36 static char *battery_current_file = "battery-%s/current.rrd";
37 static char *battery_voltage_file = "battery-%s/voltage.rrd";
38 static char *battery_charge_file = "battery-%s/charge.rrd";
40 static char *ds_def_current[] =
42 "DS:current:GAUGE:25:0:U",
45 static int ds_num_current = 1;
47 static char *ds_def_voltage[] =
49 "DS:voltage:GAUGE:25:0:U",
52 static int ds_num_voltage = 1;
54 static char *ds_def_charge[] =
56 "DS:charge:GAUGE:25:0:U",
59 static int ds_num_charge = 1;
61 static int battery_pmu_num = 0;
62 static char *battery_pmu_file = "/proc/pmu/battery_%i";
64 static void battery_init (void)
68 char filename[BUFSIZE];
70 for (battery_pmu_num = 0; ; battery_pmu_num++)
72 len = snprintf (filename, BUFSIZE, battery_pmu_file, battery_pmu_num);
74 if ((len >= BUFSIZE) || (len < 0))
77 if (access (filename, R_OK))
85 static void battery_current_write (char *host, char *inst, char *val)
87 char filename[BUFSIZE];
90 len = snprintf (filename, BUFSIZE, battery_current_file, inst);
91 if ((len >= BUFSIZE) || (len < 0))
94 rrd_update_file (host, filename, val,
95 ds_def_current, ds_num_current);
98 static void battery_voltage_write (char *host, char *inst, char *val)
100 char filename[BUFSIZE];
103 len = snprintf (filename, BUFSIZE, battery_voltage_file, inst);
104 if ((len >= BUFSIZE) || (len < 0))
107 rrd_update_file (host, filename, val,
108 ds_def_voltage, ds_num_voltage);
111 static void battery_charge_write (char *host, char *inst, char *val)
113 char filename[BUFSIZE];
116 len = snprintf (filename, BUFSIZE, battery_charge_file, inst);
117 if ((len >= BUFSIZE) || (len < 0))
120 rrd_update_file (host, filename, val,
121 ds_def_charge, ds_num_charge);
124 #if BATTERY_HAVE_READ
125 static void battery_submit (int batnum, double current, double voltage, double charge)
128 char buffer[BUFSIZE];
129 char batnum_str[BUFSIZE];
131 len = snprintf (batnum_str, BUFSIZE, "%i", batnum);
132 if ((len >= BUFSIZE) || (len < 0))
137 len = snprintf (buffer, BUFSIZE, "N:%.3f", current);
139 if ((len > 0) && (len < BUFSIZE))
140 plugin_submit ("battery_current", batnum_str, buffer);
145 len = snprintf (buffer, BUFSIZE, "N:%.3f", voltage);
147 if ((len > 0) && (len < BUFSIZE))
148 plugin_submit ("battery_voltage", batnum_str, buffer);
153 len = snprintf (buffer, BUFSIZE, "N:%.3f", charge);
155 if ((len > 0) && (len < BUFSIZE))
156 plugin_submit ("battery_charge", batnum_str, buffer);
160 static void battery_read (void)
164 char buffer[BUFSIZE];
165 char filename[BUFSIZE];
173 for (i = 0; i < battery_pmu_num; i++)
175 double current = 0.0;
176 double voltage = 0.0;
179 len = snprintf (filename, BUFSIZE, battery_pmu_file, i);
181 if ((len >= BUFSIZE) || (len < 0))
184 if ((fh = fopen (filename, "r")) == NULL)
187 while (fgets (buffer, BUFSIZE, fh) != NULL)
189 numfields = strsplit (buffer, fields, 8);
194 if (strcmp ("current", fields[0]) == 0)
195 current = atof (fields[2]) / 1000;
196 else if (strcmp ("voltage", fields[0]) == 0)
197 voltage = atof (fields[2]) / 1000;
198 else if (strcmp ("charge", fields[0]) == 0)
199 charge = atof (fields[2]) / 1000;
202 if ((current != 0.0) || (voltage != 0.0) || (charge != 0.0))
203 battery_submit (i, current, voltage, charge);
209 if (access ("/proc/acpi/battery", R_OK | X_OK) == 0)
212 * [11:00] <@tokkee> $ cat /proc/acpi/battery/BAT1/state
213 * [11:00] <@tokkee> present: yes
214 * [11:00] <@tokkee> capacity state: ok
215 * [11:00] <@tokkee> charging state: charging
216 * [11:00] <@tokkee> present rate: 1724 mA
217 * [11:00] <@tokkee> remaining capacity: 4136 mAh
218 * [11:00] <@tokkee> present voltage: 12428 mV
220 syslog (LOG_DEBUG, "Found directory `/proc/acpi/battery'");
222 #endif /* KERNEL_LINUX */
225 # define battery_read NULL
226 #endif /* BATTERY_HAVE_READ */
228 void module_register (void)
230 plugin_register (MODULE_NAME, battery_init, battery_read, NULL);
231 plugin_register ("battery_current", NULL, NULL, battery_current_write);
232 plugin_register ("battery_voltage", NULL, NULL, battery_voltage_write);
233 plugin_register ("battery_charge", NULL, NULL, battery_charge_write);