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