Skip to content

Instantly share code, notes, and snippets.

@Karunamon
Last active August 29, 2015 14:25
Show Gist options
  • Save Karunamon/fa3151ac747cfc9415cd to your computer and use it in GitHub Desktop.
Save Karunamon/fa3151ac747cfc9415cd to your computer and use it in GitHub Desktop.
Syslog Generator
#!/usr/bin/env ruby
#Log message generation utility for testing syslog config
#Apache License - Michael Parks <[email protected]>
require 'random-word'
require 'trollop'
require 'syslog_protocol'
require 'socket'
$opts = Trollop::options do
banner <<-EOS
Generate random syslog lines according to the provided options
Usage: syslog_generator.rb [options]
EOS
opt :count, "Count of lines to generate. Set to -1 for infinite", :type => Integer, :default => 100
opt :words, "Words per line", :type => Integer, :default => 5
opt :progname, "Program name attached to each log line", :type => String, :default => "loggen"
opt :test, "Send lines to stdout rather than a remote server"
opt :server, "Send lines to this remote server", :type => String, :default => "localhost"
opt :facility, "Syslog facility to use", :type => String, :default => "local6"
opt :protocol, "TCP or UDP", :type => String, :default => "udp"
opt :port, "Port number to send generated lines to", :type => Integer, :default => 514
end
class LogEndpoint
def initialize
@formatter = SyslogProtocol::Logger.new(`hostname`.strip, $opts[:progname], $opts[:facility])
@socket = case $opts[:protocol].downcase
when "udp"
UDPSocket.new
when "tcp"
TCPSocket.new
else fail "Invalid protocol specified."
end
end
def send(text)
text.is_a?(Array) ? @payload = @formatter.info(text.join(' ')) : @payload = @formatter.info(text)
$opts[:test] ? puts(@payload) : @socket.send(@payload, 0, $opts[:server], $opts[:port])
end
end
log = LogEndpoint.new
if $opts[:count] != -1
$opts[:count].times do
log.send RandomWord.nouns.take($opts[:words])
end
else
loop do
log.send RandomWord.nouns.take($opts[:words])
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment