zombie_check 0.1.1 → 0.1.3

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: da130e4c71bccc19171d3c39de7aaffa673bb31b
4
- data.tar.gz: 2638fd4b6a1327b88db32ad533bf20f54390cac3
3
+ metadata.gz: 374bf645f5a31e6318793a994e89349b76882741
4
+ data.tar.gz: 3b43fc59457bede9cc2944cf72b6953f3a4edadb
5
5
  SHA512:
6
- metadata.gz: fec0d2135d0c1033f7d7c806c0a958004243351ead9935032d2c172e72f6b7b4083d72fb7505449297a214dbeb1ce1c4902526ec9f2e718be4acd09720561564
7
- data.tar.gz: 020caad88071704c47982ff2cd5a46fdee7bc774d80840f73748afa7a1fe45e9a906e2c92e1c57d49fdf2f28f86c98cf78ea7333779bf3ef2e81bdb428a880f1
6
+ metadata.gz: 93edb259e776a4a114f39f0180fac2a34a330883738fcf43ad90ca6db9104a5c9f5a5caa2d9f4cab5467c9ea7d1f59d9bb0e5a4c71b01332031c555739d35974
7
+ data.tar.gz: b022d2c72a2a18f93baf453a1a1a4450d0ada188abfc9dbe53fae7c2a087c28614f75c66f11a3424b469f69b80d5b6a6fe7a26c7c03bf87809486bb08c626c6c
data/.gitignore CHANGED
@@ -9,3 +9,4 @@
9
9
  /tmp/
10
10
  /tmp/*
11
11
  /.idea/
12
+ *.gem
data/README.md CHANGED
@@ -43,3 +43,16 @@ Password: xxx
43
43
  В данном виде, на бесконечном отрезке времени прога сожрет всю память. Я вкурсе, если это важно- поправлю.
44
44
 
45
45
  Если нужны тесты- приделаю.
46
+
47
+ Ввиду net-ping тесты придется стартовать под `sudo -i`.
48
+
49
+ ## UPDATED
50
+
51
+ Приделал тесты. Прошу учесть, что feature спеки хотят `sudo -i`. Unit тесты sudo не требуют.
52
+
53
+ ## UPDATED 2
54
+
55
+ Тк провера затянулось, а меня давно подмывало избавиться в ТЗ от sudo, приделал адаптер для обычного ping из Unix.
56
+ Старый функционал так же оставил, переключается через опцию `tool:unix_ping` или `tool:net_ping`. Тесты не менял,
57
+ ибо акцептанс живут также, а тестить в юнит тестах тут нечего имхо (хотя, если это противит политике партии,
58
+ то это лечится, без эвтаназии :)
data/bin/zombie_check CHANGED
@@ -11,8 +11,10 @@ if ['-v', '--version'].include? ARGV[0]
11
11
  exit 0
12
12
  elsif ['-h', '--help'].include? ARGV[0]
13
13
  puts 'You can specify options with syntax like option_name:value'
14
- puts 'hosts_file - file with list of all hosts'
15
- puts "delay - delay between pings in ms, default 1000 \n\n"
14
+ puts 'hosts_file - file with list of all hosts'
15
+ puts "delay - delay between pings in ms, default 1000"
16
+ puts "tool:unix_ping - use Unix ping, do not require sudo (default)"
17
+ puts "tool:net_ping - use NET::Ping, require sudo \n\n"
16
18
  exit 0
17
19
  end
18
20
 
data/hosts.txt CHANGED
@@ -3,3 +3,5 @@
3
3
  mail.ru
4
4
  google.ru
5
5
  ya.ru
6
+ 1.1.1.1
7
+ bbc.com
data/lib/zombie_check.rb CHANGED
@@ -1,10 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
  require "zombie_check/core_ext/ennumerable"
3
+ require "zombie_check/core_ext/string"
3
4
  require "zombie_check/version"
4
5
  require "zombie_check/ping"
5
6
  require "zombie_check/ping/checker"
6
7
  require "zombie_check/ping/checker_report"
7
8
  require "zombie_check/ping/host_stat"
9
+ require "zombie_check/ping/ping_sender"
10
+ require "zombie_check/ping/ping_sender/net_ping"
11
+ require "zombie_check/ping/ping_sender/unix_ping"
8
12
  require "net/ping"
9
13
 
10
14
  module ZombieCheck
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+ class String
3
+ def camel_case
4
+ split("_").map(&:capitalize).join
5
+ end
6
+ end
@@ -3,12 +3,14 @@ module ZombieCheck
3
3
  module Ping
4
4
  class Checker
5
5
  attr_accessor :hosts_file, :delay, :hosts, :report, :interrupted
6
+ attr_reader :tool
6
7
 
7
8
  def initialize(options = {})
8
9
  @hosts_file ||= options[:hosts_file] || "hosts.txt"
9
10
  @delay ||= (options[:delay] || 1000).to_i
10
11
  @hosts ||= []
11
- @report = CheckerReport.new
12
+ @report ||= CheckerReport.new
13
+ @tool ||= options[:tool] || "unix_ping"
12
14
  setup_interruptor
13
15
  end
14
16
 
@@ -23,13 +25,16 @@ module ZombieCheck
23
25
  end
24
26
 
25
27
  def ping(host)
26
- icmp = Net::Ping::ICMP.new(host)
27
- icmp.ping
28
- @report << icmp
28
+ response = sender_class.send(:new, host).send
29
+ @report << response
29
30
  end
30
31
 
31
32
  private
32
33
 
34
+ def sender_class
35
+ Object.const_get "ZombieCheck::Ping::#{tool.camel_case}"
36
+ end
37
+
33
38
  def update_hosts!
34
39
  check_file_exists! hosts_file_path
35
40
  result = File.open(hosts_file_path, "r") { |f| f.readlines.map(&:chomp) }
@@ -62,9 +67,9 @@ module ZombieCheck
62
67
  end
63
68
 
64
69
  def interrupted_exit!
65
- exit_all_threads!
66
- puts @report.generate
67
- exit 0
70
+ exit_all_threads!
71
+ puts @report.generate
72
+ exit 0
68
73
  end
69
74
  end
70
75
  end
@@ -1,18 +1,31 @@
1
1
  # frozen_string_literal: true
2
- require 'pry'
3
2
  module ZombieCheck
4
3
  module Ping
5
4
  class CheckerReport
5
+ @nodes = {}
6
+
7
+ class<<self
8
+ attr_accessor :nodes
9
+
10
+ def store(ping)
11
+ if ping.stored?
12
+ nodes[ping.host].durations += ping.durations
13
+ nodes[ping.host].lost += ping.lost
14
+ else
15
+ nodes[ping.host] = ping
16
+ end
17
+ end
18
+ end
6
19
 
7
20
  def <<(ping)
8
- HostStat.new(ping).store
21
+ store(HostStat.new(ping))
9
22
  end
10
23
 
11
24
  def generate
12
25
  [].tap do |result|
13
- HostStat.nodes.each_pair do |host, node|
26
+ nodes.each_pair do |host, node|
14
27
  total = node.durations.size + node.lost
15
- percentage = total > 0 ? node.lost / total : 0
28
+ percentage = total > 0 ? (node.lost.to_f / total * 100).round(PRECISION) : 0
16
29
  result << <<-REPORT
17
30
 
18
31
  To #{host} total sent #{total} pings, lost #{node.lost} (#{percentage}%). Time(ms):
@@ -23,6 +36,16 @@ median #{node.durations.median.round(PRECISION)}
23
36
  end
24
37
  end.join "\n"
25
38
  end
39
+
40
+ private
41
+
42
+ def store(*args)
43
+ self.class.store(*args)
44
+ end
45
+
46
+ def nodes
47
+ self.class.nodes
48
+ end
26
49
  end
27
50
  end
28
51
  end
@@ -2,12 +2,6 @@
2
2
  module ZombieCheck
3
3
  module Ping
4
4
  class HostStat
5
- @nodes = {}
6
-
7
- class<<self
8
- attr_accessor :nodes
9
- end
10
-
11
5
  attr_accessor :durations, :lost, :host
12
6
 
13
7
  def initialize(ping)
@@ -21,25 +15,12 @@ module ZombieCheck
21
15
  end
22
16
  end
23
17
 
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
18
  def stored?
34
19
  stored ? true : false
35
20
  end
36
21
 
37
22
  def stored
38
- self.class.nodes[host]
39
- end
40
-
41
- def hash
42
- host.hash
23
+ CheckerReport.nodes[host]
43
24
  end
44
25
  end
45
26
  end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+ module ZombieCheck
3
+ module Ping
4
+ class PingSender
5
+ attr_accessor :duration, :host
6
+
7
+ def initialize(host)
8
+ @host = host
9
+ @duration = nil
10
+ end
11
+
12
+ def send
13
+ raise "Not implemented"
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+ module ZombieCheck
3
+ module Ping
4
+ class NetPing < PingSender
5
+ def initialize(host)
6
+ super host
7
+ end
8
+
9
+ def send
10
+ icmp = Net::Ping::ICMP.new(host)
11
+ icmp.ping
12
+ self.duration = icmp.duration
13
+ self
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+ module ZombieCheck
3
+ module Ping
4
+ class UnixPing < PingSender
5
+ def initialize(host)
6
+ super host
7
+ end
8
+
9
+ def send
10
+ result = `ping -c 1 -W 5 #{host}`.match(/time=(\d+\.?\d*)/)
11
+ self.duration = result[1].to_f / 1000 if result
12
+ self
13
+ end
14
+ end
15
+ end
16
+ end
@@ -1,4 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
  module ZombieCheck
3
- VERSION = "0.1.1"
3
+ VERSION = "0.1.3"
4
4
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zombie_check
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kvokka
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-05-27 00:00:00.000000000 Z
11
+ date: 2016-06-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -89,10 +89,14 @@ files:
89
89
  - hosts.txt
90
90
  - lib/zombie_check.rb
91
91
  - lib/zombie_check/core_ext/ennumerable.rb
92
+ - lib/zombie_check/core_ext/string.rb
92
93
  - lib/zombie_check/ping.rb
93
94
  - lib/zombie_check/ping/checker.rb
94
95
  - lib/zombie_check/ping/checker_report.rb
95
96
  - lib/zombie_check/ping/host_stat.rb
97
+ - lib/zombie_check/ping/ping_sender.rb
98
+ - lib/zombie_check/ping/ping_sender/net_ping.rb
99
+ - lib/zombie_check/ping/ping_sender/unix_ping.rb
96
100
  - lib/zombie_check/version.rb
97
101
  - zombie_check.gemspec
98
102
  homepage: https://github.com/kvokka/zombie_check