move location generation to avoid warning
[gnt-info] / gnt-monitor
1 #!/usr/bin/perl
2 use warnings;
3 use strict;
4 use autodie;
5 use POSIX;
6
7 my $DEBUG = $ENV{DEBUG} || 0;
8 my $INFLUX = $ENV{INFLUX} || 'http://10.80.3.89:8086/write?db=gnt';
9 my $INTERVAL = $ENV{INTERVAL} || 1;
10 my $DC = $ENV{DC};
11 my $RACK = $ENV{RACK};
12
13 my $node = `hostname -s`;
14 chomp $node;
15
16 my $location = '';
17 $location .= qq{,dc=$DC} if $DC;
18 $location .= qq{,rack=$RACK} if $RACK;
19
20 warn $0 = "gnt-monitor\@$node$location $INFLUX $INTERVAL $DEBUG";
21
22 use Data::Dump;
23 sub XXX { $DEBUG ? warn "XXX ",Data::Dump::dump( @_ ) : {} };
24
25 my $stat;
26 my $last;
27
28 while(1) {
29
30 my $skip;
31
32 foreach my $instance ( glob '/var/run/ganeti/kvm-hypervisor/pid/*' ) {
33
34         open(my $fh, '<', $instance);
35         my $pid = <$fh>; chomp $pid;
36
37         $instance =~ s{^.*/}{};
38
39         if ( ! -d "/proc/$pid" ) {
40                 $skip->{$instance}++;
41                 XXX $skip;
42                 next;
43         }
44
45         my $vcpu = $last->{$instance}->{vcpu};
46
47         if ( ! $vcpu ) {
48                 foreach my $fd ( glob "/proc/$pid/fd/*" ) {
49                         $vcpu++ if -l $fd && readlink($fd) =~ m/kvm-vcpu/;
50                 }
51
52                 $last->{$instance}->{vcpu} = $vcpu;
53         }
54
55         # https://www.kernel.org/doc/Documentation/filesystems/proc.txt
56         open($fh, '<', "/proc/$pid/stat");
57         my $line = <$fh>; chomp $line;
58         my $gtime = (split(/\s+/,$line))[42]; # guest time of the task in jiffies
59
60         if ( my $last_gtime = $last->{$instance}->{gtime} ) {
61                 my $clock_ticks = POSIX::sysconf( &POSIX::_SC_CLK_TCK ); # clock ticks per second
62
63                 my $cpu = ( ( $gtime - $last_gtime ) * 100 ) / ( $clock_ticks * $vcpu );
64                 $stat->{$instance}->{cpu} = $cpu;
65         }
66
67         $last->{$instance}->{gtime} = $gtime;
68 }
69                 
70
71 foreach my $glob ( glob '/var/run/ganeti/instance-disks/*' ) {
72         my ( $instance, $disk ) = split(/:/,$glob,2);
73         $instance =~ s{^.*/}{};
74
75         next unless exists $stat->{$instance};
76
77         my $dev = readlink $glob;
78         $dev =~ s{^.*dev/}{};
79
80         if ( ! -e "/sys/class/block/$dev" ) {
81                 $skip->{$instance}++;
82                 XXX $skip;
83                 next;
84         }
85
86         open( my $fh, '<', "/sys/class/block/$dev/stat" );
87         my $v = <$fh>; chomp $v; $v =~ s/^\s+//;
88         my @s = split(/\s+/, $v );
89         # https://www.kernel.org/doc/Documentation/block/stat.txt
90         my $d = {
91                 read_io => $s[0],
92                 read_bytes => $s[2] * 512,
93                 read_wait => $s[3],
94                 write_io => $s[4],
95                 write_bytes => $s[6] * 512,
96                 write_wait => $s[7],
97         };
98         if ( my $l = $last->{$instance}->{disk}->[$disk] ) {
99                 my $delta;
100                 $delta->{$_} = $d->{$_} - $l->{$_} foreach keys %$d;
101                 $stat->{$instance}->{disk}->[$disk] = $delta;
102                 $stat->{$instance}->{disk}->[$disk]->{dev} = $dev;
103                 $stat->{$instance}->{disk}->[$disk]->{disk} = $disk;
104         }
105         $last->{$instance}->{disk}->[$disk] = $d;
106
107 }
108
109 foreach my $full_instance ( glob '/var/run/ganeti/kvm-hypervisor/nic/*' ) {
110         my $instance = $full_instance;
111         $instance =~ s{^.*/}{};
112         next unless exists $stat->{$instance};
113
114         foreach my $nic ( glob "$full_instance/*" ) {
115                 open(my $fh, '<', $nic);
116                 my $dev = <$fh>;
117
118                 next unless -e "/sys/class/net/$dev";
119
120                 $nic =~ s{^.*/}{};
121
122                 my $d;
123
124                 foreach my $f (qw( rx_bytes tx_bytes rx_packets tx_packets )) {
125                         open( my $fh, '<', "/sys/class/net/$dev/statistics/$f" );
126                         my $v = <$fh>; chomp $v;
127                         $d->{$f} = $v;
128                 }
129                 if ( my $l = $last->{$instance}->{nic}->[$nic] ) {
130                         $stat->{$instance}->{nic}->[$nic]->{$_} = $d->{$_} - $l->{$_} foreach keys %$d;
131                         $stat->{$instance}->{nic}->[$nic]->{dev} = $dev;
132                         if ( -e "/sys/class/net/$dev/master" ) {
133                                 my $vlan = readlink "/sys/class/net/$dev/master";
134                                 $vlan =~ s/^.*br//;
135                                 $stat->{$instance}->{nic}->[$nic]->{vlan} = $vlan;
136                         }
137                 }
138                 $last->{$instance}->{nic}->[$nic] = $d;
139
140         }
141 }
142
143 #XXX $stat;
144 #XXX $last;
145
146 open(my $fh, '>', '/dev/shm/gnt-monitor.influx');
147
148 sub dump4influx {
149         my $hash = shift;
150         my @v;
151         foreach my $k ( keys %$hash ) {
152                 my $v = $hash->{$k};
153                 my ( $d, $s ) = $v =~ m/^\d+$/ ? ( '',  'i' ) :
154                                 $v =~ m/\w+/   ? ( '"', ''  ) :
155                                                  ( '' , ''  ) ; # float
156
157                 push @v, "$k=$d$v$d$s";
158         }
159         my $i = join(',', @v);
160         return $i;
161 }
162
163 foreach my $instance ( keys %$stat ) {
164
165         next if $skip->{$instance};
166
167         print $fh qq{cpu,node=$node,instance=$instance$location cpu=}, $stat->{$instance}->{cpu}, "\n";
168
169         foreach my $disk ( @{ $stat->{$instance}->{disk} } ) {
170                 print $fh qq{disk,node=$node,instance=$instance$location },dump4influx( $disk ), "\n";
171         }
172
173         foreach my $nic ( @{ $stat->{$instance}->{nic} } ) {
174                 my $vlan = delete $nic->{vlan};
175                 print $fh qq{nic,node=$node,instance=$instance,vlan=${vlan}$location },dump4influx( $nic ), "\n";
176         }
177
178 }
179
180 close($fh);
181
182 system 'curl', '-XPOST', $INFLUX, '--data-binary', '@/dev/shm/gnt-monitor.influx';
183
184 sleep $INTERVAL;
185 } #/while
186