+sub dump4influx {
+ my $hash = shift;
+ my @v;
+ foreach my $k ( keys %$hash ) {
+ my $v = $hash->{$k};
+ my ( $d, $s ) = $v =~ m/^\d+(\.\d+)?$/i ? ( '', '' ) : # float
+ ( '"' ,'' ) ; # string
+
+ push @v, "$k=$d$v$d$s";
+ }
+ my $i = join(',', @v);
+ return $i;
+}
+
+open(my $fh, '>', '/dev/shm/gnt-monitor.influx');
+
+foreach my $instance ( keys %$stat ) {
+
+ next if $skip->{$instance};
+
+ print $fh qq{ganeti_cpu,node=$node,instance=$instance$location cpu=$stat->{$instance}->{cpu},ticks=$stat->{$instance}->{ticks} $t\n};
+
+ foreach my $disk ( @{ $stat->{$instance}->{disk} } ) {
+ print $fh qq{ganeti_disk,node=$node,instance=$instance$location },dump4influx( $disk ), " $t\n";
+ }
+
+ foreach my $nic ( @{ $stat->{$instance}->{nic} } ) {
+ my $vlan = delete $nic->{vlan};
+ $vlan = 0 unless defined $vlan;
+ print $fh qq{ganeti_nic,node=$node,instance=$instance,vlan=${vlan}$location },dump4influx( $nic ), " $t\n";
+ }
+
+}
+
+close($fh);
+
+if ( system( 'curl', '-s', '-XPOST', $INFLUX, '--data-binary', '@/dev/shm/gnt-monitor.influx' ) == 0 ) {
+} else {
+ rename '/dev/shm/gnt-monitor.influx', '/dev/shm/gnt-monitor.influx.bug.' . scalar glob '/dev/shm/gnt-monitor.influx.bug.*';
+ warn "curl failed: $? $!";
+}
+
+my $dt = time() - $begin_t;
+sleep $INTERVAL - $dt if $dt > 0 && $dt < $INTERVAL;