telemetry-snmp 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 232417e3a89935c883b3a322f8f46a583b13bc0ddc8fb4fbf6f5d02ec393ea9f
4
- data.tar.gz: a1c8bf04d00e4a1f4208267fbb30fb80f826aade2388e76174b600699a85fbeb
3
+ metadata.gz: 1b1cf78bbce21aa906630c1e4430ed67805d3fa1332664c0048976ea12c92746
4
+ data.tar.gz: 0365001cb5dd2dd1767c76234e485b18e0f4c5727743489459b69e1832a478bc
5
5
  SHA512:
6
- metadata.gz: 0a829cdb683ac32a96c61e1c1e6c6daf9b407363c700d73304ca196871bd4b2555738650b7a0e3beb7bf76eeb380f917a53983d7d4bc1f3b4ff9cd6ae770327a
7
- data.tar.gz: 64a6780b4798026a96d0ccaf212616b7eb194161d5e4eaf704c83e0726dd77c3b0509f69427fffeeacca5e51d2a66e85288cbda83d487538849579c65ae1e1b1
6
+ metadata.gz: 7dbd63ecd438e885bc22080aed5cf5044a7344671600134d93d77d07c89d6a2065b3d1558cfd1b30e84b44e8a718d581ad08d6436ed4af852c16ec43f14715d3
7
+ data.tar.gz: f3377fc61d505f6b165f8d85709d2ea7471933f4d3c83ecb2f50d4360c2e992dbc547114d722f344b65a72b1f94db1f633ba445df3bd82780bd9563b6d11b0cd
data/.rubocop.yml CHANGED
@@ -9,7 +9,7 @@ Metrics/ClassLength:
9
9
  Metrics/BlockLength:
10
10
  Max: 100
11
11
  Metrics/CyclomaticComplexity:
12
- Max: 10
12
+ Max: 12
13
13
  Metrics/PerceivedComplexity:
14
14
  Max: 13
15
15
  Metrics/AbcSize:
data/CHANGELOG.md CHANGED
@@ -1,4 +1,9 @@
1
1
  # Telemetry::SNMP
2
2
 
3
+ ## [0.2.0]
4
+ * Fixing ENV variables to not use .
5
+ * Adding new device lock table for future reference
6
+ * Adding new Collector class
7
+
3
8
  ## [0.1.0]
4
9
  - Initial release
data/config.ru CHANGED
@@ -8,8 +8,13 @@ require 'sinatra'
8
8
  require 'oj'
9
9
  require 'multi_json'
10
10
 
11
+ ENV['telemetry_snmp_amqp_nodes'] = 'apvrs33012'
12
+ ENV['telemetry_snmp_amqp_username'] = 'snmp'
13
+ ENV['telemetry_snmp_amqp_password'] = 'snmp'
14
+ ENV['telemetry_snmp_amqp_port'] = '8080'
15
+ ENV['telemetry_snmp_amqp_vhost'] = 'telegraf'
16
+
11
17
  require 'telemetry/snmp'
12
18
  require 'telemetry/snmp/api'
13
-
14
- Telemetry::Snmp::Data.start!
19
+ Telemetry::Snmp.bootstrap
15
20
  run Telemetry::Snmp::API
data/exe/snmp_collector CHANGED
@@ -1,5 +1,11 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
+ ENV['telemetry.snmp.amqp.nodes'] = 'apvrs33012'
4
+ ENV['telemetry.snmp.amqp.username'] = 'snmp'
5
+ ENV['telemetry.snmp.amqp.password'] = 'snmp'
6
+ ENV['telemetry.snmp.amqp.port'] = '8080'
7
+ ENV['telemetry.snmp.amqp.vhost'] = 'telegraf'
8
+
3
9
  require 'telemetry/snmp'
4
10
  Telemetry::Snmp.bootstrap
5
11
 
@@ -9,8 +15,10 @@ trap('SIGINT') { @quit = true }
9
15
 
10
16
  @quit = false
11
17
  until @quit
18
+ sleep(1)
12
19
  @lines = []
13
- Telemetry::Snmp::Data::Model::Device.each do |row|
20
+ Telemetry::Snmp::Data::Model::Device.where(:active).each do |row|
21
+ break if @quit
14
22
  next if row.values[:last_polled].to_i + row.values[:frequency] > Time.now.to_i
15
23
 
16
24
  fields = {}
@@ -20,10 +28,13 @@ until @quit
20
28
  env: row.values[:environment],
21
29
  dc: row.values[:datacenter],
22
30
  zone: row.values[:zone],
23
- influxdb_node_group: 'snmp'
31
+ influxdb_node_group: 'snmp',
32
+ influxdb_database: 'snmp'
24
33
  }
25
34
 
26
35
  Telemetry::Snmp::Data::Model::OID.each do |oid_row|
36
+ break if @quit
37
+
27
38
  oid_value = Telemetry::Snmp::Client.oid_value(row[:hostname], oid_row.values[:oid])
28
39
  next if oid_value.nil?
29
40
  next unless oid_value.is_a?(Integer) || oid_value.is_a?(Float)
@@ -34,7 +45,7 @@ until @quit
34
45
  end
35
46
 
36
47
  @lines.push Telemetry::Metrics::Parser.to_line_protocol(
37
- measurement: 'palo_alto_a',
48
+ measurement: 'palo_alto',
38
49
  fields: fields,
39
50
  tags: tags,
40
51
  timestamp: (DateTime.now.strftime('%Q').to_i * 1000 * 1000)
@@ -48,6 +59,7 @@ until @quit
48
59
  row.save
49
60
  rescue StandardError => e
50
61
  Telemetry::Logger.error "#{e.class}: #{e.message}"
62
+ Telemetry::Logger.error e.backtrace[0..20]
51
63
  end
52
64
 
53
65
  Telemetry::Snmp::Publisher.push_lines(@lines) unless @lines.empty?
@@ -37,6 +37,20 @@ module Telemetry
37
37
  response.body = Oj.dump(response.body, mode: :compat) unless response.body.is_a? String
38
38
  end
39
39
 
40
+ get '/' do
41
+ {
42
+ version: Telemetry::Snmp::VERSION,
43
+ migration_version: Telemetry::Snmp::Data.migration_version
44
+ }
45
+ end
46
+
47
+ get '' do
48
+ {
49
+ version: Telemetry::Snmp::VERSION,
50
+ migration_version: Telemetry::Snmp::Data.migration_version
51
+ }
52
+ end
53
+
40
54
  get '/version' do
41
55
  {
42
56
  version: Telemetry::Snmp::VERSION,
@@ -44,6 +58,10 @@ module Telemetry
44
58
  }
45
59
  end
46
60
 
61
+ get '/loop_devices' do
62
+ Telemetry::Snmp::Collector.loop_devices
63
+ end
64
+
47
65
  namespace('/users') { register Telemetry::Snmp::Controller::Users }
48
66
  namespace('/devices/creds') { register Telemetry::Snmp::Controller::DeviceCreds }
49
67
  namespace('/devices') { register Telemetry::Snmp::Controller::Devices }
@@ -0,0 +1,101 @@
1
+ module Telemetry
2
+ module Snmp
3
+ module Collector
4
+ class << self
5
+ def worker_name
6
+ "#{::Socket.gethostname.tr('.', '_')}.#{::Process.pid}.#{Thread.current.object_id}"
7
+ end
8
+
9
+ def loop_devices
10
+ Telemetry::Snmp::Data::Model::Device.where(:active).each do |row|
11
+ next if row.values[:last_polled].to_i + row.values[:frequency] > Time.now.to_i
12
+ next if device_locked?(row.values[:id])
13
+
14
+ collect(row.values[:id])
15
+ end
16
+ end
17
+
18
+ def unlock_expired_devices
19
+ Telemetry::Snmp::Data::Model::DeviceLock.each do |row|
20
+ next if row.values[:expires] < Sequel::CURRENT_TIMESTAMP
21
+
22
+ row.delete
23
+ end
24
+ end
25
+
26
+ def device_locked?(device_id)
27
+ !Telemetry::Snmp::Data::Model::DeviceLock[device_id: device_id].nil?
28
+ end
29
+
30
+ def lock_device(device_id)
31
+ return false unless Telemetry::Snmp::Data::Model::DeviceLock[device_id: device_id].nil?
32
+
33
+ Telemetry::Snmp::Data::Model::DeviceLock.insert(
34
+ worker_name: worker_name,
35
+ device_id: device_id,
36
+ created: Sequel::CURRENT_TIMESTAMP,
37
+ expires: Sequel::CURRENT_TIMESTAMP
38
+ )
39
+ true
40
+ end
41
+
42
+ # noinspection RubyArgCount
43
+ def unlock_device(device_id)
44
+ device = Telemetry::Snmp::Data::Model::DeviceLock[device_id: device_id]
45
+ return true if device.nil?
46
+
47
+ device.delete
48
+ end
49
+
50
+ def collect(device_id)
51
+ lock_device(device_id)
52
+ row = Telemetry::Snmp::Data::Model::Device[device_id]
53
+ lines = []
54
+ fields = {}
55
+ tags = {
56
+ hostname: row.values[:hostname],
57
+ ip_address: row.values[:ip_address],
58
+ env: row.values[:environment],
59
+ dc: row.values[:datacenter],
60
+ zone: row.values[:zone],
61
+ influxdb_node_group: 'snmp',
62
+ influxdb_database: 'snmp'
63
+ }
64
+
65
+ Telemetry::Snmp::Data::Model::OID.each do |oid_row|
66
+ break if @quit
67
+
68
+ oid_value = Telemetry::Snmp::Client.oid_value(row[:hostname], oid_row.values[:oid])
69
+ next if oid_value.nil?
70
+ next unless oid_value.is_a?(Integer) || oid_value.is_a?(Float)
71
+
72
+ fields[oid_row.values[:name]] =
73
+ "#{Telemetry::Snmp::Client.oid_value(row[:hostname], oid_row.values[:oid])}i"
74
+ rescue StandardError => e
75
+ Telemetry::Logger.error "#{e.class}: #{e.message}"
76
+ end
77
+
78
+ lines.push Telemetry::Metrics::Parser.to_line_protocol(
79
+ measurement: 'palo_alto',
80
+ fields: fields,
81
+ tags: tags,
82
+ timestamp: (DateTime.now.strftime('%Q').to_i * 1000 * 1000)
83
+ )
84
+
85
+ walker = Telemetry::Snmp::Client.grab_oid_metrics(row.values[:hostname])
86
+ Telemetry::Logger.info "Pushing #{walker.count} lines for #{row.values[:hostname]}" unless walker.empty?
87
+ Telemetry::Snmp::Publisher.push_lines(walker) unless walker.empty?
88
+
89
+ row.update(last_polled: Sequel::CURRENT_TIMESTAMP)
90
+ row.save
91
+
92
+ Telemetry::Snmp::Publisher.push_lines(lines) unless lines.empty?
93
+ unlock_device(device_id)
94
+ rescue StandardError => e
95
+ Telemetry::Logger.exception(e, level: 'error')
96
+ unlock_device(device_id)
97
+ end
98
+ end
99
+ end
100
+ end
101
+ end
@@ -19,52 +19,52 @@ module Telemetry
19
19
  module_function :default_credentials
20
20
 
21
21
  def adapter
22
- ENV["#{env_key}.adapter"] == 'postgres' ? 'postgres' : 'mysql2'
22
+ ENV["#{env_key}_adapter"] == 'postgres' ? 'postgres' : 'mysql2'
23
23
  end
24
24
  module_function :adapter
25
25
 
26
26
  def username
27
- ENV["#{env_key}.username"] || 'root'
27
+ ENV["#{env_key}_username"] || 'root'
28
28
  end
29
29
  module_function :username
30
30
 
31
31
  def password
32
- ENV["#{env_key}.password"] || nil
32
+ ENV["#{env_key}_password"] || nil
33
33
  end
34
34
  module_function :password
35
35
 
36
36
  def database
37
- ENV["#{env_key}.database"] || 'telemetry_snmp'
37
+ ENV["#{env_key}_database"] || 'telemetry_snmp'
38
38
  end
39
39
  module_function :database
40
40
 
41
41
  def host
42
- ENV["#{env_key}.host"] || '127.0.0.1'
42
+ ENV["#{env_key}_host"] || '127.0.0.1'
43
43
  end
44
44
  module_function :host
45
45
 
46
46
  def port
47
- ENV.key?("#{env_key}.port") ? ENV["#{env_key}.port"].to_i : 3306
47
+ ENV.key?("#{env_key}_port") ? ENV["#{env_key}_port"].to_i : 3306
48
48
  end
49
49
  module_function :port
50
50
 
51
51
  def max_connections
52
- ENV.key?("#{env_key}.max_connections") ? ENV["#{env_key}.max_connections"].to_i : 16
52
+ ENV.key?("#{env_key}_max_connections") ? ENV["#{env_key}_max_connections"].to_i : 16
53
53
  end
54
54
  module_function :max_connections
55
55
 
56
56
  def pool_timeout
57
- ENV.key?("#{env_key}.pool_timeout") ? ENV["#{env_key}.pool_timeout"].to_i : 2
57
+ ENV.key?("#{env_key}_pool_timeout") ? ENV["#{env_key}_pool_timeout"].to_i : 2
58
58
  end
59
59
  module_function :pool_timeout
60
60
 
61
61
  def preconnect
62
- ENV["#{env_key}.preconnect"] || 'concurrently'
62
+ ENV["#{env_key}_preconnect"] || 'concurrently'
63
63
  end
64
64
  module_function :preconnect
65
65
 
66
66
  def env_key
67
- ENV['conflux.data.key'] || 'telemetry.snmp.data'
67
+ 'telemetry_snmp_data'
68
68
  end
69
69
  module_function :env_key
70
70
  end
@@ -0,0 +1,19 @@
1
+ Sequel.migration do
2
+ change do
3
+ create_table(:device_locks) do
4
+ primary_key :id
5
+ String :worker_name, null: false
6
+ foreign_key :device_id, :devices, null: false, unique: true
7
+
8
+ DateTime :created
9
+ DateTime :updated
10
+ DateTime :expires
11
+
12
+ index :device_id
13
+ index :worker_name
14
+ index :created
15
+ index :updated
16
+ index :expires
17
+ end
18
+ end
19
+ end
@@ -4,6 +4,7 @@ module Telemetry
4
4
  module Model
5
5
  class Device < Sequel::Model
6
6
  many_to_one :device_cred
7
+ many_to_one :device_lock
7
8
  end
8
9
  end
9
10
  end
@@ -0,0 +1,11 @@
1
+ module Telemetry
2
+ module Snmp
3
+ module Data
4
+ module Model
5
+ class DeviceLock < Sequel::Model
6
+ one_to_one :device
7
+ end
8
+ end
9
+ end
10
+ end
11
+ end
@@ -38,7 +38,7 @@ module Telemetry
38
38
  end
39
39
 
40
40
  def models
41
- %w[user device_cred device oid oid_group oid_oid_groups oid_walk user_audit_log]
41
+ %w[user device_cred device device_lock oid oid_group oid_oid_groups oid_walk user_audit_log]
42
42
  end
43
43
 
44
44
  def connection(**opts)
@@ -21,8 +21,8 @@ module Telemetry
21
21
  end
22
22
 
23
23
  def username
24
- if ENV.key? 'telemetry.snmp.amqp.username'
25
- ENV['telemetry.snmp.amqp.username']
24
+ if ENV.key? 'telemetry_snmp_amqp_username'
25
+ ENV['telemetry_snmp_amqp_username']
26
26
  elsif opts[:amqp].key? :username
27
27
  opts[:amqp][:username]
28
28
  else
@@ -31,16 +31,16 @@ module Telemetry
31
31
  end
32
32
 
33
33
  def password
34
- ENV['telemetry.snmp.amqp.password'] || opts[:amqp][:password] || 'guest'
34
+ ENV['telemetry_snmp_amqp_password'] || opts[:amqp][:password] || 'guest'
35
35
  end
36
36
 
37
37
  def vhost
38
- ENV['telemetry.snmp.amqp.vhost'] || opts[:amqp][:vhost] || 'telemetry'
38
+ ENV['telemetry_snmp_amqp_vhost'] || opts[:amqp][:vhost] || 'telemetry'
39
39
  end
40
40
 
41
41
  def port
42
- if ENV.key? 'telemetry.snmp.amqp.port'
43
- ENV['telemetry.snmp.amqp.port'].to_i
42
+ if ENV.key? 'telemetry_snmp_amqp_port'
43
+ ENV['telemetry_snmp_amqp_port'].to_i
44
44
  elsif opts[:amqp].key? :port
45
45
  opts[:amqp][:port]
46
46
  elsif use_ssl?
@@ -51,8 +51,8 @@ module Telemetry
51
51
  end
52
52
 
53
53
  def use_ssl?
54
- if ENV.key? 'telemetry.snmp.amqp.use_ssl'
55
- %w[1 true].include? ENV['telemetry.snmp.amqp.use_ssl']
54
+ if ENV.key? 'telemetry_snmp_amqp_use_ssl'
55
+ %w[1 true].include? ENV['telemetry_snmp_amqp_use_ssl']
56
56
  elsif opts[:amqp].key?(:use_ssl)
57
57
  opts[:amqp][:use_ssl]
58
58
  else
@@ -61,8 +61,8 @@ module Telemetry
61
61
  end
62
62
 
63
63
  def nodes
64
- if ENV.key?('telemetry.snmp.amqp.nodes')
65
- ENV['telemetry.snmp.amqp.nodes'].split(',')
64
+ if ENV.key?('telemetry_snmp_amqp_nodes')
65
+ ENV['telemetry_snmp_amqp_nodes'].split(',')
66
66
  elsif opts[:amqp].key?(:nodes)
67
67
  opts[:amqp][:nodes]
68
68
  else
@@ -71,7 +71,7 @@ module Telemetry
71
71
  end
72
72
 
73
73
  def exchange_name
74
- ENV['telemetry.snmp.amqp.exchange_name'] || opts[:amqp][:exchange_name] || 'telemetry.snmp'
74
+ ENV['telemetry_snmp_amqp_exchange_name'] || opts[:amqp][:exchange_name] || 'telemetry.snmp'
75
75
  end
76
76
 
77
77
  def session
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Telemetry
4
4
  module Snmp
5
- VERSION = '0.1.0'
5
+ VERSION = '0.2.0'
6
6
  end
7
7
  end
@@ -6,6 +6,7 @@ require 'telemetry/metrics/parser'
6
6
  require 'telemetry/snmp/data'
7
7
  require 'telemetry/snmp/client'
8
8
  require 'telemetry/snmp/publisher'
9
+ require 'telemetry/snmp/collector'
9
10
 
10
11
  module Telemetry
11
12
  module Snmp
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: telemetry-snmp
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Esity
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-08-06 00:00:00.000000000 Z
11
+ date: 2021-09-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: concurrent-ruby
@@ -319,6 +319,7 @@ files:
319
319
  - lib/telemetry/snmp/auth.rb
320
320
  - lib/telemetry/snmp/auth/defaults.rb
321
321
  - lib/telemetry/snmp/client.rb
322
+ - lib/telemetry/snmp/collector.rb
322
323
  - lib/telemetry/snmp/controllers/device_creds.rb
323
324
  - lib/telemetry/snmp/controllers/devices.rb
324
325
  - lib/telemetry/snmp/controllers/oid_groups.rb
@@ -337,8 +338,10 @@ files:
337
338
  - lib/telemetry/snmp/data/migrations/008_create_walks_table.rb
338
339
  - lib/telemetry/snmp/data/migrations/009_create_tag_name_column.rb
339
340
  - lib/telemetry/snmp/data/migrations/010_create_user_audit_table.rb
341
+ - lib/telemetry/snmp/data/migrations/011_device_locks.rb
340
342
  - lib/telemetry/snmp/data/models/device.rb
341
343
  - lib/telemetry/snmp/data/models/device_cred.rb
344
+ - lib/telemetry/snmp/data/models/device_lock.rb
342
345
  - lib/telemetry/snmp/data/models/oid.rb
343
346
  - lib/telemetry/snmp/data/models/oid_group.rb
344
347
  - lib/telemetry/snmp/data/models/oid_oid_groups.rb