Top Level Namespace

Defined Under Namespace

Modules: Ruminate

Instance Method Summary collapse

Instance Method Details

#ruminate(arg0, rumx_mount, username, password, host, port, smtp_host, config_params, query, fields, alerts) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/ruminate/plugin.rb', line 22

def ruminate(arg0, rumx_mount, username, password, host, port, smtp_host, config_params, query, fields, alerts)
  if arg0 == 'config'
    puts config_params
    return
  end

  # Args can be of the form Myfolder/Subfolder/Foo?reset=true which will expand to <mount>/attributes.<format>?query0=Myfolder/Subfolder/Foo/attributes?reset=true&query1=etc
  path = rumx_mount + '/attributes.properties?'
  query.split.each_with_index do |query_part, index|
    path += '&' if index > 0
    path += "query_#{index}=#{CGI.escape(query_part)}"
  end

  req = Net::HTTP::Get.new(path)
  req.basic_auth(username, password) if username
  res = Net::HTTP.start(host, port) { |http| http.request(req) }
  if res.kind_of?(Net::HTTPSuccess)
    result_hash = {}
    res.body.split("\n").each do |line|
      if i = line.index('=')
        value = line[(i+1)..-1]
        if value.match(/^\s*[+-]?\d+\s*$/)
          value = value.to_i
        elsif value.match(/^\s*[+-]?((\d+_?)*\d+(\.(\d+_?)*\d+)?|\.(\d+_?)*\d+)(\s*|([eE][+-]?(\d+_?)*\d+)\s*)$/)
          value = value.to_f
        end
        result_hash[line[0,i]] = value
      end
    end
    fields.each_with_index do |field_name, i|
      puts "field#{i}.value #{result_hash[field_name]}"
    end
    alerts.each do |alert|
      filter = String.new(alert[:filter])
      result_hash.each do |field_name, value|
        filter.gsub!(Regexp.new("\\b#{field_name.gsub('.', '\\.')}\\b"), value.inspect)
      end
      status = false
      begin
        if eval(filter)
          filter = String.new(alert[:filter])
          result_hash.each do |field_name, value|
            filter.gsub!(Regexp.new("\\b#{field_name.gsub('.', '\\.')}\\b"), "#{field_name}(#{value})")
          end
          message = <<-EOM
The following filter has been triggered:

#{filter}

Output:
#{res.body}
          EOM
          send_email(smtp_host, alert[:email], "ALERT: #{alert[:title]}", message)
        end
      rescue Exception => e
        message = <<-EOM
The following filter caused an exception #{e.message}:

Original Filter: #{alert[:filter]}
Evaled Filter:   #{filter}

Output:
#{res.body}
        EOM
        send_email(smtp_host, alert[:email], "ALERT EXCEPTION: #{alert[:title]}", message)
        raise
      end
    end
  end
end

#send_email(smtp_host, to, subject, message) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/ruminate/plugin.rb', line 7

def send_email(smtp_host, to, subject, message)
  from = "#{Etc.getlogin}@#{Socket.gethostname}"
  msg = <<-EOM
To: <#{to}>
From: <#{from}>
Subject: #{subject}

#{message}
  EOM

  Net::SMTP.start(smtp_host) do |smtp|
    smtp.send_message msg, from, to
  end
end