/[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 13 by dpavlin, Fri Jul 17 16:41:12 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 $too_small = 64;     # M
16    my $filter = qr{(apache|mysql|postgres|cgi-bin|SIP)};
17    
18    GetOptions(
19            'debug!'   => \$debug,
20            'zoom=i'   => \$zoom,
21            'veid=i'   => \$only_veid,
22            'size=i'   => \$too_small,
23    );
24    
25    
26  my $veids;  my $veids;
27  my $vsz;  my $vsz;
28    
 my $too_small = 16;     # M  
29  $too_small *= 1024; # k  $too_small *= 1024; # k
30    
31  sub count {  sub count {
32          my $file = shift;          my $file = shift;
33          open(my $fh, '<', $file) || die "$file: $!";          open(my $fh, '<', $file) || die "$file: $!";
34    
35          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;  
         }  
36    
37          warn "# $file ", -s $file, " bytes [$t]\n" if $debug;          warn "# $file ", -s $file, " bytes [$t]\n" if $debug;
38    
# Line 43  sub count { Line 44  sub count {
44          $cp->{$c[$_]} = $_ foreach 0 .. $#c;          $cp->{$c[$_]} = $_ foreach 0 .. $#c;
45          our @r;          our @r;
46          sub c {          sub c {
47                    return $r[ $cp->{$_[0]} ];      # XXX speedup
48    
49                  my $name = shift;                  my $name = shift;
50                  my $n = $cp->{$name};                  my $n = $cp->{$name};
51                  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 59  sub count {
59                  @r = split(/\s+/, $_, $#c + 1 );                  @r = split(/\s+/, $_, $#c + 1 );
60    
61                  my $veid = c('VEID');                  my $veid = c('VEID');
62                  $veid =~ s/^0$/hw/;                  next if defined $only_veid && $only_veid != $veid;
63                    $veid =~ s/^0$/0-hw/;
64    
65                  my $s = c('VSZ');                  my $s = c('VSZ');
66                  if ( $s < $too_small ) {                  my $cmd = c('COMMAND');
67                    if ( $cmd =~ $filter ) {
68    
69                            $veid .= '-' . $1;
70                            $vsz->{$t}->{$veid} += $s * 1024;
71                            print STDERR uc(substr($1,0,1));
72    
73                    } elsif ( $s < $too_small ) {
74    
75                          $vsz->{$t}->{$veid}+= $s * 1024;                          $vsz->{$t}->{$veid}+= $s * 1024;
76                            print STDERR ".";
77    
78                  } else {                  } else {
79                          my $cmd = c('COMMAND');  
80                          $cmd =~ s{-.+$}{};                          $cmd =~ s{-.+$}{};
81                          $cmd =~ s{^/\S+/(\w+?)}{$1};                          $cmd =~ s{^/\S+/(\w+?)}{$1};
82                          $cmd =~ s{^\w+ /\S+/(\w+?)}{$1};                          $cmd =~ s{^\w+ /\S+/(\w+?)}{$1};
# Line 71  sub count { Line 85  sub count {
85                          $veid .= '-' . $cmd;                          $veid .= '-' . $cmd;
86                          $veid = substr($veid,0,16);                          $veid = substr($veid,0,16);
87                          $vsz->{$t}->{$veid}+= $s * 1024;                          $vsz->{$t}->{$veid}+= $s * 1024;
88                            print STDERR substr($cmd,0,1);
89                  }                  }
90                  $veids->{$veid}++;                  $veids->{$veid}++;
91          }          }
92    
93  }  }
94    
95  print STDERR "reading ps dumps";  print STDERR "parsing ps with grouping < $too_small k";
96    
97  count $_ foreach @ARGV;  count $_ foreach @ARGV;
98    
99  #print "VSZ: ",dump( $vsz );  #print "VSZ: ",dump( $vsz );
100    
101  my @veids = keys %$veids;  my @veids = sort keys %$veids;
102  warn "# veids = ",dump( sort @veids );  warn "# veids = ",dump( @veids );
103    
104  my $rrd_file = 'ps.rrd';  my $rrd_file = 'ps.rrd';
105  unlink $rrd_file if -e $rrd_file;  unlink $rrd_file if -e $rrd_file;
106    
107    my @t = sort keys %$vsz;
108    
109    print "\ndrawing $#t intervals ", $t[0], " - ", $t[$#t];
110    
111  my $rrd = RRD::Simple->new( file => $rrd_file );  my $rrd = RRD::Simple->new( file => $rrd_file );
112  $rrd->create( 'hour', map { ( $_ => 'GAUGE' ) } @veids );  $rrd->create( map { ( $_ => 'GAUGE' ) } @veids );
113    
114  foreach my $t ( sort keys %$vsz ) {  foreach my $t ( @t ) {
115          print STDERR ".";          print STDERR ".";
116  #       warn "## ",dump( %{ $vsz->{$t} } );  #       warn "## ",dump( %{ $vsz->{$t} } );
117    

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

  ViewVC Help
Powered by ViewVC 1.1.26