network-monitor 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e9b8eec6e627acd02076903b075f725f921cfa02
4
- data.tar.gz: 354895adda561dec4b12ae759ab823f84fc644ae
3
+ metadata.gz: 8d8bf7bbfb16861e4b3b7dc5f231d35d096a7ac1
4
+ data.tar.gz: 0055de1e28db3278cacc700c02ca8436dd52e9cd
5
5
  SHA512:
6
- metadata.gz: aa723ac94362ea32fe7ad661ddb6b0d72885a27661a3ab921a520dc22222b0cf2f3c86a1a4215d4291a2789d26385d8897b854e84a3160fa1c4370e53a857fd7
7
- data.tar.gz: a912d1f63af9d9b3bbf28bec2c7d6746a7bad5145ddcebd7a38ba14bf0675d5f6cf483e0e93f688470e0453136dc3f16c44745d6c18039443e6a156755159038
6
+ metadata.gz: a515bd98b35c122164f110518f891b6f90985376a443d1ed2d5b3ea41cfe64f2db163844dc92cf9e457ae235bad1fb04c2fb332902eacf753ddf49baadcfbd58
7
+ data.tar.gz: a47c2e85c67966477c1f3e8480fcc39254c4693849aae9f3201f5089c168855a1cd3ce055eef780f7db31dc191866b19b534471f360b9b8628e486d6a29ff24a
@@ -1,6 +1,23 @@
1
1
  #!/usr/bin/env ruby
2
- # monitor.rb - a simple script to monitor SNMP port status and utilization
3
- # Copyright (c) 2008 Samuel Williams. Released under the GNU GPLv3.
2
+ # Copyright, 2014, by Samuel G. D. Williams. <http://www.codeotaku.com>
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ # of this software and associated documentation files (the "Software"), to deal
6
+ # in the Software without restriction, including without limitation the rights
7
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ # copies of the Software, and to permit persons to whom the Software is
9
+ # furnished to do so, subject to the following conditions:
10
+ #
11
+ # The above copyright notice and this permission notice shall be included in
12
+ # all copies or substantial portions of the Software.
13
+ #
14
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20
+ # THE SOFTWARE.
4
21
 
5
22
  require 'trollop'
6
23
 
@@ -1,3 +1,22 @@
1
+ # Copyright, 2014, by Samuel G. D. Williams. <http://www.codeotaku.com>
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ # of this software and associated documentation files (the "Software"), to deal
5
+ # in the Software without restriction, including without limitation the rights
6
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ # copies of the Software, and to permit persons to whom the Software is
8
+ # furnished to do so, subject to the following conditions:
9
+ #
10
+ # The above copyright notice and this permission notice shall be included in
11
+ # all copies or substantial portions of the Software.
12
+ #
13
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ # THE SOFTWARE.
1
20
 
2
21
  require_relative "monitor/interface"
3
22
  require_relative "monitor/schema"
@@ -1,3 +1,22 @@
1
+ # Copyright, 2014, by Samuel G. D. Williams. <http://www.codeotaku.com>
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ # of this software and associated documentation files (the "Software"), to deal
5
+ # in the Software without restriction, including without limitation the rights
6
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ # copies of the Software, and to permit persons to whom the Software is
8
+ # furnished to do so, subject to the following conditions:
9
+ #
10
+ # The above copyright notice and this permission notice shall be included in
11
+ # all copies or substantial portions of the Software.
12
+ #
13
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ # THE SOFTWARE.
1
20
 
2
21
  require 'snmp'
3
22
  require 'active_record'
@@ -7,6 +26,8 @@ require 'rainbow'
7
26
  module Network
8
27
  module Monitor
9
28
  class Interface < ActiveRecord::Base
29
+ Counter32Max = (2**32) - 1
30
+
10
31
  QueryColumns = ["ifIndex", "ifSpeed", "ifInOctets", "ifInErrors", "ifOutOctets", "ifOutErrors"]
11
32
 
12
33
  def self.hosts
@@ -90,7 +111,7 @@ module Network
90
111
  key, name = *v
91
112
 
92
113
  out.print name.rjust(8)
93
- out.print s[key].to_prs.rjust(8)
114
+ out.print sprintf("%0.2f\%", s[key] * 100.0).rjust(8)
94
115
  end
95
116
  out.puts r1.speed_string.rjust(10)
96
117
 
@@ -1,3 +1,22 @@
1
+ # Copyright, 2014, by Samuel G. D. Williams. <http://www.codeotaku.com>
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ # of this software and associated documentation files (the "Software"), to deal
5
+ # in the Software without restriction, including without limitation the rights
6
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ # copies of the Software, and to permit persons to whom the Software is
8
+ # furnished to do so, subject to the following conditions:
9
+ #
10
+ # The above copyright notice and this permission notice shall be included in
11
+ # all copies or substantial portions of the Software.
12
+ #
13
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ # THE SOFTWARE.
1
20
 
2
21
  module Network
3
22
  module Monitor
@@ -1,5 +1,25 @@
1
+ # Copyright, 2014, by Samuel G. D. Williams. <http://www.codeotaku.com>
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ # of this software and associated documentation files (the "Software"), to deal
5
+ # in the Software without restriction, including without limitation the rights
6
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ # copies of the Software, and to permit persons to whom the Software is
8
+ # furnished to do so, subject to the following conditions:
9
+ #
10
+ # The above copyright notice and this permission notice shall be included in
11
+ # all copies or substantial portions of the Software.
12
+ #
13
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ # THE SOFTWARE.
20
+
1
21
  module Network
2
- module Monitor
3
- VERSION = "0.1.0"
4
- end
22
+ module Monitor
23
+ VERSION = "0.2.0"
24
+ end
5
25
  end
@@ -0,0 +1,5 @@
1
+ - '10.0.0.1'
2
+ - '10.0.0.10'
3
+ - '10.0.0.30'
4
+ - '10.0.0.31'
5
+ - '10.0.0.32'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: network-monitor
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Samuel Williams
@@ -95,10 +95,10 @@ files:
95
95
  - bin/network-monitor
96
96
  - lib/network/monitor.rb
97
97
  - lib/network/monitor/interface.rb
98
- - lib/network/monitor/network_statistics.rb
99
98
  - lib/network/monitor/schema.rb
100
99
  - lib/network/monitor/version.rb
101
100
  - network-monitor.gemspec
101
+ - test/hosts.conf
102
102
  homepage: ''
103
103
  licenses:
104
104
  - MIT
@@ -123,4 +123,5 @@ rubygems_version: 2.0.3
123
123
  signing_key:
124
124
  specification_version: 4
125
125
  summary: A tool for monitoring network ports for both throughput and errors.
126
- test_files: []
126
+ test_files:
127
+ - test/hosts.conf
@@ -1,200 +0,0 @@
1
-
2
- require 'rubygems'
3
- gem 'snmp'
4
-
5
- require 'snmp'
6
- require 'active_record'
7
-
8
- Counter32Max = 4294967295
9
-
10
- OPTIONS = {
11
- :PostLog => true,
12
- :Duration => 10,
13
- :DatabaseFile => "ports.db",
14
- :HostsFile => "hosts.conf"
15
- }
16
-
17
- class Float
18
- def to_prs
19
- return sprintf("%0.2f\%", self * 100.0)
20
- end
21
- end
22
-
23
- def bits_to_string(size)
24
- human_size = size
25
- levels = 0
26
-
27
- while human_size >= 1000
28
- human_size /= 1000.0
29
- levels += 1
30
- end
31
-
32
- #maybe localize this?
33
- sprintf("%0.1f%s", human_size, ['', 'K', 'M', 'G', 'T', 'X'].fetch(levels)) + 'b'
34
- end
35
-
36
- ActiveRecord::Base.establish_connection(
37
- :adapter => 'sqlite3',
38
- :database => OPTIONS[:DatabaseFile]
39
- )
40
-
41
- unless File.exists? OPTIONS[:DatabaseFile]
42
- puts "Building new database..."
43
-
44
- ActiveRecord::Schema.define(:version => 1) do
45
- create_table :interfaces do |t|
46
- t.string :host, :null => false
47
- t.integer :ifIndex, :null => false
48
-
49
- t.integer :ifSpeed, :null => false
50
-
51
- t.integer :ifInOctets, :null => false
52
- t.integer :ifOutOctets, :null => false
53
-
54
- t.integer :ifInErrors, :null => false
55
- t.integer :ifOutErrors, :null => false
56
-
57
- t.datetime :created_at
58
- end
59
-
60
- change_table :interfaces do |t|
61
- t.index :host
62
- t.index :ifIndex
63
- # t.index :created_at
64
- end
65
- end
66
- end
67
-
68
- Counters = ["ifInOctets", "ifInErrors", :ifInUtilization, "ifOutOctets", "ifOutErrors", :ifOutUtilization]
69
-
70
- $names = {}
71
-
72
- def fetch_names(hosts)
73
- puts "Fetching names..."
74
- hosts.each do |h|
75
- SNMP::Manager.open(:Host => h, :Version => :SNMPv1) do |manager|
76
- response = manager.get(["sysDescr.0", "sysName.0"])
77
-
78
- name = response.varbind_list[1].value.to_s + ' (' + response.varbind_list[0].value.to_s + ')'
79
-
80
- $names[h] = name
81
- end
82
- end
83
- puts "Done."
84
- end
85
-
86
- class Interface < ActiveRecord::Base
87
- QueryColumns = ["ifIndex", "ifSpeed", "ifInOctets", "ifInErrors", "ifOutOctets", "ifOutErrors"]
88
-
89
- def self.hosts
90
- Interface.all.group("host").collect { |r| r.host }
91
- end
92
-
93
- def self.query(host)
94
- SNMP::Manager.open(:Host => host, :Version => :SNMPv1) do |manager|
95
- manager.walk(QueryColumns) do |row|
96
- next if row[1].value.to_i == 0 # Speed of interface is reported as 0 by some faulty equipment, ignore these ports.
97
-
98
- record = Interface.new(:host => host)
99
-
100
- QueryColumns.each_with_index do |c,i|
101
- record.send("#{c}=", row[i].value.to_i)
102
- end
103
-
104
- record.save!
105
- end
106
- end
107
- end
108
-
109
- def statistics(prev)
110
- dt = (created_at - prev.created_at).to_f
111
- s = {
112
- :startTime => prev.created_at,
113
- :endTime => created_at,
114
- :timePeriod => created_at - prev.created_at,
115
- :ifInOctets => ifInOctets - prev.ifInOctets,
116
- :ifOutOctets => ifOutOctets - prev.ifOutOctets,
117
- :ifInErrors => ifInErrors - prev.ifInErrors,
118
- :ifOutErrors => ifOutErrors - prev.ifOutErrors
119
- }
120
-
121
- # Counter32 may wrap and produce strange results unless we deal with it..
122
- [:ifInOctets, :ifOutOctets, :ifInErrors, :ifOutErrors].each do |c|
123
- if s[c] < 0
124
- s[c] = Counter32Max + s[c]
125
- end
126
- end
127
-
128
- s[:ifInUsage] = (s[:ifInOctets].to_f * 8.0) / (ifSpeed.to_f * dt)
129
- s[:ifOutUsage] = (s[:ifOutOctets].to_f * 8.0) / (ifSpeed.to_f * dt)
130
-
131
- return s
132
- end
133
-
134
- def speed_string
135
- bits_to_string(ifSpeed.to_i)
136
- end
137
-
138
- def previous
139
- Interface.where("host = ? and ifIndex = ? and id < ?", host, ifIndex, id).limit(1).order("created_at desc").first
140
- end
141
-
142
- def self.print_statistics(out, min = 0.01)
143
- unused_count = 0
144
-
145
- error_interfaces = []
146
-
147
- out.puts "Utilization Report @ #{Time.now.to_s}".center(84)
148
- Interface.all.group("host, ifIndex").each do |iface|
149
- r2, r1 = Interface.where("host = ? and ifIndex = ?", iface.host, iface.ifIndex).limit(2).order("created_at desc").to_a
150
-
151
- next if r1 == nil or r2 == nil
152
-
153
- unused = true
154
- name = $names[iface.host] || iface.host
155
-
156
- s = r2.statistics(r1)
157
-
158
- if s[:ifInErrors] > 0 or s[:ifOutErrors] > 0
159
- error_interfaces << [iface, name, s]
160
- unused = false
161
- end
162
-
163
- if s[:ifInUsage] > min or s[:ifOutUsage] > min
164
- out.print "#{name.rjust(20)}: #{iface.ifIndex.to_s.rjust(4)} "
165
-
166
- [[:ifInUsage, "IN%"], [:ifOutUsage, "OUT%"]].each do |v|
167
- key, name = *v
168
-
169
- out.print name.rjust(8)
170
- out.print s[key].to_prs.rjust(8)
171
- end
172
- out.puts r1.speed_string.rjust(10)
173
-
174
- unused = false
175
- end
176
-
177
- unused_count += 1 if unused
178
- end
179
-
180
- if error_interfaces.size > 0
181
- out.puts "Errors Detected".center(84)
182
- error_interfaces.each do |r|
183
- iface, name, s = r
184
-
185
- out.print "#{name.rjust(20)}: #{iface.ifIndex.to_s.rjust(4)} "
186
-
187
- [[:ifInErrors, "IN\#"], [:ifOutErrors, "OUT\#"]].each do |v|
188
- key, name = v
189
-
190
- out.print name.rjust(5)
191
- out.print s[key].to_s.rjust(8)
192
- end
193
-
194
- out.puts
195
- end
196
- end
197
-
198
- out.puts "#{unused_count} ports not displayed.".center(84)
199
- end
200
- end