/[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 14 - (hide annotations)
Fri Jul 17 18:35:34 2009 UTC (10 years 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 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 dpavlin 14 # warn "# $file ", -s $file, " bytes [$t]\n" if $debug;
38     print STDERR "\n$file\t";
39 dpavlin 6
40 dpavlin 2 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 dpavlin 8 return $r[ $cp->{$_[0]} ]; # XXX speedup
49    
50 dpavlin 2 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 dpavlin 11 next if defined $only_veid && $only_veid != $veid;
64 dpavlin 14 $veid =~ s/^0$/_hw_0/;
65 dpavlin 2
66     my $s = c('VSZ');
67 dpavlin 13 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 dpavlin 6 $vsz->{$t}->{$veid}+= $s * 1024;
77 dpavlin 10 print STDERR ".";
78 dpavlin 13
79 dpavlin 2 } else {
80 dpavlin 13
81 dpavlin 2 $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 dpavlin 6 $vsz->{$t}->{$veid}+= $s * 1024;
89 dpavlin 10 print STDERR substr($cmd,0,1);
90 dpavlin 2 }
91     $veids->{$veid}++;
92     }
93    
94     }
95    
96 dpavlin 11 print STDERR "parsing ps with grouping < $too_small k";
97 dpavlin 6
98 dpavlin 2 count $_ foreach @ARGV;
99    
100 dpavlin 6 #print "VSZ: ",dump( $vsz );
101 dpavlin 2
102 dpavlin 11 my @veids = sort keys %$veids;
103     warn "# veids = ",dump( @veids );
104 dpavlin 2
105 dpavlin 6 my $rrd_file = 'ps.rrd';
106     unlink $rrd_file if -e $rrd_file;
107    
108 dpavlin 11 my @t = sort keys %$vsz;
109    
110     print "\ndrawing $#t intervals ", $t[0], " - ", $t[$#t];
111    
112 dpavlin 6 my $rrd = RRD::Simple->new( file => $rrd_file );
113 dpavlin 12 $rrd->create( map { ( $_ => 'GAUGE' ) } @veids );
114 dpavlin 2
115 dpavlin 11 foreach my $t ( @t ) {
116 dpavlin 6 print STDERR ".";
117     # warn "## ",dump( %{ $vsz->{$t} } );
118 dpavlin 2
119     eval {
120 dpavlin 6 $rrd->update($t, map {
121     ( $_ => $vsz->{$t}->{$_} )
122 dpavlin 2 } @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 dpavlin 6 width => 500 * $zoom,
134     height => 200 * $zoom,
135 dpavlin 2 );
136    

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26