From 47da593550c17c06fa74a6bcbbee450641d7d0e8 Mon Sep 17 00:00:00 2001 From: justme-1968 Date: Wed, 27 May 2015 10:04:16 +0000 Subject: [PATCH] 32_SYSSTAT.pm: try to use /proc/uptime instead of uptime first (by viegener) git-svn-id: https://svn.fhem.de/fhem/trunk@8640 2b470e98-0d58-463d-a4d8-8e2adae1ed80 --- fhem/FHEM/32_SYSSTAT.pm | 76 ++++++++++++++++++++++++++++++----------- 1 file changed, 57 insertions(+), 19 deletions(-) diff --git a/fhem/FHEM/32_SYSSTAT.pm b/fhem/FHEM/32_SYSSTAT.pm index d154fe5dc..61f2d3c49 100644 --- a/fhem/FHEM/32_SYSSTAT.pm +++ b/fhem/FHEM/32_SYSSTAT.pm @@ -596,21 +596,59 @@ SYSSTAT_getUptime($) return $uptime; } - my $uptime = SYSSTAT_readCmd($hash,"uptime",0); + my $uptime = SYSSTAT_readFile($hash,"/proc/uptime",""); + if($uptime) { - $uptime = $1 if( $uptime && $uptime =~ m/[[:alpha:]]{2}\s+(((\d+)\D+,?\s+)?(\d+):(\d+))/ ); - $uptime = "0 days, $uptime" if( $uptime && !$2); + $uptime = $1 if ( $uptime && $uptime =~ /^\s*([0-9.]+)\s+([0-9.]+)/ ); - if( AttrVal($name, "uptime", 0) == 2 ) { - my $days = $3?$3:0; - my $hours = $4; - my $minutes = $5; + if( AttrVal($name, "uptime", 0) != 2 ) { + # cut off partial seconds + $uptime = int( $uptime ); + my $seconds = $uptime % 60; + $uptime = int($uptime / 60); + my $minutes = $uptime % 60; + $uptime = int($uptime / 60); - $uptime = $days * 24; - $uptime += $hours; - $uptime *= 60; - $uptime += $minutes; - $uptime *= 60; + my $hours = $uptime % 24; + my $days = int($uptime / 24); + + $uptime = sprintf( "%d days, %d:%.2d", $days, $hours, $minutes); + Log3 $name, 4, "$name: uptime returned :$uptime: via proc-uptime file "; # JVI + } + + # fallback if by any reason parsing /proc/uptime does not work + } else { + my $uptime = SYSSTAT_readCmd($hash,"uptime",0); + + ############# match uptime time statement with the different formats seen on linux + # examples + # 18:52:21 up 26 days, 21:08, 2 users, load average: 0.04, 0.03, 0.05 + # 18:52:21 up 26 days, 55 min, 1 user, load average: 0.05, 0.05, 0.05 + # 18:52:21 up 55 min, 1 user, load average: 0.05, 0.05, 0.05 + # 18:52:21 up 21:08, 1 user, load average: 0.05, 0.05, 0.05 + # + # complex expression to match only the time parts of the uptime result + # $1 is complete up time information of uptime result + # $2 is # days part of the uptime + # $3 just the # from the "# days"" part or nothing if no days are given + # $4 is complete hour/minutes or # min information + # $5 is hours part if hours:min are given + # $6 is minutes part if hours:min are given + # $7 is minutes if # min is given + $uptime = $1 if ( $uptime && $uptime =~ m/[[:alpha:]]{2}\s*(((\d*)\s*[[:alnum:]]*,?)?\s+((\d+):(\d+)|(\d+)\s+[[:alpha:]]+in[[:alpha:]]*)),?/ ); + $uptime = "0 days, $uptime" if( $uptime && !$2); + if( AttrVal($name, "uptime", 0) == 2 ) { + my $days = $3?$3:0; + my $hours = $5?$5:0; + my $minutes = $6?$6:$7; + + $uptime = $days * 24; + $uptime += $hours; + $uptime *= 60; + $uptime += $minutes; + $uptime *= 60; + } + Log3 $name, 4, "$name: uptime returned :$uptime: via cmdline "; # JVI } return $uptime; @@ -776,14 +814,14 @@ SYSSTAT_getStat($)

SYSSTAT