2 # Cookbook Name:: hardware
 
   5 # Copyright 2012, OpenStreetMap Foundation
 
   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
 
  11 #     https://www.apache.org/licenses/LICENSE-2.0
 
  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.
 
  20 include_recipe "tools"
 
  21 include_recipe "munin"
 
  23 ohai_plugin "hardware" do
 
  24   template "ohai.rb.erb"
 
  27 case node[:cpu][:"0"][:vendor_id]
 
  29   package "intel-microcode"
 
  31   package "amd64-microcode"
 
  34 if node[:dmi] && node[:dmi][:system]
 
  35   case node[:dmi][:system][:manufacturer]
 
  37     manufacturer = node[:dmi][:base_board][:manufacturer]
 
  38     product = node[:dmi][:base_board][:product_name]
 
  40     manufacturer = node[:dmi][:system][:manufacturer]
 
  41     product = node[:dmi][:system][:product_name]
 
  44   manufacturer = "Unknown"
 
  50 if node[:roles].include?("bytemark") || node[:roles].include?("exonetric")
 
  58   package "hp-health" do
 
  60     notifies :restart, "service[hp-health]"
 
  63   service "hp-health" do
 
  64     action [:enable, :start]
 
  65     supports :status => true, :restart => true
 
  68   if product.end_with?("Gen8", "Gen9")
 
  71       notifies :restart, "service[hp-ams]"
 
  75       action [:enable, :start]
 
  76       supports :status => true, :restart => true
 
  83 when "TYAN Computer Corporation"
 
  87   when "H8DGU", "X9SCD", "X7DBU", "X7DW3", "X9DR7/E-(J)LN4F", "X9DR3-F", "X9DRW", "SYS-1028U-TN10RT+", "SYS-2028U-TN24R4T+", "SYS-1029P-WTRT", "Super Server"
 
  96 units.sort.uniq.each do |unit|
 
  97   service "serial-getty@ttyS#{unit}" do
 
  98     action [:enable, :start]
 
 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]
 
 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"
 
 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}"
 
 121 if File.exist?("/etc/default/grub")
 
 122   execute "update-grub" do
 
 124     command "/usr/sbin/update-grub"
 
 127   template "/etc/default/grub" do
 
 132     variables :units => units, :entry => grub_entry
 
 133     notifies :run, "execute[update-grub]"
 
 137 execute "update-initramfs" do
 
 139   command "update-initramfs -u -k all"
 
 144 template "/etc/initramfs-tools/conf.d/mdadm" do
 
 145   source "initramfs-mdadm.erb"
 
 149   notifies :run, "execute[update-initramfs]"
 
 154   action [:enable, :start]
 
 157 package "ipmitool" if node[:kernel][:modules].include?("ipmi_si")
 
 161 template "/etc/default/irqbalance" do
 
 162   source "irqbalance.erb"
 
 168 service "irqbalance" do
 
 169   action [:start, :enable]
 
 170   supports :status => false, :restart => true, :reload => false
 
 171   subscribes :restart, "template[/etc/default/irqbalance]"
 
 174 # Link Layer Discovery Protocol Daemon
 
 177   action [:start, :enable]
 
 178   supports :status => true, :restart => true, :reload => true
 
 184 node[:kernel][:modules].each_key do |modname|
 
 187     tools_packages << "ssacli"
 
 188     status_packages["cciss-vol-status"] ||= []
 
 190     tools_packages << "ssacli"
 
 191     status_packages["cciss-vol-status"] ||= []
 
 193     tools_packages << "lsiutil"
 
 194     status_packages["mpt-status"] ||= []
 
 195   when "mpt2sas", "mpt3sas"
 
 196     tools_packages << "sas2ircu"
 
 197     status_packages["sas2ircu-status"] ||= []
 
 199     tools_packages << "megactl"
 
 200     status_packages["megaraid-status"] ||= []
 
 202     tools_packages << "megacli"
 
 203     status_packages["megaclisas-status"] ||= []
 
 205     tools_packages << "arcconf"
 
 206     status_packages["aacraid-status"] ||= []
 
 208     tools_packages << "areca"
 
 212 node[:block_device].each do |name, attributes|
 
 213   next unless attributes[:vendor] == "HP" && attributes[:model] == "LOGICAL VOLUME"
 
 215   if name =~ /^cciss!(c[0-9]+)d[0-9]+$/
 
 216     status_packages["cciss-vol-status"] |= ["cciss/#{Regexp.last_match[1]}d0"]
 
 218     Dir.glob("/sys/block/#{name}/device/scsi_generic/*").each do |sg|
 
 219       status_packages["cciss-vol-status"] |= [File.basename(sg)]
 
 224 %w[ssacli lsiutil sas2ircu megactl megacli arcconf].each do |tools_package|
 
 225   if tools_packages.include?(tools_package)
 
 226     package tools_package
 
 228     package tools_package do
 
 234 if tools_packages.include?("areca")
 
 239     repository "https://git.openstreetmap.org/private/areca.git"
 
 244   directory "/opt/areca" do
 
 250 if status_packages.include?("cciss-vol-status")
 
 251   template "/usr/local/bin/cciss-vol-statusd" do
 
 252     source "cciss-vol-statusd.erb"
 
 256     notifies :restart, "service[cciss-vol-statusd]"
 
 259   systemd_service "cciss-vol-statusd" do
 
 260     description "Check cciss_vol_status values in the background"
 
 261     exec_start "/usr/local/bin/cciss-vol-statusd"
 
 263     protect_system "full"
 
 265     no_new_privileges true
 
 266     notifies :restart, "service[cciss-vol-statusd]"
 
 270 ["cciss-vol-status", "mpt-status", "sas2ircu-status", "megaraid-status", "megaclisas-status", "aacraid-status"].each do |status_package|
 
 271   if status_packages.include?(status_package)
 
 272     package status_package
 
 274     template "/etc/default/#{status_package}d" do
 
 275       source "raid.default.erb"
 
 279       variables :devices => status_packages[status_package]
 
 282     service "#{status_package}d" do
 
 283       action [:start, :enable]
 
 284       supports :status => false, :restart => true, :reload => false
 
 285       subscribes :restart, "template[/etc/default/#{status_package}d]"
 
 288     package status_package do
 
 292     file "/etc/default/#{status_package}d" do
 
 298 disks = if node[:hardware][:disk]
 
 299           node[:hardware][:disk][:disks]
 
 304 intel_ssds = disks.select { |d| d[:vendor] == "INTEL" && d[:model] =~ /^SSD/ }
 
 306 nvmes = if node[:hardware][:pci]
 
 307           node[:hardware][:pci].values.select { |pci| pci[:driver] == "nvme" }
 
 312 intel_nvmes = nvmes.select { |pci| pci[:vendor_name] == "Intel Corporation" }
 
 314 if !intel_ssds.empty? || !intel_nvmes.empty?
 
 317   intel_ssd_tool_version = "3.0.19"
 
 319   remote_file "#{Chef::Config[:file_cache_path]}/Intel_SSD_Data_Center_Tool_#{intel_ssd_tool_version}_Linux.zip" do
 
 320     source "https://downloadmirror.intel.com/28639/eng/Intel_SSD_Data_Center_Tool_#{intel_ssd_tool_version}_Linux.zip"
 
 323   execute "#{Chef::Config[:file_cache_path]}/Intel_SSD_Data_Center_Tool_#{intel_ssd_tool_version}_Linux.zip" do
 
 324     command "unzip Intel_SSD_Data_Center_Tool_#{intel_ssd_tool_version}_Linux.zip isdct_#{intel_ssd_tool_version}-1_amd64.deb"
 
 325     cwd Chef::Config[:file_cache_path]
 
 328     not_if { File.exist?("#{Chef::Config[:file_cache_path]}/isdct_#{intel_ssd_tool_version}-1_amd64.deb") }
 
 331   dpkg_package "isdct" do
 
 332     version "#{intel_ssd_tool_version}-1"
 
 333     source "#{Chef::Config[:file_cache_path]}/isdct_#{intel_ssd_tool_version}-1_amd64.deb"
 
 337 disks = disks.map do |disk|
 
 338   next if disk[:state] == "spun_down" || %w[unconfigured failed].any?(disk[:status])
 
 340   if disk[:smart_device]
 
 341     controller = node[:hardware][:disk][:controllers][disk[:controller]]
 
 343     if controller && controller[:device]
 
 344       device = controller[:device].sub("/dev/", "")
 
 345       smart = disk[:smart_device]
 
 347       if device.start_with?("cciss/") && smart =~ /^cciss,(\d+)$/
 
 348         array = node[:hardware][:disk][:arrays][disk[:arrays].first]
 
 349         munin = "cciss-3#{array[:wwn]}-#{Regexp.last_match(1)}"
 
 350       elsif smart =~ /^.*,(\d+)$/
 
 351         munin = "#{device}-#{Regexp.last_match(1)}"
 
 352       elsif smart =~ %r{^.*,(\d+)/(\d+)$}
 
 353         munin = "#{device}-#{Regexp.last_match(1)}:#{Regexp.last_match(2)}"
 
 356   elsif disk[:device] =~ %r{^/dev/(nvme\d+)n\d+$}
 
 357     device = Regexp.last_match(1)
 
 360     device = disk[:device].sub("/dev/", "")
 
 370     :hddtemp => munin.tr("-:", "_")
 
 374 disks = disks.compact.uniq
 
 376 if disks.count.positive?
 
 377   package "smartmontools"
 
 379   template "/usr/local/bin/smartd-mailer" do
 
 380     source "smartd-mailer.erb"
 
 386   template "/etc/smartd.conf" do
 
 387     source "smartd.conf.erb"
 
 391     variables :disks => disks
 
 394   template "/etc/default/smartmontools" do
 
 395     source "smartmontools.erb"
 
 402     action [:enable, :start]
 
 403     subscribes :reload, "template[/etc/smartd.conf]"
 
 404     subscribes :restart, "template[/etc/default/smartmontools]"
 
 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]&.start_with?("areca,") }
 
 414     munin_plugin "smart_#{disk[:munin]}" do
 
 416       conf "munin.smart.erb"
 
 417       conf_variables :disk => disk
 
 422     action [:stop, :disable]
 
 426 if disks.count.positive?
 
 427   munin_plugin "hddtemp_smartctl" do
 
 428     conf "munin.hddtemp.erb"
 
 429     conf_variables :disks => disks
 
 432   munin_plugin "hddtemp_smartctl" do
 
 434     conf "munin.hddtemp.erb"
 
 438 plugins = Dir.glob("/etc/munin/plugins/smart_*").map { |p| File.basename(p) } -
 
 439           disks.map { |d| "smart_#{d[:munin]}" }
 
 441 plugins.each do |plugin|
 
 442   munin_plugin plugin do
 
 444     conf "munin.smart.erb"
 
 448 if File.exist?("/etc/mdadm/mdadm.conf")
 
 449   mdadm_conf = edit_file "/etc/mdadm/mdadm.conf" do |line|
 
 450     line.gsub!(/^MAILADDR .*$/, "MAILADDR admins@openstreetmap.org")
 
 455   file "/etc/mdadm/mdadm.conf" do
 
 464     subscribes :restart, "file[/etc/mdadm/mdadm.conf]"
 
 468 template "/etc/modules" do
 
 477   subscribes :start, "template[/etc/modules]"
 
 480 if node[:hardware][:watchdog]
 
 483   template "/etc/default/watchdog" do
 
 484     source "watchdog.erb"
 
 488     variables :module => node[:hardware][:watchdog]
 
 491   service "watchdog" do
 
 492     action [:enable, :start]
 
 496 unless Dir.glob("/sys/class/hwmon/hwmon*").empty?
 
 499   Dir.glob("/sys/devices/platform/coretemp.*").each do |coretemp|
 
 500     cpu = File.basename(coretemp).sub("coretemp.", "").to_i
 
 501     chip = format("coretemp-isa-%04d", cpu)
 
 503     temps = if File.exist?("#{coretemp}/name")
 
 504               Dir.glob("#{coretemp}/temp*_input").map do |temp|
 
 505                 File.basename(temp).sub("temp", "").sub("_input", "").to_i
 
 508               Dir.glob("#{coretemp}/hwmon/hwmon*/temp*_input").map do |temp|
 
 509                 File.basename(temp).sub("temp", "").sub("_input", "").to_i
 
 514       node.default[:hardware][:sensors][chip][:temps][:temp1][:label] = "CPU #{cpu}"
 
 518     temps.each_with_index do |temp, index|
 
 519       node.default[:hardware][:sensors][chip][:temps]["temp#{temp}"][:label] = "CPU #{cpu} Core #{index}"
 
 523   execute "/etc/sensors.d/chef.conf" do
 
 525     command "/usr/bin/sensors -s"
 
 530   template "/etc/sensors.d/chef.conf" do
 
 531     source "sensors.conf.erb"
 
 535     notifies :run, "execute[/etc/sensors.d/chef.conf]"
 
 539 if node[:hardware][:shm_size]
 
 541     action [:mount, :enable]
 
 544     options "rw,nosuid,nodev,size=#{node[:hardware][:shm_size]}"