/[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 20 by dpavlin, Sun Jul 19 19:06:10 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 $by_veid;
16    my $too_small = 64;     # M
17    my $filter = qr{(apache|mysql|postgres|cgi-bin|SIP|ldap|zebra)};
18    
19    my $use = 'VSZ';
20    
21    GetOptions(
22            'debug!'   => \$debug,
23            'zoom=i'   => \$zoom,
24            'only-veid=i' => \$only_veid,
25            'by-veid!' => \$by_veid,
26            'size=i'   => \$too_small,
27            'use=s'    => \$use,
28    );
29    
30    
31  my $veids;  my $veids;
32  my $vsz;  my $vsz;
33    
 my $too_small = 64;     # M  
34  $too_small *= 1024; # k  $too_small *= 1024; # k
35    
36  sub count {  sub count {
37          my $file = shift;          my $file = shift;
38          open(my $fh, '<', $file) || die "$file: $!";          open(my $fh, '<', $file) || die "$file: $!";
39          warn "# $file ", -s $file, $/;  
40            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)};
41    
42    #       warn "# $file ", -s $file, " bytes [$t]\n" if $debug;
43            print STDERR "\n$file\t";
44    
45          my $cols = <$fh>;          my $cols = <$fh>;
46          $cols =~ s/^\s+//;          $cols =~ s/^\s+//;
# Line 26  sub count { Line 50  sub count {
50          $cp->{$c[$_]} = $_ foreach 0 .. $#c;          $cp->{$c[$_]} = $_ foreach 0 .. $#c;
51          our @r;          our @r;
52          sub c {          sub c {
53                    return $r[ $cp->{$_[0]} ];      # XXX speedup
54    
55                  my $name = shift;                  my $name = shift;
56                  my $n = $cp->{$name};                  my $n = $cp->{$name};
57                  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 65  sub count {
65                  @r = split(/\s+/, $_, $#c + 1 );                  @r = split(/\s+/, $_, $#c + 1 );
66    
67                  my $veid = c('VEID');                  my $veid = c('VEID');
68                  $veid =~ s/^0$/hw/;                  next if defined $only_veid && $only_veid != $veid;
69                    $veid =~ s/^0$/_hw_0/;
70    
71                    my $s = c($use);
72                    my $cmd = c('COMMAND');
73                    if ( ! $by_veid && $cmd =~ $filter ) {
74    
75                            $veid .= '-' . $1;
76                            $vsz->{$t}->{$veid} += $s * 1024;
77                            print STDERR uc(substr($1,0,1));
78    
79                    } elsif ( $s < $too_small || $by_veid ) {
80    
81                            $vsz->{$t}->{$veid}+= $s * 1024;
82                            print STDERR ".";
83    
                 my $s = c('VSZ');  
                 if ( $s < $too_small ) {  
                         $vsz->{$file}->{$veid}+= $s * 1024;  
84                  } else {                  } else {
85                          my $cmd = c('COMMAND');  
86                          $cmd =~ s{-.+$}{};                          $cmd =~ s{-.+$}{};
87                          $cmd =~ s{^/\S+/(\w+?)}{$1};                          $cmd =~ s{^/\S+/(\w+?)}{$1};
88                          $cmd =~ s{^\w+ /\S+/(\w+?)}{$1};                          $cmd =~ s{^\w+ /\S+/(\w+?)}{$1};
# Line 53  sub count { Line 90  sub count {
90                          $cmd =~ s{\W+}{_}g;                          $cmd =~ s{\W+}{_}g;
91                          $veid .= '-' . $cmd;                          $veid .= '-' . $cmd;
92                          $veid = substr($veid,0,16);                          $veid = substr($veid,0,16);
93                          $vsz->{$file}->{$veid}+= $s * 1024;                          $vsz->{$t}->{$veid}+= $s * 1024;
94                            print STDERR substr($cmd,0,1);
95                  }                  }
96                  $veids->{$veid}++;                  $veids->{$veid}++;
97          }          }
98    
99  }  }
100    
101    print STDERR "parsing ps with grouping < $too_small k";
102    
103  count $_ foreach @ARGV;  count $_ foreach @ARGV;
104    
105  print "VSZ: ",dump( $vsz );  #print "VSZ: ",dump( $vsz );
106    
107  my @veids = keys %$veids;  my @veids = sort keys %$veids;
108  warn "# veids = ",dump( sort @veids );  warn "# veids = ",dump( @veids );
109    
110  my $rrd = RRD::Simple->new( file => "ps.rrd" );  my $rrd_file = $use . '.rrd';
111  $rrd->create( 'hour', map { ( $_ => 'GAUGE' ) } @veids );  unlink $rrd_file if -e $rrd_file;
112    
113  foreach my $file ( sort keys %$vsz ) {  my @t = sort keys %$vsz;
114          my $t = (stat($file))[9];  
115          if ( $file =~ m{(\d\d\d\d)-(\d\d)-(\d\d)/(\d\d)(\d\d)} ) {  print "\ndrawing $#t intervals ", $t[0], " - ", $t[$#t];
116                  $t = new DateTime(  
117                          year   => $1,  my $rrd = RRD::Simple->new( file => $rrd_file );
118                          month  => $2,  $rrd->create( map { ( $_ => 'GAUGE' ) } @veids );
                         day    => $3,  
                         hour   => $4,  
                         minute => $5,  
                 );  
                 warn "+ $t\n";  
         }  
119    
120          #warn "## ",dump( %{ $vsz->{$file} } );  foreach my $t ( @t ) {
121            print STDERR ".";
122    #       warn "## ",dump( %{ $vsz->{$t} } );
123    
124          eval {          eval {
125                  $rrd->update($t->epoch, map {                  $rrd->update($t, map {
126                          ( $_ => $vsz->{$file}->{$_} )                          ( $_ => $vsz->{$t}->{$_} )
127                  } @veids );                  } @veids );
128          };          };
129          warn "SKIP $t: $@\n" if $@;          warn "SKIP $t: $@\n" if $@;
# Line 98  $rrd->graph( Line 134  $rrd->graph(
134          source_drawtypes => [ map { $_ ? 'STACK' : 'AREA' } 0 .. $#veids ],          source_drawtypes => [ map { $_ ? 'STACK' : 'AREA' } 0 .. $#veids ],
135          periods => [ qw/hour 6hour 12hour day week month year 3years/ ],          periods => [ qw/hour 6hour 12hour day week month year 3years/ ],
136          extended_legend => 1,          extended_legend => 1,
137          title => "memory > $too_small K",          title => "$use > $too_small K",
138          width => 500,          width =>  500 * $zoom,
139          height => 200,          height => 200 * $zoom,
140  );  );
141    

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

  ViewVC Help
Powered by ViewVC 1.1.26