-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathComputer.rb
85 lines (75 loc) · 2.34 KB
/
Computer.rb
1
2
3
4
5
6
7
8
9
10
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# Copyright (C) 2014 Wesleyan University
#
# This file is part of cmdr-devices.
#
# cmdr-devices is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# cmdr-devices is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with cmdr-devices. If not, see <http://www.gnu.org/licenses/>.
#---
#{
# "name": "Computer",
# "depends_on": "Device",
# "description": "A generic computer class, providing reachability monitoring",
# "author": "Micah Wylde",
# "email": "[email protected]",
# "type": "Computer"
#}
#---
require 'wol'
require 'timeout'
require "socket"
module Ping
def pingecho(host, timeout=5, service="echo")
begin
timeout(timeout) do
s = TCPSocket.new(host, service)
s.close
end
rescue Errno::ECONNREFUSED
return true
rescue Timeout::Error, StandardError
return false
end
return true
end
module_function :pingecho
end
class Computer < Cmdr::Device
configure do
ip_address :type => :string
mac_address :type => :string
end
#The following turns the computer on from sleeping (all devices) or off (not Macs)
#For Dells: WOL needs to be set to on and deep sleep needs to be turned off
#For Macs: change energy saving, dark wake, display, allow WOL, and possibly more
#Troubleshooting: An "intrusion alert" will keep a Dell from booting when off
#Macs will not turn on the display if only the display is sleeping: the computer
#itself must be asleep for it to wake
command :start, :action => proc{
w = Wol::WakeOnLan.new({:mac => @mac_address})
w.wake
}
#current info
state_var :reachable, :type => :boolean, :editable => false, :display_order => 1
def initialize(name, options)
@ip_address = options[:ip_address]
super(name, options)
EM.defer {
Thread.abort_on_exception = true
while true
self.reachable = Ping.pingecho(configuration[:ip_address])
sleep 5
end
}
end
end
#126.184