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