--- ps2rrd.pl 2009/07/16 19:13:01 6 +++ ps2rrd.pl 2009/07/19 17:49:55 15 @@ -5,35 +5,39 @@ 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 $by_veid; +my $too_small = 64; # M +my $filter = qr{(apache|mysql|postgres|cgi-bin|SIP)}; + +GetOptions( + 'debug!' => \$debug, + 'zoom=i' => \$zoom, + 'only-veid=i' => \$only_veid, + 'by-veid!' => \$by_veid, + 'size=i' => \$too_small, +); + + my $veids; my $vsz; -my $too_small = 16; # M $too_small *= 1024; # k sub count { my $file = shift; open(my $fh, '<', $file) || die "$file: $!"; - print STDERR "."; + 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)}; - 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, - )->epoch; - } - - warn "# $file ", -s $file, " bytes [$t]\n" if $debug; +# warn "# $file ", -s $file, " bytes [$t]\n" if $debug; + print STDERR "\n$file\t"; my $cols = <$fh>; $cols =~ s/^\s+//; @@ -43,6 +47,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; @@ -56,13 +62,24 @@ @r = split(/\s+/, $_, $#c + 1 ); my $veid = c('VEID'); - $veid =~ s/^0$/hw/; + next if defined $only_veid && $only_veid != $veid; + $veid =~ s/^0$/_hw_0/; my $s = c('VSZ'); - if ( $s < $too_small ) { + my $cmd = c('COMMAND'); + if ( ! $by_veid && $cmd =~ $filter ) { + + $veid .= '-' . $1; + $vsz->{$t}->{$veid} += $s * 1024; + print STDERR uc(substr($1,0,1)); + + } elsif ( $s < $too_small || $by_veid ) { + $vsz->{$t}->{$veid}+= $s * 1024; + print STDERR "."; + } else { - my $cmd = c('COMMAND'); + $cmd =~ s{-.+$}{}; $cmd =~ s{^/\S+/(\w+?)}{$1}; $cmd =~ s{^\w+ /\S+/(\w+?)}{$1}; @@ -71,28 +88,33 @@ $veid .= '-' . $cmd; $veid = substr($veid,0,16); $vsz->{$t}->{$veid}+= $s * 1024; + print STDERR substr($cmd,0,1); } $veids->{$veid}++; } } -print STDERR "reading ps dumps"; +print STDERR "parsing ps with grouping < $too_small k"; count $_ foreach @ARGV; #print "VSZ: ",dump( $vsz ); -my @veids = keys %$veids; -warn "# veids = ",dump( sort @veids ); +my @veids = sort keys %$veids; +warn "# veids = ",dump( @veids ); my $rrd_file = 'ps.rrd'; unlink $rrd_file if -e $rrd_file; +my @t = sort keys %$vsz; + +print "\ndrawing $#t intervals ", $t[0], " - ", $t[$#t]; + my $rrd = RRD::Simple->new( file => $rrd_file ); -$rrd->create( 'hour', map { ( $_ => 'GAUGE' ) } @veids ); +$rrd->create( map { ( $_ => 'GAUGE' ) } @veids ); -foreach my $t ( sort keys %$vsz ) { +foreach my $t ( @t ) { print STDERR "."; # warn "## ",dump( %{ $vsz->{$t} } );