]> git.openstreetmap.org Git - chef.git/blob - cookbooks/hardware/recipes/default.rb
Autoconfigure sensible labels for Intel coretemp sensors
[chef.git] / cookbooks / hardware / recipes / default.rb
1 #
2 # Cookbook Name:: 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 #     http://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 "tools"
21 include_recipe "munin"
22
23 case node[:cpu][:"0"][:vendor_id]
24 when "GenuineIntel"
25   package "intel-microcode"
26 end
27
28 case node[:cpu][:"0"][:vendor_id]
29 when "AuthenticAMD"
30   if node[:lsb][:release].to_f >= 14.04
31     package "amd64-microcode"
32   end
33 end
34
35 if node[:dmi] && node[:dmi][:system]
36   case node[:dmi][:system][:manufacturer]
37   when "empty"
38     manufacturer = node[:dmi][:base_board][:manufacturer]
39     product = node[:dmi][:base_board][:product_name]
40   else
41     manufacturer = node[:dmi][:system][:manufacturer]
42     product = node[:dmi][:system][:product_name]
43   end
44 else
45   manufacturer = "Unknown"
46   product = "Unknown"
47 end
48
49 case manufacturer
50 when "HP"
51   package "hponcfg"
52   package "hp-health"
53   unit = "1"
54   speed = "115200"
55 when "TYAN"
56   unit = "0"
57   speed = "115200"
58 when "TYAN Computer Corporation"
59   unit = "0"
60   speed = "115200"
61 when "Supermicro"
62   case product
63   when "H8DGU", "X9SCD", "X7DBU", "X7DW3", "X9DR7/E-(J)LN4F", "X9DR3-F", "X9DRW"
64     unit = "1"
65     speed = "115200"
66   else
67     unit = "0"
68     speed = "115200"
69   end
70 when "IBM"
71   unit = "0"
72   speed = "115200"
73 end
74
75 if manufacturer == "HP" && node[:lsb][:release].to_f > 11.10
76   include_recipe "git"
77
78   git "/opt/hp/hp-legacy" do
79     action :sync
80     repository "git://chef.openstreetmap.org/hp-legacy.git"
81     user "root"
82     group "root"
83     ignore_failure true
84   end
85
86   link "/opt/hp/hp-health/bin/hpasmd" do
87     to "/opt/hp/hp-legacy/hpasmd"
88   end
89
90   link "/usr/lib/libhpasmintrfc.so.3.0" do
91     to "/opt/hp/hp-legacy/libhpasmintrfc.so.3.0"
92   end
93
94   link "/usr/lib/libhpasmintrfc.so.3" do
95     to "libhpasmintrfc.so.3.0"
96   end
97
98   link "/usr/lib/libhpasmintrfc.so" do
99     to "libhpasmintrfc.so.3.0"
100   end
101 end
102
103 unless unit.nil?
104   file "/etc/init/ttySttyS#{unit}.conf" do
105     action :delete
106   end
107
108   template "/etc/init/ttyS#{unit}.conf" do
109     source "tty.conf.erb"
110     owner "root"
111     group "root"
112     mode 0644
113     variables :unit => unit, :speed => speed
114   end
115
116   service "ttyS#{unit}" do
117     provider Chef::Provider::Service::Upstart
118     action [:enable, :start]
119     supports :status => true, :restart => true, :reload => false
120     subscribes :restart, "template[/etc/init/ttyS#{unit}.conf]"
121   end
122 end
123
124 # if we need a different / special kernel version to make the hardware
125 # work (e.g: https://github.com/openstreetmap/operations/issues/45) then
126 # ensure that we have the package installed. the grub template will
127 # make sure that this is the default on boot.
128 if node[:hardware][:grub][:kernel]
129   kernel_version = node[:hardware][:grub][:kernel]
130
131   package "linux-image-#{kernel_version}-generic"
132   package "linux-image-extra-#{kernel_version}-generic"
133   package "linux-headers-#{kernel_version}-generic"
134
135   boot_device = IO.popen(["df", "/boot"]).readlines.last.split.first
136   boot_uuid = IO.popen(["blkid", "-o", "value", "-s", "UUID", boot_device]).readlines.first.chomp
137   grub_entry = "gnulinux-advanced-#{boot_uuid}>gnulinux-#{kernel_version}-advanced-#{boot_uuid}"
138 else
139   grub_entry = "0"
140 end
141
142 if File.exist?("/etc/default/grub")
143   execute "update-grub" do
144     action :nothing
145     command "/usr/sbin/update-grub"
146   end
147
148   template "/etc/default/grub" do
149     source "grub.erb"
150     owner "root"
151     group "root"
152     mode 0644
153     variables :unit => unit, :speed => speed, :entry => grub_entry
154     notifies :run, "execute[update-grub]"
155   end
156 end
157
158 execute "update-initramfs" do
159   action :nothing
160   command "update-initramfs -u -k all"
161   user "root"
162   group "root"
163 end
164
165 template "/etc/initramfs-tools/conf.d/mdadm" do
166   source "initramfs-mdadm.erb"
167   owner "root"
168   group "root"
169   mode 0644
170   notifies :run, "execute[update-initramfs]"
171 end
172
173 package "haveged"
174 service "haveged" do
175   action [:enable, :start]
176 end
177
178 if node[:kernel][:modules].include?("ipmi_si")
179   package "ipmitool"
180 end
181
182 if node[:lsb][:release].to_f >= 12.10
183   package "irqbalance"
184
185   template "/etc/default/irqbalance" do
186     source "irqbalance.erb"
187     owner "root"
188     group "root"
189     mode 0644
190   end
191
192   service "irqbalance" do
193     action [:start, :enable]
194     supports :status => false, :restart => true, :reload => false
195     subscribes :restart, "template[/etc/default/irqbalance]"
196   end
197 end
198
199 tools_packages = []
200 status_packages = {}
201
202 node[:kernel][:modules].each_key do |modname|
203   case modname
204   when "cciss"
205     tools_packages << "hpssacli"
206     status_packages["cciss-vol-status"] ||= []
207   when "hpsa"
208     tools_packages << "hpssacli"
209     status_packages["cciss-vol-status"] ||= []
210   when "mptsas"
211     tools_packages << "lsiutil"
212     # status_packages["mpt-status"] ||= []
213   when "mpt2sas", "mpt3sas"
214     tools_packages << "sas2ircu"
215     status_packages["sas2ircu-status"] ||= []
216   when "megaraid_mm"
217     tools_packages << "megactl"
218     status_packages["megaraid-status"] ||= []
219   when "megaraid_sas"
220     tools_packages << "megacli"
221     status_packages["megaclisas-status"] ||= []
222   when "aacraid"
223     tools_packages << "arcconf"
224     status_packages["aacraid-status"] ||= []
225   when "arcmsr"
226     tools_packages << "areca"
227   end
228 end
229
230 node[:block_device].each do |name, attributes|
231   next unless attributes[:vendor] == "HP" && attributes[:model] == "LOGICAL VOLUME"
232
233   if name =~ /^cciss!(c[0-9]+)d[0-9]+$/
234     status_packages["cciss-vol-status"] |= ["cciss/#{Regexp.last_match[1]}d0"]
235   else
236     Dir.glob("/sys/block/#{name}/device/scsi_generic/*").each do |sg|
237       status_packages["cciss-vol-status"] |= [File.basename(sg)]
238     end
239   end
240 end
241
242 %w(hpacucli lsiutil sas2ircu megactl megacli arcconf).each do |tools_package|
243   if tools_packages.include?(tools_package)
244     package tools_package
245   else
246     package tools_package do
247       action :purge
248     end
249   end
250 end
251
252 if tools_packages.include?("areca")
253   include_recipe "git"
254
255   git "/opt/areca" do
256     action :sync
257     repository "git://chef.openstreetmap.org/areca.git"
258     user "root"
259     group "root"
260   end
261 else
262   directory "/opt/areca" do
263     action :delete
264     recursive true
265   end
266 end
267
268 ["cciss-vol-status", "mpt-status", "sas2ircu-status", "megaraid-status", "megaclisas-status", "aacraid-status"].each do |status_package|
269   if status_packages.include?(status_package)
270     package status_package
271
272     template "/etc/default/#{status_package}d" do
273       source "raid.default.erb"
274       owner "root"
275       group "root"
276       mode 0644
277       variables :devices => status_packages[status_package]
278     end
279
280     service "#{status_package}d" do
281       action [:start, :enable]
282       supports :status => false, :restart => true, :reload => false
283       subscribes :restart, "template[/etc/default/#{status_package}d]"
284     end
285   else
286     package status_package do
287       action :purge
288     end
289
290     file "/etc/default/#{status_package}d" do
291       action :delete
292     end
293   end
294 end
295
296 disks = []
297
298 node[:block_device].each do |name, attributes|
299   disks << { :device => name } if attributes[:vendor] == "ATA"
300 end
301
302 if status_packages["cciss-vol-status"] && File.exist?("/usr/sbin/cciss_vol_status")
303   status_packages["cciss-vol-status"].each do |device|
304     IO.popen(["cciss_vol_status", "-V", "/dev/#{device}"]).each do |line|
305       disks << { :device => device, :driver => "cciss", :id => Regexp.last_match[1].to_i - 1 } if line =~ / bay ([0-9]+) +HP /
306     end
307   end
308 end
309
310 if status_packages["megaclisas-status"]
311   controller = 0
312
313   Dir.glob("/sys/class/scsi_host/host*") do |host|
314     driver = File.new("#{host}/proc_name").read.chomp
315
316     next unless driver == "megaraid_sas"
317
318     bus = host.sub("/sys/class/scsi_host/host", "")
319     device = File.basename(Dir.glob("/sys/bus/scsi/devices/#{bus}:*/scsi_generic/*").first)
320
321     IO.popen(["megacli", "-PDList", "-a#{controller}", "-NoLog"]).each do |line|
322       disks << { :device => device, :driver => "megaraid",  :id => Regexp.last_match[1] } if line =~ /^Device Id: ([0-9]+)$/
323
324       disks.pop if line =~ /^Firmware state: Hotspare, Spun down$/
325     end
326
327     controller += 1
328   end
329 end
330
331 if tools_packages.include?("lsiutil")
332   Dir.glob("/sys/class/scsi_host/host*") do |host|
333     driver = File.new("#{host}/proc_name").read.chomp
334
335     next unless driver == "mptsas"
336
337     bus = host.sub("/sys/class/scsi_host/host", "")
338
339     Dir.glob("/sys/bus/scsi/devices/#{bus}:0:*/scsi_generic/*").each do |sg|
340       disks << { :device => File.basename(sg) }
341     end
342   end
343 end
344
345 if status_packages["sas2ircu-status"]
346   Dir.glob("/sys/class/scsi_host/host*") do |host|
347     driver = File.new("#{host}/proc_name").read.chomp
348
349     next unless driver == "mpt2sas" || driver == "mpt3sas"
350
351     bus = host.sub("/sys/class/scsi_host/host", "")
352
353     Dir.glob("/sys/bus/scsi/devices/#{bus}:0:*/scsi_generic/*").each do |sg|
354       next if File.directory?("#{sg}/../../block")
355
356       disks << { :device => File.basename(sg) }
357     end
358   end
359 end
360
361 if status_packages["aacraid-status"]
362   Dir.glob("/sys/class/scsi_host/host*") do |host|
363     driver = File.new("#{host}/proc_name").read.chomp
364
365     next unless driver == "aacraid"
366
367     bus = host.sub("/sys/class/scsi_host/host", "")
368
369     Dir.glob("/sys/bus/scsi/devices/#{bus}:1:*/scsi_generic/*").each do |sg|
370       disks << { :device => File.basename(sg) }
371     end
372   end
373 end
374
375 if tools_packages.include?("areca") && File.exist?("/opt/areca/x86_64/cli64")
376   device = IO.popen(["lsscsi", "-g"]).grep(%r{Areca +RAID controller .*/dev/(sg[0-9]+)}) do
377     Regexp.last_match[1]
378   end.first
379
380   IO.popen(["/opt/areca/x86_64/cli64", "disk", "info"]).each do |line|
381     next if line =~ /N\.A\./
382
383     if line =~ /^ +[0-9]+ +0*([0-9]+) +(?:Slot#|SLOT )0*([0-9]+) +/
384       enc = Regexp.last_match[1]
385       slot = Regexp.last_match[2]
386
387       disks << { :device => device, :driver => "areca", :id => "#{slot}/#{enc}" }
388     elsif line =~ /^ +([0-9]+) +[0-9]+ +/
389       disks << { :device => device, :driver => "areca", :id => Regexp.last_match[1] }
390     end
391   end
392 end
393
394 disks.each do |disk|
395   if disk[:device] =~ %r{^cciss/(.*)$}
396     id = File.read("/sys/bus/cciss/devices/#{Regexp.last_match[1]}/unique_id").chomp
397
398     disk[:munin] = "cciss-3#{id.downcase}"
399   else
400     disk[:munin] = disk[:device]
401   end
402
403   if disk[:id]
404     disk[:munin] = "#{disk[:munin]}-#{disk[:id].to_s.tr('/', ':')}"
405   end
406
407   disk[:hddtemp] = disk[:munin].tr("-:", "_")
408 end
409
410 if disks.count > 0
411   package "smartmontools"
412
413   template "/usr/local/bin/smartd-mailer" do
414     source "smartd-mailer.erb"
415     owner "root"
416     group "root"
417     mode 0755
418   end
419
420   template "/etc/smartd.conf" do
421     source "smartd.conf.erb"
422     owner "root"
423     group "root"
424     mode 0644
425     variables :disks => disks
426     notifies :reload, "service[smartmontools]"
427   end
428
429   template "/etc/default/smartmontools" do
430     source "smartmontools.erb"
431     owner "root"
432     group "root"
433     mode 0644
434     notifies :restart, "service[smartmontools]"
435   end
436
437   service "smartmontools" do
438     action [:enable, :start]
439     supports :status => true, :restart => true, :reload => true
440   end
441
442   # Don't try and do munin monitoring of disks behind
443   # an Areca controller as they only allow one thing to
444   # talk to the controller at a time and smartd will
445   # throw errors if it clashes with munin
446   disks = disks.reject { |disk| disk[:driver] == "areca" }
447
448   disks.each do |disk|
449     munin_plugin "smart_#{disk[:munin]}" do
450       target "smart_"
451       conf "munin.smart.erb"
452       conf_variables :disk => disk
453     end
454   end
455
456   munin_plugin "hddtemp_smartctl" do
457     conf "munin.hddtemp.erb"
458     conf_variables :disks => disks
459   end
460 else
461   service "smartmontools" do
462     action [:stop, :disable]
463   end
464
465   munin_plugin "hddtemp_smartctl" do
466     action :delete
467   end
468 end
469
470 plugins = Dir.glob("/etc/munin/plugins/smart_*").map { |p| File.basename(p) } -
471           disks.map { |d| "smart_#{d[:munin]}" }
472
473 plugins.each do |plugin|
474   munin_plugin plugin do
475     action :delete
476   end
477 end
478
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")
482
483     line
484   end
485
486   file "/etc/mdadm/mdadm.conf" do
487     owner "root"
488     group "root"
489     mode 0644
490     content mdadm_conf
491   end
492
493   service "mdadm" do
494     action :nothing
495     subscribes :restart, "file[/etc/mdadm/mdadm.conf]"
496   end
497 end
498
499 template "/etc/modules" do
500   source "modules.erb"
501   owner "root"
502   group "root"
503   mode 0644
504 end
505
506 if node[:lsb][:release].to_f <= 12.10
507   service "module-init-tools" do
508     provider Chef::Provider::Service::Upstart
509     action :nothing
510     subscribes :start, "template[/etc/modules]"
511   end
512 else
513   service "kmod" do
514     provider Chef::Provider::Service::Upstart
515     action :nothing
516     subscribes :start, "template[/etc/modules]"
517   end
518 end
519
520 if node[:hardware][:watchdog]
521   package "watchdog"
522
523   template "/etc/default/watchdog" do
524     source "watchdog.erb"
525     owner "root"
526     group "root"
527     mode 0644
528     variables :module => node[:hardware][:watchdog]
529   end
530
531   service "watchdog" do
532     action [:enable, :start]
533   end
534 end
535
536 unless Dir.glob("/sys/class/hwmon/hwmon*").empty?
537   package "lm-sensors"
538
539   Dir.glob("/sys/devices/platform/coretemp.*").each do |coretemp|
540     cpu = File.basename(coretemp).sub("coretemp.", "").to_i
541     chip = format("coretemp-isa-%04d", cpu)
542
543     temps = Dir.glob("#{coretemp}/temp*_input").map do |temp|
544       File.basename(temp).sub("temp", "").sub("_input", "").to_i
545     end.sort
546
547     if temps.first == 1
548       node.default[:hardware][:sensors][chip][:temps][:temp1][:label] = "CPU #{cpu}"
549       temps.shift
550     end
551
552     temps.each_with_index do |temp, index|
553       node.default[:hardware][:sensors][chip][:temps]["temp#{temp}"][:label] = "CPU #{cpu} Core #{index}"
554     end
555   end
556
557   execute "/etc/sensors.d/chef.conf" do
558     action :nothing
559     command "/usr/bin/sensors -s"
560     user "root"
561     group "root"
562   end
563
564   template "/etc/sensors.d/chef.conf" do
565     source "sensors.conf.erb"
566     owner "root"
567     group "root"
568     mode 0644
569     notifies :run, "execute[/etc/sensors.d/chef.conf]"
570   end
571 end