/[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

Annotation of /ps2rrd.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 13 - (hide annotations)
Fri Jul 17 16:41:12 2009 UTC (10 years ago) by dpavlin
File MIME type: text/plain
File size: 2649 byte(s)
added filter regex to always track processes by name

1 dpavlin 2 #!/usr/bin/perl
2    
3     use warnings;
4     use strict;
5    
6     use Data::Dump qw/dump/;
7     use RRD::Simple;
8 dpavlin 9 use POSIX qw/mktime/;
9 dpavlin 11 use Getopt::Long;
10 dpavlin 2
11 dpavlin 6 my $debug = 0;
12     my $zoom = 2;
13    
14 dpavlin 11 my $only_veid;
15     my $too_small = 64; # M
16 dpavlin 13 my $filter = qr{(apache|mysql|postgres|cgi-bin|SIP)};
17 dpavlin 11
18     GetOptions(
19     'debug!' => \$debug,
20     'zoom=i' => \$zoom,
21     'veid=i' => \$only_veid,
22     'size=i' => \$too_small,
23     );
24    
25    
26 dpavlin 2 my $veids;
27     my $vsz;
28    
29     $too_small *= 1024; # k
30    
31     sub count {
32     my $file = shift;
33     open(my $fh, '<', $file) || die "$file: $!";
34    
35 dpavlin 9 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)};
36 dpavlin 6
37     warn "# $file ", -s $file, " bytes [$t]\n" if $debug;
38    
39 dpavlin 2 my $cols = <$fh>;
40     $cols =~ s/^\s+//;
41     my @c = split(/\s+/,$cols);
42     # warn "# c = ",dump( @c );
43     our $cp;
44     $cp->{$c[$_]} = $_ foreach 0 .. $#c;
45     our @r;
46     sub c {
47 dpavlin 8 return $r[ $cp->{$_[0]} ]; # XXX speedup
48    
49 dpavlin 2 my $name = shift;
50     my $n = $cp->{$name};
51     die "no column $name in ",dump( $cp ) unless defined $n;
52     return $r[$n];
53     }
54    
55     while(<$fh>) {
56     chomp;
57     s/^\s+//g;
58     s/\s+$//g;
59     @r = split(/\s+/, $_, $#c + 1 );
60    
61     my $veid = c('VEID');
62 dpavlin 11 next if defined $only_veid && $only_veid != $veid;
63     $veid =~ s/^0$/0-hw/;
64 dpavlin 2
65     my $s = c('VSZ');
66 dpavlin 13 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 dpavlin 6 $vsz->{$t}->{$veid}+= $s * 1024;
76 dpavlin 10 print STDERR ".";
77 dpavlin 13
78 dpavlin 2 } else {
79 dpavlin 13
80 dpavlin 2 $cmd =~ s{-.+$}{};
81     $cmd =~ s{^/\S+/(\w+?)}{$1};
82     $cmd =~ s{^\w+ /\S+/(\w+?)}{$1};
83     $cmd =~ s{:?\s+.*$}{};
84     $cmd =~ s{\W+}{_}g;
85     $veid .= '-' . $cmd;
86     $veid = substr($veid,0,16);
87 dpavlin 6 $vsz->{$t}->{$veid}+= $s * 1024;
88 dpavlin 10 print STDERR substr($cmd,0,1);
89 dpavlin 2 }
90     $veids->{$veid}++;
91     }
92    
93     }
94    
95 dpavlin 11 print STDERR "parsing ps with grouping < $too_small k";
96 dpavlin 6
97 dpavlin 2 count $_ foreach @ARGV;
98    
99 dpavlin 6 #print "VSZ: ",dump( $vsz );
100 dpavlin 2
101 dpavlin 11 my @veids = sort keys %$veids;
102     warn "# veids = ",dump( @veids );
103 dpavlin 2
104 dpavlin 6 my $rrd_file = 'ps.rrd';
105     unlink $rrd_file if -e $rrd_file;
106    
107 dpavlin 11 my @t = sort keys %$vsz;
108    
109     print "\ndrawing $#t intervals ", $t[0], " - ", $t[$#t];
110    
111 dpavlin 6 my $rrd = RRD::Simple->new( file => $rrd_file );
112 dpavlin 12 $rrd->create( map { ( $_ => 'GAUGE' ) } @veids );
113 dpavlin 2
114 dpavlin 11 foreach my $t ( @t ) {
115 dpavlin 6 print STDERR ".";
116     # warn "## ",dump( %{ $vsz->{$t} } );
117 dpavlin 2
118     eval {
119 dpavlin 6 $rrd->update($t, map {
120     ( $_ => $vsz->{$t}->{$_} )
121 dpavlin 2 } @veids );
122     };
123     warn "SKIP $t: $@\n" if $@;
124     }
125    
126     $rrd->graph(
127     sources => [ sort @veids ],
128     source_drawtypes => [ map { $_ ? 'STACK' : 'AREA' } 0 .. $#veids ],
129     periods => [ qw/hour 6hour 12hour day week month year 3years/ ],
130     extended_legend => 1,
131     title => "memory > $too_small K",
132 dpavlin 6 width => 500 * $zoom,
133     height => 200 * $zoom,
134 dpavlin 2 );
135    

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26