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