Skip to content

Instantly share code, notes, and snippets.

@johnyzed
Created July 9, 2014 10:37
Show Gist options
  • Save johnyzed/8bed34f99667ace51016 to your computer and use it in GitHub Desktop.
Save johnyzed/8bed34f99667ace51016 to your computer and use it in GitHub Desktop.
facter script that create custom facts on the memory of the node
require 'facter'
{ :MemorySizeRaw => "MemTotal",
:MemoryFreeRaw => "MemFree",
:SwapSizeRaw => "SwapTotal",
:SwapFreeRaw => "SwapFree"
}.each do |fact, name|
Facter.add(fact) do
confine :kernel => :linux
setcode do
memsize_raw = ""
Thread::exclusive do
File.readlines("/proc/meminfo").each do |l|
memsize_raw = $1.to_i if l =~ /^#{name}:\s+(\d+)\s+\S+/
# MemoryFree == memfree + cached + buffers
# (assume scales are all the same as memfree)
if name == "MemFree" &&
l =~ /^(?:Buffers|Cached):\s+(\d+)\s+\S+/
memsize_raw += $1.to_i
end
end
end
memsize_raw
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment