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