HttpPing 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,100 @@
1
+ require 'socket'
2
+ require 'timeout'
3
+
4
+ # The Net module serves as a namespace only.
5
+ #
6
+ module HttpPing
7
+
8
+ # The Ping class serves as an abstract base class for all other Ping class
9
+ # types. You should not instantiate this class directly.
10
+ #
11
+ class Ping
12
+ # The version of the net-ping library.
13
+ VERSION = '1.7.6'
14
+
15
+ # The host to ping. In the case of Ping::HTTP, this is the URI.
16
+ attr_accessor :host
17
+
18
+ # The port to ping. This is set to the echo port (7) by default. The
19
+ # Ping::HTTP class defaults to port 80.
20
+ #
21
+ attr_accessor :port
22
+
23
+ # The maximum time a ping attempt is made.
24
+ attr_accessor :timeout
25
+
26
+ # If a ping fails, this value is set to the error that occurred which
27
+ # caused it to fail.
28
+ #
29
+ attr_reader :exception
30
+
31
+ # This value is set if a ping succeeds, but some other condition arose
32
+ # during the ping attempt which merits warning, e.g a redirect in the
33
+ # case of Ping::HTTP#ping.
34
+ #
35
+ attr_reader :warning
36
+
37
+ # The number of seconds (returned as a Float) that it took to ping
38
+ # the host. This is not a precise value, but rather a good estimate
39
+ # since there is a small amount of internal calculation that is added
40
+ # to the overall time.
41
+ #
42
+ attr_reader :duration
43
+
44
+ # The default constructor for the Net::Ping class. Accepts an optional
45
+ # +host+, +port+ and +timeout+. The port defaults to your echo port, or
46
+ # 7 if that happens to be undefined. The default timeout is 5 seconds.
47
+ #
48
+ # The host, although optional in the constructor, must be specified at
49
+ # some point before the Net::Ping#ping method is called, or else an
50
+ # ArgumentError will be raised.
51
+ #
52
+ # Yields +self+ in block context.
53
+ #
54
+ # This class is not meant to be instantiated directly. It is strictly
55
+ # meant as an interface for subclasses.
56
+ #
57
+ def initialize(host=nil, port=nil, timeout=5)
58
+ @host = host
59
+ @port = port || Socket.getservbyname('echo') || 7
60
+ @timeout = timeout
61
+ @exception = nil
62
+ @warning = nil
63
+ @duration = 0
64
+
65
+ yield self if block_given?
66
+ end
67
+
68
+ # The default interface for the Net::Ping#ping method. Each subclass
69
+ # should call super() before continuing with their own implementation in
70
+ # order to ensure that the @exception and @warning instance variables
71
+ # are reset.
72
+ #
73
+ # If +host+ is nil here, then it will use the host specified in the
74
+ # constructor. If the +host+ is nil and there was no host specified
75
+ # in the constructor then an ArgumentError is raised.
76
+ #--
77
+ # The @duration should be set in the subclass' ping method.
78
+ #
79
+ def ping(host = @host)
80
+ raise ArgumentError, 'no host specified' unless host
81
+ @exception = nil
82
+ @warning = nil
83
+ @duration = nil
84
+ end
85
+
86
+ def ping6(host = @host)
87
+ raise ArgumentError, 'no host specified' unless host
88
+ @exception = nil
89
+ @warning = nil
90
+ @duration = nil
91
+ end
92
+
93
+ def ping?(host = @host)
94
+ !!ping(host)
95
+ end
96
+
97
+ alias pingecho ping
98
+
99
+ end
100
+ end
@@ -0,0 +1,3 @@
1
+ module HttpPing
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,119 @@
1
+
2
+ require File.join(File.dirname(__FILE__), 'ping')
3
+ require 'win32ole'
4
+
5
+ # The Net module serves as a namespace only.
6
+ #
7
+ module HttpPing
8
+
9
+ # The Ping::WMI class encapsulates the Win32_PingStatus WMI class for
10
+ # MS Windows.
11
+ #
12
+ class HttpPing::WMI < Ping
13
+
14
+ PingStatus = Struct.new(
15
+ 'PingStatus',
16
+ :address,
17
+ :buffer_size,
18
+ :no_fragmentation,
19
+ :primary_address_resolution_status,
20
+ :protocol_address,
21
+ :protocol_address_resolved,
22
+ :record_route,
23
+ :reply_inconsistency,
24
+ :reply_size,
25
+ :resolve_address_names,
26
+ :response_time,
27
+ :response_time_to_live,
28
+ :route_record,
29
+ :route_record_resolved,
30
+ :source_route,
31
+ :source_route_type,
32
+ :status_code,
33
+ :timeout,
34
+ :timestamp_record,
35
+ :timestamp_record_address,
36
+ :timestamp_record_address_resolved,
37
+ :timestamp_route,
38
+ :time_to_live,
39
+ :type_of_service
40
+ )
41
+
42
+ # Unlike the ping method for other Ping subclasses, this version returns
43
+ # a PingStatus struct which contains various bits of information about
44
+ # the results of the ping itself, such as response time.
45
+ #
46
+ # In addition, this version allows you to pass certain options that are
47
+ # then passed on to the underlying WQL query. See the MSDN documentation
48
+ # on Win32_PingStatus for details.
49
+ #
50
+ # Examples:
51
+ #
52
+ # # Ping with no options
53
+ # Ping::WMI.ping('www.perl.com')
54
+ #
55
+ # # Ping with options
56
+ # Ping::WMI.ping('www.perl.com', :BufferSize => 64, :NoFragmentation => true)
57
+ #--
58
+ # The PingStatus struct is a wrapper for the Win32_PingStatus WMI class.
59
+ #
60
+ def ping(host = @host, options = {})
61
+ super(host)
62
+
63
+ lhost = Socket.gethostname
64
+
65
+ cs = "winmgmts:{impersonationLevel=impersonate}!//#{lhost}/root/cimv2"
66
+ wmi = WIN32OLE.connect(cs)
67
+
68
+ query = "select * from win32_pingstatus where address = '#{host}'"
69
+
70
+ unless options.empty?
71
+ options.each{ |key, value|
72
+ if value.is_a?(String)
73
+ query << " and #{key} = '#{value}'"
74
+ else
75
+ query << " and #{key} = #{value}"
76
+ end
77
+ }
78
+ end
79
+
80
+ status = Struct::PingStatus.new
81
+
82
+ wmi.execquery(query).each{ |obj|
83
+ status.address = obj.Address
84
+ status.buffer_size = obj.BufferSize
85
+ status.no_fragmentation = obj.NoFragmentation
86
+ status.primary_address_resolution_status = obj.PrimaryAddressResolutionStatus
87
+ status.protocol_address = obj.ProtocolAddress
88
+ status.protocol_address_resolved = obj.ProtocolAddressResolved
89
+ status.record_route = obj.RecordRoute
90
+ status.reply_inconsistency = obj.ReplyInconsistency
91
+ status.reply_size = obj.ReplySize
92
+ status.resolve_address_names = obj.ResolveAddressNames
93
+ status.response_time = obj.ResponseTime
94
+ status.response_time_to_live = obj.ResponseTimeToLive
95
+ status.route_record = obj.RouteRecord
96
+ status.route_record_resolved = obj.RouteRecordResolved
97
+ status.source_route = obj.SourceRoute
98
+ status.source_route_type = obj.SourceRouteType
99
+ status.status_code = obj.StatusCode
100
+ status.timeout = obj.Timeout
101
+ status.timestamp_record = obj.TimeStampRecord
102
+ status.timestamp_record_address = obj.TimeStampRecordAddress
103
+ status.timestamp_record_address_resolved = obj.TimeStampRecordAddressResolved
104
+ status.timestamp_route = obj.TimeStampRoute
105
+ status.time_to_live = obj.TimeToLive
106
+ status.type_of_service = obj.TypeOfService
107
+ }
108
+
109
+ status.freeze # Read-only data
110
+ end
111
+
112
+ # Unlike Net::Ping::WMI#ping, this method returns true or false to
113
+ # indicate whether or not the ping was successful.
114
+ #
115
+ def ping?(host = @host, options = {})
116
+ ping(host, options).status_code == 0
117
+ end
118
+ end
119
+ end
@@ -0,0 +1,25 @@
1
+ require_relative "HttpPing/version"
2
+ require 'rbconfig'
3
+ require_relative 'HttpPing/ping'
4
+ require_relative 'HttpPing/h_ping'
5
+
6
+ module HttpPing
7
+ # By doing a "require 'net/ping'" you are requiring every subclass. If you
8
+ # want to require a specific ping type only, do "require 'net/ping/tcp'",
9
+ # for example.
10
+ #
11
+
12
+
13
+ RbConfig = Config unless Object.const_defined?(:RbConfig)
14
+
15
+ begin
16
+ `busybox`
17
+ RbConfig::CONFIG['busybox'] = true
18
+ rescue Errno::ENOENT
19
+ RbConfig::CONFIG['busybox'] = false
20
+ end
21
+
22
+ if File::ALT_SEPARATOR
23
+ require_relative 'HttpPing/wmi'
24
+ end
25
+ end
metadata ADDED
@@ -0,0 +1,128 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: HttpPing
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - whyarkadas
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-04-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: thor
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: ruby ping gem for only http pings and also returns duration
70
+ email:
71
+ - dortbucukkilo@gmail.com
72
+ executables:
73
+ - http_ping
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - ".idea/.rakeTasks"
79
+ - ".idea/http_ping.iml"
80
+ - ".idea/inspectionProfiles/Project_Default.xml"
81
+ - ".idea/misc.xml"
82
+ - ".idea/modules.xml"
83
+ - ".idea/vcs.xml"
84
+ - ".idea/workspace.xml"
85
+ - ".rspec"
86
+ - ".travis.yml"
87
+ - CODE_OF_CONDUCT.md
88
+ - Gemfile
89
+ - Gemfile.lock
90
+ - LICENSE.txt
91
+ - README.md
92
+ - Rakefile
93
+ - bin/console
94
+ - bin/setup
95
+ - example/example.rb
96
+ - exe/http_ping
97
+ - http_ping.gemspec
98
+ - lib/HttpPing/cli.rb
99
+ - lib/HttpPing/h_ping.rb
100
+ - lib/HttpPing/ping.rb
101
+ - lib/HttpPing/version.rb
102
+ - lib/HttpPing/wmi.rb
103
+ - lib/http_ping.rb
104
+ homepage: https://github.com/whyarkadas/http_ping
105
+ licenses:
106
+ - MIT
107
+ metadata: {}
108
+ post_install_message:
109
+ rdoc_options: []
110
+ require_paths:
111
+ - lib
112
+ required_ruby_version: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ required_rubygems_version: !ruby/object:Gem::Requirement
118
+ requirements:
119
+ - - ">="
120
+ - !ruby/object:Gem::Version
121
+ version: '0'
122
+ requirements: []
123
+ rubyforge_project:
124
+ rubygems_version: 2.6.14
125
+ signing_key:
126
+ specification_version: 4
127
+ summary: ruby ping gem for only http pings
128
+ test_files: []