#!/usr/bin/perl
use warnings;
use strict;
use Time::Local;

use Data::Dump;
sub XXX { warn Data::Dump::dump( @_ ) };

my $measurement = 'debug';
my $host;
my $yyyymmdd;

my ($dd,$mm,$yyyy);
my ($hour,$min,$sec)=(0,0,0);
my @cols;
my $time;
sub update_time {
	#warn "# time $yyyy-$mm-$dd $hour:$min:$sec $time\n";
	$time = timelocal($sec,$min,$hour,$dd,$mm-1,$yyyy) * 1000_000_000;
	return $time;
}

while(<>) {
	chomp;
	next if /^$/;

	#warn "#[$_]\n";

	my @vals;

	if ( /^(\w+)\s+(\S+)\s+(.+)\s+(\w+)\s+(\d\d\d\d)\/(\d\d)\/(\d\d)$/ ) {
		$host = $1;
		@cols = qw( kernel build arch );
		@vals =   ( qq{"$2"}, qq{"$3"}, qq{"$4"} ); # must quote all this as strings for influx
		( $yyyy, $mm, $dd ) = ( $5, $6, $7 ); update_time;

	} elsif ( m/analysis date: (\S+)/ ) {
		( $yyyy, $mm, $dd ) = split(/\//, $1, 3); update_time;
		next;

	} elsif ( m/^00:00:01\s+(.+)_(\w+)_/ ) {
		$measurement = $2;
		my $cols = $1;
		$cols =~ s/(in|out):\s+(\w+)/$1_$2/g; # ipv4, ipv6
		@cols = split(/\s+/, $cols);
		next;

	} elsif ( m/^(\d\d:\d\d:\d\d)\s+(.+)/ ) {
		( $hour, $min, $sec )  = split(/:/, $1, 3); update_time;
		@vals = split(/\s+/, $2);
	} elsif ( m/^\s+(.+)/ ) {
		@vals = split(/\s+/, $1);
	} else {
		die "UNPARSED: $_\n";
	}

	next unless @vals;

	next if $measurement =~ /top3/; # FIXME this isn't parsed yet

	#XXX { cols => \@cols, vals => \@vals };
	print "atop_$measurement,host=$host";
	my $d = " ";
	foreach my $i ( 0 .. $#cols ) {
		if ( my $c = $cols[$i] ) {
			my $s = '';

			if ( $c =~ m/(cpu|disk|interf)/ ) { ; # XXX tag
				($d,$s) = (',',' ');
			}

			$c =~ s/^%//;
			$c =~ s{/s$}{};
			$c =~ s/\W+/_/g;

			my $v = $vals[$i];
			die "line: [$_]\nno vals[$i] in ",Data::Dump::dump( @vals ) if ! defined $v;

			#$v *= 1024 * 1024 if $v =~ s/M$//;
			$v =~ s/M$//;
			$v =~ s/%$//;

			$v =~ s/\s/\\ /g unless $v =~ m/"/;

			next if $c eq "busy" && $v eq "?";

			print "$d$c=$v$s";

			$d ||= ',';
			$d = '' if $s eq " "; # move to values
			$d = ',' if $d eq " "; # move to values


		} else {
			die "line: [$_]\nno cols[$i] in ",Data::Dump::dump( @cols );
		};
		
	}
	print " $time\n";
}

