Skip to content

Commit

Permalink
fix: Filter invalid firmware date for at least HP peripherals
Browse files Browse the repository at this point in the history
Closes #781
  • Loading branch information
g-bougard committed Dec 6, 2024
1 parent 15cea42 commit 165bf96
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 2 deletions.
1 change: 1 addition & 0 deletions Changes
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ netdiscovery/netinventory:
* fix #769: Added Intelbras devices support
* don't send discovery xml to server target for remoteinventory task event initiated
from toolbox
* fix #781: Filter invalid firmware date on HP peripherals

deploy:
* Fix checks on command run and clarify reason of success or failure. This fixes
Expand Down
4 changes: 2 additions & 2 deletions lib/GLPI/Agent/SNMP/Device.pm
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ sub getFirmwareDateByMibSupport {

return unless $self->{MIBSUPPORT};

return $self->{MIBSUPPORT}->getMethod('getFirmwareDate');
return getCanonicalDate($self->{MIBSUPPORT}->getMethod('getFirmwareDate'));
}

sub getMacAddressByMibSupport {
Expand Down Expand Up @@ -333,7 +333,7 @@ sub setFirmware {
NAME => $self->{MODEL} || 'device',
DESCRIPTION => 'device firmware',
TYPE => 'device',
DATE => $self->getFirmwareDateByMibSupport(),
DATE => $self->getFirmwareDateByMibSupport() // undef,
VERSION => $self->{FIRMWARE},
MANUFACTURER => $self->{MANUFACTURER}
});
Expand Down
58 changes: 58 additions & 0 deletions lib/GLPI/Agent/Tools/SNMP.pm
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ our @EXPORT = qw(
getCanonicalConstant
getCanonicalMemory
getCanonicalCount
getCanonicalDate
isInteger
getRegexpOidMatch
);
Expand Down Expand Up @@ -150,6 +151,63 @@ sub getRegexpOidMatch {
return qr/^$match/;
}

my %M = qw(Jan 1 Feb 2 Mar 3 Apr 4 May 5 Jun 6 Jul 7 Aug 8 Sep 9 Oct 10 Nov 11 Dec 12);
my $months = join('|', keys(%M));
my $days = "Mon|Tue|Wed|Thu|Fri|Sat|Sun";
my $first = join("|", map { "0?$_" } 1..9);
my $month = join("|", $first, 10..12);
my $month2 = join("|", map { sprintf("%02d", $_) } 1..12);
my $day = join("|", $first, 10..31);
my $day2 = join("|", map { sprintf("%02d", $_) } 1..31);
my $hour = join("|", map { sprintf("%02d", $_) } 0..23);
my $min = join("|", map { sprintf("%02d", $_) } 0..59);
my $sec = $min;
my $year = "[1-9][0-9]{3}";

# Return date if possible
sub getCanonicalDate {
my ($value) = @_;

return if empty($value);

# Match on 'D M d H:i:s Y'
if ($value =~ /^(?:$days) ($months) +($day) (?:$hour):(?:$min):(?:$sec) .*($year)$/) {
return sprintf("%4d-%02d-%02d", $3, $M{$1}, $2);
}

# Match 'D M d, Y H:i:s' as in "Wed Aug 01, 2012 05:50:43PM"
if ($value =~ /^(?:$days) ($months) +($day), ($year) /) {
return sprintf("%4d-%02d-%02d", $3, $M{$1}, $2);
}

# Match on 'Y-m-d\TH:i:sZ' and others with same prefix
if ($value =~ /^($year)-($month)-($day)/) {
return sprintf("%4d-%02d-%02d", $1, $2, $3);
}

# Match on 'd/m/Y H:i:s' and others
if ($value =~ m{^($day)/($month)/($year)} ) {
return sprintf("%4d-%02d-%02d", $3, $2, $1);
}

# Match on 'm/d/Y'
if ($value =~ m{^($month)/($day)/($year)} ) {
return sprintf("%4d-%02d-%02d", $3, $1, $2);
}

# Match on 'd.m.Y'
if ($value =~ /^($day)\.($month)\.($year)/ ) {
return sprintf("%4d-%02d-%02d", $3, $2, $1);
}

# Match on 'Ymd'
if ($value =~ /^($year)($month2)($day2)$/ ) {
return sprintf("%4d-%02d-%02d", $1, $2, $3);
}

return;
}

1;
__END__
Expand Down

0 comments on commit 165bf96

Please sign in to comment.