Class: SlashPort::Component::LinuxHost

Inherits:
SlashPort::Component show all
Defined in:
app/models/components/linuxhost.rb

Instance Method Summary collapse

Methods inherited from SlashPort::Component

#_want, attribute, attributes, #attributes, class_initialize, components, config, #configs, configs, disable, enable, #get_attributes, get_attributes, #get_configs, get_configs, #get_things, get_things, inherited, is_enabled?, label, #path

Instance Method Details

#_reapchildObject



26
27
28
29
30
31
32
# File 'app/models/components/linuxhost.rb', line 26

def _reapchild
  begin
    Process.wait(-1, Process::WNOHANG)
  rescue Errno::ECHILD
    # ignore
  end
end

#DiskStatsObject

def MemStats



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'app/models/components/linuxhost.rb', line 81

def DiskStats
  data = Array.new
  IO.popen("df -PlB 1").readlines().each do |line|
    # skip header
    next if line =~ /Filesystem\s/
    # skip nonpath sources (tmpfs, udev, etc)
    next unless line =~ /^\//

    line.chomp!
    tuple = SlashPort::Tuple.new
    fields = %w[size used available percentused]
    values = line.split
    source = values.shift
    mount = values.pop

    tuple.labels["source"] = source
    tuple.labels["mount"] = mount
    fields.zip(values).each do |field,value|
      if field == "percentused"
        value = value.to_i / 100.0
      end

      tuple.data[field] = value
    end
    data << tuple
  end
  _reapchild
  return data
end

#IfStatsObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'app/models/components/linuxhost.rb', line 40

def IfStats
  data = Array.new
  File.open("/proc/net/dev").readlines().each do |line|
    line.chomp!
    next if line =~ /^Inter-|^ face/

    tuple = SlashPort::Tuple.new

    fields = %w[rx_bytes rx_packets rx_errors rx_drop rx_fifo rx_frame
                rx_compressed rx_multicast tx_bytes tx_packets tx_errors
                tx_drop tx_fifo tx_colls tx_carrier tx_compressed]

    interface, values = line.split(":")
    interface.gsub!(/\s+/, "")
    
    tuple.labels["interface"] = interface
    fields.zip(values.split).each do |field,value|
      tuple.data[field] = value
    end
    data << tuple
  end
  return data
end

#LoadAverageObject

def DiskStats



111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'app/models/components/linuxhost.rb', line 111

def LoadAverage
  data = Array.new
  tuple = SlashPort::Tuple.new
  loads = %x{uptime}.chomp.delete(",").split(/ +/)[-3..-1].map { |x| x.to_f }
  _reapchild
  load1, load5, load15 = loads
  tuple.data["load_1min"] = load1
  tuple.data["load_5min"] = load5
  tuple.data["load_15min"] = load15

  data << tuple
  return data
end

#MemStatsObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'app/models/components/linuxhost.rb', line 64

def MemStats
  data = Array.new
  tuple = SlashPort::Tuple.new
  File.open("/proc/meminfo").readlines().each do |line|
    line.chomp!
    key, value, unit = line.split(/[: ]+/)
    value = value.to_i
    if unit == "kB"
      value *= 1024
    end

    tuple.data[key.downcase] = value
  end
  data << tuple
  return data
end

#UptimeObject



34
35
36
37
38
# File 'app/models/components/linuxhost.rb', line 34

def Uptime
  tuple = SlashPort::Tuple.new
  tuple.data["uptime"] = File.open("/proc/uptime").read().split(" ")[0]
  return tuple
end