]> git.openstreetmap.org Git - chef.git/blob - cookbooks/hardware/recipes/default.rb
Remove munin
[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 "apt"
21 include_recipe "git"
22 include_recipe "munin"
23 include_recipe "prometheus"
24 include_recipe "sysfs"
25 include_recipe "tools"
26
27 ohai_plugin "hardware" do
28   template "ohai.rb.erb"
29 end
30
31 if platform?("debian")
32   package "firmware-linux"
33 end
34
35 if node[:cpu] && node[:cpu][:"0"] && node[:cpu][:"0"][:vendor_id]
36   case node[:cpu][:"0"][:vendor_id]
37   when "GenuineIntel"
38     package "intel-microcode"
39   when "AuthenticAMD"
40     package "amd64-microcode"
41   end
42 end
43
44 if node[:dmi] && node[:dmi][:system]
45   case node[:dmi][:system][:manufacturer]
46   when "empty"
47     manufacturer = node[:dmi][:base_board][:manufacturer]
48     product = node[:dmi][:base_board][:product_name]
49   else
50     manufacturer = node[:dmi][:system][:manufacturer]
51     product = node[:dmi][:system][:product_name]
52   end
53 else
54   manufacturer = "Unknown"
55   product = "Unknown"
56 end
57
58 units = []
59
60 if node[:roles].include?("bytemark") || node[:roles].include?("exonetric") || node[:roles].include?("prgmr")
61   units << "0"
62 end
63
64 case manufacturer
65 when "HP", "HPE"
66   include_recipe "apt::management-component-pack"
67
68   package "hponcfg"
69
70   execute "update-ilo" do
71     action :nothing
72     command "/usr/sbin/hponcfg -f /etc/ilo-defaults.xml"
73     not_if { kitchen? }
74   end
75
76   template "/etc/ilo-defaults.xml" do
77     source "ilo-defaults.xml.erb"
78     owner "root"
79     group "root"
80     mode "644"
81     notifies :run, "execute[update-ilo]"
82   end
83
84   package "hp-health" do
85     action :install
86     notifies :restart, "service[hp-health]"
87     only_if { platform?("ubuntu") && node[:lsb][:release].to_f < 22.04 }
88   end
89
90   service "hp-health" do
91     action [:enable, :start]
92     supports :status => true, :restart => true
93     only_if { platform?("ubuntu") && node[:lsb][:release].to_f < 22.04 }
94   end
95
96   if product.end_with?("Gen8", "Gen9")
97     package "hp-ams" do
98       action :install
99       notifies :restart, "service[hp-ams]"
100     end
101
102     service "hp-ams" do
103       action [:enable, :start]
104       supports :status => true, :restart => true
105     end
106   elsif product.end_with?("Gen10")
107     package "amsd" do
108       action :install
109       notifies :restart, "service[amsd]"
110     end
111
112     service "amsd" do
113       action [:enable, :start]
114       supports :status => true, :restart => true
115     end
116   end
117
118   units << if product.end_with?("Gen10")
119              "0"
120            else
121              "1"
122            end
123 when "TYAN"
124   units << "0"
125 when "TYAN Computer Corporation"
126   units << "0"
127 when "Supermicro"
128   units << "1"
129 when "IBM"
130   units << "0"
131 when "VMware, Inc."
132   package "open-vm-tools"
133
134   # Remove timeSync plugin completely
135   # https://github.com/vmware/open-vm-tools/issues/302
136   file "/usr/lib/open-vm-tools/plugins/vmsvc/libtimeSync.so" do
137     action :delete
138     notifies :restart, "service[open-vm-tools]"
139   end
140
141   # Attempt to tell Host we are not interested in timeSync
142   execute "vmware-toolbox-cmd-timesync-disable" do
143     command "/usr/bin/vmware-toolbox-cmd timesync disable"
144     ignore_failure true
145   end
146
147   service "open-vm-tools" do
148     action [:enable, :start]
149     supports :status => true, :restart => true
150   end
151 end
152
153 units.sort.uniq.each do |unit|
154   service "serial-getty@ttyS#{unit}" do
155     action [:enable, :start]
156     not_if { kitchen? }
157   end
158 end
159
160 # if we need a different / special kernel version to make the hardware
161 # work (e.g: https://github.com/openstreetmap/operations/issues/45) then
162 # ensure that we have the package installed. the grub template will
163 # make sure that this is the default on boot.
164 if node[:hardware][:grub][:kernel]
165   kernel_version = node[:hardware][:grub][:kernel]
166
167   package "linux-image-#{kernel_version}-generic"
168   package "linux-image-extra-#{kernel_version}-generic"
169   package "linux-headers-#{kernel_version}-generic"
170   package "linux-tools-#{kernel_version}-generic"
171
172   boot_device = IO.popen(["df", "/boot"]).readlines.last.split.first
173   boot_uuid = IO.popen(["blkid", "-o", "value", "-s", "UUID", boot_device]).readlines.first.chomp
174   grub_entry = "gnulinux-advanced-#{boot_uuid}>gnulinux-#{kernel_version}-advanced-#{boot_uuid}"
175 else
176   grub_entry = "0"
177 end
178
179 if File.exist?("/etc/default/grub")
180   execute "update-grub" do
181     action :nothing
182     command "/usr/sbin/update-grub"
183     not_if { kitchen? }
184   end
185
186   template "/etc/default/grub" do
187     source "grub.erb"
188     owner "root"
189     group "root"
190     mode "644"
191     variables :units => units, :entry => grub_entry
192     notifies :run, "execute[update-grub]"
193   end
194 end
195
196 package "initramfs-tools"
197
198 execute "update-initramfs" do
199   action :nothing
200   command "update-initramfs -u -k all"
201   user "root"
202   group "root"
203 end
204
205 template "/etc/initramfs-tools/conf.d/mdadm" do
206   source "initramfs-mdadm.erb"
207   owner "root"
208   group "root"
209   mode "644"
210   notifies :run, "execute[update-initramfs]"
211 end
212
213 # haveged is only required on older kernels
214 # /dev/random is not blocking anymore in 5.15+
215 if Chef::Util.compare_versions(node[:kernel][:release], [5, 15]).negative?
216   package "haveged"
217   service "haveged" do
218     action [:enable, :start]
219   end
220 else
221   service "haveged" do
222     action [:stop, :disable]
223   end
224   package "haveged" do
225     action :remove
226   end
227 end
228
229 watchdog_module = %w[hpwdt sp5100_tco].find do |module_name|
230   node[:hardware][:pci]&.any? { |_, pci| pci[:modules]&.any?(module_name) }
231 end
232
233 if node[:kernel][:modules].include?("ipmi_si")
234   package "ipmitool"
235   package "freeipmi-tools"
236
237   template "/etc/prometheus/ipmi_local.yml" do
238     source "ipmi_local.yml.erb"
239     owner "root"
240     group "root"
241     mode "644"
242   end
243
244   prometheus_exporter "ipmi" do
245     port 9290
246     user "root"
247     private_devices false
248     protect_clock false
249     system_call_filter ["@system-service", "@raw-io"]
250     options "--config.file=/etc/prometheus/ipmi_local.yml"
251     subscribes :restart, "template[/etc/prometheus/ipmi_local.yml]"
252   end
253
254   watchdog_module ||= "ipmi_watchdog"
255 end
256
257 package "irqbalance"
258
259 service "irqbalance" do
260   action [:start, :enable]
261   supports :status => false, :restart => true, :reload => false
262 end
263
264 package "lldpd"
265
266 service "lldpd" do
267   action [:start, :enable]
268   supports :status => true, :restart => true, :reload => true
269 end
270
271 ohai_plugin "lldp" do
272   template "lldp.rb.erb"
273 end
274
275 package %w[
276   rasdaemon
277   ruby-sqlite3
278 ]
279
280 service "rasdaemon" do
281   action [:enable, :start]
282 end
283
284 prometheus_exporter "rasdaemon" do
285   port 9797
286   user "root"
287 end
288
289 tools_packages = []
290 status_packages = {}
291
292 if node[:virtualization][:role] != "guest" ||
293    (node[:virtualization][:system] != "lxc" &&
294     node[:virtualization][:system] != "lxd" &&
295     node[:virtualization][:system] != "openvz")
296
297   node[:kernel][:modules].each_key do |modname|
298     case modname
299     when "cciss"
300       tools_packages << "ssacli"
301       status_packages["cciss-vol-status"] ||= []
302     when "hpsa"
303       tools_packages << "ssacli"
304       status_packages["cciss-vol-status"] ||= []
305     when "mptsas"
306       tools_packages << "lsiutil"
307       status_packages["mpt-status"] ||= []
308     when "mpt2sas", "mpt3sas"
309       tools_packages << "sas2ircu"
310       status_packages["sas2ircu-status"] ||= []
311     when "megaraid_sas"
312       tools_packages << "megacli"
313       status_packages["megaclisas-status"] ||= []
314     when "aacraid"
315       tools_packages << "arcconf"
316       status_packages["aacraid-status"] ||= []
317     when "arcmsr"
318       tools_packages << "areca"
319     end
320   end
321
322   node[:block_device].each do |name, attributes|
323     next unless attributes[:vendor] == "HP" && attributes[:model] == "LOGICAL VOLUME"
324
325     if name =~ /^cciss!(c[0-9]+)d[0-9]+$/
326       status_packages["cciss-vol-status"] |= ["cciss/#{Regexp.last_match[1]}d0"]
327     else
328       Dir.glob("/sys/block/#{name}/device/scsi_generic/*").each do |sg|
329         status_packages["cciss-vol-status"] |= [File.basename(sg)]
330       end
331     end
332   end
333 end
334
335 %w[ssacli lsiutil sas2ircu megactl megacli arcconf].each do |tools_package|
336   if tools_packages.include?(tools_package)
337     package tools_package
338   else
339     package tools_package do
340       action :purge
341     end
342   end
343 end
344
345 if tools_packages.include?("areca")
346   include_recipe "git"
347
348   git "/opt/areca" do
349     action :sync
350     repository "https://git.openstreetmap.org/private/areca.git"
351     depth 1
352     user "root"
353     group "root"
354     not_if { kitchen? }
355   end
356 else
357   directory "/opt/areca" do
358     action :delete
359     recursive true
360   end
361 end
362
363 include_recipe "apt::hwraid" unless status_packages.empty?
364
365 %w[cciss-vol-status mpt-status sas2ircu-status megaclisas-status aacraid-status].each do |status_package|
366   if status_packages.include?(status_package)
367     package status_package
368
369     service "#{status_package}d" do
370       action [:stop, :disable]
371     end
372
373     file "/etc/default/#{status_package}d" do
374       action :delete
375     end
376   else
377     package status_package do
378       action :purge
379     end
380   end
381 end
382
383 systemd_service "cciss-vol-statusd" do
384   action :delete
385 end
386
387 template "/usr/local/bin/cciss-vol-statusd" do
388   action :delete
389 end
390
391 disks = if node[:hardware][:disk]
392           node[:hardware][:disk][:disks]
393         else
394           []
395         end
396
397 intel_ssds = disks.select { |d| d[:vendor] == "INTEL" && d[:model] =~ /^SSD/ }
398
399 nvmes = if node[:hardware][:pci]
400           node[:hardware][:pci].values.select { |pci| pci[:driver] == "nvme" }
401         else
402           []
403         end
404
405 unless nvmes.empty?
406   package "nvme-cli"
407 end
408
409 intel_nvmes = nvmes.select { |pci| pci[:vendor_name] == "Intel Corporation" }
410
411 if !intel_ssds.empty? || !intel_nvmes.empty?
412   package "unzip"
413
414   sst_tool_version = "1.3"
415   sst_package_version = "#{sst_tool_version}.208-0"
416
417   # remote_file "#{Chef::Config[:file_cache_path]}/SST_CLI_Linux_#{sst_tool_version}.zip" do
418   #   source "https://downloadmirror.intel.com/743764/SST_CLI_Linux_#{sst_tool_version}.zip"
419   # end
420
421   execute "#{Chef::Config[:file_cache_path]}/SST_CLI_Linux_#{sst_tool_version}.zip" do
422     command "unzip SST_CLI_Linux_#{sst_tool_version}.zip sst_#{sst_package_version}_amd64.deb"
423     cwd Chef::Config[:file_cache_path]
424     user "root"
425     group "root"
426     not_if { ::File.exist?("#{Chef::Config[:file_cache_path]}/sst_#{sst_package_version}_amd64.deb") }
427   end
428
429   dpkg_package "sst" do
430     version "#{sst_package_version}"
431     source "#{Chef::Config[:file_cache_path]}/sst_#{sst_package_version}_amd64.deb"
432   end
433
434   dpkg_package "intelmas" do
435     action :purge
436   end
437 end
438
439 disks = disks.map do |disk|
440   next if disk[:state] == "spun_down" || %w[unconfigured failed].any?(disk[:status])
441
442   if disk[:smart_device]
443     controller = node[:hardware][:disk][:controllers][disk[:controller]]
444
445     if controller && controller[:device]
446       device = controller[:device].sub("/dev/", "")
447       smart = disk[:smart_device]
448     elsif disk[:device]
449       device = disk[:device].sub("/dev/", "")
450       smart = disk[:smart_device]
451     end
452   elsif disk[:device] =~ %r{^/dev/(nvme\d+)n\d+$}
453     device = Regexp.last_match(1)
454   elsif disk[:device]
455     device = disk[:device].sub("/dev/", "")
456   end
457
458   next if device.nil?
459
460   Hash[
461     :device => device,
462     :smart => smart
463   ]
464 end
465
466 disks = disks.compact.uniq
467
468 if disks.count.positive?
469   package "smartmontools"
470
471   template "/etc/cron.daily/update-smart-drivedb" do
472     source "update-smart-drivedb.erb"
473     owner "root"
474     group "root"
475     mode "755"
476   end
477
478   template "/usr/local/bin/smartd-mailer" do
479     source "smartd-mailer.erb"
480     owner "root"
481     group "root"
482     mode "755"
483   end
484
485   template "/etc/smartd.conf" do
486     source "smartd.conf.erb"
487     owner "root"
488     group "root"
489     mode "644"
490     variables :disks => disks
491   end
492
493   template "/etc/default/smartmontools" do
494     source "smartmontools.erb"
495     owner "root"
496     group "root"
497     mode "644"
498   end
499
500   service "smartmontools" do
501     action [:enable, :start]
502     subscribes :reload, "template[/etc/smartd.conf]"
503     subscribes :restart, "template[/etc/default/smartmontools]"
504   end
505
506   template "/etc/prometheus/collectors/smart.devices" do
507     source "smart.devices.erb"
508     owner "root"
509     group "root"
510     mode "644"
511     variables :disks => disks
512   end
513
514   prometheus_collector "smart" do
515     interval "15m"
516     user "root"
517     capability_bounding_set %w[CAP_DAC_OVERRIDE CAP_SYS_ADMIN CAP_SYS_RAWIO]
518     private_devices false
519     private_users false
520     protect_clock false
521   end
522 else
523   service "smartd" do
524     action [:stop, :disable]
525   end
526 end
527
528 if File.exist?("/etc/mdadm/mdadm.conf")
529   mdadm_conf = edit_file "/etc/mdadm/mdadm.conf" do |line|
530     line.gsub!(/^MAILADDR .*$/, "MAILADDR admins@openstreetmap.org")
531
532     line
533   end
534
535   file "/etc/mdadm/mdadm.conf" do
536     owner "root"
537     group "root"
538     mode "644"
539     content mdadm_conf
540   end
541
542   service "mdmonitor" do
543     action :nothing
544     subscribes :restart, "file[/etc/mdadm/mdadm.conf]"
545   end
546 end
547
548 file "/etc/modules" do
549   action :delete
550 end
551
552 node[:hardware][:modules].each do |module_name|
553   kernel_module module_name do
554     action :install
555     not_if { kitchen? }
556   end
557 end
558
559 node[:hardware][:blacklisted_modules].each do |module_name|
560   kernel_module module_name do
561     action :blacklist
562   end
563 end
564
565 if watchdog_module
566   kernel_module watchdog_module do
567     action :install
568   end
569
570   execute "systemctl-reload" do
571     action :nothing
572     command "systemctl daemon-reload"
573     user "root"
574     group "root"
575   end
576
577   directory "/etc/systemd/system.conf.d" do
578     owner "root"
579     group "root"
580     mode "755"
581   end
582
583   template "/etc/systemd/system.conf.d/watchdog.conf" do
584     source "watchdog.conf.erb"
585     owner "root"
586     group "root"
587     mode "644"
588     notifies :run, "execute[systemctl-reload]"
589   end
590 end
591
592 unless Dir.glob("/sys/class/hwmon/hwmon*").empty?
593   package "lm-sensors"
594
595   Dir.glob("/sys/devices/platform/coretemp.*").each do |coretemp|
596     cpu = File.basename(coretemp).sub("coretemp.", "").to_i
597     chip = format("coretemp-isa-%04d", cpu)
598
599     temps = if File.exist?("#{coretemp}/name")
600               Dir.glob("#{coretemp}/temp*_input").map do |temp|
601                 File.basename(temp).sub("temp", "").sub("_input", "").to_i
602               end.sort
603             else
604               Dir.glob("#{coretemp}/hwmon/hwmon*/temp*_input").map do |temp|
605                 File.basename(temp).sub("temp", "").sub("_input", "").to_i
606               end.sort
607             end
608
609     if temps.first == 1
610       node.default[:hardware][:sensors][chip][:temps][:temp1][:label] = "CPU #{cpu}"
611       temps.shift
612     end
613
614     temps.each_with_index do |temp, index|
615       node.default[:hardware][:sensors][chip][:temps]["temp#{temp}"][:label] = "CPU #{cpu} Core #{index}"
616     end
617   end
618
619   execute "/etc/sensors.d/chef.conf" do
620     action :nothing
621     command "/usr/bin/sensors -s"
622     user "root"
623     group "root"
624   end
625
626   template "/etc/sensors.d/chef.conf" do
627     source "sensors.conf.erb"
628     owner "root"
629     group "root"
630     mode "644"
631     notifies :run, "execute[/etc/sensors.d/chef.conf]"
632   end
633 end
634
635 if node[:hardware][:shm_size]
636   execute "remount-dev-shm" do
637     action :nothing
638     command "/bin/mount -o remount /dev/shm"
639     user "root"
640     group "root"
641   end
642
643   mount "/dev/shm" do
644     action :enable
645     device "tmpfs"
646     fstype "tmpfs"
647     options "rw,nosuid,nodev,size=#{node[:hardware][:shm_size]}"
648     notifies :run, "execute[remount-dev-shm]"
649   end
650 end
651
652 prometheus_collector "ohai" do
653   interval "15m"
654   user "root"
655   proc_subset "all"
656   capability_bounding_set %w[CAP_DAC_OVERRIDE CAP_SYS_ADMIN CAP_SYS_RAWIO]
657   private_devices false
658   private_users false
659   protect_clock false
660   protect_kernel_modules false
661 end