zombie_check 0.1.0 → 0.1.1

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4920ff896447270b5f95b172f733995e82a7fc8f
4
- data.tar.gz: 5c7b2c79e3ad4e1ac2b160a4e92abdfe512cadbe
3
+ metadata.gz: da130e4c71bccc19171d3c39de7aaffa673bb31b
4
+ data.tar.gz: 2638fd4b6a1327b88db32ad533bf20f54390cac3
5
5
  SHA512:
6
- metadata.gz: b4429185406db8e6d1f8e0f7916192c9f814b7c82bff399b05fb75415f1e230fa4195f42ac7d18113164cf9d052b42546efeb4e17359e179fb7c2f5d5327f7de
7
- data.tar.gz: 0ca9fb3648d8bc51dd0e91358960046071d2d20cd23d32544692ed8d99bd849ffc5398ee0ecdd94225ad9c096669246defb64928ad7dbc550449f08a4d34cfed
6
+ metadata.gz: fec0d2135d0c1033f7d7c806c0a958004243351ead9935032d2c172e72f6b7b4083d72fb7505449297a214dbeb1ce1c4902526ec9f2e718be4acd09720561564
7
+ data.tar.gz: 020caad88071704c47982ff2cd5a46fdee7bc774d80840f73748afa7a1fe45e9a906e2c92e1c57d49fdf2f28f86c98cf78ea7333779bf3ef2e81bdb428a880f1
data/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ The MIT License
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
data/bin/zombie_check CHANGED
@@ -1,6 +1,5 @@
1
1
  #!/usr/bin/env ruby
2
2
  require 'pathname'
3
- require 'pry'
4
3
 
5
4
  source_path = (Pathname.new(__FILE__).dirname + '../lib').expand_path
6
5
  $LOAD_PATH << source_path
@@ -6,23 +6,19 @@ module ZombieCheck
6
6
 
7
7
  def initialize(options = {})
8
8
  @hosts_file ||= options[:hosts_file] || "hosts.txt"
9
- @delay ||= (options[:delay] || 1000).to_i / 1000.0
9
+ @delay ||= (options[:delay] || 1000).to_i
10
10
  @hosts ||= []
11
11
  @report = CheckerReport.new
12
12
  setup_interruptor
13
13
  end
14
14
 
15
15
  def start
16
- puts "Using file #{hosts_file}, delay #{delay}, for exit press Ctrl+C"
16
+ puts "Using file #{hosts_file}, delay #{delay}ms, for exit press Ctrl+C"
17
17
  loop do
18
18
  update_hosts!
19
19
  @hosts.each { |host| Thread.new { ping host } }
20
- if interrupted
21
- exit_all_threads!
22
- puts @report.generate
23
- exit 0
24
- end
25
- sleep @delay
20
+ interrupted_exit! if interrupted
21
+ sleep @delay / 1000.0
26
22
  end
27
23
  end
28
24
 
@@ -64,6 +60,12 @@ module ZombieCheck
64
60
  def exit_all_threads!
65
61
  Thread.list.reject { |t| t == Thread.current }.each(&:exit)
66
62
  end
63
+
64
+ def interrupted_exit!
65
+ exit_all_threads!
66
+ puts @report.generate
67
+ exit 0
68
+ end
67
69
  end
68
70
  end
69
71
  end
@@ -1,37 +1,24 @@
1
1
  # frozen_string_literal: true
2
+ require 'pry'
2
3
  module ZombieCheck
3
4
  module Ping
4
5
  class CheckerReport
5
- PRECISION = 3
6
- attr_accessor :stat
7
-
8
- def initialize
9
- @stat ||= {}
10
- end
11
6
 
12
7
  def <<(ping)
13
- host = @stat[ping.host] ||= {}
14
- host[:durations] ||= []
15
- host[:lost] ||= 0
16
- if (ping_duration = ping.duration)
17
- host[:durations] << (ping_duration * 1000).round(PRECISION)
18
- else
19
- host[:lost] += 1
20
- end
8
+ HostStat.new(ping).store
21
9
  end
22
10
 
23
11
  def generate
24
12
  [].tap do |result|
25
- stat.each_pair do |host, log|
26
- total = log[:durations].size + log[:lost]
27
- percentage = total > 0 ? log[:lost] / total : 0
28
-
13
+ HostStat.nodes.each_pair do |host, node|
14
+ total = node.durations.size + node.lost
15
+ percentage = total > 0 ? node.lost / total : 0
29
16
  result << <<-REPORT
30
17
 
31
- To #{host} total sent #{total} pings, lost #{log[:lost]} (#{percentage}%). Time(ms):
32
- AVG #{log[:durations].mean.round(PRECISION)} MIN #{log[:durations].min} \
33
- MAX #{log[:durations].max} sigma #{log[:durations].sigma.round(PRECISION)} \
34
- median #{log[:durations].median.round(PRECISION)}
18
+ To #{host} total sent #{total} pings, lost #{node.lost} (#{percentage}%). Time(ms):
19
+ AVG #{node.durations.mean.round(PRECISION)} MIN #{node.durations.min} \
20
+ MAX #{node.durations.max} sigma #{node.durations.sigma.round(PRECISION)} \
21
+ median #{node.durations.median.round(PRECISION)}
35
22
  REPORT
36
23
  end
37
24
  end.join "\n"
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+ module ZombieCheck
3
+ module Ping
4
+ class HostStat
5
+ @nodes = {}
6
+
7
+ class<<self
8
+ attr_accessor :nodes
9
+ end
10
+
11
+ attr_accessor :durations, :lost, :host
12
+
13
+ def initialize(ping)
14
+ @durations ||= []
15
+ @lost ||= 0
16
+ @host = ping.host
17
+ if (ping_duration = ping.duration)
18
+ @durations << (ping_duration * 1000).round(PRECISION)
19
+ else
20
+ @lost += 1
21
+ end
22
+ end
23
+
24
+ def store
25
+ if stored?
26
+ stored.durations += durations
27
+ stored.lost += lost
28
+ else
29
+ self.class.nodes[host] = self
30
+ end
31
+ end
32
+
33
+ def stored?
34
+ stored ? true : false
35
+ end
36
+
37
+ def stored
38
+ self.class.nodes[host]
39
+ end
40
+
41
+ def hash
42
+ host.hash
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+ module ZombieCheck
3
+ module Ping
4
+ PRECISION = 3
5
+ end
6
+ end
@@ -1,4 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
  module ZombieCheck
3
- VERSION = "0.1.0"
3
+ VERSION = "0.1.1"
4
4
  end
data/lib/zombie_check.rb CHANGED
@@ -1,8 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
  require "zombie_check/core_ext/ennumerable"
3
3
  require "zombie_check/version"
4
+ require "zombie_check/ping"
4
5
  require "zombie_check/ping/checker"
5
6
  require "zombie_check/ping/checker_report"
7
+ require "zombie_check/ping/host_stat"
6
8
  require "net/ping"
7
9
 
8
10
  module ZombieCheck
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zombie_check
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kvokka
@@ -80,6 +80,7 @@ files:
80
80
  - ".rubocop_todo.yml"
81
81
  - ".travis.yml"
82
82
  - Gemfile
83
+ - LICENSE
83
84
  - README.md
84
85
  - Rakefile
85
86
  - bin/console
@@ -88,8 +89,10 @@ files:
88
89
  - hosts.txt
89
90
  - lib/zombie_check.rb
90
91
  - lib/zombie_check/core_ext/ennumerable.rb
92
+ - lib/zombie_check/ping.rb
91
93
  - lib/zombie_check/ping/checker.rb
92
94
  - lib/zombie_check/ping/checker_report.rb
95
+ - lib/zombie_check/ping/host_stat.rb
93
96
  - lib/zombie_check/version.rb
94
97
  - zombie_check.gemspec
95
98
  homepage: https://github.com/kvokka/zombie_check