From e961d30284c0c27f4f0f88ece6db073180d81b99 Mon Sep 17 00:00:00 2001 From: Avinash D'Silva Date: Wed, 20 Apr 2016 10:45:07 +0100 Subject: [PATCH] Update linux.rb --- lib/usagewatch/linux.rb | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/lib/usagewatch/linux.rb b/lib/usagewatch/linux.rb index f0a2b32..51804d4 100644 --- a/lib/usagewatch/linux.rb +++ b/lib/usagewatch/linux.rb @@ -103,16 +103,34 @@ def self.uw_udpused end # Show the percentage of Active Memory used - def self.uw_memused + 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 end end - + @memstat = @result.split("\n").collect{|x| x.strip} - @memtotal = @memstat[0].gsub(/[^0-9]/, "") - @memactive = @memstat[5].gsub(/[^0-9]/, "") + 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 + 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