-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtempMon2.rb
executable file
·132 lines (103 loc) · 3.1 KB
/
tempMon2.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# !/home/jkolber/.rvm/rubies/ruby-1.9.2-p318/bin/ruby
require "socket"
class TempMon
attr_accessor :statsHost
attr_accessor :graphitePort
attr_accessor :statsdPort
attr_accessor :graphite_key
attr_accessor :temp2_threshhold
attr_accessor :too_cool
def initialize()
self.statsHost = "192.168.1.200"
self.graphitePort = 2003
self.statsdPort = 8125
self.temp2_threshhold = 47
self.too_cool = 42.5
self.graphite_key = "macmini.sensors"
end
##
# Public
#
def toGraphite(key, val)
date = Time.now.to_i
value = val.to_i
puts "#{self.graphite_key}.#{key} #{val} #{date} | nc #{self.statsHost} #{self.graphitePort} "
%x{echo "#{self.graphite_key}.#{key} #{value} #{date}" | nc #{self.statsHost} #{self.graphitePort} }
end
##
# Public
#
def toStatsd(key, val)
s = UDPSocket.new
s.send("#{graphite_key}.#{key}:#{val}|g", 0, self.statsHost, self.statsdPort)
end
##
# readSensors
#
def readSensors()
wasCore = 0
sensor_in = `sensors -u`
out = {}
sensor_in.split(/\r?\n/).each do | line |
# some of these keys occurr more than once
# and we can skip the one that comes after Core 0:
if wasCore == 1
wasCore = 0
next
end
if line == "Core 0:"
wasCore = 1
next
end
probe = /(?<key>[a-zA-Z0-9_]+):\s(?<value>[0-9\.]+)/.match(line)
if probe
# puts probe[:key]
# puts "----"
# puts probe[:value]
out[probe[:key]] = probe[:value]
toGraphite(probe[:key], probe[:value])
toStatsd(probe[:key], probe[:value])
end
end
return out
end
end
class FanControl
def get_current()
`cat /sys/devices/platform/applesmc.768/fan1_output`
end
def fan_medium()
`echo 1 > /sys/devices/platform/applesmc.768/fan1_manual`
`echo 4200 > /sys/devices/platform/applesmc.768/fan1_output`
end
def fan_low()
`echo 1 > /sys/devices/platform/applesmc.768/fan1_manual`
`echo 3200 > /sys/devices/platform/applesmc.768/fan1_output`
end
def fan_high()
`echo 1 > /sys/devices/platform/applesmc.768/fan1_manual`
`echo 5100 > /sys/devices/platform/applesmc.768/fan1_output`
end
end
if __FILE__ == $0
t = TempMon.new
fc = FanControl.new
# puts t.statsHost
temps = t.readSensors()
puts "Temp is #{temps['temp2_input']}."
if temps['temp2_input'].to_f > t.temp2_threshhold
# puts "TOO HOT"
fc.fan_high()
t.toGraphite("fan.high", 1)
elsif temps['temp2_input'].to_f < t.too_cool
# puts "TOO COOL"
fc.fan_low()
t.toGraphite("fan.low", 1);
else
# puts "JUST RIGHT"
fc.fan_medium()
t.toGraphite("fan.medium", 1)
end
current_speed = fc.get_current().strip!
t.toGraphite("fan.speed", current_speed)
end