require_relative 'alarm/email' require_relative 'alarm/udp2irc' require_relative 'alarm/cfg' require_relative 'mtr' require_relative 'paste' require_relative 'nodes_json' module Ring class SQA class Alarm def set alarm_buffer if @alarm == false @alarm = true msg = compose_message alarm_buffer Log.info msg[:short] @methods.each { |alarm_method| alarm_method.send msg } end end def clear if @alarm == true @alarm = false msg = { short: "#{@hostname}: clearing alarm" } msg[:long] = msg[:short] Log.info msg[:short] @methods.each { |alarm_method| alarm_method.send msg } end end private def initialize database @db = database @methods = [] @methods << Email.new if CFG.email.to? @methods << UDP2IRC.new if CFG.irc.password? @alarm = false @hostname = (Socket.gethostname rescue 'anonymous') end def compose_message alarm_buffer exceeding_nodes = alarm_buffer.exceeding_nodes msg = {short: "#{@hostname}: raising alarm - #{exceeding_nodes.size} new nodes down"} nodes = NodesJSON.new nodes_list = '' exceeding_nodes.sort!.each do |node| json = nodes.get node nodes_list << "- %-30s %14s AS%5s %2s\n" % [json['hostname'], node, json['asn'], json['countrycode']] end mtr_list = '' exceeding_nodes.sample(3).each do |node| json = nodes.get node mtr_list << "%-30s AS%5s (%2s)\n" % [json['hostname'], json['asn'], json['countrycode']] mtr_list << MTR.run(node) mtr_list << "\n" end buffer_list = '' time = alarm_buffer.array.size-1 alarm_buffer.array.each do |ary| buffer_list << "%2s min ago %3s measurements failed" % [time, ary.size/2] type = time.to_i < 3 ? " (raised alarm)\n" : " (baseline)\n" buffer_list << type time -= 1 end msg[:long] = <