Created
December 11, 2008 18:52
-
-
Save lisa/34818 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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