Class: Telemetry::Snmp::DeviceCollector

Inherits:
Object
  • Object
show all
Includes:
Concurrent::Async
Defined in:
lib/telemetry/snmp/device_collector.rb

Instance Method Summary collapse

Constructor Details

#initialize(host) ⇒ DeviceCollector

Returns a new instance of DeviceCollector.



6
7
8
9
10
11
12
# File 'lib/telemetry/snmp/device_collector.rb', line 6

def initialize(host)
  @device = Telemetry::Snmp::Data::Model::Device.where(hostname: host).or(ip_address: host).first
  return if @device.nil?

  @id = @device.values[:id]
  @hostname = @device.values[:hostname]
end

Instance Method Details

#collectObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/telemetry/snmp/device_collector.rb', line 27

def collect
  return false unless lock_device

  @collection_start = Time.now

  @device.update(last_polled: Sequel::CURRENT_TIMESTAMP)
  @device.save
  @lines = []
  @fields = {}

  Telemetry::Snmp::Data::Model::OID.where(:active).each do |oid_row|
    oid_value = oid_value(oid_row.values[:oid])
    oid_value = oid_value.to_i if oid_value.is_a?(NETSNMP::Timetick)

    if oid_value.nil?
      Telemetry::Logger.warn "#{@hostname} nil result for #{oid_row.values[:oid]}"
      next
    end

    unless oid_value.is_a?(Integer) || oid_value.is_a?(Float)
      Telemetry::Logger.error "#{@hostname} nil result for #{oid_row.values[:oid]} class: #{oid_value.class}, #{oid_value}" # rubocop:disable Layout/LineLength
      next
    end

    @fields[oid_row.values[:name]] = "#{oid_value}i"
  rescue StandardError => e
    Telemetry::Logger.error "#{e.class}: #{e.message}"
  end

  @lines.push Telemetry::Metrics::Parser.to_line_protocol(
    measurement: 'palo_alto',
    fields: @fields,
    tags: tags,
    timestamp: (DateTime.now.strftime('%Q').to_i * 1000 * 1000)
  )

  walker = grab_oid_metrics
  unless walker.empty?
    Telemetry::Logger.info "Pushing #{walker.count} lines for #{@hostname} in #{((Time.now - @collection_start) * 1000).round}ms" # rubocop:disable Layout/LineLength
  end
  Telemetry::Snmp::Publisher.push_lines(walker) unless walker.empty?
  Telemetry::Snmp::Publisher.push_lines(@lines) unless @lines.empty?
  unlock_device
rescue StandardError => e
  Telemetry::Logger.exception(e, level: 'error')
  unlock_device
end

#connectionObject



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/telemetry/snmp/device_collector.rb', line 14

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

#device_locked?Boolean

Returns:

  • (Boolean)


141
142
143
# File 'lib/telemetry/snmp/device_collector.rb', line 141

def device_locked?
  Telemetry::Snmp::Data::Model::DeviceLock.where(device_id: @id).count.positive?
end

#device_unlocked?Boolean

Returns:

  • (Boolean)


145
146
147
# File 'lib/telemetry/snmp/device_collector.rb', line 145

def device_unlocked?
  Telemetry::Snmp::Data::Model::DeviceLock.where(device_id: @id).count.zero?
end

#grab_oid_metricsObject



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/telemetry/snmp/device_collector.rb', line 98

def grab_oid_metrics
  @lines = []
  Telemetry::Snmp::Data::Model::OIDWalks.where(:active).each do |row|
    index = {}
    oid_walk(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
    oid_walk(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: @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',
        influxdb_database: 'snmp'
      }

      line = Telemetry::Metrics::Parser.to_line_protocol(
        measurement: "snmp_#{row.values[:measurement_name]}",
        fields: fields,
        tags: tags,
        timestamp: timestamp
      )

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

  @lines
end

#lock_deviceObject



149
150
151
152
153
154
155
156
157
# File 'lib/telemetry/snmp/device_collector.rb', line 149

def lock_device
  Telemetry::Snmp::Data::Model::DeviceLock.insert(
    worker_name: worker_name,
    device_id: @id,
    created: Sequel::CURRENT_TIMESTAMP,
    expires: Sequel::CURRENT_TIMESTAMP
  )
  true
end

#oid_value(oid) ⇒ Object



75
76
77
78
79
# File 'lib/telemetry/snmp/device_collector.rb', line 75

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

#oid_walk(oid) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/telemetry/snmp/device_collector.rb', line 81

def oid_walk(oid)
  results = []
  connection.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

#tagsObject



159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/telemetry/snmp/device_collector.rb', line 159

def tags
  tags = {
    hostname: @device.values[:hostname],
    ip_address: @device.values[:ip_address],
    env: @device.values[:environment],
    dc: @device.values[:datacenter],
    zone: @device.values[:zone],
    influxdb_node_group: 'snmp',
    influxdb_database: 'snmp'
  }
  tags.delete_if { |_k, v| v.nil? }

  tags
end

#unlock_deviceObject



174
175
176
177
178
179
# File 'lib/telemetry/snmp/device_collector.rb', line 174

def unlock_device
  device = Telemetry::Snmp::Data::Model::DeviceLock[device_id: @id]
  return true if device.nil?

  device.delete
end

#worker_nameObject



181
182
183
# File 'lib/telemetry/snmp/device_collector.rb', line 181

def worker_name
  "#{::Socket.gethostname.tr('.', '_')}.#{::Process.pid}.#{Thread.current.object_id}"
end