Skip to content

Instantly share code, notes, and snippets.

@alisdair
Created July 28, 2017 18:33
Show Gist options
  • Save alisdair/ebccae88ae0eccd957c0c321639f2e2b to your computer and use it in GitHub Desktop.
Save alisdair/ebccae88ae0eccd957c0c321639f2e2b to your computer and use it in GitHub Desktop.
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