| 1 | package App::ProcTrends::Commandline; | ||
| 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 App::ProcTrends::RRD; | ||
| 10 | use Data::Dumper; | ||
| 11 | use RRDTool::OO; | ||
| 12 | use File::Path qw/make_path/; | ||
| 13 | use RRDs; | ||
| 14 | |||
| 15 | =head1 NAME | ||
| 16 | |||
| 17 | App::ProcTrends::Commandline - The great new App::ProcTrends::Commandline! | ||
| 18 | |||
| 19 | =head1 VERSION | ||
| 20 | |||
| 21 | Version 0.03 | ||
| 22 | |||
| 23 | =cut | ||
| 24 | |||
| 25 | our $VERSION = '0.03'; | ||
| 26 | |||
| 27 | =head1 SYNOPSIS | ||
| 28 | |||
| 29 | use App::ProcTrends::Commandline; | ||
| 30 | |||
| 31 | my $ref = { | ||
| 32 | start => '-1d', | ||
| 33 | end => 'now' | ||
| 34 | }; | ||
| 35 | |||
| 36 | my $obj = App::ProcTrends::Commandline->new( $ref ); | ||
| 37 | my $value = $obj->generate_table_data(); | ||
| 38 | |||
| 39 | =head1 SUBROUTINES/METHODS | ||
| 40 | |||
| 41 | =head2 new | ||
| 42 | |||
| 43 | The constructor. | ||
| 44 | |||
| 45 | =cut | ||
| 46 | |||
| 47 | sub new { | ||
| 48 | my ( $class, $ref ) = @_; | ||
| 49 | $ref = {} unless( $ref && ref( $ref ) eq 'HASH' ); | ||
| 50 | |||
| 51 | my $cfg = App::ProcTrends::Config->new(); | ||
| 52 | my $rrd = App::ProcTrends::RRD->new(); | ||
| 53 | |||
| 54 | my $self = { | ||
| 55 | start => $cfg->COMMANDLINE_START(), | ||
| 56 | end => $cfg->COMMANDLINE_END(), | ||
| 57 | interval => $cfg->COMMANDLINE_INTERVAL(), | ||
| 58 | procs => $cfg->COMMANDLINE_PROCS(), | ||
| 59 | command => $cfg->COMMANDLINE_COMMAND(), | ||
| 60 | rrd_dir => $cfg->RRD_DIR(), | ||
| 61 | out_dir => $cfg->RRD_DIR(), | ||
| 62 | rrd => $rrd, | ||
| 63 | %{ $ref }, | ||
| 64 | }; | ||
| 65 | |||
| 66 | __PACKAGE__->mk_ro_accessors( keys %{ $self } ); | ||
| 67 | return bless $self, $class; | ||
| 68 | } | ||
| 69 | |||
| 70 | =head2 table_handler | ||
| 71 | |||
| 72 | Generates a table from RRD data | ||
| 73 | |||
| 74 | =cut | ||
| 75 | |||
| 76 | sub table_handler { | ||
| 77 | my $self = shift; | ||
| 78 | |||
| 79 | my $dir = $self->{ rrd_dir }; | ||
| 80 | my $start = $self->{ start }; | ||
| 81 | my $end = $self->{ end }; | ||
| 82 | my $interval = $self->{ interval } * 60; # minutes => seconds | ||
| 83 | my $procs = $self->{ procs }; | ||
| 84 | my @procs = split( /,\s*/, $procs ) if ( $procs ); | ||
| 85 | |||
| 86 | my $result; | ||
| 87 | |||
| 88 | for my $metric ( 'cpu', 'rss' ) { | ||
| 89 | my $path = $dir . "/$metric"; | ||
| 90 | croak "$path does not exist" unless( -d $path ); | ||
| 91 | my $rrds = $self->{ rrd }->find_rrds( $path ); | ||
| 92 | next unless( $rrds ); | ||
| 93 | |||
| 94 | for my $proc ( sort keys %{ $rrds } ) { | ||
| 95 | if ( @procs ) { | ||
| 96 | next unless( grep( /^$proc$/, @procs ) ); | ||
| 97 | } | ||
| 98 | |||
| 99 | my $file = $rrds->{ $proc }; | ||
| 100 | my $rrd_obj = RRDTool::OO->new( file => $file ); | ||
| 101 | $rrd_obj->fetch_start( start => $start, end => $end ); | ||
| 102 | |||
| 103 | # accounts for undefs with respect to start time, but not end time. | ||
| 104 | #$rrd_obj->fetch_skip_undef(); | ||
| 105 | |||
| 106 | # tracks the offset | ||
| 107 | my $next_time = 0; | ||
| 108 | |||
| 109 | while( my ( $time, $value ) = $rrd_obj->fetch_next() ) { | ||
| 110 | $next_time = $time if ( $next_time == 0 ); | ||
| 111 | |||
| 112 | if ( $time == $next_time ) { | ||
| 113 | push @{ $result->{ $metric }->{ $proc } }, { time => $time, value => $value }; | ||
| 114 | $next_time += $interval; | ||
| 115 | } | ||
| 116 | } | ||
| 117 | } | ||
| 118 | } | ||
| 119 | |||
| 120 | return $self->print_table( $result ); | ||
| 121 | } | ||
| 122 | |||
| 123 | =head2 print_table | ||
| 124 | |||
| 125 | Parses a hashref and prints out on the screen. | ||
| 126 | |||
| 127 | =cut | ||
| 128 | |||
| 129 | sub print_table { | ||
| 130 | my ( $self, $ref ) = @_; | ||
| 131 | |||
| 132 | for my $metric ( sort keys %{ $ref } ) { | ||
| 133 | for my $proc ( sort keys %{ $ref->{ $metric } } ) { | ||
| 134 | print "$metric for $proc\n"; | ||
| 135 | print "=" x 50, "\n"; | ||
| 136 | |||
| 137 | for my $hashref ( @{ $ref->{ $metric }->{ $proc } } ) { | ||
| 138 | my ( $time, $value ) = ( $hashref->{ time }, $hashref->{ value } ); | ||
| 139 | $value = "-" unless( $value ); | ||
| 140 | my $t_string = localtime( $time ); | ||
| 141 | print $t_string, " " x 20, $value, "\n"; | ||
| 142 | } | ||
| 143 | print "\n"; | ||
| 144 | } | ||
| 145 | } | ||
| 146 | return 1; | ||
| 147 | } | ||
| 148 | |||
| 149 | =head2 list_handler | ||
| 150 | |||
| 151 | Return $ref->{ 'cpu', 'rss' } = [ procs ] | ||
| 152 | |||
| 153 | =cut | ||
| 154 | |||
| 155 | sub list_handler { | ||
| 156 | my $self = shift; | ||
| 157 | |||
| 158 | my $rrd_dir = $self->{ rrd_dir }; | ||
| 159 | my $rrd = $self->{ rrd }; | ||
| 160 | my $result; | ||
| 161 | |||
| 162 | for my $metric ( 'cpu', 'rss' ) { | ||
| 163 | my $path = $rrd_dir . "/$metric"; | ||
| 164 | |||
| 165 | my $ref = $rrd->find_rrds( $path ); | ||
| 166 | $result->{ $metric } = [ sort keys %{ $ref } ]; | ||
| 167 | } | ||
| 168 | return $self->print_list( $result ); | ||
| 169 | } | ||
| 170 | |||
| 171 | =head2 print_list | ||
| 172 | |||
| 173 | Prints the list (output from list method) on screen | ||
| 174 | |||
| 175 | =cut | ||
| 176 | |||
| 177 | sub print_list { | ||
| 178 | my ( $self, $arg ) = @_; | ||
| 179 | |||
| 180 | for my $metric ( 'cpu', 'rss' ) { | ||
| 181 | my @procs = @{ $arg->{ $metric } }; | ||
| 182 | print "$metric processes:\n"; | ||
| 183 | print "=" x 19 . "\n"; | ||
| 184 | |||
| 185 | for my $proc ( @procs ) { | ||
| 186 | print $proc, "\n"; | ||
| 187 | } | ||
| 188 | print "\n"; | ||
| 189 | } | ||
| 190 | return 1; | ||
| 191 | } | ||
| 192 | |||
| 193 | =head2 img_handler | ||
| 194 | |||
| 195 | Generates png image files using object params, and store them in $self->{ out_dir } | ||
| 196 | |||
| 197 | =cut | ||
| 198 | |||
| 199 | sub img_handler { | ||
| 200 | my $self = shift; | ||
| 201 | |||
| 202 | my $rrd_dir = $self->{ rrd_dir }; | ||
| 203 | my $out_dir = $self->{ out_dir }; | ||
| 204 | my $rrd = $self->{ rrd }; | ||
| 205 | my $procs = $self->{ procs }; | ||
| 206 | my @procs = split( /,\s*/, $procs ) if ( $procs ); | ||
| 207 | my $start = $self->{ start }; | ||
| 208 | my $end = $self->{ end }; | ||
| 209 | |||
| 210 | make_path( $out_dir ) unless( -d $out_dir ); | ||
| 211 | |||
| 212 | for my $metric ( 'cpu', 'rss' ) { | ||
| 213 | my $path = $rrd_dir . "/$metric"; | ||
| 214 | my $ref = $rrd->find_rrds( $path ); | ||
| 215 | |||
| 216 | for my $process ( keys %{ $ref } ) { | ||
| 217 | if ( @procs ) { | ||
| 218 | next unless ( grep( /^$process$/, @procs ) ); | ||
| 219 | } | ||
| 220 | my $rrdfile = $ref->{ $process }; | ||
| 221 | my $filename = $out_dir . "/${metric}_$process.png"; | ||
| 222 | my @params; | ||
| 223 | |||
| 224 | push @params, $filename; | ||
| 225 | push @params, "-s $start"; | ||
| 226 | push @params, "-e $end"; | ||
| 227 | push @params, "DEF:mydata=$rrdfile:$process:AVERAGE"; | ||
| 228 | push @params, "AREA:mydata#0000FF:foo"; | ||
| 229 | RRDs::graph( @params ); | ||
| 230 | } | ||
| 231 | } | ||
| 232 | return 1; | ||
| 233 | } | ||
| 234 | |||
| 235 | =head1 AUTHOR | ||
| 236 | |||
| 237 | Satoshi Yagi, C<< <satoshi.yagi at yahoo.com> >> | ||
| 238 | |||
| 239 | =head1 BUGS | ||
| 240 | |||
| 241 | Please report any bugs or feature requests to C<bug-app-proctrends-cron at rt.cpan.org>, or through | ||
| 242 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=App-ProcTrends-Cron>. I will be notified, and then you'll | ||
| 243 | automatically be notified of progress on your bug as I make changes. | ||
| 244 | |||
| 245 | |||
| 246 | |||
| 247 | |||
| 248 | =head1 SUPPORT | ||
| 249 | |||
| 250 | You can find documentation for this module with the perldoc command. | ||
| 251 | |||
| 252 | perldoc App::ProcTrends::Commandline | ||
| 253 | |||
| 254 | |||
| 255 | You can also look for information at: | ||
| 256 | |||
| 257 | =over 4 | ||
| 258 | |||
| 259 | =item * RT: CPAN's request tracker (report bugs here) | ||
| 260 | |||
| 261 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=App-ProcTrends-Cron> | ||
| 262 | |||
| 263 | =item * AnnoCPAN: Annotated CPAN documentation | ||
| 264 | |||
| 265 | L<http://annocpan.org/dist/App-ProcTrends-Cron> | ||
| 266 | |||
| 267 | =item * CPAN Ratings | ||
| 268 | |||
| 269 | L<http://cpanratings.perl.org/d/App-ProcTrends-Cron> | ||
| 270 | |||
| 271 | =item * Search CPAN | ||
| 272 | |||
| 273 | L<http://search.cpan.org/dist/App-ProcTrends-Cron/> | ||
| 274 | |||
| 275 | =back | ||
| 276 | |||
| 277 | |||
| 278 | =head1 ACKNOWLEDGEMENTS | ||
| 279 | |||
| 280 | |||
| 281 | =head1 LICENSE AND COPYRIGHT | ||
| 282 | |||
| 283 | Copyright 2013 Satoshi Yagi. | ||
| 284 | |||
| 285 | This program is free software; you can redistribute it and/or modify it | ||
| 286 | under the terms of either: the GNU General Public License as published | ||
| 287 | by the Free Software Foundation; or the Artistic License. | ||
| 288 | |||
| 289 | See http://dev.perl.org/licenses/ for more information. | ||
| 290 | |||
| 291 | |||
| 292 | =cut | ||
| 293 | |||
| 294 | 1; # End of App::ProcTrends::Commandline |