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 node[:cpu] && node[:cpu][:"0"] && node[:cpu][:"0"][:vendor_id]
 
  32   case node[:cpu][:"0"][:vendor_id]
 
  34     package "intel-microcode"
 
  36     package "amd64-microcode"
 
  40 if node[:dmi] && node[:dmi][:system]
 
  41   case node[:dmi][:system][:manufacturer]
 
  43     manufacturer = node[:dmi][:base_board][:manufacturer]
 
  44     product = node[:dmi][:base_board][:product_name]
 
  46     manufacturer = node[:dmi][:system][:manufacturer]
 
  47     product = node[:dmi][:system][:product_name]
 
  50   manufacturer = "Unknown"
 
  56 if node[:roles].include?("bytemark") || node[:roles].include?("exonetric") || node[:roles].include?("prgmr")
 
  62   include_recipe "apt::management-component-pack"
 
  66   execute "update-ilo" do
 
  68     command "/usr/sbin/hponcfg -f /etc/ilo-defaults.xml"
 
  72   template "/etc/ilo-defaults.xml" do
 
  73     source "ilo-defaults.xml.erb"
 
  77     notifies :run, "execute[update-ilo]"
 
  80   package "hp-health" do
 
  82     notifies :restart, "service[hp-health]"
 
  83     only_if { platform?("ubuntu") && node[:lsb][:release].to_f < 22.04 }
 
  86   service "hp-health" do
 
  87     action [:enable, :start]
 
  88     supports :status => true, :restart => true
 
  89     only_if { platform?("ubuntu") && node[:lsb][:release].to_f < 22.04 }
 
  92   if product.end_with?("Gen8", "Gen9")
 
  95       notifies :restart, "service[hp-ams]"
 
  99       action [:enable, :start]
 
 100       supports :status => true, :restart => true
 
 102   elsif product.end_with?("Gen10")
 
 105       notifies :restart, "service[amsd]"
 
 109       action [:enable, :start]
 
 110       supports :status => true, :restart => true
 
 114   units << if product.end_with?("Gen10")
 
 121 when "TYAN Computer Corporation"
 
 128   package "open-vm-tools"
 
 130   # Remove timeSync plugin completely
 
 131   # https://github.com/vmware/open-vm-tools/issues/302
 
 132   file "/usr/lib/open-vm-tools/plugins/vmsvc/libtimeSync.so" do
 
 134     notifies :restart, "service[open-vm-tools]"
 
 137   # Attempt to tell Host we are not interested in timeSync
 
 138   execute "vmware-toolbox-cmd-timesync-disable" do
 
 139     command "/usr/bin/vmware-toolbox-cmd timesync disable"
 
 143   service "open-vm-tools" do
 
 144     action [:enable, :start]
 
 145     supports :status => true, :restart => true
 
 149 units.sort.uniq.each do |unit|
 
 150   service "serial-getty@ttyS#{unit}" do
 
 151     action [:enable, :start]
 
 156 # if we need a different / special kernel version to make the hardware
 
 157 # work (e.g: https://github.com/openstreetmap/operations/issues/45) then
 
 158 # ensure that we have the package installed. the grub template will
 
 159 # make sure that this is the default on boot.
 
 160 if node[:hardware][:grub][:kernel]
 
 161   kernel_version = node[:hardware][:grub][:kernel]
 
 163   package "linux-image-#{kernel_version}-generic"
 
 164   package "linux-image-extra-#{kernel_version}-generic"
 
 165   package "linux-headers-#{kernel_version}-generic"
 
 166   package "linux-tools-#{kernel_version}-generic"
 
 168   boot_device = IO.popen(["df", "/boot"]).readlines.last.split.first
 
 169   boot_uuid = IO.popen(["blkid", "-o", "value", "-s", "UUID", boot_device]).readlines.first.chomp
 
 170   grub_entry = "gnulinux-advanced-#{boot_uuid}>gnulinux-#{kernel_version}-advanced-#{boot_uuid}"
 
 175 if File.exist?("/etc/default/grub")
 
 176   execute "update-grub" do
 
 178     command "/usr/sbin/update-grub"
 
 182   template "/etc/default/grub" do
 
 187     variables :units => units, :entry => grub_entry
 
 188     notifies :run, "execute[update-grub]"
 
 192 package "initramfs-tools"
 
 194 execute "update-initramfs" do
 
 196   command "update-initramfs -u -k all"
 
 201 template "/etc/initramfs-tools/conf.d/mdadm" do
 
 202   source "initramfs-mdadm.erb"
 
 206   notifies :run, "execute[update-initramfs]"
 
 209 # haveged is only required on older kernels
 
 210 # /dev/random is not blocking anymore in 5.15+
 
 211 if Chef::Util.compare_versions(node[:kernel][:release], [5, 15]).negative?
 
 214     action [:enable, :start]
 
 218     action [:stop, :disable]
 
 225 if node[:kernel][:modules].include?("ipmi_si")
 
 227   package "freeipmi-tools"
 
 229   template "/etc/prometheus/ipmi_local.yml" do
 
 230     source "ipmi_local.yml.erb"
 
 236   prometheus_exporter "ipmi" do
 
 239     private_devices false
 
 241     system_call_filter ["@system-service", "@raw-io"]
 
 242     options "--config.file=/etc/prometheus/ipmi_local.yml"
 
 243     subscribes :restart, "template[/etc/prometheus/ipmi_local.yml]"
 
 249 service "irqbalance" do
 
 250   action [:start, :enable]
 
 251   supports :status => false, :restart => true, :reload => false
 
 257   action [:start, :enable]
 
 258   supports :status => true, :restart => true, :reload => true
 
 261 ohai_plugin "lldp" do
 
 262   template "lldp.rb.erb"
 
 270 service "rasdaemon" do
 
 271   action [:enable, :start]
 
 274 prometheus_exporter "rasdaemon" do
 
 282 if node[:virtualization][:role] != "guest" ||
 
 283    (node[:virtualization][:system] != "lxc" &&
 
 284     node[:virtualization][:system] != "lxd" &&
 
 285     node[:virtualization][:system] != "openvz")
 
 287   node[:kernel][:modules].each_key do |modname|
 
 290       tools_packages << "ssacli"
 
 291       status_packages["cciss-vol-status"] ||= []
 
 293       tools_packages << "ssacli"
 
 294       status_packages["cciss-vol-status"] ||= []
 
 296       tools_packages << "lsiutil"
 
 297       status_packages["mpt-status"] ||= []
 
 298     when "mpt2sas", "mpt3sas"
 
 299       tools_packages << "sas2ircu"
 
 300       status_packages["sas2ircu-status"] ||= []
 
 302       tools_packages << "megacli"
 
 303       status_packages["megaclisas-status"] ||= []
 
 305       tools_packages << "arcconf"
 
 306       status_packages["aacraid-status"] ||= []
 
 308       tools_packages << "areca"
 
 312   node[:block_device].each do |name, attributes|
 
 313     next unless attributes[:vendor] == "HP" && attributes[:model] == "LOGICAL VOLUME"
 
 315     if name =~ /^cciss!(c[0-9]+)d[0-9]+$/
 
 316       status_packages["cciss-vol-status"] |= ["cciss/#{Regexp.last_match[1]}d0"]
 
 318       Dir.glob("/sys/block/#{name}/device/scsi_generic/*").each do |sg|
 
 319         status_packages["cciss-vol-status"] |= [File.basename(sg)]
 
 325 %w[ssacli lsiutil sas2ircu megactl megacli arcconf].each do |tools_package|
 
 326   if tools_packages.include?(tools_package)
 
 327     package tools_package
 
 329     package tools_package do
 
 335 if tools_packages.include?("areca")
 
 340     repository "https://git.openstreetmap.org/private/areca.git"
 
 347   directory "/opt/areca" do
 
 353 include_recipe "apt::hwraid" unless status_packages.empty?
 
 355 %w[cciss-vol-status mpt-status sas2ircu-status megaclisas-status aacraid-status].each do |status_package|
 
 356   if status_packages.include?(status_package)
 
 357     package status_package
 
 359     service "#{status_package}d" do
 
 360       action [:stop, :disable]
 
 363     file "/etc/default/#{status_package}d" do
 
 367     package status_package do
 
 373 systemd_service "cciss-vol-statusd" do
 
 377 template "/usr/local/bin/cciss-vol-statusd" do
 
 381 disks = if node[:hardware][:disk]
 
 382           node[:hardware][:disk][:disks]
 
 387 intel_ssds = disks.select { |d| d[:vendor] == "INTEL" && d[:model] =~ /^SSD/ }
 
 389 nvmes = if node[:hardware][:pci]
 
 390           node[:hardware][:pci].values.select { |pci| pci[:driver] == "nvme" }
 
 399 intel_nvmes = nvmes.select { |pci| pci[:vendor_name] == "Intel Corporation" }
 
 401 if !intel_ssds.empty? || !intel_nvmes.empty?
 
 404   sst_tool_version = "1.3"
 
 405   sst_package_version = "#{sst_tool_version}.208-0"
 
 407   # remote_file "#{Chef::Config[:file_cache_path]}/SST_CLI_Linux_#{sst_tool_version}.zip" do
 
 408   #   source "https://downloadmirror.intel.com/743764/SST_CLI_Linux_#{sst_tool_version}.zip"
 
 411   execute "#{Chef::Config[:file_cache_path]}/SST_CLI_Linux_#{sst_tool_version}.zip" do
 
 412     command "unzip SST_CLI_Linux_#{sst_tool_version}.zip sst_#{sst_package_version}_amd64.deb"
 
 413     cwd Chef::Config[:file_cache_path]
 
 416     not_if { ::File.exist?("#{Chef::Config[:file_cache_path]}/sst_#{sst_package_version}_amd64.deb") }
 
 419   dpkg_package "sst" do
 
 420     version "#{sst_package_version}"
 
 421     source "#{Chef::Config[:file_cache_path]}/sst_#{sst_package_version}_amd64.deb"
 
 424   dpkg_package "intelmas" do
 
 429 disks = disks.map do |disk|
 
 430   next if disk[:state] == "spun_down" || %w[unconfigured failed].any?(disk[:status])
 
 432   if disk[:smart_device]
 
 433     controller = node[:hardware][:disk][:controllers][disk[:controller]]
 
 435     if controller && controller[:device]
 
 436       device = controller[:device].sub("/dev/", "")
 
 437       smart = disk[:smart_device]
 
 439       if device.start_with?("cciss/") && smart =~ /^cciss,(\d+)$/
 
 440         array = node[:hardware][:disk][:arrays][disk[:arrays].first]
 
 441         munin = "cciss-3#{array[:wwn]}-#{Regexp.last_match(1)}"
 
 442       elsif smart =~ /^.*,(\d+)$/
 
 443         munin = "#{device}-#{Regexp.last_match(1)}"
 
 444       elsif smart =~ %r{^.*,(\d+)/(\d+)$}
 
 445         munin = "#{device}-#{Regexp.last_match(1)}:#{Regexp.last_match(2)}"
 
 448       device = disk[:device].sub("/dev/", "")
 
 449       smart = disk[:smart_device]
 
 451       if smart =~ /^.*,(\d+),(\d+),(\d+)$/
 
 452         munin = "#{device}-#{Regexp.last_match(1)}:#{Regexp.last_match(2)}:#{Regexp.last_match(3)}"
 
 455   elsif disk[:device] =~ %r{^/dev/(nvme\d+)n\d+$}
 
 456     device = Regexp.last_match(1)
 
 459     device = disk[:device].sub("/dev/", "")
 
 463   next if device.nil? || munin.nil?
 
 469     :hddtemp => munin.tr("-:", "_")
 
 473 disks = disks.compact.uniq
 
 475 if disks.count.positive?
 
 476   package "smartmontools"
 
 478   template "/etc/cron.daily/update-smart-drivedb" do
 
 479     source "update-smart-drivedb.erb"
 
 485   template "/usr/local/bin/smartd-mailer" do
 
 486     source "smartd-mailer.erb"
 
 492   template "/etc/smartd.conf" do
 
 493     source "smartd.conf.erb"
 
 497     variables :disks => disks
 
 500   template "/etc/default/smartmontools" do
 
 501     source "smartmontools.erb"
 
 507   service "smartmontools" do
 
 508     action [:enable, :start]
 
 509     subscribes :reload, "template[/etc/smartd.conf]"
 
 510     subscribes :restart, "template[/etc/default/smartmontools]"
 
 513   template "/etc/prometheus/collectors/smart.devices" do
 
 514     source "smart.devices.erb"
 
 518     variables :disks => disks
 
 521   prometheus_collector "smart" do
 
 524     capability_bounding_set %w[CAP_DAC_OVERRIDE CAP_SYS_ADMIN CAP_SYS_RAWIO]
 
 525     private_devices false
 
 530   # Don't try and do munin monitoring of disks behind
 
 531   # an Areca controller as they only allow one thing to
 
 532   # talk to the controller at a time and smartd will
 
 533   # throw errors if it clashes with munin
 
 534   disks = disks.reject { |disk| disk[:smart]&.start_with?("areca,") }
 
 537     munin_plugin "smart_#{disk[:munin]}" do
 
 539       conf "munin.smart.erb"
 
 540       conf_variables :disk => disk
 
 545     action [:stop, :disable]
 
 549 if disks.count.positive?
 
 550   munin_plugin "hddtemp_smartctl" do
 
 551     conf "munin.hddtemp.erb"
 
 552     conf_variables :disks => disks
 
 555   munin_plugin "hddtemp_smartctl" do
 
 557     conf "munin.hddtemp.erb"
 
 561 plugins = Dir.glob("/etc/munin/plugins/smart_*").map { |p| File.basename(p) } -
 
 562           disks.map { |d| "smart_#{d[:munin]}" }
 
 564 plugins.each do |plugin|
 
 565   munin_plugin plugin do
 
 567     conf "munin.smart.erb"
 
 571 if File.exist?("/etc/mdadm/mdadm.conf")
 
 572   mdadm_conf = edit_file "/etc/mdadm/mdadm.conf" do |line|
 
 573     line.gsub!(/^MAILADDR .*$/, "MAILADDR admins@openstreetmap.org")
 
 578   file "/etc/mdadm/mdadm.conf" do
 
 585   service "mdmonitor" do
 
 587     subscribes :restart, "file[/etc/mdadm/mdadm.conf]"
 
 591 file "/etc/modules" do
 
 595 node[:hardware][:modules].each do |module_name|
 
 596   kernel_module module_name do
 
 602 node[:hardware][:blacklisted_modules].each do |module_name|
 
 603   kernel_module module_name do
 
 608 if node[:hardware][:watchdog]
 
 611   template "/etc/default/watchdog" do
 
 612     source "watchdog.erb"
 
 616     variables :module => node[:hardware][:watchdog]
 
 619   service "watchdog" do
 
 620     action [:enable, :start]
 
 624 unless Dir.glob("/sys/class/hwmon/hwmon*").empty?
 
 627   Dir.glob("/sys/devices/platform/coretemp.*").each do |coretemp|
 
 628     cpu = File.basename(coretemp).sub("coretemp.", "").to_i
 
 629     chip = format("coretemp-isa-%04d", cpu)
 
 631     temps = if File.exist?("#{coretemp}/name")
 
 632               Dir.glob("#{coretemp}/temp*_input").map do |temp|
 
 633                 File.basename(temp).sub("temp", "").sub("_input", "").to_i
 
 636               Dir.glob("#{coretemp}/hwmon/hwmon*/temp*_input").map do |temp|
 
 637                 File.basename(temp).sub("temp", "").sub("_input", "").to_i
 
 642       node.default[:hardware][:sensors][chip][:temps][:temp1][:label] = "CPU #{cpu}"
 
 646     temps.each_with_index do |temp, index|
 
 647       node.default[:hardware][:sensors][chip][:temps]["temp#{temp}"][:label] = "CPU #{cpu} Core #{index}"
 
 651   execute "/etc/sensors.d/chef.conf" do
 
 653     command "/usr/bin/sensors -s"
 
 658   template "/etc/sensors.d/chef.conf" do
 
 659     source "sensors.conf.erb"
 
 663     notifies :run, "execute[/etc/sensors.d/chef.conf]"
 
 667 if node[:hardware][:shm_size]
 
 668   execute "remount-dev-shm" do
 
 670     command "/bin/mount -o remount /dev/shm"
 
 679     options "rw,nosuid,nodev,size=#{node[:hardware][:shm_size]}"
 
 680     notifies :run, "execute[remount-dev-shm]"
 
 684 prometheus_collector "ohai" do
 
 688   capability_bounding_set %w[CAP_DAC_OVERRIDE CAP_SYS_ADMIN CAP_SYS_RAWIO]
 
 689   private_devices false
 
 692   protect_kernel_modules false