From a3aa61a174a00b58de7ec03a804d054e29a8ee6c Mon Sep 17 00:00:00 2001 From: Javi Date: Fri, 11 Dec 2015 15:32:50 +0100 Subject: [PATCH 1/2] Improve detection to diskused_per Because in some systems the last line of `df --total` command is: total 105837756 15884756 85946856 16% - Then self.uw_diskused_perc always returned 0 --- lib/usagewatch/linux.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/usagewatch/linux.rb b/lib/usagewatch/linux.rb index f0a2b32..b51fb49 100644 --- a/lib/usagewatch/linux.rb +++ b/lib/usagewatch/linux.rb @@ -16,7 +16,10 @@ def self.uw_diskused # Show the percentage of disk used. def self.uw_diskused_perc df = `df --total` - df.split(" ").last.to_f.round(2) + + df.split(" ").reverse.each do |l| + return l.to_f.round(2) if l.include?("%") + end end # Show the percentage of CPU used From ccb281288cbbc2ec671eccbbe6228a3c331b05d3 Mon Sep 17 00:00:00 2001 From: Javi Date: Fri, 11 Dec 2015 16:10:34 +0100 Subject: [PATCH 2/2] Improved parse /proc/meminfo based on name property instead of position --- lib/usagewatch/linux.rb | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/lib/usagewatch/linux.rb b/lib/usagewatch/linux.rb index b51fb49..a9034fa 100644 --- a/lib/usagewatch/linux.rb +++ b/lib/usagewatch/linux.rb @@ -113,10 +113,14 @@ def self.uw_memused end end - @memstat = @result.split("\n").collect{|x| x.strip} - @memtotal = @memstat[0].gsub(/[^0-9]/, "") - @memactive = @memstat[5].gsub(/[^0-9]/, "") - @memactivecalc = (@memactive.to_f * 100) / @memtotal.to_f + memhash = Hash.new + @result.each_line do |l| + key, val = l.split(':') + if val.include?('kB') then val = val.gsub(/\s+kB/, ''); end + memhash["#{key}"] = val.strip + end + + @memactivecalc = (memhash["Active"].to_f * 100) / memhash["MemTotal"].to_f @memusagepercentage = @memactivecalc.round end