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 rrd_update_file (host, battery_current_file, val,
88 ds_def_current, ds_num_current);
91 static void battery_voltage_write (char *host, char *inst, char *val)
93 rrd_update_file (host, battery_voltage_file, val,
94 ds_def_voltage, ds_num_voltage);
97 static void battery_charge_write (char *host, char *inst, char *val)
99 rrd_update_file (host, battery_charge_file, val,
100 ds_def_charge, ds_num_charge);
103 #if BATTERY_HAVE_READ
104 static void battery_submit (int batnum, double current, double voltage, double charge)
107 char buffer[BUFSIZE];
108 char batnum_str[BUFSIZE];
110 len = snprintf (batnum_str, BUFSIZE, "%i", batnum);
111 if ((len >= BUFSIZE) || (len < 0))
116 len = snprintf (buffer, BUFSIZE, "N:%.3f", current);
118 if ((len > 0) && (len < BUFSIZE))
119 plugin_submit ("battery_current", batnum_str, buffer);
124 len = snprintf (buffer, BUFSIZE, "N:%.3f", voltage);
126 if ((len > 0) && (len < BUFSIZE))
127 plugin_submit ("battery_voltage", batnum_str, buffer);
132 len = snprintf (buffer, BUFSIZE, "N:%.3f", charge);
134 if ((len > 0) && (len < BUFSIZE))
135 plugin_submit ("battery_charge", batnum_str, buffer);
139 static void battery_read (void)
143 char buffer[BUFSIZE];
144 char filename[BUFSIZE];
152 for (i = 0; i < battery_pmu_num; i++)
154 double current = 0.0;
155 double voltage = 0.0;
158 len = snprintf (filename, BUFSIZE, battery_pmu_file, i);
160 if ((len >= BUFSIZE) || (len < 0))
163 if ((fh = fopen (filename, "r")) == NULL)
166 while (fgets (buffer, BUFSIZE, fh) != NULL)
168 numfields = strsplit (buffer, fields, 8);
173 if (strcmp ("current", fields[0]) == 0)
174 current = atof (fields[2]) / 1000;
175 else if (strcmp ("voltage", fields[0]) == 0)
176 voltage = atof (fields[2]) / 1000;
177 else if (strcmp ("charge", fields[0]) == 0)
178 charge = atof (fields[2]) / 1000;
181 if ((current != 0.0) || (voltage != 0.0) || (charge != 0.0))
182 battery_submit (i, current, voltage, charge);
187 #endif /* KERNEL_LINUX */
190 # define battery_read NULL
191 #endif /* BATTERY_HAVE_READ */
193 void module_register (void)
195 plugin_register (MODULE_NAME, battery_init, battery_read, NULL);
196 plugin_register ("battery_current", NULL, NULL, battery_current_write);
197 plugin_register ("battery_voltage", NULL, NULL, battery_voltage_write);
198 plugin_register ("battery_charge", NULL, NULL, battery_charge_write);