Commit c7f3e2ab authored by Alexandre Derumier's avatar Alexandre Derumier Committed by Dietmar Maurer

add influxdb stats plugin V2

/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: 's avatarAlexandre Derumier <aderumier@odiso.com>
parent 9815097f
...@@ -21,8 +21,10 @@ use PVE::AutoBalloon; ...@@ -21,8 +21,10 @@ use PVE::AutoBalloon;
use PVE::Status::Plugin; use PVE::Status::Plugin;
use PVE::Status::Graphite; use PVE::Status::Graphite;
use PVE::Status::InfluxDB;
PVE::Status::Graphite->register(); PVE::Status::Graphite->register();
PVE::Status::InfluxDB->register();
PVE::Status::Plugin->init(); PVE::Status::Plugin->init();
use base qw(PVE::Daemon); use base qw(PVE::Daemon);
......
...@@ -6,7 +6,7 @@ use PVE::Status::Plugin; ...@@ -6,7 +6,7 @@ use PVE::Status::Plugin;
# example config (/etc/pve/status.cfg) # example config (/etc/pve/status.cfg)
#graphite: #graphite:
# graphiteserver test # server test
# port 2003 # port 2003
# path proxmox.mycluster # path proxmox.mycluster
# disable 0 # disable 0
...@@ -20,13 +20,13 @@ sub type { ...@@ -20,13 +20,13 @@ sub type {
sub properties { sub properties {
return { return {
graphiteserver => { server => {
type => 'string', format => 'dns-name', type => 'string', format => 'dns-name',
description => "External graphite statistic server dns name", description => "server dns name",
}, },
port => { port => {
type => 'integer', type => 'integer',
description => "graphite server port", description => "network port",
}, },
path => { path => {
type => 'string', format => 'graphite-path', type => 'string', format => 'graphite-path',
...@@ -37,7 +37,7 @@ sub properties { ...@@ -37,7 +37,7 @@ sub properties {
sub options { sub options {
return { return {
graphiteserver => {}, server => {},
port => { optional => 1 }, port => { optional => 1 },
path => { optional => 1 }, path => { optional => 1 },
disable => { optional => 1 }, disable => { optional => 1 },
...@@ -72,7 +72,7 @@ sub update_storage_status { ...@@ -72,7 +72,7 @@ sub update_storage_status {
sub write_graphite_hash { sub write_graphite_hash {
my ($plugin_config, $d, $ctime, $object) = @_; 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 $port = $plugin_config->{port} ? $plugin_config->{port} : 2003;
my $path = $plugin_config->{path} ? $plugin_config->{path} : 'proxmox'; my $path = $plugin_config->{path} ? $plugin_config->{path} : 'proxmox';
......
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;
...@@ -2,6 +2,7 @@ include ../../defines.mk ...@@ -2,6 +2,7 @@ include ../../defines.mk
PERLSOURCE = \ PERLSOURCE = \
Graphite.pm \ Graphite.pm \
InfluxDB.pm \
Plugin.pm Plugin.pm
all: all:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment