| 1 | package App::ProcTrends::RRD; | ||
| 2 | |||
| 3 | use 5.006; | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | use base qw(Class::Accessor::Fast); | ||
| 7 | use Carp; | ||
| 8 | use App::ProcTrends::Config; | ||
| 9 | use RRDs; | ||
| 10 | use Data::Dumper; | ||
| 11 | use File::Temp qw/tempfile/; | ||
| 12 | use File::Slurp; | ||
| 13 | |||
| 14 | =head1 NAME | ||
| 15 | |||
| 16 | App::ProcTrends::RRD - The great new App::ProcTrends::RRD! | ||
| 17 | |||
| 18 | =head1 VERSION | ||
| 19 | |||
| 20 | Version 0.03 | ||
| 21 | |||
| 22 | =cut | ||
| 23 | |||
| 24 | our $VERSION = '0.03'; | ||
| 25 | |||
| 26 | =head1 SYNOPSIS | ||
| 27 | |||
| 28 | use App::ProcTrends::RRD; | ||
| 29 | |||
| 30 | my $obj = App::ProcTrends::RRD->new(); | ||
| 31 | $obj->do_something(); | ||
| 32 | |||
| 33 | =head1 SUBROUTINES/METHODS | ||
| 34 | |||
| 35 | =head2 new | ||
| 36 | |||
| 37 | The constructor | ||
| 38 | |||
| 39 | =cut | ||
| 40 | |||
| 41 | sub new { | ||
| 42 | my ( $class, $ref ) = @_; | ||
| 43 | |||
| 44 | $ref = {} unless( $ref && ref( $ref ) eq 'HASH' ); | ||
| 45 | my $cfg = App::ProcTrends::Config->new(); | ||
| 46 | |||
| 47 | my $self = { | ||
| 48 | rrd_dir => $cfg->RRD_DIR(), | ||
| 49 | start => $cfg->RRD_START(), | ||
| 50 | end => $cfg->RRD_END(), | ||
| 51 | line => $cfg->RRD_LINE(), | ||
| 52 | stack => $cfg->RRD_STACK(), | ||
| 53 | imgformat => $cfg->RRD_IMGFORMAT(), | ||
| 54 | title => $cfg->RRD_TITLE(), | ||
| 55 | width => $cfg->RRD_WIDTH(), | ||
| 56 | height => $cfg->RRD_HEIGHT(), | ||
| 57 | %{ $ref }, | ||
| 58 | }; | ||
| 59 | |||
| 60 | __PACKAGE__->mk_ro_accessors( keys %{ $self } ); | ||
| 61 | return bless $self, $class; | ||
| 62 | } | ||
| 63 | |||
| 64 | =head2 find_rrds | ||
| 65 | |||
| 66 | Look for *.rrd files in a particular directory | ||
| 67 | Notes: although this is an object method, it doesn't use object internals | ||
| 68 | |||
| 69 | =cut | ||
| 70 | |||
| 71 | sub find_rrds { | ||
| 72 | my ( $self, $dir ) = @_; | ||
| 73 | |||
| 74 | my $ref; | ||
| 75 | opendir( my $dh, $dir ) or return; | ||
| 76 | while( my $file = readdir $dh ) { | ||
| 77 | next unless( $file =~ m/(.*?)\.rrd$/ ); | ||
| 78 | my $procname = $1; | ||
| 79 | my $filename = $dir . "/$file"; | ||
| 80 | $ref->{ $procname } = $filename; | ||
| 81 | } | ||
| 82 | closedir $dh; | ||
| 83 | |||
| 84 | return $ref; | ||
| 85 | } | ||
| 86 | |||
| 87 | =head2 gen_image | ||
| 88 | |||
| 89 | Generates an image then return the graph data in a scalar. Because RRDs is | ||
| 90 | an XS module, this method actually creates a temp file, dump graph data then | ||
| 91 | slurp back in. | ||
| 92 | |||
| 93 | =cut | ||
| 94 | |||
| 95 | sub gen_image { | ||
| 96 | my ( $self, $metric, $process ) = @_; Arguments: metric, arrayref of processes | ||
| 97 | Returns: a scalar containing graph data. 0 on failure | ||
| 98 | |||
| 99 | return 0 unless( $metric && $process ); | ||
| 100 | |||
| 101 | my $rrd_dir = $self->{ rrd_dir }; | ||
| 102 | my $path = $rrd_dir . "/$metric"; | ||
| 103 | return 0 unless( -d $path ); | ||
| 104 | |||
| 105 | my $rrds = $self->find_rrds( $path ); | ||
| 106 | my $rrdfile = $rrds->{ $process }; | ||
| 107 | return 0 unless( $rrdfile ); | ||
| 108 | |||
| 109 | my $title = $self->{ title }; | ||
| 110 | $title =~ s/<%METRIC%>/$metric/; | ||
| 111 | $title =~ s/<%PROCESS%>/$process/; | ||
| 112 | my $line = $self->{ line }; | ||
| 113 | my $stack = $self->{ stack }; | ||
| 114 | |||
| 115 | my @params; | ||
| 116 | push @params, "--start", $self->{ start }; | ||
| 117 | push @params, "--end", $self->{ end }; | ||
| 118 | push @params, "--imgformat", $self->{ imgformat }; | ||
| 119 | push @params, "--width", $self->{ width }; | ||
| 120 | push @params, "--height", $self->{ height }; | ||
| 121 | push @params, "--title", $title; | ||
| 122 | push @params, "--alt-autoscale"; | ||
| 123 | push @params, "DEF:$process=$rrdfile:$process:AVERAGE"; | ||
| 124 | push @params, "$line:$process#FF0000:$process:$stack"; | ||
| 125 | push @params, "GPRINT:$process:AVERAGE:Avg %-8.0lf"; | ||
| 126 | |||
| 127 | my ( $fh, $filename ) = tempfile( DIR => $rrd_dir ); | ||
| 128 | RRDs::graph( $filename, @params ); | ||
| 129 | my $output = read_file( $filename ); | ||
| 130 | unlink( $filename ); | ||
| 131 | ( $output ) ? return $output : return 0; | ||
| 132 | } | ||
| 133 | |||
| 134 | =head2 gen_group_image | ||
| 135 | |||
| 136 | Generates an image from bunch of processes | ||
| 137 | |||
| 138 | =cut | ||
| 139 | |||
| 140 | sub gen_group_image { | ||
| 141 | my ( $self, $metric, $procs ) = @_; | ||
| 142 | |||
| 143 | return 0 unless( $metric && $procs && ref( $procs ) eq 'ARRAY' ); | ||
| 144 | |||
| 145 | my $rrd_dir = $self->{ rrd_dir }; | ||
| 146 | my $path = $rrd_dir . "/$metric"; | ||
| 147 | return 0 unless( -d $path ); | ||
| 148 | |||
| 149 | my $rrds = $self->find_rrds( $path ); | ||
| 150 | |||
| 151 | my $title = $self->{ title }; | ||
| 152 | $title =~ s/<%METRIC%>/$metric/; | ||
| 153 | |||
| 154 | $title =~ s/<%PROCESS%>/processes/; | ||
| 155 | my $line = $self->{ line }; | ||
| 156 | my $stack = $self->{ stack }; | ||
| 157 | |||
| 158 | my @params; | ||
| 159 | push @params, "--start", $self->{ start }; | ||
| 160 | push @params, "--end", $self->{ end }; | ||
| 161 | push @params, "--imgformat", $self->{ imgformat }; | ||
| 162 | push @params, "--width", $self->{ width }; | ||
| 163 | push @params, "--height", $self->{ height }; | ||
| 164 | push @params, "--title", $title; | ||
| 165 | push @params, "--alt-autoscale"; | ||
| 166 | |||
| 167 | for my $proc ( @{ $procs } ) { | ||
| 168 | my $rrdfile = $rrds->{ $proc }; | ||
| 169 | return 0 unless( $rrdfile ); | ||
| 170 | |||
| 171 | push @params, "DEF:$proc=$rrdfile:$proc:AVERAGE"; | ||
| 172 | push @params, "$line:$proc#FF0000:$proc:$stack"; | ||
| 173 | push @params, "GPRINT:$proc:AVERAGE:Avg %-8.0lf"; | ||
| 174 | } | ||
| 175 | |||
| 176 | my ( $fh, $filename ) = tempfile( DIR => $rrd_dir ); | ||
| 177 | RRDs::graph( $filename, @params ); | ||
| 178 | my $output = read_file( $filename ); | ||
| 179 | unlink( $filename ); | ||
| 180 | ( $output ) ? return $output : return 0; | ||
| 181 | } | ||
| 182 | |||
| 183 | =head1 AUTHOR | ||
| 184 | |||
| 185 | Satoshi Yagi, C<< <satoshi.yagi at yahoo.com> >> | ||
| 186 | |||
| 187 | =head1 BUGS | ||
| 188 | |||
| 189 | Please report any bugs or feature requests to C<bug-app-proctrends-cron at rt.cpan.org>, or through | ||
| 190 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=App-ProcTrends-Cron>. I will be notified, and then you'll | ||
| 191 | automatically be notified of progress on your bug as I make changes. | ||
| 192 | |||
| 193 | |||
| 194 | |||
| 195 | |||
| 196 | =head1 SUPPORT | ||
| 197 | |||
| 198 | You can find documentation for this module with the perldoc command. | ||
| 199 | |||
| 200 | perldoc App::ProcTrends::RRD | ||
| 201 | |||
| 202 | |||
| 203 | You can also look for information at: | ||
| 204 | |||
| 205 | =over 4 | ||
| 206 | |||
| 207 | =item * RT: CPAN's request tracker (report bugs here) | ||
| 208 | |||
| 209 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=App-ProcTrends-Cron> | ||
| 210 | |||
| 211 | =item * AnnoCPAN: Annotated CPAN documentation | ||
| 212 | |||
| 213 | L<http://annocpan.org/dist/App-ProcTrends-Cron> | ||
| 214 | |||
| 215 | =item * CPAN Ratings | ||
| 216 | |||
| 217 | L<http://cpanratings.perl.org/d/App-ProcTrends-Cron> | ||
| 218 | |||
| 219 | =item * Search CPAN | ||
| 220 | |||
| 221 | L<http://search.cpan.org/dist/App-ProcTrends-Cron/> | ||
| 222 | |||
| 223 | =back | ||
| 224 | |||
| 225 | |||
| 226 | =head1 ACKNOWLEDGEMENTS | ||
| 227 | |||
| 228 | |||
| 229 | =head1 LICENSE AND COPYRIGHT | ||
| 230 | |||
| 231 | Copyright 2013 Satoshi Yagi. | ||
| 232 | |||
| 233 | This program is free software; you can redistribute it and/or modify it | ||
| 234 | under the terms of either: the GNU General Public License as published | ||
| 235 | by the Free Software Foundation; or the Artistic License. | ||
| 236 | |||
| 237 | See http://dev.perl.org/licenses/ for more information. | ||
| 238 | |||
| 239 | |||
| 240 | =cut | ||
| 241 | |||
| 242 | 1; # End of App::ProcTrends::RRD |