--- ps2rrd.pl 2009/07/14 22:41:33 2 +++ ps2rrd.pl 2009/07/17 13:43:41 12 @@ -5,18 +5,35 @@ use Data::Dump qw/dump/; use RRD::Simple; -use DateTime; +use POSIX qw/mktime/; +use Getopt::Long; + +my $debug = 0; +my $zoom = 2; + +my $only_veid; +my $too_small = 64; # M + +GetOptions( + 'debug!' => \$debug, + 'zoom=i' => \$zoom, + 'veid=i' => \$only_veid, + 'size=i' => \$too_small, +); + my $veids; my $vsz; -my $too_small = 64; # M $too_small *= 1024; # k sub count { my $file = shift; open(my $fh, '<', $file) || die "$file: $!"; - warn "# $file ", -s $file, $/; + + my $t = mktime( 0, $5, $4, $3, $2 - 1, $1 - 1900 ) if $file =~ m{(\d\d\d\d)-(\d\d)-(\d\d)/(\d\d)(\d\d)}; + + warn "# $file ", -s $file, " bytes [$t]\n" if $debug; my $cols = <$fh>; $cols =~ s/^\s+//; @@ -26,6 +43,8 @@ $cp->{$c[$_]} = $_ foreach 0 .. $#c; our @r; sub c { + return $r[ $cp->{$_[0]} ]; # XXX speedup + my $name = shift; my $n = $cp->{$name}; die "no column $name in ",dump( $cp ) unless defined $n; @@ -39,11 +58,13 @@ @r = split(/\s+/, $_, $#c + 1 ); my $veid = c('VEID'); - $veid =~ s/^0$/hw/; + next if defined $only_veid && $only_veid != $veid; + $veid =~ s/^0$/0-hw/; my $s = c('VSZ'); if ( $s < $too_small ) { - $vsz->{$file}->{$veid}+= $s * 1024; + $vsz->{$t}->{$veid}+= $s * 1024; + print STDERR "."; } else { my $cmd = c('COMMAND'); $cmd =~ s{-.+$}{}; @@ -53,41 +74,40 @@ $cmd =~ s{\W+}{_}g; $veid .= '-' . $cmd; $veid = substr($veid,0,16); - $vsz->{$file}->{$veid}+= $s * 1024; + $vsz->{$t}->{$veid}+= $s * 1024; + print STDERR substr($cmd,0,1); } $veids->{$veid}++; } } +print STDERR "parsing ps with grouping < $too_small k"; + count $_ foreach @ARGV; -print "VSZ: ",dump( $vsz ); +#print "VSZ: ",dump( $vsz ); -my @veids = keys %$veids; -warn "# veids = ",dump( sort @veids ); +my @veids = sort keys %$veids; +warn "# veids = ",dump( @veids ); -my $rrd = RRD::Simple->new( file => "ps.rrd" ); -$rrd->create( 'hour', map { ( $_ => 'GAUGE' ) } @veids ); +my $rrd_file = 'ps.rrd'; +unlink $rrd_file if -e $rrd_file; -foreach my $file ( sort keys %$vsz ) { - my $t = (stat($file))[9]; - if ( $file =~ m{(\d\d\d\d)-(\d\d)-(\d\d)/(\d\d)(\d\d)} ) { - $t = new DateTime( - year => $1, - month => $2, - day => $3, - hour => $4, - minute => $5, - ); - warn "+ $t\n"; - } +my @t = sort keys %$vsz; + +print "\ndrawing $#t intervals ", $t[0], " - ", $t[$#t]; + +my $rrd = RRD::Simple->new( file => $rrd_file ); +$rrd->create( map { ( $_ => 'GAUGE' ) } @veids ); - #warn "## ",dump( %{ $vsz->{$file} } ); +foreach my $t ( @t ) { + print STDERR "."; +# warn "## ",dump( %{ $vsz->{$t} } ); eval { - $rrd->update($t->epoch, map { - ( $_ => $vsz->{$file}->{$_} ) + $rrd->update($t, map { + ( $_ => $vsz->{$t}->{$_} ) } @veids ); }; warn "SKIP $t: $@\n" if $@; @@ -99,7 +119,7 @@ periods => [ qw/hour 6hour 12hour day week month year 3years/ ], extended_legend => 1, title => "memory > $too_small K", - width => 500, - height => 200, + width => 500 * $zoom, + height => 200 * $zoom, );