691defccad7c80c5d1c9e9e4b3ae978321d10b46
[atop-influxdb] / atop2influx.pl
1 #!/usr/bin/perl
2 use warnings;
3 use strict;
4 use Time::Local;
5
6 use Data::Dump;
7 sub XXX { warn Data::Dump::dump( @_ ) };
8
9 my $measurement = 'debug';
10 my $host;
11 my $yyyymmdd;
12
13 my ($dd,$mm,$yyyy);
14 my ($hour,$min,$sec)=(0,0,0);
15 my @cols;
16 my $time;
17 sub update_time {
18         #warn "# time $yyyy-$mm-$dd $hour:$min:$sec $time\n";
19         $time = timelocal($sec,$min,$hour,$dd,$mm-1,$yyyy) * 1000_000_000;
20         return $time;
21 }
22
23 while(<>) {
24         chomp;
25         next if /^$/;
26
27         #warn "#[$_]\n";
28
29         my @vals;
30
31         if ( /^(\w+)\s+(\d+\S+)\s+(.+)\s+(\w+)\s+(\d\d\d\d)\/(\d\d)\/(\d\d)$/ ) {
32                 $host = $1;
33                 @cols = qw( kernel build arch );
34                 @vals =   ( qq{"$2"}, qq{"$3"}, qq{"$4"} ); # must quote all this as strings for influx
35                 ( $yyyy, $mm, $dd ) = ( $5, $6, $7 ); update_time;
36
37         } elsif ( m/analysis date: (\S+)/ ) {
38                 ( $yyyy, $mm, $dd ) = split(/\//, $1, 3); update_time;
39                 next;
40
41         } elsif ( m/^\d\d:\d\d:\d\d\s+(.+)_(\w+)_/ ) {
42                 $measurement = $2;
43                 my $cols = $1;
44                 $cols =~ s/(in|out):\s+(\w+)/$1_$2/g; # ipv4, ipv6
45                 @cols = split(/\s+/, $cols);
46                 next;
47
48         } elsif ( m/^(\d\d:\d\d:\d\d)\s+(.+)/ ) {
49                 next if m/logging restarted/;
50                 ( $hour, $min, $sec )  = split(/:/, $1, 3); update_time;
51                 @vals = split(/\s+/, $2);
52         } elsif ( m/^\s+(.+)/ ) {
53                 @vals = split(/\s+/, $1);
54         } else {
55                 die "UNPARSED: $_\n";
56         }
57
58         next unless @vals;
59
60         next if $measurement =~ /top3/; # FIXME this isn't parsed yet
61
62         #XXX { cols => \@cols, vals => \@vals };
63         print "atop_$measurement,host=$host";
64         my $d = " ";
65         foreach my $i ( 0 .. $#cols ) {
66                 if ( my $c = $cols[$i] ) {
67                         my $s = '';
68
69                         if ( $c =~ m/(cpu|disk|interf)/ ) { ; # XXX tag
70                                 ($d,$s) = (',',' ');
71                         }
72
73                         $c =~ s/^%//;
74                         $c =~ s{/s$}{};
75                         $c =~ s/\W+/_/g;
76
77                         my $v = $vals[$i];
78                         if ( ! defined $v ) {
79                                 warn "WARN line: [$_]\nno vals[$i] in ",Data::Dump::dump( @vals );
80                                 next;
81                         };
82
83                         #$v *= 1024 * 1024 if $v =~ s/M$//;
84                         $v =~ s/M$//;
85                         $v =~ s/%$//;
86
87                         $v =~ s/\s/\\ /g unless $v =~ m/"/;
88
89                         next if $c eq "busy" && $v eq "?";
90
91 #                       $s = 'i' if $v =~ m/^\d+$/;
92
93                         print "$d$c=$v$s";
94
95                         $d ||= ',';
96                         $d = '' if $s eq " "; # move to values
97                         $d = ',' if $d eq " "; # move to values
98
99
100                 } else {
101                         die "line: [$_]\nno cols[$i] in ",Data::Dump::dump( @cols );
102                 };
103                 
104         }
105         print " $time\n";
106 }
107