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

Alexandre Derumier aderumier at odiso.com
Thu Sep 10 06:58:52 CEST 2015


/etc/pve/status.cfg
-------------------
influxdb:
      server 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/Graphite.pm  |  12 ++---
 PVE/Status/InfluxDB.pm  | 136 ++++++++++++++++++++++++++++++++++++++++++++++++
 PVE/Status/Makefile     |   1 +
 4 files changed, 145 insertions(+), 6 deletions(-)
 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/Graphite.pm b/PVE/Status/Graphite.pm
index 5f02754..b744968 100644
--- a/PVE/Status/Graphite.pm
+++ b/PVE/Status/Graphite.pm
@@ -6,7 +6,7 @@ use PVE::Status::Plugin;
 
 # example config (/etc/pve/status.cfg)
 #graphite:
-#	graphiteserver test
+#	server test
 #	port 2003
 #	path proxmox.mycluster
 #	disable 0
@@ -20,13 +20,13 @@ sub type {
 
 sub properties {
     return {
-	graphiteserver => {
+	server => {
 	    type => 'string', format => 'dns-name',
-	    description => "External graphite statistic server dns name",
+	    description => "server dns name",
 	},
 	port => {
             type => 'integer',
-	    description => "graphite server port",
+	    description => "network port",
 	},
 	path => {
             type => 'string', format => 'graphite-path',
@@ -37,7 +37,7 @@ sub properties {
 
 sub options {
     return {
-	graphiteserver => {},
+	server => {},
 	port => { optional => 1 },
 	path => { optional => 1 },
 	disable => { optional => 1 },
@@ -72,7 +72,7 @@ sub update_storage_status {
 sub write_graphite_hash {
     my ($plugin_config, $d, $ctime, $object) = @_;
 
-    my $host = $plugin_config->{graphiteserver};
+    my $host = $plugin_config->{server};
     my $port = $plugin_config->{port} ? $plugin_config->{port} : 2003;
     my $path = $plugin_config->{path} ? $plugin_config->{path} : 'proxmox';
 
diff --git a/PVE/Status/InfluxDB.pm b/PVE/Status/InfluxDB.pm
new file mode 100644
index 0000000..507f9b2
--- /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:
+#	server test
+#	port 8089
+#	disable 0
+#
+
+use base('PVE::Status::Plugin');
+
+sub type {
+    return 'influxdb';
+}
+
+sub options {
+    return {
+	server => {},
+	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->{server};
+    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