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