Module: Telemetry::Snmp::Client

Defined in:
lib/telemetry/snmp/client.rb

Class Method Summary collapse

Class Method Details

.connection(host) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/telemetry/snmp/client.rb', line 21

def connection(host)
  return @connections[host.to_sym] if @connections.key? host.to_sym

  dataset = Telemetry::Snmp::Data::Model::Device.where(hostname: host).or(ip_address: host).first

  @connections[host.to_sym] = NETSNMP::Client.new(
    host: dataset.values[:hostname],
    port: dataset.values[:port],
    username: dataset.device_cred.values[:username],
    auth_password: dataset.device_cred.values[:auth_password],
    auth_protocol: dataset.device_cred.values[:auth_protocol].to_sym,
    priv_password: dataset.device_cred.values[:priv_password],
    priv_protocol: dataset.device_cred.values[:priv_protocol].to_sym,
    security_level: dataset.device_cred.values[:security_level].to_sym
  )
end

.grab_oid_metrics(hostname) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/telemetry/snmp/client.rb', line 61

def grab_oid_metrics(hostname)
  device = Telemetry::Snmp::Data::Model::Device.where(hostname: hostname).or(ip_address: hostname).first
  @lines = []
  Telemetry::Snmp::Data::Model::OIDWalks.where(:active).each do |row|
    index = {}
    Telemetry::Snmp::Client.oid_walk(device.values[:hostname], row.values[:oid_index]).each do |hash|
      index[hash[:oid_code].delete_prefix("#{row.values[:oid_index]}.")] = hash[:value].gsub(%r{\\/}, '.')
    end

    timestamp = DateTime.now.strftime('%Q').to_i * 1000 * 1000
    Telemetry::Snmp::Client.oid_walk(device.values[:hostname], row.values[:oid_walk]).each do |walk|
      key = walk[:oid_code].split('.').last
      next if walk[:value].is_a? String
      next if walk[:value].nil?

      fields = {}
      fields[walk[:identifier]] = "#{walk[:value]}i"
      tags = {
        hostname: device.values[:hostname],
        interface: index[key],
        ip_address: device.values[:ip_address],
        zone: device.values[:zone],
        env: device.values[:environment],
        dc: device.values[:datacenter],
        influxdb_node_group: 'snmp'
      }

      line = Telemetry::Metrics::Parser.to_line_protocol(
        measurement: row.values[:measurement],
        fields: fields,
        tags: tags,
        timestamp: timestamp
      )

      @lines.push line
    end
  rescue StandardError => e
    Telemetry::Logger.error "#{e.class}: #{hostname}, #{e.message}"
  end

  @lines
end

.load_mibsObject



10
11
12
13
14
15
16
17
18
19
# File 'lib/telemetry/snmp/client.rb', line 10

def load_mibs
  ENV['MIBDIRS'] = "#{__dir__}/mibs"
  NETSNMP::MIB.load_defaults
  # NETSNMP::MIB.load("#{__dir__}/mibs/PAN-COMMON-MIB.my")
  # NETSNMP::MIB.load("#{__dir__}/mibs/PAN-ENTITY-EXT-MIB.my")
  # NETSNMP::MIB.load("#{__dir__}/mibs/PAN-GLOBAL-REG-MIB.my")
  # NETSNMP::MIB.load("#{__dir__}/mibs/PAN-GLOBAL-TC-MIB.my")
  # NETSNMP::MIB.load("#{__dir__}/mibs/PAN-LC-MIB.my")
  # NETSNMP::MIB.load("#{__dir__}/mibs/PAN-PRODUCT-MIB.my")
end

.oid_value(host, oid) ⇒ Object



38
39
40
41
42
# File 'lib/telemetry/snmp/client.rb', line 38

def oid_value(host, oid)
  connection(host).get(oid: oid)
rescue StandardError
  nil
end

.oid_walk(host, oid) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/telemetry/snmp/client.rb', line 44

def oid_walk(host, oid)
  results = []
  connection(host).walk(oid: oid).each do |oid_code, value|
    hash = { oid_code: oid_code, value: value }
    begin
      ident = NETSNMP::MIB.identifier(oid_code)

      hash[:identifier] = ident.first
    rescue StandardError
      # literally do nothing
    end
    results.push hash
  end

  results
end