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