Module: SystemInspector::OSX

Defined in:
lib/instrumental_tools/system_inspector/osx.rb

Class Method Summary collapse

Class Method Details

.dfObject



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/instrumental_tools/system_inspector/osx.rb', line 79

def self.df
  output = {}
  `df -k`.lines.grep(%r{^/dev/}).each do |line|

    device, total, used, available, capacity, mount = line.chomp.split

    names = [File.basename(device)]
    names << 'root' if mount == '/'

    names.each do |name|
      output["disk.#{name}.total_mb"]          = total.to_f / 1024
      output["disk.#{name}.used_mb"]           = used.to_f / 1024
      output["disk.#{name}.available_mb"]      = available.to_f / 1024
      output["disk.#{name}.available_percent"] = available.to_f / total.to_f * 100
    end

  end
  output
end

.load_cpuObject



3
4
5
6
7
8
9
# File 'lib/instrumental_tools/system_inspector/osx.rb', line 3

def self.load_cpu
  output = { :gauges => {} }
  if SystemInspector.command_present?('top', 'CPU')
    output[:gauges].merge!(top)
  end
  output
end

.load_disksObject



71
72
73
74
75
76
77
# File 'lib/instrumental_tools/system_inspector/osx.rb', line 71

def self.load_disks
  output = { :gauges => {} }
  if SystemInspector.command_present?('df', 'disk')
    output[:gauges].merge!(df)
  end
  output
end

.load_filesystemObject



110
111
112
# File 'lib/instrumental_tools/system_inspector/osx.rb', line 110

def self.load_filesystem
  {}
end

.load_memoryObject



41
42
43
44
45
46
47
48
# File 'lib/instrumental_tools/system_inspector/osx.rb', line 41

def self.load_memory
  # TODO: swap
  output = { :gauges => {} }
  if SystemInspector.command_present?('vm_stat', 'memory')
    output[:gauges].merge!(vm_stat)
  end
  output
end

.netstat(interface = 'en1') ⇒ Object



99
100
101
102
103
104
105
106
107
108
# File 'lib/instrumental_tools/system_inspector/osx.rb', line 99

def self.netstat(interface = 'en1')
  # mostly functional network io stats
  headers, *lines = `netstat -ibI #{interface}`.lines.map { |l| l.split } # FIXME: vulnerability?
  headers         = headers.map { |h| h.downcase }
  lines.each do |line|
    if !line[3].include?(':')
      return Hash[headers.zip(line)]
    end
  end
end

.topObject



11
12
13
14
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
# File 'lib/instrumental_tools/system_inspector/osx.rb', line 11

def self.top
  lines     = []
  processes = date = load = cpu = nil

  IO.popen('top -l 1 -n 0') do |top|
    processes = top.gets.split(': ')[1]
    date      = top.gets
    load      = top.gets.split(': ')[1]
    cpu       = top.gets.split(': ')[1]
  end

  user, system, idle                       = cpu.split(", ").map { |v| v.to_f }
  load1, load5, load15                     = load.split(", ").map { |v| v.to_f }
  total, running, stuck, sleeping, threads = processes.split(", ").map { |v| v.to_i }

  {
    'cpu.user'           => user,
    'cpu.system'         => system,
    'cpu.idle'           => idle,
    'load.1min'          => load1,
    'load.5min'          => load5,
    'load.15min'         => load15,
    'processes.total'    => total,
    'processes.running'  => running,
    'processes.stuck'    => stuck,
    'processes.sleeping' => sleeping,
    'threads'            => threads,
  }
end

.vm_statObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/instrumental_tools/system_inspector/osx.rb', line 50

def self.vm_stat
  header, *rows = `vm_stat`.lines.map(&:chomp)
  page_size     = header.match(/page size of (\d+) bytes/)[1].to_i
  sections      = ["free", "active", "inactive", "wired", "speculative", "wired down"]
  output        = {}
  total         = 0.0

  rows.each do |row|
    if match = row.match(/Pages (.*):\s+(\d+)\./)
      section, value = match[1, 2]
      if sections.include?(section)
        value                                         = value.to_f * page_size / 1024 / 1024
        output["memory.#{section.gsub(' ', '_')}_mb"] = value
        total                                        += value
      end
    end
  end
  output["memory.free_percent"] = output["memory.free_mb"] / total * 100 # TODO: verify
  output
end