Skip to content

Commit e3721f9

Browse files
committed
Fixes #403: Extend Rudder specific inventory with client side data
1 parent a533aec commit e3721f9

File tree

2 files changed

+69
-1
lines changed

2 files changed

+69
-1
lines changed

lib/FusionInventory/Agent/Inventory.pm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ my %fields = (
7777
CMD/ ],
7878
REGISTRY => [ qw/NAME REGVALUE HIVE/ ],
7979
REMOTE_MGMT => [ qw/ID TYPE/ ],
80-
RUDDER => [ qw/AGENT UUID HOSTNAME SERVER_ROLES AGENT_CAPABILITIES/ ],
80+
RUDDER => [ qw/AGENT UUID HOSTNAME SERVER_ROLES AGENT_CAPABILITIES CUSTOM_PROPERTIES/ ],
8181
SLOTS => [ qw/DESCRIPTION DESIGNATION NAME STATUS/ ],
8282
SOFTWARES => [ qw/COMMENTS FILESIZE FOLDER FROM HELPLINK INSTALLDATE
8383
NAME NO_REMOVE RELEASE_TYPE PUBLISHER

lib/FusionInventory/Agent/Task/Inventory/Generic/Rudder.pm

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ use warnings;
66
use English qw(-no_match_vars);
77

88
use FusionInventory::Agent::Tools;
9+
use UNIVERSAL::require;
10+
use File::stat;
11+
912

1013
sub isEnabled {
1114
return -r getUuidFile();
@@ -42,19 +45,84 @@ sub doInventory {
4245
# Get agent capabilities
4346
my @agentCapabilities = _listAgentCapabilities();
4447

48+
my $customProperties = _getCustomProperties(logger => $logger);
49+
4550
my $rudder = {
4651
HOSTNAME => $hostname,
4752
UUID => $Uuid,
4853
AGENT => \@agents,
4954
SERVER_ROLES => { SERVER_ROLE => \@serverRoles },
5055
AGENT_CAPABILITIES => { AGENT_CAPABILITY => \@agentCapabilities },
56+
CUSTOM_PROPERTIES => $customProperties,
5157
};
5258

5359
$inventory->addEntry(
5460
section => 'RUDDER', entry => $rudder
5561
);
5662
}
5763

64+
sub _getCustomProperties {
65+
my (%params) = @_;
66+
my $logger = $params{logger};
67+
68+
my $custom_properties_dir = ($OSNAME eq 'MSWin32') ? 'C:\Program Files\Rudder\hooks.d' : '/var/rudder/hooks.d';
69+
my $custom_properties;
70+
if (-d "$custom_properties_dir") {
71+
my @custom_properties_list = ();
72+
my @ordered_script_list = ();
73+
opendir(DIR, $custom_properties_dir);
74+
# List each file in the custom_properties directory, each files being a script
75+
@ordered_script_list = sort readdir(DIR);
76+
closedir(DIR);
77+
while (my $file = shift @ordered_script_list) {
78+
my $script_file = $custom_properties_dir . "/" . $file;
79+
if (-f $script_file) {
80+
next if ($file =~ m/^\./);
81+
# Ignore non-executable file, or folders
82+
next unless -x $script_file;
83+
84+
# Check that the file is not world writable
85+
my $permissions = stat($script_file);
86+
my $retMode = $permissions->mode;
87+
$retMode = $retMode & 0777;
88+
if (($retMode & 002) || ($retMode & 020)) {
89+
$logger->error("Skipping script $script_file as it is world or group writable") if $logger;
90+
next;
91+
}
92+
93+
$logger->debug2("executiong $script_file") if $logger;
94+
my $properties = qx($script_file);
95+
my $exit_code = $? >> 8;
96+
if ($exit_code > 0) {
97+
$logger->error("Script $script_file failed to run properly, with exit code $exit_code") if $logger;
98+
next;
99+
}
100+
101+
# check that it is valid JSON
102+
eval {
103+
my $package = "JSON::PP";
104+
$package->require();
105+
if ($EVAL_ERROR) {
106+
print STDERR
107+
"Failed to load JSON module: ($EVAL_ERROR)\n";
108+
next;
109+
}
110+
my $coder = JSON::PP->new;
111+
my $propertiesData = $coder->decode($properties);
112+
push @custom_properties_list, $coder->encode($propertiesData);
113+
};
114+
if ($@) {
115+
$logger->error("Script $script_file didn't return valid JSON entry, error is:$@") if $logger;
116+
}
117+
}
118+
119+
}
120+
$custom_properties = "[". join(",", @custom_properties_list) . "]";
121+
}
122+
return $custom_properties;
123+
}
124+
125+
58126
sub _listServerRoles {
59127
my $server_roles_dir = ($OSNAME eq 'MSWin32') ? 'C:\Program Files\Rudder\etc\server-roles.d' : '/opt/rudder/etc/server-roles.d';
60128
my @server_roles;

0 commit comments

Comments
 (0)