Apache-Logmonster
view release on metacpan or search on metacpan
lib/Apache/Logmonster/Utility.pm view on Meta::CPAN
chomp $response;
return $response if $response; # if they typed something, return it
return $default if $default; # return the default, if available
return ''; # return empty handed
}
sub audit {
my $self = shift;
my $mess = shift;
my %p = validate( @_, { %std_opts } );
if ($mess) {
push @{ $log->{audit} }, $mess;
print "$mess\n" if $self->{debug} || $p{debug};
}
return \$log->{audit};
}
sub archive_file {
my $self = shift;
my $file = shift or return $log->error("missing filename in request");
my %p = validate( @_,
{ 'sudo' => { type => BOOLEAN, optional => 1, default => 1 },
'mode' => { type => SCALAR, optional => 1 },
destdir => { type => SCALAR, optional => 1 },
%std_opts,
}
);
my %args = $self->get_std_args( %p );
return $log->error( "file ($file) is missing!", %args )
if !-e $file;
my $archive = $file . '.' . time;
if ( $p{destdir} && -d $p{destdir} ) {
my ($vol,$dirs,$file_wo_path) = File::Spec->splitpath( $archive );
$archive = File::Spec->catfile( $p{destdir}, $file_wo_path );
};
# see if we can write to both files (new & archive) with current user
if ( $self->is_writable( $file, %args )
&& $self->is_writable( $archive, %args ) ) {
# we have permission, use perl's native copy
copy( $file, $archive );
if ( -e $archive ) {
$log->audit("archive_file: $file backed up to $archive");
$self->chmod( file => $file, mode => $p{mode}, %args ) if $p{mode};
return $archive;
};
}
# we failed with existing permissions, try to escalate
$self->archive_file_sudo( $file ) if ( $p{sudo} && $< != 0 );
return $log->error( "backup of $file to $archive failed: $!", %args)
if ! -e $archive;
$self->chmod( file => $file, mode => $p{mode}, %args ) if $p{mode};
$log->audit("$file backed up to $archive");
return $archive;
}
sub archive_file_sudo {
my $self = shift;
my ($file, $archive) = @_;
my $sudo = $self->sudo();
my $cp = $self->find_bin( 'cp',fatal=>0 );
if ( $sudo && $cp ) {
return $self->syscmd( "$sudo $cp $file $archive",fatal=>0 );
}
$log->error( "archive_file: sudo or cp was missing, could not escalate.",fatal=>0);
return;
};
sub chmod {
my $self = shift;
my %p = validate(
@_,
{ 'file' => { type => SCALAR, optional => 1, },
'file_or_dir' => { type => SCALAR, optional => 1, },
'dir' => { type => SCALAR, optional => 1, },
'mode' => { type => SCALAR, optional => 0, },
'sudo' => { type => BOOLEAN, optional => 1, default => 0 },
%std_opts,
}
);
my $mode = $p{mode};
my %args = $self->get_std_args( %p );
my $file = $p{file} || $p{file_or_dir} || $p{dir}
or return $log->error( "invalid params to chmod in ". ref $self );
if ( $p{sudo} ) {
my $chmod = $self->find_bin( 'chmod', debug => 0 );
my $sudo = $self->sudo();
$self->syscmd( "$sudo $chmod $mode $file", debug => 0 )
or return $log->error( "couldn't chmod $file: $!", %args );
}
# note the conversion of ($mode) to an octal value. Very important!
CORE::chmod( oct($mode), $file ) or
return $log->error( "couldn't chmod $file: $!", %args);
$log->audit("chmod $mode $file");
}
sub chown {
my $self = shift;
my $file = shift;
my %p = validate( @_,
{ 'uid' => { type => SCALAR },
lib/Apache/Logmonster/Utility.pm view on Meta::CPAN
# Returns : 0 - failure, 1 - success
# Parameters : S - dir - a directory to build programs in
=item check_homedir_ownership
Checks the ownership on all home directories to see if they are owned by their respective users in /etc/password. Offers to repair the permissions on incorrectly owned directories. This is useful when someone that knows better does something like "ch...
######### check_homedir_ownership ############
# Usage : $util->check_homedir_ownership();
# Purpose : repair user homedir ownership
# Returns : 0 - failure, 1 - success
# Parameters :
# Optional : I - auto - no prompts, just fix everything
# See Also : sysadmin
Comments: Auto mode should be run with great caution. Run it first to see the results and then, if everything looks good, run in auto mode to do the actual repairs.
=item chown_system
The advantage this sub has over a Pure Perl implementation is that it can utilize sudo to gain elevated permissions that we might not otherwise have.
############### chown_system #################
# Usage : $util->chown_system( dir=>"/tmp/example", user=>'matt' );
# Purpose : change the ownership of a file or directory
# Returns : 0 - failure, 1 - success
# Parameters : S - dir - the directory to chown
# : S - user - a system username
# Optional : S - group - a sytem group name
# : I - recurse - include all files/folders in directory?
# Comments : Uses the system chown binary
# See Also : n/a
=item clean_tmp_dir
############## clean_tmp_dir ################
# Usage : $util->clean_tmp_dir( $dir );
# Purpose : clean up old build stuff before rebuilding
# Returns : 0 - failure, 1 - success
# Parameters : S - $dir - a directory or file.
# Throws : die on failure
# Comments : Running this will delete its contents. Be careful!
=item get_mounted_drives
############# get_mounted_drives ############
# Usage : my $mounts = $util->get_mounted_drives();
# Purpose : Uses mount to fetch a list of mounted drive/partitions
# Returns : a hashref of mounted slices and their mount points.
=item archive_file
############### archive_file #################
# Purpose : Make a backup copy of a file by copying the file to $file.timestamp.
# Usage : my $archived_file = $util->archive_file( $file );
# Returns : the filename of the backup file, or 0 on failure.
# Parameters : S - file - the filname to be backed up
# Comments : none
=item chmod
Set the permissions (ugo-rwx) of a file. Will use the native perl methods (by default) but can also use system calls and prepend sudo if additional permissions are needed.
$util->chmod(
file_or_dir => '/etc/resolv.conf',
mode => '0755',
sudo => $sudo
)
arguments required:
file_or_dir - a file or directory to alter permission on
mode - the permissions (numeric)
arguments optional:
sudo - the output of $util->sudo
fatal - die on errors? (default: on)
debug
result:
0 - failure
1 - success
=item chown
Set the ownership (user and group) of a file. Will use the native perl methods (by default) but can also use system calls and prepend sudo if additional permissions are needed.
$util->chown(
file_or_dir => '/etc/resolv.conf',
uid => 'root',
gid => 'wheel',
sudo => 1
);
arguments required:
file_or_dir - a file or directory to alter permission on
uid - the uid or user name
gid - the gid or group name
arguments optional:
file - alias for file_or_dir
dir - alias for file_or_dir
sudo - the output of $util->sudo
fatal - die on errors? (default: on)
debug
result:
0 - failure
1 - success
=item file_delete
############################################
# Usage : $util->file_delete( $file );
lib/Apache/Logmonster/Utility.pm view on Meta::CPAN
etcdir - the etc directory to prefer
debug
fatal
result:
0 - failure
the path to $file
=item get_my_ips
returns an arrayref of IP addresses on local interfaces.
=item is_process_running
Verify if a process is running or not.
$util->is_process_running($process) ? print "yes" : print "no";
$process is the name as it would appear in the process table.
=item is_readable
############################################
# Usage : $util->is_readable( file=>$file );
# Purpose : ????
# Returns : 0 = no (not reabable), 1 = yes
# Parameters : S - file - a path name to a file
# Throws : no exceptions
# Comments : none
# See Also : n/a
result:
0 - no (file is not readable)
1 - yes (file is readable)
=item is_writable
If the file exists, it checks to see if it is writable. If the file does not exist, it checks to see if the enclosing directory is writable.
############################################
# Usage : $util->is_writable( "/tmp/boogers");
# Purpose : make sure a file is writable
# Returns : 0 - no (not writable), 1 - yes (is writeable)
# Parameters : S - file - a path name to a file
# Throws : no exceptions
=item fstab_list
############ fstab_list ###################
# Usage : $util->fstab_list;
# Purpose : Fetch a list of drives that are mountable from /etc/fstab.
# Returns : an arrayref
# Comments : used in backup.pl
# See Also : n/a
=item get_dir_files
$util->get_dir_files( $dir, debug=>1 )
required arguments:
dir - a directory
optional arguments:
fatal
debug
result:
an array of files names contained in that directory.
0 - failure
=item get_the_date
Returns the date split into a easy to work with set of strings.
$util->get_the_date( bump=>$bump, debug=>$debug )
required arguments:
none
optional arguments:
bump - the offset (in days) to subtract from the date.
debug
result: (array with the following elements)
$dd = day
$mm = month
$yy = year
$lm = last month
$hh = hours
$mn = minutes
$ss = seconds
my ($dd, $mm, $yy, $lm, $hh, $mn, $ss) = $util->get_the_date();
=item install_from_source
usage:
$util->install_from_source(
package => 'simscan-1.07',
site => 'http://www.inter7.com',
url => '/simscan/',
targets => ['./configure', 'make', 'make install'],
patches => '',
debug => 1,
);
Downloads and installs a program from sources.
required arguments:
( run in 3.034 seconds using v1.01-cache-2.11-cpan-c966e8aa7e8 )