]> git.proxmox.com Git - pve-manager.git/blob - PVE/Status/Graphite.pm
add graphite tcp support
[pve-manager.git] / PVE / Status / Graphite.pm
1 package PVE::Status::Graphite;
2
3 use strict;
4 use warnings;
5
6 use PVE::Status::Plugin;
7 use IO::Socket::Timeout;
8
9 # example config (/etc/pve/status.cfg)
10 #graphite:
11 # server test
12 # port 2003
13 # proto udp
14 # path proxmox.mycluster
15 # disable 0
16 #
17
18 use base('PVE::Status::Plugin');
19
20 sub type {
21 return 'graphite';
22 }
23
24 sub properties {
25 return {
26 path => {
27 type => 'string', format => 'graphite-path',
28 description => "root graphite path (ex: proxmox.mycluster.mykey)",
29 },
30 timeout => {
31 type => 'integer',
32 description => "graphite tcp socket timeout (default=3)",
33 optional => 1
34 },
35 proto => {
36 type => 'string',
37 enum => ['udp', 'tcp'],
38 description => "send graphite data using tcp or udp (default)",
39 optional => 1,
40 },
41 };
42 }
43
44 sub options {
45 return {
46 server => {},
47 port => { optional => 1 },
48 proto => { optional => 1 },
49 timeout => { optional => 1 },
50 path => { optional => 1 },
51 disable => { optional => 1 },
52 };
53 }
54
55 # we do not want boolean/state information to export to graphite
56 my $key_blacklist = {
57 'template' => 1,
58 'pid' => 1,
59 'agent' => 1,
60 'serial' => 1,
61 };
62
63 # Plugin implementation
64 sub update_node_status {
65 my ($class, $plugin_config, $node, $data, $ctime) = @_;
66
67 write_graphite_hash($plugin_config, $data, $ctime, "nodes.$node");
68
69 }
70
71 sub update_qemu_status {
72 my ($class, $plugin_config, $vmid, $data, $ctime, $nodename) = @_;
73 write_graphite_hash($plugin_config, $data, $ctime, "qemu.$vmid");
74 }
75
76 sub update_lxc_status {
77 my ($class, $plugin_config, $vmid, $data, $ctime, $nodename) = @_;
78
79 write_graphite_hash($plugin_config, $data, $ctime, "lxc.$vmid");
80 }
81
82 sub update_storage_status {
83 my ($class, $plugin_config, $nodename, $storeid, $data, $ctime) = @_;
84
85 write_graphite_hash($plugin_config, $data, $ctime, "storages.$nodename.$storeid");
86 }
87
88 sub write_graphite_hash {
89 my ($plugin_config, $d, $ctime, $object) = @_;
90
91 my $host = $plugin_config->{server};
92 my $port = $plugin_config->{port} ? $plugin_config->{port} : 2003;
93 my $path = $plugin_config->{path} ? $plugin_config->{path} : 'proxmox';
94 my $proto = $plugin_config->{proto} ? $plugin_config->{proto} : 'udp';
95 my $timeout = $plugin_config->{timeout} ? $plugin_config->{timeout} : 3;
96
97 my $carbon_socket = IO::Socket::IP->new(
98 PeerAddr => $host,
99 PeerPort => $port,
100 Proto => $proto,
101 Timeout => $timeout,
102 ) || die "couldn't create carbon socket [$host]:$port - $@\n";
103
104 if ( $proto eq 'tcp' ) {
105 IO::Socket::Timeout->enable_timeouts_on($carbon_socket);
106 $carbon_socket->read_timeout($timeout);
107 $carbon_socket->write_timeout($timeout);
108 }
109 write_graphite($carbon_socket, $d, $ctime, $path.".$object");
110
111 $carbon_socket->close() if $carbon_socket;
112
113 }
114
115 sub write_graphite {
116 my ($carbon_socket, $d, $ctime, $path) = @_;
117
118 for my $key (keys %$d) {
119
120 my $value = $d->{$key};
121 my $oldpath = $path;
122 $key =~ s/\./-/g;
123 $path .= ".$key";
124
125 if ( defined $value ) {
126 if ( ref $value eq 'HASH' ) {
127 write_graphite($carbon_socket, $value, $ctime, $path);
128 } elsif ($value =~ m/^[+-]?[0-9]*\.?[0-9]+$/ &&
129 !$key_blacklist->{$key}) {
130 $carbon_socket->send( "$path $value $ctime\n" );
131 } else {
132 # do not send blacklisted or non-numeric values
133 }
134 }
135 $path = $oldpath;
136 }
137 }
138
139 PVE::JSONSchema::register_format('graphite-path', \&pve_verify_graphite_path);
140 sub pve_verify_graphite_path {
141 my ($path, $noerr) = @_;
142
143 my $regex = "([a-zA-Z0-9]([a-zA-Z0-9\-]*[a-zA-Z0-9])?)";
144
145 if ($path !~ /^(${regex}\.)*${regex}$/) {
146 return undef if $noerr;
147 die "value does not look like a valid graphite path\n";
148 }
149
150 return $path;
151 }
152
153
154 1;