Created
December 6, 2016 01:36
-
-
Save eric/16fa143a7128e06224c8a320502c16c1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'socket' | |
LISTEN_PORT = 8514 | |
DESTINATION_HOST = "xxxx.papertrailapp.com" | |
DESTINATION_PORT = xxxx | |
class SyslogRelay | |
PARSER = /^(<\d+>\S+\s+\S+\s+\S+) \("([^,]+),([^,]+),([^"]+)"\) ([^:]+): (.*)$/ | |
def initialize(listen_port, destination_host, destination_port, include_version_details = false) | |
@server = UDPSocket.new | |
@server.bind("0.0.0.0", listen_port) | |
@client = UDPSocket.new | |
@destination_host = destination_host | |
@destination_port = destination_port | |
@include_version_details = include_version_details | |
end | |
def start | |
if @running | |
return | |
end | |
@running = true | |
run | |
end | |
def join | |
if @thread | |
@thread.join | |
end | |
end | |
# <30>Apr 6 18:09:55 ("U7PG2,44d9exxxxxx,v3.4.16.3435") hostapd: ath0: STA 18:b4:30:xx:xx:xx IEEE 802.11: associated | |
def run | |
@thread = Thread.new do | |
while @running | |
begin | |
data, from = @server.recvfrom(65535) | |
if data && data.length > 0 | |
if matched = data.match(PARSER) | |
_, prefix, model, mac, version, program, message = *matched | |
if @include_version_details | |
version_details = "#{model}: #{version}: " | |
end | |
rewritten = "#{prefix} #{mac} #{program}: #{version_details}#{message}" | |
@client.send(rewritten, 0, @destination_host, @destination_port) | |
end | |
end | |
rescue => ex | |
puts "Error: #{ex.class}: #{ex.message}" | |
end | |
end | |
end | |
end | |
end | |
relay = SyslogRelay.new(LISTEN_PORT, DESTINATION_HOST, DESTINATION_PORT) | |
relay.start | |
relay.join |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment