]> git.proxmox.com Git - pve-manager.git/blob - PVE/Status/InfluxDB.pm
507f9b2193c6909d8b65aee3a2d92b33d4e1a0de
[pve-manager.git] / PVE / Status / InfluxDB.pm
1 package PVE::Status::InfluxDB;
2
3 use strict;
4 use warnings;
5 use PVE::Status::Plugin;
6 use Data::Dumper;
7 use PVE::SafeSyslog;
8
9 # example config (/etc/pve/status.cfg)
10 #influxdb:
11 # server test
12 # port 8089
13 # disable 0
14 #
15
16 use base('PVE::Status::Plugin');
17
18 sub type {
19 return 'influxdb';
20 }
21
22 sub options {
23 return {
24 server => {},
25 port => {},
26 disable => { optional => 1 },
27 };
28 }
29
30 # Plugin implementation
31 sub update_node_status {
32 my ($class, $plugin_config, $node, $data, $ctime) = @_;
33
34 $ctime *= 1000000000;
35
36 write_influxdb_hash($plugin_config, $data, $ctime, "object=nodes,host=$node");
37
38 }
39
40 sub update_qemu_status {
41 my ($class, $plugin_config, $vmid, $data, $ctime) = @_;
42
43 $ctime *= 1000000000;
44
45 my $object = "object=qemu,vmid=$vmid";
46 if($data->{name} && $data->{name} ne '') {
47 $object .= ",host=$data->{name}";
48 }
49 $object =~ s/\s/\\ /g;
50 write_influxdb_hash($plugin_config, $data, $ctime, $object);
51 }
52
53 sub update_lxc_status {
54 my ($class, $plugin_config, $vmid, $data, $ctime) = @_;
55
56 $ctime *= 1000000000;
57
58 my $object = "object=lxc,vmid=$vmid";
59 if($data->{name} && $data->{name} ne '') {
60 $object .= ",host=$data->{name}";
61 }
62 $object =~ s/\s/\\ /g;
63
64 write_influxdb_hash($plugin_config, $data, $ctime, $object);
65 }
66
67 sub update_storage_status {
68 my ($class, $plugin_config, $nodename, $storeid, $data, $ctime) = @_;
69
70 $ctime *= 1000000000;
71
72 my $object = "object=storages,nodename=$nodename,host=$storeid";
73 if($data->{type} && $data->{type} ne '') {
74 $object .= ",type=$data->{type}";
75 }
76 $object =~ s/\s/\\ /g;
77
78 write_influxdb_hash($plugin_config, $data, $ctime, $object);
79 }
80
81 sub write_influxdb_hash {
82 my ($plugin_config, $d, $ctime, $tags) = @_;
83
84 my $payload = {};
85
86 build_influxdb_payload($payload, $d, $ctime, $tags);
87
88 my $host = $plugin_config->{server};
89 my $port = $plugin_config->{port};
90
91 my $socket = IO::Socket::IP->new(
92 PeerAddr => $host,
93 PeerPort => $port,
94 Proto => 'udp',
95 );
96
97 $socket->send($payload->{string});
98 $socket->close() if $socket;
99
100 }
101
102 sub build_influxdb_payload {
103 my ($payload, $d, $ctime, $tags, $keyprefix, $depth) = @_;
104
105 $depth = 0 if !$depth;
106
107 for my $key (keys %$d) {
108
109 my $value = $d->{$key};
110 my $oldtags = $tags;
111
112 if ( defined $value ) {
113 if ( ref $value eq 'HASH' ) {
114
115 if($depth == 0) {
116 $keyprefix = $key;
117 }elsif($depth == 1){
118 $tags .= ",instance=$key";
119 }
120
121 $depth++;
122 build_influxdb_payload($payload, $value, $ctime, $tags, $keyprefix, $depth);
123 $depth--;
124
125 }elsif ($value =~ m/^\d+$/) {
126
127 $keyprefix = "system" if !$keyprefix && $depth == 0;
128
129 $payload->{string} .= $keyprefix."_"."$key,$tags value=$value $ctime\n";
130 }
131 }
132 $tags = $oldtags;
133 }
134 }
135
136 1;