Skip to content

Commit

Permalink
fix: Update getProcesses() API to filter processes on regexp and/or s…
Browse files Browse the repository at this point in the history
…ame namespace

Refacto related usage
  • Loading branch information
g-bougard committed Dec 19, 2024
1 parent 21f9863 commit 03af48e
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 26 deletions.
2 changes: 2 additions & 0 deletions Changes
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ inventory:
* fix #811: Fix network interface inventory on lxc linux container, but also fix
few other cases where more interfaces are found due to interface name aliasing
* Fix --partial option when used with glpi-agent script
* Update getProcesses() API to permit filtering and report processes only in the same
namespace to not list containers processes. Refacto inventory module using this API.

remoteinventory:
* Store remote inventory part checksums in dedicated state files and support maintenance
Expand Down
7 changes: 5 additions & 2 deletions lib/GLPI/Agent/Task/Inventory/Generic/Databases/Oracle.pm
Original file line number Diff line number Diff line change
Expand Up @@ -379,8 +379,11 @@ sub _runSql {
my $env = "";
if ($ENV{ORACLE_SID}) {
# Get instance asm_pmon process
my ($asm_pmon) = grep { $_->{CMD} =~ /^asm_pmon_$ENV{ORACLE_SID}/ }
getProcesses(logger => $params{logger});
my ($asm_pmon) = getProcesses(
namespace => "same",
filter => qr/^asm_pmon_$ENV{ORACLE_SID}/,
logger => $params{logger}
);
$user = $asm_pmon->{USER} if $asm_pmon;
$env = "ORACLE_SID=$ENV{ORACLE_SID}";
}
Expand Down
4 changes: 2 additions & 2 deletions lib/GLPI/Agent/Task/Inventory/Generic/Drives/ASM.pm
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use GLPI::Agent::Tools::Unix;

sub isEnabled {
return 0 if OSNAME eq 'MSWin32';
return grep { $_->{CMD} =~ /^asm_pmon/ } getProcesses();
return scalar(getProcesses(filter => qr/^asm_pmon/, namespace => "same"));
}

sub doInventory {
Expand All @@ -20,7 +20,7 @@ sub doInventory {
my $logger = $params{logger};

# First get asm_pmon process
my ($asm_pmon) = grep { $_->{CMD} =~ /^asm_pmon/ } getProcesses(logger => $logger)
my ($asm_pmon) = getProcesses(filter => qr/^asm_pmon/, namespace => "same", logger => $logger)
or return;

my $user = $asm_pmon->{USER};
Expand Down
13 changes: 7 additions & 6 deletions lib/GLPI/Agent/Task/Inventory/Linux/Videos.pm
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,10 @@ sub doInventory {
my $xorgData;

my $xorgPid;
foreach my $process (getProcesses(logger => $logger)) {
next unless $process->{CMD} =~ m{
my ($process) = getProcesses(
namespace => "same",
logger => $logger,
filter => qr{
^
(?:
/usr/bin
Expand All @@ -59,10 +61,9 @@ sub doInventory {
/usr/libexec
)
/X
}x;
$xorgPid = $process->{PID};
last;
}
}x
);
$xorgPid = $process->{PID} if $process;

if ($xorgPid) {
my $fd = 0;
Expand Down
10 changes: 6 additions & 4 deletions lib/GLPI/Agent/Task/Inventory/Virtualization/Qemu.pm
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,12 @@ sub doInventory {
my $inventory = $params{inventory};
my $logger = $params{logger};

foreach my $process (getProcesses(logger => $logger)) {
# match only if an qemu instance
next if $process->{CMD} =~ /^\[/;
next if $process->{CMD} !~ /(qemu|kvm|qemu-kvm|qemu-system\S+) .*\S/x;
# check only qemu instances
foreach my $process (getProcesses(
filter => qr/(qemu|kvm|qemu-kvm|qemu-system\S+) .*\S/x,
namespace => "same",
logger => $logger,
)) {

# Don't inventory qemu guest agent as a virtualmachine
next if $process->{CMD} =~ /qemu-ga/;
Expand Down
65 changes: 53 additions & 12 deletions lib/GLPI/Agent/Tools/Unix.pm
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,29 @@ sub _getProcessesBusybox {
return @processes;
}

my $qrProcessWithNameSpace = qr/^ \s*
(\S+) \s+
(\S+) \s+
(\S+) \s+
(\S+) \s+
(\S+) \s+
(\S+) \s+
(\S+) \s+
(\S+) \s+
(\S.*\S)
/x;

my $qrProcessWithoutNameSpace = qr/^ \s*
(\S+) \s+
(\S+) \s+
(\S+) \s+
(\S+) \s+
(\S+) \s+
(\S+) \s+
(\S+) \s+
(\S.*\S)
/x;

sub _getProcessesOther {
my (%params) = (
command =>
Expand All @@ -301,6 +324,29 @@ sub _getProcessesOther {
@_
);

# Support a parameter to only keep processes from the same namespace
# Useful to exclude processes from docker or lxc containers on linux
my $sameNameSpace = delete $params{namespace} // "";
if (OSNAME eq "solaris") {
$sameNameSpace = 0;
} elsif ($sameNameSpace && $sameNameSpace eq "same") {
# Extract namespace number from the system first process
$sameNameSpace = getFirstLine(command => "ps --no-headers -o cgroupns 1", logger => $params{logger});
if ($sameNameSpace && $sameNameSpace =~ /^\d+$/) {
$sameNameSpace = int($sameNameSpace);
$params{command} = "ps -A -o user,pid,pcpu,pmem,vsz,tty,etime,cgroupns,command";
} else {
$sameNameSpace = 0;
}
} else {
$sameNameSpace = 0;
}

my $filter = delete $params{filter};
$filter = 0 unless ref($filter) eq "Regexp";

my $qrLine = $sameNameSpace ? $qrProcessWithNameSpace : $qrProcessWithoutNameSpace;

my @lines = getAllLines(%params)
or return;

Expand All @@ -314,17 +360,7 @@ sub _getProcessesOther {

foreach my $line (@lines) {

next unless $line =~
/^ \s*
(\S+) \s+
(\S+) \s+
(\S+) \s+
(\S+) \s+
(\S+) \s+
(\S+) \s+
(\S+) \s+
(\S.*\S)
/x;
next unless $line =~ $qrLine;

my $user = $1;
my $pid = $2;
Expand All @@ -333,7 +369,12 @@ sub _getProcessesOther {
my $vsz = $5;
my $tty = $6;
my $etime = $7;
my $cmd = $8;
my $ns = $sameNameSpace ? $8 : 0;
my $cmd = $sameNameSpace ? $9 : $8;

next if $sameNameSpace && $ns && $ns =~ /^\d+$/ && int($ns) != $sameNameSpace;

next if $filter && $cmd !~ $filter;

push @processes, {
USER => $user,
Expand Down

0 comments on commit 03af48e

Please sign in to comment.