Class: RemoteSyslog::Agent

Inherits:
Servolux::Server
  • Object
show all
Defined in:
lib/remote_syslog/agent.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Agent

Returns a new instance of Agent.



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/remote_syslog/agent.rb', line 47

def initialize(options = {})
  @files = []
  @glob_check_interval = 10
  @eventmachine_tail = options.fetch(:eventmachine_tail, true)

  unless logger = options[:logger]
    logger = Logger.new(STDERR)
    logger.level = Logger::ERROR
  end

  super('remote_syslog', :logger => logger, :pid_file => options[:pid_file])
end

Instance Attribute Details

#client_cert_chainObject

TLS settings



24
25
26
# File 'lib/remote_syslog/agent.rb', line 24

def client_cert_chain
  @client_cert_chain
end

#client_private_keyObject

TLS settings



24
25
26
# File 'lib/remote_syslog/agent.rb', line 24

def client_private_key
  @client_private_key
end

#destination_hostObject

Who should we connect to?



15
16
17
# File 'lib/remote_syslog/agent.rb', line 15

def destination_host
  @destination_host
end

#destination_portObject

Who should we connect to?



15
16
17
# File 'lib/remote_syslog/agent.rb', line 15

def destination_port
  @destination_port
end

#eventmachine_tailObject

Should we use eventmachine to tail?



45
46
47
# File 'lib/remote_syslog/agent.rb', line 45

def eventmachine_tail
  @eventmachine_tail
end

#exclude_file_patternObject

Exclude files matching pattern



39
40
41
# File 'lib/remote_syslog/agent.rb', line 39

def exclude_file_pattern
  @exclude_file_pattern
end

#exclude_patternObject

Exclude messages matching pattern



33
34
35
# File 'lib/remote_syslog/agent.rb', line 33

def exclude_pattern
  @exclude_pattern
end

#facilityObject

syslog defaults



27
28
29
# File 'lib/remote_syslog/agent.rb', line 27

def facility
  @facility
end

#filesObject

Files (can be globs)



36
37
38
# File 'lib/remote_syslog/agent.rb', line 36

def files
  @files
end

#glob_check_intervalObject

How often should we check for new files?



42
43
44
# File 'lib/remote_syslog/agent.rb', line 42

def glob_check_interval
  @glob_check_interval
end

#hostnameObject

syslog defaults



27
28
29
# File 'lib/remote_syslog/agent.rb', line 27

def hostname
  @hostname
end

#parse_fieldsObject

Other settings



30
31
32
# File 'lib/remote_syslog/agent.rb', line 30

def parse_fields
  @parse_fields
end

#prependObject

Other settings



30
31
32
# File 'lib/remote_syslog/agent.rb', line 30

def prepend
  @prepend
end

#server_certObject

TLS settings



24
25
26
# File 'lib/remote_syslog/agent.rb', line 24

def server_cert
  @server_cert
end

#severityObject

syslog defaults



27
28
29
# File 'lib/remote_syslog/agent.rb', line 27

def severity
  @severity
end

#strip_colorObject

Other settings



30
31
32
# File 'lib/remote_syslog/agent.rb', line 30

def strip_color
  @strip_color
end

#tcpObject

Should use TCP?



18
19
20
# File 'lib/remote_syslog/agent.rb', line 18

def tcp
  @tcp
end

#tlsObject

Should use TLS?



21
22
23
# File 'lib/remote_syslog/agent.rb', line 21

def tls
  @tls
end

Instance Method Details

#before_stoppingObject



140
141
142
# File 'lib/remote_syslog/agent.rb', line 140

def before_stopping
  EM.stop
end

#endpoint_modeObject



130
131
132
133
134
135
136
137
138
# File 'lib/remote_syslog/agent.rb', line 130

def endpoint_mode
  @endpoint_mode ||= if @tls
    'TCP/TLS'
  elsif @tcp
    'TCP'
  else
    'UDP'
  end
end

#log_file=(file) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/remote_syslog/agent.rb', line 60

def log_file=(file)
  @log_file = File.expand_path(file)

  level = self.logger.level
  self.logger = Logger.new(file)
  self.logger.level = level
end

#redirect_io!Object



68
69
70
71
72
73
74
# File 'lib/remote_syslog/agent.rb', line 68

def redirect_io!
  if @log_file
    STDOUT.reopen(@log_file, 'a')
    STDERR.reopen(@log_file, 'a')
    STDERR.sync = STDOUT.sync = true
  end
end

#runObject



92
93
94
95
96
97
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
# File 'lib/remote_syslog/agent.rb', line 92

def run
  EventMachine.run do
    EM.error_handler do |e|
      logger.error "Unhandled EventMachine Exception: #{e.class}: #{e.message}:\n\t#{e.backtrace.join("\n\t")}"
    end

    if @tls
      max_message_size = 10240

      connection = TlsEndpoint.new(@destination_host, @destination_port,
        :client_cert_chain => @client_cert_chain,
        :client_private_key => @client_private_key,
        :server_cert => @server_cert,
        :logger => logger)
    elsif @tcp
      max_message_size = 20480

      connection = TcpEndpoint.new(@destination_host, @destination_port,
        :logger => logger)
    else
      max_message_size = 1024
      connection = UdpEndpoint.new(@destination_host, @destination_port,
        :logger => logger)
    end

    @message_generator = RemoteSyslog::MessageGenerator.new(connection, 
      :facility => @facility, :severity => @severity, 
      :strip_color => @strip_color, :hostname => @hostname, 
      :parse_fields => @parse_fields, :exclude_pattern => @exclude_pattern,
      :prepend => @prepend, :max_message_size => max_message_size)

    files.each do |file|
      RemoteSyslog::GlobWatch.new(file, @glob_check_interval, 
        @exclude_file_pattern, method(:watch_file))
    end
  end
end

#watch_file(file) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
# File 'lib/remote_syslog/agent.rb', line 80

def watch_file(file)
  if eventmachine_tail
    RemoteSyslog::EventMachineReader.new(file,
      :callback => @message_generator.method(:transmit),
      :logger => logger)
  else
    RemoteSyslog::FileTailReader.new(file,
      :callback => @message_generator.method(:transmit),
      :logger => logger)
  end
end