/[ps-trend]/ps2rrd.pl
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /ps2rrd.pl

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 2 by dpavlin, Tue Jul 14 22:41:33 2009 UTC revision 12 by dpavlin, Fri Jul 17 13:43:41 2009 UTC
# Line 5  use strict; Line 5  use strict;
5    
6  use Data::Dump qw/dump/;  use Data::Dump qw/dump/;
7  use RRD::Simple;  use RRD::Simple;
8  use DateTime;  use POSIX qw/mktime/;
9    use Getopt::Long;
10    
11    my $debug = 0;
12    my $zoom = 2;
13    
14    my $only_veid;
15    my $too_small = 64;     # M
16    
17    GetOptions(
18            'debug!'   => \$debug,
19            'zoom=i'   => \$zoom,
20            'veid=i'   => \$only_veid,
21            'size=i'   => \$too_small,
22    );
23    
24    
25  my $veids;  my $veids;
26  my $vsz;  my $vsz;
27    
 my $too_small = 64;     # M  
28  $too_small *= 1024; # k  $too_small *= 1024; # k
29    
30  sub count {  sub count {
31          my $file = shift;          my $file = shift;
32          open(my $fh, '<', $file) || die "$file: $!";          open(my $fh, '<', $file) || die "$file: $!";
33          warn "# $file ", -s $file, $/;  
34            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)};
35    
36            warn "# $file ", -s $file, " bytes [$t]\n" if $debug;
37    
38          my $cols = <$fh>;          my $cols = <$fh>;
39          $cols =~ s/^\s+//;          $cols =~ s/^\s+//;
# Line 26  sub count { Line 43  sub count {
43          $cp->{$c[$_]} = $_ foreach 0 .. $#c;          $cp->{$c[$_]} = $_ foreach 0 .. $#c;
44          our @r;          our @r;
45          sub c {          sub c {
46                    return $r[ $cp->{$_[0]} ];      # XXX speedup
47    
48                  my $name = shift;                  my $name = shift;
49                  my $n = $cp->{$name};                  my $n = $cp->{$name};
50                  die "no column $name in ",dump( $cp ) unless defined $n;                  die "no column $name in ",dump( $cp ) unless defined $n;
# Line 39  sub count { Line 58  sub count {
58                  @r = split(/\s+/, $_, $#c + 1 );                  @r = split(/\s+/, $_, $#c + 1 );
59    
60                  my $veid = c('VEID');                  my $veid = c('VEID');
61                  $veid =~ s/^0$/hw/;                  next if defined $only_veid && $only_veid != $veid;
62                    $veid =~ s/^0$/0-hw/;
63    
64                  my $s = c('VSZ');                  my $s = c('VSZ');
65                  if ( $s < $too_small ) {                  if ( $s < $too_small ) {
66                          $vsz->{$file}->{$veid}+= $s * 1024;                          $vsz->{$t}->{$veid}+= $s * 1024;
67                            print STDERR ".";
68                  } else {                  } else {
69                          my $cmd = c('COMMAND');                          my $cmd = c('COMMAND');
70                          $cmd =~ s{-.+$}{};                          $cmd =~ s{-.+$}{};
# Line 53  sub count { Line 74  sub count {
74                          $cmd =~ s{\W+}{_}g;                          $cmd =~ s{\W+}{_}g;
75                          $veid .= '-' . $cmd;                          $veid .= '-' . $cmd;
76                          $veid = substr($veid,0,16);                          $veid = substr($veid,0,16);
77                          $vsz->{$file}->{$veid}+= $s * 1024;                          $vsz->{$t}->{$veid}+= $s * 1024;
78                            print STDERR substr($cmd,0,1);
79                  }                  }
80                  $veids->{$veid}++;                  $veids->{$veid}++;
81          }          }
82    
83  }  }
84    
85    print STDERR "parsing ps with grouping < $too_small k";
86    
87  count $_ foreach @ARGV;  count $_ foreach @ARGV;
88    
89  print "VSZ: ",dump( $vsz );  #print "VSZ: ",dump( $vsz );
90    
91  my @veids = keys %$veids;  my @veids = sort keys %$veids;
92  warn "# veids = ",dump( sort @veids );  warn "# veids = ",dump( @veids );
93    
94  my $rrd = RRD::Simple->new( file => "ps.rrd" );  my $rrd_file = 'ps.rrd';
95  $rrd->create( 'hour', map { ( $_ => 'GAUGE' ) } @veids );  unlink $rrd_file if -e $rrd_file;
96    
97  foreach my $file ( sort keys %$vsz ) {  my @t = sort keys %$vsz;
98          my $t = (stat($file))[9];  
99          if ( $file =~ m{(\d\d\d\d)-(\d\d)-(\d\d)/(\d\d)(\d\d)} ) {  print "\ndrawing $#t intervals ", $t[0], " - ", $t[$#t];
100                  $t = new DateTime(  
101                          year   => $1,  my $rrd = RRD::Simple->new( file => $rrd_file );
102                          month  => $2,  $rrd->create( map { ( $_ => 'GAUGE' ) } @veids );
                         day    => $3,  
                         hour   => $4,  
                         minute => $5,  
                 );  
                 warn "+ $t\n";  
         }  
103    
104          #warn "## ",dump( %{ $vsz->{$file} } );  foreach my $t ( @t ) {
105            print STDERR ".";
106    #       warn "## ",dump( %{ $vsz->{$t} } );
107    
108          eval {          eval {
109                  $rrd->update($t->epoch, map {                  $rrd->update($t, map {
110                          ( $_ => $vsz->{$file}->{$_} )                          ( $_ => $vsz->{$t}->{$_} )
111                  } @veids );                  } @veids );
112          };          };
113          warn "SKIP $t: $@\n" if $@;          warn "SKIP $t: $@\n" if $@;
# Line 99  $rrd->graph( Line 119  $rrd->graph(
119          periods => [ qw/hour 6hour 12hour day week month year 3years/ ],          periods => [ qw/hour 6hour 12hour day week month year 3years/ ],
120          extended_legend => 1,          extended_legend => 1,
121          title => "memory > $too_small K",          title => "memory > $too_small K",
122          width => 500,          width =>  500 * $zoom,
123          height => 200,          height => 200 * $zoom,
124  );  );
125    

Legend:
Removed from v.2  
changed lines
  Added in v.12

  ViewVC Help
Powered by ViewVC 1.1.26