]> git.openstreetmap.org Git - chef.git/blob - cookbooks/hardware/recipes/default.rb
Split out default munin plugin installation to a new recipe
[chef.git] / cookbooks / hardware / recipes / default.rb
1 #
2 # Cookbook:: hardware
3 # Recipe:: default
4 #
5 # Copyright:: 2012, OpenStreetMap Foundation
6 #
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
10 #
11 #     https://www.apache.org/licenses/LICENSE-2.0
12 #
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.
18 #
19
20 include_recipe "munin"
21 include_recipe "tools"
22
23 ohai_plugin "hardware" do
24   template "ohai.rb.erb"
25 end
26
27 case node[:cpu][:"0"][:vendor_id]
28 when "GenuineIntel"
29   package "intel-microcode"
30 when "AuthenticAMD"
31   package "amd64-microcode"
32 end
33
34 if node[:dmi] && node[:dmi][:system]
35   case node[:dmi][:system][:manufacturer]
36   when "empty"
37     manufacturer = node[:dmi][:base_board][:manufacturer]
38     product = node[:dmi][:base_board][:product_name]
39   else
40     manufacturer = node[:dmi][:system][:manufacturer]
41     product = node[:dmi][:system][:product_name]
42   end
43 else
44   manufacturer = "Unknown"
45   product = "Unknown"
46 end
47
48 units = []
49
50 if node[:roles].include?("bytemark") || node[:roles].include?("exonetric")
51   units << "0"
52 end
53
54 case manufacturer
55 when "HP"
56   package "hponcfg"
57
58   package "hp-health" do
59     action :install
60     notifies :restart, "service[hp-health]"
61   end
62
63   service "hp-health" do
64     action [:enable, :start]
65     supports :status => true, :restart => true
66   end
67
68   if product.end_with?("Gen8", "Gen9")
69     package "hp-ams" do
70       action :install
71       notifies :restart, "service[hp-ams]"
72     end
73
74     service "hp-ams" do
75       action [:enable, :start]
76       supports :status => true, :restart => true
77     end
78   end
79
80   units << "1"
81 when "TYAN"
82   units << "0"
83 when "TYAN Computer Corporation"
84   units << "0"
85 when "Supermicro"
86   case product
87   when "H8DGU", "X9SCD", "X7DBU", "X7DW3", "X9DR7/E-(J)LN4F", "X9DR3-F", "X9DRW", "SYS-1028U-TN10RT+", "SYS-2028U-TN24R4T+", "SYS-1029P-WTRT", "Super Server"
88     units << "1"
89   else
90     units << "0"
91   end
92 when "IBM"
93   units << "0"
94 when "VMware, Inc."
95   package "open-vm-tools"
96
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
100     action :delete
101     notifies :restart, "service[open-vm-tools]"
102   end
103
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"
107     ignore_failure true
108   end
109
110   service "open-vm-tools" do
111     action [:enable, :start]
112     supports :status => true, :restart => true
113   end
114 end
115
116 units.sort.uniq.each do |unit|
117   service "serial-getty@ttyS#{unit}" do
118     action [:enable, :start]
119   end
120 end
121
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]
128
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"
133
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}"
137 else
138   grub_entry = "0"
139 end
140
141 if File.exist?("/etc/default/grub")
142   execute "update-grub" do
143     action :nothing
144     command "/usr/sbin/update-grub"
145   end
146
147   template "/etc/default/grub" do
148     source "grub.erb"
149     owner "root"
150     group "root"
151     mode 0o644
152     variables :units => units, :entry => grub_entry
153     notifies :run, "execute[update-grub]"
154   end
155 end
156
157 execute "update-initramfs" do
158   action :nothing
159   command "update-initramfs -u -k all"
160   user "root"
161   group "root"
162 end
163
164 template "/etc/initramfs-tools/conf.d/mdadm" do
165   source "initramfs-mdadm.erb"
166   owner "root"
167   group "root"
168   mode 0o644
169   notifies :run, "execute[update-initramfs]"
170 end
171
172 package "haveged"
173 service "haveged" do
174   action [:enable, :start]
175 end
176
177 package "ipmitool" if node[:kernel][:modules].include?("ipmi_si")
178
179 package "irqbalance"
180
181 template "/etc/default/irqbalance" do
182   source "irqbalance.erb"
183   owner "root"
184   group "root"
185   mode 0o644
186 end
187
188 service "irqbalance" do
189   action [:start, :enable]
190   supports :status => false, :restart => true, :reload => false
191   subscribes :restart, "template[/etc/default/irqbalance]"
192 end
193
194 # Link Layer Discovery Protocol Daemon
195 package "lldpd"
196 service "lldpd" do
197   action [:start, :enable]
198   supports :status => true, :restart => true, :reload => true
199 end
200
201 tools_packages = []
202 status_packages = {}
203
204 node[:kernel][:modules].each_key do |modname|
205   case modname
206   when "cciss"
207     tools_packages << "ssacli"
208     status_packages["cciss-vol-status"] ||= []
209   when "hpsa"
210     tools_packages << "ssacli"
211     status_packages["cciss-vol-status"] ||= []
212   when "mptsas"
213     tools_packages << "lsiutil"
214     status_packages["mpt-status"] ||= []
215   when "mpt2sas", "mpt3sas"
216     tools_packages << "sas2ircu"
217     status_packages["sas2ircu-status"] ||= []
218   when "megaraid_mm"
219     tools_packages << "megactl"
220     status_packages["megaraid-status"] ||= []
221   when "megaraid_sas"
222     tools_packages << "megacli"
223     status_packages["megaclisas-status"] ||= []
224   when "aacraid"
225     tools_packages << "arcconf"
226     status_packages["aacraid-status"] ||= []
227   when "arcmsr"
228     tools_packages << "areca"
229   end
230 end
231
232 node[:block_device].each do |name, attributes|
233   next unless attributes[:vendor] == "HP" && attributes[:model] == "LOGICAL VOLUME"
234
235   if name =~ /^cciss!(c[0-9]+)d[0-9]+$/
236     status_packages["cciss-vol-status"] |= ["cciss/#{Regexp.last_match[1]}d0"]
237   else
238     Dir.glob("/sys/block/#{name}/device/scsi_generic/*").each do |sg|
239       status_packages["cciss-vol-status"] |= [File.basename(sg)]
240     end
241   end
242 end
243
244 %w[ssacli lsiutil sas2ircu megactl megacli arcconf].each do |tools_package|
245   if tools_packages.include?(tools_package)
246     package tools_package
247   else
248     package tools_package do
249       action :purge
250     end
251   end
252 end
253
254 if tools_packages.include?("areca")
255   include_recipe "git"
256
257   git "/opt/areca" do
258     action :sync
259     repository "https://git.openstreetmap.org/private/areca.git"
260     user "root"
261     group "root"
262   end
263 else
264   directory "/opt/areca" do
265     action :delete
266     recursive true
267   end
268 end
269
270 if status_packages.include?("cciss-vol-status")
271   template "/usr/local/bin/cciss-vol-statusd" do
272     source "cciss-vol-statusd.erb"
273     owner "root"
274     group "root"
275     mode 0o755
276     notifies :restart, "service[cciss-vol-statusd]"
277   end
278
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"
282     private_tmp true
283     protect_system "full"
284     protect_home true
285     no_new_privileges true
286     notifies :restart, "service[cciss-vol-statusd]"
287   end
288 end
289
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
293
294     template "/etc/default/#{status_package}d" do
295       source "raid.default.erb"
296       owner "root"
297       group "root"
298       mode 0o644
299       variables :devices => status_packages[status_package]
300     end
301
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]"
306     end
307   else
308     package status_package do
309       action :purge
310     end
311
312     file "/etc/default/#{status_package}d" do
313       action :delete
314     end
315   end
316 end
317
318 disks = if node[:hardware][:disk]
319           node[:hardware][:disk][:disks]
320         else
321           []
322         end
323
324 # intel_ssds = disks.select { |d| d[:vendor] == "INTEL" && d[:model] =~ /^SSD/ }
325 #
326 # nvmes = if node[:hardware][:pci]
327 #           node[:hardware][:pci].values.select { |pci| pci[:driver] == "nvme" }
328 #         else
329 #           []
330 #         end
331 #
332 # intel_nvmes = nvmes.select { |pci| pci[:vendor_name] == "Intel Corporation" }
333 #
334 # if !intel_ssds.empty? || !intel_nvmes.empty?
335 #   package "unzip"
336 #
337 #   intel_ssd_tool_version = "3.0.21"
338 #
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"
341 #   end
342 #
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]
346 #     user "root"
347 #     group "root"
348 #     not_if { File.exist?("#{Chef::Config[:file_cache_path]}/isdct_#{intel_ssd_tool_version}-1_amd64.deb") }
349 #   end
350 #
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"
354 #   end
355 # end
356
357 disks = disks.map do |disk|
358   next if disk[:state] == "spun_down" || %w[unconfigured failed].any?(disk[:status])
359
360   if disk[:smart_device]
361     controller = node[:hardware][:disk][:controllers][disk[:controller]]
362
363     if controller && controller[:device]
364       device = controller[:device].sub("/dev/", "")
365       smart = disk[:smart_device]
366
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)}"
374       end
375     end
376   elsif disk[:device] =~ %r{^/dev/(nvme\d+)n\d+$}
377     device = Regexp.last_match(1)
378     munin = device
379   elsif disk[:device]
380     device = disk[:device].sub("/dev/", "")
381     munin = device
382   end
383
384   next if device.nil?
385
386   Hash[
387     :device => device,
388     :smart => smart,
389     :munin => munin,
390     :hddtemp => munin.tr("-:", "_")
391   ]
392 end
393
394 disks = disks.compact.uniq
395
396 if disks.count.positive?
397   package "smartmontools"
398
399   template "/usr/local/bin/smartd-mailer" do
400     source "smartd-mailer.erb"
401     owner "root"
402     group "root"
403     mode 0o755
404   end
405
406   template "/etc/smartd.conf" do
407     source "smartd.conf.erb"
408     owner "root"
409     group "root"
410     mode 0o644
411     variables :disks => disks
412   end
413
414   template "/etc/default/smartmontools" do
415     source "smartmontools.erb"
416     owner "root"
417     group "root"
418     mode 0o644
419   end
420
421   service "smartd" do
422     action [:enable, :start]
423     subscribes :reload, "template[/etc/smartd.conf]"
424     subscribes :restart, "template[/etc/default/smartmontools]"
425   end
426
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,") }
432
433   disks.each do |disk|
434     munin_plugin "smart_#{disk[:munin]}" do
435       target "smart_"
436       conf "munin.smart.erb"
437       conf_variables :disk => disk
438     end
439   end
440 else
441   service "smartd" do
442     action [:stop, :disable]
443   end
444 end
445
446 if disks.count.positive?
447   munin_plugin "hddtemp_smartctl" do
448     conf "munin.hddtemp.erb"
449     conf_variables :disks => disks
450   end
451 else
452   munin_plugin "hddtemp_smartctl" do
453     action :delete
454     conf "munin.hddtemp.erb"
455   end
456 end
457
458 plugins = Dir.glob("/etc/munin/plugins/smart_*").map { |p| File.basename(p) } -
459           disks.map { |d| "smart_#{d[:munin]}" }
460
461 plugins.each do |plugin|
462   munin_plugin plugin do
463     action :delete
464     conf "munin.smart.erb"
465   end
466 end
467
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")
471
472     line
473   end
474
475   file "/etc/mdadm/mdadm.conf" do
476     owner "root"
477     group "root"
478     mode 0o644
479     content mdadm_conf
480   end
481
482   service "mdadm" do
483     action :nothing
484     subscribes :restart, "file[/etc/mdadm/mdadm.conf]"
485   end
486 end
487
488 template "/etc/modules" do
489   source "modules.erb"
490   owner "root"
491   group "root"
492   mode 0o644
493 end
494
495 service "kmod" do
496   action :nothing
497   subscribes :start, "template[/etc/modules]"
498 end
499
500 if node[:hardware][:watchdog]
501   package "watchdog"
502
503   template "/etc/default/watchdog" do
504     source "watchdog.erb"
505     owner "root"
506     group "root"
507     mode 0o644
508     variables :module => node[:hardware][:watchdog]
509   end
510
511   service "watchdog" do
512     action [:enable, :start]
513   end
514 end
515
516 unless Dir.glob("/sys/class/hwmon/hwmon*").empty?
517   package "lm-sensors"
518
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)
522
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
526               end.sort
527             else
528               Dir.glob("#{coretemp}/hwmon/hwmon*/temp*_input").map do |temp|
529                 File.basename(temp).sub("temp", "").sub("_input", "").to_i
530               end.sort
531             end
532
533     if temps.first == 1
534       node.default[:hardware][:sensors][chip][:temps][:temp1][:label] = "CPU #{cpu}"
535       temps.shift
536     end
537
538     temps.each_with_index do |temp, index|
539       node.default[:hardware][:sensors][chip][:temps]["temp#{temp}"][:label] = "CPU #{cpu} Core #{index}"
540     end
541   end
542
543   execute "/etc/sensors.d/chef.conf" do
544     action :nothing
545     command "/usr/bin/sensors -s"
546     user "root"
547     group "root"
548   end
549
550   template "/etc/sensors.d/chef.conf" do
551     source "sensors.conf.erb"
552     owner "root"
553     group "root"
554     mode 0o644
555     notifies :run, "execute[/etc/sensors.d/chef.conf]"
556   end
557 end
558
559 if node[:hardware][:shm_size]
560   execute "remount-dev-shm" do
561     action :nothing
562     command "/bin/mount -o remount /dev/shm"
563     user "root"
564     group "root"
565   end
566
567   mount "/dev/shm" do
568     action :enable
569     device "tmpfs"
570     fstype "tmpfs"
571     options "rw,nosuid,nodev,size=#{node[:hardware][:shm_size]}"
572     notifies :run, "execute[remount-dev-shm]"
573   end
574 end