hotwired 0.0.1 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7dab1b10129f03eea7cac6f1a20c2d1b08505ba75e59b378f85899b8cc81d087
4
- data.tar.gz: 65be9085ccd1af47bb8ed42c76de9a1845c7d4f2f01ad04e549d8fd29fa466a8
3
+ metadata.gz: cdc82fd81f043f0314acf14c6d994496ab6b715242f9805bf3e66d8de698f9c3
4
+ data.tar.gz: 6c6fccd48234012e8bb80fbdb1243046a5bf42e141eb451b796cd01a96342b60
5
5
  SHA512:
6
- metadata.gz: 17ce5565e6b88d24aca47ccaeb550b15492b6b24a4051d2131907ce9cd865901f7fbba99c9909cec5c9069d8e4ea197c89b6fdeb3899840496b9ac4f7b9b4657
7
- data.tar.gz: 7c942ae65c15eb0a28cd4f617f3d7b514a6fe8e302bed9cd46d490a40230f6235ac6119e21a2fae4ea464e2a6504196dae0ec9d1c4a5e9bf52b6b61f66daf4d7
6
+ metadata.gz: 73dab2a97d0e57b3cd505ee7273b6b68f495c380289128229249395351ab00f84cdb96fea34905cfe23b8ca05485799f22497df7f8d3437fc3c440c01c2491c4
7
+ data.tar.gz: b212362721b3d79e84021ce97cb44e1322fed38c894fbb20b550542e2f20140ade701dee1b92694007c2fd0ab4e25a01b42f97a9012206241aa231798e9e05d0
data/.idea/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ # 默认忽略的文件
2
+ /shelf/
3
+ /workspace.xml
4
+ # 基于编辑器的 HTTP 客户端请求
5
+ /httpRequests/
6
+ # Datasource local storage ignored files
7
+ /dataSources/
8
+ /dataSources.local.xml
@@ -0,0 +1,6 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="ProjectDictionaryState">
4
+ <dictionary name="careline" />
5
+ </component>
6
+ </project>
@@ -0,0 +1,79 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <module type="RUBY_MODULE" version="4">
3
+ <component name="ModuleRunConfigurationManager">
4
+ <shared />
5
+ </component>
6
+ <component name="NewModuleRootManager">
7
+ <content url="file://$MODULE_DIR$">
8
+ <sourceFolder url="file://$MODULE_DIR$/features" isTestSource="true" />
9
+ <sourceFolder url="file://$MODULE_DIR$/spec" isTestSource="true" />
10
+ <sourceFolder url="file://$MODULE_DIR$/test" isTestSource="true" />
11
+ </content>
12
+ <orderEntry type="inheritedJdk" />
13
+ <orderEntry type="sourceFolder" forTests="false" />
14
+ <orderEntry type="library" scope="PROVIDED" name="activesupport (v7.0.2.3, RVM: ruby-3.1.0) [gem]" level="application" />
15
+ <orderEntry type="library" scope="PROVIDED" name="ast (v2.4.2, RVM: ruby-3.1.0) [gem]" level="application" />
16
+ <orderEntry type="library" scope="PROVIDED" name="bundler (v2.3.7, RVM: ruby-3.1.0) [gem]" level="application" />
17
+ <orderEntry type="library" scope="PROVIDED" name="concurrent-ruby (v1.1.10, RVM: ruby-3.1.0) [gem]" level="application" />
18
+ <orderEntry type="library" scope="PROVIDED" name="i18n (v1.10.0, RVM: ruby-3.1.0) [gem]" level="application" />
19
+ <orderEntry type="library" scope="PROVIDED" name="minitest (v5.15.0, RVM: ruby-3.1.0) [gem]" level="application" />
20
+ <orderEntry type="library" scope="PROVIDED" name="parallel (v1.22.1, RVM: ruby-3.1.0) [gem]" level="application" />
21
+ <orderEntry type="library" scope="PROVIDED" name="parser (v3.1.2.0, RVM: ruby-3.1.0) [gem]" level="application" />
22
+ <orderEntry type="library" scope="PROVIDED" name="rack (v2.2.3, RVM: ruby-3.1.0) [gem]" level="application" />
23
+ <orderEntry type="library" scope="PROVIDED" name="rainbow (v3.1.1, RVM: ruby-3.1.0) [gem]" level="application" />
24
+ <orderEntry type="library" scope="PROVIDED" name="rake (v13.0.6, RVM: ruby-3.1.0) [gem]" level="application" />
25
+ <orderEntry type="library" scope="PROVIDED" name="regexp_parser (v2.3.0, RVM: ruby-3.1.0) [gem]" level="application" />
26
+ <orderEntry type="library" scope="PROVIDED" name="rexml (v3.2.5, RVM: ruby-3.1.0) [gem]" level="application" />
27
+ <orderEntry type="library" scope="PROVIDED" name="rubocop (v1.27.0, RVM: ruby-3.1.0) [gem]" level="application" />
28
+ <orderEntry type="library" scope="PROVIDED" name="rubocop-ast (v1.17.0, RVM: ruby-3.1.0) [gem]" level="application" />
29
+ <orderEntry type="library" scope="PROVIDED" name="rubocop-minitest (v0.19.1, RVM: ruby-3.1.0) [gem]" level="application" />
30
+ <orderEntry type="library" scope="PROVIDED" name="rubocop-packaging (v0.5.1, RVM: ruby-3.1.0) [gem]" level="application" />
31
+ <orderEntry type="library" scope="PROVIDED" name="rubocop-performance (v1.13.3, RVM: ruby-3.1.0) [gem]" level="application" />
32
+ <orderEntry type="library" scope="PROVIDED" name="rubocop-rails (v2.14.2, RVM: ruby-3.1.0) [gem]" level="application" />
33
+ <orderEntry type="library" scope="PROVIDED" name="ruby-progressbar (v1.11.0, RVM: ruby-3.1.0) [gem]" level="application" />
34
+ <orderEntry type="library" scope="PROVIDED" name="sequel (v5.55.0, RVM: ruby-3.1.0) [gem]" level="application" />
35
+ <orderEntry type="library" scope="PROVIDED" name="slop (v4.9.2, RVM: ruby-3.1.0) [gem]" level="application" />
36
+ <orderEntry type="library" scope="PROVIDED" name="snmp (v1.3.2, RVM: ruby-3.1.0) [gem]" level="application" />
37
+ <orderEntry type="library" scope="PROVIDED" name="sqlite3 (v1.4.2, RVM: ruby-3.1.0) [gem]" level="application" />
38
+ <orderEntry type="library" scope="PROVIDED" name="tzinfo (v2.0.4, RVM: ruby-3.1.0) [gem]" level="application" />
39
+ <orderEntry type="library" scope="PROVIDED" name="unicode-display_width (v2.1.0, RVM: ruby-3.1.0) [gem]" level="application" />
40
+ </component>
41
+ <component name="RakeTasksCache">
42
+ <option name="myRootTask">
43
+ <RakeTaskImpl id="rake">
44
+ <subtasks>
45
+ <RakeTaskImpl description="Build hotwired-0.0.3.gem into the pkg directory" fullCommand="build" id="build" />
46
+ <RakeTaskImpl id="build">
47
+ <subtasks>
48
+ <RakeTaskImpl description="Generate SHA512 checksum if hotwired-0.0.3.gem into the checksums directory" fullCommand="build:checksum" id="checksum" />
49
+ </subtasks>
50
+ </RakeTaskImpl>
51
+ <RakeTaskImpl description="Remove any temporary products" fullCommand="clean" id="clean" />
52
+ <RakeTaskImpl description="Remove any generated files" fullCommand="clobber" id="clobber" />
53
+ <RakeTaskImpl description="Build and install hotwired-0.0.3.gem into system gems" fullCommand="install" id="install" />
54
+ <RakeTaskImpl id="install">
55
+ <subtasks>
56
+ <RakeTaskImpl description="Build and install hotwired-0.0.3.gem into system gems without network access" fullCommand="install:local" id="local" />
57
+ </subtasks>
58
+ </RakeTaskImpl>
59
+ <RakeTaskImpl description="Create tag v0.0.3 and build and push hotwired-0.0.3.gem to rubygems.org" fullCommand="release[remote]" id="release[remote]" />
60
+ <RakeTaskImpl description="Run RuboCop" fullCommand="rubocop" id="rubocop" />
61
+ <RakeTaskImpl id="rubocop">
62
+ <subtasks>
63
+ <RakeTaskImpl description="Auto-correct RuboCop offenses" fullCommand="rubocop:auto_correct" id="auto_correct" />
64
+ </subtasks>
65
+ </RakeTaskImpl>
66
+ <RakeTaskImpl description="" fullCommand="default" id="default" />
67
+ <RakeTaskImpl description="" fullCommand="release" id="release" />
68
+ <RakeTaskImpl id="release">
69
+ <subtasks>
70
+ <RakeTaskImpl description="" fullCommand="release:guard_clean" id="guard_clean" />
71
+ <RakeTaskImpl description="" fullCommand="release:rubygem_push" id="rubygem_push" />
72
+ <RakeTaskImpl description="" fullCommand="release:source_control_push" id="source_control_push" />
73
+ </subtasks>
74
+ </RakeTaskImpl>
75
+ </subtasks>
76
+ </RakeTaskImpl>
77
+ </option>
78
+ </component>
79
+ </module>
data/.idea/misc.xml ADDED
@@ -0,0 +1,7 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="MarkdownSettingsMigration">
4
+ <option name="stateVersion" value="1" />
5
+ </component>
6
+ <component name="ProjectRootManager" version="2" project-jdk-name="RVM: ruby-3.1.0" project-jdk-type="RUBY_SDK" />
7
+ </project>
data/.idea/modules.xml ADDED
@@ -0,0 +1,8 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="ProjectModuleManager">
4
+ <modules>
5
+ <module fileurl="file://$PROJECT_DIR$/.idea/hotwired.iml" filepath="$PROJECT_DIR$/.idea/hotwired.iml" />
6
+ </modules>
7
+ </component>
8
+ </project>
data/.idea/vagrant.xml ADDED
@@ -0,0 +1,7 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="VagrantProjectSettings">
4
+ <option name="instanceFolder" value="" />
5
+ <option name="provider" value="" />
6
+ </component>
7
+ </project>
data/.idea/vcs.xml ADDED
@@ -0,0 +1,6 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="VcsDirectoryMappings">
4
+ <mapping directory="$PROJECT_DIR$" vcs="Git" />
5
+ </component>
6
+ </project>
data/Gemfile.lock ADDED
@@ -0,0 +1,77 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ hotwired (0.0.4)
5
+ sequel (~> 5.0)
6
+ slop (~> 4.0)
7
+ snmp (~> 1.3)
8
+ sqlite3 (~> 1.4)
9
+ strada (>= 0.0.5)
10
+
11
+ GEM
12
+ remote: https://rubygems.org/
13
+ specs:
14
+ activesupport (7.0.2.3)
15
+ concurrent-ruby (~> 1.0, >= 1.0.2)
16
+ i18n (>= 1.6, < 2)
17
+ minitest (>= 5.1)
18
+ tzinfo (~> 2.0)
19
+ ast (2.4.2)
20
+ concurrent-ruby (1.1.10)
21
+ i18n (1.10.0)
22
+ concurrent-ruby (~> 1.0)
23
+ minitest (5.15.0)
24
+ parallel (1.22.1)
25
+ parser (3.1.2.0)
26
+ ast (~> 2.4.1)
27
+ rack (2.2.3)
28
+ rainbow (3.1.1)
29
+ rake (13.0.6)
30
+ regexp_parser (2.3.0)
31
+ rexml (3.2.5)
32
+ rubocop (1.27.0)
33
+ parallel (~> 1.10)
34
+ parser (>= 3.1.0.0)
35
+ rainbow (>= 2.2.2, < 4.0)
36
+ regexp_parser (>= 1.8, < 3.0)
37
+ rexml
38
+ rubocop-ast (>= 1.16.0, < 2.0)
39
+ ruby-progressbar (~> 1.7)
40
+ unicode-display_width (>= 1.4.0, < 3.0)
41
+ rubocop-ast (1.17.0)
42
+ parser (>= 3.1.1.0)
43
+ rubocop-minitest (0.19.1)
44
+ rubocop (>= 0.90, < 2.0)
45
+ rubocop-packaging (0.5.1)
46
+ rubocop (>= 0.89, < 2.0)
47
+ rubocop-performance (1.13.3)
48
+ rubocop (>= 1.7.0, < 2.0)
49
+ rubocop-ast (>= 0.4.0)
50
+ rubocop-rails (2.14.2)
51
+ activesupport (>= 4.2.0)
52
+ rack (>= 1.1)
53
+ rubocop (>= 1.7.0, < 2.0)
54
+ ruby-progressbar (1.11.0)
55
+ sequel (5.55.0)
56
+ slop (4.9.2)
57
+ snmp (1.3.2)
58
+ sqlite3 (1.4.2)
59
+ strada (0.0.5)
60
+ tzinfo (2.0.4)
61
+ concurrent-ruby (~> 1.0)
62
+ unicode-display_width (2.1.0)
63
+
64
+ PLATFORMS
65
+ x86_64-linux
66
+
67
+ DEPENDENCIES
68
+ hotwired!
69
+ rake (~> 13.0)
70
+ rubocop
71
+ rubocop-minitest
72
+ rubocop-packaging
73
+ rubocop-performance
74
+ rubocop-rails
75
+
76
+ BUNDLED WITH
77
+ 2.3.7
data/README.md CHANGED
@@ -41,7 +41,7 @@ Or install it yourself as:
41
41
  # CLI execute sample
42
42
 
43
43
  ```shell
44
- [hotwired@nodes ~/hotwired]% hotwired --help
44
+ careline@careline:~/.config/hotwired -h
45
45
  Usage: hotwired [options] [argument]
46
46
  -d, --debug Debugging on
47
47
  -p, --poll Poll CIDR [argument]
@@ -54,6 +54,28 @@ Usage: hotwired [options] [argument]
54
54
  % hotwired -p 192.0.2.0/28 # poll specific CIDR
55
55
  % hotwired -r core-sw1 # remove specific record
56
56
  % hotwired -o 7 # remore records older than 7 days
57
+
58
+ careline@careline:~/.config/hotwired
59
+ % ll 22-04-17 - 16:42:14
60
+ 总用量 20K
61
+ -rw-r--r-- 1 careline careline 245 4月 17 16:39 config
62
+ -rw-r--r-- 1 careline careline 12K 4月 17 16:42 hotwired.db
63
+ -rw-r--r-- 1 careline careline 67 4月 17 16:09 log
64
+
65
+ careline@careline:~/.config/hotwired
66
+ % rm -rf hotwired.db 22-04-17 - 16:43:10
67
+
68
+ careline@careline:~/.config/hotwired
69
+ % hotwired -d 22-04-17 - 16:43:12
70
+ I, [2022-04-17T16:43:18.909105 #599481] INFO -- : ptr [R1.cisco.com] sysName [RUBY_R1.cisco.com] ip [192.168.8.80]
71
+ I, [2022-04-17T16:43:18.918306 #599481] INFO -- : ptr [R4.cisco.com] sysName [R4.cisco.com] ip [192.168.8.83]
72
+ I, [2022-04-17T16:43:18.925623 #599481] INFO -- : ptr [R10.cisco.com] sysName [R10.cisco.com] ip [192.168.8.89]
73
+ I, [2022-04-17T16:43:18.930507 #599481] INFO -- : ptr [R5.cisco.com] sysName [R5.cisco.com] ip [192.168.8.84]
74
+ I, [2022-04-17T16:43:18.936821 #599481] INFO -- : ptr [R2.cisco.com] sysName [R2.cisco.com] ip [192.168.8.81]
75
+ I, [2022-04-17T16:43:18.942099 #599481] INFO -- : ptr [R8.cisco.com] sysName [R8.cisco.com] ip [192.168.8.87]
76
+ I, [2022-04-17T16:43:18.947214 #599481] INFO -- : ptr [R3.cisco.com] sysName [R3.cisco.com] ip [192.168.8.82]
77
+ I, [2022-04-17T16:43:18.953543 #599481] INFO -- : ptr [R7.cisco.com] sysName [R7.cisco.com] ip [192.168.8.86]
78
+ I, [2022-04-17T16:43:18.969960 #599481] INFO -- : ptr [R9.cisco.com] sysName [R9.cisco.com] ip [192.168.8.88]
57
79
  ```
58
80
 
59
81
  ## Development
@@ -67,9 +89,9 @@ push git commits and the created tag, and push the `.gem` file to [rubygems.org]
67
89
 
68
90
  ## Contributing
69
91
 
70
- Bug reports and pull requests are welcome on GitHub at https://github.com/ciscolive/hotwired. This project is intended
92
+ Bug reports and pull requests are welcome on GitHub at https://github.com/ciscolive/hotwired-ruby. This project is intended
71
93
  to be a safe, welcoming space for collaboration, and contributors are expected to adhere to
72
- the [code of conduct](https://github.com/ciscolive/hotwired/blob/main/CODE_OF_CONDUCT.md).
94
+ the [code of conduct](https://github.com/ciscolive/hotwired-ruby/blob/main/CODE_OF_CONDUCT.md).
73
95
 
74
96
  ## License
75
97
 
@@ -78,4 +100,4 @@ The gem is available as open source under the terms of the [MIT License](https:/
78
100
  ## Code of Conduct
79
101
 
80
102
  Everyone interacting in the Hotwired project's codebases, issue trackers, chat rooms and mailing lists is expected to
81
- follow the [code of conduct](https://github.com/ciscolive/hotwired/blob/main/CODE_OF_CONDUCT.md).
103
+ follow the [code of conduct](https://github.com/ciscolive/hotwired-ruby/blob/main/CODE_OF_CONDUCT.md).
data/bin/hotwired CHANGED
@@ -3,9 +3,8 @@
3
3
 
4
4
  begin
5
5
  require_relative "../lib/hotwired/cli"
6
-
7
6
  Hotwired::CLI.new.run
8
- rescue StoreError => error
9
- warn "#{error}"
7
+ rescue StandardError => e
8
+ warn e.to_s
10
9
  raise if Hotwired::CFG.debug
11
10
  end
data/hotwired.gemspec CHANGED
@@ -10,14 +10,14 @@ Gem::Specification.new do |spec|
10
10
 
11
11
  spec.summary = "Device discovery via snmp polls"
12
12
  spec.description = "Threaded SNMP poll based network discovery. Devices are stored in SQL"
13
- spec.homepage = "https://github.com/ciscolive/hotwired"
13
+ spec.homepage = "https://github.com/ciscolive/hotwired-ruby"
14
14
  spec.licenses = "MIT"
15
15
 
16
16
  # spec.metadata["allowed_push_host"] = "https://rubygems.org"
17
17
 
18
18
  spec.metadata["homepage_uri"] = spec.homepage
19
- spec.metadata["source_code_uri"] = "https://github.com/ciscolive/hotwired"
20
- spec.metadata["changelog_uri"] = "https://github.com/ciscolive/hotwired"
19
+ spec.metadata["source_code_uri"] = "https://github.com/ciscolive/hotwired-ruby"
20
+ spec.metadata["changelog_uri"] = "https://github.com/ciscolive/hotwired-ruby"
21
21
 
22
22
  spec.files = `git ls-files`.split("\n")
23
23
  spec.executables = %w( hotwired )
@@ -26,7 +26,7 @@ Gem::Specification.new do |spec|
26
26
  spec.required_ruby_version = ">= 2.6.0"
27
27
 
28
28
  # 项目依赖
29
- spec.add_runtime_dependency "strada", "~> 0.0.2"
29
+ spec.add_runtime_dependency "strada", ">= 0.0.5"
30
30
  spec.add_runtime_dependency "sequel", "~> 5.0"
31
31
  spec.add_runtime_dependency "slop", "~> 4.0"
32
32
  spec.add_runtime_dependency "snmp", "~> 1.3"
data/lib/hotwired/cli.rb CHANGED
@@ -5,6 +5,8 @@ module Hotwired
5
5
  require "slop"
6
6
  require_relative "../hotwired"
7
7
 
8
+ class HotwiredError < StandardError; end
9
+
8
10
  class NoConfig < HotwiredError; end
9
11
 
10
12
  MAX_DELETE = 1
@@ -10,18 +10,18 @@ module Hotwired
10
10
  end
11
11
 
12
12
  FileUtils.mkdir_p Config::ROOT
13
- CONFIG = Strada.new name: "hotwired", load: "false", key_to_s: true
13
+ CONFIG = Strada.new name: "hotwired", load: "false"
14
14
 
15
15
  CONFIG.default.community = "cisco"
16
16
  CONFIG.default.db = File.join Config::ROOT, "hotwired.db"
17
- CONFIG.default.poll = %w( 192.168.8.0/24 )
17
+ CONFIG.default.poll = %w( 192.168.8.0/24 10.250.8.0/24 )
18
18
  CONFIG.default.ignore = %w( 192.168.8.100 )
19
19
  CONFIG.default.mgmt = %w( lo0.0 loopback0 vlan2 )
20
20
  CONFIG.default.threads = 50
21
21
  CONFIG.default.timeout = 0.25
22
22
  CONFIG.default.retries = 2
23
23
  CONFIG.default.log = File.join Config::ROOT, "log"
24
- CONFIG.default.debug = true
24
+ CONFIG.default.debug = false
25
25
 
26
26
  # 加载初始化配置
27
27
  CONFIG.load
@@ -29,5 +29,6 @@ module Hotwired
29
29
 
30
30
  # 初始化项目日志参数
31
31
  Log.file = CFG.log if CFG.log
32
- Log.level = CFG.debug ? Logger::INFO : Logger::DEBUG
32
+ Log.level = Logger::INFO
33
+ Log.level = Logger::DEBUG if CFG.debug
33
34
  end
data/lib/hotwired/core.rb CHANGED
@@ -21,7 +21,7 @@ module Hotwired
21
21
 
22
22
  # 轮询 host 数据
23
23
  def poll(opts = {})
24
- host = opts.delete :host
24
+ host = opts.delete(:host)
25
25
  raise HotwiredError, "'host' not given" unless host
26
26
  hotwire = new(opts)
27
27
  result = hotwire.poll Resolv.getaddress(host)
@@ -41,7 +41,7 @@ module Hotwired
41
41
  # 类对象外部调用函数入口
42
42
  def run
43
43
  # 解析变量
44
- cidr = @opts.delete :cidr
44
+ cidr = @opts.delete(:cidr)
45
45
  # @output = @opts.delete :output
46
46
 
47
47
  # 设置缺省 logger 输出
@@ -51,13 +51,13 @@ module Hotwired
51
51
  # end
52
52
 
53
53
  # 初始化变量及遍历 CIDR
54
- poll, ignores = resolve_networks cidr
54
+ poll, ignores = resolve_networks(cidr)
55
55
  # 实例化线程和数据库联结
56
56
  @mutex = Mutex.new
57
57
  @db = DB.new
58
58
  threads = []
59
59
  # 线程遇到异常及时终止
60
- Thread.abort_on_exception = true
60
+ Thread.abort_on_exception = false
61
61
 
62
62
  # 遍历待轮询的 IPAddr
63
63
  poll.each do |net|
@@ -67,11 +67,11 @@ module Hotwired
67
67
  # 清除空闲线程
68
68
  while threads.size >= CFG.threads
69
69
  threads.delete_if { |thread| not thread.alive? }
70
- sleep 0.01
70
+ sleep 0.02
71
71
  end
72
72
  # 线程不够则主动添加线程
73
73
  threads << Thread.new do
74
- result = poll ip
74
+ result = poll(ip)
75
75
  @mutex.synchronize { process result } if result
76
76
  end
77
77
  end
@@ -80,11 +80,34 @@ module Hotwired
80
80
  threads.each { |thread| thread.join }
81
81
  end
82
82
 
83
+ # 解析 cidr
84
+ def resolve_networks(cidr)
85
+ # 如未接收外部变量则使用缺省值
86
+ cidr = cidr ? [cidr].flatten : CFG.poll
87
+
88
+ # 从 CIDR 中剔除排除清单
89
+ # 支持数组以及文本形式,数据返回包含2个数组对象的数组
90
+ [cidr, CFG.ignore].map do |nets|
91
+ if nets.respond_to? :each
92
+ nets.map { |net| IPAddr.new net }
93
+ else
94
+ out = []
95
+ File.read(nets).each_line do |net|
96
+ # 模糊的 IP 地址正则表达式
97
+ net = net.match(/^([\d.\/]+)$/)
98
+ out << IPAddr.new(net[1]) if net
99
+ end
100
+ out
101
+ end
102
+ end
103
+ end
104
+
83
105
  # 轮询单个 IP 设备信息
84
106
  def poll(ip)
85
107
  result = nil
86
108
  # 实例化 SNMP 对象,批量获取相关监控数据
87
109
  snmp = SNMP.new(ip.to_s, @community)
110
+ # 如有异常则返回 false
88
111
  oids = snmp.dbget
89
112
 
90
113
  if oids
@@ -95,16 +118,16 @@ module Hotwired
95
118
  result = { oids: oids, ip: ip, int: "n/a" }
96
119
  # 联机查询数据
97
120
  index = snmp.ip2index(ip.to_s)
98
- int = snmp.ifdescr(index)
99
121
  # 逻辑处理
100
- if index
101
- if int
102
- result[:int] = int.downcase
103
- else
122
+ if index.nil?
123
+ Log.debug "no ifIndex for #{ip}"
124
+ else
125
+ int = snmp.ifdescr(index)
126
+ if int.nil?
104
127
  Log.debug "no ifDescr for #{index} at #{ip}"
128
+ else
129
+ result[:int] = int.downcase
105
130
  end
106
- else
107
- Log.debug "no ifIndex for #{ip}"
108
131
  end
109
132
  end
110
133
  # 关闭 SNMP 会话并返回结果
@@ -112,8 +135,17 @@ module Hotwired
112
135
  result
113
136
  end
114
137
 
138
+ # 解析 IP 关联的主机名
139
+ def ip2name(ip)
140
+ Resolv.getname ip rescue ip
141
+ end
142
+
115
143
  # 新增表记录
116
144
  def make_record(opt)
145
+ # 判断是否存在 sysObjectID,不存在则设置为 nil
146
+ sys_object_id = opt[:oids][:sysObjectID].empty? ? nil : opt[:oids][:sysObjectID].join(".")
147
+
148
+ # 返回数据结构
117
149
  {
118
150
  ip: opt[:ip].to_s,
119
151
  ptr: ip2name(opt[:ip].to_s),
@@ -122,7 +154,7 @@ module Hotwired
122
154
  oid_sysName: opt[:oids][:sysName],
123
155
  oid_sysLocation: opt[:oids][:sysLocation],
124
156
  oid_sysDescr: opt[:oids][:sysDescr],
125
- oid_sysObjectID: opt[:oids][:sysObjectID].join("."),
157
+ oid_sysObjectID: sys_object_id
126
158
  }
127
159
  end
128
160
 
@@ -139,8 +171,8 @@ module Hotwired
139
171
  # 查无记录则需要新增
140
172
  if (not old_by_sysname) && (not old_by_ip)
141
173
  # all new device
142
- @output.info "ptr [%s] sysName [%s] ip [%s]" % [record[:ptr], record[:oid_sysName], record[:ip]]
143
- Log.info "#{record[:ip]} added"
174
+ Log.info "ptr [%s] sysName [%s] ip [%s]" % [record[:ptr], record[:oid_sysName], record[:ip]]
175
+ # Log.info "#{record[:ip]} added"
144
176
  @db.add record
145
177
  # 根据 IP 可以查询到数据,但设备名称发生变化
146
178
  elsif (not old_by_sysname) && old_by_ip
@@ -206,32 +238,5 @@ module Hotwired
206
238
  opt[:oids][:sysName].sub!(/-re[1-9]\./, "-re0.")
207
239
  opt
208
240
  end
209
-
210
- # 解析 IP 关联的主机名
211
- def ip2name(ip)
212
- Resolv.getname ip rescue ip
213
- end
214
-
215
- # 解析 cidr
216
- def resolve_networks(cidr)
217
- # 如未接收外部变量则使用缺省值
218
- cidr = cidr ? [cidr].flatten : CFG.poll
219
-
220
- # 从 CIDR 中剔除排除清单
221
- # 支持数组以及文本形式,数据返回包含2个数组对象的数组
222
- [cidr, CFG.ignore].map do |nets|
223
- if nets.respond_to? :each
224
- nets.map { |net| IPAddr.new net }
225
- else
226
- out = []
227
- File.read(nets).each_line do |net|
228
- # 模糊的 IP 地址正则表达式
229
- net = net.match(/^([\d.\/]+)$/)
230
- out << IPAddr.new(net[1]) if net
231
- end
232
- out
233
- end
234
- end
235
- end
236
241
  end
237
242
  end
data/lib/hotwired/snmp.rb CHANGED
@@ -41,7 +41,8 @@ module Hotwired
41
41
  begin
42
42
  @snmp.get(oid).each_varbind { |vb| return vb }
43
43
  rescue ::SNMP::RequestTimeout, Errno::EACCES
44
- false
44
+ Log.debug "::SNMP::RequestTimeout, Errno::EACCES when get(*oid)"
45
+ nil
45
46
  end
46
47
  end
47
48
 
@@ -62,9 +63,11 @@ module Hotwired
62
63
  end
63
64
  end
64
65
  rescue ::SNMP::RequestTimeout, Errno::EACCES
65
- return false
66
+ Log.debug "::SNMP::RequestTimeout, Errno::EACCES when mget"
67
+ return nil
66
68
  rescue InvalidResponse
67
- return false
69
+ Log.debug "InvalidResponse when mget(oids = {})"
70
+ return nil
68
71
  end
69
72
  result
70
73
  end
@@ -100,19 +103,19 @@ module Hotwired
100
103
  oids = mget(route: [OID[:ipCidrRouteIfIndex], ip, "255.255.255.255.0.0.0.0.0"].join("."),
101
104
  new: [OID[:ipAddressIfIndex], UNICAST, IPV4, ip].join("."),
102
105
  old: [OID[:ipAdEntIfIndex], ip].join("."))
103
- return false unless oids
106
+ return nil unless oids
104
107
  # 优先使用 route 命中,如查询不到则进一步尝试使用其他属性
105
108
  index = oids[:route]
106
109
  index = oids[:new] if (not index.class == ::SNMP::Integer) || (index.to_s == "0")
107
110
  index = oids[:old] if (not index.class == ::SNMP::Integer) || (index.to_s == "0")
108
- return false unless index.class == ::SNMP::Integer
111
+ return nil unless index.class == ::SNMP::Integer
109
112
  index.to_s
110
113
  end
111
114
 
112
115
  # 查询接口描述
113
116
  def ifdescr(index)
114
117
  descr = get OID[:ifDescr], index
115
- return false unless descr && (descr.value.class == ::SNMP::OctetString)
118
+ return nil unless descr && (descr.value.class == ::SNMP::OctetString)
116
119
  descr.value.to_s
117
120
  end
118
121
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Hotwired
4
- VERSION = "0.0.1"
4
+ VERSION = "0.0.4"
5
5
  end
metadata CHANGED
@@ -1,29 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hotwired
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - WENWU.YAN
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-04-17 00:00:00.000000000 Z
11
+ date: 2022-04-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: strada
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: 0.0.2
19
+ version: 0.0.5
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - "~>"
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: 0.0.2
26
+ version: 0.0.5
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: sequel
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -90,10 +90,18 @@ extra_rdoc_files: []
90
90
  files:
91
91
  - ".github/workflows/main.yml"
92
92
  - ".gitignore"
93
+ - ".idea/.gitignore"
94
+ - ".idea/dictionaries"
95
+ - ".idea/hotwired.iml"
96
+ - ".idea/misc.xml"
97
+ - ".idea/modules.xml"
98
+ - ".idea/vagrant.xml"
99
+ - ".idea/vcs.xml"
93
100
  - ".rubocop.yml"
94
101
  - CHANGELOG.md
95
102
  - CODE_OF_CONDUCT.md
96
103
  - Gemfile
104
+ - Gemfile.lock
97
105
  - LICENSE.txt
98
106
  - README.md
99
107
  - Rakefile
@@ -112,13 +120,13 @@ files:
112
120
  - lib/hotwired/snmp.rb
113
121
  - lib/hotwired/version.rb
114
122
  - sig/hotwired.rbs
115
- homepage: https://github.com/ciscolive/hotwired
123
+ homepage: https://github.com/ciscolive/hotwired-ruby
116
124
  licenses:
117
125
  - MIT
118
126
  metadata:
119
- homepage_uri: https://github.com/ciscolive/hotwired
120
- source_code_uri: https://github.com/ciscolive/hotwired
121
- changelog_uri: https://github.com/ciscolive/hotwired
127
+ homepage_uri: https://github.com/ciscolive/hotwired-ruby
128
+ source_code_uri: https://github.com/ciscolive/hotwired-ruby
129
+ changelog_uri: https://github.com/ciscolive/hotwired-ruby
122
130
  post_install_message:
123
131
  rdoc_options: []
124
132
  require_paths: