systemd-journal 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: dfbc842565f8f2b0c02a231336a2398a52d5fd3e
4
- data.tar.gz: 55318a3e7b014bbff59acfb837363961ece1817b
3
+ metadata.gz: 62fda8140f386ef899a2e599ebd0b28dcc4d371e
4
+ data.tar.gz: 20856e68190c5cb1b62c79baeeeb90a1db5ac009
5
5
  SHA512:
6
- metadata.gz: 3a0251260147d45da969179dd6053eda8826cf08187e55efd2fa6d83c7994fe75ee82683c4ca9b80770cd3172d21299fe46f960a8bbe975f9e12f2e8fa8cfc62
7
- data.tar.gz: 96b8d2c07dae3dca254e36d6b2fefb6c190edd61749ff9a6e941056837fbc8e5c1650727633a900afed77bfd19108ac5d7766d0e14436951721ee494665c23c5
6
+ metadata.gz: 1501c0de4be3f35d44ed101e2c9f068beb66b14b9554eb61c382430bc1c946d5039ff55a71388ba7acca5a16eed898a25ed1d1164fcd95be1a3dbb24b6971c84
7
+ data.tar.gz: 8fbb64089d686aeabc530dbd207da49d47ac170fcac504d31291e277cec527f9a2ca957d78c4a68cbe72c253fc645ea8162b19b756e3934d34b9e92d24dfd2e3
@@ -0,0 +1,67 @@
1
+ require 'ffi'
2
+
3
+ module Systemd
4
+ module Id128
5
+
6
+ # Get the 128-bit hex string identifying the current machine.
7
+ # Can be used to filter a journal to show only messages originating
8
+ # from this machine.
9
+ # @example Filter journal to the current machine.
10
+ # j = Systemd::Journal.new
11
+ # j.add_match('_MACHINE_ID', Systemd::Id128.machine_id)
12
+ # @return [String] 128-bit hex string representing the current machine.
13
+ def self.machine_id
14
+ @machine_id ||= begin
15
+ ptr = FFI::MemoryPointer.new(Native::Id128, 1)
16
+ rc = Native.sd_id128_get_machine(ptr)
17
+ raise JournalError.new(rc) if rc < 0
18
+ Native::Id128.new(ptr).to_s
19
+ end
20
+ end
21
+
22
+ # Get the 128-bit hex string identifying the current system's current boot.
23
+ # Can be used to filter a journal to show only messages originating from
24
+ # the current boot.
25
+ # @example Filter journal to the current boot.
26
+ # j = Systemd::Journal.new
27
+ # j.add_match('_BOOT_ID', Systemd::Id128.boot_id)
28
+ # @return [String] 128-bit hex string representing the current boot.
29
+ def self.boot_id
30
+ @boot_id ||= begin
31
+ ptr = FFI::MemoryPointer.new(Native::Id128, 1)
32
+ rc = Native.sd_id128_get_boot(ptr)
33
+ raise JournalError.new(rc) if rc < 0
34
+ Native::Id128.new(ptr).to_s
35
+ end
36
+ end
37
+
38
+ # Get a random 128-bit hex string.
39
+ # @return [String] 128-bit random hex string.
40
+ def self.random
41
+ ptr = FFI::MemoryPointer.new(Native::Id128, 1)
42
+ rc = Native.sd_id128_randomize(ptr)
43
+ raise JournalError.new(rc) if rc < 0
44
+ Native::Id128.new(ptr).to_s
45
+ end
46
+
47
+ # providing bindings to the systemd-id128 library.
48
+ module Native
49
+ require 'ffi'
50
+ extend FFI::Library
51
+ ffi_lib %w[libsystemd-id128.so libsystemd-id128.so.0]
52
+
53
+ class Id128 < FFI::Union
54
+ layout :bytes, [:uint8, 16],
55
+ :dwords, [:uint32, 4],
56
+ :qwords, [:uint64, 2]
57
+
58
+ def to_s
59
+ ("%02x" * 16) % self[:bytes].to_a
60
+ end
61
+ end
62
+ attach_function :sd_id128_get_machine, [:pointer], :int
63
+ attach_function :sd_id128_get_boot, [:pointer], :int
64
+ attach_function :sd_id128_randomize, [:pointer], :int
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,18 @@
1
+ module Systemd
2
+ class Journal
3
+ # Fields directly passed by client programs and stored in the journal.
4
+ USER_FIELDS = %w{ MESSAGE MESSAGE_ID PRIORITY CODE_FILE CODE_LINE CODE_FUNC
5
+ ERRNO SYSLOG_FACILITY SYSLOG_IDENTIFIER SYSLOG_PID }
6
+
7
+ # Fields generated by the journal and added to each event.
8
+ TRUSTED_FIELDS = %w{ _PID _UID _GID _COMM _EXE _CMDLINE _AUDIT_SESSION
9
+ _AUDIT_LOGINUID _SYSTEMD_CGROUP _SYSTEMD_SESSION
10
+ _SYSTEMD_UNIT _SYSTEMD_USER_UNIT _SYSTEMD_OWNER_UID
11
+ _SELINUX_CONTEXT _SOURCE_REALTIME_TIMESTAMP _BOOT_ID
12
+ _MACHINE_ID _HOSTNAME _TRANSPORT }
13
+
14
+ # Fields used in messages originating from the kernel.
15
+ KERNEL_FIELDS = %w{ _KERNEL_DEVICE _KERNEL_SUBSYSTEM _UDEV_SYSNAME _UDEV_DEVNODE _UDEV_DEVLINK }
16
+
17
+ end
18
+ end
@@ -27,6 +27,11 @@ module Systemd
27
27
  attach_function :sd_journal_restart_data, [:pointer], :void
28
28
  attach_function :sd_journal_enumerate_data, [:pointer, :pointer, :pointer], :int
29
29
 
30
+ # querying
31
+ attach_function :sd_journal_query_unique, [:pointer, :string], :int
32
+ attach_function :sd_journal_enumerate_unique, [:pointer, :pointer, :pointer], :int
33
+ attach_function :sd_journal_restart_unique, [:pointer], :void
34
+
30
35
  # event notification
31
36
  enum :wake_reason, [
32
37
  :nop,
@@ -1,6 +1,6 @@
1
1
  module Systemd
2
2
  class Journal
3
3
  # The version of the systemd-journal gem.
4
- VERSION = '0.1.1'
4
+ VERSION = '0.1.2'
5
5
  end
6
6
  end
@@ -1,7 +1,9 @@
1
1
  require 'systemd/journal/native'
2
2
  require 'systemd/journal/flags'
3
3
  require 'systemd/journal/compat'
4
+ require 'systemd/journal/fields'
4
5
  require 'systemd/journal_error'
6
+ require 'systemd/id128'
5
7
 
6
8
  module Systemd
7
9
  # Class to allow interacting with the systemd journal.
@@ -154,11 +156,7 @@ module Systemd
154
156
  out_ptr = FFI::MemoryPointer.new(:pointer, 1)
155
157
  results = {}
156
158
 
157
- while
158
- rc = Native::sd_journal_enumerate_data(@ptr, out_ptr, len_ptr)
159
- raise JournalError.new(rc) if rc < 0
160
- break if rc == 0
161
-
159
+ while (rc = Native::sd_journal_enumerate_data(@ptr, out_ptr, len_ptr)) > 0
162
160
  len = read_size_t(len_ptr)
163
161
  key, value = out_ptr.read_pointer.read_string_length(len).split('=', 2)
164
162
  results[key] = value
@@ -166,6 +164,43 @@ module Systemd
166
164
  yield(key, value) if block_given?
167
165
  end
168
166
 
167
+ raise JournalError.new(rc) if rc < 0
168
+
169
+ results
170
+ end
171
+
172
+ # Get the list of unique values stored in the journal for the given field.
173
+ # If passed a block, each possible value will be yielded.
174
+ # @return [Array] the list of possible values.
175
+ # @example Fetch all possible boot ids from the journal
176
+ # j = Systemd::Journal.new
177
+ # j.query_unique('_BOOT_ID')
178
+ # @example Enumerate machine IDs with a block
179
+ # j = Systemd::Journal.new
180
+ # j.query_unique('_MACHINE_ID') do |machine_id|
181
+ # puts "found machine id #{machine_id}"
182
+ # end
183
+ def query_unique(field)
184
+ results = []
185
+ field = field.to_s.upcase
186
+ out_ptr = FFI::MemoryPointer.new(:pointer, 1)
187
+ len_ptr = FFI::MemoryPointer.new(:size_t, 1)
188
+
189
+ Native::sd_journal_restart_unique(@ptr)
190
+
191
+ if (rc = Native::sd_journal_query_unique(@ptr, field)) < 0
192
+ raise JournalError.new(rc)
193
+ end
194
+
195
+ while (rc = Native::sd_journal_enumerate_unique(@ptr, out_ptr, len_ptr)) > 0
196
+ len = read_size_t(len_ptr)
197
+ results << out_ptr.read_pointer.read_string_length(len).split('=', 2).last
198
+
199
+ yield results.last if block_given?
200
+ end
201
+
202
+ raise JournalError.new(rc) if rc < 0
203
+
169
204
  results
170
205
  end
171
206
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: systemd-journal
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Ledbetter
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-07-27 00:00:00.000000000 Z
12
+ date: 2013-08-29 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: ffi
@@ -41,8 +41,10 @@ files:
41
41
  - examples/journal_directory.rb
42
42
  - examples/ssh_watcher.rb
43
43
  - lib/systemd-journal.rb
44
+ - lib/systemd/id128.rb
44
45
  - lib/systemd/journal.rb
45
46
  - lib/systemd/journal/compat.rb
47
+ - lib/systemd/journal/fields.rb
46
48
  - lib/systemd/journal/flags.rb
47
49
  - lib/systemd/journal/native.rb
48
50
  - lib/systemd/journal/version.rb
@@ -67,7 +69,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
67
69
  version: '0'
68
70
  requirements: []
69
71
  rubyforge_project:
70
- rubygems_version: 2.0.3
72
+ rubygems_version: 2.0.5
71
73
  signing_key:
72
74
  specification_version: 4
73
75
  summary: Ruby bindings to libsystemd-journal