bloopletech-webstats 0.6.0 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/VERSION.yml CHANGED
@@ -1,4 +1,4 @@
1
1
  ---
2
2
  :major: 0
3
- :minor: 6
3
+ :minor: 7
4
4
  :patch: 0
data/clients/common.rb CHANGED
@@ -5,22 +5,38 @@ class Hash
5
5
  end
6
6
  end
7
7
 
8
- def load_settings(defaults_key, defaults)
8
+ def load_settings(client_key, defaults, message)
9
9
  config_file_path = File.expand_path("~/.webstats_clients")
10
10
 
11
11
  $settings = {}
12
12
 
13
13
  if File.exists?(config_file_path)
14
- $settings = YAML.load(IO.read(config_file_path))
14
+ $settings = YAML.load(IO.read(config_file_path))[client_key]
15
15
  else
16
- $settings[defaults_key] = defaults
16
+ $settings[client_key] = defaults
17
17
 
18
18
  File.open(config_file_path, "w") do |f|
19
19
  YAML.dump($settings, f)
20
20
  end
21
21
 
22
- puts "Please edit ~/.webstats_clients and add some URLs to monitor"
22
+ puts message
23
23
  exit
24
24
  end
25
25
  end
26
+
27
+ def make_request(url, password, failed_proc)
28
+ while(true)
29
+ begin
30
+ Net::HTTP.start(url.host, url.port) { |http|
31
+ http.read_timeout = http.open_timeout = 15
32
+ puts url.request_uri
33
+ req = Net::HTTP::Get.new(url.request_uri)
34
+ req.basic_auth 'webstats', password unless password.nil?
35
+ return JSON.parse(http.request(req).body)
36
+ }
37
+ rescue Exception => e
38
+ return nil unless failed_proc.call(url, password, e)
39
+ end
40
+ end
41
+ end
26
42
 
@@ -1,4 +1,4 @@
1
- if $DEBUG
1
+ if true or $DEBUG
2
2
  Thread.abort_on_exception
3
3
  else
4
4
  exit if fork
@@ -13,26 +13,26 @@ require 'uri'
13
13
  require File.dirname(__FILE__) + '/Growl.rb'
14
14
 
15
15
  require File.dirname(__FILE__) + '/../common'
16
- load_settings('growl_notifier', { 'urls' => [{ 'url' => '', 'password' => nil }] })
16
+ load_settings('growl_notifier', { 'urls' => [{ 'url' => '', 'password' => nil }] }, "Please edit ~/.webstats_clients and add some URLs to monitor")
17
17
 
18
- def make_request(url, password)
19
- req = Net::HTTP::Get.new(url.request_uri)
20
- req.basic_auth 'webstats', password unless password.nil?
21
- JSON.parse(Net::HTTP.new(url.host, url.port).start { |http| http.request(req).body })
22
- end
23
-
24
- urls = $settings[:growl_notifier][:urls]
18
+ urls = $settings[:urls]
25
19
 
26
20
  g = GrowlNotifier.new("Webstats", ['Webstats Notification'], nil, OSX::NSWorkspace.sharedWorkspace().iconForFileType_('unknown'))
27
21
  g.register
28
22
 
23
+ failed_url = lambda do |url, password, exception|
24
+ g.notify "Webstats Notification", "Cannot load Webstats data", "Could not load #{url}#{!password.nil? ? " with password #{password}" : ""}, error was #{exception.message}. Will try again in 60 seconds."
25
+ sleep(60)
26
+ true
27
+ end
28
+
29
29
  urls.each do |url|
30
- url.merge!({ :meta_info => make_request(URI.join(url[:url], "information"), url[:password]), :last_warnings_text => nil, :last_danger_text => nil, :last_time => 0 })
30
+ url.merge!({ :meta_info => make_request(URI.join(url[:url], "information"), url[:password], failed_url), :last_warnings_text => nil, :last_danger_text => nil, :last_time => 0 })
31
31
  end
32
32
 
33
33
  while(true)
34
34
  urls.each do |url|
35
- data = make_request(URI.join(url[:url], "update"), url[:password])
35
+ data = make_request(URI.join(url[:url], "update"), url[:password], failed_url)
36
36
 
37
37
  bad = data.sort { |a, b| b[1]['importance'].to_f <=> a[1]['importance'].to_f }.select { |(k, v)| !v['status'].nil? && v['status'] != '' }
38
38
 
@@ -11,13 +11,19 @@ class DataProviders::UrlMonitor
11
11
  @thread = Thread.new do
12
12
  while(true)
13
13
  @settings[:urls].sort.each do |url|
14
+ @mutex.synchronize { @readings[url] = { :response_time => -1, :works => (@readings.key?(url) && @readings[url][:works] == :failed ? :failed : :waiting) } }
14
15
  duration = -1
15
- works = false
16
+ works = :failed
16
17
  begin
18
+ uri = URI.parse(url)
17
19
  start = Time.now
18
- result = Net::HTTP.get(URI.parse(url))
20
+ Net::HTTP.start(uri.host) { |http|
21
+ http.read_timeout = http.open_timeout = @settings[:danger_response_time_threshold] * 1000
22
+ raise Exception unless http.get(uri.path).code.include?(200, 201, 202, 203, 204, 205, 206, 301, 302, 304)
23
+ }
24
+
19
25
  duration = Time.now - start
20
- works = true
26
+ works = :works
21
27
  rescue Exception => e
22
28
  end
23
29
  @mutex.synchronize { @readings[url] = { :response_time => duration * 1000, :works => works } }
@@ -32,8 +38,8 @@ class DataProviders::UrlMonitor
32
38
  out = {}
33
39
  @mutex.synchronize { out[:urls] = @readings.to_a.sort_by { |e| e[0] } }
34
40
  out[:urls].each do |(url, info)|
35
- out[:status] = 'warning' if !info[:works] or info[:response_time] > @settings[:warning_response_time_threshold] and !out[:status] == 'danger'
36
- out[:status] = 'danger' if !info[:works] or info[:response_time] > @settings[:danger_response_time_threshold]
41
+ out[:status] = 'warning' if (info[:works] == :failed) or info[:response_time] > @settings[:warning_response_time_threshold] and !out[:status] == 'danger'
42
+ out[:status] = 'danger' if (info[:works] == :failed) or info[:response_time] > @settings[:danger_response_time_threshold]
37
43
  end
38
44
  out
39
45
  end
@@ -45,7 +51,7 @@ for(var i = 0; i < data_source['urls'].length; i++)
45
51
  {
46
52
  var ud = data_source['urls'][i][1];
47
53
  temp += "<div class='major_figure'><span class='title'>" + data_source['urls'][i][0] + "</span><span class='figure'>" +
48
- (!ud['works'] ? 'Failed</span>' : ud['response_time'] + "</span><span class='unit'>ms</span>") + "</div>";
54
+ (ud['works'] == 'failed' ? 'Failed</span>' : (ud['works'] == 'waiting' ? 'Waiting</span>' : ud['response_time'] + "</span><span class='unit'>ms</span>")) + "</div>";
49
55
  }
50
56
 
51
57
  sc.innerHTML = temp;
data/webstats.gemspec CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{webstats}
5
- s.version = "0.6.0"
5
+ s.version = "0.7.0"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["Brenton Fletcher"]
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bloopletech-webstats
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brenton Fletcher