]> git.openstreetmap.org Git - chef.git/blob - cookbooks/hardware/recipes/default.rb
Don't do munin monitoring for disks behind an Areca controller
[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"
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"
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       disks << { :device => File.basename(sg) }
336     end
337   end
338 end
339
340 if status_packages["aacraid-status"]
341   Dir.glob("/sys/class/scsi_host/host*") do |host|
342     driver = File.new("#{host}/proc_name").read.chomp
343
344     next unless driver == "aacraid"
345
346     bus = host.sub("/sys/class/scsi_host/host", "")
347
348     Dir.glob("/sys/bus/scsi/devices/#{bus}:1:*/scsi_generic/*").each do |sg|
349       disks << { :device => File.basename(sg) }
350     end
351   end
352 end
353
354 if tools_packages.include?("areca") && File.exist?("/opt/areca/x86_64/cli64")
355   device = IO.popen(["lsscsi", "-g"]).grep(%r{Areca +RAID controller .*/dev/(sg[0-9]+)}) do
356     Regexp.last_match[1]
357   end.first
358
359   IO.popen(["/opt/areca/x86_64/cli64", "disk", "info"]).each do |line|
360     next if line =~ /N\.A\./
361
362     if line =~ /^ +[0-9]+ +0*([0-9]+) +(?:Slot#|SLOT )0*([0-9]+) +/
363       enc = Regexp.last_match[1]
364       slot = Regexp.last_match[2]
365
366       disks << { :device => device, :driver => "areca", :id => "#{slot}/#{enc}" }
367     elsif line =~ /^ +([0-9]+) +[0-9]+ +/
368       disks << { :device => device, :driver => "areca", :id => Regexp.last_match[1] }
369     end
370   end
371 end
372
373 disks.each do |disk|
374   if disk[:device] =~ %r{^cciss/(.*)$}
375     id = File.read("/sys/bus/cciss/devices/#{Regexp.last_match[1]}/unique_id").chomp
376
377     disk[:munin] = "cciss-3#{id.downcase}"
378   else
379     disk[:munin] = disk[:device]
380   end
381
382   if disk[:id]
383     disk[:munin] = "#{disk[:munin]}-#{disk[:id].to_s.tr('/', ':')}"
384   end
385
386   disk[:hddtemp] = disk[:munin].tr("-:", "_")
387 end
388
389 if disks.count > 0
390   package "smartmontools"
391
392   template "/etc/smartd.conf" do
393     source "smartd.conf.erb"
394     owner "root"
395     group "root"
396     mode 0644
397     variables :disks => disks
398     notifies :reload, "service[smartmontools]"
399   end
400
401   template "/etc/default/smartmontools" do
402     source "smartmontools.erb"
403     owner "root"
404     group "root"
405     mode 0644
406     notifies :restart, "service[smartmontools]"
407   end
408
409   service "smartmontools" do
410     action [:enable, :start]
411     supports :status => true, :restart => true, :reload => true
412   end
413
414   # Don't try and do munin monitoring of disks behind
415   # an Areca controller as they only allow one thing to
416   # talk to the controller at a time and smartd will
417   # throw errors if it clashes with munin
418   disks = disks.reject { |disk| disk[:driver] == "areca" }
419
420   disks.each do |disk|
421     munin_plugin "smart_#{disk[:munin]}" do
422       target "smart_"
423       conf "munin.smart.erb"
424       conf_variables :disk => disk
425     end
426   end
427
428   munin_plugin "hddtemp_smartctl" do
429     conf "munin.hddtemp.erb"
430     conf_variables :disks => disks
431   end
432 else
433   service "smartmontools" do
434     action [:stop, :disable]
435   end
436
437   munin_plugin "hddtemp_smartctl" do
438     action :delete
439   end
440 end
441
442 plugins = Dir.glob("/etc/munin/plugins/smart_*").map { |p| File.basename(p) } -
443           disks.map { |d| "smart_#{d[:munin]}" }
444
445 plugins.each do |plugin|
446   munin_plugin plugin do
447     action :delete
448   end
449 end
450
451 if File.exist?("/etc/mdadm/mdadm.conf")
452   mdadm_conf = edit_file "/etc/mdadm/mdadm.conf" do |line|
453     line.gsub!(/^MAILADDR .*$/, "MAILADDR admins@openstreetmap.org")
454
455     line
456   end
457
458   file "/etc/mdadm/mdadm.conf" do
459     owner "root"
460     group "root"
461     mode 0644
462     content mdadm_conf
463   end
464
465   service "mdadm" do
466     action :nothing
467     subscribes :restart, "file[/etc/mdadm/mdadm.conf]"
468   end
469 end
470
471 template "/etc/modules" do
472   source "modules.erb"
473   owner "root"
474   group "root"
475   mode 0644
476 end
477
478 if node[:lsb][:release].to_f <= 12.10
479   service "module-init-tools" do
480     provider Chef::Provider::Service::Upstart
481     action :nothing
482     subscribes :start, "template[/etc/modules]"
483   end
484 else
485   service "kmod" do
486     provider Chef::Provider::Service::Upstart
487     action :nothing
488     subscribes :start, "template[/etc/modules]"
489   end
490 end
491
492 if node[:hardware][:watchdog]
493   package "watchdog"
494
495   template "/etc/default/watchdog" do
496     source "watchdog.erb"
497     owner "root"
498     group "root"
499     mode 0644
500     variables :module => node[:hardware][:watchdog]
501   end
502
503   service "watchdog" do
504     action [:enable, :start]
505   end
506 end
507
508 unless Dir.glob("/sys/class/hwmon/hwmon*").empty?
509   package "lm-sensors"
510
511   execute "/etc/sensors.d/chef.conf" do
512     action :nothing
513     command "/usr/bin/sensors -s"
514     user "root"
515     group "root"
516   end
517
518   template "/etc/sensors.d/chef.conf" do
519     source "sensors.conf.erb"
520     owner "root"
521     group "root"
522     mode 0644
523     notifies :run, "execute[/etc/sensors.d/chef.conf]"
524   end
525 end