]> git.proxmox.com Git - pve-manager.git/blob - PVE/Status/Graphite.pm
Graphite.pm: fix whitespace
[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 path => {
24 type => 'string', format => 'graphite-path',
25 description => "root graphite path (ex: proxmox.mycluster.mykey)",
26 },
27 };
28 }
29
30 sub options {
31 return {
32 server => {},
33 port => { optional => 1 },
34 path => { optional => 1 },
35 disable => { optional => 1 },
36 };
37 }
38
39 # we do not want boolean/state information to export to graphite
40 my $key_blacklist = {
41 'template' => 1,
42 'pid' => 1,
43 'agent' => 1,
44 'serial' => 1,
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, $nodename) = @_;
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, $nodename) = @_;
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 ) || die "couldn't create carbon socket [$host]:$port - $@\n";
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 } elsif ($value =~ m/^[+-]?[0-9]*\.?[0-9]+$/ &&
105 !$key_blacklist->{$key}) {
106 $carbon_socket->send( "$path $value $ctime\n" );
107 } else {
108 # do not send blacklisted or non-numeric values
109 }
110 }
111 $path = $oldpath;
112 }
113 }
114
115 PVE::JSONSchema::register_format('graphite-path', \&pve_verify_graphite_path);
116 sub pve_verify_graphite_path {
117 my ($path, $noerr) = @_;
118
119 my $regex = "([a-zA-Z0-9]([a-zA-Z0-9\-]*[a-zA-Z0-9])?)";
120
121 if ($path !~ /^(${regex}\.)*${regex}$/) {
122 return undef if $noerr;
123 die "value does not look like a valid graphite path\n";
124 }
125
126 return $path;
127 }
128
129
130 1;