<commit_before>class Riemann::Babbler::Mdadm < Riemann::Babbler

  def init
    plugin.set_default(:service, 'mdadm')
    plugin.set_default(:interval, 60)
    plugin.states.set_default(:critical, 1)
  end

  def run_plugin
    File.exists? '/proc/mdstat'
  end

  def collect 
    file = File.read('/proc/mdstat').split("\n")
    status = Array.new
    file.each_with_index do |line, index|
      next unless line.include? '_'
      device = file[index-1].split(':')[0].strip
      status << { :service => plugin.service + " #{device}", :metric => 1, :description => "mdadm failed device #{device}: #{get_failed_parts(device)}" }
    end
    status
  end

  def get_failed_parts (device)
    failed_parts = []
    Dir["/sys/block/#{device}/md/dev-*"].each do |p|
      state = File.read("#{p}/state").strip
      next unless state != "in_sync"
      p.gsub!(/.+\/dev-/,"")
      failed_parts << "#{p} (#{state})"
    end
    failed_parts.join(", ")
  end

end<commit_msg>mdadm: rescue from potential exceptions<commit_after>class Riemann::Babbler::Mdadm < Riemann::Babbler

  def init
    plugin.set_default(:service, 'mdadm')
    plugin.set_default(:interval, 60)
    plugin.states.set_default(:critical, 1)
  end

  def run_plugin
    File.exists? '/proc/mdstat'
  end

  def collect 
    file = File.read('/proc/mdstat').split("\n")
    status = Array.new
    file.each_with_index do |line, index|
      next unless line.include? '_'
      device = file[index-1].split(':')[0].strip
      status << { :service => plugin.service + " #{device}", :metric => 1, :description => "mdadm failed device #{device}: #{get_failed_parts(device)}" }
    end
    status
  end

  def get_failed_parts (device)
    begin
      failed_parts = []
      Dir["/sys/block/#{device}/md/dev-*"].each do |p|
        state = File.read("#{p}/state").strip
        next unless state != "in_sync"
        p.gsub!(/.+\/dev-/,"")
        failed_parts << "#{p} (#{state})"
      end
      failed_parts.join(", ")
    rescue
      nil
    end
  end

end
{'fraction_non_alphanumeric': 0.17135802469135802, 'fraction_numerical': 0.005925925925925926, 'mean_word_length': 2.888675623800384, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '957344', 'n_tokens_mistral': 712, 'n_tokens_neox': 673, 'n_words': 143}