diff --git a/bin/glpi-agent b/bin/glpi-agent index 9c7be73b1..71faf670a 100755 --- a/bin/glpi-agent +++ b/bin/glpi-agent @@ -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', @@ -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) diff --git a/bin/glpi-inventory b/bin/glpi-inventory index b392dd9c8..93c760df0 100755 --- a/bin/glpi-inventory +++ b/bin/glpi-inventory @@ -28,6 +28,7 @@ GetOptions( 'assetname-support=i', 'additional-content=s', 'backend-collect-timeout=s', + 'category=s', 'credentials=s@', 'glpi_version=s', 'html', @@ -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 diff --git a/lib/GLPI/Agent.pm b/lib/GLPI/Agent.pm index d7834cb3e..e6d45c3c9 100644 --- a/lib/GLPI/Agent.pm +++ b/lib/GLPI/Agent.pm @@ -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"}) ]; diff --git a/lib/GLPI/Agent/Config.pm b/lib/GLPI/Agent/Config.pm index 268cbee4a..48d1d1a7b 100644 --- a/lib/GLPI/Agent/Config.pm +++ b/lib/GLPI/Agent/Config.pm @@ -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, @@ -350,6 +351,7 @@ sub _checkContent { server httpd-trust no-task + category no-category required-category tasks diff --git a/lib/GLPI/Agent/Task/Inventory.pm b/lib/GLPI/Agent/Task/Inventory.pm index 032c010ee..49c44ba5a 100644 --- a/lib/GLPI/Agent/Task/Inventory.pm +++ b/lib/GLPI/Agent/Task/Inventory.pm @@ -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}); @@ -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") + $self->{enabled}->{os} = 1; + } + } # Support inventory event if ($event && !$self->setupEvent()) { @@ -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; @@ -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} ))) { $logger->debug2("module $module disabled: '$category' category disabled"); $self->{modules}->{$module}->{enabled} = 0; next; @@ -532,6 +550,7 @@ sub _runModule { datadir => $self->{datadir}, inventory => $self->{inventory}, no_category => $self->{disabled}, + category => $self->{enabled}, logger => $self->{logger}, registry => $self->{registry}, params => $self->{params},