Skip to content

Commit

Permalink
Merge pull request Normation#301 from Kegeruneku/bug_4721/int/4721_in…
Browse files Browse the repository at this point in the history
…tegrate_aix_process_date_patch

Bug 4721/int/4721 integrate aix process date patch
  • Loading branch information
Kegeruneku committed Apr 17, 2014
2 parents 67db4cb + 0d6e24b commit b47a9fa
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 0 deletions.
1 change: 1 addition & 0 deletions rudder-agent/SOURCES/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ localdepends: ./initial-promises ./detect_os.sh ./files ./fusioninventory-agent
mv $(TMP_DIR)/FusionInventory-Agent-2.3.6 ./fusioninventory-agent-2.3.6
$(PATCH) -d ./fusioninventory-agent-2.3.6 -p1 < ./patches/fusioninventory/2.3.6/0001-4096-char-line-truncation.patch
$(PATCH) -d ./fusioninventory-agent-2.3.6 -p1 < ./patches/fusioninventory/2.3.6/0002-install-d-mkdir-p.patch
$(PATCH) -d ./fusioninventory-agent-2.3.6 -p1 < ./patches/fusioninventory/2.3.6/0003-unix-ps-etime-usage.patch
$(PATCH) -d ./fusioninventory-agent-2.3.6 -p1 < ./patches/fusioninventory/2.3.6/0004-aix-microcode-version-retrieval.patch
# Fix a lsusb invocation that crashes some SLES machines
$(FIND) ./fusioninventory-agent-2.3.6 -iname "USB.pm" -exec rm "{}" \;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
--- a/lib/FusionInventory/Agent/Tools/Unix.pm 2014-04-17 16:06:39.112811462 +0200
+++ a/lib/FusionInventory/Agent/Tools/Unix.pm 2014-04-17 16:08:50.985349955 +0200
@@ -285,7 +285,7 @@
sub _getProcessesOther {
my (%params) = (
command =>
- 'ps -A -o user,pid,pcpu,pmem,vsz,tty,stime,time,' .
+ 'ps -A -o user,pid,pcpu,pmem,vsz,tty,etime,time' . ',' .
($OSNAME eq 'solaris' ? 'comm' : 'command'),
@_
);
@@ -295,15 +295,15 @@
# skip headers
my $line = <$handle>;

- my (undef, undef, undef, $day, $month, $year) = localtime(time());
- $year = $year + 1900;
- $month = $month + 1;
+ # get the current timestamp
+ my $localtime = time();

my @processes;

while ($line = <$handle>) {
+
next unless $line =~
- /^
+ /^ \s*
(\S+) \s+
(\S+) \s+
(\S+) \s+
@@ -312,15 +312,16 @@
(\S+) \s+
(\S+) \s+
\S+ \s+
- (.*\S)
+ (\S.*\S)
/x;
+
my $user = $1;
my $pid = $2;
my $cpu = $3;
my $mem = $4;
my $vsz = $5;
my $tty = $6;
- my $start = $7;
+ my $etime = $7;
my $cmd = $8;

# Rudder #4314/Alex Tkatchenko: Workaround for a CFEngine bug that truncates
@@ -337,7 +338,7 @@
MEM => $mem,
VIRTUALMEMORY => $vsz,
TTY => $tty,
- STARTED => _getProcessStartTime($start, $day, $month, $year, $pid),
+ STARTED => _getProcessStartTime($pid,$localtime,$etime),
CMD => $cmd
};
}
@@ -372,43 +373,45 @@
);
my $monthPattern = join ('|', keys %month);

-# consistant time format
-sub _getProcessStartTime {
- my ($start, $day, $month, $year, $pid) = @_;
-
- if ($start =~ /^(\d{1,2}):(\d{2})/) {
- # 10:00PM
- return sprintf("%04d-%02d-%02d %s", $year, $month, $day, $start);
- }
+# Computes a consistent process starting time from either the process
+# PID (using /proc) or using the process etime value.
+# Arguments:
+# - pid : Process pid
+# - localtime : Current timestamp
+# - elapsedtime : Process etime value

- if ($start =~ /^(Mon|Tue|Wed|Thu|Fri|Sat|Sun)(\d{2})[AP]M/) {
- # Sat03PM
- my $start_day = $2;
- return sprintf("%04d-%02d-%02d", $year, $month, $start_day);
- }
+sub _getProcessStartTime {

- if ($start =~ /^($monthPattern)(\d{2})/) {
- # Apr03
- my $start_month = $month{$1};
- my $start_day = $2;
- return sprintf("%04d-%02d-%02d", $year, $start_month, $start_day);
- }
+ my $pid = shift;
+ my $localtime = shift;
+ my $elapsedtime = shift;

- if ($start =~ /^(\d{1,2})($monthPattern)\d{1,2}/) {
- # 5Oct10
- my $start_day = $1;
- my $start_month = $month{$2};
- return sprintf("%04d-%02d-%02d", $year, $start_month, $start_day);
- }
+ my ($min, $hour, $day, $month, $year);

if (-f "/proc/$pid") {
# this will work only on OS with /proc/$pid like Linux and FreeBSD
my $stat = stat("/proc/$pid");
- my (undef, $min, $hour, $day, $month, $year) = localtime($stat->ctime());
- $year = $year + 1900;
- $month = $month + 1;
- return sprintf("%04d-%02d-%02d %s:%s", $year, $month, $day, $hour, $min);
+ (undef, $min, $hour, $day, $month, $year) = localtime($stat->ctime());
+ } else {
+ # POSIX specifies that ps etime entry looks like [[dd-]hh:]mm:ss
+ # if either day and hour are not present then they will eat
+ # up the minutes and seconds so split on a non digit and reverse it:
+ my ($psec, $pmin, $phour, $pday) = reverse split(/\D/, $elapsedtime);
+
+ # Compute a timestamp from the process etime value
+ # (ternary operators prevent perl from complaining about possibly undefined
+ # values, initializing them as 0)
+ my $etime_secs = $psec+($pmin*60)+($phour?$phour*60*60:0)+($pday?$pday*24*60*60:0);
+
+ # Substract this timestamp from the current time, creating the date at which
+ # the process was launched
+ (undef, $min, $hour, $day, $month, $year) = localtime($localtime-$etime_secs);
}
+
+ # Output the final date, after completing it (time + UNIX epoch)
+ $year = $year + 1900;
+ $month = $month + 1;
+ return sprintf("%04d-%02d-%02d %02d:%02d", $year, $month, $day, $hour, $min);
}

sub getRoutingTable {

0 comments on commit b47a9fa

Please sign in to comment.