=head1 NAME VAdmind::System::Process.pm - Provides information about system procceses. =head1 DESCRIPTION This plugin provides methods to manage system processes. =head1 SYNOPSIS my $plugin = System::Process->new; =head1 AUTHOR Urivan Alyasid Flores =cut package VAdmind::Plugins::System::Process; use strict; use warnings; =head1 METHODS =head2 CONSTRUCTORS =head3 new Creates a new System::Process plugin object. =cut sub new { my $type = shift; my $self = {@_}; my @proc_list; $self->{'_proc_list'} = @proc_list; return bless ($self, $type); } # Anonnymous subroutines { =head3 _proc_list Creates a list of the current running process list. Returns: @proclist - Array of hashes with the following keys: pid - Process ID user - User owner of the process app - Basename of the process cmd - Full process command line =cut sub _proc_list { my $self = shift; my @procs; if (open (PS, "/bin/ps -eo pid=,user=,ucmd=,command= a 2>/dev/null |")) { my $idx = 0; while () { chomp; s/^\s*//g; s/\s*$//g; s/\s */ /g; ( $procs[$idx]->{'pid'}, $procs[$idx]->{'user'}, $procs[$idx]->{'app'}, $procs[$idx]->{'cmd'} ) = split (/ /,$_,4); $idx++; } } if (@procs) { $self->{'_proc_list'} = \@procs; } #return @procs; } } =head3 get_list Creates an XML tree of the current system process list. =cut sub get_list { my $self = shift; my $out = $self->{'out'}; $self->_proc_list(); my $idx = 0; for my $proc (@{$self->{'_proc_list'}}) { $out->{'xml'}->{'proc'}->[$idx]->{'pid'} = $proc->{'pid'}; $out->{'xml'}->{'proc'}->[$idx]->{'user'}->[0] = $proc->{'user'}; $out->{'xml'}->{'proc'}->[$idx]->{'app'}->[0] = $proc->{'app'}; $out->{'xml'}->{'proc'}->[$idx]->{'cmd'}->[0] = $proc->{'cmd'}; $idx++; } if (! @{$self->{'_proc_list'}}) { $out->{'error'} = 'Unable to get process list.'; $out->{'result'} = 1; } } =head3 get_pid Retrieves the process id of a command. =cut sub get_pid { my $self = shift; my $in = $self->{'in'}; my $out = $self->{'out'}; # Check if any command was given my %cmds; my $idx = 0; for my $cmd (@{$in->{'cmd'}}) { $idx++; } if ($idx) { $self->_proc_list(); for my $proc ($self->{'_proc_list'}) { if (defined $cmds{$proc->{'app'}}) { $out->{'xml'}->{'cmd'}->[$cmds{$proc->{'app'}}]->{'pid'} = $proc->{'pid'}; } } if (! $self->{'_proc_list'}) { $out->{'error'} = 'Unable to check processes.'; $out->{'result'} = 1; } } else { $out->{'error'} = 'Command required.'; $out->{'result'} = 1; } } =head3 get_cpids Returns the child process id of a running process. =cut sub get_cpids { my $self = shift; my $in = $self->{'in'}; my $count = shift || 0; if (defined $in->{'pid'} && ref \$in->{'pid'}->[0] eq 'SCALAR') { my $ppid = shift || $in->{'pid'}->[0]; if (open (PS, "ps -eo pid,ppid|awk '{if (\$2 == $ppid) {print \$1} }' |")) { my @cpids; while () { chomp; push (@cpids, $_); } for my $cpid (@cpids) { $self->{'out'}->{'xml'}->{'pid'}->[$count] = $cpid; $count ++; $self->get_cpids ($count, $cpid); } if ($count == 0) { close (PS); } } else { $self->{'out'}->{'result'} = 1; $self->{'out'}->{'error'} = 'Unable to access process list.'; } } else { if ($count == 0) { $self->{'out'}->{'result'} = 1; $self->{'out'}->{'error'} = 'PID required.'; } } } sub kill_pid { my $self = shift; my $in = $self->{'in'}; if (defined $in->{'pid'}) { kill 9, @{$in->{'pid'}}; } } 1;