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