pingly 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +52 -0
- data/Rakefile +1 -0
- data/bin/pingly +9 -0
- data/lib/pingly.rb +68 -0
- data/pinger.gemspec +19 -0
- metadata +56 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Robert Jackson
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
# Pingly
|
2
|
+
|
3
|
+
We built this to allow us to quickly know if our Internet connection had failed, but
|
4
|
+
we quickly realized it had many more possibilities. Uses your systems 'ping' command
|
5
|
+
to attempt a ping on the host specified and lets you access the results.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
gem 'Pingly'
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install Pingly
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
Use the Pingly binary (optionally specifying a host to ping) to ping every 5 seconds and log the output forever.
|
24
|
+
|
25
|
+
You can also use this in your code to test if a service is up:
|
26
|
+
|
27
|
+
require 'Pingly'
|
28
|
+
|
29
|
+
# standard initialization
|
30
|
+
p = Pingly.new('google.com')
|
31
|
+
p.ping!
|
32
|
+
|
33
|
+
# quick/shortcut initialization (same as above)
|
34
|
+
p = Pingly.ping!('google.com')
|
35
|
+
|
36
|
+
puts p.response #=> "2013-01-03 15:48:29 - google.com(74.125.139.102) - Sent: 5 Received: 5 Loss: 0.0%"
|
37
|
+
|
38
|
+
One can call any of the following methods on an instance of Pingly to get various details of the ping attempt:
|
39
|
+
|
40
|
+
* packet\_loss - Returns a float representing the packet loss.
|
41
|
+
* ip\_address - Returns a string representing the IP Address that the system resolved the passed in host to.
|
42
|
+
* packets\_sent - Returns the number of packets sent as an integer.
|
43
|
+
* packets\_received - Returns the number of packets received as an integer.
|
44
|
+
* response - Returns a formatted string containing the results if the previous methods.
|
45
|
+
|
46
|
+
## Contributing
|
47
|
+
|
48
|
+
1. Fork it
|
49
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
50
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
51
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
52
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/bin/pingly
ADDED
data/lib/pingly.rb
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
class Pingly
|
2
|
+
VERSION = '0.1.0'
|
3
|
+
|
4
|
+
attr_accessor :host, :timeout, :raw_response
|
5
|
+
|
6
|
+
def self.ping_loop(host, timeout = 5)
|
7
|
+
while true do
|
8
|
+
p = new(host)
|
9
|
+
p.ping!
|
10
|
+
|
11
|
+
if p.packet_loss > 0
|
12
|
+
yield if block_given?
|
13
|
+
end
|
14
|
+
|
15
|
+
puts p.response
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.ping!(host, timeout = 5)
|
20
|
+
new(host,timeout).ping!
|
21
|
+
end
|
22
|
+
|
23
|
+
def initialize(host, timeout = 5)
|
24
|
+
self.host, self.timeout = host, timeout
|
25
|
+
end
|
26
|
+
|
27
|
+
def ping!
|
28
|
+
self.raw_response = `#{build_ping_string}`
|
29
|
+
end
|
30
|
+
|
31
|
+
def packet_loss
|
32
|
+
response_regex(/(\d+\.\d+)\% packet loss$/).to_f
|
33
|
+
end
|
34
|
+
|
35
|
+
def ip_address
|
36
|
+
response_regex(/^PING #{Regexp.escape(host)} \((\d+\.\d+\.\d+\.\d+)\)/)
|
37
|
+
end
|
38
|
+
|
39
|
+
def packets_sent
|
40
|
+
response_regex(/^(\d+) packets transmitted/).to_i
|
41
|
+
end
|
42
|
+
|
43
|
+
def packets_received
|
44
|
+
response_regex(/, (\d+) packets received/).to_i
|
45
|
+
end
|
46
|
+
|
47
|
+
def response
|
48
|
+
"#{Time.now.strftime('%Y-%m-%d %H:%M:%S')} - #{host}(#{ip_address}) - Sent: #{packets_sent} Received: #{packets_received} Loss: #{packet_loss}%"
|
49
|
+
end
|
50
|
+
|
51
|
+
private
|
52
|
+
|
53
|
+
def response_regex(regex)
|
54
|
+
ping! unless raw_response
|
55
|
+
raw_response =~ regex
|
56
|
+
$1
|
57
|
+
end
|
58
|
+
|
59
|
+
def build_ping_string
|
60
|
+
"ping -t #{timeout} -q #{host}"
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
if __FILE__ == $0
|
65
|
+
Pingly.ping_loop('google.com') do
|
66
|
+
`say 'link down'`
|
67
|
+
end
|
68
|
+
end
|
data/pinger.gemspec
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'pingly'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "pingly"
|
8
|
+
gem.version = Pingly::VERSION
|
9
|
+
gem.authors = ["Jon Jackson", "Robert Jackson"]
|
10
|
+
gem.email = ["robertj@promedicalinc.com"]
|
11
|
+
gem.description = %q{Ping a specified host, logs the output, and calls a block on packet loss.}
|
12
|
+
gem.summary = %q{Ping a specified host.}
|
13
|
+
gem.homepage = "https://github.com/rjackson/Pingly"
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
end
|
metadata
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: pingly
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Jon Jackson
|
9
|
+
- Robert Jackson
|
10
|
+
autorequire:
|
11
|
+
bindir: bin
|
12
|
+
cert_chain: []
|
13
|
+
date: 2013-01-03 00:00:00.000000000 Z
|
14
|
+
dependencies: []
|
15
|
+
description: Ping a specified host, logs the output, and calls a block on packet loss.
|
16
|
+
email:
|
17
|
+
- robertj@promedicalinc.com
|
18
|
+
executables:
|
19
|
+
- pingly
|
20
|
+
extensions: []
|
21
|
+
extra_rdoc_files: []
|
22
|
+
files:
|
23
|
+
- .gitignore
|
24
|
+
- Gemfile
|
25
|
+
- LICENSE.txt
|
26
|
+
- README.md
|
27
|
+
- Rakefile
|
28
|
+
- bin/pingly
|
29
|
+
- lib/pingly.rb
|
30
|
+
- pinger.gemspec
|
31
|
+
homepage: https://github.com/rjackson/Pingly
|
32
|
+
licenses: []
|
33
|
+
post_install_message:
|
34
|
+
rdoc_options: []
|
35
|
+
require_paths:
|
36
|
+
- lib
|
37
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - ! '>='
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '0'
|
42
|
+
none: false
|
43
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ! '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
none: false
|
49
|
+
requirements: []
|
50
|
+
rubyforge_project:
|
51
|
+
rubygems_version: 1.8.24
|
52
|
+
signing_key:
|
53
|
+
specification_version: 3
|
54
|
+
summary: Ping a specified host.
|
55
|
+
test_files: []
|
56
|
+
has_rdoc:
|