Class: Pipark::Host

Inherits:
Object
  • Object
show all
Defined in:
lib/pipark/host.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(address) ⇒ Host

Returns a new instance of Host.



29
30
31
# File 'lib/pipark/host.rb', line 29

def initialize(address)
  @address = address
end

Instance Attribute Details

#addressObject (readonly)

Returns the value of attribute address.



27
28
29
# File 'lib/pipark/host.rb', line 27

def address
  @address
end

Instance Method Details

#boot_timeObject

Returns the host’s boot time.



75
76
77
78
# File 'lib/pipark/host.rb', line 75

def boot_time
  cache['boot_time'] ||=
    file_read('/proc/stat').match( /btime\s+(\d+)/ ) { |m| Time.at(m.captures[0].to_i) }
end

#cpu_temperatureObject

Returns the hosts’s CPU temperature.



64
65
66
# File 'lib/pipark/host.rb', line 64

def cpu_temperature
  file_read('/sys/class/thermal/thermal_zone0/temp').chomp.to_i
end

#flushObject

Flushes cached attributes.



39
40
41
# File 'lib/pipark/host.rb', line 39

def flush
  @cache = {}
end

#hatObject

Experimental: returns info for the installed HAT, or false if no HAT is found.

Only supported for localhost.



114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/pipark/host.rb', line 114

def hat
  if localhost?
    dir = '/sys/firmware/devicetree/base/hat'
    if File.exist? dir
      %w(product vendor).map do |k|
        file = "#{dir}/#{k}"
        [ k, File.readable?(file) ? File.read(file).chop : nil ]
      end.to_h
    else
      false
    end
  end
end

#hostnameObject

Returns the host’s hostname.



54
55
56
# File 'lib/pipark/host.rb', line 54

def hostname
  cache['hostname'] ||= file_read('/etc/hostname').chomp
end

#inspectObject

Returns a programmer-friendly representation of the host.



34
35
36
# File 'lib/pipark/host.rb', line 34

def inspect
  %Q(Pipark::Host "#{address}")
end

#ledsObject

Experimental: returns the status of the host’s LEDs.

Only supported for localhost.



100
101
102
103
104
105
106
107
108
109
# File 'lib/pipark/host.rb', line 100

def leds
  dir = '/sys/class/leds'
  if localhost?
    Dir[dir + '/led[0-9]'].map do |f|
      if f.match(dir + '/(led\d)')
        [ $1, get_led_status($1).first ]
      end
    end.to_h
  end
end

#localhost?Boolean

Returns true if the host is localhost.

Returns:

  • (Boolean)


44
45
46
# File 'lib/pipark/host.rb', line 44

def localhost?
  @localhost ||= %w(localhost 127.0.0.1 ::1).include? address
end

#modelObject

Returns the host’s Raspberry Pi model.



59
60
61
# File 'lib/pipark/host.rb', line 59

def model
  cache['model'] ||= file_read('/proc/device-tree/model').chop
end

#os_releaseObject

Returns information about the host’s operating system.



69
70
71
72
# File 'lib/pipark/host.rb', line 69

def os_release
  cache['os-release'] ||=
    file_read('/etc/os-release').lines.map { |l| l.chomp.split('=') }.map { |k,v| [k,v.chomp.gsub('"','')] }.to_h
end

#pingable?Boolean

Returns true if the host can be pinged.

Returns:

  • (Boolean)


49
50
51
# File 'lib/pipark/host.rb', line 49

def pingable?
  Pipark.pingable? address
end

#rubyObject

Returns version information for the Ruby installed on the host.

Nil if no Ruby is found.



88
89
90
91
92
93
94
95
# File 'lib/pipark/host.rb', line 88

def ruby
  cache['ruby'] ||=
    if localhost?
      RUBY_DESCRIPTION
    else
      (result = sh 'ruby -v').empty? ? nil : result.chomp
    end
end

#serial_numberObject

Returns the host’s serial number.



81
82
83
# File 'lib/pipark/host.rb', line 81

def serial_number
  cache['serial-number'] ||= file_read('/proc/device-tree/serial-number').chop
end

#stateObject

Returns the host’s state.



129
130
131
132
133
134
135
136
137
# File 'lib/pipark/host.rb', line 129

def state
  result = { 'update_time' => Time.now.gmtime }
  result.merge %w(
    address hostname model serial_number
    os_release boot_time
    ruby
    cpu_temperature
  ).map { |m| [m, send(m)] }.to_h
end