syslog_protocol 0.9.0 → 0.9.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -4,5 +4,5 @@ require 'syslog_protocol/logger'
4
4
  require 'syslog_protocol/parser'
5
5
 
6
6
  module SyslogProtocol
7
- VERSION = '0.9.0'
7
+ VERSION = '0.9.1'
8
8
  end
@@ -2,7 +2,7 @@ module SyslogProtocol
2
2
  class Packet
3
3
  attr_reader :facility, :severity, :hostname, :tag
4
4
  attr_accessor :time, :content
5
-
5
+
6
6
  def to_s
7
7
  assemble
8
8
  end
@@ -12,12 +12,17 @@ module SyslogProtocol
12
12
  raise "Could not assemble packet without hostname, tag, facility, and severity"
13
13
  end
14
14
  data = "<#{pri}>#{generate_timestamp} #{@hostname} #{@tag}: #{@content}"
15
- while data.bytesize > 1024
16
- data = data[0..(data.length-2)]
15
+
16
+ if string_bytesize(data) > 1024
17
+ data = data.slice(0, 1024)
18
+ while string_bytesize(data) > 1024
19
+ data = data.slice(0, data.length - 1)
20
+ end
17
21
  end
22
+
18
23
  data
19
24
  end
20
-
25
+
21
26
  def facility=(f)
22
27
  if f.is_a? Integer
23
28
  if (0..23).include?(f)
@@ -103,7 +108,7 @@ module SyslogProtocol
103
108
  @facility = p / 8
104
109
  @severity = p - (@facility * 8)
105
110
  end
106
-
111
+
107
112
  def generate_timestamp
108
113
  time = @time || Time.now
109
114
  # The timestamp format requires that a day with fewer than 2 digits have
@@ -112,11 +117,19 @@ module SyslogProtocol
112
117
  day = day.sub(/^0/, ' ') if day =~ /^0\d/
113
118
  time.strftime("%b #{day} %H:%M:%S")
114
119
  end
115
-
120
+
121
+ if "".respond_to?(:bytesize)
122
+ def string_bytesize(string)
123
+ string.bytesize
124
+ end
125
+ else
126
+ def string_bytesize(string)
127
+ string.length
128
+ end
129
+ end
130
+
116
131
  SEVERITIES.each do |k,v|
117
- define_method("#{k}?") {SEVERITIES[k] == @severity}
132
+ define_method("#{k}?") { SEVERITIES[k] == @severity }
118
133
  end
119
-
120
134
  end
121
-
122
135
  end
@@ -13,7 +13,7 @@ Gem::Specification.new do |s|
13
13
  ## If your rubyforge_project name is different, then edit it and comment out
14
14
  ## the sub! line in the Rakefile
15
15
  s.name = 'syslog_protocol'
16
- s.version = '0.9.0'
16
+ s.version = '0.9.1'
17
17
  s.date = "2009-08-01"
18
18
  # s.rubyforge_project = 'syslog_protocol'
19
19
 
@@ -10,4 +10,4 @@ end
10
10
  require 'bacon'
11
11
  Bacon.summary_at_exit
12
12
 
13
- require 'syslog_proto'
13
+ require 'syslog_protocol'
@@ -1,15 +1,15 @@
1
1
  require File.expand_path('../helper', __FILE__)
2
2
 
3
3
  describe "syslog logger" do
4
-
4
+
5
5
  it "create a new logger with hostname and facility" do
6
- lambda {@logger = SyslogProto::Logger.new("space_station", 'test', "local0")}.should.not.raise
6
+ lambda {@logger = SyslogProtocol::Logger.new("space_station", 'test', "local0")}.should.not.raise
7
7
  end
8
-
8
+
9
9
  it "hostname and facility must conform to the requirements of a Packet" do
10
- lambda {SyslogProto::Logger.new("space station", "some shit", 'test test')}.should.raise ArgumentError
10
+ lambda {SyslogProtocol::Logger.new("space station", "some shit", 'test test')}.should.raise ArgumentError
11
11
  end
12
-
12
+
13
13
  it "generates packets" do
14
14
  # We have to set a time so we have a consistant timestamp to check against..
15
15
  p = @logger.instance_variable_get("@packet")
@@ -24,5 +24,5 @@ describe "syslog logger" do
24
24
  @logger.alert("LEAKING ATMOSPHERE").should.equal "<129>#{ts} space_station test: LEAKING ATMOSPHERE"
25
25
  @logger.emerg("LEAKING ASTRONAUTS WE ARE DONE").should.equal "<128>#{ts} space_station test: LEAKING ASTRONAUTS WE ARE DONE"
26
26
  end
27
-
27
+
28
28
  end
@@ -1,23 +1,23 @@
1
1
  require File.expand_path('../helper', __FILE__)
2
2
 
3
3
  describe "a syslog packet" do
4
-
5
- @p = SyslogProto::Packet.new
6
-
4
+
5
+ @p = SyslogProtocol::Packet.new
6
+
7
7
  it "should embarrass a person who does not set the fields" do
8
8
  lambda { @p.to_s }.should.raise RuntimeError
9
9
  end
10
-
10
+
11
11
  it "hostname may not be omitted" do
12
12
  lambda {@p.hostname = ""}.should.raise ArgumentError
13
13
  end
14
-
14
+
15
15
  it "hostname may only contain ASCII characters 33-126 (no spaces!)" do
16
16
  lambda {@p.hostname = "linux box"}.should.raise ArgumentError
17
17
  lambda {@p.hostname = "\000" + "linuxbox"}.should.raise ArgumentError
18
18
  lambda {@p.hostname = "space_station"}.should.not.raise
19
19
  end
20
-
20
+
21
21
  it 'tag may only contain ASCII characters 33-126 (no spaces!)' do
22
22
  lambda {@p.tag = "linux box"}.should.raise ArgumentError
23
23
  lambda {@p.tag = "\000" + "linuxbox"}.should.raise ArgumentError
@@ -27,66 +27,70 @@ describe "a syslog packet" do
27
27
  it "facility may only be set within 0-23 or with a proper string name" do
28
28
  lambda {@p.facility = 666}.should.raise ArgumentError
29
29
  lambda {@p.facility = "mir space station"}.should.raise ArgumentError
30
-
30
+
31
31
  lambda {@p.facility = 16}.should.not.raise
32
32
  @p.facility.should.equal 16
33
33
  lambda {@p.facility = 'local0'}.should.not.raise
34
34
  @p.facility.should.equal 16
35
35
  end
36
-
36
+
37
37
  it "severity may only be set within 0-7 or with a proper string name" do
38
38
  lambda {@p.severity = 9876}.should.raise ArgumentError
39
39
  lambda {@p.severity = "omgbroken"}.should.raise ArgumentError
40
-
40
+
41
41
  lambda {@p.severity = 6}.should.not.raise
42
42
  @p.severity.should.equal 6
43
43
  lambda {@p.severity = 'info'}.should.not.raise
44
44
  @p.severity.should.equal 6
45
45
  end
46
-
46
+
47
47
  it "severity can be checked using 'some_severity?' methods" do
48
48
  @p.info?.should.equal true
49
49
  @p.alert?.should.equal false
50
50
  @p.emerg?.should.equal false
51
51
  end
52
-
52
+
53
53
  it "PRI is calculated from the facility and severity" do
54
54
  @p.pri.should.equal 134
55
55
  end
56
-
56
+
57
57
  it "PRI may only be within 0-191" do
58
58
  lambda {@p.pri = 22331}.should.raise ArgumentError
59
59
  lambda {@p.pri = "foo"}.should.raise ArgumentError
60
60
  end
61
-
61
+
62
62
  it "facility and severity are deduced and set from setting a valid PRI" do
63
63
  @p.pri = 165
64
64
  @p.severity.should.equal 5
65
65
  @p.facility.should.equal 20
66
66
  end
67
-
67
+
68
68
  it "return the proper names for facility and severity" do
69
69
  @p.severity_name.should.equal 'notice'
70
70
  @p.facility_name.should.equal 'local4'
71
71
  end
72
-
72
+
73
73
  it "set a message, which apparently can be anything" do
74
74
  @p.content = "exploring ze black hole"
75
75
  @p.content.should.equal "exploring ze black hole"
76
76
  end
77
-
77
+
78
78
  it "timestamp must conform to the retarded format" do
79
79
  @p.generate_timestamp.should.match /(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\s(\s|[1-9])\d\s\d\d:\d\d:\d\d/
80
80
  end
81
-
81
+
82
82
  it "use the current time and assemble the packet" do
83
83
  timestamp = @p.generate_timestamp
84
84
  @p.to_s.should.equal "<165>#{timestamp} space_station test: exploring ze black hole"
85
85
  end
86
-
86
+
87
87
  it "packets larger than 1024 will be truncated" do
88
88
  @p.content = "space warp" * 1000
89
- @p.to_s.bytesize.should.equal 1024
89
+ if "".respond_to?(:bytesize)
90
+ @p.to_s.bytesize.should.equal 1024
91
+ else
92
+ @p.to_s.size.should.equal 1024
93
+ end
90
94
  end
91
-
95
+
92
96
  end
@@ -1,9 +1,9 @@
1
1
  require File.expand_path('../helper', __FILE__)
2
2
 
3
3
  describe "syslog packet parser" do
4
-
4
+
5
5
  it "parse some valid packets" do
6
- p = SyslogProto.parse("<34>Oct 11 22:14:15 mymachine su: 'su root' failed for lonvick on /dev/pts/8")
6
+ p = SyslogProtocol.parse("<34>Oct 11 22:14:15 mymachine su: 'su root' failed for lonvick on /dev/pts/8")
7
7
  p.facility.should.equal 4
8
8
  p.severity.should.equal 2
9
9
  p.pri.should.equal 34
@@ -11,8 +11,8 @@ describe "syslog packet parser" do
11
11
  p.tag.should.equal 'su'
12
12
  p.content.should.equal "'su root' failed for lonvick on /dev/pts/8"
13
13
  p.time.should.equal Time.parse("Oct 11 22:14:15")
14
-
15
- p = SyslogProto.parse("<13>Feb 5 17:32:18 10.0.0.99 test: Use the BFG!")
14
+
15
+ p = SyslogProtocol.parse("<13>Feb 5 17:32:18 10.0.0.99 test: Use the BFG!")
16
16
  p.facility.should.equal 1
17
17
  p.severity.should.equal 5
18
18
  p.pri.should.equal 13
@@ -21,27 +21,27 @@ describe "syslog packet parser" do
21
21
  p.content.should.equal "Use the BFG!"
22
22
  p.time.should.equal Time.parse("Feb 5 17:32:18")
23
23
  end
24
-
24
+
25
25
  it "treat a packet with no valid PRI as all content, setting defaults" do
26
- p = SyslogProto.parse("nomnom")
26
+ p = SyslogProtocol.parse("nomnom")
27
27
  p.facility.should.equal 1
28
28
  p.severity.should.equal 5
29
29
  p.pri.should.equal 13
30
30
  p.hostname.should.equal 'unknown'
31
31
  p.content.should.equal "nomnom"
32
32
  end
33
-
33
+
34
34
  it "PRI with preceding 0's shall be considered invalid" do
35
- p = SyslogProto.parse("<045>Oct 11 22:14:15 space_station my PRI is not valid")
35
+ p = SyslogProtocol.parse("<045>Oct 11 22:14:15 space_station my PRI is not valid")
36
36
  p.facility.should.equal 1
37
37
  p.severity.should.equal 5
38
38
  p.pri.should.equal 13
39
39
  p.hostname.should.equal 'unknown'
40
40
  p.content.should.equal "<045>Oct 11 22:14:15 space_station my PRI is not valid"
41
41
  end
42
-
42
+
43
43
  it "allow the user to pass an origin to be used as the hostname if packet is invalid" do
44
- p = SyslogProto.parse("<045>Oct 11 22:14:15 space_station my PRI is not valid", '127.0.0.1')
44
+ p = SyslogProtocol.parse("<045>Oct 11 22:14:15 space_station my PRI is not valid", '127.0.0.1')
45
45
  p.facility.should.equal 1
46
46
  p.severity.should.equal 5
47
47
  p.pri.should.equal 13
metadata CHANGED
@@ -1,13 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: syslog_protocol
3
3
  version: !ruby/object:Gem::Version
4
- hash: 59
5
4
  prerelease: false
6
5
  segments:
7
6
  - 0
8
7
  - 9
9
- - 0
10
- version: 0.9.0
8
+ - 1
9
+ version: 0.9.1
11
10
  platform: ruby
12
11
  authors:
13
12
  - Jake Douglas
@@ -23,11 +22,9 @@ dependencies:
23
22
  name: bacon
24
23
  prerelease: false
25
24
  requirement: &id001 !ruby/object:Gem::Requirement
26
- none: false
27
25
  requirements:
28
26
  - - ~>
29
27
  - !ruby/object:Gem::Version
30
- hash: 19
31
28
  segments:
32
29
  - 1
33
30
  - 1
@@ -69,27 +66,23 @@ rdoc_options:
69
66
  require_paths:
70
67
  - lib
71
68
  required_ruby_version: !ruby/object:Gem::Requirement
72
- none: false
73
69
  requirements:
74
70
  - - ">="
75
71
  - !ruby/object:Gem::Version
76
- hash: 3
77
72
  segments:
78
73
  - 0
79
74
  version: "0"
80
75
  required_rubygems_version: !ruby/object:Gem::Requirement
81
- none: false
82
76
  requirements:
83
77
  - - ">="
84
78
  - !ruby/object:Gem::Version
85
- hash: 3
86
79
  segments:
87
80
  - 0
88
81
  version: "0"
89
82
  requirements: []
90
83
 
91
84
  rubyforge_project:
92
- rubygems_version: 1.3.7
85
+ rubygems_version: 1.3.6
93
86
  signing_key:
94
87
  specification_version: 2
95
88
  summary: Syslog protocol parser and generator