Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: fqdn value for assetname support option #538

Merged
merged 2 commits into from
Nov 24, 2023
Merged
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
9 changes: 9 additions & 0 deletions Changes
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@ Revision history for GLPI agent

inventory:
* PR #531: Add SentinelOne Antivirus support on Linux, thanks to @MarcSamD
* Feature: Update support assetname-support as option for agent on most unix
if 1 (the default), the short hostname is used as asset name
if 2, the as-is hostname (can be fqdn) is used as asset name
if 3, the fqdn hostname is used as asset name
this feature does not apply on MacOS or Windows
this feature now works for remote ssh and for option 3, it requires perl installed
on targeted computer and perl mode enabled on defined remote.
* Feature: Make assetname-support option also works to compute agent deviceid when
unknown

netdiscovery/netinventory:
* Enhanced Toshiba printers support
Expand Down
10 changes: 9 additions & 1 deletion lib/GLPI/Agent.pm
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,15 @@ sub _handlePersistentState {

if (!$self->{deviceid} && !$data->{deviceid}) {
# compute an unique agent identifier, based on host name and current time
my $hostname = getHostname();
my %config = ();
if ($self->{config}->{'assetname-support'}) {
if ($self->{config}->{'assetname-support'} == 1) {
$config{short} = 1;
} elsif ($self->{config}->{'assetname-support'} == 3) {
$config{fqdn} = 1;
}
}
my $hostname = getHostname(%config);

my ($year, $month , $day, $hour, $min, $sec) =
(localtime (time))[5, 4, 3, 2, 1, 0];
Expand Down
2 changes: 1 addition & 1 deletion lib/GLPI/Agent/Task/Inventory/Generic/Domains.pm
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ sub doInventory {

# attempt to deduce the actual domain from the host name
# and fallback on the domain search list
my $hostname = getHostname();
my $hostname = getHostname(fqdn => 1);
my $pos = index $hostname, '.';

if ($pos > 0) {
Expand Down
2 changes: 2 additions & 0 deletions lib/GLPI/Agent/Task/Inventory/Generic/Hostname.pm
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ sub doInventory {
my $hostname;
if ($assetname_support == 2) {
$hostname = getHostname();
} elsif ($assetname_support == 3) {
$hostname = getHostname(fqdn => 1);
} else {
$hostname = getHostname(short => 1);
}
Expand Down
34 changes: 27 additions & 7 deletions lib/GLPI/Agent/Tools/Hostname.pm
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,22 @@ BEGIN {
sub getHostname {
my (%params) = @_;

my $remote = $GLPI::Agent::Tools::remote;
return $remote->getRemoteHostname() if $remote;
my $hostname;

my $hostname = $OSNAME eq 'MSWin32' ?
_getHostnameWindows() :
_getHostnameUnix() ;
my $remote = $GLPI::Agent::Tools::remote;
if ($remote) {
if ($params{fqdn}) {
$hostname = $remote->getRemoteFQDN()
and $hostname =~ s/\.$//; # the module may return a trailing dot
}
# Fallback on getgetRemoteHostname if fqdn was requested and failed
$hostname = $remote->getRemoteHostname()
if empty($hostname);
} else {
$hostname = $OSNAME eq 'MSWin32' ? _getHostnameWindows() : _getHostnameUnix(%params);
}

# Support assetname-support = 1 option
if ($params{short}) {
$hostname =~ s/\..*$//;
}
Expand All @@ -45,9 +54,20 @@ sub getHostname {
}

sub _getHostnameUnix {
my (%params) = @_;

Sys::Hostname->require();
return Sys::Hostname::hostname();
my $hostname;

if ($params{fqdn}) {
Net::Domain->require();
$hostname = Net::Domain::hostfqdn();
$hostname =~ s/\.$//; # the module may return a trailing dot
} else {
Sys::Hostname->require();
$hostname = Sys::Hostname::hostname();
}

return $hostname;
}

sub _getHostnameWindows {
Expand Down