App-ProcTrends

 view release on metacpan or  search on metacpan

lib/App/ProcTrends/Cron.pm  view on Meta::CPAN

            c. pop them from my list if exists
        4. go through the remaining RRDs from my list then fill with 0's 

=cut

sub store_rrd {
    my ( $self, $ref ) = @_;
    my $dir = $self->{ rrd_dir };

    for my $metric ( 'cpu', 'rss' ) {        
        my $metric_dir = $dir . "/$metric";
        make_path( $metric_dir ) unless( -d $metric_dir );
        
        my $rrds = $self->{ rrd }->find_rrds( $metric_dir );
        
        for my $proc ( keys %{ $ref->{ $metric } } ) {
            my $file = $metric_dir . "/$proc.rrd";
            my $value = $ref->{ $metric }->{ $proc };
            $self->create_rrd( $file, $proc ) unless( -f $file );
            $self->update_rrd( $file, $proc, $value );
            delete $rrds->{ $proc } if ( exists $rrds->{ $proc } );
        }
        
        # fills the remaining proces with 0's
        for my $proc ( keys %{ $rrds } ) {
            my $file = $rrds->{ $proc };
            $self->update_rrd( $file, $proc, 0 );
        }
    }
    return 1;
}

=head2 create_rrd

    Creates an RRD file.

=cut

sub create_rrd {
    my ( $self, $file, $proc ) = @_;
    
    my $rra = $self->{ rrd_rra };
    my $ds  = $self->{ rrd_ds };
    
    my $ds_line = "DS:$proc:" . $ds;
    my @params;
    push @params, $ds_line, @{ $rra };
    
    RRDs::create( $file, @params );
    my $ERR = RRDs::error;
    croak "error while creating $file: $ERR\n" if $ERR;
    return 1;
}

=head2 update_rrd

    Updates an rrd file

=cut

sub update_rrd {
    my ( $self, $file, $key, $value ) = @_;
    
    my $rrd = RRD::Simple->new( file => $file );
    $rrd->update( $key => $value );
}

=head2 alrm_handler

    Signal handler for alarm().  It is an instance method because I need access
    to the attributes.

=cut

sub alrm_handler {
    my $self = shift;

    DEBUG "DEBUG: alrm received, in alrm_handler()";
    # so clean up basically means I want to delete any temporary files lingering
    # around in the RRD directory
    my $dir = $self->{ rrd_dir };

    for my $metric ( "cpu", "rss" ) {
        my $m_dir = $dir . "/$metric";
        
        opendir( my $dh, $m_dir );
        while( my $file = readdir $dh ) {
            # skips .rrd or ., ..
            next if ( $file =~ /(\.rrd|\.{1,2})$/ );
            my $path = $m_dir . "/$file";
            unlink $path;
        }
    }
    exit(1);
}

=head2 set_signal_handlers

    Set signal handlers.  The only one I'm expecting is SIGALRM.

=cut

sub set_signal_handlers {
    my $self = shift;

    $SIG{ALRM} = sub {
        $self->alrm_handler();
    }
}

=head1 AUTHOR

Satoshi Yagi, C<< <satoshi.yagi at yahoo.com> >>

=head1 BUGS

Please report any bugs or feature requests to C<bug-app-proctrends-cron at rt.cpan.org>, or through
the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=App-ProcTrends-Cron>.  I will be notified, and then you'll
automatically be notified of progress on your bug as I make changes.




( run in 0.707 second using v1.01-cache-2.11-cpan-9581c071862 )