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