/[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 14 - (show annotations)
Fri Jul 17 18:35:34 2009 UTC (14 years, 9 months ago) by dpavlin
File MIME type: text/plain
File size: 2678 byte(s)
print filename to display days in lines
rename hardware to _hw_0 to sort last

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 print STDERR "\n$file\t";
39
40 my $cols = <$fh>;
41 $cols =~ s/^\s+//;
42 my @c = split(/\s+/,$cols);
43 # warn "# c = ",dump( @c );
44 our $cp;
45 $cp->{$c[$_]} = $_ foreach 0 .. $#c;
46 our @r;
47 sub c {
48 return $r[ $cp->{$_[0]} ]; # XXX speedup
49
50 my $name = shift;
51 my $n = $cp->{$name};
52 die "no column $name in ",dump( $cp ) unless defined $n;
53 return $r[$n];
54 }
55
56 while(<$fh>) {
57 chomp;
58 s/^\s+//g;
59 s/\s+$//g;
60 @r = split(/\s+/, $_, $#c + 1 );
61
62 my $veid = c('VEID');
63 next if defined $only_veid && $only_veid != $veid;
64 $veid =~ s/^0$/_hw_0/;
65
66 my $s = c('VSZ');
67 my $cmd = c('COMMAND');
68 if ( $cmd =~ $filter ) {
69
70 $veid .= '-' . $1;
71 $vsz->{$t}->{$veid} += $s * 1024;
72 print STDERR uc(substr($1,0,1));
73
74 } elsif ( $s < $too_small ) {
75
76 $vsz->{$t}->{$veid}+= $s * 1024;
77 print STDERR ".";
78
79 } else {
80
81 $cmd =~ s{-.+$}{};
82 $cmd =~ s{^/\S+/(\w+?)}{$1};
83 $cmd =~ s{^\w+ /\S+/(\w+?)}{$1};
84 $cmd =~ s{:?\s+.*$}{};
85 $cmd =~ s{\W+}{_}g;
86 $veid .= '-' . $cmd;
87 $veid = substr($veid,0,16);
88 $vsz->{$t}->{$veid}+= $s * 1024;
89 print STDERR substr($cmd,0,1);
90 }
91 $veids->{$veid}++;
92 }
93
94 }
95
96 print STDERR "parsing ps with grouping < $too_small k";
97
98 count $_ foreach @ARGV;
99
100 #print "VSZ: ",dump( $vsz );
101
102 my @veids = sort keys %$veids;
103 warn "# veids = ",dump( @veids );
104
105 my $rrd_file = 'ps.rrd';
106 unlink $rrd_file if -e $rrd_file;
107
108 my @t = sort keys %$vsz;
109
110 print "\ndrawing $#t intervals ", $t[0], " - ", $t[$#t];
111
112 my $rrd = RRD::Simple->new( file => $rrd_file );
113 $rrd->create( map { ( $_ => 'GAUGE' ) } @veids );
114
115 foreach my $t ( @t ) {
116 print STDERR ".";
117 # warn "## ",dump( %{ $vsz->{$t} } );
118
119 eval {
120 $rrd->update($t, map {
121 ( $_ => $vsz->{$t}->{$_} )
122 } @veids );
123 };
124 warn "SKIP $t: $@\n" if $@;
125 }
126
127 $rrd->graph(
128 sources => [ sort @veids ],
129 source_drawtypes => [ map { $_ ? 'STACK' : 'AREA' } 0 .. $#veids ],
130 periods => [ qw/hour 6hour 12hour day week month year 3years/ ],
131 extended_legend => 1,
132 title => "memory > $too_small K",
133 width => 500 * $zoom,
134 height => 200 * $zoom,
135 );
136

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26