Class: RemoteSyslogSender::Sender

Inherits:
Object
  • Object
show all
Defined in:
lib/remote_syslog_sender/sender.rb

Direct Known Subclasses

TcpSender, UdpSender

Defined Under Namespace

Classes: Packet

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(remote_hostname, remote_port, options = {}) ⇒ Sender

Returns a new instance of Sender.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/remote_syslog_sender/sender.rb', line 17

def initialize(remote_hostname, remote_port, options = {})
  @remote_hostname = remote_hostname
  @remote_port     = remote_port
  @whinyerrors     = options[:whinyerrors]
  @packet_size     = options[:packet_size] || 1024

  @packet = Packet.new

  local_hostname   = options[:hostname] || options[:local_hostname] || (Socket.gethostname rescue `hostname`.chomp)
  local_hostname   = 'localhost' if local_hostname.nil? || local_hostname.empty?
  @packet.hostname = local_hostname

  @packet.facility = options[:facility] || 'user'
  @packet.severity = options[:severity] || 'notice'
  @packet.tag      = options[:tag] || options[:program]  || "#{File.basename($0)}[#{$$}]"

  @socket = nil
end

Instance Attribute Details

#packetObject

Returns the value of attribute packet.



15
16
17
# File 'lib/remote_syslog_sender/sender.rb', line 15

def packet
  @packet
end

#socketObject (readonly)

Returns the value of attribute socket.



14
15
16
# File 'lib/remote_syslog_sender/sender.rb', line 14

def socket
  @socket
end

Instance Method Details

#closeObject



63
64
65
# File 'lib/remote_syslog_sender/sender.rb', line 63

def close
  @socket.close
end

#transmit(message, packet_options = nil) ⇒ Object Also known as: write



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/remote_syslog_sender/sender.rb', line 36

def transmit(message, packet_options = nil)
  message.split(/\r?\n/).each do |line|
    begin
      next if line =~ /^\s*$/
      packet = @packet.dup
      if packet_options
        packet.tag = packet_options[:program] if packet_options[:program]
        packet.hostname = packet_options[:local_hostname] if packet_options[:local_hostname]
        %i(hostname facility severity tag).each do |key|
          packet.send("#{key}=", packet_options[key]) if packet_options[key]
        end
      end
      packet.content = line
      send_msg(packet.assemble(@packet_size))
    rescue
      if @whinyerrors
        raise
      else
        $stderr.puts "#{self.class} error: #{$!.class}: #{$!}\nOriginal message: #{line}"
      end
    end
  end
end