trafficmonitor 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 653f38a5bc552e9876d22517eb4ef8f1a48dd1a5
4
+ data.tar.gz: 97c60091f557c374a4532962660fe0c97b798eb4
5
+ SHA512:
6
+ metadata.gz: 1d658610008fbd37e93a5f63187532879201307c48f8ce36e2c2556d67af2f59949deb879eda3dda7ca02c56d79ae12c31e6c2bc488c708f22ac6bba09d4e428
7
+ data.tar.gz: 565d004ff6691b89bf046994a1ff5567517e2b4a5ba4754b0be4faf035fa3b3eb221823965b246c050c3926d2232d608961b311bbc4f61beb968ed6200861740
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in traffic-monitor.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Shown Tien
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+
data/README.md ADDED
@@ -0,0 +1,35 @@
1
+ # Traffic Monitor
2
+
3
+ gem help you analysis traffic per ip with port
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'trafficmonitor'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install trafficmonitor
20
+
21
+ ## Usage
22
+
23
+ ```ruby
24
+ require 'trafficmonitor'
25
+ puts Trafficmonitor::VERSION
26
+ puts Trafficmonitor::Iftop.connections 'eth0'
27
+ ```
28
+
29
+ ## Contributing
30
+
31
+ 1. Fork it ( https://github.com/[my-github-username]/traffic-monitor/fork )
32
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
33
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
34
+ 4. Push to the branch (`git push origin my-new-feature`)
35
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,5 @@
1
+ require "trafficmonitor/version"
2
+
3
+ module Trafficmonitor
4
+ autoload :Iftop, 'trafficmonitor/iftop'
5
+ end
@@ -0,0 +1,53 @@
1
+ module Trafficmonitor
2
+ module Iftop
3
+ require 'open3'
4
+
5
+ def connections(interface, s = 2)
6
+ command = 'sudo iftop -BPp -Nn -t -L 100 -s ' + s.to_s + ' -i ' + interface
7
+
8
+ stdin, stdout, stderr = Open3.popen3(command)
9
+ result = stdout.readlines
10
+ lines = result[3..-10]
11
+
12
+ conns = {"connection" => []}
13
+
14
+ lines.each_slice(2) do |i|
15
+ download = i[0].split(' ')
16
+ upload = i[1].split(' ')
17
+ remote_ip, download_speed = download[1], remove_speed_unit(download[3])
18
+ local_ip, upload_speed = upload[0], remove_speed_unit(upload[2])
19
+
20
+ conns['connection'] << {"local_ip" => local_ip,
21
+ "download_speed" => download_speed,
22
+ "remote_ip" => remote_ip,
23
+ "upload_speed" => upload_speed}
24
+ end
25
+
26
+ total_send_rate_2s = result[-8].split(' ')[3]
27
+ total_receive_rate_2s = result[-7].split(' ')[3]
28
+
29
+ conns['total_send_rate'] = total_send_rate_2s
30
+ conns['total_receive_rate'] = total_receive_rate_2s
31
+
32
+ return conns
33
+ end
34
+
35
+ private
36
+ def remove_speed_unit(string = "")
37
+ return "" if string.empty?
38
+ speed = string.delete 'a-zA-Z'
39
+ unit = string.delete speed
40
+ case unit
41
+ when "B"
42
+ multiple = 1
43
+ when "KB"
44
+ multiple = 1024
45
+ when "MB"
46
+ multiple = 1048576
47
+ when "GB"
48
+ multiple = 1073741824
49
+ end
50
+ return speed.to_f * multiple
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,3 @@
1
+ module Trafficmonitor
2
+ VERSION = "0.0.2"
3
+ end
data/spec/debug.rb ADDED
@@ -0,0 +1,16 @@
1
+ require '../lib/trafficmonitor/iftop.rb'
2
+
3
+ class Test
4
+ include Trafficmonitor::Iftop
5
+
6
+ def show_connections
7
+ connections 'en0', 3
8
+ end
9
+ end
10
+
11
+ me = Test.new
12
+
13
+ result = me.connections 'en0', 3
14
+ result['connection'].each do |i|
15
+ puts i['local_ip'] + "\t\t\t" + i['download_speed'].to_s
16
+ end
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'trafficmonitor/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "trafficmonitor"
8
+ spec.version = Trafficmonitor::VERSION
9
+ spec.authors = ["Shown Tien"]
10
+ spec.email = ["hightian@gmail.com"]
11
+ spec.summary = %q{iftop ouput parse}
12
+ spec.description = %q{iftop ouput parse, and output a hash.}
13
+ spec.homepage = "https://github.com/wenxer/trafficmonitor"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.7"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ end
metadata ADDED
@@ -0,0 +1,83 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: trafficmonitor
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Shown Tien
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-03-16 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.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
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
+ description: iftop ouput parse, and output a hash.
42
+ email:
43
+ - hightian@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - Gemfile
50
+ - LICENSE
51
+ - README.md
52
+ - Rakefile
53
+ - lib/trafficmonitor.rb
54
+ - lib/trafficmonitor/iftop.rb
55
+ - lib/trafficmonitor/version.rb
56
+ - spec/debug.rb
57
+ - trafficmonitor.gemspec
58
+ homepage: https://github.com/wenxer/trafficmonitor
59
+ licenses:
60
+ - MIT
61
+ metadata: {}
62
+ post_install_message:
63
+ rdoc_options: []
64
+ require_paths:
65
+ - lib
66
+ required_ruby_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ required_rubygems_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ requirements: []
77
+ rubyforge_project:
78
+ rubygems_version: 2.4.4
79
+ signing_key:
80
+ specification_version: 4
81
+ summary: iftop ouput parse
82
+ test_files:
83
+ - spec/debug.rb