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 = "The following filter has been triggered:\n\n\#{filter}\n\nOutput:\n\#{res.body}\n          EOM\n          send_email(smtp_host, alert[:email], \"ALERT: \#{alert[:title]}\", message)\n        end\n      rescue Exception => e\n        message = <<-EOM\nThe following filter caused an exception \#{e.message}:\n\nOriginal Filter: \#{alert[:filter]}\nEvaled Filter:   \#{filter}\n\nOutput:\n\#{res.body}\n        EOM\n        send_email(smtp_host, alert[:email], \"ALERT EXCEPTION: \#{alert[:title]}\", message)\n        raise\n      end\n    end\n  end\nend\n"

#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 = "To: <\#{to}>\nFrom: <\#{from}>\nSubject: \#{subject}\n\n\#{message}\n  EOM\n\n  Net::SMTP.start(smtp_host) do |smtp|\n    smtp.send_message msg, from, to\n  end\nend\n"