[prev in list] [next in list] [prev in thread] [next in thread] 

List:       collectd
Subject:    [collectd] battery.c patch
From:       bd <bd () bc-bd ! org>
Date:       2007-02-27 13:59:25
Message-ID: 20070227135924.GE14794 () bc-bd ! org
[Download RAW message or body]

Hello,

I tried writing a patch for battery.c (also collect information about
curent capacity and design capacity) but the rrd files are not created.

Perhaps someone can point me towards the error I made.

thanks in advance & by
	Stefan

PS: Please CC me as I am not on the list
-- 
BOFH excuse #142:

new guy cross-connected phone lines with ac power bus.

["battery.diff" (text/x-diff)]

diff --git a/src/battery.c b/src/battery.c
index 2141a58..56e3a9f 100644
--- a/src/battery.c
+++ b/src/battery.c
@@ -64,6 +64,8 @@
 static char *battery_current_file = "battery-%s/current.rrd";
 static char *battery_voltage_file = "battery-%s/voltage.rrd";
 static char *battery_charge_file  = "battery-%s/charge.rrd";
+static char *battery_lastfull_file  = "battery-%s/lastfull.rrd";
+static char *battery_designed_file  = "battery-%s/designed.rrd";
 
 static char *ds_def_current[] =
 {
@@ -86,6 +88,20 @@ static char *ds_def_charge[] =
 };
 static int ds_num_charge = 1;
 
+static char *ds_def_lastfull[] =
+{
+	"DS:lastfull:GAUGE:"COLLECTD_HEARTBEAT":0:U",
+	NULL
+};
+static int ds_num_lastfull = 1;
+
+static char *ds_def_designed[] =
+{
+	"DS:design:GAUGE:"COLLECTD_HEARTBEAT":0:U",
+	NULL
+};
+static int ds_num_designed = 1;
+
 #if HAVE_IOKIT_IOKITLIB_H || HAVE_IOKIT_PS_IOPOWERSOURCES_H
 	/* No global variables */
 /* #endif HAVE_IOKIT_IOKITLIB_H || HAVE_IOKIT_PS_IOPOWERSOURCES_H */
@@ -159,8 +175,35 @@ static void battery_charge_write (char *host, char *inst, char *val)
 			ds_def_charge, ds_num_charge);
 }
 
+static void battery_lastfull_write (char *host, char *inst, char *val)
+{
+	char filename[BUFSIZE];
+	int len;
+
+	len = snprintf (filename, BUFSIZE, battery_lastfull_file, inst);
+	if ((len >= BUFSIZE) || (len < 0))
+		return;
+
+	rrd_update_file (host, filename, val,
+			ds_def_lastfull, ds_num_lastfull);
+}
+
+static void battery_designed_write (char *host, char *inst, char *val)
+{
+	char filename[BUFSIZE];
+	int len;
+
+	len = snprintf (filename, BUFSIZE, battery_designed_file, inst);
+	if ((len >= BUFSIZE) || (len < 0))
+		return;
+
+	rrd_update_file (host, filename, val,
+			ds_def_designed, ds_num_designed);
+}
+
 #if BATTERY_HAVE_READ
-static void battery_submit (char *inst, double current, double voltage, double charge)
+static void battery_submit (char *inst, double current, double voltage, double charge,
+	double lastfull, double designed)
 {
 	int len;
 	char buffer[BUFSIZE];
@@ -200,6 +243,30 @@ static void battery_submit (char *inst, double current, double voltage, double c
 	{
 		plugin_submit ("battery_charge", inst, "N:U");
 	}
+
+	if (lastfull != INVALID_VALUE)
+	{
+		len = snprintf (buffer, BUFSIZE, "N:%.3f", lastfull);
+
+		if ((len > 0) && (len < BUFSIZE))
+			plugin_submit ("battery_lastfull", inst, buffer);
+	}
+	else
+	{
+		plugin_submit ("battery_lastfull", inst, "N:U");
+	}
+
+	if (designed != INVALID_VALUE)
+	{
+		len = snprintf (buffer, BUFSIZE, "N:%.3f", designed);
+
+		if ((len > 0) && (len < BUFSIZE))
+			plugin_submit ("battery_designed", inst, buffer);
+	}
+	else
+	{
+		plugin_submit ("battery_designed", inst, "N:U");
+	}
 }
 
 #if HAVE_IOKIT_PS_IOPOWERSOURCES_H || HAVE_IOKIT_IOKITLIB_H
@@ -452,6 +519,7 @@ static void battery_read (void)
 		double  current = INVALID_VALUE;
 		double  voltage = INVALID_VALUE;
 		double  charge  = INVALID_VALUE;
+
 		double *valptr = NULL;
 
 		len = snprintf (filename, BUFSIZE, battery_pmu_file, i);
@@ -495,10 +563,12 @@ static void battery_read (void)
 			}
 		}
 
+		// FIXME remove INVALID_VALUE in call to battery_submit()
 		if ((current != INVALID_VALUE)
 				|| (voltage != INVALID_VALUE)
 				|| (charge  != INVALID_VALUE))
-			battery_submit (batnum_str, current, voltage, charge);
+			battery_submit (batnum_str, current, voltage, charge,
+				INVALID_VALUE, INVALID_VALUE);
 
 		fclose (fh);
 		fh = NULL;
@@ -509,6 +579,8 @@ static void battery_read (void)
 		double  current = INVALID_VALUE;
 		double  voltage = INVALID_VALUE;
 		double  charge  = INVALID_VALUE;
+		double  designed = INVALID_VALUE;
+		double  lastfull = INVALID_VALUE;
 		double *valptr = NULL;
 		int charging = 0;
 
@@ -587,15 +659,54 @@ static void battery_read (void)
 				}
 			}
 
+			fclose (fh);
+
+			len = snprintf (filename, BUFSIZE, "/proc/acpi/battery/%s/info", ent->d_name);
+			if ((len >= BUFSIZE) || (len < 0))
+				continue;
+
+			if ((fh = fopen (filename, "r")) == NULL)
+			{
+				syslog (LOG_ERR, "Cannot open `%s': %s", filename, strerror (errno));
+				continue;
+			}
+
+			/* $ cat /proc/acpi/battery/BAT0/info
+			 * present:                 yes
+			 * design capacity:         71280 mWh
+			 * last full capacity:      57630 mWh
+			 * [...]
+			 * */
+			while (fgets (buffer, BUFSIZE, fh) != NULL)
+			{
+				char *endptr = NULL;
+
+				// FIXME invalidate on conversion error
+				numfields = strsplit (buffer, fields, 8);
+
+				if (numfields < 3)
+					continue;
+
+				if ((strcmp (fields[0], "design") == 0)
+						&& (strcmp (fields[1], "capacity:") == 0))
+					designed = strtod(fields[2], &endptr) / 1000.0;
+				else if ((strcmp (fields[0], "last") == 0)
+						&& (strcmp (fields[1], "full") == 0)
+						&& (strcmp (fields[2], "capacity:") == 0))
+					lastfull = strtod(fields[3], &endptr) / 1000.0;
+			}
+
+			fclose (fh);
+
 			if ((current != INVALID_VALUE) && (charging == 0))
 					current *= -1;
 
-			if ((current != INVALID_VALUE)
+				// FIXME remove INVALID_VALUE in call to battery_submit()
+				if ((current != INVALID_VALUE)
 					|| (voltage != INVALID_VALUE)
 					|| (charge  != INVALID_VALUE))
-				battery_submit (ent->d_name, current, voltage, charge);
-
-			fclose (fh);
+				battery_submit (ent->d_name, current, voltage, charge,
+					lastfull, designed);
 		}
 
 		closedir (dh);
@@ -612,6 +723,8 @@ void module_register (void)
 	plugin_register ("battery_current", NULL, NULL, battery_current_write);
 	plugin_register ("battery_voltage", NULL, NULL, battery_voltage_write);
 	plugin_register ("battery_charge",  NULL, NULL, battery_charge_write);
+	plugin_register ("battery_designed",  NULL, NULL, battery_designed_write);
+	plugin_register ("battery_lastfull",  NULL, NULL, battery_lastfull_write);
 }
 
 #undef BUFSIZE


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic