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

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26