igp 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +6 -0
- data/README.rdoc +2 -0
- data/Rakefile +1 -5
- data/igp.gemspec +4 -2
- data/lib/igp/base.rb +33 -16
- data/lib/igp/shell.rb +2 -0
- data/lib/igp/version.rb +1 -1
- data/spec/base_spec.rb +14 -7
- data/spec/format_spec.rb +48 -0
- data/spec/spec_helper.rb +13 -0
- metadata +5 -3
data/CHANGELOG
CHANGED
@@ -1,3 +1,9 @@
|
|
1
|
+
Version 0.0.2 Release: 7th May 2011
|
2
|
+
==================================================
|
3
|
+
* properly handle time format in 1.8 and 1.9 ruby
|
4
|
+
the things we forget - 1.8 didn't have strftime milliseconds format option
|
5
|
+
* refactor output formatting to make it more testable
|
6
|
+
|
1
7
|
Version 0.0.1 Release: 7th May 2011
|
2
8
|
==================================================
|
3
9
|
* Initial packaging and release
|
data/README.rdoc
CHANGED
@@ -64,6 +64,8 @@ Ping results are written as a comma-separated record with 4 components:
|
|
64
64
|
* ping duration in seconds (if the test was a success)
|
65
65
|
* error or exception code (if any)
|
66
66
|
|
67
|
+
Examples:
|
68
|
+
|
67
69
|
# a 'good' ping example:
|
68
70
|
2011-05-07T03:34:08.078Z,true,0.006508,
|
69
71
|
# a 'bad' ping example:
|
data/Rakefile
CHANGED
@@ -20,13 +20,9 @@ begin
|
|
20
20
|
gem.homepage = "http://github.com/tardate/igp"
|
21
21
|
gem.license = "MIT"
|
22
22
|
gem.summary = %Q{It goes PING!}
|
23
|
-
gem.description = %Q{
|
23
|
+
gem.description = %Q{It goes PING! .. simple command-line server monitoring with a range of protocols: ICMP, TCP, UDP, HTTP/S, LDAP/S}
|
24
24
|
gem.email = "gallagher.paul@gmail.com"
|
25
25
|
gem.authors = ["Paul Gallagher"]
|
26
|
-
# Include your dependencies below. Runtime dependencies are required when using your gem,
|
27
|
-
# and development dependencies are only needed for development (ie running rake tasks, tests, etc)
|
28
|
-
# gem.add_runtime_dependency 'jabber4r', '> 0.1'
|
29
|
-
# gem.add_development_dependency 'rspec', '> 1.2.3'
|
30
26
|
end
|
31
27
|
Jeweler::RubygemsDotOrgTasks.new
|
32
28
|
rescue LoadError
|
data/igp.gemspec
CHANGED
@@ -5,12 +5,12 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{igp}
|
8
|
-
s.version = "0.0.
|
8
|
+
s.version = "0.0.2"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Paul Gallagher"]
|
12
12
|
s.date = %q{2011-05-07}
|
13
|
-
s.description = %q{
|
13
|
+
s.description = %q{It goes PING! .. simple command-line server monitoring with a range of protocols: ICMP, TCP, UDP, HTTP/S, LDAP/S}
|
14
14
|
s.email = %q{gallagher.paul@gmail.com}
|
15
15
|
s.executables = ["igp"]
|
16
16
|
s.extra_rdoc_files = [
|
@@ -32,6 +32,7 @@ Gem::Specification.new do |s|
|
|
32
32
|
"lib/igp/shell.rb",
|
33
33
|
"lib/igp/version.rb",
|
34
34
|
"spec/base_spec.rb",
|
35
|
+
"spec/format_spec.rb",
|
35
36
|
"spec/shell_spec.rb",
|
36
37
|
"spec/spec_helper.rb"
|
37
38
|
]
|
@@ -42,6 +43,7 @@ Gem::Specification.new do |s|
|
|
42
43
|
s.summary = %q{It goes PING!}
|
43
44
|
s.test_files = [
|
44
45
|
"spec/base_spec.rb",
|
46
|
+
"spec/format_spec.rb",
|
45
47
|
"spec/shell_spec.rb",
|
46
48
|
"spec/spec_helper.rb"
|
47
49
|
]
|
data/lib/igp/base.rb
CHANGED
@@ -1,7 +1,10 @@
|
|
1
|
+
# main class that runs the ping task
|
1
2
|
class Igp::Base
|
2
3
|
|
3
4
|
# holds the parsed options
|
4
5
|
attr_reader :options
|
6
|
+
# the output formatter
|
7
|
+
attr_reader :formatter
|
5
8
|
# the Net::Ping handler for the specific protocol required
|
6
9
|
attr_reader :ping_handler
|
7
10
|
# number of pings to perform (nil => infinite)
|
@@ -18,6 +21,7 @@ class Igp::Base
|
|
18
21
|
# :port => optionally specify the port for host (else default port is assumed)
|
19
22
|
#
|
20
23
|
def initialize(options = {})
|
24
|
+
@formatter = Format.new
|
21
25
|
@options = options
|
22
26
|
@limit = options[:limit]
|
23
27
|
@interval = options[:interval] || 5
|
@@ -37,31 +41,44 @@ class Igp::Base
|
|
37
41
|
|
38
42
|
# main routine to run a complete ping test
|
39
43
|
def run
|
40
|
-
return unless ping_handler
|
41
|
-
header
|
44
|
+
return unless ping_handler && formatter
|
45
|
+
formatter.header(
|
46
|
+
'# It goes PING! .. testing',options[:url],
|
47
|
+
(limit ? "#{limit} times - once" : nil),
|
48
|
+
'every',interval,'seconds'
|
49
|
+
)
|
42
50
|
ping_count=0
|
43
51
|
while (limit.nil? || ping_count < limit) do
|
44
52
|
status = ping_handler.ping?
|
45
|
-
log
|
53
|
+
formatter.log(status,formatter.duration(ping_handler.duration),ping_handler.exception)
|
46
54
|
ping_count += 1
|
47
55
|
sleep interval if (limit.nil? || ping_count < limit)
|
48
56
|
end
|
49
57
|
end
|
50
58
|
|
51
|
-
|
59
|
+
# handle output formating tasks
|
60
|
+
class Format
|
52
61
|
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
62
|
+
# the time format - in 1.8 strftime doesn't understand milliseconds
|
63
|
+
TIME_FORMAT = (RUBY_VERSION =~ /^1\.9/) ? "%Y-%m-%dT%H:%M:%S.%LZ" : "%Y-%m-%dT%H:%M:%SZ"
|
64
|
+
|
65
|
+
# prints the header structure to STDERR
|
66
|
+
def header(*args)
|
67
|
+
$stderr.puts(args.compact.join(' '))
|
68
|
+
end
|
69
|
+
|
70
|
+
# logs ping result to STDOUT
|
71
|
+
# +args+ is an array of values to log
|
72
|
+
def log(*args)
|
73
|
+
$stdout.puts(([Time.now.utc.strftime( TIME_FORMAT )] + args).join(','))
|
74
|
+
$stdout.flush
|
75
|
+
end
|
76
|
+
|
77
|
+
# formats the duration for output. nil duration remains nil
|
78
|
+
def duration(duration)
|
79
|
+
"%.6f" % duration if duration
|
80
|
+
end
|
60
81
|
|
61
|
-
# logs ping result to STDOUT
|
62
|
-
# +args+ is an array of values to log
|
63
|
-
def log(*args)
|
64
|
-
$stdout.puts(([Time.now.utc.strftime( "%Y-%m-%dT%H:%M:%S.%LZ" )] + args).join(','))
|
65
|
-
$stdout.flush
|
66
82
|
end
|
83
|
+
|
67
84
|
end
|
data/lib/igp/shell.rb
CHANGED
data/lib/igp/version.rb
CHANGED
data/spec/base_spec.rb
CHANGED
@@ -4,17 +4,14 @@ require 'getoptions'
|
|
4
4
|
describe Igp::Base do
|
5
5
|
|
6
6
|
describe "base initialization without options" do
|
7
|
-
before do
|
8
|
-
@base = Igp::Base.new
|
9
|
-
end
|
10
7
|
it "should not set ping_handler" do
|
11
|
-
|
8
|
+
subject.ping_handler.should be_nil
|
12
9
|
end
|
13
10
|
it "should not set limit" do
|
14
|
-
|
11
|
+
subject.limit.should be_nil
|
15
12
|
end
|
16
13
|
it "should default interval to 5 sec" do
|
17
|
-
|
14
|
+
subject.interval.should eql(5)
|
18
15
|
end
|
19
16
|
end
|
20
17
|
|
@@ -22,7 +19,7 @@ describe Igp::Base do
|
|
22
19
|
it "should be Net::Ping::External for :icmp" do
|
23
20
|
options = { :type => :icmp, :host => 'localhost', :port => nil}
|
24
21
|
base = Igp::Base.new(options)
|
25
|
-
base.ping_handler.
|
22
|
+
base.ping_handler.should be_a(Net::Ping::External)
|
26
23
|
end
|
27
24
|
it "should be Net::Ping::UDP for :tcp" do
|
28
25
|
options = { :type => :udp, :host => 'localhost', :port => 22}
|
@@ -56,4 +53,14 @@ describe Igp::Base do
|
|
56
53
|
end
|
57
54
|
end
|
58
55
|
|
56
|
+
describe "#run" do
|
57
|
+
before do
|
58
|
+
@good = Igp::Base.new({ :type => :icmp, :host => 'localhost', :port => nil, :limit => 1})
|
59
|
+
end
|
60
|
+
it "should print header and log a ping result" do
|
61
|
+
@good.formatter.should_receive(:header)
|
62
|
+
@good.formatter.should_receive(:log)
|
63
|
+
capture(:stdout,:stderr){ @good.run }
|
64
|
+
end
|
65
|
+
end
|
59
66
|
end
|
data/spec/format_spec.rb
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'getoptions'
|
3
|
+
|
4
|
+
describe Igp::Base::Format do
|
5
|
+
|
6
|
+
describe '#duration' do
|
7
|
+
it "should return nil for nil duration" do
|
8
|
+
subject.duration(nil).should be_nil
|
9
|
+
end
|
10
|
+
it "should return string to 6 decimal places for non-nil duration" do
|
11
|
+
result = subject.duration(1.0)
|
12
|
+
result.should match(/^\d+\.\d{6}$/)
|
13
|
+
result.to_f.should eql(1.0)
|
14
|
+
end
|
15
|
+
it "should handle integer parameter" do
|
16
|
+
result = subject.duration(1)
|
17
|
+
result.should match(/^\d+\.\d{6}$/)
|
18
|
+
result.to_f.should eql(1.0)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
describe '#header' do
|
23
|
+
it "should output a blank line for nil parameters" do
|
24
|
+
result = capture(:stderr){ subject.header(nil) }
|
25
|
+
result.should eql("\n")
|
26
|
+
end
|
27
|
+
it "should convert an arbitrary array of strings and numbers to a space-delimited output to stderr" do
|
28
|
+
result = capture(:stderr){ subject.header("string",1.0,"another string",2.0) }
|
29
|
+
result.should eql("string 1.0 another string 2.0\n")
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
describe '#log' do
|
34
|
+
it "should output time only for nil parameters" do
|
35
|
+
result = capture(:stdout){ subject.log(nil) }
|
36
|
+
result.should match(/^.+Z,$/)
|
37
|
+
end
|
38
|
+
it "should log successful message (boolean,float,nil) to stdout" do
|
39
|
+
result = capture(:stdout){ subject.log(true,1.0,nil) }
|
40
|
+
result.should match(/^.+Z,true,1.0,$/)
|
41
|
+
end
|
42
|
+
it "should log unsuccessful message (boolean,nil,string) to stdout" do
|
43
|
+
result = capture(:stdout){ subject.log(false,nil,"message") }
|
44
|
+
result.should match(/^.+Z,false,,message$/)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1 +1,14 @@
|
|
1
1
|
require 'igp'
|
2
|
+
require 'stringio'
|
3
|
+
|
4
|
+
def capture(*streams)
|
5
|
+
streams.map! { |stream| stream.to_s }
|
6
|
+
begin
|
7
|
+
result = StringIO.new
|
8
|
+
streams.each { |stream| eval "$#{stream} = result" }
|
9
|
+
yield
|
10
|
+
ensure
|
11
|
+
streams.each { |stream| eval("$#{stream} = #{stream.upcase}") }
|
12
|
+
end
|
13
|
+
result.string
|
14
|
+
end
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: igp
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.0.
|
5
|
+
version: 0.0.2
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Paul Gallagher
|
@@ -78,7 +78,7 @@ dependencies:
|
|
78
78
|
type: :development
|
79
79
|
prerelease: false
|
80
80
|
version_requirements: *id006
|
81
|
-
description: "
|
81
|
+
description: "It goes PING! .. simple command-line server monitoring with a range of protocols: ICMP, TCP, UDP, HTTP/S, LDAP/S"
|
82
82
|
email: gallagher.paul@gmail.com
|
83
83
|
executables:
|
84
84
|
- igp
|
@@ -102,6 +102,7 @@ files:
|
|
102
102
|
- lib/igp/shell.rb
|
103
103
|
- lib/igp/version.rb
|
104
104
|
- spec/base_spec.rb
|
105
|
+
- spec/format_spec.rb
|
105
106
|
- spec/shell_spec.rb
|
106
107
|
- spec/spec_helper.rb
|
107
108
|
homepage: http://github.com/tardate/igp
|
@@ -117,7 +118,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
117
118
|
requirements:
|
118
119
|
- - ">="
|
119
120
|
- !ruby/object:Gem::Version
|
120
|
-
hash:
|
121
|
+
hash: -2613847138706563759
|
121
122
|
segments:
|
122
123
|
- 0
|
123
124
|
version: "0"
|
@@ -136,5 +137,6 @@ specification_version: 3
|
|
136
137
|
summary: It goes PING!
|
137
138
|
test_files:
|
138
139
|
- spec/base_spec.rb
|
140
|
+
- spec/format_spec.rb
|
139
141
|
- spec/shell_spec.rb
|
140
142
|
- spec/spec_helper.rb
|