Class: Netdisco::SNMP
- Inherits:
-
Object
- Object
- Netdisco::SNMP
- Defined in:
- lib/netdisco/snmp.rb
Defined Under Namespace
Classes: NetdiscoError, NoResponse, VBHash
Instance Method Summary collapse
-
#bulkwalk(root) ⇒ Array(SNMP::VarBind)
遍历 root_oid 监控值 Bulkwalk everything below root oid.
-
#close ⇒ void
Closes the SNMP connection.
-
#get(oid) ⇒ SNMP::VarBind
查询某个 oid 监控值 Gets one oid, return value.
-
#hashwalk(oid) {|VBHash| ... } ⇒ Hash
将遍历的 root 监控值转换为 VBHash 样式 bulkwalks oid and returns hash with oid as key.
-
#initialize(host, community = CFG.snmp.community, timeout = CFG.snmp.timeout, retries = CFG.snmp.retries) ⇒ SNMP
constructor
类对象初始化入口函数.
-
#mget(oids) ⇒ SNMP::VarBindList
批量查询多个 oids 监控值 Get multiple oids, return array of values.
Constructor Details
#initialize(host, community = CFG.snmp.community, timeout = CFG.snmp.timeout, retries = CFG.snmp.retries) ⇒ SNMP
类对象初始化入口函数
12 13 14 15 |
# File 'lib/netdisco/snmp.rb', line 12 def initialize(host, community = CFG.snmp.community, timeout = CFG.snmp.timeout, retries = CFG.snmp.retries) @host = host @snmp = ::SNMP::Manager.new(Host: @host, Community: community, Timeout: timeout, Retries: retries, MibModules: []) end |
Instance Method Details
#bulkwalk(root) ⇒ Array(SNMP::VarBind)
遍历 root_oid 监控值 Bulkwalk everything below root oid
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/netdisco/snmp.rb', line 43 def bulkwalk(root) # 初始化变量 last, oid, results = false, root.dup, [] # 将点分10进制字串转换为数组对象 # root = root.split(".").map { |chr| chr.to_i } # 遍历 root_oid until last vbs = snmp(:get_bulk, 0, CFG.snmp.bulkrows, oid).varbind_list vbs.each do |vb| oid = vb.name.to_str # 解析到的 oid 不匹配 root 跳出循环 (last = true; break) unless oid.match?(/^#{Regexp.quote(root)}/) # (last = true; break) unless oid[0..root.size - 1] == root results << vb end end results end |
#close ⇒ void
This method returns an undefined value.
Closes the SNMP connection
19 20 21 |
# File 'lib/netdisco/snmp.rb', line 19 def close @snmp.close end |
#get(oid) ⇒ SNMP::VarBind
查询某个 oid 监控值 Gets one oid, return value
27 28 29 |
# File 'lib/netdisco/snmp.rb', line 27 def get(oid) mget([oid]).first end |
#hashwalk(oid) {|VBHash| ... } ⇒ Hash
将遍历的 root 监控值转换为 VBHash 样式 bulkwalks oid and returns hash with oid as key
68 69 70 71 72 73 74 75 76 |
# File 'lib/netdisco/snmp.rb', line 68 def hashwalk(oid, &block) hash = VBHash.new bulkwalk(oid).each do |vb| # value, key = block.call(vb) # key ||= vb.oid hash[vb.oid] = vb end hash end |
#mget(oids) ⇒ SNMP::VarBindList
批量查询多个 oids 监控值 Get multiple oids, return array of values
35 36 37 |
# File 'lib/netdisco/snmp.rb', line 35 def mget(oids) snmp :get, oids end |