syslog_parser 0.1.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 5345750d94a2130ead033c6c9a94c79d3b4729374a5a7c4562d85de25bdb0cfa
4
+ data.tar.gz: c683f9a8336fe5253562a076ac223e4cb9760d07f3e2a0119f02beaae28ab493
5
+ SHA512:
6
+ metadata.gz: 6404d4503bab6a3ccb128bf3f22c3872cab79c342de564e5e7696278b9d96a1d8e33519af2504640f0e6458c356081f2ff465280ea5c9ada6673ade5aaae392f
7
+ data.tar.gz: 670e8077661e26de65cf63ac7718087be3d7ade15ddefa7a3652d0b4f90a8b818579618354c3ad497e0b8e0e722d5f1cdac31d71d9ca82e971be96138678fde3
@@ -0,0 +1,18 @@
1
+ name: Ruby
2
+
3
+ on: [push,pull_request]
4
+
5
+ jobs:
6
+ build:
7
+ runs-on: ubuntu-latest
8
+ steps:
9
+ - uses: actions/checkout@v2
10
+ - name: Set up Ruby
11
+ uses: ruby/setup-ruby@v1
12
+ with:
13
+ ruby-version: 3.0.1
14
+ bundler-cache: true
15
+ - name: Linter
16
+ run: bundle exec standardrb
17
+ - name: Test
18
+ run: bundle exec rspec
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2021-08-01
4
+
5
+ - Initial release
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gemspec
6
+
7
+ gem "standardrb"
8
+ gem "rake", "~> 13.0"
9
+ gem "rspec", "~> 3.0"
data/Gemfile.lock ADDED
@@ -0,0 +1,63 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ syslog_parser (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ ast (2.4.2)
10
+ diff-lcs (1.4.4)
11
+ parallel (1.20.1)
12
+ parser (3.0.2.0)
13
+ ast (~> 2.4.1)
14
+ rainbow (3.0.0)
15
+ rake (13.0.6)
16
+ regexp_parser (2.1.1)
17
+ rexml (3.2.5)
18
+ rspec (3.10.0)
19
+ rspec-core (~> 3.10.0)
20
+ rspec-expectations (~> 3.10.0)
21
+ rspec-mocks (~> 3.10.0)
22
+ rspec-core (3.10.1)
23
+ rspec-support (~> 3.10.0)
24
+ rspec-expectations (3.10.1)
25
+ diff-lcs (>= 1.2.0, < 2.0)
26
+ rspec-support (~> 3.10.0)
27
+ rspec-mocks (3.10.2)
28
+ diff-lcs (>= 1.2.0, < 2.0)
29
+ rspec-support (~> 3.10.0)
30
+ rspec-support (3.10.2)
31
+ rubocop (1.18.4)
32
+ parallel (~> 1.10)
33
+ parser (>= 3.0.0.0)
34
+ rainbow (>= 2.2.2, < 4.0)
35
+ regexp_parser (>= 1.8, < 3.0)
36
+ rexml
37
+ rubocop-ast (>= 1.8.0, < 2.0)
38
+ ruby-progressbar (~> 1.7)
39
+ unicode-display_width (>= 1.4.0, < 3.0)
40
+ rubocop-ast (1.8.0)
41
+ parser (>= 3.0.1.1)
42
+ rubocop-performance (1.11.4)
43
+ rubocop (>= 1.7.0, < 2.0)
44
+ rubocop-ast (>= 0.4.0)
45
+ ruby-progressbar (1.11.0)
46
+ standard (1.1.7)
47
+ rubocop (= 1.18.4)
48
+ rubocop-performance (= 1.11.4)
49
+ standardrb (1.0.0)
50
+ standard
51
+ unicode-display_width (2.0.0)
52
+
53
+ PLATFORMS
54
+ x86_64-linux
55
+
56
+ DEPENDENCIES
57
+ rake (~> 13.0)
58
+ rspec (~> 3.0)
59
+ standardrb
60
+ syslog_parser!
61
+
62
+ BUNDLED WITH
63
+ 2.2.15
data/README.md ADDED
@@ -0,0 +1,33 @@
1
+ # SyslogParser
2
+
3
+ Bare-bones syslog parser dependency-free.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'syslog_parser'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle install
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install syslog_parser
20
+
21
+ ## Usage
22
+
23
+ [See documentation](https://rubydoc.info/github/juanmanuelramallo/syslog_parser)
24
+
25
+ ## Development
26
+
27
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bundle exec rspec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
28
+
29
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
30
+
31
+ ## Contributing
32
+
33
+ Bug reports and pull requests are welcome on GitHub at https://github.com/juanmanuelramallo/syslog_parser.
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "syslog_parser"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,2 @@
1
+ require_relative "syslog_parser/version"
2
+ require_relative "syslog_parser/parser"
@@ -0,0 +1,75 @@
1
+ module SyslogParser
2
+ # Parses a group of syslog entries.
3
+ #
4
+ # @see https://datatracker.ietf.org/doc/html/rfc5424
5
+ #
6
+ # @example Parse a single syslog entry
7
+ # SyslogParser::Parser.new(message_count: 1, body: syslog_entry).parse
8
+ #
9
+ # @example Parse a group of syslog entries
10
+ # SyslogParser.new(message_count: 3, body: syslog_entries).parse
11
+ #
12
+ # Passing a message count value is particularly helpful when working with Heroku's LogPlex.
13
+ #
14
+ # @example The message count doesn't match the body
15
+ # parser = SyslogParser::Parser.new(message_count: 2, body: syslog_entry)
16
+ # parser.parse
17
+ # parser.errors[:message_count] #=> "with 1 is inconsistent with body, missing frames to parse."
18
+ class Parser
19
+ attr_reader :message_count, :body, :errors
20
+
21
+ FRAME_REGEX = Regexp.new [
22
+ '<(?<priority>\d+)>',
23
+ '(?<syslog_version>\d) ',
24
+ '(?<timestamp>\S+) ',
25
+ '(?<host>\S+) ',
26
+ '(?<app_name>\S+) ',
27
+ '(?<process_name>\S+) ',
28
+ "- (?<message>.+)"
29
+ ].join.freeze
30
+
31
+ # @param message_count [Integer] Amount of syslog entries to parse from +body+
32
+ # @param body [String] A string that contains either one or several syslog entries.
33
+ def initialize(message_count:, body:)
34
+ @message_count = message_count
35
+ @body = body
36
+ @errors = {}
37
+ end
38
+
39
+ # Executes the parsing algorithm against the +body+.
40
+ #
41
+ # @return Array<Hash<String, String>>
42
+ def parse
43
+ body_index = 0
44
+ parsed_logs = message_count.times.map do |index|
45
+ # Overflow error
46
+ if body_index >= body.size
47
+ errors[:message_count] = "with #{message_count} is inconsistent with body, overflowed body."
48
+ return []
49
+ end
50
+
51
+ # Get frame size
52
+ frame = /\A(\d)+/.match(body[body_index..])[0].to_i
53
+
54
+ # Discard frame information and whitespace
55
+ body_index += frame.to_s.size + 1
56
+
57
+ # Get current frame
58
+ current_frame = body[body_index..(body_index + frame)]
59
+
60
+ # Advance pointer to the next frame
61
+ body_index += frame
62
+
63
+ FRAME_REGEX.match(current_frame).named_captures
64
+ end
65
+
66
+ # Missing frames to parse
67
+ if body_index != body.size
68
+ errors[:message_count] = "with #{message_count} is inconsistent with body, missing frames to parse."
69
+ return []
70
+ end
71
+
72
+ parsed_logs
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,6 @@
1
+ module SyslogParser
2
+ MAJOR = 0
3
+ MINOR = 1
4
+ PATCH = 0
5
+ VERSION = [MAJOR, MINOR, PATCH].join(".")
6
+ end
@@ -0,0 +1,22 @@
1
+ require_relative "lib/syslog_parser/version"
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "syslog_parser"
5
+ spec.version = SyslogParser::VERSION
6
+ spec.authors = ["Juan Manuel Ramallo"]
7
+ spec.email = ["juanmanuelramallo@hey.com"]
8
+
9
+ spec.summary = "Syslog parser"
10
+ spec.description = "Syslog parser"
11
+ spec.homepage = "https://github.com/juanmanuelramallo/syslog_parser"
12
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.4.0")
13
+
14
+ spec.metadata["homepage_uri"] = spec.homepage
15
+ spec.metadata["source_code_uri"] = "https://github.com/juanmanuelramallo/syslog_parser"
16
+ spec.metadata["changelog_uri"] = "https://github.com/juanmanuelramallo/syslog_parser/blob/master/CHANGELOG.md"
17
+
18
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
19
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
20
+ end
21
+ spec.require_paths = ["lib"]
22
+ end
metadata ADDED
@@ -0,0 +1,59 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: syslog_parser
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Juan Manuel Ramallo
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-08-01 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Syslog parser
14
+ email:
15
+ - juanmanuelramallo@hey.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".github/workflows/main.yml"
21
+ - ".gitignore"
22
+ - ".rspec"
23
+ - CHANGELOG.md
24
+ - Gemfile
25
+ - Gemfile.lock
26
+ - README.md
27
+ - Rakefile
28
+ - bin/console
29
+ - bin/setup
30
+ - lib/syslog_parser.rb
31
+ - lib/syslog_parser/parser.rb
32
+ - lib/syslog_parser/version.rb
33
+ - syslog_parser.gemspec
34
+ homepage: https://github.com/juanmanuelramallo/syslog_parser
35
+ licenses: []
36
+ metadata:
37
+ homepage_uri: https://github.com/juanmanuelramallo/syslog_parser
38
+ source_code_uri: https://github.com/juanmanuelramallo/syslog_parser
39
+ changelog_uri: https://github.com/juanmanuelramallo/syslog_parser/blob/master/CHANGELOG.md
40
+ post_install_message:
41
+ rdoc_options: []
42
+ require_paths:
43
+ - lib
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: 2.4.0
49
+ required_rubygems_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ requirements: []
55
+ rubygems_version: 3.2.15
56
+ signing_key:
57
+ specification_version: 4
58
+ summary: Syslog parser
59
+ test_files: []