]> git.proxmox.com Git - pve-manager.git/blobdiff - PVE/Status/Graphite.pm
ui: fix align mode of two column container
[pve-manager.git] / PVE / Status / Graphite.pm
index 9cde5a93c996f9a8cd3804d9280b0bf05239f4ef..1b196c2418f0796763fe8354919e6b0bf6c07468 100644 (file)
@@ -3,8 +3,11 @@ package PVE::Status::Graphite;
 use strict;
 use warnings;
 
+use IO::Socket::IP;
+use Socket qw(SOL_SOCKET SO_SNDTIMEO SO_RCVTIMEO);
+
 use PVE::Status::Plugin;
-use IO::Socket::Timeout;
+use PVE::JSONSchema;
 
 # example config (/etc/pve/status.cfg)
 #graphite:
@@ -29,13 +32,15 @@ sub properties {
        },
        timeout => {
            type => 'integer',
-           description => "graphite tcp socket timeout (default=3)",
+           description => "graphite TCP socket timeout (default=1)",
+           minimum => 0,
+           default => 1,
            optional => 1
        },
        proto => {
            type => 'string',
            enum => ['udp', 'tcp'],
-           description => "send graphite data using tcp or udp (default)",
+           description => "Protocol to send graphite data. TCP or UDP (default)",
            optional => 1,
        },
     };
@@ -45,6 +50,7 @@ sub options {
     return {
        server => {},
        port => { optional => 1 },
+       mtu => { optional => 1 },
        proto => { optional => 1 },
        timeout => { optional => 1 },
        path => { optional => 1 },
@@ -52,47 +58,48 @@ sub options {
     };
 }
 
-# we do not want boolean/state information to export to graphite
-my $key_blacklist = {
-    'template' => 1,
-    'pid' => 1,
-    'agent' => 1,
-    'serial' => 1,
-};
-
 # Plugin implementation
 sub update_node_status {
-    my ($class, $plugin_config, $node, $data, $ctime) = @_;
+    my ($class, $txn, $node, $data, $ctime) = @_;
 
-    write_graphite_hash($plugin_config, $data, $ctime, "nodes.$node");
+    return assemble($class, $txn, $data, $ctime, "nodes.$node");
 
 }
 
 sub update_qemu_status {
-    my ($class, $plugin_config, $vmid, $data, $ctime, $nodename) = @_;
-    write_graphite_hash($plugin_config, $data, $ctime, "qemu.$vmid");
+    my ($class, $txn, $vmid, $data, $ctime, $nodename) = @_;
+
+    return assemble($class, $txn, $data, $ctime, "qemu.$vmid");
 }
 
 sub update_lxc_status {
-    my ($class, $plugin_config, $vmid, $data, $ctime, $nodename) = @_;
+    my ($class, $txn, $vmid, $data, $ctime, $nodename) = @_;
 
-    write_graphite_hash($plugin_config, $data, $ctime, "lxc.$vmid");
+    return assemble($class, $txn, $data, $ctime, "lxc.$vmid");
 }
 
 sub update_storage_status {
-    my ($class, $plugin_config, $nodename, $storeid, $data, $ctime) = @_;
+    my ($class, $txn, $nodename, $storeid, $data, $ctime) = @_;
 
-    write_graphite_hash($plugin_config, $data, $ctime, "storages.$nodename.$storeid");
+    return assemble($class, $txn, $data, $ctime, "storages.$nodename.$storeid");
 }
 
-sub write_graphite_hash {
-    my ($plugin_config, $d, $ctime, $object) = @_;
+sub _send_batch_size {
+    my ($class, $cfg) = @_;
+    my $proto = $cfg->{proto} || 'udp';
+    if ($proto eq 'tcp') {
+       return 56000;
+    }
+    return $class->SUPER::_send_batch_size($cfg);
+}
+
+sub _connect {
+    my ($class, $cfg) = @_;
 
-    my $host = $plugin_config->{server};
-    my $port = $plugin_config->{port} || 2003;
-    my $path = $plugin_config->{path} // 'proxmox';
-    my $proto = $plugin_config->{proto} || 'udp';
-    my $timeout = $plugin_config->{timeout} // 3;
+    my $host    = $cfg->{server};
+    my $port    = $cfg->{port} || 2003;
+    my $proto   = $cfg->{proto} || 'udp';
+    my $timeout = $cfg->{timeout} // 1;
 
     my $carbon_socket = IO::Socket::IP->new(
        PeerAddr    => $host,
@@ -101,39 +108,51 @@ sub write_graphite_hash {
        Timeout     => $timeout,
     ) || die "couldn't create carbon socket [$host]:$port - $@\n";
 
-    if ( $proto eq 'tcp' ) {
-       IO::Socket::Timeout->enable_timeouts_on($carbon_socket);
-       $carbon_socket->read_timeout($timeout);
-       $carbon_socket->write_timeout($timeout);
+    if ($proto eq 'tcp') {
+       # seconds and µs
+       my $timeout_struct = pack( 'l!l!', $timeout, 0);
+       setsockopt($carbon_socket, SOL_SOCKET, SO_SNDTIMEO, $timeout_struct);
+       setsockopt($carbon_socket, SOL_SOCKET, SO_RCVTIMEO, $timeout_struct);
     }
-    write_graphite($carbon_socket, $d, $ctime, $path.".$object");
-
-    $carbon_socket->close() if $carbon_socket;
 
+    return $carbon_socket;
 }
 
-sub write_graphite {
-    my ($carbon_socket, $d, $ctime, $path) = @_;
+sub assemble {
+    my ($class, $txn, $data, $ctime, $object) = @_;
 
-    for my $key (keys %$d) {
+    my $path = $txn->{cfg}->{path} // 'proxmox';
+    $path .= ".$object";
 
-       my $value = $d->{$key};
-       my $oldpath = $path;
-       $key =~ s/\./-/g;
-       $path .= ".$key";
+    # we do not want boolean/state information to export to graphite
+    my $key_blacklist = {
+       'template' => 1,
+       'pid' => 1,
+       'agent' => 1,
+       'serial' => 1,
+    };
+
+    my $assemble_graphite_data;
+    $assemble_graphite_data = sub {
+       my ($metric, $path) = @_;
+
+       for my $key (sort keys %$metric) {
+           my $value = $metric->{$key};
+           next if !defined($value);
+
+           $key =~ s/\./-/g;
+           my $metricpath = $path . ".$key";
 
-       if ( defined $value ) {
-           if ( ref $value eq 'HASH' ) {
-               write_graphite($carbon_socket, $value, $ctime, $path);
-           } elsif ($value =~ m/^[+-]?[0-9]*\.?[0-9]+$/ &&
-               !$key_blacklist->{$key}) {
-               $carbon_socket->send( "$path $value $ctime\n" );
-           } else {
-               # do not send blacklisted or non-numeric values
+           if (ref($value) eq 'HASH') {
+               $assemble_graphite_data->($value, $metricpath);
+           } elsif ($value =~ m/^[+-]?[0-9]*\.?[0-9]+$/ && !$key_blacklist->{$key}) {
+               $class->add_metric_data($txn, "$metricpath $value $ctime\n");
            }
        }
-       $path = $oldpath;
-    }
+    };
+    $assemble_graphite_data->($data, $path);
+
+    $assemble_graphite_data = undef; # avoid cyclic reference!
 }
 
 PVE::JSONSchema::register_format('graphite-path', \&pve_verify_graphite_path);