From 1e18741e2cd5372a2c79c4e6be75c05b4a2f4cd3 Mon Sep 17 00:00:00 2001 From: Nicolas Charles Date: Tue, 14 Nov 2017 14:51:26 +0100 Subject: [PATCH] Fixes #403: Extend Rudder specific inventory with client side data --- lib/FusionInventory/Agent/Inventory.pm | 2 +- .../Agent/Task/Inventory/Generic/Rudder.pm | 48 +++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/lib/FusionInventory/Agent/Inventory.pm b/lib/FusionInventory/Agent/Inventory.pm index 7467a30796..08a7fa6c2f 100644 --- a/lib/FusionInventory/Agent/Inventory.pm +++ b/lib/FusionInventory/Agent/Inventory.pm @@ -77,7 +77,7 @@ my %fields = ( CMD/ ], REGISTRY => [ qw/NAME REGVALUE HIVE/ ], REMOTE_MGMT => [ qw/ID TYPE/ ], - RUDDER => [ qw/AGENT UUID HOSTNAME SERVER_ROLES AGENT_CAPABILITIES/ ], + RUDDER => [ qw/AGENT UUID HOSTNAME SERVER_ROLES AGENT_CAPABILITIES CUSTOM_PROPERTIES/ ], SLOTS => [ qw/DESCRIPTION DESIGNATION NAME STATUS/ ], SOFTWARES => [ qw/COMMENTS FILESIZE FOLDER FROM HELPLINK INSTALLDATE NAME NO_REMOVE RELEASE_TYPE PUBLISHER diff --git a/lib/FusionInventory/Agent/Task/Inventory/Generic/Rudder.pm b/lib/FusionInventory/Agent/Task/Inventory/Generic/Rudder.pm index b2fd04d2a1..8878c60eff 100644 --- a/lib/FusionInventory/Agent/Task/Inventory/Generic/Rudder.pm +++ b/lib/FusionInventory/Agent/Task/Inventory/Generic/Rudder.pm @@ -7,6 +7,8 @@ use English qw(-no_match_vars); use FusionInventory::Agent::Tools; +use JSON::PP; + sub isEnabled { return -r getUuidFile(); } @@ -42,12 +44,15 @@ sub doInventory { # Get agent capabilities my @agentCapabilities = _listAgentCapabilities(); + my $customProperties = _getCustomProperties(logger => $logger); + my $rudder = { HOSTNAME => $hostname, UUID => $Uuid, AGENT => \@agents, SERVER_ROLES => { SERVER_ROLE => \@serverRoles }, AGENT_CAPABILITIES => { AGENT_CAPABILITY => \@agentCapabilities }, + CUSTOM_PROPERTIES => $customProperties, }; $inventory->addEntry( @@ -55,6 +60,49 @@ sub doInventory { ); } +sub _getCustomProperties { + my (%params) = @_; + my $logger = $params{logger}; + + my $custom_properties_dir = ($OSNAME eq 'MSWin32') ? 'C:\Program Files\Rudder\local\hooks.d' : '/var/rudder/local/hooks.d'; + my $custom_properties; + if (-d "$custom_properties_dir") { + my @custom_propertes_list = (); + opendir(DIR, $custom_properties_dir); # or die $!; + # List each file in the custom_properties directory, each files being a script + my @ordered_script_list = sort readdir(DIR); + while (my $file = shift @ordered_script_list) { + my $script_file = $custom_properties_dir . "/" . $file; + if (-f $script_file) { + next if ($file =~ m/^\./); + # Ignore non-executable file, or folders + next unless -x $script_file; + my $properties = qx($script_file); + my $exit_code = $? >> 8; + if ($exit_code > 0) { + $logger->error("Script $script_file failed to run properly, with exit code $exit_code"); + next; + } + + # check that it is valid JSON + eval { + my $coder = JSON::PP->new; + my $propertiesData = $coder->decode($properties); + push @custom_propertes_list, $coder->encode($propertiesData); + }; + if ($@) { + $logger->error("Script $script_file didn't return valid JSON entry, error is:$@"); + } + } + + } + closedir(DIR); + $custom_properties = "[". join(",", @custom_propertes_list) . "]"; + } + return $custom_properties; +} + + sub _listServerRoles { my $server_roles_dir = ($OSNAME eq 'MSWin32') ? 'C:\Program Files\Rudder\etc\server-roles.d' : '/opt/rudder/etc/server-roles.d'; my @server_roles;