| 1 | package App::ProcTrends::Cron; | ||
| 2 | |||
| 3 | use 5.006; | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | use App::ProcTrends::Config; | ||
| 7 | use base qw(Class::Accessor::Fast); | ||
| 8 | use Carp; | ||
| 9 | use Data::Dumper; | ||
| 10 | use File::Path qw/make_path/; | ||
| 11 | use RRDs; | ||
| 12 | use RRD::Simple; | ||
| 13 | use autodie; | ||
| 14 | use Log::Log4perl qw/:easy/; | ||
| 15 | use App::ProcTrends::RRD; | ||
| 16 | |||
| 17 | =head1 NAME | ||
| 18 | |||
| 19 | App::ProcTrends::Cron - The great new App::ProcTrends::Cron! | ||
| 20 | |||
| 21 | =head1 VERSION | ||
| 22 | |||
| 23 | Version 0.03 | ||
| 24 | |||
| 25 | =cut | ||
| 26 | |||
| 27 | our $VERSION = '0.03'; | ||
| 28 | |||
| 29 | =head1 SYNOPSIS | ||
| 30 | |||
| 31 | use App::ProcTrends::Cron; | ||
| 32 | |||
| 33 | my $ref = { | ||
| 34 | rrd_dir => '/foo', | ||
| 35 | }; | ||
| 36 | my $obj = App::ProcTrends::Cron->new( $ref ); | ||
| 37 | my $data = $obj->run_ps(); | ||
| 38 | my $rc = $obj->store_rrd( $data ); | ||
| 39 | |||
| 40 | =head1 SUBROUTINES/METHODS | ||
| 41 | |||
| 42 | =head2 new | ||
| 43 | |||
| 44 | The constructor. Takes a hashref to override defaults. | ||
| 45 | |||
| 46 | =cut | ||
| 47 | |||
| 48 | sub new { | ||
| 49 | my ( $class, $ref ) = @_; | ||
| 50 | $ref = {} unless( ref( $ref ) eq 'HASH' ); | ||
| 51 | |||
| 52 | my $cfg = App::ProcTrends::Config->new(); | ||
| 53 | my $rrd = App::ProcTrends::RRD->new(); | ||
| 54 | |||
| 55 | my $self = { | ||
| 56 | rrd_dir => $cfg->RRD_DIR(), | ||
| 57 | timeout => $cfg->CRON_TIMEOUT(), | ||
| 58 | ps_cmd => $cfg->CRON_PS_CMD(), | ||
| 59 | cpu_cores => $cfg->CRON_CPU_CORES(), | ||
| 60 | cpu_threshold => $cfg->CRON_CPU_THRESHOLD(), | ||
| 61 | rss_threshold => $cfg->CRON_RSS_THRESHOLD(), | ||
| 62 | rss_unit => $cfg->CRON_RSS_UNIT(), | ||
| 63 | rrd_rra => $cfg->CRON_RRD_RRA(), | ||
| 64 | rrd_ds => $cfg->CRON_RRD_DS(), | ||
| 65 | debug => 0, | ||
| 66 | rrd => $rrd, | ||
| 67 | %{ $ref }, | ||
| 68 | }; | ||
| 69 | |||
| 70 | bless $self, $class; | ||
| 71 | |||
| 72 | alarm( $self->{ timeout } ); | ||
| 73 | Log::Log4perl->easy_init($DEBUG) if ( $self->{ debug } ); | ||
| 74 | $self->set_signal_handlers(); | ||
| 75 | |||
| 76 | __PACKAGE__->mk_ro_accessors( keys %{ $self } ); | ||
| 77 | return $self; | ||
| 78 | } | ||
| 79 | |||
| 80 | =head2 DESTROY | ||
| 81 | |||
| 82 | Destructor. Alarm should be cleared upon exit. | ||
| 83 | |||
| 84 | =cut | ||
| 85 | |||
| 86 | sub DESTROY { | ||
| 87 | my ($self) = @_; | ||
| 88 | alarm(0); # clearing alarm | ||
| 89 | } | ||
| 90 | |||
| 91 | =head2 run_ps | ||
| 92 | |||
| 93 | Driver method to execute ps, parses the output and return data structure | ||
| 94 | |||
| 95 | =cut | ||
| 96 | |||
| 97 | sub run_ps { | ||
| 98 | my $self = shift; | ||
| 99 | |||
| 100 | my $cmd = $self->{ ps_cmd }; | ||
| 101 | my $cores = $self->{ cpu_cores }; | ||
| 102 | my $unit = $self->{ rss_unit }; | ||
| 103 | my $divisor = $self->calc_rss_divisor( $unit ); | ||
| 104 | |||
| 105 | chomp( my @output = `$cmd` ); | ||
| 106 | croak "something went wrong: $!" if ( $? >> 8 ); | ||
| 107 | |||
| 108 | my $ref = {}; | ||
| 109 | |||
| 110 | for my $line ( @output ) { | ||
| 111 | my ( $cpu, $rss, $command ) = $line =~ /(\d+\.\d)\s+(\d+)\s(.*)$/; | ||
| 112 | next unless ( $cpu && $rss && $command ); | ||
| 113 | my $command_str = $self->sanitize_cmd( $command ); | ||
| 114 | |||
| 115 | # apparently, some combination results in empty string | ||
| 116 | next unless( $command_str ); | ||
| 117 | $ref->{ cpu }->{ $command_str } += $cpu / $cores; | ||
| 118 | $ref->{ rss }->{ $command_str } += $rss / $divisor; | ||
| 119 | } | ||
| 120 | |||
| 121 | $self->trim_below_threshold( $ref ); | ||
| 122 | return $ref; | ||
| 123 | } | ||
| 124 | |||
| 125 | =head2 sanitize_cmd | ||
| 126 | |||
| 127 | Parses a command and turns into a valid RRD DS name. | ||
| 128 | |||
| 129 | =cut | ||
| 130 | |||
| 131 | sub sanitize_cmd { | ||
| 132 | my ( $self, $cmd ) = @_; | ||
| 133 | |||
| 134 | my @result; | ||
| 135 | my @fields = split( /\s+/, $cmd ); | ||
| 136 | |||
| 137 | foreach my $field ( @fields ) { | ||
| 138 | if ( $field =~ m|^/| ) { | ||
| 139 | # fetching the last field of absolute path for truncation | ||
| 140 | my @paths = split( /\//, $field ); | ||
| 141 | $field = $paths[-1]; | ||
| 142 | } | ||
| 143 | |||
| 144 | next if ( $field =~ /^-/ ); # skips options | ||
| 145 | $field =~ s/-/_/g; # converts hyphen to underscore for clarity | ||
| 146 | $field =~ s/[^a-zA-Z0-9_]//g; # removes RRD-invalid chars | ||
| 147 | |||
| 148 | push @result, $field; | ||
| 149 | } | ||
| 150 | |||
| 151 | my $str = join( "_", @result ); | ||
| 152 | return substr( $str, 0, 19 ); | ||
| 153 | } | ||
| 154 | |||
| 155 | =head2 trim_below_threshold | ||
| 156 | |||
| 157 | Trims metrics <= threshold for cpu, rss | ||
| 158 | |||
| 159 | =cut | ||
| 160 | |||
| 161 | sub trim_below_threshold { | ||
| 162 | my ( $self, $ref ) = @_; | ||
| 163 | |||
| 164 | my $cpu_thresh = $self->{ cpu_threshold }; | ||
| 165 | my $rss_thresh = $self->{ rss_threshold }; | ||
| 166 | |||
| 167 | for my $metric ( 'cpu', 'rss' ) { | ||
| 168 | for my $cmd ( keys %{ $ref->{ $metric } } ) { | ||
| 169 | my $val = $ref->{ $metric }->{ $cmd }; | ||
| 170 | |||
| 171 | if ( $metric eq 'cpu' ) { | ||
| 172 | delete $ref->{ $metric }->{ $cmd } unless ( $val >= $cpu_thresh ); | ||
| 173 | } | ||
| 174 | else { | ||
| 175 | delete $ref->{ $metric }->{ $cmd } unless ( $val >= $rss_thresh ); | ||
| 176 | } | ||
| 177 | } | ||
| 178 | } | ||
| 179 | } | ||
| 180 | |||
| 181 | =head2 calc_rss_divisor | ||
| 182 | |||
| 183 | Returns what to divide the ps RSS output by. For example, if the unit is MB then we would divide KB by 1024. | ||
| 184 | |||
| 185 | =cut | ||
| 186 | |||
| 187 | sub calc_rss_divisor { | ||
| 188 | my ( $self, $unit ) = @_; | ||
| 189 | |||
| 190 | croak "invalid unit" unless ( $unit && $unit =~ /^(kb|mb|gb|tb)$/i ); | ||
| 191 | |||
| 192 | return 1 if ( $unit =~ /^kb$/i ); | ||
| 193 | return 1024 if ( $unit =~ /^mb$/i ); | ||
| 194 | return 1024 * 1024 if ( $unit =~ /^gb$/i ); | ||
| 195 | return 1024 * 1024 * 1024 if ( $unit =~ /^tb$/i ); | ||
| 196 | } | ||
| 197 | |||
| 198 | =head2 store_rrd | ||
| 199 | |||
| 200 | Driver method to: | ||
| 201 | 1. create directories if they don't already exist. | ||
| 202 | 2. get a list of RRDs I already have (so that I can fill in with 0's to | ||
| 203 | avoid spotty graphs) | ||
| 204 | 3. go through the current list of metrics | ||
| 205 | a. create RRD if needed | ||
| 206 | b. update RRD | ||
| 207 | c. pop them from my list if exists | ||
| 208 | 4. go through the remaining RRDs from my list then fill with 0's | ||
| 209 | |||
| 210 | =cut | ||
| 211 | |||
| 212 | sub store_rrd { | ||
| 213 | my ( $self, $ref ) = @_; | ||
| 214 | my $dir = $self->{ rrd_dir }; | ||
| 215 | |||
| 216 | for my $metric ( 'cpu', 'rss' ) { | ||
| 217 | my $metric_dir = $dir . "/$metric"; | ||
| 218 | make_path( $metric_dir ) unless( -d $metric_dir ); | ||
| 219 | |||
| 220 | my $rrds = $self->{ rrd }->find_rrds( $metric_dir ); | ||
| 221 | |||
| 222 | for my $proc ( keys %{ $ref->{ $metric } } ) { | ||
| 223 | my $file = $metric_dir . "/$proc.rrd"; | ||
| 224 | my $value = $ref->{ $metric }->{ $proc }; | ||
| 225 | $self->create_rrd( $file, $proc ) unless( -f $file ); | ||
| 226 | $self->update_rrd( $file, $proc, $value ); | ||
| 227 | delete $rrds->{ $proc } if ( exists $rrds->{ $proc } ); | ||
| 228 | } | ||
| 229 | |||
| 230 | # fills the remaining proces with 0's | ||
| 231 | for my $proc ( keys %{ $rrds } ) { | ||
| 232 | my $file = $rrds->{ $proc }; | ||
| 233 | $self->update_rrd( $file, $proc, 0 ); | ||
| 234 | } | ||
| 235 | } | ||
| 236 | return 1; | ||
| 237 | } | ||
| 238 | |||
| 239 | =head2 create_rrd | ||
| 240 | |||
| 241 | Creates an RRD file. | ||
| 242 | |||
| 243 | =cut | ||
| 244 | |||
| 245 | sub create_rrd { | ||
| 246 | my ( $self, $file, $proc ) = @_; | ||
| 247 | |||
| 248 | my $rra = $self->{ rrd_rra }; | ||
| 249 | my $ds = $self->{ rrd_ds }; | ||
| 250 | |||
| 251 | my $ds_line = "DS:$proc:" . $ds; | ||
| 252 | my @params; | ||
| 253 | push @params, $ds_line, @{ $rra }; | ||
| 254 | |||
| 255 | RRDs::create( $file, @params ); | ||
| 256 | my $ERR = RRDs::error; | ||
| 257 | croak "error while creating $file: $ERR\n" if $ERR; | ||
| 258 | return 1; | ||
| 259 | } | ||
| 260 | |||
| 261 | =head2 update_rrd | ||
| 262 | |||
| 263 | Updates an rrd file | ||
| 264 | |||
| 265 | =cut | ||
| 266 | |||
| 267 | sub update_rrd { | ||
| 268 | my ( $self, $file, $key, $value ) = @_; | ||
| 269 | |||
| 270 | my $rrd = RRD::Simple->new( file => $file ); | ||
| 271 | $rrd->update( $key => $value ); | ||
| 272 | } | ||
| 273 | |||
| 274 | =head2 alrm_handler | ||
| 275 | |||
| 276 | Signal handler for alarm(). It is an instance method because I need access | ||
| 277 | to the attributes. | ||
| 278 | |||
| 279 | =cut | ||
| 280 | |||
| 281 | sub alrm_handler { | ||
| 282 | my $self = shift; | ||
| 283 | |||
| 284 | DEBUG "DEBUG: alrm received, in alrm_handler()"; | ||
| 285 | # so clean up basically means I want to delete any temporary files lingering | ||
| 286 | # around in the RRD directory | ||
| 287 | my $dir = $self->{ rrd_dir }; | ||
| 288 | |||
| 289 | for my $metric ( "cpu", "rss" ) { | ||
| 290 | my $m_dir = $dir . "/$metric"; | ||
| 291 | |||
| 292 | opendir( my $dh, $m_dir ); | ||
| 293 | while( my $file = readdir $dh ) { | ||
| 294 | # skips .rrd or ., .. | ||
| 295 | next if ( $file =~ /(\.rrd|\.{1,2})$/ ); | ||
| 296 | my $path = $m_dir . "/$file"; | ||
| 297 | unlink $path; | ||
| 298 | } | ||
| 299 | } | ||
| 300 | exit(1); | ||
| 301 | } | ||
| 302 | |||
| 303 | =head2 set_signal_handlers | ||
| 304 | |||
| 305 | Set signal handlers. The only one I'm expecting is SIGALRM. | ||
| 306 | |||
| 307 | =cut | ||
| 308 | |||
| 309 | sub set_signal_handlers { | ||
| 310 | my $self = shift; | ||
| 311 | |||
| 312 | $SIG{ALRM} = sub { | ||
| 313 | $self->alrm_handler(); | ||
| 314 | } | ||
| 315 | } | ||
| 316 | |||
| 317 | =head1 AUTHOR | ||
| 318 | |||
| 319 | Satoshi Yagi, C<< <satoshi.yagi at yahoo.com> >> | ||
| 320 | |||
| 321 | =head1 BUGS | ||
| 322 | |||
| 323 | Please report any bugs or feature requests to C<bug-app-proctrends-cron at rt.cpan.org>, or through | ||
| 324 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=App-ProcTrends-Cron>. I will be notified, and then you'll | ||
| 325 | automatically be notified of progress on your bug as I make changes. | ||
| 326 | |||
| 327 | |||
| 328 | |||
| 329 | |||
| 330 | =head1 SUPPORT | ||
| 331 | |||
| 332 | You can find documentation for this module with the perldoc command. | ||
| 333 | |||
| 334 | perldoc App::ProcTrends::Cron | ||
| 335 | |||
| 336 | |||
| 337 | You can also look for information at: | ||
| 338 | |||
| 339 | =over 4 | ||
| 340 | |||
| 341 | =item * RT: CPAN's request tracker (report bugs here) | ||
| 342 | |||
| 343 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=App-ProcTrends-Cron> | ||
| 344 | |||
| 345 | =item * AnnoCPAN: Annotated CPAN documentation | ||
| 346 | |||
| 347 | L<http://annocpan.org/dist/App-ProcTrends-Cron> | ||
| 348 | |||
| 349 | =item * CPAN Ratings | ||
| 350 | |||
| 351 | L<http://cpanratings.perl.org/d/App-ProcTrends-Cron> | ||
| 352 | |||
| 353 | =item * Search CPAN | ||
| 354 | |||
| 355 | L<http://search.cpan.org/dist/App-ProcTrends-Cron/> | ||
| 356 | |||
| 357 | =back | ||
| 358 | |||
| 359 | |||
| 360 | =head1 ACKNOWLEDGEMENTS | ||
| 361 | |||
| 362 | |||
| 363 | =head1 LICENSE AND COPYRIGHT | ||
| 364 | |||
| 365 | Copyright 2013 Satoshi Yagi. | ||
| 366 | |||
| 367 | This program is free software; you can redistribute it and/or modify it | ||
| 368 | under the terms of either: the GNU General Public License as published | ||
| 369 | by the Free Software Foundation; or the Artistic License. | ||
| 370 | |||
| 371 | See http://dev.perl.org/licenses/ for more information. | ||
| 372 | |||
| 373 | |||
| 374 | =cut | ||
| 375 | |||
| 376 | 1; # End of App::ProcTrends::Cron |