Class: Fluent::Plugin::SyslogTlsOutput

Inherits:
Output
  • Object
show all
Defined in:
lib/fluent/plugin/out_devo.rb

Constant Summary collapse

DEFAULT_FORMAT_TYPE =
'json'
SYSLOG_HEADERS =

Allow to map keys from record to syslog message headers

[
  :severity, :facility, :hostname, :app_name, :procid, :msgid
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSyslogTlsOutput

Returns a new instance of SyslogTlsOutput.



56
57
58
59
# File 'lib/fluent/plugin/out_devo.rb', line 56

def initialize
  super
  @loggers = {}
end

Instance Attribute Details

#formatterObject

Returns the value of attribute formatter.



53
54
55
# File 'lib/fluent/plugin/out_devo.rb', line 53

def formatter
  @formatter
end

Instance Method Details

#configure(conf) ⇒ Object

This method is called before starting.



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/fluent/plugin/out_devo.rb', line 67

def configure(conf)
  if conf['output_type'] && !conf['format']
    conf['format'] = conf['output_type']
  end
  compat_parameters_convert(conf, :inject, :formatter)

  super
  @host = conf['host']
  @port = conf['port']
  @token = conf['token']
  @hostname = conf['hostname'] || Socket.gethostname.split('.').first

  # Determine mapping of record keys to syslog keys
  @mappings = {}
  SYSLOG_HEADERS.each do |key_name|
    conf_key = "#{key_name}_key"
    @mappings[key_name] = conf[conf_key] if conf.key?(conf_key)
  end

  @formatter = formatter_create(conf: conf.elements('format').first, default_type: DEFAULT_FORMAT_TYPE)
end

#format(tag, time, record) ⇒ Object



118
119
120
121
# File 'lib/fluent/plugin/out_devo.rb', line 118

def format(tag, time, record)
  record = inject_values_to_record(tag, time, record)
  @formatter.format(tag, time, record)
end

#logger(tag) ⇒ Object

Get logger for given tag



90
91
92
93
94
95
96
97
98
99
100
# File 'lib/fluent/plugin/out_devo.rb', line 90

def logger(tag)
  # Try to reuse existing logger
  @loggers[tag] ||= new_logger(tag)

  # Create new logger if old one is closed
  if @loggers[tag].closed?
    @loggers[tag] = new_logger(tag)
  end

  @loggers[tag]
end

#new_logger(tag) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/fluent/plugin/out_devo.rb', line 102

def new_logger(tag)
  transport = ::SyslogTls::SSLTransport.new(host, port,
    idle_timeout: idle_timeout,
    ca_cert: ca_cert,
    client_cert: client_cert,
    client_key: client_key,
    verify_cert_name: verify_cert_name,
    max_retries: 3,
  )
  logger = ::SyslogTls::Logger.new(transport, token)
  logger.facility(facility)
  logger.hostname(hostname)
  logger.app_name(tag)
  logger
end

#process(tag, es) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/fluent/plugin/out_devo.rb', line 123

def process(tag, es)
  es.each do |time, record|
    record.each_pair do |_, v|
      v.force_encoding('utf-8') if v.is_a?(String)
    end

    # Check if severity has been provided in record otherwise use INFO
    # by default.
    severity = if @mappings.key?(:severity)
                 record[@mappings[:severity]] || 'INFO'
               else
                 'INFO'
               end

    # Send message to Syslog
    begin
      logger(tag).log(severity, format(tag, time, record), time: Time.at(time)) do |header|
        # Map syslog headers from record
        @mappings.each do |name, record_key|
          header.send("#{name}=", record[record_key]) unless record[record_key].nil?
        end
      end
    rescue => e
      log.error e.to_s
    end
  end
end

#shutdownObject



61
62
63
64
# File 'lib/fluent/plugin/out_devo.rb', line 61

def shutdown
  @loggers.values.each(&:close)
  super
end