Skip to content

Commit

Permalink
Fixes #403: Extend Rudder specific inventory with client side data
Browse files Browse the repository at this point in the history
  • Loading branch information
ncharles committed Nov 20, 2017
1 parent a533aec commit e3721f9
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/FusionInventory/Agent/Inventory.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
68 changes: 68 additions & 0 deletions lib/FusionInventory/Agent/Task/Inventory/Generic/Rudder.pm
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ use warnings;
use English qw(-no_match_vars);

use FusionInventory::Agent::Tools;
use UNIVERSAL::require;
use File::stat;


sub isEnabled {
return -r getUuidFile();
Expand Down Expand Up @@ -42,19 +45,84 @@ 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(
section => 'RUDDER', entry => $rudder
);
}

sub _getCustomProperties {
my (%params) = @_;
my $logger = $params{logger};

my $custom_properties_dir = ($OSNAME eq 'MSWin32') ? 'C:\Program Files\Rudder\hooks.d' : '/var/rudder/hooks.d';
my $custom_properties;
if (-d "$custom_properties_dir") {
my @custom_properties_list = ();
my @ordered_script_list = ();
opendir(DIR, $custom_properties_dir);
# List each file in the custom_properties directory, each files being a script
@ordered_script_list = sort readdir(DIR);
closedir(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;

# Check that the file is not world writable
my $permissions = stat($script_file);
my $retMode = $permissions->mode;
$retMode = $retMode & 0777;
if (($retMode & 002) || ($retMode & 020)) {
$logger->error("Skipping script $script_file as it is world or group writable") if $logger;
next;
}

$logger->debug2("executiong $script_file") if $logger;
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") if $logger;
next;
}

# check that it is valid JSON
eval {
my $package = "JSON::PP";
$package->require();
if ($EVAL_ERROR) {
print STDERR
"Failed to load JSON module: ($EVAL_ERROR)\n";
next;
}
my $coder = JSON::PP->new;
my $propertiesData = $coder->decode($properties);
push @custom_properties_list, $coder->encode($propertiesData);
};
if ($@) {
$logger->error("Script $script_file didn't return valid JSON entry, error is:$@") if $logger;
}
}

}
$custom_properties = "[". join(",", @custom_properties_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;
Expand Down

0 comments on commit e3721f9

Please sign in to comment.