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