syslog_generator 1.0.3 → 1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 10c4620e56001ee6f2df0158e5a064e16c79da04
4
- data.tar.gz: d1b9cc462cf6de19e549bb033c6f5b91cb4e6d5c
3
+ metadata.gz: 8ea00844536286b7a35920e5fcdb297bd63aa72e
4
+ data.tar.gz: f7ae016391982ca2c5bcd3f6e26155194d2915e3
5
5
  SHA512:
6
- metadata.gz: f40206a3bdcb212003e1e0d8051421faaaaad6520abd716485e59f3d8f4880af55eaff746f5439a477096b76cf201fb2c2a05c426945a39e6734a3128ba8b3be
7
- data.tar.gz: 5d21ef9e46db1627bc616019a532d43a7763f64f56e781365ff8b50b67708e10d86bbadf1603b1c5aaafd2de20916dba925074510a321addf63532bce9c44140
6
+ metadata.gz: 87b95be36f0cb72f6a7e3c727738e009212ed59528feb5ab148018ad82d0b3d4ba1a9de3378f1999c7e78141c940cae6e732006bf720b65ee445c2031e06b309
7
+ data.tar.gz: aea3e051459c37d627172ea31cb74f0f8af60635ccd74aba2058beb770a422cfa8033a9f5ce9842dbb1b81b72214ba7c400534a93fbdf8ec8f81afffff19cd76
data/Rakefile CHANGED
@@ -1,2 +1 @@
1
- require "bundler/gem_tasks"
2
-
1
+ require 'bundler/gem_tasks'
data/bin/loggen CHANGED
@@ -1,12 +1,21 @@
1
1
  #!/usr/bin/env ruby
2
2
  require 'syslog_generator'
3
- require 'trollop'
4
3
  require 'syslog_generator/version'
4
+ require 'syslog_protocol'
5
+ require 'trollop'
6
+
7
+ # Drop the : from the severities hash when presenting it to the user
8
+ valid_priorities_list = SyslogProtocol::SEVERITIES.keys.each do |k|
9
+ k.to_s.gsub(':', '')
10
+ end
11
+
5
12
  opts = Trollop.options do
6
- version "loggen #{SyslogGenerator::VERSION} - Michael Parks <mparks@tkware.info>"
13
+ version "loggen #{SyslogGenerator::VERSION}
14
+ APL, Michael Parks, TKWare Enteprises <mparks@tkware.info>"
7
15
  banner <<-EOS
8
16
  Generate random syslog lines according to the provided options.
9
- Absent any options, 100 lines of nonsense will be sent to the syslog port on localhost.
17
+ Absent any options, 100 lines of nonsense will be sent to the syslog port on
18
+ the local machine (localhost).
10
19
 
11
20
  Usage: loggen [options]
12
21
 
@@ -14,12 +23,17 @@ Where [options] can be any of the following:
14
23
  EOS
15
24
  opt :count, 'Count of lines to generate. Set to -1 for infinite.', type: Integer, default: 100
16
25
  opt :words, 'Words per line', type: Integer, default: 5
17
- opt :progname, 'Program name attached to each log line', type: String, default: 'log_generator'
26
+ opt :name, 'Program name attached to each log line', type: String, default: 'log_generator'
18
27
  opt :test, 'Send lines to stdout rather than a remote server'
19
28
  opt :server, 'Send lines to this remote server', type: String, default: 'localhost'
20
29
  opt :facility, 'Syslog facility to use', type: String, default: 'local6'
21
30
  opt :protocol, 'TCP or UDP', type: String, default: 'udp'
22
31
  opt :port, 'Port number to send generated lines to', type: Integer, default: 514
32
+ opt :priority, "Message priority, must be one of: #{valid_priorities_list}", type: String, default: 'info'
33
+ end
34
+
35
+ if SyslogProtocol::SEVERITIES.keys.grep(opts[:priority]).empty?
36
+ Trollop.die :priority, "Must be one of #{valid_priorities_list}"
23
37
  end
24
38
 
25
39
  logger = SyslogGenerator::Logger.new(opts)
@@ -1,38 +1,72 @@
1
- require "syslog_generator/version"
1
+ require 'syslog_generator/version'
2
2
  require 'random-word'
3
3
  require 'syslog_protocol'
4
4
  require 'socket'
5
-
6
- # Initialize a new logger endpoint, setting up the appropriate socket from options.
5
+ require 'pry'
6
+ # Initialize a new logger endpoint, setting up the appropriate socket from
7
+ # the provided options.
7
8
  module SyslogGenerator
9
+ # Implementation of a log formatter/sender
8
10
  class Logger
9
11
  attr_accessor :options
10
12
 
13
+ private
14
+
15
+ def initialize_socket(options)
16
+ case options[:protocol].downcase
17
+ when 'udp'
18
+ UDPSocket.new
19
+ when 'tcp'
20
+ TCPSocket.new
21
+ else fail 'Invalid protocol specified.'
22
+ end
23
+ end
24
+
25
+ def gen_payload(text)
26
+ # Syslog_protocol uses different methods for each priority
27
+ # (@formatter.debug, @formatter.emerg, etc) so we use .method to save the
28
+ # call with the appropriate name, and .call to invoke it with the right
29
+ # string. This won't be likely to break since we check for valid names
30
+ # back at options parsing.
31
+ meta_formatter = @formatter.method(options[:priority])
32
+ if text.is_a?(Array)
33
+ @payload = meta_formatter.call(text.join(' '))
34
+ else
35
+ @payload = meta_formatter.call(info(text))
36
+ end
37
+ end
38
+
39
+ def gen_words
40
+ RandomWord.nouns.take(options[:words])
41
+ end
42
+
43
+ public
44
+
11
45
  def initialize(options)
12
46
  self.options = options
13
- @formatter = SyslogProtocol::Logger.new(`hostname`.strip, options[:progname], options[:facility])
14
- @socket = case options[:protocol].downcase
15
- when 'udp'
16
- UDPSocket.new
17
- when 'tcp'
18
- TCPSocket.new
19
- else fail 'Invalid protocol specified.'
20
- end
47
+ # TODO: Won't work on systems that don't have `hostname`?
48
+ @formatter = SyslogProtocol::Logger.new(
49
+ `hostname`.strip,
50
+ options[:name],
51
+ options[:facility]
52
+ )
53
+ @socket = initialize_socket(options)
21
54
  end
22
55
 
23
56
  def send(text)
24
- text.is_a?(Array) ? @payload = @formatter.info(text.join(' ')) : @payload = @formatter.info(text)
25
- self.options[:test] ? puts(@payload) : @socket.send(@payload, 0, self.options[:server], self.options[:port])
57
+ if options[:test]
58
+ puts(gen_payload(text))
59
+ else
60
+ @socket.send(gen_payload(text), 0, options[:server], options[:port])
61
+ end
26
62
  end
27
63
 
28
64
  def start
29
- if self.options[:count] != -1
30
- self.options[:count].times { self.send RandomWord.nouns.take(self.options[:words]) }
65
+ if options[:count] != -1
66
+ options[:count].times { send(gen_words) }
31
67
  else
32
- loop { self.send RandomWord.nouns.take(self.options[:words]) }
68
+ loop { send(gen_words) }
33
69
  end
34
70
  end
35
-
36
71
  end
37
72
  end
38
-
@@ -1,3 +1,4 @@
1
+ # Just pull in the version number (Gem defaults)
1
2
  module SyslogGenerator
2
- VERSION = "1.0.3"
3
+ VERSION = '1.1'
3
4
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: syslog_generator
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.3
4
+ version: '1.1'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Parks
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-07-22 00:00:00.000000000 Z
11
+ date: 2015-07-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler