From a1cb9006c0e717a39808ec5ad2e2ecb9411f724b Mon Sep 17 00:00:00 2001 From: Geoff Evans Date: Wed, 27 Aug 2014 10:17:27 +1200 Subject: [PATCH 1/3] Define Memory Locations Allows the user to define the lines that memtotal and memactive are on. If not supplied it will try finding the lines within the file. --- lib/usagewatch/linux.rb | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/lib/usagewatch/linux.rb b/lib/usagewatch/linux.rb index f0a2b32..247fbdc 100644 --- a/lib/usagewatch/linux.rb +++ b/lib/usagewatch/linux.rb @@ -103,16 +103,33 @@ def self.uw_udpused end # Show the percentage of Active Memory used - def self.uw_memused + def self.uw_memused(memtotal_place, memactive_place) if File.exists?("/proc/meminfo") File.open("/proc/meminfo", "r") do |file| @result = file.read end end - + @memstat = @result.split("\n").collect{|x| x.strip} - @memtotal = @memstat[0].gsub(/[^0-9]/, "") - @memactive = @memstat[5].gsub(/[^0-9]/, "") + + @memstat.each_with_index do |stat, index| + if memtotal_place == nil + if stat.include?("MemTotal") + memtotal_place = index + end + end + if memactive_place == nil + if stat.include?("Active") + memactive_place = index + end + end + + end + if memtotal_place == nil or memactive_place == nil + raise "Unable to locate memory total and memory free entries. Please define with uw_memused(memtotal_place, memactive_place) to list actual line locations" + end + @memtotal = @memstat[memtotal_place].gsub(/[^0-9]/, "") + @memactive = @memstat[memactive_place].gsub(/[^0-9]/, "") @memactivecalc = (@memactive.to_f * 100) / @memtotal.to_f @memusagepercentage = @memactivecalc.round end From 8545780249d47ff0aa34f449eda4fca886c89991 Mon Sep 17 00:00:00 2001 From: Geoff Evans Date: Wed, 27 Aug 2014 10:42:12 +1200 Subject: [PATCH 2/3] . --- lib/usagewatch/linux.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/usagewatch/linux.rb b/lib/usagewatch/linux.rb index 247fbdc..b000607 100644 --- a/lib/usagewatch/linux.rb +++ b/lib/usagewatch/linux.rb @@ -103,7 +103,7 @@ def self.uw_udpused end # Show the percentage of Active Memory used - def self.uw_memused(memtotal_place, memactive_place) + def self.uw_memused(memtotal_place = nil, memactive_place = nil) if File.exists?("/proc/meminfo") File.open("/proc/meminfo", "r") do |file| @result = file.read From b5358fcebdd5fa018fabf86081ed26aad969669f Mon Sep 17 00:00:00 2001 From: Geoff Evans Date: Wed, 27 Aug 2014 10:46:37 +1200 Subject: [PATCH 3/3] If defined don't look at all --- lib/usagewatch/linux.rb | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/lib/usagewatch/linux.rb b/lib/usagewatch/linux.rb index b000607..51804d4 100644 --- a/lib/usagewatch/linux.rb +++ b/lib/usagewatch/linux.rb @@ -111,20 +111,21 @@ def self.uw_memused(memtotal_place = nil, memactive_place = nil) end @memstat = @result.split("\n").collect{|x| x.strip} - - @memstat.each_with_index do |stat, index| - if memtotal_place == nil - if stat.include?("MemTotal") - memtotal_place = index - end - end - if memactive_place == nil - if stat.include?("Active") - memactive_place = index - end + if memtotal_place == nil or memactive_place == nil + @memstat.each_with_index do |stat, index| + if memtotal_place == nil + if stat.include?("MemTotal") + memtotal_place = index + end + end + if memactive_place == nil + if stat.include?("Active") + memactive_place = index + end + end + end - - end + end if memtotal_place == nil or memactive_place == nil raise "Unable to locate memory total and memory free entries. Please define with uw_memused(memtotal_place, memactive_place) to list actual line locations" end