Project

General

Profile

« Previous | Next » 

Revision a59889a7

Added by Matthieu CERDA over 7 years ago

Fixes #7093: Include Timezone in Inventory

View differences:

rudder-agent/SOURCES/patches/fusioninventory/0023-Fixes-9278-add-timezone-information-to-inventory.patch
Fixes #9278: Add Timezone information in inventory
https://www.rudder-project.org/redmine/issues/9278
--- a/README
+++ b/README
@@ -40,6 +40,8 @@
* Net::CUPS, for printers detection
* Parse::EDID, for EDID data parsing
+* DateTime, DateTime::TimeZone and DateTime::TimeZone::Local::{Win32,Unix}
+ for reliable timezone name extraction
Optional programs:
--- a/Makefile.PL
+++ b/Makefile.PL
@@ -59,6 +59,10 @@
# Inventory
recommends 'Parse::EDID' => '0';
recommends 'Net::CUPS' => 0.60 if $OSNAME ne 'MSWin32';
+recommends 'DateTime' => '0';
+recommends 'DateTime::TimeZone' => '0';
+recommends 'DateTime::TimeZone::Local::Unix' => '0' if $OSNAME ne 'MSWin32';
+recommends 'DateTime::TimeZone::Local::Win32' => '0' if $OSNAME eq 'MSWin32';
# Deploy
recommends 'Archive::Extract' => '0';
--- a/lib/FusionInventory/Agent/Inventory.pm
+++ b/lib/FusionInventory/Agent/Inventory.pm
@@ -25,7 +25,7 @@
VMNAME VMHOSTSERIAL/ ],
OPERATINGSYSTEM => [ qw/KERNEL_NAME KERNEL_VERSION NAME VERSION FULL_NAME
SERVICE_PACK INSTALL_DATE FQDN DNS_DOMAIN
- SSH_KEY ARCH BOOT_TIME/ ],
+ SSH_KEY ARCH BOOT_TIME TIMEZONE/ ],
ACCESSLOG => [ qw/USERID LOGDATE/ ],
ANTIVIRUS => [ qw/COMPANY ENABLED GUID NAME UPTODATE VERSION/ ],
--- /dev/null 2016-10-06 11:58:32.025981414 +0200
+++ b/lib/FusionInventory/Agent/Task/Inventory/Generic/Timezone.pm 2016-10-07 17:25:35.261739989 +0200
@@ -0,0 +1,97 @@
+package FusionInventory::Agent::Task::Inventory::Generic::Timezone;
+
+use strict;
+use warnings;
+
+use English qw(-no_match_vars);
+use UNIVERSAL::require;
+
+use POSIX;
+use Time::Local;
+
+use FusionInventory::Agent::Tools;
+
+my $seen;
+
+sub isEnabled {
+
+ # No specific dependencies necessary
+ return 1;
+}
+
+sub doInventory {
+ my (%params) = @_;
+
+ my $inventory = $params{inventory};
+ my $logger = $params{logger};
+
+ # Compute a timezone offset like '+0200' using the difference between UTC and local time
+ # Might require merging with detectLocalTimeOffset (macOS inventory) in the future
+
+ ## Get the local time
+ my @t = localtime(time);
+
+ ## Compute the time offset in seconds between local and UTC time (relative and absolute)
+ my $utc_offset_seconds = timegm(@t) - timelocal(@t);
+ my $utc_offset_seconds_abs = abs($utc_offset_seconds);
+
+ ## If relative and absolute values do not match, it means the offset is negative
+ my $offset_sign =
+ $utc_offset_seconds == $utc_offset_seconds_abs ? '+' : '-';
+
+ ## Format the offset string: sign + H (XX) + M (XX)
+ my $tz_offset =
+ strftime( $offset_sign . "\%H\%M", gmtime($utc_offset_seconds_abs) );
+
+ # Assume by default that the offset is empty (safe default in case something goes wrong below)
+ my $tz_name = '';
+
+ # Timezone name extraction will use one of the following sources:
+ # * DateTime::TimeZone and DateTime::TimeZone::Local::{Win32,Unix} => 'Europe/Paris'
+ # * tzutil (Win 7+, Win 2008+) => 'Romance Standard Time'
+ # * strftime '%Z' => 'CEST'
+ #
+ # strftime will not be used on Windows, as it returns unpredictable localized TZ names. It means
+ # that if reliable timezone name extraction is wanted, DateTime::TimeZone MUST be used.
+ if (
+ ( DateTime::TimeZone->require() )
+ && ( $OSNAME eq 'MSWin32'
+ ? DateTime::TimeZone::Local::Win32->require()
+ : DateTime::TimeZone::Local::Unix->require() )
+ )
+ {
+ $logger->debug("Using DateTime::TimeZone to get the timezone name");
+ $tz_name = DateTime::TimeZone->new( name => 'local' )->name();
+ }
+ elsif ( ( $OSNAME eq 'MSWin32' ) || ( canRun('tzutil') ) ) {
+
+ $logger->debug("Using tzutil to get the timezone name");
+
+ my $handle = getFileHandle(
+ logger => $logger,
+ command => 'tzutil /g',
+ );
+
+ while ( my $line = <$handle> ) {
+ $tz_name = $line;
+ }
+ close $handle;
+
+ }
+ elsif ( $OSNAME ne 'MSWin32' ) {
+ $logger->debug("Using strftime to get the timezone name");
+ $tz_name = strftime( "%Z", localtime() );
+ }
+
+ $inventory->setOperatingSystem(
+ {
+ TIMEZONE => {
+ NAME => $tz_name,
+ OFFSET => $tz_offset,
+ }
+ }
+ );
+
+}
+
+1;

Also available in: Unified diff