Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions bin/glpi-agent
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ GetOptions(
'assetname-support=i',
'additional-content=s',
'backend-collect-timeout=s',
'category=s',
'ca-cert-dir=s',
'ca-cert-file=s',
'conf-file=s',
Expand Down Expand Up @@ -265,6 +266,7 @@ glpi-agent [options] [--server server|--local path]
--tasks=TASK1[,TASK]...[,...] run given tasks in given order

Inventory task specific options:
--category=CATEGORY include items only from given categories
--no-category=CATEGORY do not list given category items
--list-categories list supported categories
--scan-homedirs scan user home directories (false)
Expand Down
2 changes: 2 additions & 0 deletions bin/glpi-inventory
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ GetOptions(
'assetname-support=i',
'additional-content=s',
'backend-collect-timeout=s',
'category=s',
'credentials=s@',
'glpi_version=s',
'html',
Expand Down Expand Up @@ -111,6 +112,7 @@ glpi-inventory [options]
--scan-profiles scan user profiles (false)
--html save the inventory as HTML (false)
--json save the inventory as JSON (false)
--category=CATEGORY include items only from given categories
--no-category=CATEGORY do not list given category items
--partial=CATEGORY make a partial inventory of given category
items, this option implies --json
Expand Down
7 changes: 7 additions & 0 deletions lib/GLPI/Agent.pm
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,13 @@ sub getContact {
$self->{config}->{"no-category"} = $no_category;
}
}
if ($tasks->{inventory}->{"category"}) {
my $category = [ sort split(/,+/, $tasks->{inventory}->{"category"}) ];
unless (@{$self->{config}->{"category"}} && join(",", sort @{$self->{config}->{"category"}}) eq join(",", @{$category})) {
$self->{logger}->debug("set category configuration to: ".$tasks->{inventory}->{"category"});
$self->{config}->{"category"} = $category;
}
}
# Handle required-category set by server on inventory task
if ($tasks->{inventory}->{"required-category"}) {
my $required_category = [ sort split(/,+/, $tasks->{inventory}->{"required-category"}) ];
Expand Down
2 changes: 2 additions & 0 deletions lib/GLPI/Agent/Config.pm
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use GLPI::Agent::Tools;
my $default = {
'additional-content' => undef,
'backend-collect-timeout' => 180,
'category' => [],
'ca-cert-dir' => undef,
'ca-cert-file' => undef,
'color' => undef,
Expand Down Expand Up @@ -350,6 +351,7 @@ sub _checkContent {
server
httpd-trust
no-task
category
no-category
required-category
tasks
Expand Down
25 changes: 22 additions & 3 deletions lib/GLPI/Agent/Task/Inventory.pm
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,12 @@ sub isEnabled {
# Add a GLPI client to each param with a category and a use property
# and if related category is not disabled
my %disabled = map { $_ => 1 } @{$self->{config}->{'no-category'}};
my %enabled = map { $_ => 1 } @{$self->{config}->{'category'}};
my @params;
my $cant_load_glpi_client = 0;
foreach my $param (@{$tasks->{inventory}->{params}}) {
my @validated;
if (!$param->{category} || $disabled{$param->{category}}) {
if (!$param->{category} || $disabled{$param->{category}} || (%enabled && ! exists $enabled{ $param->{category} })) {
} elsif ($param->{params_id}) {
# Here we must handle the case of remotely triggered events
my @categories = map { trimWhitespace($_) } split(/,+/, $param->{category});
Expand Down Expand Up @@ -154,6 +155,23 @@ sub run {
$self->{disabled} = {
map { $_ => 1 } @{$self->{config}->{'no-category'}}
};
$self->{enabled} = {
map { $_ => 1 } @{$self->{config}->{'category'}}
};

if (keys(%{$self->{enabled}})) {
# Be sure at least bios and hardware category are enabled via category option
foreach my $category (qw(bios hardware)) {
$self->{logger}->debug("$category not enabled, forcing as it is required")
unless $self->{enabled}->{$category};
$self->{enabled}->{$category} = 1;
}
# Software category requires os to be enabled too
if ($self->{enabled}->{software} && !$self->{enabled}->{os}) {
$self->{logger}->debug("Forcing os category as required by software one")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My apologies, one char is missing at the end here:

Suggested change
$self->{logger}->debug("Forcing os category as required by software one")
$self->{logger}->debug("Forcing os category as required by software one");

$self->{enabled}->{os} = 1;
}
}

# Support inventory event
if ($event && !$self->setupEvent()) {
Expand Down Expand Up @@ -207,7 +225,7 @@ sub setupEvent {

# Support partial event with category defined only if partial
if ($event->partial && $event->category) {
my %keep = map { lc($_) => 1 } grep { ! $self->{disabled}->{$_} } split(/,+/, $event->category);
my %keep = map { lc($_) => 1 } grep { ! $self->{disabled}->{$_} && (!%{ $self->{enabled} } || $self->{enabled}->{$_}) } split(/,+/, $event->category);
unless (keys(%keep)) {
$self->{logger}->info("Nothing to inventory on partial inventory event");
return 0;
Expand Down Expand Up @@ -413,7 +431,7 @@ sub _initModulesList {
if (defined(*{$module."::category"})) {
no strict 'refs'; ## no critic (ProhibitNoStrict)
my $category = &{$module."::category"}();
if ($category && $self->{disabled}->{$category}) {
if ($category && ($self->{disabled}->{$category} || ( %{ $self->{enabled} } && !exists $self->{enabled}->{$category} ))) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here I would prefer a elsif after the original case with a log telling category is "not enabled" in place of "disabled". This way, we can better understand why a module remains disabled while reading the log.

$logger->debug2("module $module disabled: '$category' category disabled");
$self->{modules}->{$module}->{enabled} = 0;
next;
Expand Down Expand Up @@ -532,6 +550,7 @@ sub _runModule {
datadir => $self->{datadir},
inventory => $self->{inventory},
no_category => $self->{disabled},
category => $self->{enabled},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not needed, no module uses category param in their isEnabled method.

logger => $self->{logger},
registry => $self->{registry},
params => $self->{params},
Expand Down
Loading