6 use Time::HiRes qw(time);
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;
12 my $RACK = $ENV{RACK};
14 my $node = `hostname -s`;
18 $location .= qq{,dc=$DC} if $DC;
19 $location .= qq{,rack=$RACK} if $RACK;
21 warn $0 = "gnt-monitor\@$node$location $INFLUX $INTERVAL $DEBUG";
24 sub XXX { $DEBUG ? warn "XXX ",Data::Dump::dump( @_ ) : {} };
33 foreach my $instance ( glob '/var/run/ganeti/kvm-hypervisor/pid/*' ) {
35 open(my $fh, '<', $instance);
36 my $pid = <$fh>; chomp $pid;
38 $instance =~ s{^.*/}{};
40 if ( ! -d "/proc/$pid" ) {
46 my $vcpu = $last->{$instance}->{vcpu};
49 foreach my $fd ( glob "/proc/$pid/fd/*" ) {
50 $vcpu++ if -l $fd && readlink($fd) =~ m/kvm-vcpu/;
53 $last->{$instance}->{vcpu} = $vcpu;
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
61 if ( my $last_gtime = $last->{$instance}->{gtime} ) {
62 my $clock_ticks = POSIX::sysconf( &POSIX::_SC_CLK_TCK ); # clock ticks per second
64 my $cpu = ( ( $gtime - $last_gtime ) * 100 ) / ( $clock_ticks * $vcpu );
65 $stat->{$instance}->{cpu} = $cpu;
66 $stat->{$instance}->{ticks} = $gtime - $last_gtime;
69 $last->{$instance}->{gtime} = $gtime;
73 foreach my $glob ( glob '/var/run/ganeti/instance-disks/*' ) {
74 my ( $instance, $disk ) = split(/:/,$glob,2);
75 $instance =~ s{^.*/}{};
77 next unless exists $stat->{$instance};
79 my $dev = readlink $glob;
82 if ( ! -e "/sys/class/block/$dev" ) {
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
94 read_bytes => $s[2] * 512,
97 write_bytes => $s[6] * 512,
100 if ( my $l = $last->{$instance}->{disk}->[$disk] ) {
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;
107 $last->{$instance}->{disk}->[$disk] = $d;
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};
116 foreach my $nic ( glob "$full_instance/*" ) {
117 open(my $fh, '<', $nic);
120 next unless -e "/sys/class/net/$dev";
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;
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";
137 $stat->{$instance}->{nic}->[$nic]->{vlan} = $vlan;
140 $last->{$instance}->{nic}->[$nic] = $d;
151 foreach my $k ( keys %$hash ) {
153 my ( $d, $s ) = $v =~ m/^\d+$/ ? ( '', 'i' ) :
154 $v =~ m/\w+/ ? ( '"', '' ) :
155 ( '' , '' ) ; # float
157 push @v, "$k=$d$v$d$s";
159 my $i = join(',', @v);
163 open(my $fh, '>', '/dev/shm/gnt-monitor.influx');
165 my $t = time() * 1000_000_000;
167 foreach my $instance ( keys %$stat ) {
169 next if $skip->{$instance};
171 print $fh qq{cpu,node=$node,instance=$instance$location cpu=$stat->{$instance}->{cpu},ticks=$stat->{$instance}->{ticks} $t\n};
173 foreach my $disk ( @{ $stat->{$instance}->{disk} } ) {
174 print $fh qq{disk,node=$node,instance=$instance$location },dump4influx( $disk ), " $t\n";
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";
186 if ( system( 'curl', '-XPOST', $INFLUX, '--data-binary', '@/dev/shm/gnt-monitor.influx' ) == 0 ) {
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: $? $!";