em-syslog 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/README +48 -0
  2. data/lib/em/syslog.rb +64 -0
  3. data/test.rb +33 -0
  4. metadata +82 -0
data/README ADDED
@@ -0,0 +1,48 @@
1
+ em-syslog
2
+ #########
3
+ Simple syslog integration into EventMachine at the class level.
4
+
5
+ Still needs work and testing.
6
+
7
+ To install:
8
+
9
+ git clone git://github.com/melito/em-syslog.git
10
+
11
+ -or-
12
+
13
+ sudo gem install melito-em-syslog -s http://gems.github.com
14
+
15
+ SYNOPSIS:
16
+
17
+ require 'em/syslog'
18
+
19
+ # Setup syslog
20
+ EM.syslog_setup('centralserver.com', 514)
21
+
22
+ # Send commands
23
+ EM.emergency('system is unusable')
24
+ EM.alert('action must be taken immediately')
25
+ EM.critical('critical conditions')
26
+ EM.error('error conditions')
27
+ EM.warning('warning conditions')
28
+ EM.notice('normal but significant conditions')
29
+ EM.informational('informational messages')
30
+ EM.info('informational messages (short name for the previous)')
31
+ EM.debug('debug-level messages')
32
+
33
+ BUGS:
34
+ kqueue doesn't seem to work. (Haven't tested epoll)
35
+
36
+ TODO:
37
+ Benchmark
38
+ Take some of the server examples and insert.
39
+ Make sure blocking is minimal/non-existant
40
+ Add support for syslog-ng
41
+ Refactor
42
+ Add support for TCP (syslog-ng)
43
+ Add support for TLS (syslog-ng)
44
+ Clean up API
45
+ Just improve everything.
46
+
47
+ FIXES:
48
+ Was creating a new datagram socket everytime a packet was sent out. Only use one now.
data/lib/em/syslog.rb ADDED
@@ -0,0 +1,64 @@
1
+ require "rubygems"
2
+ require "eventmachine"
3
+ require "socket"
4
+
5
+ module EventMachine
6
+ module Protocols
7
+ module Syslog
8
+ # THIEVERY: http://github.com/kpumuk/ruby_syslog
9
+ SEVERITIES = {
10
+ :emergency => 0, # system is unusable
11
+ :alert => 1, # action must be taken immediately
12
+ :critical => 2, # critical conditions
13
+ :error => 3, # error conditions
14
+ :warning => 4, # warning conditions
15
+ :notice => 5, # normal but significant condition
16
+ :informational => 6, # informational messages
17
+ :info => 6, # informational messages (short name for the previous)
18
+ :debug => 7 # debug-level messages
19
+ }
20
+
21
+ class << self
22
+ # THIEVERY: http://github.com/kpumuk/ruby_syslog
23
+ def log(severity, message, time = nil)
24
+ raise "Syslog Server not set" if EM.syslog_server.nil?
25
+
26
+ severity = SEVERITIES[severity] if severity.is_a? Symbol
27
+ time ||= Time.now
28
+ hostname = Socket.gethostname
29
+ day = time.strftime('%b %d').sub(/0(\d)/, ' \\1')
30
+ tag = "#{$0.split('/').last}[#{Process.pid}]"
31
+
32
+ send_msg "<#{0 * 8 + severity}>#{day} #{time.strftime('%T')} #{hostname} #{tag}: #{message}"
33
+ end
34
+
35
+ def send_msg(data)
36
+ EM.next_tick{
37
+ EM.syslog_sd.send_datagram(data, EM.syslog_server, EM.syslog_port)
38
+ }
39
+ end
40
+ private :send_msg
41
+
42
+ end
43
+
44
+ end
45
+ end
46
+
47
+ # Class methods for EM
48
+ class << self
49
+ attr_reader :syslog_server, :syslog_port, :syslog_sd
50
+ def syslog_setup(server, port=514)
51
+ @syslog_server, @syslog_port = server, port
52
+ @syslog_sd = EM.open_datagram_socket('0.0.0.0', 0) # FIXME: Dumb quick fix.
53
+ end
54
+
55
+ # THIEVERY: http://github.com/kpumuk/ruby_syslog
56
+ EM::P::Syslog::SEVERITIES.keys.each do |severity|
57
+ define_method severity do |message|
58
+ EM::P::Syslog.log(severity, message)
59
+ end
60
+ end
61
+
62
+ end
63
+
64
+ end
data/test.rb ADDED
@@ -0,0 +1,33 @@
1
+ require "lib/em/syslog"
2
+
3
+ #SYSLOG_SERVER = '192.168.249.134'
4
+ SYSLOG_SERVER = '0.0.0.0'
5
+ SYSLOG_PORT = 514
6
+
7
+ #EM.kqueue
8
+ #EM.epoll
9
+ EM.run {
10
+
11
+ # This is gonna be our 'syslog' server for testing to make sure we're getting the correct packets
12
+ #EM.open_datagram_socket SYSLOG_SERVER, SYSLOG_PORT do |c|
13
+ # def c.receive_data data
14
+ # p data
15
+ # end
16
+ #end
17
+
18
+ EM.syslog_setup(SYSLOG_SERVER, SYSLOG_PORT)
19
+
20
+ EM.add_periodic_timer(1) {
21
+
22
+ EM.emergency('system is unusable')
23
+ EM.alert('action must be taken immediately')
24
+ EM.critical('critical conditions')
25
+ EM.error('error conditions')
26
+ EM.warning('warning conditions')
27
+ EM.notice('normal but significant conditions')
28
+ EM.informational('informational messages')
29
+ EM.info('informational messages (short name for the previous)')
30
+ EM.debug('debug-level messages')
31
+ }
32
+
33
+ }
metadata ADDED
@@ -0,0 +1,82 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: em-syslog
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 2
10
+ version: 0.0.2
11
+ platform: ruby
12
+ authors:
13
+ - Mel Gray
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2009-06-30 00:00:00 -07:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: eventmachine
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 0
32
+ version: "0"
33
+ type: :runtime
34
+ version_requirements: *id001
35
+ description: Simple syslog integration into EventMachine at the class level.
36
+ email: melgray@gmail.com
37
+ executables: []
38
+
39
+ extensions: []
40
+
41
+ extra_rdoc_files: []
42
+
43
+ files:
44
+ - README
45
+ - lib/em/syslog.rb
46
+ - test.rb
47
+ has_rdoc: false
48
+ homepage: http://www.github.com/melito/em-syslog
49
+ licenses: []
50
+
51
+ post_install_message:
52
+ rdoc_options: []
53
+
54
+ require_paths:
55
+ - lib
56
+ required_ruby_version: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ hash: 3
62
+ segments:
63
+ - 0
64
+ version: "0"
65
+ required_rubygems_version: !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ hash: 3
71
+ segments:
72
+ - 0
73
+ version: "0"
74
+ requirements: []
75
+
76
+ rubyforge_project:
77
+ rubygems_version: 1.3.7
78
+ signing_key:
79
+ specification_version: 3
80
+ summary: Simple syslog integration into EventMachine at the class level.
81
+ test_files: []
82
+