hotwired 0.0.3 → 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: 1420623f2d8f3637568742b1a9bc3e988b7326b40704a2da228987f130ffc500
4
- data.tar.gz: 7a261a56eccd6451103d071e77ff0fa4a25dda2a07f74ab211ec6fea24aec0bb
3
+ metadata.gz: cdc82fd81f043f0314acf14c6d994496ab6b715242f9805bf3e66d8de698f9c3
4
+ data.tar.gz: 6c6fccd48234012e8bb80fbdb1243046a5bf42e141eb451b796cd01a96342b60
5
5
  SHA512:
6
- metadata.gz: 92bdb2cbbaa8ce871a22265324b64667474135cda7c1bdd6c3b71143813e19692b7c86351f59efa760b35a22c0ccf513c1d9a3d9b24791fcb09c9ae5b89995db
7
- data.tar.gz: 8c1707d6cd69f2335e98ad3aad3010a29f87ee8159adacd3c30b47da1106876a54590ea981ba16b0691368a55d82226a4ec06206dd5ef12aa01616985f250ef0
6
+ metadata.gz: 73dab2a97d0e57b3cd505ee7273b6b68f495c380289128229249395351ab00f84cdb96fea34905cfe23b8ca05485799f22497df7f8d3437fc3c440c01c2491c4
7
+ data.tar.gz: b212362721b3d79e84021ce97cb44e1322fed38c894fbb20b550542e2f20140ade701dee1b92694007c2fd0ab4e25a01b42f97a9012206241aa231798e9e05d0
data/.idea/hotwired.iml CHANGED
@@ -40,7 +40,40 @@
40
40
  </component>
41
41
  <component name="RakeTasksCache">
42
42
  <option name="myRootTask">
43
- <RakeTaskImpl id="rake" />
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>
44
77
  </option>
45
78
  </component>
46
79
  </module>
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- hotwired (0.0.3)
4
+ hotwired (0.0.4)
5
5
  sequel (~> 5.0)
6
6
  slop (~> 4.0)
7
7
  snmp (~> 1.3)
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
 
@@ -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.3"
4
+ VERSION = "0.0.4"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hotwired
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
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