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

Contents of /ps2rrd.pl

Parent Directory Parent Directory | Revision Log Revision Log


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

1 #!/usr/bin/perl
2
3 use warnings;
4 use strict;
5
6 use Data::Dump qw/dump/;
7 use RRD::Simple;
8 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 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;
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 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
37 warn "# $file ", -s $file, " bytes [$t]\n" if $debug;
38
39 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 return $r[ $cp->{$_[0]} ]; # XXX speedup
48
49 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 next if defined $only_veid && $only_veid != $veid;
63 $veid =~ s/^0$/0-hw/;
64
65 my $s = c('VSZ');
66 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;
76 print STDERR ".";
77
78 } else {
79
80 $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 $vsz->{$t}->{$veid}+= $s * 1024;
88 print STDERR substr($cmd,0,1);
89 }
90 $veids->{$veid}++;
91 }
92
93 }
94
95 print STDERR "parsing ps with grouping < $too_small k";
96
97 count $_ foreach @ARGV;
98
99 #print "VSZ: ",dump( $vsz );
100
101 my @veids = sort keys %$veids;
102 warn "# veids = ",dump( @veids );
103
104 my $rrd_file = 'ps.rrd';
105 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 );
112 $rrd->create( map { ( $_ => 'GAUGE' ) } @veids );
113
114 foreach my $t ( @t ) {
115 print STDERR ".";
116 # warn "## ",dump( %{ $vsz->{$t} } );
117
118 eval {
119 $rrd->update($t, map {
120 ( $_ => $vsz->{$t}->{$_} )
121 } @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 width => 500 * $zoom,
133 height => 200 * $zoom,
134 );
135

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26