]> git.openstreetmap.org Git - chef.git/blob - cookbooks/hardware/recipes/default.rb
3fdbac12fd6e840c436ed760ab78896fa0e96aa7
[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 template "/etc/default/irqbalance" do
185   source "irqbalance.erb"
186   owner "root"
187   group "root"
188   mode 0o644
189 end
190
191 service "irqbalance" do
192   action [:start, :enable]
193   supports :status => false, :restart => true, :reload => false
194   subscribes :restart, "template[/etc/default/irqbalance]"
195 end
196
197 # Link Layer Discovery Protocol Daemon
198 package "lldpd"
199 service "lldpd" do
200   action [:start, :enable]
201   supports :status => true, :restart => true, :reload => true
202 end
203
204 tools_packages = []
205 status_packages = {}
206
207 if node[:virtualization][:role] != "guest" ||
208   node[:virtualization][:system] != "lxd"
209
210   node[:kernel][:modules].each_key do |modname|
211     case modname
212     when "cciss"
213       tools_packages << "ssacli"
214       status_packages["cciss-vol-status"] ||= []
215     when "hpsa"
216       tools_packages << "ssacli"
217       status_packages["cciss-vol-status"] ||= []
218     when "mptsas"
219       tools_packages << "lsiutil"
220       status_packages["mpt-status"] ||= []
221     when "mpt2sas", "mpt3sas"
222       tools_packages << "sas2ircu"
223       status_packages["sas2ircu-status"] ||= []
224     when "megaraid_mm"
225       tools_packages << "megactl"
226       status_packages["megaraid-status"] ||= []
227     when "megaraid_sas"
228       tools_packages << "megacli"
229       status_packages["megaclisas-status"] ||= []
230     when "aacraid"
231       tools_packages << "arcconf"
232       status_packages["aacraid-status"] ||= []
233     when "arcmsr"
234       tools_packages << "areca"
235     end
236   end
237
238   node[:block_device].each do |name, attributes|
239     next unless attributes[:vendor] == "HP" && attributes[:model] == "LOGICAL VOLUME"
240
241     if name =~ /^cciss!(c[0-9]+)d[0-9]+$/
242       status_packages["cciss-vol-status"] |= ["cciss/#{Regexp.last_match[1]}d0"]
243     else
244       Dir.glob("/sys/block/#{name}/device/scsi_generic/*").each do |sg|
245         status_packages["cciss-vol-status"] |= [File.basename(sg)]
246       end
247     end
248   end
249 end
250
251 %w[ssacli lsiutil sas2ircu megactl megacli arcconf].each do |tools_package|
252   if tools_packages.include?(tools_package)
253     package tools_package
254   else
255     package tools_package do
256       action :purge
257     end
258   end
259 end
260
261 if tools_packages.include?("areca")
262   include_recipe "git"
263
264   git "/opt/areca" do
265     action :sync
266     repository "https://git.openstreetmap.org/private/areca.git"
267     depth 1
268     user "root"
269     group "root"
270     not_if { ENV["TEST_KITCHEN"] }
271   end
272 else
273   directory "/opt/areca" do
274     action :delete
275     recursive true
276   end
277 end
278
279 if status_packages.include?("cciss-vol-status")
280   template "/usr/local/bin/cciss-vol-statusd" do
281     source "cciss-vol-statusd.erb"
282     owner "root"
283     group "root"
284     mode 0o755
285     notifies :restart, "service[cciss-vol-statusd]"
286   end
287
288   systemd_service "cciss-vol-statusd" do
289     description "Check cciss_vol_status values in the background"
290     exec_start "/usr/local/bin/cciss-vol-statusd"
291     private_tmp true
292     protect_system "full"
293     protect_home true
294     no_new_privileges true
295     notifies :restart, "service[cciss-vol-statusd]"
296   end
297 else
298   systemd_service "cciss-vol-statusd" do
299     action :delete
300   end
301
302   template "/usr/local/bin/cciss-vol-statusd" do
303     action :delete
304   end
305 end
306
307 %w[cciss-vol-status mpt-status sas2ircu-status megaraid-status megaclisas-status aacraid-status].each do |status_package|
308   if status_packages.include?(status_package)
309     package status_package
310
311     template "/etc/default/#{status_package}d" do
312       source "raid.default.erb"
313       owner "root"
314       group "root"
315       mode 0o644
316       variables :devices => status_packages[status_package]
317     end
318
319     service "#{status_package}d" do
320       action [:start, :enable]
321       supports :status => false, :restart => true, :reload => false
322       subscribes :restart, "template[/etc/default/#{status_package}d]"
323     end
324   else
325     package status_package do
326       action :purge
327     end
328
329     file "/etc/default/#{status_package}d" do
330       action :delete
331     end
332   end
333 end
334
335 disks = if node[:hardware][:disk]
336           node[:hardware][:disk][:disks]
337         else
338           []
339         end
340
341 # intel_ssds = disks.select { |d| d[:vendor] == "INTEL" && d[:model] =~ /^SSD/ }
342 #
343 # nvmes = if node[:hardware][:pci]
344 #           node[:hardware][:pci].values.select { |pci| pci[:driver] == "nvme" }
345 #         else
346 #           []
347 #         end
348 #
349 # intel_nvmes = nvmes.select { |pci| pci[:vendor_name] == "Intel Corporation" }
350 #
351 # if !intel_ssds.empty? || !intel_nvmes.empty?
352 #   package "unzip"
353 #
354 #   intel_ssd_tool_version = "3.0.21"
355 #
356 #   remote_file "#{Chef::Config[:file_cache_path]}/Intel_SSD_Data_Center_Tool_#{intel_ssd_tool_version}_Linux.zip" do
357 #     source "https://downloadmirror.intel.com/29115/eng/Intel_SSD_Data_Center_Tool_#{intel_ssd_tool_version}_Linux.zip"
358 #   end
359 #
360 #   execute "#{Chef::Config[:file_cache_path]}/Intel_SSD_Data_Center_Tool_#{intel_ssd_tool_version}_Linux.zip" do
361 #     command "unzip Intel_SSD_Data_Center_Tool_#{intel_ssd_tool_version}_Linux.zip isdct_#{intel_ssd_tool_version}-1_amd64.deb"
362 #     cwd Chef::Config[:file_cache_path]
363 #     user "root"
364 #     group "root"
365 #     not_if { File.exist?("#{Chef::Config[:file_cache_path]}/isdct_#{intel_ssd_tool_version}-1_amd64.deb") }
366 #   end
367 #
368 #   dpkg_package "isdct" do
369 #     version "#{intel_ssd_tool_version}-1"
370 #     source "#{Chef::Config[:file_cache_path]}/isdct_#{intel_ssd_tool_version}-1_amd64.deb"
371 #   end
372 # end
373
374 disks = disks.map do |disk|
375   next if disk[:state] == "spun_down" || %w[unconfigured failed].any?(disk[:status])
376
377   if disk[:smart_device]
378     controller = node[:hardware][:disk][:controllers][disk[:controller]]
379
380     if controller && controller[:device]
381       device = controller[:device].sub("/dev/", "")
382       smart = disk[:smart_device]
383
384       if device.start_with?("cciss/") && smart =~ /^cciss,(\d+)$/
385         array = node[:hardware][:disk][:arrays][disk[:arrays].first]
386         munin = "cciss-3#{array[:wwn]}-#{Regexp.last_match(1)}"
387       elsif smart =~ /^.*,(\d+)$/
388         munin = "#{device}-#{Regexp.last_match(1)}"
389       elsif smart =~ %r{^.*,(\d+)/(\d+)$}
390         munin = "#{device}-#{Regexp.last_match(1)}:#{Regexp.last_match(2)}"
391       end
392     end
393   elsif disk[:device] =~ %r{^/dev/(nvme\d+)n\d+$}
394     device = Regexp.last_match(1)
395     munin = device
396   elsif disk[:device]
397     device = disk[:device].sub("/dev/", "")
398     munin = device
399   end
400
401   next if device.nil?
402
403   Hash[
404     :device => device,
405     :smart => smart,
406     :munin => munin,
407     :hddtemp => munin.tr("-:", "_")
408   ]
409 end
410
411 disks = disks.compact.uniq
412
413 if disks.count.positive?
414   package "smartmontools"
415
416   template "/usr/local/bin/smartd-mailer" do
417     source "smartd-mailer.erb"
418     owner "root"
419     group "root"
420     mode 0o755
421   end
422
423   template "/etc/smartd.conf" do
424     source "smartd.conf.erb"
425     owner "root"
426     group "root"
427     mode 0o644
428     variables :disks => disks
429   end
430
431   template "/etc/default/smartmontools" do
432     source "smartmontools.erb"
433     owner "root"
434     group "root"
435     mode 0o644
436   end
437
438   service "smartd" do
439     action [:enable, :start]
440     subscribes :reload, "template[/etc/smartd.conf]"
441     subscribes :restart, "template[/etc/default/smartmontools]"
442   end
443
444   # Don't try and do munin monitoring of disks behind
445   # an Areca controller as they only allow one thing to
446   # talk to the controller at a time and smartd will
447   # throw errors if it clashes with munin
448   disks = disks.reject { |disk| disk[:smart]&.start_with?("areca,") }
449
450   disks.each do |disk|
451     munin_plugin "smart_#{disk[:munin]}" do
452       target "smart_"
453       conf "munin.smart.erb"
454       conf_variables :disk => disk
455     end
456   end
457 else
458   service "smartd" do
459     action [:stop, :disable]
460   end
461 end
462
463 if disks.count.positive?
464   munin_plugin "hddtemp_smartctl" do
465     conf "munin.hddtemp.erb"
466     conf_variables :disks => disks
467   end
468 else
469   munin_plugin "hddtemp_smartctl" do
470     action :delete
471     conf "munin.hddtemp.erb"
472   end
473 end
474
475 plugins = Dir.glob("/etc/munin/plugins/smart_*").map { |p| File.basename(p) } -
476           disks.map { |d| "smart_#{d[:munin]}" }
477
478 plugins.each do |plugin|
479   munin_plugin plugin do
480     action :delete
481     conf "munin.smart.erb"
482   end
483 end
484
485 if File.exist?("/etc/mdadm/mdadm.conf")
486   mdadm_conf = edit_file "/etc/mdadm/mdadm.conf" do |line|
487     line.gsub!(/^MAILADDR .*$/, "MAILADDR admins@openstreetmap.org")
488
489     line
490   end
491
492   file "/etc/mdadm/mdadm.conf" do
493     owner "root"
494     group "root"
495     mode 0o644
496     content mdadm_conf
497   end
498
499   service "mdadm" do
500     action :nothing
501     subscribes :restart, "file[/etc/mdadm/mdadm.conf]"
502   end
503 end
504
505 template "/etc/modules" do
506   source "modules.erb"
507   owner "root"
508   group "root"
509   mode 0o644
510 end
511
512 service "kmod" do
513   action :nothing
514   subscribes :start, "template[/etc/modules]"
515 end
516
517 if node[:hardware][:watchdog]
518   package "watchdog"
519
520   template "/etc/default/watchdog" do
521     source "watchdog.erb"
522     owner "root"
523     group "root"
524     mode 0o644
525     variables :module => node[:hardware][:watchdog]
526   end
527
528   service "watchdog" do
529     action [:enable, :start]
530   end
531 end
532
533 unless Dir.glob("/sys/class/hwmon/hwmon*").empty?
534   package "lm-sensors"
535
536   Dir.glob("/sys/devices/platform/coretemp.*").each do |coretemp|
537     cpu = File.basename(coretemp).sub("coretemp.", "").to_i
538     chip = format("coretemp-isa-%04d", cpu)
539
540     temps = if File.exist?("#{coretemp}/name")
541               Dir.glob("#{coretemp}/temp*_input").map do |temp|
542                 File.basename(temp).sub("temp", "").sub("_input", "").to_i
543               end.sort
544             else
545               Dir.glob("#{coretemp}/hwmon/hwmon*/temp*_input").map do |temp|
546                 File.basename(temp).sub("temp", "").sub("_input", "").to_i
547               end.sort
548             end
549
550     if temps.first == 1
551       node.default[:hardware][:sensors][chip][:temps][:temp1][:label] = "CPU #{cpu}"
552       temps.shift
553     end
554
555     temps.each_with_index do |temp, index|
556       node.default[:hardware][:sensors][chip][:temps]["temp#{temp}"][:label] = "CPU #{cpu} Core #{index}"
557     end
558   end
559
560   execute "/etc/sensors.d/chef.conf" do
561     action :nothing
562     command "/usr/bin/sensors -s"
563     user "root"
564     group "root"
565   end
566
567   template "/etc/sensors.d/chef.conf" do
568     source "sensors.conf.erb"
569     owner "root"
570     group "root"
571     mode 0o644
572     notifies :run, "execute[/etc/sensors.d/chef.conf]"
573   end
574 end
575
576 if node[:hardware][:shm_size]
577   execute "remount-dev-shm" do
578     action :nothing
579     command "/bin/mount -o remount /dev/shm"
580     user "root"
581     group "root"
582   end
583
584   mount "/dev/shm" do
585     action :enable
586     device "tmpfs"
587     fstype "tmpfs"
588     options "rw,nosuid,nodev,size=#{node[:hardware][:shm_size]}"
589     notifies :run, "execute[remount-dev-shm]"
590   end
591 end