Class: Qnotifier::WebService

Inherits:
Object
  • Object
show all
Defined in:
lib/web_service.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeWebService

Returns a new instance of WebService.



11
12
13
# File 'lib/web_service.rb', line 11

def initialize
  @@poll_interval = 300
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



9
10
11
# File 'lib/web_service.rb', line 9

def config
  @config
end

Instance Method Details

#register_server(temp_key) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/web_service.rb', line 131

def register_server(temp_key)
  return if temp_key.empty?
  Qnotifier.log.info "Registering Server"      
  begin 
    puts "Connecting to the QNotifier web service..."        
    url = URI.parse("#{@config["api_endpoint"]}/register_server")
    req = Net::HTTP::Post.new(url.path)
    req.body = {"temp_key" => temp_key, "hostname" => @hostname}.to_json
    req["Content-Type"] = "Qnotifier-#{Qnotifier::VERSION}"
    req["User-Agent"] = "application/json"
    res = Net::HTTP.new(url.host, url.port).start {|http| http.request(req) }
    puts res.body
    case res
    when Net::HTTPSuccess
      Qnotifier.log.debug "Registration Request Response: HTTP #{res.code} #{res.body}"
      return res.body
    else
      Qnotifier.log.warn "Registration Request Response: HTTP #{res.code} #{res.body}"
    end
  rescue Exception => e
    Qnotifier.log.error "Registering Server: Can't connect to #{@config["api_endpoint"]} - #{e.message}"
  end
  return nil
end

#send_alerts(alerts) ⇒ Object



15
16
17
18
19
20
21
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
# File 'lib/web_service.rb', line 15

def send_alerts(alerts)
      
  return if alerts.empty?

  unless @hostname
    unless @config["hostname"]
      @hostname = `hostname`.strip
    else
      @hostname = @config["hostname"]
    end
  end
  
  if $qnotifier_debug
    puts "\nAlerts:"
    puts alerts
    puts "\nNot sending alerts to server due to debug mode."
    return
  end
  
  Qnotifier.log.info "Sending alerts #{alerts}"      

  message = {"api_key" => @config["api_key"], 
             "api_version" => "1.0",
             "agent_version" => Qnotifier::VERSION,
             "hostname" => @hostname,
             "alerts" => alerts}.to_json
  
  begin 
    url = URI.parse("#{@config["api_endpoint"]}/send_alert")
    req = Net::HTTP::Post.new(url.path)
    req.body = message
    req["Content-Type"] = "Qnotifier-#{Qnotifier::VERSION}"
    req["User-Agent"] = "application/json"
    res = Net::HTTP.new(url.host, url.port).start {|http| http.request(req) }
    case res
    when Net::HTTPSuccess
      body = JSON.parse(res.body)
      if body["poll_interval"]
        @@poll_interval = body["poll_interval"].to_i
      end
    else
      Qnotifier.log.warn "Server Response: HTTP #{res.code} #{res.body}"
      if res.code.to_i == 402 
        Qnotifier::Storage.put("server_disabled_pings", Time.now)
        Qnotifier.log.error "Server has asked us to disable pings"
      end
    end
  rescue Exception => e
    Qnotifier.log.error "Sending Alert: Can't connect to #{@config["api_endpoint"]} - #{e.message}"
  end
end

#send_reports_stats(reports, stats, alert_count) ⇒ Object



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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/web_service.rb', line 67

def send_reports_stats(reports, stats, alert_count)
  
  # Only send reports every 300s
  last_run = Qnotifier::Storage.get("last_run")
  if $qnotifier_debug
    puts "\nStats:"
    puts stats
    puts "\nReports:"
    puts reports
    puts "\nNot sending data to server due to debug mode."
    exit
  end
  if last_run and (Time.now - last_run) <= @@poll_interval
    return
  end
  Qnotifier::Storage.put("last_run", Time.now)

  return if reports.empty? and stats.empty?
  Qnotifier.log.debug "Sending Reports and Stats"
        
  unless @hostname
    unless @config["hostname"]
      @hostname = `hostname`.strip
    else
      @hostname = @config["hostname"]
    end
  end
        
  message = {"api_key" => @config["api_key"], 
             "api_version" => "1.0",
             "agent_version" => Qnotifier::VERSION,
             "hostname" => @hostname,
             "stats" => stats,
             "alert_count" => alert_count,
             "reports" => reports}.to_json
        
  begin 
    url = URI.parse("#{@config["api_endpoint"]}/update_server")
    req = Net::HTTP::Post.new(url.path)
    req.body = message
    req["Content-Type"] = "Qnotifier-#{Qnotifier::VERSION}"
    req["User-Agent"] = "application/json"
    res = Net::HTTP.new(url.host, url.port).start {|http| http.request(req) }
    puts res.body
    case res
    when Net::HTTPSuccess
      body = JSON.parse(res.body)
      if body["poll_interval"]
       @@poll_interval = body["poll_interval"].to_i
      end
      Qnotifier.log.debug "Server Response: HTTP #{res.code} #{body[:message]}"
    else
      Qnotifier.log.warn "Server Response: HTTP #{res.code} #{res.body}"
      if res.code.to_i == 402 
       puts "disabling pings"
       Qnotifier::Storage.put("server_disabled_pings", Time.now)
       Qnotifier.log.error "Server has asked us to disable pings"
      end
    end
  rescue Exception => e
   Qnotifier.log.error "Sending Reports/Stats: Can't connect to #{@config["api_endpoint"]} - #{e.message}"
  end
end