Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
It's a statsd! Kind of!
#!/usr/bin/env ruby
require 'socket'
require 'optparse'
port = 8125
pattern = /./
OptionParser.new do |opts|
opts.banner = "Usage: #{File.basename(__FILE__)} [options]"
opts.on("-p", "--port=PORT", Integer) { |val| port = val }
opts.on("-m", "--match=REGEX", String) { |val| pattern = /#{val}/ }
opts.parse!
end
u = UDPSocket.new
u.bind(nil, port)
puts "Listening on #{port} for datagrams matching #{pattern.inspect}…"
while true
text, sender = u.recvfrom(65535)
puts "#{Time.now}: #{text}" if text =~ pattern
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment