Class: WatchmonkeyCli::Checkers::UnixMemory

Inherits:
WatchmonkeyCli::Checker show all
Defined in:
lib/watchmonkey_cli/checkers/unix_memory.rb

Constant Summary

Constants included from Helper

Helper::BYTE_UNITS

Instance Attribute Summary

Attributes inherited from WatchmonkeyCli::Checker

#app

Instance Method Summary collapse

Methods inherited from WatchmonkeyCli::Checker

#_tolog, #blank_config, checker_name, checker_name=, #debug, descendants, #error, #info, inherited, #init, #initialize, #local, maxrt, maxrt=, #rsafe, #safe, #spawn_sub, #start, #stop

Methods included from Helper

#human_filesize, #human_number, #human_seconds

Constructor Details

This class inherits a constructor from WatchmonkeyCli::Checker

Instance Method Details

#_parse_response(res) ⇒ Object



25
26
27
28
29
30
31
32
33
34
# File 'lib/watchmonkey_cli/checkers/unix_memory.rb', line 25

def _parse_response res
  return false if res.downcase["no such file"]
  {}.tap do |r|
    res.strip.split("\n").each do |line|
      chunks = line.split(":").map(&:strip)
      r[chunks[0]] = chunks[1].to_i
    end
    r["free"] = ((r["MemFree"].to_i + r["Buffers"].to_i + r["Cached"].to_i) / r["MemTotal"].to_f * 100).round(2)
  end
end

#check!(result, host, opts = {}) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/watchmonkey_cli/checkers/unix_memory.rb', line 13

def check! result, host, opts = {}
  result.command = "cat /proc/meminfo"
  result.result = host.exec(result.command)
  result.data = _parse_response(result.result)

  if !result.data
    result.error! result.result
  else
    result.error! "memory is low (limit is min. #{opts[:min_percent]}%, got #{result.data["free"]}%)" if opts[:min_percent] && result.data["free"] < opts[:min_percent]
  end
end

#enqueue(host, opts = {}) ⇒ Object



6
7
8
9
10
11
# File 'lib/watchmonkey_cli/checkers/unix_memory.rb', line 6

def enqueue host, opts = {}
  opts = { min_percent: 25 }.merge(opts)
  host = app.fetch_connection(:loopback, :local) if !host || host == :local
  host = app.fetch_connection(:ssh, host) if host.is_a?(Symbol)
  app.enqueue(self, host, opts)
end