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

2015-09-11 Thread Dietmar Maurer

applied


___
pve-devel mailing list
pve-devel@pve.proxmox.com
http://pve.proxmox.com/cgi-bin/mailman/listinfo/pve-devel


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

2015-09-09 Thread Alexandre Derumier
/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 
---
 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 000..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 *= 10;
+
+write_influxdb_hash($plugin_config, $data, $ctime, 
"object=nodes,host=$node");
+
+}
+
+sub update_qemu_status {
+my ($class, $plugin_config, $vmid, $data, $ctime) = @_;
+
+$ctime *= 10;
+
+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 *= 10;
+
+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 *= 10;
+
+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 {
+