Module: Telemetry::Snmp::Publisher

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

Class Method Summary collapse

Class Method Details

.channelObject



94
95
96
97
98
99
100
101
102
# File 'lib/telemetry/snmp/publisher.rb', line 94

def channel
  if !@channel_thread.nil? && !@channel_thread.value.nil? && @channel_thread.value.open?
    return @channel_thread.value
  end

  @channel_thread = Concurrent::ThreadLocalVar.new(nil) if @channel_thread.nil?
  @channel_thread.value = session.create_channel
  @channel_thread.value
end

.exchange_nameObject



73
74
75
# File 'lib/telemetry/snmp/publisher.rb', line 73

def exchange_name
  ENV['telemetry_snmp_amqp_exchange_name'] || opts[:amqp][:exchange_name] || 'telemetry.snmp'
end

.line_exchangeObject



104
105
106
107
108
109
110
111
112
113
114
# File 'lib/telemetry/snmp/publisher.rb', line 104

def line_exchange
  @exchange ||= channel.topic(
    exchange_name,
    durable: true,
    auto_delete: false,
    internal: false,
    passive: false
  )
rescue Bunny::PreconditionFailed
  @exchange ||= channel.topic(exchange_name, passive: true)
end

.nodesObject



63
64
65
66
67
68
69
70
71
# File 'lib/telemetry/snmp/publisher.rb', line 63

def nodes
  if ENV.key?('telemetry_snmp_amqp_nodes')
    ENV['telemetry_snmp_amqp_nodes'].split(',')
  elsif opts[:amqp].key?(:nodes)
    opts[:amqp][:nodes]
  else
    ['localhost']
  end
end

.optsObject



19
20
21
# File 'lib/telemetry/snmp/publisher.rb', line 19

def opts
  @opts ||= { amqp: {} }
end

.passwordObject



33
34
35
# File 'lib/telemetry/snmp/publisher.rb', line 33

def password
  ENV['telemetry_snmp_amqp_password'] || opts[:amqp][:password] || 'guest'
end

.portObject



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/telemetry/snmp/publisher.rb', line 41

def port
  if ENV.key? 'telemetry_snmp_amqp_port'
    ENV['telemetry_snmp_amqp_port'].to_i
  elsif opts[:amqp].key? :port
    opts[:amqp][:port]
  elsif use_ssl?
    5671
  else
    5672
  end
end

.publish_optsObject



116
117
118
119
120
121
122
123
124
125
126
# File 'lib/telemetry/snmp/publisher.rb', line 116

def publish_opts
  {
    routing_key: 'snmp',
    persistent: true,
    mandatory: false,
    timestamp: Time.now.to_i,
    type: 'metric',
    content_type: 'application/json',
    content_encoding: 'identity'
  }
end

.push_lines(lines) ⇒ Object



90
91
92
# File 'lib/telemetry/snmp/publisher.rb', line 90

def push_lines(lines)
  line_exchange.publish(lines.join("\n"), **publish_opts, routing_key: 'snmp')
end

.sessionObject



77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/telemetry/snmp/publisher.rb', line 77

def session
  @session ||= Telemetry::AMQP::Base.new(
    auto_start: true,
    vhost: vhost,
    application: 'Telemetry::Snmp',
    app_version: Telemetry::Snmp::VERSION,
    username: username,
    password: password,
    port: port,
    nodes: nodes
  )
end

.start!(**opts) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/telemetry/snmp/publisher.rb', line 7

def start!(**opts)
  @opts = opts
  @opts[:amqp] = {} if opts[:amqp].nil?

  @session = nil
  @channel = nil
  @exchange = nil
  session
  channel
  true
end

.use_ssl?Boolean

Returns:

  • (Boolean)


53
54
55
56
57
58
59
60
61
# File 'lib/telemetry/snmp/publisher.rb', line 53

def use_ssl?
  if ENV.key? 'telemetry_snmp_amqp_use_ssl'
    %w[1 true].include? ENV['telemetry_snmp_amqp_use_ssl']
  elsif opts[:amqp].key?(:use_ssl)
    opts[:amqp][:use_ssl]
  else
    false
  end
end

.usernameObject



23
24
25
26
27
28
29
30
31
# File 'lib/telemetry/snmp/publisher.rb', line 23

def username
  if ENV.key? 'telemetry_snmp_amqp_username'
    ENV['telemetry_snmp_amqp_username']
  elsif opts[:amqp].key? :username
    opts[:amqp][:username]
  else
    'guest'
  end
end

.vhostObject



37
38
39
# File 'lib/telemetry/snmp/publisher.rb', line 37

def vhost
  ENV['telemetry_snmp_amqp_vhost'] || opts[:amqp][:vhost] || 'telemetry'
end