Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Define or Search Memory Row #13

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 22 additions & 4 deletions lib/usagewatch/linux.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down