[pve-devel] [PATCH] pvestatd: add support for external graphite stats server

Alexandre Derumier aderumier at odiso.com
Mon Jun 15 08:00:29 CEST 2015


Signed-off-by: Alexandre Derumier <aderumier at odiso.com>
---
 bin/pvestatd | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 58 insertions(+), 5 deletions(-)

diff --git a/bin/pvestatd b/bin/pvestatd
index 6e66207..0114c82 100755
--- a/bin/pvestatd
+++ b/bin/pvestatd
@@ -17,8 +17,8 @@ use PVE::LXC;
 use PVE::RPCEnvironment;
 use PVE::API2::Subscription;
 use PVE::AutoBalloon;
-
 use base qw(PVE::Daemon);
+use IO::Socket::IP;
 
 my $opt_debug;
 
@@ -149,6 +149,7 @@ sub auto_balloning {
 }
 
 sub update_qemu_status {
+    my ($datacenter) = @_;
 
     my $ctime = time();
 
@@ -176,7 +177,9 @@ sub update_qemu_status {
 		":::";
 	}
 	PVE::Cluster::broadcast_rrd("pve2.3-vm/$vmid", $data);
+	write_graphite_hash($datacenter, $d, $ctime, "vms.$vmid");
     }
+
 }
 
 sub remove_stale_lxc_consoles {
@@ -194,6 +197,7 @@ sub remove_stale_lxc_consoles {
 }
 
 sub update_lxc_status {
+    my ($datacenter) = @_;
 
     my $ctime = time();
 
@@ -215,10 +219,13 @@ sub update_lxc_status {
 		":::";
 	}
 	PVE::Cluster::broadcast_rrd("pve2.3-vm/$vmid", $data);
+	write_graphite_hash($datacenter, $d, $ctime, "vms.$vmid");
+
     }
 }
 
 sub update_storage_status {
+    my ($datacenter) = @_;
 
     my $cfg = cfs_read_file("storage.cfg");
 
@@ -237,11 +244,14 @@ sub update_storage_status {
 
 	my $key = "pve2-storage/${nodename}/$storeid";
 	PVE::Cluster::broadcast_rrd($key, $data);
+	write_graphite_hash($datacenter, $d, $ctime, "storages.$nodename.$storeid");
+
     }
 }
 
 sub update_status {
 
+    my $datacenter = cfs_read_file('datacenter.cfg');
     # update worker list. This is not really required and
     # we just call this to make sure that we have a correct
     # list in case of an unexpected crash.
@@ -253,25 +263,25 @@ sub update_status {
     syslog('err', $err) if $err;
 
     eval {
-	update_node_status();
+	update_node_status($datacenter);
     };
     $err = $@;
     syslog('err', "node status update error: $err") if $err;
 
     eval {
-	update_qemu_status();
+	update_qemu_status($datacenter);
     };
     $err = $@;
     syslog('err', "qemu status update error: $err") if $err;
 
     eval {
-	update_lxc_status();
+	update_lxc_status($datacenter);
     };
     $err = $@;
     syslog('err', "lxc status update error: $err") if $err;
 
     eval {
-	update_storage_status();
+	update_storage_status($datacenter);
     };
     $err = $@;
     syslog('err', "storage status update error: $err") if $err;
@@ -343,6 +353,49 @@ sub run {
     }
 }
 
+
+sub write_graphite_hash {
+    my ($datacenter, $d, $ctime, $object) = @_;
+
+    return if !$datacenter->{graphiteserver};
+
+    my ($host, $port, $path) = split(/:/, $datacenter->{graphiteserver});
+
+    return if !$host || !$port || !$path;
+    return if $port !~ m/(\d+)/;
+
+    my $carbon_socket = IO::Socket::IP->new(
+	PeerAddr    => $host,
+	PeerPort    => $port,
+	Proto       => 'udp',
+    );
+
+    write_graphite($carbon_socket, $d, $ctime, $path.".$object");
+
+    $carbon_socket->close() if $carbon_socket;
+
+}
+
+sub write_graphite {
+    my ($carbon_socket, $d, $ctime, $path) = @_;
+
+    for my $key (keys %$d) {
+
+        my $value = $d->{$key};
+	my $oldpath = $path;
+	$path .= ".$key";
+
+	if ( defined $value ) {
+	    if ( ref $value eq 'HASH' ) {
+		write_graphite($carbon_socket, $value, $ctime, $path);
+	    }else {
+		$carbon_socket->send( "$path $value $ctime" );
+	    }
+        }	
+	$path = $oldpath;
+    }
+}
+
 $daemon->register_start_command();
 $daemon->register_restart_command(1);
 $daemon->register_stop_command();
-- 
2.1.4




More information about the pve-devel mailing list