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 #     http://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"
 
  32 case node[:cpu][:"0"][:vendor_id]
 
  34   if node[:lsb][:release].to_f >= 14.04
 
  35     package "amd64-microcode"
 
  39 if node[:dmi] && node[:dmi][:system]
 
  40   case node[:dmi][:system][:manufacturer]
 
  42     manufacturer = node[:dmi][:base_board][:manufacturer]
 
  43     product = node[:dmi][:base_board][:product_name]
 
  45     manufacturer = node[:dmi][:system][:manufacturer]
 
  46     product = node[:dmi][:system][:product_name]
 
  49   manufacturer = "Unknown"
 
  62 when "TYAN Computer Corporation"
 
  67   when "H8DGU", "X9SCD", "X7DBU", "X7DW3", "X9DR7/E-(J)LN4F", "X9DR3-F", "X9DRW"
 
  80   file "/etc/init/ttySttyS#{unit}.conf" do
 
  84   template "/etc/init/ttyS#{unit}.conf" do
 
  89     variables :unit => unit, :speed => speed
 
  92   service "ttyS#{unit}" do
 
  93     provider Chef::Provider::Service::Upstart
 
  94     action [:enable, :start]
 
  95     supports :status => true, :restart => true, :reload => false
 
  96     subscribes :restart, "template[/etc/init/ttyS#{unit}.conf]"
 
 100 # if we need a different / special kernel version to make the hardware
 
 101 # work (e.g: https://github.com/openstreetmap/operations/issues/45) then
 
 102 # ensure that we have the package installed. the grub template will
 
 103 # make sure that this is the default on boot.
 
 104 if node[:hardware][:grub][:kernel]
 
 105   kernel_version = node[:hardware][:grub][:kernel]
 
 107   package "linux-image-#{kernel_version}-generic"
 
 108   package "linux-image-extra-#{kernel_version}-generic"
 
 109   package "linux-headers-#{kernel_version}-generic"
 
 111   boot_device = IO.popen(["df", "/boot"]).readlines.last.split.first
 
 112   boot_uuid = IO.popen(["blkid", "-o", "value", "-s", "UUID", boot_device]).readlines.first.chomp
 
 113   grub_entry = "gnulinux-advanced-#{boot_uuid}>gnulinux-#{kernel_version}-advanced-#{boot_uuid}"
 
 118 if File.exist?("/etc/default/grub")
 
 119   execute "update-grub" do
 
 121     command "/usr/sbin/update-grub"
 
 124   template "/etc/default/grub" do
 
 129     variables :unit => unit, :speed => speed, :entry => grub_entry
 
 130     notifies :run, "execute[update-grub]"
 
 134 execute "update-initramfs" do
 
 136   command "update-initramfs -u -k all"
 
 141 template "/etc/initramfs-tools/conf.d/mdadm" do
 
 142   source "initramfs-mdadm.erb"
 
 146   notifies :run, "execute[update-initramfs]"
 
 151   action [:enable, :start]
 
 154 if node[:kernel][:modules].include?("ipmi_si")
 
 158 if node[:lsb][:release].to_f >= 12.10
 
 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]"
 
 178 node[:kernel][:modules].each_key do |modname|
 
 181     tools_packages << "hpssacli"
 
 182     status_packages["cciss-vol-status"] ||= []
 
 184     tools_packages << "hpssacli"
 
 185     status_packages["cciss-vol-status"] ||= []
 
 187     tools_packages << "lsiutil"
 
 188     # status_packages["mpt-status"] ||= []
 
 189   when "mpt2sas", "mpt3sas"
 
 190     tools_packages << "sas2ircu"
 
 191     status_packages["sas2ircu-status"] ||= []
 
 193     tools_packages << "megactl"
 
 194     status_packages["megaraid-status"] ||= []
 
 196     tools_packages << "megacli"
 
 197     status_packages["megaclisas-status"] ||= []
 
 199     tools_packages << "arcconf"
 
 200     status_packages["aacraid-status"] ||= []
 
 202     tools_packages << "areca"
 
 206 node[:block_device].each do |name, attributes|
 
 207   next unless attributes[:vendor] == "HP" && attributes[:model] == "LOGICAL VOLUME"
 
 209   if name =~ /^cciss!(c[0-9]+)d[0-9]+$/
 
 210     status_packages["cciss-vol-status"] |= ["cciss/#{Regexp.last_match[1]}d0"]
 
 212     Dir.glob("/sys/block/#{name}/device/scsi_generic/*").each do |sg|
 
 213       status_packages["cciss-vol-status"] |= [File.basename(sg)]
 
 218 %w(hpssacli lsiutil sas2ircu megactl megacli arcconf).each do |tools_package|
 
 219   if tools_packages.include?(tools_package)
 
 220     package tools_package
 
 222     package tools_package do
 
 228 if tools_packages.include?("areca")
 
 233     repository "git://chef.openstreetmap.org/areca.git"
 
 238   directory "/opt/areca" do
 
 244 ["cciss-vol-status", "mpt-status", "sas2ircu-status", "megaraid-status", "megaclisas-status", "aacraid-status"].each do |status_package|
 
 245   if status_packages.include?(status_package)
 
 246     package status_package
 
 248     template "/etc/default/#{status_package}d" do
 
 249       source "raid.default.erb"
 
 253       variables :devices => status_packages[status_package]
 
 256     service "#{status_package}d" do
 
 257       action [:start, :enable]
 
 258       supports :status => false, :restart => true, :reload => false
 
 259       subscribes :restart, "template[/etc/default/#{status_package}d]"
 
 262     package status_package do
 
 266     file "/etc/default/#{status_package}d" do
 
 272 disks = if node[:hardware][:disk]
 
 273           node[:hardware][:disk][:disks]
 
 278 disks = disks.map do |disk|
 
 279   next if disk[:state] == "spun_down"
 
 281   if disk[:smart_device]
 
 282     controller = node[:hardware][:disk][:controllers][disk[:controller]]
 
 283     device = controller[:device].sub("/dev/", "")
 
 284     smart = disk[:smart_device]
 
 286     if device.start_with?("cciss/") && smart =~ /^cciss,(\d+)$/
 
 287       array = node[:hardware][:disk][:arrays][disk[:arrays].first]
 
 288       munin = "cciss-3#{array[:wwn]}-#{Regexp.last_match(1)}"
 
 289     elsif smart =~ /^.*,(\d+)$/
 
 290       munin = "#{device}-#{Regexp.last_match(1)}"
 
 291     elsif smart =~ %r{^.*,(\d+)/(\d+)$}
 
 292       munin = "#{device}-#{Regexp.last_match(1)}:#{Regexp.last_match(2)}"
 
 295     device = disk[:device].sub("/dev/", "")
 
 305     :hddtemp => munin.tr("-:", "_")
 
 309 disks = disks.compact
 
 312   package "smartmontools"
 
 314   template "/usr/local/bin/smartd-mailer" do
 
 315     source "smartd-mailer.erb"
 
 321   template "/etc/smartd.conf" do
 
 322     source "smartd.conf.erb"
 
 326     variables :disks => disks
 
 327     notifies :reload, "service[smartmontools]"
 
 330   template "/etc/default/smartmontools" do
 
 331     source "smartmontools.erb"
 
 335     notifies :restart, "service[smartmontools]"
 
 338   service "smartmontools" do
 
 339     action [:enable, :start]
 
 340     supports :status => true, :restart => true, :reload => true
 
 343   # Don't try and do munin monitoring of disks behind
 
 344   # an Areca controller as they only allow one thing to
 
 345   # talk to the controller at a time and smartd will
 
 346   # throw errors if it clashes with munin
 
 347   disks = disks.reject { |disk| disk[:smart] && disk[:smart].start_with?("areca,") }
 
 350     munin_plugin "smart_#{disk[:munin]}" do
 
 352       conf "munin.smart.erb"
 
 353       conf_variables :disk => disk
 
 357   service "smartmontools" do
 
 358     action [:stop, :disable]
 
 363   munin_plugin "hddtemp_smartctl" do
 
 364     conf "munin.hddtemp.erb"
 
 365     conf_variables :disks => disks
 
 368   munin_plugin "hddtemp_smartctl" do
 
 370     conf "munin.hddtemp.erb"
 
 374 plugins = Dir.glob("/etc/munin/plugins/smart_*").map { |p| File.basename(p) } -
 
 375           disks.map { |d| "smart_#{d[:munin]}" }
 
 377 plugins.each do |plugin|
 
 378   munin_plugin plugin do
 
 383 if File.exist?("/etc/mdadm/mdadm.conf")
 
 384   mdadm_conf = edit_file "/etc/mdadm/mdadm.conf" do |line|
 
 385     line.gsub!(/^MAILADDR .*$/, "MAILADDR admins@openstreetmap.org")
 
 390   file "/etc/mdadm/mdadm.conf" do
 
 399     subscribes :restart, "file[/etc/mdadm/mdadm.conf]"
 
 403 template "/etc/modules" do
 
 410 if node[:lsb][:release].to_f <= 12.10
 
 411   service "module-init-tools" do
 
 412     provider Chef::Provider::Service::Upstart
 
 414     subscribes :start, "template[/etc/modules]"
 
 418     if node[:lsb][:release].to_f >= 15.10
 
 419       provider Chef::Provider::Service::Systemd
 
 421       provider Chef::Provider::Service::Upstart
 
 424     subscribes :start, "template[/etc/modules]"
 
 428 if node[:hardware][:watchdog]
 
 431   template "/etc/default/watchdog" do
 
 432     source "watchdog.erb"
 
 436     variables :module => node[:hardware][:watchdog]
 
 439   service "watchdog" do
 
 440     action [:enable, :start]
 
 444 unless Dir.glob("/sys/class/hwmon/hwmon*").empty?
 
 447   Dir.glob("/sys/devices/platform/coretemp.*").each do |coretemp|
 
 448     cpu = File.basename(coretemp).sub("coretemp.", "").to_i
 
 449     chip = format("coretemp-isa-%04d", cpu)
 
 451     temps = if File.exist?("#{coretemp}/name")
 
 452               Dir.glob("#{coretemp}/temp*_input").map do |temp|
 
 453                 File.basename(temp).sub("temp", "").sub("_input", "").to_i
 
 456               Dir.glob("#{coretemp}/hwmon/hwmon*/temp*_input").map do |temp|
 
 457                 File.basename(temp).sub("temp", "").sub("_input", "").to_i
 
 462       node.default[:hardware][:sensors][chip][:temps][:temp1][:label] = "CPU #{cpu}"
 
 466     temps.each_with_index do |temp, index|
 
 467       node.default[:hardware][:sensors][chip][:temps]["temp#{temp}"][:label] = "CPU #{cpu} Core #{index}"
 
 471   execute "/etc/sensors.d/chef.conf" do
 
 473     command "/usr/bin/sensors -s"
 
 478   template "/etc/sensors.d/chef.conf" do
 
 479     source "sensors.conf.erb"
 
 483     notifies :run, "execute[/etc/sensors.d/chef.conf]"