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