-
Notifications
You must be signed in to change notification settings - Fork 9
/
udp-to-syslog
executable file
·53 lines (47 loc) · 1.13 KB
/
udp-to-syslog
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/usr/bin/env ruby
require 'socket'
require 'logger'
require 'syslog'
require 'optparse'
options = {
:ident => 'udp-to-syslog',
:port => 6666
}
parser = OptionParser.new do |opts|
nl = "\n" + ' ' * 37
opts.banner = "Usage: udp-to-syslog [options]"
opts.separator "Forwards UDP messages to syslog."
opts.separator ""
opts.separator "Options:"
opts.on("-i", "--ident NAME",
"Use the given syslog ident. Default: udp-to-syslog") do |value|
options[:ident] = value
end
opts.on("-p", "--port PORT",
"Listen on the given port. Default: 6666") do |value|
options[:port] = value
end
end
begin
parser.parse!
rescue OptionParser::ParseError => e
STDERR.puts e
STDERR.puts
STDERR.puts "Please see '--help' for valid options."
exit 1
end
include Syslog::Constants
Syslog.open(options[:ident], 0, LOG_USER)
logger = Logger.new(STDERR)
sock = UDPSocket.new
sock.bind('0.0.0.0', options[:port])
begin
while true
msg, addr = sock.recvfrom(1024 * 128)
Syslog.log(LOG_CRIT, "#{addr[2]}: #{msg}")
logger.warn("#{addr[2]}: #{msg}")
end
rescue Interrupt
rescue SignalException => e
raise if e.signo != Signal.list['TERM']
end