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"
 
  95   package "open-vm-tools"
 
  97   # Remove timeSync plugin completely
 
  98   # https://github.com/vmware/open-vm-tools/issues/302
 
  99   file "/usr/lib/open-vm-tools/plugins/vmsvc/libtimeSync.so" do
 
 101     notifies :restart, "service[open-vm-tools]"
 
 104   # Attempt to tell Host we are not interested in timeSync
 
 105   execute "vmware-toolbox-cmd-timesync-disable" do
 
 106     command "/usr/bin/vmware-toolbox-cmd timesync disable"
 
 110   service "open-vm-tools" do
 
 111     action [:enable, :start]
 
 112     supports :status => true, :restart => true
 
 116 units.sort.uniq.each do |unit|
 
 117   service "serial-getty@ttyS#{unit}" do
 
 118     action [:enable, :start]
 
 122 # if we need a different / special kernel version to make the hardware
 
 123 # work (e.g: https://github.com/openstreetmap/operations/issues/45) then
 
 124 # ensure that we have the package installed. the grub template will
 
 125 # make sure that this is the default on boot.
 
 126 if node[:hardware][:grub][:kernel]
 
 127   kernel_version = node[:hardware][:grub][:kernel]
 
 129   package "linux-image-#{kernel_version}-generic"
 
 130   package "linux-image-extra-#{kernel_version}-generic"
 
 131   package "linux-headers-#{kernel_version}-generic"
 
 132   package "linux-tools-#{kernel_version}-generic"
 
 134   boot_device = IO.popen(["df", "/boot"]).readlines.last.split.first
 
 135   boot_uuid = IO.popen(["blkid", "-o", "value", "-s", "UUID", boot_device]).readlines.first.chomp
 
 136   grub_entry = "gnulinux-advanced-#{boot_uuid}>gnulinux-#{kernel_version}-advanced-#{boot_uuid}"
 
 141 if File.exist?("/etc/default/grub")
 
 142   execute "update-grub" do
 
 144     command "/usr/sbin/update-grub"
 
 147   template "/etc/default/grub" do
 
 152     variables :units => units, :entry => grub_entry
 
 153     notifies :run, "execute[update-grub]"
 
 157 execute "update-initramfs" do
 
 159   command "update-initramfs -u -k all"
 
 164 template "/etc/initramfs-tools/conf.d/mdadm" do
 
 165   source "initramfs-mdadm.erb"
 
 169   notifies :run, "execute[update-initramfs]"
 
 174   action [:enable, :start]
 
 177 package "ipmitool" if node[:kernel][:modules].include?("ipmi_si")
 
 181 template "/etc/default/irqbalance" do
 
 182   source "irqbalance.erb"
 
 188 service "irqbalance" do
 
 189   action [:start, :enable]
 
 190   supports :status => false, :restart => true, :reload => false
 
 191   subscribes :restart, "template[/etc/default/irqbalance]"
 
 194 # Link Layer Discovery Protocol Daemon
 
 197   action [:start, :enable]
 
 198   supports :status => true, :restart => true, :reload => true
 
 204 node[:kernel][:modules].each_key do |modname|
 
 207     tools_packages << "ssacli"
 
 208     status_packages["cciss-vol-status"] ||= []
 
 210     tools_packages << "ssacli"
 
 211     status_packages["cciss-vol-status"] ||= []
 
 213     tools_packages << "lsiutil"
 
 214     status_packages["mpt-status"] ||= []
 
 215   when "mpt2sas", "mpt3sas"
 
 216     tools_packages << "sas2ircu"
 
 217     status_packages["sas2ircu-status"] ||= []
 
 219     tools_packages << "megactl"
 
 220     status_packages["megaraid-status"] ||= []
 
 222     tools_packages << "megacli"
 
 223     status_packages["megaclisas-status"] ||= []
 
 225     tools_packages << "arcconf"
 
 226     status_packages["aacraid-status"] ||= []
 
 228     tools_packages << "areca"
 
 232 node[:block_device].each do |name, attributes|
 
 233   next unless attributes[:vendor] == "HP" && attributes[:model] == "LOGICAL VOLUME"
 
 235   if name =~ /^cciss!(c[0-9]+)d[0-9]+$/
 
 236     status_packages["cciss-vol-status"] |= ["cciss/#{Regexp.last_match[1]}d0"]
 
 238     Dir.glob("/sys/block/#{name}/device/scsi_generic/*").each do |sg|
 
 239       status_packages["cciss-vol-status"] |= [File.basename(sg)]
 
 244 %w[ssacli lsiutil sas2ircu megactl megacli arcconf].each do |tools_package|
 
 245   if tools_packages.include?(tools_package)
 
 246     package tools_package
 
 248     package tools_package do
 
 254 if tools_packages.include?("areca")
 
 259     repository "https://git.openstreetmap.org/private/areca.git"
 
 264   directory "/opt/areca" do
 
 270 if status_packages.include?("cciss-vol-status")
 
 271   template "/usr/local/bin/cciss-vol-statusd" do
 
 272     source "cciss-vol-statusd.erb"
 
 276     notifies :restart, "service[cciss-vol-statusd]"
 
 279   systemd_service "cciss-vol-statusd" do
 
 280     description "Check cciss_vol_status values in the background"
 
 281     exec_start "/usr/local/bin/cciss-vol-statusd"
 
 283     protect_system "full"
 
 285     no_new_privileges true
 
 286     notifies :restart, "service[cciss-vol-statusd]"
 
 290 %w[cciss-vol-status mpt-status sas2ircu-status megaraid-status megaclisas-status aacraid-status].each do |status_package|
 
 291   if status_packages.include?(status_package)
 
 292     package status_package
 
 294     template "/etc/default/#{status_package}d" do
 
 295       source "raid.default.erb"
 
 299       variables :devices => status_packages[status_package]
 
 302     service "#{status_package}d" do
 
 303       action [:start, :enable]
 
 304       supports :status => false, :restart => true, :reload => false
 
 305       subscribes :restart, "template[/etc/default/#{status_package}d]"
 
 308     package status_package do
 
 312     file "/etc/default/#{status_package}d" do
 
 318 disks = if node[:hardware][:disk]
 
 319           node[:hardware][:disk][:disks]
 
 324 # intel_ssds = disks.select { |d| d[:vendor] == "INTEL" && d[:model] =~ /^SSD/ }
 
 326 # nvmes = if node[:hardware][:pci]
 
 327 #           node[:hardware][:pci].values.select { |pci| pci[:driver] == "nvme" }
 
 332 # intel_nvmes = nvmes.select { |pci| pci[:vendor_name] == "Intel Corporation" }
 
 334 # if !intel_ssds.empty? || !intel_nvmes.empty?
 
 337 #   intel_ssd_tool_version = "3.0.21"
 
 339 #   remote_file "#{Chef::Config[:file_cache_path]}/Intel_SSD_Data_Center_Tool_#{intel_ssd_tool_version}_Linux.zip" do
 
 340 #     source "https://downloadmirror.intel.com/29115/eng/Intel_SSD_Data_Center_Tool_#{intel_ssd_tool_version}_Linux.zip"
 
 343 #   execute "#{Chef::Config[:file_cache_path]}/Intel_SSD_Data_Center_Tool_#{intel_ssd_tool_version}_Linux.zip" do
 
 344 #     command "unzip Intel_SSD_Data_Center_Tool_#{intel_ssd_tool_version}_Linux.zip isdct_#{intel_ssd_tool_version}-1_amd64.deb"
 
 345 #     cwd Chef::Config[:file_cache_path]
 
 348 #     not_if { File.exist?("#{Chef::Config[:file_cache_path]}/isdct_#{intel_ssd_tool_version}-1_amd64.deb") }
 
 351 #   dpkg_package "isdct" do
 
 352 #     version "#{intel_ssd_tool_version}-1"
 
 353 #     source "#{Chef::Config[:file_cache_path]}/isdct_#{intel_ssd_tool_version}-1_amd64.deb"
 
 357 disks = disks.map do |disk|
 
 358   next if disk[:state] == "spun_down" || %w[unconfigured failed].any?(disk[:status])
 
 360   if disk[:smart_device]
 
 361     controller = node[:hardware][:disk][:controllers][disk[:controller]]
 
 363     if controller && controller[:device]
 
 364       device = controller[:device].sub("/dev/", "")
 
 365       smart = disk[:smart_device]
 
 367       if device.start_with?("cciss/") && smart =~ /^cciss,(\d+)$/
 
 368         array = node[:hardware][:disk][:arrays][disk[:arrays].first]
 
 369         munin = "cciss-3#{array[:wwn]}-#{Regexp.last_match(1)}"
 
 370       elsif smart =~ /^.*,(\d+)$/
 
 371         munin = "#{device}-#{Regexp.last_match(1)}"
 
 372       elsif smart =~ %r{^.*,(\d+)/(\d+)$}
 
 373         munin = "#{device}-#{Regexp.last_match(1)}:#{Regexp.last_match(2)}"
 
 376   elsif disk[:device] =~ %r{^/dev/(nvme\d+)n\d+$}
 
 377     device = Regexp.last_match(1)
 
 380     device = disk[:device].sub("/dev/", "")
 
 390     :hddtemp => munin.tr("-:", "_")
 
 394 disks = disks.compact.uniq
 
 396 if disks.count.positive?
 
 397   package "smartmontools"
 
 399   template "/usr/local/bin/smartd-mailer" do
 
 400     source "smartd-mailer.erb"
 
 406   template "/etc/smartd.conf" do
 
 407     source "smartd.conf.erb"
 
 411     variables :disks => disks
 
 414   template "/etc/default/smartmontools" do
 
 415     source "smartmontools.erb"
 
 422     action [:enable, :start]
 
 423     subscribes :reload, "template[/etc/smartd.conf]"
 
 424     subscribes :restart, "template[/etc/default/smartmontools]"
 
 427   # Don't try and do munin monitoring of disks behind
 
 428   # an Areca controller as they only allow one thing to
 
 429   # talk to the controller at a time and smartd will
 
 430   # throw errors if it clashes with munin
 
 431   disks = disks.reject { |disk| disk[:smart]&.start_with?("areca,") }
 
 434     munin_plugin "smart_#{disk[:munin]}" do
 
 436       conf "munin.smart.erb"
 
 437       conf_variables :disk => disk
 
 442     action [:stop, :disable]
 
 446 if disks.count.positive?
 
 447   munin_plugin "hddtemp_smartctl" do
 
 448     conf "munin.hddtemp.erb"
 
 449     conf_variables :disks => disks
 
 452   munin_plugin "hddtemp_smartctl" do
 
 454     conf "munin.hddtemp.erb"
 
 458 plugins = Dir.glob("/etc/munin/plugins/smart_*").map { |p| File.basename(p) } -
 
 459           disks.map { |d| "smart_#{d[:munin]}" }
 
 461 plugins.each do |plugin|
 
 462   munin_plugin plugin do
 
 464     conf "munin.smart.erb"
 
 468 if File.exist?("/etc/mdadm/mdadm.conf")
 
 469   mdadm_conf = edit_file "/etc/mdadm/mdadm.conf" do |line|
 
 470     line.gsub!(/^MAILADDR .*$/, "MAILADDR admins@openstreetmap.org")
 
 475   file "/etc/mdadm/mdadm.conf" do
 
 484     subscribes :restart, "file[/etc/mdadm/mdadm.conf]"
 
 488 template "/etc/modules" do
 
 497   subscribes :start, "template[/etc/modules]"
 
 500 if node[:hardware][:watchdog]
 
 503   template "/etc/default/watchdog" do
 
 504     source "watchdog.erb"
 
 508     variables :module => node[:hardware][:watchdog]
 
 511   service "watchdog" do
 
 512     action [:enable, :start]
 
 516 unless Dir.glob("/sys/class/hwmon/hwmon*").empty?
 
 519   Dir.glob("/sys/devices/platform/coretemp.*").each do |coretemp|
 
 520     cpu = File.basename(coretemp).sub("coretemp.", "").to_i
 
 521     chip = format("coretemp-isa-%04d", cpu)
 
 523     temps = if File.exist?("#{coretemp}/name")
 
 524               Dir.glob("#{coretemp}/temp*_input").map do |temp|
 
 525                 File.basename(temp).sub("temp", "").sub("_input", "").to_i
 
 528               Dir.glob("#{coretemp}/hwmon/hwmon*/temp*_input").map do |temp|
 
 529                 File.basename(temp).sub("temp", "").sub("_input", "").to_i
 
 534       node.default[:hardware][:sensors][chip][:temps][:temp1][:label] = "CPU #{cpu}"
 
 538     temps.each_with_index do |temp, index|
 
 539       node.default[:hardware][:sensors][chip][:temps]["temp#{temp}"][:label] = "CPU #{cpu} Core #{index}"
 
 543   execute "/etc/sensors.d/chef.conf" do
 
 545     command "/usr/bin/sensors -s"
 
 550   template "/etc/sensors.d/chef.conf" do
 
 551     source "sensors.conf.erb"
 
 555     notifies :run, "execute[/etc/sensors.d/chef.conf]"
 
 559 if node[:hardware][:shm_size]
 
 560   execute "remount-dev-shm" do
 
 562     command "/bin/mount -o remount /dev/shm"
 
 571     options "rw,nosuid,nodev,size=#{node[:hardware][:shm_size]}"
 
 572     notifies :run, "execute[remount-dev-shm]"