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

fix(OSRelease): add fallback when os-release file is not in key = value format #479

Merged
merged 1 commit into from
Aug 13, 2024
Merged
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
90 changes: 56 additions & 34 deletions lib/Ocsinventory/Agent/Backend/OS/Linux/Distro/OSRelease.pm
Original file line number Diff line number Diff line change
Expand Up @@ -6,59 +6,81 @@ use strict;
sub check {
my $params = shift;
my $common = $params->{common};
$common->can_read ("/etc/os-release")
$common->can_read("/etc/os-release");
}

sub run {

my $name;
my $version;
my $description;
my $version = "";
my $description = "";

my $params = shift;
my $common = $params->{common};
my $first_non_commented_line;

open V, "/etc/os-release" or warn;
foreach (<V>) {
next if /^#/;
$name = $1 if (/^NAME="?([^"]+)"?/);
$version = $1 if (/^VERSION_ID="?([^"]+)"?/);
$description=$1 if (/^PRETTY_NAME="?([^"]+)"?/);
open my $v, '<', "/etc/os-release" or warn;
foreach (<$v>) {
next if /^#/;
$first_non_commented_line //= $_; # Capture the first non-commented line
$name = $1 if (/^NAME="?([^"]+)"?/);
$version = $1 if (/^VERSION_ID="?([^"]+)"?/);
$description = $1 if (/^PRETTY_NAME="?([^"]+)"?/);
}
close V;
chomp($name);
chomp($version);
chomp($description);
close $v;

$name = $first_non_commented_line unless defined $name;

chomp($name) if defined $name;
chomp($version) if defined $version;
chomp($description) if defined $description;

# Debian version number is set in/etc/debian_version file
if (-r "/etc/debian_version") {
if (`uname -v` =~ /debian/i) {
open V, "/etc/debian_version" or warn;
foreach (<V>) {
$version = $1 if ($_ =~ /^(\d+.*)/);
if ( -r "/etc/debian_version" ) {
if ( `uname -v` =~ /debian/i ) {
open my $v, '<', "/etc/debian_version" or warn;
foreach (<$v>) {
$version = $1 if ( $_ =~ /^(\d+.*)/ );
}
close V;
chomp($version);
}
close $v;
chomp($version) if defined $version;
}
}

# CentOS exact version number is set in /etc/centos-release file
if (-r "/etc/centos-release") {
open V, "/etc/centos-release" or warn;
foreach ($description=<V>) {
$version = $1 if ($_ =~ /(\d+\.\d+)./g);
my @centOsRedHatfiles = ( "/etc/centos-release", "/etc/redhat-release" );
foreach my $file (@centOsRedHatfiles) {
if ( -r $file ) {
open my $v, '<', $file or warn;
foreach my $line (<$v>) {
$version = $1 if ( $line =~ /(\d+\.\d+)./g );
$description = $line;
}
close $v;
chomp($version) if defined $version;
chomp($description) if defined $description;
last; # Exit the loop after finding the first matching file
}
close V;
chomp($version);
chomp($description);
}

$common->setHardware({
OSNAME => "$name $version",
OSVERSION => $version,
OSCOMMENTS => $description,
});
if ( !defined $version || $version eq "" ) {

# if no version found try to retrieve it on the name with format x.x or x.x.x
$version = $1 if ( $name =~ /^(\d+.*)/ );
}

my $fullosname = $name;
$fullosname .= " $version" if $version;
$fullosname =~ s/^\s+|\s+$//g;

$common->setHardware(
{
OSNAME => $fullosname,
OSVERSION => $version,
OSCOMMENTS => $description,
}
);

}

1;
1;