]> git.openstreetmap.org Git - chef.git/blob - cookbooks/hardware/recipes/default.rb
43c682ca0a51d7bb413bb003f00a638b55570ea3
[chef.git] / cookbooks / hardware / recipes / default.rb
1 #
2 # Cookbook Name:: hardware
3 # Recipe:: default
4 #
5 # Copyright 2012, OpenStreetMap Foundation
6 #
7 # Licensed under the Apache License, Version 2.0 (the "License");
8 # you may not use this file except in compliance with the License.
9 # You may obtain a copy of the License at
10 #
11 #     http://www.apache.org/licenses/LICENSE-2.0
12 #
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS,
15 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 # See the License for the specific language governing permissions and
17 # limitations under the License.
18 #
19
20 include_recipe "tools"
21 include_recipe "munin"
22
23 ohai_plugin "hardware" do
24   template "ohai.rb.erb"
25 end
26
27 case node[:cpu][:"0"][:vendor_id]
28 when "GenuineIntel"
29   package "intel-microcode"
30 end
31
32 case node[:cpu][:"0"][:vendor_id]
33 when "AuthenticAMD"
34   if node[:lsb][:release].to_f >= 14.04
35     package "amd64-microcode"
36   end
37 end
38
39 if node[:dmi] && node[:dmi][:system]
40   case node[:dmi][:system][:manufacturer]
41   when "empty"
42     manufacturer = node[:dmi][:base_board][:manufacturer]
43     product = node[:dmi][:base_board][:product_name]
44   else
45     manufacturer = node[:dmi][:system][:manufacturer]
46     product = node[:dmi][:system][:product_name]
47   end
48 else
49   manufacturer = "Unknown"
50   product = "Unknown"
51 end
52
53 units = []
54
55 if node[:roles].include?("bytemark") || node[:roles].include?("exonetric")
56   units << "0"
57 end
58
59 case manufacturer
60 when "HP"
61   package "hponcfg"
62   package "hp-health"
63   units << "1"
64 when "TYAN"
65   units << "0"
66 when "TYAN Computer Corporation"
67   units << "0"
68 when "Supermicro"
69   case product
70   when "H8DGU", "X9SCD", "X7DBU", "X7DW3", "X9DR7/E-(J)LN4F", "X9DR3-F", "X9DRW"
71     units << "1"
72   else
73     units << "0"
74   end
75 when "IBM"
76   units << "0"
77 end
78
79 units.sort.uniq.each do |unit|
80   file "/etc/init/ttySttyS#{unit}.conf" do
81     action :delete
82   end
83
84   template "/etc/init/ttyS#{unit}.conf" do
85     source "tty.conf.erb"
86     owner "root"
87     group "root"
88     mode 0644
89     variables :unit => unit
90   end
91
92   service "ttyS#{unit}" do
93     provider Chef::Provider::Service::Upstart
94     action [:enable, :start]
95     supports :status => true, :restart => true, :reload => false
96     subscribes :restart, "template[/etc/init/ttyS#{unit}.conf]"
97   end
98 end
99
100 # if we need a different / special kernel version to make the hardware
101 # work (e.g: https://github.com/openstreetmap/operations/issues/45) then
102 # ensure that we have the package installed. the grub template will
103 # make sure that this is the default on boot.
104 if node[:hardware][:grub][:kernel]
105   kernel_version = node[:hardware][:grub][:kernel]
106
107   package "linux-image-#{kernel_version}-generic"
108   package "linux-image-extra-#{kernel_version}-generic"
109   package "linux-headers-#{kernel_version}-generic"
110
111   boot_device = IO.popen(["df", "/boot"]).readlines.last.split.first
112   boot_uuid = IO.popen(["blkid", "-o", "value", "-s", "UUID", boot_device]).readlines.first.chomp
113   grub_entry = "gnulinux-advanced-#{boot_uuid}>gnulinux-#{kernel_version}-advanced-#{boot_uuid}"
114 else
115   grub_entry = "0"
116 end
117
118 if File.exist?("/etc/default/grub")
119   execute "update-grub" do
120     action :nothing
121     command "/usr/sbin/update-grub"
122   end
123
124   template "/etc/default/grub" do
125     source "grub.erb"
126     owner "root"
127     group "root"
128     mode 0644
129     variables :units => units, :entry => grub_entry
130     notifies :run, "execute[update-grub]"
131   end
132 end
133
134 execute "update-initramfs" do
135   action :nothing
136   command "update-initramfs -u -k all"
137   user "root"
138   group "root"
139 end
140
141 template "/etc/initramfs-tools/conf.d/mdadm" do
142   source "initramfs-mdadm.erb"
143   owner "root"
144   group "root"
145   mode 0644
146   notifies :run, "execute[update-initramfs]"
147 end
148
149 package "haveged"
150 service "haveged" do
151   action [:enable, :start]
152 end
153
154 if node[:kernel][:modules].include?("ipmi_si")
155   package "ipmitool"
156 end
157
158 if node[:lsb][:release].to_f >= 12.10
159   package "irqbalance"
160
161   template "/etc/default/irqbalance" do
162     source "irqbalance.erb"
163     owner "root"
164     group "root"
165     mode 0644
166   end
167
168   service "irqbalance" do
169     action [:start, :enable]
170     supports :status => false, :restart => true, :reload => false
171     subscribes :restart, "template[/etc/default/irqbalance]"
172   end
173 end
174
175 tools_packages = []
176 status_packages = {}
177
178 node[:kernel][:modules].each_key do |modname|
179   case modname
180   when "cciss"
181     tools_packages << "hpssacli"
182     status_packages["cciss-vol-status"] ||= []
183   when "hpsa"
184     tools_packages << "hpssacli"
185     status_packages["cciss-vol-status"] ||= []
186   when "mptsas"
187     tools_packages << "lsiutil"
188     # status_packages["mpt-status"] ||= []
189   when "mpt2sas", "mpt3sas"
190     tools_packages << "sas2ircu"
191     status_packages["sas2ircu-status"] ||= []
192   when "megaraid_mm"
193     tools_packages << "megactl"
194     status_packages["megaraid-status"] ||= []
195   when "megaraid_sas"
196     tools_packages << "megacli"
197     status_packages["megaclisas-status"] ||= []
198   when "aacraid"
199     tools_packages << "arcconf"
200     status_packages["aacraid-status"] ||= []
201   when "arcmsr"
202     tools_packages << "areca"
203   end
204 end
205
206 node[:block_device].each do |name, attributes|
207   next unless attributes[:vendor] == "HP" && attributes[:model] == "LOGICAL VOLUME"
208
209   if name =~ /^cciss!(c[0-9]+)d[0-9]+$/
210     status_packages["cciss-vol-status"] |= ["cciss/#{Regexp.last_match[1]}d0"]
211   else
212     Dir.glob("/sys/block/#{name}/device/scsi_generic/*").each do |sg|
213       status_packages["cciss-vol-status"] |= [File.basename(sg)]
214     end
215   end
216 end
217
218 %w(hpssacli lsiutil sas2ircu megactl megacli arcconf).each do |tools_package|
219   if tools_packages.include?(tools_package)
220     package tools_package
221   else
222     package tools_package do
223       action :purge
224     end
225   end
226 end
227
228 if tools_packages.include?("areca")
229   include_recipe "git"
230
231   git "/opt/areca" do
232     action :sync
233     repository "git://chef.openstreetmap.org/areca.git"
234     user "root"
235     group "root"
236   end
237 else
238   directory "/opt/areca" do
239     action :delete
240     recursive true
241   end
242 end
243
244 ["cciss-vol-status", "mpt-status", "sas2ircu-status", "megaraid-status", "megaclisas-status", "aacraid-status"].each do |status_package|
245   if status_packages.include?(status_package)
246     package status_package
247
248     template "/etc/default/#{status_package}d" do
249       source "raid.default.erb"
250       owner "root"
251       group "root"
252       mode 0644
253       variables :devices => status_packages[status_package]
254     end
255
256     service "#{status_package}d" do
257       action [:start, :enable]
258       supports :status => false, :restart => true, :reload => false
259       subscribes :restart, "template[/etc/default/#{status_package}d]"
260     end
261   else
262     package status_package do
263       action :purge
264     end
265
266     file "/etc/default/#{status_package}d" do
267       action :delete
268     end
269   end
270 end
271
272 disks = if node[:hardware][:disk]
273           node[:hardware][:disk][:disks]
274         else
275           []
276         end
277
278 disks = disks.map do |disk|
279   next if disk[:state] == "spun_down"
280
281   if disk[:smart_device]
282     controller = node[:hardware][:disk][:controllers][disk[:controller]]
283     device = controller[:device].sub("/dev/", "")
284     smart = disk[:smart_device]
285
286     if device.start_with?("cciss/") && smart =~ /^cciss,(\d+)$/
287       array = node[:hardware][:disk][:arrays][disk[:arrays].first]
288       munin = "cciss-3#{array[:wwn]}-#{Regexp.last_match(1)}"
289     elsif smart =~ /^.*,(\d+)$/
290       munin = "#{device}-#{Regexp.last_match(1)}"
291     elsif smart =~ %r{^.*,(\d+)/(\d+)$}
292       munin = "#{device}-#{Regexp.last_match(1)}:#{Regexp.last_match(2)}"
293     end
294   elsif disk[:device]
295     device = disk[:device].sub("/dev/", "")
296     munin = device
297   end
298
299   next if device.nil?
300
301   Hash[
302     :device => device,
303     :smart => smart,
304     :munin => munin,
305     :hddtemp => munin.tr("-:", "_")
306   ]
307 end
308
309 disks = disks.compact
310
311 if disks.count > 0
312   package "smartmontools"
313
314   template "/usr/local/bin/smartd-mailer" do
315     source "smartd-mailer.erb"
316     owner "root"
317     group "root"
318     mode 0755
319   end
320
321   template "/etc/smartd.conf" do
322     source "smartd.conf.erb"
323     owner "root"
324     group "root"
325     mode 0644
326     variables :disks => disks
327     notifies :reload, "service[smartmontools]"
328   end
329
330   template "/etc/default/smartmontools" do
331     source "smartmontools.erb"
332     owner "root"
333     group "root"
334     mode 0644
335     notifies :restart, "service[smartmontools]"
336   end
337
338   service "smartmontools" do
339     action [:enable, :start]
340     supports :status => true, :restart => true, :reload => true
341   end
342
343   # Don't try and do munin monitoring of disks behind
344   # an Areca controller as they only allow one thing to
345   # talk to the controller at a time and smartd will
346   # throw errors if it clashes with munin
347   disks = disks.reject { |disk| disk[:smart] && disk[:smart].start_with?("areca,") }
348
349   disks.each do |disk|
350     munin_plugin "smart_#{disk[:munin]}" do
351       target "smart_"
352       conf "munin.smart.erb"
353       conf_variables :disk => disk
354     end
355   end
356 else
357   service "smartmontools" do
358     action [:stop, :disable]
359   end
360 end
361
362 if disks.count > 0
363   munin_plugin "hddtemp_smartctl" do
364     conf "munin.hddtemp.erb"
365     conf_variables :disks => disks
366   end
367 else
368   munin_plugin "hddtemp_smartctl" do
369     action :delete
370     conf "munin.hddtemp.erb"
371   end
372 end
373
374 plugins = Dir.glob("/etc/munin/plugins/smart_*").map { |p| File.basename(p) } -
375           disks.map { |d| "smart_#{d[:munin]}" }
376
377 plugins.each do |plugin|
378   munin_plugin plugin do
379     action :delete
380   end
381 end
382
383 if File.exist?("/etc/mdadm/mdadm.conf")
384   mdadm_conf = edit_file "/etc/mdadm/mdadm.conf" do |line|
385     line.gsub!(/^MAILADDR .*$/, "MAILADDR admins@openstreetmap.org")
386
387     line
388   end
389
390   file "/etc/mdadm/mdadm.conf" do
391     owner "root"
392     group "root"
393     mode 0644
394     content mdadm_conf
395   end
396
397   service "mdadm" do
398     action :nothing
399     subscribes :restart, "file[/etc/mdadm/mdadm.conf]"
400   end
401 end
402
403 template "/etc/modules" do
404   source "modules.erb"
405   owner "root"
406   group "root"
407   mode 0644
408 end
409
410 if node[:lsb][:release].to_f <= 12.10
411   service "module-init-tools" do
412     provider Chef::Provider::Service::Upstart
413     action :nothing
414     subscribes :start, "template[/etc/modules]"
415   end
416 else
417   service "kmod" do
418     if node[:lsb][:release].to_f >= 15.10
419       provider Chef::Provider::Service::Systemd
420     else
421       provider Chef::Provider::Service::Upstart
422     end
423     action :nothing
424     subscribes :start, "template[/etc/modules]"
425   end
426 end
427
428 if node[:hardware][:watchdog]
429   package "watchdog"
430
431   template "/etc/default/watchdog" do
432     source "watchdog.erb"
433     owner "root"
434     group "root"
435     mode 0644
436     variables :module => node[:hardware][:watchdog]
437   end
438
439   service "watchdog" do
440     action [:enable, :start]
441   end
442 end
443
444 unless Dir.glob("/sys/class/hwmon/hwmon*").empty?
445   package "lm-sensors"
446
447   Dir.glob("/sys/devices/platform/coretemp.*").each do |coretemp|
448     cpu = File.basename(coretemp).sub("coretemp.", "").to_i
449     chip = format("coretemp-isa-%04d", cpu)
450
451     temps = if File.exist?("#{coretemp}/name")
452               Dir.glob("#{coretemp}/temp*_input").map do |temp|
453                 File.basename(temp).sub("temp", "").sub("_input", "").to_i
454               end.sort
455             else
456               Dir.glob("#{coretemp}/hwmon/hwmon*/temp*_input").map do |temp|
457                 File.basename(temp).sub("temp", "").sub("_input", "").to_i
458               end.sort
459             end
460
461     if temps.first == 1
462       node.default[:hardware][:sensors][chip][:temps][:temp1][:label] = "CPU #{cpu}"
463       temps.shift
464     end
465
466     temps.each_with_index do |temp, index|
467       node.default[:hardware][:sensors][chip][:temps]["temp#{temp}"][:label] = "CPU #{cpu} Core #{index}"
468     end
469   end
470
471   execute "/etc/sensors.d/chef.conf" do
472     action :nothing
473     command "/usr/bin/sensors -s"
474     user "root"
475     group "root"
476   end
477
478   template "/etc/sensors.d/chef.conf" do
479     source "sensors.conf.erb"
480     owner "root"
481     group "root"
482     mode 0644
483     notifies :run, "execute[/etc/sensors.d/chef.conf]"
484   end
485 end