Class: Solanum::Source::Memory

Inherits:
Solanum::Source show all
Defined in:
lib/solanum/source/memory.rb

Instance Attribute Summary collapse

Attributes inherited from Solanum::Source

#attributes, #period, #type

Instance Method Summary collapse

Methods inherited from Solanum::Source

#next_run

Constructor Details

#initialize(opts) ⇒ Memory

Returns a new instance of Memory.



8
9
10
11
12
# File 'lib/solanum/source/memory.rb', line 8

def initialize(opts)
  super(opts)
  @thresholds = opts['thresholds'] || {}
  @swap_thresholds = opts['swap_thresholds'] || {}
end

Instance Attribute Details

#swap_thresholdsObject (readonly)

Returns the value of attribute swap_thresholds.



5
6
7
# File 'lib/solanum/source/memory.rb', line 5

def swap_thresholds
  @swap_thresholds
end

#thresholdsObject (readonly)

Returns the value of attribute thresholds.



5
6
7
# File 'lib/solanum/source/memory.rb', line 5

def thresholds
  @thresholds
end

Instance Method Details

#collect!Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/solanum/source/memory.rb', line 15

def collect!
  events = []

  meminfo = Hash.new(0)
  File.readlines('/proc/meminfo').each do |line|
    measure, quantity = *line.chomp.split(/: +/)
    value, unit = *quantity.split(' ')
    meminfo[measure] = value.to_i
  end

  mem_total = meminfo['MemTotal'].to_f
  record_usage = lambda do |type|
    if meminfo[type]
      usage_pct = meminfo[type]/mem_total.to_f
      events << {
        service: "memory #{type.downcase}",
        metric: usage_pct,
      }
    end
  end

  total_used = mem_total - meminfo['MemFree']
  mem_used = total_used - meminfo['Buffers'] - meminfo['Cached']
  usage = mem_used/mem_total
  events << {
    service: 'memory usage',
    metric: usage,
    state: state_over(@thresholds, usage),
  }

  events << {
    service: 'memory buffers',
    metric: meminfo['Buffers']/mem_total
  }

  cached = meminfo['Cached'] + meminfo['SReclaimable'] - meminfo['Shmem']
  events << {
    service: 'memory cached',
    metric: cached/mem_total
  }

  if meminfo['SwapTotal'] && meminfo['SwapTotal'] > 0
    swap_total = meminfo['SwapTotal']
    swap_free = meminfo['SwapFree']
    usage = 1.0 - swap_free/swap_total.to_f
    events << {
      service: 'swap usage',
      metric: usage,
      state: state_over(@swap_thresholds, usage),
    }
  end

  events
end