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