Class: Graphiterb::Monitors::System

Inherits:
PeriodicMonitor show all
Defined in:
lib/graphiterb/monitors/system.rb

Instance Attribute Summary

Attributes inherited from PeriodicMonitor

#current_iter, #iter, #iter_interval, #last_time, #main_scope, #options, #started_at, #time_interval

Instance Method Summary collapse

Methods inherited from PeriodicMonitor

#enough_iterations?, #enough_time?, #initialize, #inst_rate, #periodically, #rate, #run!, #scope, #sender, #since

Methods included from Utils::SystemInfo

#graphite_identifier, #hostname, #node_name

Constructor Details

This class inherits a constructor from Graphiterb::Monitors::PeriodicMonitor

Instance Method Details

#cpu(lines) ⇒ Object



15
16
17
18
19
20
21
22
23
24
# File 'lib/graphiterb/monitors/system.rb', line 15

def cpu lines
  cpus             = 0.0
  total_percentage = 0.0
  lines.each do |line|
    next unless line =~ /^Cpu.* *: *([\d\.]+)%us/
    cpus += 1.0
    total_percentage += $1.to_f
  end
  total_percentage / cpus rescue 0.0
end

#dfObject



9
10
11
12
13
# File 'lib/graphiterb/monitors/system.rb', line 9

def df
  `/bin/df`.chomp.split("\n").
    grep(%r{^/dev/}).
    map{|line| line.split(/\s+/) } rescue []
end

#get_metrics(metrics, since) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/graphiterb/monitors/system.rb', line 56

def get_metrics metrics, since
  df.each do |handle, size, spaceused, spacefree, percentused, location|
    disk_name = handle.gsub(/^\//, '').split('/')
    percent_free = (100.0 * spacefree.to_f / (spaceused.to_f + spacefree.to_f)) rescue 0.0
    metrics << [scope(graphite_identifier, disk_name, 'available'), percent_free]
  end

  lines = top
  
  metrics << [scope(graphite_identifier, 'cpu', 'avg_usage'),   cpu(lines)]

  proc_total, proc_running = processes(lines)
  metrics << [scope(graphite_identifier, 'processes', 'total'),   proc_total   ]
  metrics << [scope(graphite_identifier, 'processes', 'running'), proc_running ]

  mem_used, mem_free = memory(lines)
  swap_used, swap_free = swap(lines)
  
  metrics << [scope(graphite_identifier, 'memory', 'used'), mem_used  ]
  metrics << [scope(graphite_identifier, 'memory', 'free'), mem_free  ]
  metrics << [scope(graphite_identifier, 'swap', 'used'),   swap_used ]
  metrics << [scope(graphite_identifier, 'swap', 'free'),   swap_free ]
end

#memory(lines) ⇒ Object



34
35
36
37
38
39
40
41
42
43
# File 'lib/graphiterb/monitors/system.rb', line 34

def memory lines
  lines.each do |line|
    next unless line =~ /^Mem: *(\d+)k *total, *(\d+)k *used, *(\d+)k *free/
    total = $1.to_f
    return [$2, $3].map do |bytes|
      100.0 * (bytes.to_f / total) rescue 0.0
    end
  end
  [0,0]
end

#processes(lines) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/graphiterb/monitors/system.rb', line 26

def processes lines
  lines.each do |line|
    next unless line =~ /^Tasks: *(\d+) *total, *(\d+) *running/
    return [$1, $2].map(&:to_i)
  end
  [0, 0]
end

#swap(lines) ⇒ Object



45
46
47
48
49
50
51
52
53
54
# File 'lib/graphiterb/monitors/system.rb', line 45

def swap lines
  lines.each do |line|
    next unless line =~ /^Swap: *(\d+)k *total, *(\d+)k *used, *(\d+)k *free/
    total = $1.to_f
    return [$2, $3].map do |bytes|
      100.0 * (bytes.to_f / total) rescue 0.0
    end
  end
  [0,0]
end

#topObject



5
6
7
# File 'lib/graphiterb/monitors/system.rb', line 5

def top
  `top -b -n3`.chomp.split(/^top -/).last.split("\n") rescue []
end