]> git.ipfire.org Git - thirdparty/collectd.git/commitdiff
statefs_battery config
authorRinigus <rinigus.git@gmail.com>
Fri, 15 Jul 2016 07:03:58 +0000 (10:03 +0300)
committerRinigus <rinigus.git@gmail.com>
Sat, 16 Jul 2016 12:36:46 +0000 (15:36 +0300)
README
configure.ac
src/Makefile.am
src/collectd.conf.in
src/collectd.conf.pod
src/statefs_battery.c [new file with mode: 0644]
src/types.db

diff --git a/README b/README
index 1c3af7a4baa251640ac45a21a96cd04b7236b6a1..894c8626364752f18ae188804ce6fe3e432b6c1a 100644 (file)
--- a/README
+++ b/README
@@ -324,6 +324,9 @@ Features
       network devices such as switches, routers, thermometers, rack monitoring
       servers, etc. See collectd-snmp(5).
 
+    - statefs_battery
+      Battery statistics exposed via StateFS, as in Mer
+
     - statsd
       Acts as a StatsD server, reading values sent over the network from StatsD
       clients and calculating rates and other aggregates out of these values.
index 62de3ecd28b868891496fca77e90238d6b04a180..6fcbc77d800e75fa69122aa5876174969e2020bd 100644 (file)
@@ -5560,6 +5560,7 @@ plugin_processes="no"
 plugin_protocols="no"
 plugin_serial="no"
 plugin_smart="no"
+plugin_statefs_battery="no"
 plugin_swap="no"
 plugin_tape="no"
 plugin_tcpconns="no"
@@ -5619,6 +5620,11 @@ then
        then
                plugin_turbostat="yes"
        fi
+
+       if test "x$enable_statefs_battery" = "xyes"
+       then
+               plugin_statefs_battery="yes"
+       fi
 fi
 
 if test "x$ac_system" = "xOpenBSD"
@@ -6033,6 +6039,7 @@ AC_PLUGIN([serial],              [$plugin_serial],          [serial port traffic
 AC_PLUGIN([sigrok],              [$with_libsigrok],         [sigrok acquisition sources])
 AC_PLUGIN([smart],               [$plugin_smart],           [SMART statistics])
 AC_PLUGIN([snmp],                [$with_libnetsnmp],        [SNMP querying plugin])
+AC_PLUGIN([statefs_battery],     [$plugin_statefs_battery], [Battery statistics exposed via StateFS])
 AC_PLUGIN([statsd],              [yes],                     [StatsD plugin])
 AC_PLUGIN([swap],                [$plugin_swap],            [Swap usage statistics])
 AC_PLUGIN([syslog],              [$have_syslog],            [Syslog logging plugin])
@@ -6444,6 +6451,7 @@ Configuration:
     sigrok  . . . . . . . $enable_sigrok
     smart . . . . . . . . $enable_smart
     snmp  . . . . . . . . $enable_snmp
+    statefs_battery . . . $enable_statefs_battery
     statsd  . . . . . . . $enable_statsd
     swap  . . . . . . . . $enable_swap
     syslog  . . . . . . . $enable_syslog
index 641e2fae53cead1c386f4cbf37671d57f29f0156..234a857ce54a620292e97cf23949087c9ea1305b 100644 (file)
@@ -984,6 +984,12 @@ snmp_la_LIBADD += $(BUILD_WITH_LIBSNMP_LIBS)
 endif
 endif
 
+if BUILD_PLUGIN_STATEFS_BATTERY
+pkglib_LTLIBRARIES += statefs_battery.la
+statefs_battery_la_SOURCES = statefs_battery.c
+statefs_battery_la_LDFLAGS = $(PLUGIN_LDFLAGS)
+endif
+
 if BUILD_PLUGIN_STATSD
 pkglib_LTLIBRARIES += statsd.la
 statsd_la_SOURCES = statsd.c
index 54072d0fe63feec2a60f6711929c70cafda8c92e..a9ea209360590918c7654102e68d6db0c9692dc6 100644 (file)
 #@BUILD_PLUGIN_SIGROK_TRUE@LoadPlugin sigrok
 #@BUILD_PLUGIN_SMART_TRUE@LoadPlugin smart
 #@BUILD_PLUGIN_SNMP_TRUE@LoadPlugin snmp
+#@BUILD_PLUGIN_STATEFS_BATTERY_TRUE@LoadPlugin statefs_battery
 #@BUILD_PLUGIN_STATSD_TRUE@LoadPlugin statsd
 #@BUILD_PLUGIN_SWAP_TRUE@LoadPlugin swap
 #@BUILD_PLUGIN_TABLE_TRUE@LoadPlugin table
index 24dc59dbfd019c89e2359fb54ba7167d225ba7e8..2772f3db847007334aee0b2ee82dbbd3611a7b74 100644 (file)
@@ -6464,6 +6464,13 @@ Since the configuration of the C<snmp plugin> is a little more complicated than
 other plugins, its documentation has been moved to an own manpage,
 L<collectd-snmp(5)>. Please see there for details.
 
+=head2 Plugin C<statefs_battery>
+
+This plugin doesn't have any options. It reads statistics related to
+battery performance exposed by StateFS at /run/state. StateFS is used
+in Mer-based Sailfish OS, for example.
+
+
 =head2 Plugin C<statsd>
 
 The I<statsd plugin> listens to a UDP socket, reads "events" in the statsd
diff --git a/src/statefs_battery.c b/src/statefs_battery.c
new file mode 100644 (file)
index 0000000..2a2589a
--- /dev/null
@@ -0,0 +1,144 @@
+/**
+ * collectd - src/statefs_battery.c
+ * Copyright (C) 2016 rinigus
+ *
+ *
+The MIT License (MIT)
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
+
+ * Authors:
+ *   rinigus <http://github.com/rinigus>
+
+ Battery stats are collected from statefs Battery namespace. Reported
+ units are as follows:
+ capacity %
+ charge %
+ current A
+ energy Wh
+ power W
+ temperature C
+ timefull and timelow seconds
+ voltage V
+
+ Provider at
+ https://git.merproject.org/mer-core/statefs-providers/blob/master/src/power_udev/provider_power_udev.cpp
+ **/
+
+#include "collectd.h"
+#include "common.h"
+#include "plugin.h"
+
+#include <stdio.h>
+
+#define STATEFS_ROOT "/run/state/namespaces/Battery/"
+#define BFSZ 512
+
+static int submitted_this_run = 0;
+
+static void battery_submit (char const *type, gauge_t value)
+{
+  value_t values[1];
+  value_list_t vl = VALUE_LIST_INIT;
+  
+  values[0].gauge = value;
+  
+  vl.values = values;
+  vl.values_len = 1;
+  sstrncpy (vl.host, hostname_g, sizeof (vl.host));
+  sstrncpy (vl.plugin, "statefs_battery", sizeof (vl.plugin));
+  sstrncpy (vl.type, type, sizeof (vl.type));
+  
+  plugin_dispatch_values (&vl);
+
+  submitted_this_run++;
+}
+
+
+static _Bool getvalue(char const *fname, gauge_t *value, char *buffer, size_t buffer_size)
+{
+  FILE *fh;
+  if ((fh = fopen (fname, "r")) == NULL)
+    return 0;
+
+  if ( fgets( buffer, buffer_size, fh ) == NULL )
+    {
+      fclose( fh );
+      return 0; // empty file
+    }
+
+  (*value) = atof( buffer );
+
+  fclose(fh);
+
+  return 1;
+}
+
+
+static int battery_read (void)
+{
+  char buffer[BFSZ];
+  gauge_t value = NAN;
+  
+  submitted_this_run = 0;
+  
+
+  if ( getvalue( STATEFS_ROOT "ChargePercentage", &value, buffer, BFSZ ) )
+    battery_submit( "charge", value );
+  // Use capacity as a charge estimate if ChargePercentage is not available
+  else if ( getvalue( STATEFS_ROOT "Capacity", &value, buffer, BFSZ ) )
+    battery_submit( "charge", value );
+
+  if ( getvalue( STATEFS_ROOT "Current", &value, buffer, BFSZ ) )
+    battery_submit( "current", value * 1e-6 ); // from uA to A
+
+  if ( getvalue( STATEFS_ROOT "Energy", &value, buffer, BFSZ ) )
+    battery_submit( "energy", value * 1e-6 ); // from uWh to Wh
+
+  if ( getvalue( STATEFS_ROOT "Power", &value, buffer, BFSZ ) )
+    battery_submit( "power_battery", value * 1e-6 ); // from uW to W
+  
+  if ( getvalue( STATEFS_ROOT "Temperature", &value, buffer, BFSZ ) )
+    battery_submit( "temperature", value * 0.1 ); // from 10xC to C
+  
+  if ( getvalue( STATEFS_ROOT "TimeUntilFull", &value, buffer, BFSZ ) )
+    battery_submit( "timefull", value );
+
+  if ( getvalue( STATEFS_ROOT "TimeUntilLow", &value, buffer, BFSZ ) )
+    battery_submit( "timelow", value );
+  
+  if ( getvalue( STATEFS_ROOT "Voltage", &value, buffer, BFSZ ) )
+    battery_submit( "voltage", value * 1e-6 ); // from uV to V
+
+  if ( submitted_this_run == 0 )
+    {
+      ERROR ("statefs_battery plugin: none of the statistics are available.");
+      return (-1);
+    }
+  
+  return 0;
+}
+
+void module_register (void)
+{
+  plugin_register_read ("statefs_battery", battery_read);
+} /* void module_register */
index e328a46abefce4f707a8ffd864880127aa8e6352..158dd920d94634d049f7e4738ed94c24b844d016 100644 (file)
@@ -73,6 +73,7 @@ duration                seconds:GAUGE:0:U
 email_check             value:GAUGE:0:U
 email_count             value:GAUGE:0:U
 email_size              value:GAUGE:0:U
+energy                 value:GAUGE:U:U
 entropy                 value:GAUGE:0:4294967295
 evicted_keys            value:DERIVE:0:U
 expired_keys            value:DERIVE:0:U
@@ -171,6 +172,7 @@ ping_droprate           value:GAUGE:0:100
 ping_stddev             value:GAUGE:0:65535
 players                 value:GAUGE:0:1000000
 power                   value:GAUGE:0:U
+power_battery          value:GAUGE:U:U
 pressure                value:GAUGE:0:U
 protocol_counter        value:DERIVE:0:U
 ps_code                 value:GAUGE:0:9223372036854775807
@@ -214,6 +216,8 @@ swap_io                 value:DERIVE:0:U
 tcp_connections         value:GAUGE:0:4294967295
 temperature             value:GAUGE:U:U
 threads                 value:GAUGE:0:U
+timefull               value:GAUGE:0:U
+timelow                        value:GAUGE:0:U
 time_dispersion         value:GAUGE:-1000000:1000000
 time_offset             value:GAUGE:-1000000:1000000
 time_offset_ntp         value:GAUGE:-1000000:1000000