/[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 6 by dpavlin, Thu Jul 16 19:13:01 2009 UTC revision 15 by dpavlin, Sun Jul 19 17:49:55 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;  my $debug = 0;
12  my $zoom = 2;  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)};
18    
19    GetOptions(
20            'debug!'   => \$debug,
21            'zoom=i'   => \$zoom,
22            'only-veid=i' => \$only_veid,
23            'by-veid!' => \$by_veid,
24            'size=i'   => \$too_small,
25    );
26    
27    
28  my $veids;  my $veids;
29  my $vsz;  my $vsz;
30    
 my $too_small = 16;     # M  
31  $too_small *= 1024; # k  $too_small *= 1024; # k
32    
33  sub count {  sub count {
34          my $file = shift;          my $file = shift;
35          open(my $fh, '<', $file) || die "$file: $!";          open(my $fh, '<', $file) || die "$file: $!";
36    
37          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)};
38    
39          my $t = (stat($file))[9];  #       warn "# $file ", -s $file, " bytes [$t]\n" if $debug;
40          if ( $file =~ m{(\d\d\d\d)-(\d\d)-(\d\d)/(\d\d)(\d\d)} ) {          print STDERR "\n$file\t";
                 $t = new DateTime(  
                         year   => $1,  
                         month  => $2,  
                         day    => $3,  
                         hour   => $4,  
                         minute => $5,  
                 )->epoch;  
         }  
   
         warn "# $file ", -s $file, " bytes [$t]\n" if $debug;  
41    
42          my $cols = <$fh>;          my $cols = <$fh>;
43          $cols =~ s/^\s+//;          $cols =~ s/^\s+//;
# Line 43  sub count { Line 47  sub count {
47          $cp->{$c[$_]} = $_ foreach 0 .. $#c;          $cp->{$c[$_]} = $_ foreach 0 .. $#c;
48          our @r;          our @r;
49          sub c {          sub c {
50                    return $r[ $cp->{$_[0]} ];      # XXX speedup
51    
52                  my $name = shift;                  my $name = shift;
53                  my $n = $cp->{$name};                  my $n = $cp->{$name};
54                  die "no column $name in ",dump( $cp ) unless defined $n;                  die "no column $name in ",dump( $cp ) unless defined $n;
# Line 56  sub count { Line 62  sub count {
62                  @r = split(/\s+/, $_, $#c + 1 );                  @r = split(/\s+/, $_, $#c + 1 );
63    
64                  my $veid = c('VEID');                  my $veid = c('VEID');
65                  $veid =~ s/^0$/hw/;                  next if defined $only_veid && $only_veid != $veid;
66                    $veid =~ s/^0$/_hw_0/;
67    
68                  my $s = c('VSZ');                  my $s = c('VSZ');
69                  if ( $s < $too_small ) {                  my $cmd = c('COMMAND');
70                    if ( ! $by_veid && $cmd =~ $filter ) {
71    
72                            $veid .= '-' . $1;
73                            $vsz->{$t}->{$veid} += $s * 1024;
74                            print STDERR uc(substr($1,0,1));
75    
76                    } elsif ( $s < $too_small || $by_veid ) {
77    
78                          $vsz->{$t}->{$veid}+= $s * 1024;                          $vsz->{$t}->{$veid}+= $s * 1024;
79                            print STDERR ".";
80    
81                  } else {                  } else {
82                          my $cmd = c('COMMAND');  
83                          $cmd =~ s{-.+$}{};                          $cmd =~ s{-.+$}{};
84                          $cmd =~ s{^/\S+/(\w+?)}{$1};                          $cmd =~ s{^/\S+/(\w+?)}{$1};
85                          $cmd =~ s{^\w+ /\S+/(\w+?)}{$1};                          $cmd =~ s{^\w+ /\S+/(\w+?)}{$1};
# Line 71  sub count { Line 88  sub count {
88                          $veid .= '-' . $cmd;                          $veid .= '-' . $cmd;
89                          $veid = substr($veid,0,16);                          $veid = substr($veid,0,16);
90                          $vsz->{$t}->{$veid}+= $s * 1024;                          $vsz->{$t}->{$veid}+= $s * 1024;
91                            print STDERR substr($cmd,0,1);
92                  }                  }
93                  $veids->{$veid}++;                  $veids->{$veid}++;
94          }          }
95    
96  }  }
97    
98  print STDERR "reading ps dumps";  print STDERR "parsing ps with grouping < $too_small k";
99    
100  count $_ foreach @ARGV;  count $_ foreach @ARGV;
101    
102  #print "VSZ: ",dump( $vsz );  #print "VSZ: ",dump( $vsz );
103    
104  my @veids = keys %$veids;  my @veids = sort keys %$veids;
105  warn "# veids = ",dump( sort @veids );  warn "# veids = ",dump( @veids );
106    
107  my $rrd_file = 'ps.rrd';  my $rrd_file = 'ps.rrd';
108  unlink $rrd_file if -e $rrd_file;  unlink $rrd_file if -e $rrd_file;
109    
110    my @t = sort keys %$vsz;
111    
112    print "\ndrawing $#t intervals ", $t[0], " - ", $t[$#t];
113    
114  my $rrd = RRD::Simple->new( file => $rrd_file );  my $rrd = RRD::Simple->new( file => $rrd_file );
115  $rrd->create( 'hour', map { ( $_ => 'GAUGE' ) } @veids );  $rrd->create( map { ( $_ => 'GAUGE' ) } @veids );
116    
117  foreach my $t ( sort keys %$vsz ) {  foreach my $t ( @t ) {
118          print STDERR ".";          print STDERR ".";
119  #       warn "## ",dump( %{ $vsz->{$t} } );  #       warn "## ",dump( %{ $vsz->{$t} } );
120    

Legend:
Removed from v.6  
changed lines
  Added in v.15

  ViewVC Help
Powered by ViewVC 1.1.26