[pve-devel] [PATCH] add influxdb stats plugin

Alexandre Derumier aderumier at odiso.com
Tue Sep 8 08:27:46 CEST 2015


/etc/pve/status.cfg
-------------------
influxdb:
      graphiteserver influxdb3.odiso.net
      port 8089

This require influxdb >= 0.9 with udp enabled

influxdb.conf
-------------

[[udp]]
  enabled = true
  bind-address = "0.0.0.0:8089"
  database = "proxmox"
  batch-size = 1000
  batch-timeout = "1s"

Signed-off-by: Alexandre Derumier <aderumier at odiso.com>
---
 PVE/Service/pvestatd.pm |   2 +
 PVE/Status/InfluxDB.pm  | 136 ++++++++++++++++++++++++++++++++++++++++++++++++
 PVE/Status/Makefile     |   1 +
 3 files changed, 139 insertions(+)
 create mode 100644 PVE/Status/InfluxDB.pm

diff --git a/PVE/Service/pvestatd.pm b/PVE/Service/pvestatd.pm
index 486f24c..90add56 100755
--- a/PVE/Service/pvestatd.pm
+++ b/PVE/Service/pvestatd.pm
@@ -21,8 +21,10 @@ use PVE::AutoBalloon;
 
 use PVE::Status::Plugin;
 use PVE::Status::Graphite;
+use PVE::Status::InfluxDB;
 
 PVE::Status::Graphite->register();
+PVE::Status::InfluxDB->register();
 PVE::Status::Plugin->init();
 
 use base qw(PVE::Daemon);
diff --git a/PVE/Status/InfluxDB.pm b/PVE/Status/InfluxDB.pm
new file mode 100644
index 0000000..c94c4bb
--- /dev/null
+++ b/PVE/Status/InfluxDB.pm
@@ -0,0 +1,136 @@
+package PVE::Status::InfluxDB;
+
+use strict;
+use warnings;
+use PVE::Status::Plugin;
+use Data::Dumper;
+use PVE::SafeSyslog;
+
+# example config (/etc/pve/status.cfg)
+#influxdb:
+#	graphiteserver test
+#	port 8089
+#	disable 0
+#
+
+use base('PVE::Status::Plugin');
+
+sub type {
+    return 'influxdb';
+}
+
+sub options {
+    return {
+	graphiteserver => {},
+	port => {},
+	disable => { optional => 1 },
+   };
+}
+
+# Plugin implementation
+sub update_node_status {
+    my ($class, $plugin_config, $node, $data, $ctime) = @_;
+
+    $ctime *= 1000000000;
+
+    write_influxdb_hash($plugin_config, $data, $ctime, "object=nodes,host=$node");
+
+}
+
+sub update_qemu_status {
+    my ($class, $plugin_config, $vmid, $data, $ctime) = @_;
+
+    $ctime *= 1000000000;
+
+    my $object = "object=qemu,vmid=$vmid";
+    if($data->{name} && $data->{name} ne '') {
+	$object .= ",host=$data->{name}";
+    }
+    $object =~ s/\s/\\ /g;
+    write_influxdb_hash($plugin_config, $data, $ctime, $object);
+}
+
+sub update_lxc_status {
+    my ($class, $plugin_config, $vmid, $data, $ctime) = @_;
+
+    $ctime *= 1000000000;
+
+    my $object = "object=lxc,vmid=$vmid";
+    if($data->{name} && $data->{name} ne '') {
+	$object .= ",host=$data->{name}";
+    }
+    $object =~ s/\s/\\ /g;
+
+    write_influxdb_hash($plugin_config, $data, $ctime, $object);
+}
+
+sub update_storage_status {
+    my ($class, $plugin_config, $nodename, $storeid, $data, $ctime) = @_;
+
+    $ctime *= 1000000000;
+
+    my $object = "object=storages,nodename=$nodename,host=$storeid";
+    if($data->{type} && $data->{type} ne '') {
+	$object .= ",type=$data->{type}";
+    }
+    $object =~ s/\s/\\ /g;
+
+    write_influxdb_hash($plugin_config, $data, $ctime, $object);
+}
+
+sub write_influxdb_hash {
+    my ($plugin_config, $d, $ctime, $tags) = @_;
+
+    my $payload = {};
+
+    build_influxdb_payload($payload, $d, $ctime, $tags);
+
+    my $host = $plugin_config->{graphiteserver};
+    my $port = $plugin_config->{port};
+
+    my $socket = IO::Socket::IP->new(
+        PeerAddr    => $host,
+        PeerPort    => $port,
+        Proto       => 'udp',
+    );
+
+    $socket->send($payload->{string});
+    $socket->close() if $socket;
+
+}
+
+sub build_influxdb_payload {
+    my ($payload, $d, $ctime, $tags, $keyprefix, $depth) = @_;
+
+    $depth = 0 if !$depth;
+
+    for my $key (keys %$d) {
+
+        my $value = $d->{$key};
+        my $oldtags = $tags;
+
+        if ( defined $value ) {
+            if ( ref $value eq 'HASH' ) {
+
+		if($depth == 0) {
+		    $keyprefix = $key;
+		}elsif($depth == 1){
+		    $tags .= ",instance=$key";
+		}
+
+		$depth++;
+                build_influxdb_payload($payload, $value, $ctime, $tags, $keyprefix, $depth);
+		$depth--;
+
+            }elsif ($value =~ m/^\d+$/) {
+
+		$keyprefix = "system" if !$keyprefix && $depth == 0;
+
+                $payload->{string} .= $keyprefix."_"."$key,$tags value=$value $ctime\n";
+            }
+        }
+        $tags = $oldtags;
+    }
+}
+
+1;
diff --git a/PVE/Status/Makefile b/PVE/Status/Makefile
index 9d90aa0..8ff6902 100644
--- a/PVE/Status/Makefile
+++ b/PVE/Status/Makefile
@@ -2,6 +2,7 @@ include ../../defines.mk
 
 PERLSOURCE = 			\
 	Graphite.pm		\
+	InfluxDB.pm		\
 	Plugin.pm
 
 all:
-- 
2.1.4



More information about the pve-devel mailing list