Skip to content

Instantly share code, notes, and snippets.

@lisa
Created December 11, 2008 18:52
Show Gist options
  • Save lisa/34818 to your computer and use it in GitHub Desktop.
Save lisa/34818 to your computer and use it in GitHub Desktop.
require 'socket'
class DaemonTemperature
include Socket::Constants
def initialize(hostname,port = 7634)
socket = Socket.new AF_INET,SOCK_STREAM,0
sockaddr = Socket.pack_sockaddr_in(port,hostname)
sock_results = ""
begin
socket.connect(sockaddr)
sock_results = socket.read
rescue Exception => e
puts "Got Exception #{e.message}"
ensure
socket.close rescue true
end
socket = nil
parse_disks!(sock_results)
end
def disks
@temps.keys
end
def temp(disk)
@temps[disk]
end
protected
def parse_disks!(sock_results = "")
groups = sock_results.gsub(/^\|/,'').gsub(/\|$/,'').split("||")
@temps = groups.inject({}) do |t,group|
splitted = group.split("|")
t[splitted[0].gsub(/\/dev\//,'')] = splitted[2].to_i
t
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment