]> git.openstreetmap.org Git - chef.git/blob - cookbooks/hardware/recipes/default.rb
4474578a5c74a0294994423221a287615b6a59ca
[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 when "AuthenticAMD"
31   package "amd64-microcode"
32 end
33
34 if node[:dmi] && node[:dmi][:system]
35   case node[:dmi][:system][:manufacturer]
36   when "empty"
37     manufacturer = node[:dmi][:base_board][:manufacturer]
38     product = node[:dmi][:base_board][:product_name]
39   else
40     manufacturer = node[:dmi][:system][:manufacturer]
41     product = node[:dmi][:system][:product_name]
42   end
43 else
44   manufacturer = "Unknown"
45   product = "Unknown"
46 end
47
48 units = []
49
50 if node[:roles].include?("bytemark") || node[:roles].include?("exonetric")
51   units << "0"
52 end
53
54 case manufacturer
55 when "HP"
56   package "hponcfg"
57
58   # Downgrade hp-health to 10.0.0.1.3-4. as 10.40-1815.49 has issues with reliable startup
59   package "hp-health" do
60     action :install
61     version "10.0.0.1.3-4."
62     options "--force-yes"
63     notifies :restart, "service[hp-health]"
64   end
65
66   service "hp-health" do
67     action [:enable, :start]
68     supports :status => true, :restart => true
69   end
70
71   units << "1"
72 when "TYAN"
73   units << "0"
74 when "TYAN Computer Corporation"
75   units << "0"
76 when "Supermicro"
77   case product
78   when "H8DGU", "X9SCD", "X7DBU", "X7DW3", "X9DR7/E-(J)LN4F", "X9DR3-F", "X9DRW", "SYS-2028U-TN24R4T+", "Super Server"
79     units << "1"
80   else
81     units << "0"
82   end
83 when "IBM"
84   units << "0"
85 end
86
87 # Remove legacy HP G4 support which breaks modern hp-health 10.4
88 if manufacturer == "HP"
89   %w[/opt/hp/hp-health/bin/hpasmd /usr/lib/libhpasmintrfc.so.3.0 %/usr/lib/libhpasmintrfc.so.3 /usr/lib/libhpasmintrfc.so].each do |filename|
90     file filename do
91       action :delete
92     end
93   end
94
95   directory "/opt/hp/hp-legacy" do
96     action :delete
97     recursive true
98   end
99 end
100
101 units.sort.uniq.each do |unit|
102   if node[:lsb][:release].to_f >= 16.04
103     service "serial-getty@ttyS#{unit}" do
104       action [:enable, :start]
105     end
106   else
107     file "/etc/init/ttySttyS#{unit}.conf" do
108       action :delete
109     end
110
111     template "/etc/init/ttyS#{unit}.conf" do
112       source "tty.conf.erb"
113       owner "root"
114       group "root"
115       mode 0o644
116       variables :unit => unit
117     end
118
119     service "ttyS#{unit}" do
120       provider Chef::Provider::Service::Upstart
121       action [:enable, :start]
122       supports :status => true, :restart => true, :reload => false
123       subscribes :restart, "template[/etc/init/ttyS#{unit}.conf]"
124     end
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 0o644
157     variables :units => units, :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 0o644
174   notifies :run, "execute[update-initramfs]"
175 end
176
177 package "haveged"
178 service "haveged" do
179   action [:enable, :start]
180 end
181
182 package "ipmitool" if node[:kernel][:modules].include?("ipmi_si")
183
184 package "irqbalance"
185
186 template "/etc/default/irqbalance" do
187   source "irqbalance.erb"
188   owner "root"
189   group "root"
190   mode 0o644
191 end
192
193 service "irqbalance" do
194   action [:start, :enable]
195   supports :status => false, :restart => true, :reload => false
196   subscribes :restart, "template[/etc/default/irqbalance]"
197 end
198
199 # Link Layer Discovery Protocol Daemon
200 package "lldpd"
201 service "lldpd" do
202   action [:start, :enable]
203   supports :status => true, :restart => true, :reload => true
204 end
205
206 tools_packages = []
207 status_packages = {}
208
209 node[:kernel][:modules].each_key do |modname|
210   case modname
211   when "cciss"
212     tools_packages << "hpssacli"
213     status_packages["cciss-vol-status"] ||= []
214   when "hpsa"
215     tools_packages << "hpssacli"
216     status_packages["cciss-vol-status"] ||= []
217   when "mptsas"
218     tools_packages << "lsiutil"
219     status_packages["mpt-status"] ||= []
220   when "mpt2sas", "mpt3sas"
221     tools_packages << "sas2ircu"
222     status_packages["sas2ircu-status"] ||= []
223   when "megaraid_mm"
224     tools_packages << "megactl"
225     status_packages["megaraid-status"] ||= []
226   when "megaraid_sas"
227     tools_packages << "megacli"
228     status_packages["megaclisas-status"] ||= []
229   when "aacraid"
230     tools_packages << "arcconf"
231     status_packages["aacraid-status"] ||= []
232   when "arcmsr"
233     tools_packages << "areca"
234   end
235 end
236
237 node[:block_device].each do |name, attributes|
238   next unless attributes[:vendor] == "HP" && attributes[:model] == "LOGICAL VOLUME"
239
240   if name =~ /^cciss!(c[0-9]+)d[0-9]+$/
241     status_packages["cciss-vol-status"] |= ["cciss/#{Regexp.last_match[1]}d0"]
242   else
243     Dir.glob("/sys/block/#{name}/device/scsi_generic/*").each do |sg|
244       status_packages["cciss-vol-status"] |= [File.basename(sg)]
245     end
246   end
247 end
248
249 %w[hpssacli lsiutil sas2ircu megactl megacli arcconf].each do |tools_package|
250   if tools_packages.include?(tools_package)
251     package tools_package
252   else
253     package tools_package do
254       action :purge
255     end
256   end
257 end
258
259 if tools_packages.include?("areca")
260   include_recipe "git"
261
262   git "/opt/areca" do
263     action :sync
264     repository "git://chef.openstreetmap.org/areca.git"
265     user "root"
266     group "root"
267   end
268 else
269   directory "/opt/areca" do
270     action :delete
271     recursive true
272   end
273 end
274
275 ["cciss-vol-status", "mpt-status", "sas2ircu-status", "megaraid-status", "megaclisas-status", "aacraid-status"].each do |status_package|
276   if status_packages.include?(status_package)
277     package status_package
278
279     template "/etc/default/#{status_package}d" do
280       source "raid.default.erb"
281       owner "root"
282       group "root"
283       mode 0o644
284       variables :devices => status_packages[status_package]
285     end
286
287     service "#{status_package}d" do
288       action [:start, :enable]
289       supports :status => false, :restart => true, :reload => false
290       subscribes :restart, "template[/etc/default/#{status_package}d]"
291     end
292   else
293     package status_package do
294       action :purge
295     end
296
297     file "/etc/default/#{status_package}d" do
298       action :delete
299     end
300   end
301 end
302
303 disks = if node[:hardware][:disk]
304           node[:hardware][:disk][:disks]
305         else
306           []
307         end
308
309 intel_ssds = disks.select { |d| d[:vendor] == "INTEL" && d[:model] =~ /^SSD/ }
310
311 nvmes = if node[:hardware][:pci]
312           node[:hardware][:pci].values.select { |pci| pci[:driver] == "nvme" }
313         else
314           []
315         end
316
317 intel_nvmes = nvmes.select { |pci| pci[:vendor_name] == "Intel Corporation" }
318
319 if !intel_ssds.empty? || !intel_nvmes.empty?
320   package "unzip"
321
322   remote_file "#{Chef::Config[:file_cache_path]}/Intel_SSD_Data_Center_Tool_3.0.7_Linux.zip" do
323     source "https://downloadmirror.intel.com/27144/eng/Intel_SSD_Data_Center_Tool_3.0.7_Linux.zip"
324   end
325
326   execute "#{Chef::Config[:file_cache_path]}/Intel_SSD_Data_Center_Tool_3.0.7_Linux.zip" do
327     command "unzip Intel_SSD_Data_Center_Tool_3.0.7_Linux.zip isdct_3.0.7.401-17_amd64.deb"
328     cwd Chef::Config[:file_cache_path]
329     user "root"
330     group "root"
331     not_if { File.exist?("#{Chef::Config[:file_cache_path]}/isdct_3.0.7.401-17_amd64.deb") }
332   end
333
334   dpkg_package "isdct" do
335     version "3.0.7.401-17"
336     source "#{Chef::Config[:file_cache_path]}/isdct_3.0.7.401-17_amd64.deb"
337   end
338 end
339
340 disks = disks.map do |disk|
341   next if disk[:state] == "spun_down"
342
343   if disk[:smart_device]
344     controller = node[:hardware][:disk][:controllers][disk[:controller]]
345     device = controller[:device].sub("/dev/", "")
346     smart = disk[:smart_device]
347
348     if device.start_with?("cciss/") && smart =~ /^cciss,(\d+)$/
349       array = node[:hardware][:disk][:arrays][disk[:arrays].first]
350       munin = "cciss-3#{array[:wwn]}-#{Regexp.last_match(1)}"
351     elsif smart =~ /^.*,(\d+)$/
352       munin = "#{device}-#{Regexp.last_match(1)}"
353     elsif smart =~ %r{^.*,(\d+)/(\d+)$}
354       munin = "#{device}-#{Regexp.last_match(1)}:#{Regexp.last_match(2)}"
355     end
356   elsif disk[:device]
357     device = disk[:device].sub("/dev/", "")
358     munin = device
359   end
360
361   next if device.nil?
362
363   Hash[
364     :device => device,
365     :smart => smart,
366     :munin => munin,
367     :hddtemp => munin.tr("-:", "_")
368   ]
369 end
370
371 smartd_service = if node[:lsb][:release].to_f >= 16.04
372                    "smartd"
373                  else
374                    "smartmontools"
375                  end
376
377 disks = disks.compact
378
379 if disks.count.positive?
380   package "smartmontools"
381
382   template "/usr/local/bin/smartd-mailer" do
383     source "smartd-mailer.erb"
384     owner "root"
385     group "root"
386     mode 0o755
387   end
388
389   template "/etc/smartd.conf" do
390     source "smartd.conf.erb"
391     owner "root"
392     group "root"
393     mode 0o644
394     variables :disks => disks
395   end
396
397   template "/etc/default/smartmontools" do
398     source "smartmontools.erb"
399     owner "root"
400     group "root"
401     mode 0o644
402   end
403
404   service smartd_service do
405     action [:enable, :start]
406     subscribes :reload, "template[/etc/smartd.conf]"
407     subscribes :restart, "template[/etc/default/smartmontools]"
408   end
409
410   # Don't try and do munin monitoring of disks behind
411   # an Areca controller as they only allow one thing to
412   # talk to the controller at a time and smartd will
413   # throw errors if it clashes with munin
414   disks = disks.reject { |disk| disk[:smart] && disk[:smart].start_with?("areca,") }
415
416   disks.each do |disk|
417     munin_plugin "smart_#{disk[:munin]}" do
418       target "smart_"
419       conf "munin.smart.erb"
420       conf_variables :disk => disk
421     end
422   end
423 else
424   service smartd_service do
425     action [:stop, :disable]
426   end
427 end
428
429 if disks.count.positive?
430   munin_plugin "hddtemp_smartctl" do
431     conf "munin.hddtemp.erb"
432     conf_variables :disks => disks
433   end
434 else
435   munin_plugin "hddtemp_smartctl" do
436     action :delete
437     conf "munin.hddtemp.erb"
438   end
439 end
440
441 plugins = Dir.glob("/etc/munin/plugins/smart_*").map { |p| File.basename(p) } -
442           disks.map { |d| "smart_#{d[:munin]}" }
443
444 plugins.each do |plugin|
445   munin_plugin plugin do
446     action :delete
447   end
448 end
449
450 if File.exist?("/etc/mdadm/mdadm.conf")
451   mdadm_conf = edit_file "/etc/mdadm/mdadm.conf" do |line|
452     line.gsub!(/^MAILADDR .*$/, "MAILADDR admins@openstreetmap.org")
453
454     line
455   end
456
457   file "/etc/mdadm/mdadm.conf" do
458     owner "root"
459     group "root"
460     mode 0o644
461     content mdadm_conf
462   end
463
464   service "mdadm" do
465     action :nothing
466     subscribes :restart, "file[/etc/mdadm/mdadm.conf]"
467   end
468 end
469
470 template "/etc/modules" do
471   source "modules.erb"
472   owner "root"
473   group "root"
474   mode 0o644
475 end
476
477 service "kmod" do
478   action :nothing
479   subscribes :start, "template[/etc/modules]"
480 end
481
482 if node[:hardware][:watchdog]
483   package "watchdog"
484
485   template "/etc/default/watchdog" do
486     source "watchdog.erb"
487     owner "root"
488     group "root"
489     mode 0o644
490     variables :module => node[:hardware][:watchdog]
491   end
492
493   service "watchdog" do
494     action [:enable, :start]
495   end
496 end
497
498 unless Dir.glob("/sys/class/hwmon/hwmon*").empty?
499   package "lm-sensors"
500
501   Dir.glob("/sys/devices/platform/coretemp.*").each do |coretemp|
502     cpu = File.basename(coretemp).sub("coretemp.", "").to_i
503     chip = format("coretemp-isa-%04d", cpu)
504
505     temps = if File.exist?("#{coretemp}/name")
506               Dir.glob("#{coretemp}/temp*_input").map do |temp|
507                 File.basename(temp).sub("temp", "").sub("_input", "").to_i
508               end.sort
509             else
510               Dir.glob("#{coretemp}/hwmon/hwmon*/temp*_input").map do |temp|
511                 File.basename(temp).sub("temp", "").sub("_input", "").to_i
512               end.sort
513             end
514
515     if temps.first == 1
516       node.default[:hardware][:sensors][chip][:temps][:temp1][:label] = "CPU #{cpu}"
517       temps.shift
518     end
519
520     temps.each_with_index do |temp, index|
521       node.default[:hardware][:sensors][chip][:temps]["temp#{temp}"][:label] = "CPU #{cpu} Core #{index}"
522     end
523   end
524
525   execute "/etc/sensors.d/chef.conf" do
526     action :nothing
527     command "/usr/bin/sensors -s"
528     user "root"
529     group "root"
530   end
531
532   template "/etc/sensors.d/chef.conf" do
533     source "sensors.conf.erb"
534     owner "root"
535     group "root"
536     mode 0o644
537     notifies :run, "execute[/etc/sensors.d/chef.conf]"
538   end
539 end