App-Slaughter

 view release on metacpan or  search on metacpan

lib/Slaughter/API/generic.pm  view on Meta::CPAN


B<NOTE>: This primitive invoke C<mount> and parses the output.  This
is reasonably portable, but will fail upon systems which have no "mount"
binary.  In that case the method will output a stub message to complain
that the function is not implemented.

=cut

sub Mounts
{
    my $path = FindBinary( Binary => "mount" );

    if ($path)
    {
        my @results;

        open my $handle, "-|", $path or
          die "Failed to run mount: $!";

        while ( my $line = <$handle> )
        {
            chomp($line);

            if ( $line =~ /^([^ \t]+)[ \t]+on[ \t]+([^ \t]+)/ )
            {
                my ( $dev, $point ) = ( $1, $2 );
                push( @results, $point ) if ( $dev =~ /dev/ );
            }
        }
        close($handle);

        return (@results);

    }
    else
    {
        print "Mounts - not implemented for $^O\n";
    }
}



=head2 PackageInstalled


This method is a stub which does nothing but output a line of text to
inform the caller that the method is not implemented.

For an implementation, and documentation, please consult C<Slaughter::API::linux>.

=cut

sub PackageInstalled
{
    print "PackageInstalled - not implemented for $^O\n";
}


=head2 PercentageUsed

Return the percentage of space used in in the given mounted-device.

=for example begin

  foreach my $point ( Mounts() )
  {
     if ( PercentageUsed( Path => $point ) > 80 )
     {
        Alert( To      => "root",
               From    => "root",
               Subject => "$server is running out of space on $point",
               Message => "This is a friendly warning." );
     }
  }

=for example end

The following parameters are supported:

=over 8

=item Path

The mount-point to the filesystem in question.

=back

The return value will be a percentage in the range 0-100.

B<NOTE>: This primitive invokes C<df> and parses the output.  This
is reasonably portable, but will fail upon systems which have no "df"
binary.  In that case the method will output a stub message to complain
that the function is not implemented.

=cut

sub PercentageUsed
{
    my (%params) = (@_);

    #
    # Ensure we have a 'df' binary.
    #
    my $path = FindBinary( Binary => "df" );
    if ( !$path )
    {
        print "PercentageUsed - not implemented for $^O\n";
    }


    #
    #  The mount-point
    #
    my $point = $params{ 'Path' } || "/";
    my $perc = 0;


    #
    #  Call df to get the output, use posix mode.
    #
    my $out = `$path -P $point`;

    foreach my $line ( split( /[\r\n]/, $out ) )
    {
        next unless ( $line =~ /%/ );

        if ( $line =~ /[ \t]([0-9]*)%[ \t]/ )
        {
            $perc = $1;
        }
    }

    return ($perc);

}


=head2 RemovePackage

This method is a stub which does nothing but output a line of text to
inform the caller that the method is not implemented.

For an implementation, and documentation, please consult C<Slaughter::API::linux>.

=cut

sub RemovePackage
{



( run in 1.004 second using v1.01-cache-2.11-cpan-39bf76dae61 )