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 "prometheus"
 
  24 include_recipe "sysfs"
 
  25 include_recipe "tools"
 
  27 ohai_plugin "hardware" do
 
  28   template "ohai.rb.erb"
 
  31 if platform?("debian")
 
  32   package "firmware-linux"
 
  35 if node[:cpu] && node[:cpu][:"0"] && node[:cpu][:"0"][:vendor_id]
 
  36   case node[:cpu][:"0"][:vendor_id]
 
  38     package "intel-microcode"
 
  40     package "amd64-microcode"
 
  44 if node[:dmi] && node[:dmi][:system]
 
  45   case node[:dmi][:system][:manufacturer]
 
  47     manufacturer = node[:dmi][:base_board][:manufacturer]
 
  48     product = node[:dmi][:base_board][:product_name]
 
  50     manufacturer = node[:dmi][:system][:manufacturer]
 
  51     product = node[:dmi][:system][:product_name]
 
  54   manufacturer = "Unknown"
 
  60 if node[:roles].include?("bytemark") || node[:roles].include?("exonetric") || node[:roles].include?("prgmr")
 
  66   include_recipe "apt::management-component-pack"
 
  70   execute "update-ilo" do
 
  72     command "/usr/sbin/hponcfg -f /etc/ilo-defaults.xml"
 
  76   template "/etc/ilo-defaults.xml" do
 
  77     source "ilo-defaults.xml.erb"
 
  81     notifies :run, "execute[update-ilo]"
 
  84   package "hp-health" do
 
  86     notifies :restart, "service[hp-health]"
 
  87     only_if { platform?("ubuntu") && node[:lsb][:release].to_f < 22.04 }
 
  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 }
 
  96   if product.end_with?("Gen8", "Gen9")
 
  99       notifies :restart, "service[hp-ams]"
 
 103       action [:enable, :start]
 
 104       supports :status => true, :restart => true
 
 106   elsif product.end_with?("Gen10")
 
 109       notifies :restart, "service[amsd]"
 
 113       action [:enable, :start]
 
 114       supports :status => true, :restart => true
 
 118   units << if product.end_with?("Gen10")
 
 125 when "TYAN Computer Corporation"
 
 132   package "open-vm-tools"
 
 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
 
 138     notifies :restart, "service[open-vm-tools]"
 
 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"
 
 147   service "open-vm-tools" do
 
 148     action [:enable, :start]
 
 149     supports :status => true, :restart => true
 
 153 units.sort.uniq.each do |unit|
 
 154   service "serial-getty@ttyS#{unit}" do
 
 155     action [:enable, :start]
 
 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]
 
 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"
 
 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}"
 
 179 if File.exist?("/etc/default/grub")
 
 180   execute "update-grub" do
 
 182     command "/usr/sbin/update-grub"
 
 186   template "/etc/default/grub" do
 
 191     variables :units => units, :entry => grub_entry
 
 192     notifies :run, "execute[update-grub]"
 
 196 package "initramfs-tools"
 
 198 execute "update-initramfs" do
 
 200   command "update-initramfs -u -k all"
 
 205 template "/etc/initramfs-tools/conf.d/mdadm" do
 
 206   source "initramfs-mdadm.erb"
 
 210   notifies :run, "execute[update-initramfs]"
 
 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?
 
 218     action [:enable, :start]
 
 222     action [:stop, :disable]
 
 229 watchdog_module = %w[hpwdt sp5100_tco].find do |module_name|
 
 230   node[:hardware][:pci].any? { |_, pci| pci[:modules]&.any?(module_name) }
 
 233 if node[:kernel][:modules].include?("ipmi_si")
 
 235   package "freeipmi-tools"
 
 237   template "/etc/prometheus/ipmi_local.yml" do
 
 238     source "ipmi_local.yml.erb"
 
 244   prometheus_exporter "ipmi" do
 
 247     private_devices 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]"
 
 254   watchdog_module ||= "ipmi_watchdog"
 
 259 service "irqbalance" do
 
 260   action [:start, :enable]
 
 261   supports :status => false, :restart => true, :reload => false
 
 267   action [:start, :enable]
 
 268   supports :status => true, :restart => true, :reload => true
 
 271 ohai_plugin "lldp" do
 
 272   template "lldp.rb.erb"
 
 280 service "rasdaemon" do
 
 281   action [:enable, :start]
 
 284 prometheus_exporter "rasdaemon" do
 
 292 if node[:virtualization][:role] != "guest" ||
 
 293    (node[:virtualization][:system] != "lxc" &&
 
 294     node[:virtualization][:system] != "lxd" &&
 
 295     node[:virtualization][:system] != "openvz")
 
 297   node[:kernel][:modules].each_key do |modname|
 
 300       tools_packages << "ssacli"
 
 301       status_packages["cciss-vol-status"] ||= []
 
 303       tools_packages << "ssacli"
 
 304       status_packages["cciss-vol-status"] ||= []
 
 306       tools_packages << "lsiutil"
 
 307       status_packages["mpt-status"] ||= []
 
 308     when "mpt2sas", "mpt3sas"
 
 309       tools_packages << "sas2ircu"
 
 310       status_packages["sas2ircu-status"] ||= []
 
 312       tools_packages << "megacli"
 
 313       status_packages["megaclisas-status"] ||= []
 
 315       tools_packages << "arcconf"
 
 316       status_packages["aacraid-status"] ||= []
 
 318       tools_packages << "areca"
 
 322   node[:block_device].each do |name, attributes|
 
 323     next unless attributes[:vendor] == "HP" && attributes[:model] == "LOGICAL VOLUME"
 
 325     if name =~ /^cciss!(c[0-9]+)d[0-9]+$/
 
 326       status_packages["cciss-vol-status"] |= ["cciss/#{Regexp.last_match[1]}d0"]
 
 328       Dir.glob("/sys/block/#{name}/device/scsi_generic/*").each do |sg|
 
 329         status_packages["cciss-vol-status"] |= [File.basename(sg)]
 
 335 %w[ssacli lsiutil sas2ircu megactl megacli arcconf].each do |tools_package|
 
 336   if tools_packages.include?(tools_package)
 
 337     package tools_package
 
 339     package tools_package do
 
 345 if tools_packages.include?("areca")
 
 350     repository "https://git.openstreetmap.org/private/areca.git"
 
 357   directory "/opt/areca" do
 
 363 include_recipe "apt::hwraid" unless status_packages.empty?
 
 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
 
 369     service "#{status_package}d" do
 
 370       action [:stop, :disable]
 
 373     file "/etc/default/#{status_package}d" do
 
 377     package status_package do
 
 383 systemd_service "cciss-vol-statusd" do
 
 387 template "/usr/local/bin/cciss-vol-statusd" do
 
 391 disks = if node[:hardware][:disk]
 
 392           node[:hardware][:disk][:disks]
 
 397 intel_ssds = disks.select { |d| d[:vendor] == "INTEL" && d[:model] =~ /^SSD/ }
 
 399 nvmes = if node[:hardware][:pci]
 
 400           node[:hardware][:pci].values.select { |pci| pci[:driver] == "nvme" }
 
 409 intel_nvmes = nvmes.select { |pci| pci[:vendor_name] == "Intel Corporation" }
 
 411 if !intel_ssds.empty? || !intel_nvmes.empty?
 
 414   sst_tool_version = "1.3"
 
 415   sst_package_version = "#{sst_tool_version}.208-0"
 
 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"
 
 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]
 
 426     not_if { ::File.exist?("#{Chef::Config[:file_cache_path]}/sst_#{sst_package_version}_amd64.deb") }
 
 429   dpkg_package "sst" do
 
 430     version "#{sst_package_version}"
 
 431     source "#{Chef::Config[:file_cache_path]}/sst_#{sst_package_version}_amd64.deb"
 
 434   dpkg_package "intelmas" do
 
 439 disks = disks.map do |disk|
 
 440   next if disk[:state] == "spun_down" || %w[unconfigured failed].any?(disk[:status])
 
 442   if disk[:smart_device]
 
 443     controller = node[:hardware][:disk][:controllers][disk[:controller]]
 
 445     if controller && controller[:device]
 
 446       device = controller[:device].sub("/dev/", "")
 
 447       smart = disk[:smart_device]
 
 449       if device.start_with?("cciss/") && smart =~ /^cciss,(\d+)$/
 
 450         array = node[:hardware][:disk][:arrays][disk[:arrays].first]
 
 451         munin = "cciss-3#{array[:wwn]}-#{Regexp.last_match(1)}"
 
 452       elsif smart =~ /^.*,(\d+)$/
 
 453         munin = "#{device}-#{Regexp.last_match(1)}"
 
 454       elsif smart =~ %r{^.*,(\d+)/(\d+)$}
 
 455         munin = "#{device}-#{Regexp.last_match(1)}:#{Regexp.last_match(2)}"
 
 458       device = disk[:device].sub("/dev/", "")
 
 459       smart = disk[:smart_device]
 
 461       if smart =~ /^.*,(\d+),(\d+),(\d+)$/
 
 462         munin = "#{device}-#{Regexp.last_match(1)}:#{Regexp.last_match(2)}:#{Regexp.last_match(3)}"
 
 465   elsif disk[:device] =~ %r{^/dev/(nvme\d+)n\d+$}
 
 466     device = Regexp.last_match(1)
 
 469     device = disk[:device].sub("/dev/", "")
 
 473   next if device.nil? || munin.nil?
 
 479     :hddtemp => munin.tr("-:", "_")
 
 483 disks = disks.compact.uniq
 
 485 if disks.count.positive?
 
 486   package "smartmontools"
 
 488   template "/etc/cron.daily/update-smart-drivedb" do
 
 489     source "update-smart-drivedb.erb"
 
 495   template "/usr/local/bin/smartd-mailer" do
 
 496     source "smartd-mailer.erb"
 
 502   template "/etc/smartd.conf" do
 
 503     source "smartd.conf.erb"
 
 507     variables :disks => disks
 
 510   template "/etc/default/smartmontools" do
 
 511     source "smartmontools.erb"
 
 517   service "smartmontools" do
 
 518     action [:enable, :start]
 
 519     subscribes :reload, "template[/etc/smartd.conf]"
 
 520     subscribes :restart, "template[/etc/default/smartmontools]"
 
 523   template "/etc/prometheus/collectors/smart.devices" do
 
 524     source "smart.devices.erb"
 
 528     variables :disks => disks
 
 531   prometheus_collector "smart" do
 
 534     capability_bounding_set %w[CAP_DAC_OVERRIDE CAP_SYS_ADMIN CAP_SYS_RAWIO]
 
 535     private_devices false
 
 540   # Don't try and do munin monitoring of disks behind
 
 541   # an Areca controller as they only allow one thing to
 
 542   # talk to the controller at a time and smartd will
 
 543   # throw errors if it clashes with munin
 
 544   disks = disks.reject { |disk| disk[:smart]&.start_with?("areca,") }
 
 547     munin_plugin "smart_#{disk[:munin]}" do
 
 549       conf "munin.smart.erb"
 
 550       conf_variables :disk => disk
 
 555     action [:stop, :disable]
 
 559 if disks.count.positive?
 
 560   munin_plugin "hddtemp_smartctl" do
 
 561     conf "munin.hddtemp.erb"
 
 562     conf_variables :disks => disks
 
 565   munin_plugin "hddtemp_smartctl" do
 
 567     conf "munin.hddtemp.erb"
 
 571 plugins = Dir.glob("/etc/munin/plugins/smart_*").map { |p| File.basename(p) } -
 
 572           disks.map { |d| "smart_#{d[:munin]}" }
 
 574 plugins.each do |plugin|
 
 575   munin_plugin plugin do
 
 577     conf "munin.smart.erb"
 
 581 if File.exist?("/etc/mdadm/mdadm.conf")
 
 582   mdadm_conf = edit_file "/etc/mdadm/mdadm.conf" do |line|
 
 583     line.gsub!(/^MAILADDR .*$/, "MAILADDR admins@openstreetmap.org")
 
 588   file "/etc/mdadm/mdadm.conf" do
 
 595   service "mdmonitor" do
 
 597     subscribes :restart, "file[/etc/mdadm/mdadm.conf]"
 
 601 file "/etc/modules" do
 
 605 node[:hardware][:modules].each do |module_name|
 
 606   kernel_module module_name do
 
 612 node[:hardware][:blacklisted_modules].each do |module_name|
 
 613   kernel_module module_name do
 
 619   kernel_module watchdog_module do
 
 623   execute "systemctl-reload" do
 
 625     command "systemctl daemon-reload"
 
 630   directory "/etc/systemd/system.conf.d" do
 
 636   template "/etc/systemd/system.conf.d/watchdog.conf" do
 
 637     source "watchdog.conf.erb"
 
 641     notifies :run, "execute[systemctl-reload]"
 
 645 unless Dir.glob("/sys/class/hwmon/hwmon*").empty?
 
 648   Dir.glob("/sys/devices/platform/coretemp.*").each do |coretemp|
 
 649     cpu = File.basename(coretemp).sub("coretemp.", "").to_i
 
 650     chip = format("coretemp-isa-%04d", cpu)
 
 652     temps = if File.exist?("#{coretemp}/name")
 
 653               Dir.glob("#{coretemp}/temp*_input").map do |temp|
 
 654                 File.basename(temp).sub("temp", "").sub("_input", "").to_i
 
 657               Dir.glob("#{coretemp}/hwmon/hwmon*/temp*_input").map do |temp|
 
 658                 File.basename(temp).sub("temp", "").sub("_input", "").to_i
 
 663       node.default[:hardware][:sensors][chip][:temps][:temp1][:label] = "CPU #{cpu}"
 
 667     temps.each_with_index do |temp, index|
 
 668       node.default[:hardware][:sensors][chip][:temps]["temp#{temp}"][:label] = "CPU #{cpu} Core #{index}"
 
 672   execute "/etc/sensors.d/chef.conf" do
 
 674     command "/usr/bin/sensors -s"
 
 679   template "/etc/sensors.d/chef.conf" do
 
 680     source "sensors.conf.erb"
 
 684     notifies :run, "execute[/etc/sensors.d/chef.conf]"
 
 688 if node[:hardware][:shm_size]
 
 689   execute "remount-dev-shm" do
 
 691     command "/bin/mount -o remount /dev/shm"
 
 700     options "rw,nosuid,nodev,size=#{node[:hardware][:shm_size]}"
 
 701     notifies :run, "execute[remount-dev-shm]"
 
 705 prometheus_collector "ohai" do
 
 709   capability_bounding_set %w[CAP_DAC_OVERRIDE CAP_SYS_ADMIN CAP_SYS_RAWIO]
 
 710   private_devices false
 
 713   protect_kernel_modules false