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.
 
  22 include_recipe "munin"
 
  23 include_recipe "sysfs"
 
  24 include_recipe "tools"
 
  26 ohai_plugin "hardware" do
 
  27   template "ohai.rb.erb"
 
  30 case node[:cpu][:"0"][:vendor_id]
 
  32   package "intel-microcode"
 
  34   package "amd64-microcode"
 
  37 if node[:dmi] && node[:dmi][:system]
 
  38   case node[:dmi][:system][:manufacturer]
 
  40     manufacturer = node[:dmi][:base_board][:manufacturer]
 
  41     product = node[:dmi][:base_board][:product_name]
 
  43     manufacturer = node[:dmi][:system][:manufacturer]
 
  44     product = node[:dmi][:system][:product_name]
 
  47   manufacturer = "Unknown"
 
  53 if node[:roles].include?("bytemark") || node[:roles].include?("exonetric")
 
  61   package "hp-health" do
 
  63     notifies :restart, "service[hp-health]"
 
  66   service "hp-health" do
 
  67     action [:enable, :start]
 
  68     supports :status => true, :restart => true
 
  71   if product.end_with?("Gen8", "Gen9")
 
  74       notifies :restart, "service[hp-ams]"
 
  78       action [:enable, :start]
 
  79       supports :status => true, :restart => true
 
  86 when "TYAN Computer Corporation"
 
  90   when "H8DGU", "X9SCD", "X7DBU", "X7DW3", "X9DR7/E-(J)LN4F", "X9DR3-F", "X9DRW", "SYS-1028U-TN10RT+", "SYS-2028U-TN24R4T+", "SYS-1029P-WTRT", "Super Server"
 
  98   package "open-vm-tools"
 
 100   # Remove timeSync plugin completely
 
 101   # https://github.com/vmware/open-vm-tools/issues/302
 
 102   file "/usr/lib/open-vm-tools/plugins/vmsvc/libtimeSync.so" do
 
 104     notifies :restart, "service[open-vm-tools]"
 
 107   # Attempt to tell Host we are not interested in timeSync
 
 108   execute "vmware-toolbox-cmd-timesync-disable" do
 
 109     command "/usr/bin/vmware-toolbox-cmd timesync disable"
 
 113   service "open-vm-tools" do
 
 114     action [:enable, :start]
 
 115     supports :status => true, :restart => true
 
 119 units.sort.uniq.each do |unit|
 
 120   service "serial-getty@ttyS#{unit}" do
 
 121     action [:enable, :start]
 
 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]
 
 132   package "linux-image-#{kernel_version}-generic"
 
 133   package "linux-image-extra-#{kernel_version}-generic"
 
 134   package "linux-headers-#{kernel_version}-generic"
 
 135   package "linux-tools-#{kernel_version}-generic"
 
 137   boot_device = IO.popen(["df", "/boot"]).readlines.last.split.first
 
 138   boot_uuid = IO.popen(["blkid", "-o", "value", "-s", "UUID", boot_device]).readlines.first.chomp
 
 139   grub_entry = "gnulinux-advanced-#{boot_uuid}>gnulinux-#{kernel_version}-advanced-#{boot_uuid}"
 
 144 if File.exist?("/etc/default/grub")
 
 145   execute "update-grub" do
 
 147     command "/usr/sbin/update-grub"
 
 150   template "/etc/default/grub" do
 
 155     variables :units => units, :entry => grub_entry
 
 156     notifies :run, "execute[update-grub]"
 
 160 execute "update-initramfs" do
 
 162   command "update-initramfs -u -k all"
 
 167 template "/etc/initramfs-tools/conf.d/mdadm" do
 
 168   source "initramfs-mdadm.erb"
 
 172   notifies :run, "execute[update-initramfs]"
 
 177   action [:enable, :start]
 
 180 package "ipmitool" if node[:kernel][:modules].include?("ipmi_si")
 
 184 service "irqbalance" do
 
 185   action [:start, :enable]
 
 186   supports :status => false, :restart => true, :reload => false
 
 189 # Link Layer Discovery Protocol Daemon
 
 192   action [:start, :enable]
 
 193   supports :status => true, :restart => true, :reload => true
 
 199 if node[:virtualization][:role] != "guest" ||
 
 200    (node[:virtualization][:system] != "lxc" &&
 
 201     node[:virtualization][:system] != "lxd" &&
 
 202     node[:virtualization][:system] != "openvz")
 
 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)]
 
 245 %w[ssacli lsiutil sas2ircu megactl megacli arcconf].each do |tools_package|
 
 246   if tools_packages.include?(tools_package)
 
 247     package tools_package
 
 249     package tools_package do
 
 255 if tools_packages.include?("areca")
 
 260     repository "https://git.openstreetmap.org/private/areca.git"
 
 264     not_if { ENV["TEST_KITCHEN"] }
 
 267   directory "/opt/areca" do
 
 273 if status_packages.include?("cciss-vol-status")
 
 274   template "/usr/local/bin/cciss-vol-statusd" do
 
 275     source "cciss-vol-statusd.erb"
 
 279     notifies :restart, "service[cciss-vol-statusd]"
 
 282   systemd_service "cciss-vol-statusd" do
 
 283     description "Check cciss_vol_status values in the background"
 
 284     exec_start "/usr/local/bin/cciss-vol-statusd"
 
 286     protect_system "full"
 
 288     no_new_privileges true
 
 289     notifies :restart, "service[cciss-vol-statusd]"
 
 292   systemd_service "cciss-vol-statusd" do
 
 296   template "/usr/local/bin/cciss-vol-statusd" do
 
 301 %w[cciss-vol-status mpt-status sas2ircu-status megaraid-status megaclisas-status aacraid-status].each do |status_package|
 
 302   if status_packages.include?(status_package)
 
 303     package status_package
 
 305     template "/etc/default/#{status_package}d" do
 
 306       source "raid.default.erb"
 
 310       variables :devices => status_packages[status_package]
 
 313     service "#{status_package}d" do
 
 314       action [:start, :enable]
 
 315       supports :status => false, :restart => true, :reload => false
 
 316       subscribes :restart, "template[/etc/default/#{status_package}d]"
 
 319     package status_package do
 
 323     file "/etc/default/#{status_package}d" do
 
 329 disks = if node[:hardware][:disk]
 
 330           node[:hardware][:disk][:disks]
 
 335 intel_ssds = disks.select { |d| d[:vendor] == "INTEL" && d[:model] =~ /^SSD/ }
 
 337 nvmes = if node[:hardware][:pci]
 
 338           node[:hardware][:pci].values.select { |pci| pci[:driver] == "nvme" }
 
 343 intel_nvmes = nvmes.select { |pci| pci[:vendor_name] == "Intel Corporation" }
 
 345 if !intel_ssds.empty? || !intel_nvmes.empty?
 
 348   intel_ssd_tool_version = "3.0.24"
 
 350   remote_file "#{Chef::Config[:file_cache_path]}/Intel_SSD_Data_Center_Tool_#{intel_ssd_tool_version}_Linux.zip" do
 
 351     source "https://downloadmirror.intel.com/29399/eng/Intel_SSD_DCT_#{intel_ssd_tool_version}_Linux%20.zip"
 
 354   execute "#{Chef::Config[:file_cache_path]}/Intel_SSD_Data_Center_Tool_#{intel_ssd_tool_version}_Linux.zip" do
 
 355     command "unzip Intel_SSD_Data_Center_Tool_#{intel_ssd_tool_version}_Linux.zip isdct_#{intel_ssd_tool_version}-1_amd64.deb"
 
 356     cwd Chef::Config[:file_cache_path]
 
 359     not_if { ::File.exist?("#{Chef::Config[:file_cache_path]}/isdct_#{intel_ssd_tool_version}-1_amd64.deb") }
 
 362   dpkg_package "isdct" do
 
 363     version "#{intel_ssd_tool_version}-1"
 
 364     source "#{Chef::Config[:file_cache_path]}/isdct_#{intel_ssd_tool_version}-1_amd64.deb"
 
 368 disks = disks.map do |disk|
 
 369   next if disk[:state] == "spun_down" || %w[unconfigured failed].any?(disk[:status])
 
 371   if disk[:smart_device]
 
 372     controller = node[:hardware][:disk][:controllers][disk[:controller]]
 
 374     if controller && controller[:device]
 
 375       device = controller[:device].sub("/dev/", "")
 
 376       smart = disk[:smart_device]
 
 378       if device.start_with?("cciss/") && smart =~ /^cciss,(\d+)$/
 
 379         array = node[:hardware][:disk][:arrays][disk[:arrays].first]
 
 380         munin = "cciss-3#{array[:wwn]}-#{Regexp.last_match(1)}"
 
 381       elsif smart =~ /^.*,(\d+)$/
 
 382         munin = "#{device}-#{Regexp.last_match(1)}"
 
 383       elsif smart =~ %r{^.*,(\d+)/(\d+)$}
 
 384         munin = "#{device}-#{Regexp.last_match(1)}:#{Regexp.last_match(2)}"
 
 387   elsif disk[:device] =~ %r{^/dev/(nvme\d+)n\d+$}
 
 388     device = Regexp.last_match(1)
 
 391     device = disk[:device].sub("/dev/", "")
 
 401     :hddtemp => munin.tr("-:", "_")
 
 405 disks = disks.compact.uniq
 
 407 if disks.count.positive?
 
 408   package "smartmontools"
 
 410   template "/usr/local/bin/smartd-mailer" do
 
 411     source "smartd-mailer.erb"
 
 417   template "/etc/smartd.conf" do
 
 418     source "smartd.conf.erb"
 
 422     variables :disks => disks
 
 425   template "/etc/default/smartmontools" do
 
 426     source "smartmontools.erb"
 
 433     action [:enable, :start]
 
 434     subscribes :reload, "template[/etc/smartd.conf]"
 
 435     subscribes :restart, "template[/etc/default/smartmontools]"
 
 438   # Don't try and do munin monitoring of disks behind
 
 439   # an Areca controller as they only allow one thing to
 
 440   # talk to the controller at a time and smartd will
 
 441   # throw errors if it clashes with munin
 
 442   disks = disks.reject { |disk| disk[:smart]&.start_with?("areca,") }
 
 445     munin_plugin "smart_#{disk[:munin]}" do
 
 447       conf "munin.smart.erb"
 
 448       conf_variables :disk => disk
 
 453     action [:stop, :disable]
 
 457 if disks.count.positive?
 
 458   munin_plugin "hddtemp_smartctl" do
 
 459     conf "munin.hddtemp.erb"
 
 460     conf_variables :disks => disks
 
 463   munin_plugin "hddtemp_smartctl" do
 
 465     conf "munin.hddtemp.erb"
 
 469 plugins = Dir.glob("/etc/munin/plugins/smart_*").map { |p| File.basename(p) } -
 
 470           disks.map { |d| "smart_#{d[:munin]}" }
 
 472 plugins.each do |plugin|
 
 473   munin_plugin plugin do
 
 475     conf "munin.smart.erb"
 
 479 if File.exist?("/etc/mdadm/mdadm.conf")
 
 480   mdadm_conf = edit_file "/etc/mdadm/mdadm.conf" do |line|
 
 481     line.gsub!(/^MAILADDR .*$/, "MAILADDR admins@openstreetmap.org")
 
 486   file "/etc/mdadm/mdadm.conf" do
 
 495     subscribes :restart, "file[/etc/mdadm/mdadm.conf]"
 
 499 template "/etc/modules" do
 
 508   subscribes :start, "template[/etc/modules]"
 
 511 if node[:hardware][:watchdog]
 
 514   template "/etc/default/watchdog" do
 
 515     source "watchdog.erb"
 
 519     variables :module => node[:hardware][:watchdog]
 
 522   service "watchdog" do
 
 523     action [:enable, :start]
 
 527 unless Dir.glob("/sys/class/hwmon/hwmon*").empty?
 
 530   Dir.glob("/sys/devices/platform/coretemp.*").each do |coretemp|
 
 531     cpu = File.basename(coretemp).sub("coretemp.", "").to_i
 
 532     chip = format("coretemp-isa-%04d", cpu)
 
 534     temps = if File.exist?("#{coretemp}/name")
 
 535               Dir.glob("#{coretemp}/temp*_input").map do |temp|
 
 536                 File.basename(temp).sub("temp", "").sub("_input", "").to_i
 
 539               Dir.glob("#{coretemp}/hwmon/hwmon*/temp*_input").map do |temp|
 
 540                 File.basename(temp).sub("temp", "").sub("_input", "").to_i
 
 545       node.default[:hardware][:sensors][chip][:temps][:temp1][:label] = "CPU #{cpu}"
 
 549     temps.each_with_index do |temp, index|
 
 550       node.default[:hardware][:sensors][chip][:temps]["temp#{temp}"][:label] = "CPU #{cpu} Core #{index}"
 
 554   execute "/etc/sensors.d/chef.conf" do
 
 556     command "/usr/bin/sensors -s"
 
 561   template "/etc/sensors.d/chef.conf" do
 
 562     source "sensors.conf.erb"
 
 566     notifies :run, "execute[/etc/sensors.d/chef.conf]"
 
 570 if node[:hardware][:shm_size]
 
 571   execute "remount-dev-shm" do
 
 573     command "/bin/mount -o remount /dev/shm"
 
 582     options "rw,nosuid,nodev,size=#{node[:hardware][:shm_size]}"
 
 583     notifies :run, "execute[remount-dev-shm]"