]> git.proxmox.com Git - pve-manager.git/blob - PVE/Status/Graphite.pm
b744968990567e3542df6609cd7b90cd85de9861
[pve-manager.git] / PVE / Status / Graphite.pm
1 package PVE::Status::Graphite;
2
3 use strict;
4 use warnings;
5 use PVE::Status::Plugin;
6
7 # example config (/etc/pve/status.cfg)
8 #graphite:
9 # server test
10 # port 2003
11 # path proxmox.mycluster
12 # disable 0
13 #
14
15 use base('PVE::Status::Plugin');
16
17 sub type {
18 return 'graphite';
19 }
20
21 sub properties {
22 return {
23 server => {
24 type => 'string', format => 'dns-name',
25 description => "server dns name",
26 },
27 port => {
28 type => 'integer',
29 description => "network port",
30 },
31 path => {
32 type => 'string', format => 'graphite-path',
33 description => "root graphite path (ex: proxmox.mycluster.mykey)",
34 },
35 };
36 }
37
38 sub options {
39 return {
40 server => {},
41 port => { optional => 1 },
42 path => { optional => 1 },
43 disable => { optional => 1 },
44 };
45 }
46
47 # Plugin implementation
48 sub update_node_status {
49 my ($class, $plugin_config, $node, $data, $ctime) = @_;
50
51 write_graphite_hash($plugin_config, $data, $ctime, "nodes.$node");
52
53 }
54
55 sub update_qemu_status {
56 my ($class, $plugin_config, $vmid, $data, $ctime) = @_;
57 write_graphite_hash($plugin_config, $data, $ctime, "qemu.$vmid");
58 }
59
60 sub update_lxc_status {
61 my ($class, $plugin_config, $vmid, $data, $ctime) = @_;
62
63 write_graphite_hash($plugin_config, $data, $ctime, "lxc.$vmid");
64 }
65
66 sub update_storage_status {
67 my ($class, $plugin_config, $nodename, $storeid, $data, $ctime) = @_;
68
69 write_graphite_hash($plugin_config, $data, $ctime, "storages.$nodename.$storeid");
70 }
71
72 sub write_graphite_hash {
73 my ($plugin_config, $d, $ctime, $object) = @_;
74
75 my $host = $plugin_config->{server};
76 my $port = $plugin_config->{port} ? $plugin_config->{port} : 2003;
77 my $path = $plugin_config->{path} ? $plugin_config->{path} : 'proxmox';
78
79 my $carbon_socket = IO::Socket::IP->new(
80 PeerAddr => $host,
81 PeerPort => $port,
82 Proto => 'udp',
83 );
84
85 write_graphite($carbon_socket, $d, $ctime, $path.".$object");
86
87 $carbon_socket->close() if $carbon_socket;
88
89 }
90
91 sub write_graphite {
92 my ($carbon_socket, $d, $ctime, $path) = @_;
93
94 for my $key (keys %$d) {
95
96 my $value = $d->{$key};
97 my $oldpath = $path;
98 $key =~ s/\./-/g;
99 $path .= ".$key";
100
101 if ( defined $value ) {
102 if ( ref $value eq 'HASH' ) {
103 write_graphite($carbon_socket, $value, $ctime, $path);
104 }else {
105 $carbon_socket->send( "$path $value $ctime" );
106 }
107 }
108 $path = $oldpath;
109 }
110 }
111
112 PVE::JSONSchema::register_format('graphite-path', \&pve_verify_graphite_path);
113 sub pve_verify_graphite_path {
114 my ($path, $noerr) = @_;
115
116 my $regex = "([a-zA-Z0-9]([a-zA-Z0-9\-]*[a-zA-Z0-9])?)";
117
118 if ($path !~ /^(${regex}\.)*${regex}$/) {
119 return undef if $noerr;
120 die "value does not look like a valid graphite path\n";
121 }
122
123 return $path;
124 }
125
126
127 1;