From da1e55f3c442b80508c613e3619e5033f38247d6 Mon Sep 17 00:00:00 2001 From: Steffen Siering Date: Thu, 2 Jul 2020 14:18:45 +0200 Subject: [PATCH] introduce journalbeat/pkg in order to provide reusable shared code (#19581) This PR moves some functionality into journalbeat/pkg for reuse: - field conversion - adding field filters - low-level reader The change is mostly moving code around with minor cleanups (e.g. by introducing more dedicated types and tests). Functionality from original read is split into: reader, matchers, and conversion --- NOTICE.txt | 422 +++++++++--------- go.mod | 1 + journalbeat/config/config.go | 38 -- journalbeat/config/config_test.go | 94 ---- journalbeat/input/config.go | 13 +- journalbeat/pkg/journalfield/conv.go | 119 +++++ journalbeat/pkg/journalfield/conv_test.go | 119 +++++ journalbeat/pkg/journalfield/default.go | 88 ++++ journalbeat/pkg/journalfield/default_other.go | 46 ++ journalbeat/pkg/journalfield/matcher.go | 120 +++++ journalbeat/pkg/journalfield/matcher_test.go | 80 ++++ journalbeat/pkg/journalread/mode.go | 57 +++ journalbeat/pkg/journalread/mode_test.go | 58 +++ journalbeat/pkg/journalread/reader.go | 199 +++++++++ journalbeat/reader/config.go | 9 +- journalbeat/reader/fields.go | 75 ---- journalbeat/reader/journal.go | 269 ++--------- journalbeat/reader/journal_test.go | 184 -------- 18 files changed, 1159 insertions(+), 832 deletions(-) delete mode 100644 journalbeat/config/config_test.go create mode 100644 journalbeat/pkg/journalfield/conv.go create mode 100644 journalbeat/pkg/journalfield/conv_test.go create mode 100644 journalbeat/pkg/journalfield/default.go create mode 100644 journalbeat/pkg/journalfield/default_other.go create mode 100644 journalbeat/pkg/journalfield/matcher.go create mode 100644 journalbeat/pkg/journalfield/matcher_test.go create mode 100644 journalbeat/pkg/journalread/mode.go create mode 100644 journalbeat/pkg/journalread/mode_test.go create mode 100644 journalbeat/pkg/journalread/reader.go delete mode 100644 journalbeat/reader/journal_test.go diff --git a/NOTICE.txt b/NOTICE.txt index 8a2746cb564..97ddd49332d 100644 --- a/NOTICE.txt +++ b/NOTICE.txt @@ -13547,6 +13547,217 @@ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +-------------------------------------------------------------------------------- +Dependency : github.com/urso/sderr +Version: v0.0.0-20200210124243-c2a16f3d43ec +Licence type (autodetected): Apache-2.0 +-------------------------------------------------------------------------------- + +Contents of probable licence file $GOMODCACHE/github.com/urso/sderr@v0.0.0-20200210124243-c2a16f3d43ec/LICENSE: + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + + -------------------------------------------------------------------------------- Dependency : github.com/vmware/govmomi Version: v0.0.0-20170802214208-2cad15190b41 @@ -36368,217 +36579,6 @@ Contents of probable licence file $GOMODCACHE/github.com/urso/qcgen@v0.0.0-20180 limitations under the License. --------------------------------------------------------------------------------- -Dependency : github.com/urso/sderr -Version: v0.0.0-20200210124243-c2a16f3d43ec -Licence type (autodetected): Apache-2.0 --------------------------------------------------------------------------------- - -Contents of probable licence file $GOMODCACHE/github.com/urso/sderr@v0.0.0-20200210124243-c2a16f3d43ec/LICENSE: - - Apache License - Version 2.0, January 2004 - http://www.apache.org/licenses/ - - TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION - - 1. Definitions. - - "License" shall mean the terms and conditions for use, reproduction, - and distribution as defined by Sections 1 through 9 of this document. - - "Licensor" shall mean the copyright owner or entity authorized by - the copyright owner that is granting the License. - - "Legal Entity" shall mean the union of the acting entity and all - other entities that control, are controlled by, or are under common - control with that entity. For the purposes of this definition, - "control" means (i) the power, direct or indirect, to cause the - direction or management of such entity, whether by contract or - otherwise, or (ii) ownership of fifty percent (50%) or more of the - outstanding shares, or (iii) beneficial ownership of such entity. - - "You" (or "Your") shall mean an individual or Legal Entity - exercising permissions granted by this License. - - "Source" form shall mean the preferred form for making modifications, - including but not limited to software source code, documentation - source, and configuration files. - - "Object" form shall mean any form resulting from mechanical - transformation or translation of a Source form, including but - not limited to compiled object code, generated documentation, - and conversions to other media types. - - "Work" shall mean the work of authorship, whether in Source or - Object form, made available under the License, as indicated by a - copyright notice that is included in or attached to the work - (an example is provided in the Appendix below). - - "Derivative Works" shall mean any work, whether in Source or Object - form, that is based on (or derived from) the Work and for which the - editorial revisions, annotations, elaborations, or other modifications - represent, as a whole, an original work of authorship. For the purposes - of this License, Derivative Works shall not include works that remain - separable from, or merely link (or bind by name) to the interfaces of, - the Work and Derivative Works thereof. - - "Contribution" shall mean any work of authorship, including - the original version of the Work and any modifications or additions - to that Work or Derivative Works thereof, that is intentionally - submitted to Licensor for inclusion in the Work by the copyright owner - or by an individual or Legal Entity authorized to submit on behalf of - the copyright owner. For the purposes of this definition, "submitted" - means any form of electronic, verbal, or written communication sent - to the Licensor or its representatives, including but not limited to - communication on electronic mailing lists, source code control systems, - and issue tracking systems that are managed by, or on behalf of, the - Licensor for the purpose of discussing and improving the Work, but - excluding communication that is conspicuously marked or otherwise - designated in writing by the copyright owner as "Not a Contribution." - - "Contributor" shall mean Licensor and any individual or Legal Entity - on behalf of whom a Contribution has been received by Licensor and - subsequently incorporated within the Work. - - 2. Grant of Copyright License. Subject to the terms and conditions of - this License, each Contributor hereby grants to You a perpetual, - worldwide, non-exclusive, no-charge, royalty-free, irrevocable - copyright license to reproduce, prepare Derivative Works of, - publicly display, publicly perform, sublicense, and distribute the - Work and such Derivative Works in Source or Object form. - - 3. Grant of Patent License. Subject to the terms and conditions of - this License, each Contributor hereby grants to You a perpetual, - worldwide, non-exclusive, no-charge, royalty-free, irrevocable - (except as stated in this section) patent license to make, have made, - use, offer to sell, sell, import, and otherwise transfer the Work, - where such license applies only to those patent claims licensable - by such Contributor that are necessarily infringed by their - Contribution(s) alone or by combination of their Contribution(s) - with the Work to which such Contribution(s) was submitted. If You - institute patent litigation against any entity (including a - cross-claim or counterclaim in a lawsuit) alleging that the Work - or a Contribution incorporated within the Work constitutes direct - or contributory patent infringement, then any patent licenses - granted to You under this License for that Work shall terminate - as of the date such litigation is filed. - - 4. Redistribution. You may reproduce and distribute copies of the - Work or Derivative Works thereof in any medium, with or without - modifications, and in Source or Object form, provided that You - meet the following conditions: - - (a) You must give any other recipients of the Work or - Derivative Works a copy of this License; and - - (b) You must cause any modified files to carry prominent notices - stating that You changed the files; and - - (c) You must retain, in the Source form of any Derivative Works - that You distribute, all copyright, patent, trademark, and - attribution notices from the Source form of the Work, - excluding those notices that do not pertain to any part of - the Derivative Works; and - - (d) If the Work includes a "NOTICE" text file as part of its - distribution, then any Derivative Works that You distribute must - include a readable copy of the attribution notices contained - within such NOTICE file, excluding those notices that do not - pertain to any part of the Derivative Works, in at least one - of the following places: within a NOTICE text file distributed - as part of the Derivative Works; within the Source form or - documentation, if provided along with the Derivative Works; or, - within a display generated by the Derivative Works, if and - wherever such third-party notices normally appear. The contents - of the NOTICE file are for informational purposes only and - do not modify the License. You may add Your own attribution - notices within Derivative Works that You distribute, alongside - or as an addendum to the NOTICE text from the Work, provided - that such additional attribution notices cannot be construed - as modifying the License. - - You may add Your own copyright statement to Your modifications and - may provide additional or different license terms and conditions - for use, reproduction, or distribution of Your modifications, or - for any such Derivative Works as a whole, provided Your use, - reproduction, and distribution of the Work otherwise complies with - the conditions stated in this License. - - 5. Submission of Contributions. Unless You explicitly state otherwise, - any Contribution intentionally submitted for inclusion in the Work - by You to the Licensor shall be under the terms and conditions of - this License, without any additional terms or conditions. - Notwithstanding the above, nothing herein shall supersede or modify - the terms of any separate license agreement you may have executed - with Licensor regarding such Contributions. - - 6. Trademarks. This License does not grant permission to use the trade - names, trademarks, service marks, or product names of the Licensor, - except as required for reasonable and customary use in describing the - origin of the Work and reproducing the content of the NOTICE file. - - 7. Disclaimer of Warranty. Unless required by applicable law or - agreed to in writing, Licensor provides the Work (and each - Contributor provides its Contributions) on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or - implied, including, without limitation, any warranties or conditions - of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A - PARTICULAR PURPOSE. You are solely responsible for determining the - appropriateness of using or redistributing the Work and assume any - risks associated with Your exercise of permissions under this License. - - 8. Limitation of Liability. In no event and under no legal theory, - whether in tort (including negligence), contract, or otherwise, - unless required by applicable law (such as deliberate and grossly - negligent acts) or agreed to in writing, shall any Contributor be - liable to You for damages, including any direct, indirect, special, - incidental, or consequential damages of any character arising as a - result of this License or out of the use or inability to use the - Work (including but not limited to damages for loss of goodwill, - work stoppage, computer failure or malfunction, or any and all - other commercial damages or losses), even if such Contributor - has been advised of the possibility of such damages. - - 9. Accepting Warranty or Additional Liability. While redistributing - the Work or Derivative Works thereof, You may choose to offer, - and charge a fee for, acceptance of support, warranty, indemnity, - or other liability obligations and/or rights consistent with this - License. However, in accepting such obligations, You may act only - on Your own behalf and on Your sole responsibility, not on behalf - of any other Contributor, and only if You agree to indemnify, - defend, and hold each Contributor harmless for any liability - incurred by, or claims asserted against, such Contributor by reason - of your accepting any such warranty or additional liability. - - END OF TERMS AND CONDITIONS - - APPENDIX: How to apply the Apache License to your work. - - To apply the Apache License to your work, attach the following - boilerplate notice, with the fields enclosed by brackets "[]" - replaced with your own identifying information. (Don't include - the brackets!) The text should be enclosed in the appropriate - comment syntax for the file format. We also recommend that a - file or class name and description of purpose be included on the - same "printed page" as the copyright notice for easier - identification within third-party archives. - - Copyright [yyyy] [name of copyright owner] - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. - - -------------------------------------------------------------------------------- Dependency : github.com/vbatts/tar-split Version: v0.11.1 diff --git a/go.mod b/go.mod index aa84ec00f94..44231c67d65 100644 --- a/go.mod +++ b/go.mod @@ -143,6 +143,7 @@ require ( github.com/stretchr/testify v1.6.1 github.com/tsg/go-daemon v0.0.0-20200207173439-e704b93fd89b github.com/tsg/gopacket v0.0.0-20200626092518-2ab8e397a786 + github.com/urso/sderr v0.0.0-20200210124243-c2a16f3d43ec github.com/vmware/govmomi v0.0.0-20170802214208-2cad15190b41 github.com/xdg/scram v0.0.0-20180814205039-7eeb5667e42c github.com/yuin/gopher-lua v0.0.0-20170403160031-b402f3114ec7 // indirect diff --git a/journalbeat/config/config.go b/journalbeat/config/config.go index a5d9127641d..1771a2a4e7c 100644 --- a/journalbeat/config/config.go +++ b/journalbeat/config/config.go @@ -21,56 +21,18 @@ package config import ( - "fmt" - "github.com/elastic/beats/v7/libbeat/common" ) -// SeekMode is specifies how a journal is read -type SeekMode uint8 - // Config stores the configuration of Journalbeat type Config struct { Inputs []*common.Config `config:"inputs"` RegistryFile string `config:"registry_file"` } -const ( - // SeekInvalid is an invalid value for seek - SeekInvalid SeekMode = iota - // SeekHead option seeks to the head of a journal - SeekHead - // SeekTail option seeks to the tail of a journal - SeekTail - // SeekCursor option seeks to the position specified in the cursor - SeekCursor - - seekHeadStr = "head" - seekTailStr = "tail" - seekCursorStr = "cursor" -) - var ( // DefaultConfig are the defaults of a Journalbeat instance DefaultConfig = Config{ RegistryFile: "registry", } - - seekModes = map[string]SeekMode{ - seekHeadStr: SeekHead, - seekTailStr: SeekTail, - seekCursorStr: SeekCursor, - } ) - -// Unpack validates and unpack "seek" config option -func (m *SeekMode) Unpack(value string) error { - mode, ok := seekModes[value] - if !ok { - return fmt.Errorf("invalid seek mode '%s'", value) - } - - *m = mode - - return nil -} diff --git a/journalbeat/config/config_test.go b/journalbeat/config/config_test.go deleted file mode 100644 index f842b812b1f..00000000000 --- a/journalbeat/config/config_test.go +++ /dev/null @@ -1,94 +0,0 @@ -// Licensed to Elasticsearch B.V. under one or more contributor -// license agreements. See the NOTICE file distributed with -// this work for additional information regarding copyright -// ownership. Elasticsearch B.V. licenses this file to you under -// the Apache License, Version 2.0 (the "License"); you may -// not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, -// software distributed under the License is distributed on an -// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -// KIND, either express or implied. See the License for the -// specific language governing permissions and limitations -// under the License. - -// +build !integration - -package config - -import ( - "testing" -) - -func TestUnpack(t *testing.T) { - - tests := []struct { - mode SeekMode - modeStr string - }{ - { - mode: SeekHead, - modeStr: seekHeadStr, - }, - { - mode: SeekTail, - modeStr: seekTailStr, - }, - { - mode: SeekCursor, - modeStr: seekCursorStr, - }, - } - - for _, tc := range tests { - tc := tc - - t.Run(tc.modeStr, func(t *testing.T) { - t.Parallel() - - m := SeekInvalid - err := m.Unpack(tc.modeStr) - if err != nil { - t.Fatal(err) - } - - if m != tc.mode { - t.Errorf("wrong mode, expected %v, got %v", tc.mode, m) - } - }) - } -} - -func TestUnpackFailure(t *testing.T) { - - tests := []struct { - modeStr string - }{ - { - modeStr: "invalid", - }, - { - modeStr: "", - }, - { - modeStr: "unknown", - }, - } - - for _, tc := range tests { - tc := tc - - t.Run(tc.modeStr, func(t *testing.T) { - t.Parallel() - - m := SeekInvalid - err := m.Unpack(tc.modeStr) - if err == nil { - t.Errorf("an error was expected, got %v", m) - } - }) - } -} diff --git a/journalbeat/input/config.go b/journalbeat/input/config.go index 63c31ccfce7..6c202030b35 100644 --- a/journalbeat/input/config.go +++ b/journalbeat/input/config.go @@ -20,7 +20,8 @@ package input import ( "time" - "github.com/elastic/beats/v7/journalbeat/config" + "github.com/elastic/beats/v7/journalbeat/pkg/journalfield" + "github.com/elastic/beats/v7/journalbeat/pkg/journalread" "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/common/fmtstr" "github.com/elastic/beats/v7/libbeat/processors" @@ -38,11 +39,11 @@ type Config struct { // MaxBackoff is the limit of the backoff time. MaxBackoff time.Duration `config:"max_backoff" validate:"min=0,nonzero"` // Seek is the method to read from journals. - Seek config.SeekMode `config:"seek"` + Seek journalread.SeekMode `config:"seek"` // CursorSeekFallback sets where to seek if registry file is not available. - CursorSeekFallback config.SeekMode `config:"cursor_seek_fallback"` + CursorSeekFallback journalread.SeekMode `config:"cursor_seek_fallback"` // Matches store the key value pairs to match entries. - Matches []string `config:"include_matches"` + Matches []journalfield.Matcher `config:"include_matches"` // SaveRemoteHostname defines if the original source of the entry needs to be saved. SaveRemoteHostname bool `config:"save_remote_hostname"` @@ -59,8 +60,8 @@ var ( DefaultConfig = Config{ Backoff: 1 * time.Second, MaxBackoff: 20 * time.Second, - Seek: config.SeekCursor, - CursorSeekFallback: config.SeekHead, + Seek: journalread.SeekCursor, + CursorSeekFallback: journalread.SeekHead, SaveRemoteHostname: false, } ) diff --git a/journalbeat/pkg/journalfield/conv.go b/journalbeat/pkg/journalfield/conv.go new file mode 100644 index 00000000000..47214c3f91d --- /dev/null +++ b/journalbeat/pkg/journalfield/conv.go @@ -0,0 +1,119 @@ +// Licensed to Elasticsearch B.V. under one or more contributor +// license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright +// ownership. Elasticsearch B.V. licenses this file to you under +// the Apache License, Version 2.0 (the "License"); you may +// not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package journalfield + +import ( + "fmt" + "strconv" + "strings" + + "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/beats/v7/libbeat/logp" +) + +// FieldConversion provides the mappings and conversion rules for raw fields of journald entries. +type FieldConversion map[string]Conversion + +// Conversion configures the conversion rules for a field. +type Conversion struct { + Names []string + IsInteger bool + Dropped bool +} + +// Converter applis configured conversion rules to journald entries, producing +// a new common.MapStr. +type Converter struct { + log *logp.Logger + conversions FieldConversion +} + +// NewConverter creates a new Converter from the given conversion rules. If +// conversions is nil, internal default conversion rules will be applied. +func NewConverter(log *logp.Logger, conversions FieldConversion) *Converter { + if conversions == nil { + conversions = journaldEventFields + } + + return &Converter{log: log, conversions: conversions} +} + +// Convert creates a common.MapStr from the raw fields by applying the +// configured conversion rules. +// Field type conversion errors are logged to at debug level and the original +// value is added to the map. +func (c *Converter) Convert(entryFields map[string]string) common.MapStr { + fields := common.MapStr{} + var custom common.MapStr + + for entryKey, v := range entryFields { + if fieldConversionInfo, ok := c.conversions[entryKey]; !ok { + if custom == nil { + custom = common.MapStr{} + } + normalized := strings.ToLower(strings.TrimLeft(entryKey, "_")) + custom.Put(normalized, v) + } else if !fieldConversionInfo.Dropped { + value, err := convertValue(fieldConversionInfo, v) + if err != nil { + value = v + c.log.Debugf("Journald mapping error: %v", err) + } + for _, name := range fieldConversionInfo.Names { + fields.Put(name, value) + } + } + } + + if len(custom) != 0 { + fields.Put("journald.custom", custom) + } + + return fields +} + +func convertValue(fc Conversion, value string) (interface{}, error) { + if fc.IsInteger { + v, err := strconv.ParseInt(value, 10, 64) + if err != nil { + // On some versions of systemd the 'syslog.pid' can contain the username + // appended to the end of the pid. In most cases this does not occur + // but in the cases that it does, this tries to strip ',\w*' from the + // value and then perform the conversion. + s := strings.Split(value, ",") + v, err = strconv.ParseInt(s[0], 10, 64) + if err != nil { + return value, fmt.Errorf("failed to convert field %s \"%v\" to int: %v", fc.Names[0], value, err) + } + } + return v, nil + } + return value, nil +} + +// helpers for creating a field conversion table. + +var ignoredField = Conversion{Dropped: true} + +func text(names ...string) Conversion { + return Conversion{Names: names, IsInteger: false, Dropped: false} +} + +func integer(names ...string) Conversion { + return Conversion{Names: names, IsInteger: true, Dropped: false} +} diff --git a/journalbeat/pkg/journalfield/conv_test.go b/journalbeat/pkg/journalfield/conv_test.go new file mode 100644 index 00000000000..a6514a95545 --- /dev/null +++ b/journalbeat/pkg/journalfield/conv_test.go @@ -0,0 +1,119 @@ +// Licensed to Elasticsearch B.V. under one or more contributor +// license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright +// ownership. Elasticsearch B.V. licenses this file to you under +// the Apache License, Version 2.0 (the "License"); you may +// not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +//+build linux,cgo + +package journalfield + +import ( + "testing" + + "github.com/coreos/go-systemd/v22/sdjournal" + "github.com/stretchr/testify/assert" + + "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/beats/v7/libbeat/logp" +) + +func TestConversion(t *testing.T) { + tests := map[string]struct { + fields map[string]string + want common.MapStr + }{ + "field name from fields.go": { + fields: map[string]string{ + sdjournal.SD_JOURNAL_FIELD_BOOT_ID: "123456", + }, + want: common.MapStr{ + "host": common.MapStr{ + "boot_id": "123456", + }, + }, + }, + "'syslog.pid' field without user append": { + fields: map[string]string{ + sdjournal.SD_JOURNAL_FIELD_SYSLOG_PID: "123456", + }, + want: common.MapStr{ + "syslog": common.MapStr{ + "pid": int64(123456), + }, + }, + }, + "'syslog.priority' field with junk": { + fields: map[string]string{ + sdjournal.SD_JOURNAL_FIELD_PRIORITY: "123456, ", + }, + want: common.MapStr{ + "syslog": common.MapStr{ + "priority": int64(123456), + }, + "log": common.MapStr{ + "syslog": common.MapStr{ + "priority": int64(123456), + }, + }, + }, + }, + "'syslog.pid' field with user append": { + fields: map[string]string{ + sdjournal.SD_JOURNAL_FIELD_SYSLOG_PID: "123456,root", + }, + want: common.MapStr{ + "syslog": common.MapStr{ + "pid": int64(123456), + }, + }, + }, + "'syslog.pid' field empty": { + fields: map[string]string{ + sdjournal.SD_JOURNAL_FIELD_SYSLOG_PID: "", + }, + want: common.MapStr{ + "syslog": common.MapStr{ + "pid": "", + }, + }, + }, + "custom field": { + fields: map[string]string{ + "my_custom_field": "value", + }, + want: common.MapStr{ + "journald": common.MapStr{ + "custom": common.MapStr{ + "my_custom_field": "value", + }, + }, + }, + }, + "dropped field": { + fields: map[string]string{ + "_SOURCE_MONOTONIC_TIMESTAMP": "value", + }, + want: common.MapStr{}, + }, + } + + for name, test := range tests { + t.Run(name, func(t *testing.T) { + log := logp.NewLogger("test") + converted := NewConverter(log, nil).Convert(test.fields) + assert.Equal(t, test.want, converted) + }) + } +} diff --git a/journalbeat/pkg/journalfield/default.go b/journalbeat/pkg/journalfield/default.go new file mode 100644 index 00000000000..a8b3860e956 --- /dev/null +++ b/journalbeat/pkg/journalfield/default.go @@ -0,0 +1,88 @@ +// Licensed to Elasticsearch B.V. under one or more contributor +// license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright +// ownership. Elasticsearch B.V. licenses this file to you under +// the Apache License, Version 2.0 (the "License"); you may +// not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +//+build linux,cgo + +package journalfield + +import "github.com/coreos/go-systemd/v22/sdjournal" + +// journaldEventFields provides default field mappings and conversions rules. +var journaldEventFields = FieldConversion{ + // provided by systemd journal + "COREDUMP_UNIT": text("journald.coredump.unit"), + "COREDUMP_USER_UNIT": text("journald.coredump.user_unit"), + "OBJECT_AUDIT_LOGINUID": integer("journald.object.audit.login_uid"), + "OBJECT_AUDIT_SESSION": integer("journald.object.audit.session"), + "OBJECT_CMDLINE": text("journald.object.cmd"), + "OBJECT_COMM": text("journald.object.name"), + "OBJECT_EXE": text("journald.object.executable"), + "OBJECT_GID": integer("journald.object.gid"), + "OBJECT_PID": integer("journald.object.pid"), + "OBJECT_SYSTEMD_OWNER_UID": integer("journald.object.systemd.owner_uid"), + "OBJECT_SYSTEMD_SESSION": text("journald.object.systemd.session"), + "OBJECT_SYSTEMD_UNIT": text("journald.object.systemd.unit"), + "OBJECT_SYSTEMD_USER_UNIT": text("journald.object.systemd.user_unit"), + "OBJECT_UID": integer("journald.object.uid"), + "_KERNEL_DEVICE": text("journald.kernel.device"), + "_KERNEL_SUBSYSTEM": text("journald.kernel.subsystem"), + "_SYSTEMD_INVOCATION_ID": text("systemd.invocation_id"), + "_SYSTEMD_USER_SLICE": text("systemd.user_slice"), + "_UDEV_DEVLINK": text("journald.kernel.device_symlinks"), + "_UDEV_DEVNODE": text("journald.kernel.device_node_path"), + "_UDEV_SYSNAME": text("journald.kernel.device_name"), + sdjournal.SD_JOURNAL_FIELD_AUDIT_LOGINUID: integer("process.audit.login_uid"), + sdjournal.SD_JOURNAL_FIELD_AUDIT_SESSION: text("process.audit.session"), + sdjournal.SD_JOURNAL_FIELD_BOOT_ID: text("host.boot_id"), + sdjournal.SD_JOURNAL_FIELD_CAP_EFFECTIVE: text("process.capabilites"), + sdjournal.SD_JOURNAL_FIELD_CMDLINE: text("process.cmd"), + sdjournal.SD_JOURNAL_FIELD_CODE_FILE: text("journald.code.file"), + sdjournal.SD_JOURNAL_FIELD_CODE_FUNC: text("journald.code.func"), + sdjournal.SD_JOURNAL_FIELD_CODE_LINE: integer("journald.code.line"), + sdjournal.SD_JOURNAL_FIELD_COMM: text("process.name"), + sdjournal.SD_JOURNAL_FIELD_EXE: text("process.executable"), + sdjournal.SD_JOURNAL_FIELD_GID: integer("process.uid"), + sdjournal.SD_JOURNAL_FIELD_HOSTNAME: text("host.hostname"), + sdjournal.SD_JOURNAL_FIELD_MACHINE_ID: text("host.id"), + sdjournal.SD_JOURNAL_FIELD_MESSAGE: text("message"), + sdjournal.SD_JOURNAL_FIELD_PID: integer("process.pid"), + sdjournal.SD_JOURNAL_FIELD_PRIORITY: integer("syslog.priority", "log.syslog.priority"), + sdjournal.SD_JOURNAL_FIELD_SYSLOG_FACILITY: integer("syslog.facility", "log.syslog.facility.name"), + sdjournal.SD_JOURNAL_FIELD_SYSLOG_IDENTIFIER: text("syslog.identifier"), + sdjournal.SD_JOURNAL_FIELD_SYSLOG_PID: integer("syslog.pid"), + sdjournal.SD_JOURNAL_FIELD_SYSTEMD_CGROUP: text("systemd.cgroup"), + sdjournal.SD_JOURNAL_FIELD_SYSTEMD_OWNER_UID: integer("systemd.owner_uid"), + sdjournal.SD_JOURNAL_FIELD_SYSTEMD_SESSION: text("systemd.session"), + sdjournal.SD_JOURNAL_FIELD_SYSTEMD_SLICE: text("systemd.slice"), + sdjournal.SD_JOURNAL_FIELD_SYSTEMD_UNIT: text("systemd.unit"), + sdjournal.SD_JOURNAL_FIELD_SYSTEMD_USER_UNIT: text("systemd.user_unit"), + sdjournal.SD_JOURNAL_FIELD_TRANSPORT: text("systemd.transport"), + sdjournal.SD_JOURNAL_FIELD_UID: integer("process.uid"), + + // docker journald fields from: https://docs.docker.com/config/containers/logging/journald/ + "CONTAINER_ID": text("container.id_truncated"), + "CONTAINER_ID_FULL": text("container.id"), + "CONTAINER_NAME": text("container.name"), + "CONTAINER_TAG": text("container.log.tag"), + "CONTAINER_PARTIAL_MESSAGE": text("container.partial"), + + // dropped fields + sdjournal.SD_JOURNAL_FIELD_MONOTONIC_TIMESTAMP: ignoredField, // saved in the registry + sdjournal.SD_JOURNAL_FIELD_SOURCE_REALTIME_TIMESTAMP: ignoredField, // saved in the registry + sdjournal.SD_JOURNAL_FIELD_CURSOR: ignoredField, // saved in the registry + "_SOURCE_MONOTONIC_TIMESTAMP": ignoredField, // received timestamp stored in @timestamp +} diff --git a/journalbeat/pkg/journalfield/default_other.go b/journalbeat/pkg/journalfield/default_other.go new file mode 100644 index 00000000000..ca3d26c9266 --- /dev/null +++ b/journalbeat/pkg/journalfield/default_other.go @@ -0,0 +1,46 @@ +// Licensed to Elasticsearch B.V. under one or more contributor +// license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright +// ownership. Elasticsearch B.V. licenses this file to you under +// the Apache License, Version 2.0 (the "License"); you may +// not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +//+build !linux !cgo + +package journalfield + +// journaldEventFields provides default field mappings and conversions rules. +var journaldEventFields = FieldConversion{ + // provided by systemd journal + "COREDUMP_UNIT": text("journald.coredump.unit"), + "COREDUMP_USER_UNIT": text("journald.coredump.user_unit"), + "OBJECT_AUDIT_LOGINUID": integer("journald.object.audit.login_uid"), + "OBJECT_AUDIT_SESSION": integer("journald.object.audit.session"), + "OBJECT_CMDLINE": text("journald.object.cmd"), + "OBJECT_COMM": text("journald.object.name"), + "OBJECT_EXE": text("journald.object.executable"), + "OBJECT_GID": integer("journald.object.gid"), + "OBJECT_PID": integer("journald.object.pid"), + "OBJECT_SYSTEMD_OWNER_UID": integer("journald.object.systemd.owner_uid"), + "OBJECT_SYSTEMD_SESSION": text("journald.object.systemd.session"), + "OBJECT_SYSTEMD_UNIT": text("journald.object.systemd.unit"), + "OBJECT_SYSTEMD_USER_UNIT": text("journald.object.systemd.user_unit"), + "OBJECT_UID": integer("journald.object.uid"), + "_KERNEL_DEVICE": text("journald.kernel.device"), + "_KERNEL_SUBSYSTEM": text("journald.kernel.subsystem"), + "_SYSTEMD_INVOCATION_ID": text("systemd.invocation_id"), + "_SYSTEMD_USER_SLICE": text("systemd.user_slice"), + "_UDEV_DEVLINK": text("journald.kernel.device_symlinks"), + "_UDEV_DEVNODE": text("journald.kernel.device_node_path"), + "_UDEV_SYSNAME": text("journald.kernel.device_name"), +} diff --git a/journalbeat/pkg/journalfield/matcher.go b/journalbeat/pkg/journalfield/matcher.go new file mode 100644 index 00000000000..0227b2aae83 --- /dev/null +++ b/journalbeat/pkg/journalfield/matcher.go @@ -0,0 +1,120 @@ +// Licensed to Elasticsearch B.V. under one or more contributor +// license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright +// ownership. Elasticsearch B.V. licenses this file to you under +// the Apache License, Version 2.0 (the "License"); you may +// not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package journalfield + +import ( + "fmt" + "strings" +) + +// Matcher is a single field condition for filtering journal entries. +// +// The Matcher type can be used as is with Beats configuration unpacking. The +// internal default conversion table will be used, similar to BuildMatcher. +type Matcher struct { + str string +} + +// MatcherBuilder can be used to create a custom builder for creating matchers +// based on a conversion table. +type MatcherBuilder struct { + Conversions map[string]Conversion +} + +type journal interface { + AddMatch(string) error + AddDisjunction() error +} + +var defaultBuilder = MatcherBuilder{Conversions: journaldEventFields} + +// Build creates a new Matcher using the configured conversion table. +// If no table has been configured the internal default table will be used. +func (b MatcherBuilder) Build(in string) (Matcher, error) { + elems := strings.Split(in, "=") + if len(elems) != 2 { + return Matcher{}, fmt.Errorf("invalid match format: %s", in) + } + + conversions := b.Conversions + if conversions == nil { + conversions = journaldEventFields + } + + for journalKey, eventField := range conversions { + for _, name := range eventField.Names { + if elems[0] == name { + return Matcher{journalKey + "=" + elems[1]}, nil + } + } + } + + // pass custom fields as is + return Matcher{in}, nil +} + +// BuildMatcher creates a Matcher from a field filter string. +func BuildMatcher(in string) (Matcher, error) { + return defaultBuilder.Build(in) +} + +// IsValid returns true if the matcher was initialized correctly. +func (m Matcher) IsValid() bool { return m.str != "" } + +// String returns the string representation of the field match. +func (m Matcher) String() string { return m.str } + +// Apply adds the field match to an open journal for filtering. +func (m Matcher) Apply(j journal) error { + if !m.IsValid() { + return fmt.Errorf("can not apply invalid matcher to a journal") + } + + err := j.AddMatch(m.str) + if err != nil { + return fmt.Errorf("error adding match '%s' to journal: %v", m.str, err) + } + return nil +} + +// Unpack initializes the Matcher from a given string representation. Unpack +// fails if the input string is invalid. +// Unpack can be used with Beats configuration loading. +func (m *Matcher) Unpack(value string) error { + tmp, err := BuildMatcher(value) + if err != nil { + return err + } + *m = tmp + return nil +} + +// ApplyMatchersOr adds a list of matchers to a journal, calling AddDisjunction after each matcher being added. +func ApplyMatchersOr(j journal, matchers []Matcher) error { + for _, m := range matchers { + if err := m.Apply(j); err != nil { + return err + } + + if err := j.AddDisjunction(); err != nil { + return fmt.Errorf("error adding disjunction to journal: %v", err) + } + } + + return nil +} diff --git a/journalbeat/pkg/journalfield/matcher_test.go b/journalbeat/pkg/journalfield/matcher_test.go new file mode 100644 index 00000000000..9ce8c63c8a4 --- /dev/null +++ b/journalbeat/pkg/journalfield/matcher_test.go @@ -0,0 +1,80 @@ +// Licensed to Elasticsearch B.V. under one or more contributor +// license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright +// ownership. Elasticsearch B.V. licenses this file to you under +// the Apache License, Version 2.0 (the "License"); you may +// not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +//+build linux,cgo + +package journalfield + +import ( + "testing" + + "github.com/coreos/go-systemd/v22/sdjournal" +) + +func TestApplyMatchersOr(t *testing.T) { + cases := map[string]struct { + filters []string + wantErr bool + }{ + "correct filter expression": { + filters: []string{"systemd.unit=nginx"}, + wantErr: false, + }, + "custom field": { + filters: []string{"_MY_CUSTOM_FIELD=value"}, + wantErr: false, + }, + "mixed filters": { + filters: []string{"systemd.unit=nginx", "_MY_CUSTOM_FIELD=value"}, + wantErr: false, + }, + "same field filters": { + filters: []string{"systemd.unit=nginx", "systemd.unit=mysql"}, + wantErr: false, + }, + "incorrect separator": { + filters: []string{"systemd.unit~nginx"}, + wantErr: true, + }, + } + + for name, test := range cases { + t.Run(name, func(t *testing.T) { + journal, err := sdjournal.NewJournal() + if err != nil { + t.Fatalf("error while creating test journal: %v", err) + } + defer journal.Close() + + matchers := make([]Matcher, len(test.filters)) + for i, str := range test.filters { + m, err := BuildMatcher(str) + if err != nil && !test.wantErr { + t.Fatalf("unexpected error compiling the filters: %v", err) + } + matchers[i] = m + } + + // double check if journald likes our filters + err = ApplyMatchersOr(journal, matchers) + fail := (test.wantErr && err == nil) || (!test.wantErr && err != nil) + if fail { + t.Errorf("unexpected outcome: error: '%v', expected error: %v", err, test.wantErr) + } + }) + } +} diff --git a/journalbeat/pkg/journalread/mode.go b/journalbeat/pkg/journalread/mode.go new file mode 100644 index 00000000000..3eff2d93c5d --- /dev/null +++ b/journalbeat/pkg/journalread/mode.go @@ -0,0 +1,57 @@ +// Licensed to Elasticsearch B.V. under one or more contributor +// license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright +// ownership. Elasticsearch B.V. licenses this file to you under +// the Apache License, Version 2.0 (the "License"); you may +// not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package journalread + +import ( + "errors" + "fmt" +) + +// SeekMode is used by (*Reader).Seek to decide where to advance the read pointer to. +type SeekMode uint + +const ( + // SeekInvalid is an invalid value for seek + SeekInvalid SeekMode = iota + // SeekHead option seeks to the head of a journal + SeekHead + // SeekTail option seeks to the tail of a journal + SeekTail + // SeekCursor option seeks to the position specified in the cursor + SeekCursor +) + +var seekModes = map[string]SeekMode{ + "head": SeekHead, + "tail": SeekTail, + "cursor": SeekCursor, +} + +var errInvalidSeekFallback = errors.New("invalid setting for cursor_seek_fallback") + +// Unpack validates and unpack "seek" config options. It returns an error if +// the string is no valid seek mode. +func (m *SeekMode) Unpack(value string) error { + mode, ok := seekModes[value] + if !ok { + return fmt.Errorf("invalid seek mode '%s'", value) + } + + *m = mode + return nil +} diff --git a/journalbeat/pkg/journalread/mode_test.go b/journalbeat/pkg/journalread/mode_test.go new file mode 100644 index 00000000000..aef0ed4150c --- /dev/null +++ b/journalbeat/pkg/journalread/mode_test.go @@ -0,0 +1,58 @@ +// Licensed to Elasticsearch B.V. under one or more contributor +// license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright +// ownership. Elasticsearch B.V. licenses this file to you under +// the Apache License, Version 2.0 (the "License"); you may +// not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package journalread + +import ( + "testing" +) + +func TestMode_Unpack(t *testing.T) { + t.Run("ok", func(t *testing.T) { + tests := map[string]SeekMode{ + "head": SeekHead, + "tail": SeekTail, + "cursor": SeekCursor, + } + + for str, want := range tests { + t.Run(str, func(t *testing.T) { + var m SeekMode + if err := m.Unpack(str); err != nil { + t.Fatal(err) + } + + if m != want { + t.Errorf("wrong mode, expected %v, got %v", want, m) + } + }) + } + }) + + t.Run("failing", func(t *testing.T) { + cases := []string{"invalid", "", "unknown"} + + for _, str := range cases { + t.Run(str, func(t *testing.T) { + var m SeekMode + if err := m.Unpack(str); err == nil { + t.Errorf("an error was expected, got %v", m) + } + }) + } + }) +} diff --git a/journalbeat/pkg/journalread/reader.go b/journalbeat/pkg/journalread/reader.go new file mode 100644 index 00000000000..7d5ac50e965 --- /dev/null +++ b/journalbeat/pkg/journalread/reader.go @@ -0,0 +1,199 @@ +// Licensed to Elasticsearch B.V. under one or more contributor +// license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright +// ownership. Elasticsearch B.V. licenses this file to you under +// the Apache License, Version 2.0 (the "License"); you may +// not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +// +build linux,cgo + +package journalread + +import ( + "fmt" + "io" + "os" + "syscall" + "time" + + "github.com/coreos/go-systemd/v22/sdjournal" + "github.com/urso/sderr" + + input "github.com/elastic/beats/v7/filebeat/input/v2" + "github.com/elastic/beats/v7/libbeat/common/backoff" + "github.com/elastic/beats/v7/libbeat/common/cleanup" + "github.com/elastic/beats/v7/libbeat/logp" +) + +// Reader implements a Journald base reader with backoff support. The reader +// will block until a new entry can be read from the journal. +type Reader struct { + log *logp.Logger + backoff backoff.Backoff + journal journal +} + +type journal interface { + Close() error + + Next() (uint64, error) + Wait(time.Duration) int + GetEntry() (*sdjournal.JournalEntry, error) + + SeekHead() error + SeekTail() error + SeekCursor(string) error +} + +// LocalSystemJournalID is the ID of the local system journal. +const localSystemJournalID = "LOCAL_SYSTEM_JOURNAL" + +// NewReader creates a new Reader for an already opened journal. The reader assumed to take +// ownership of the journal, and needs to be closed. +func NewReader(log *logp.Logger, journal journal, backoff backoff.Backoff) *Reader { + return &Reader{log: log, journal: journal, backoff: backoff} +} + +// Open opens a journal and creates a reader for it. +// Additonal settings can be applied to the journal by passing functions to with. +// Open returns an error if the journal can not be opened, or if one with-function failed. +// +// Open will opend the systems journal if the path is empty or matches LOCAL_SYSTEM_JOURNAL. +// The path can optionally point to a file or a directory. +func Open(log *logp.Logger, path string, backoff backoff.Backoff, with ...func(j *sdjournal.Journal) error) (*Reader, error) { + j, err := openJournal(path) + if err != nil { + return nil, err + } + + ok := false + defer cleanup.IfNot(&ok, func() { j.Close() }) + + for _, w := range with { + if err := w(j); err != nil { + return nil, err + } + } + + ok = true + return NewReader(log, j, backoff), nil +} + +func openJournal(path string) (*sdjournal.Journal, error) { + if path == localSystemJournalID || path == "" { + j, err := sdjournal.NewJournal() + if err != nil { + err = sderr.Wrap(err, "failed to open local journal") + } + return j, err + } + + stat, err := os.Stat(path) + if err != nil { + return nil, sderr.Wrap(err, "failed to read meta data for %{path}", path) + } + + if stat.IsDir() { + j, err := sdjournal.NewJournalFromDir(path) + if err != nil { + err = sderr.Wrap(err, "failed to open journal directory %{path}", path) + } + return j, err + } + + j, err := sdjournal.NewJournalFromFiles(path) + if err != nil { + err = sderr.Wrap(err, "failed to open journal file %{path}", path) + } + return j, err +} + +// Close closes the journal. +func (r *Reader) Close() error { + return r.journal.Close() +} + +// Seek moves the read pointer to a new position. +// If a cursor or SeekTail is given, Seek tries to ignore the entry at the +// given position, jumping right to the next entry. +func (r *Reader) Seek(mode SeekMode, cursor string) (err error) { + switch mode { + case SeekHead: + err = r.journal.SeekHead() + case SeekTail: + if err = r.journal.SeekTail(); err == nil { + _, err = r.journal.Next() + } + case SeekCursor: + if err = r.journal.SeekCursor(cursor); err == nil { + _, err = r.journal.Next() + } + default: + return fmt.Errorf("invalid seek mode '%v'", mode) + } + return err +} + +// Next reads a new journald entry from the journal. It blocks if there is +// currently no entry available in the journal, or until an error has occured. +func (r *Reader) Next(cancel input.Canceler) (*sdjournal.JournalEntry, error) { + for cancel.Err() == nil { + c, err := r.journal.Next() + if err != nil && err != io.EOF { + return nil, err + } + + switch { + // error while reading next entry + case c < 0: + return nil, fmt.Errorf("error while reading next entry %+v", syscall.Errno(-c)) + // no new entry, so wait + case c == 0: + hasNewEntry, err := r.checkForNewEvents() + if err != nil { + return nil, err + } + if !hasNewEntry { + // TODO: backoff support is currently not cancellable :( + r.backoff.Wait() + } + continue + // new entries are available + default: + } + + entry, err := r.journal.GetEntry() + if err != nil { + return nil, err + } + r.backoff.Reset() + + return entry, nil + } + return nil, cancel.Err() +} + +func (r *Reader) checkForNewEvents() (bool, error) { + c := r.journal.Wait(100 * time.Millisecond) + switch c { + case sdjournal.SD_JOURNAL_NOP: + return false, nil + // new entries are added or the journal has changed (e.g. vacuum, rotate) + case sdjournal.SD_JOURNAL_APPEND, sdjournal.SD_JOURNAL_INVALIDATE: + return true, nil + default: + } + + r.log.Errorf("Unknown return code from Wait: %d\n", c) + return false, nil +} diff --git a/journalbeat/reader/config.go b/journalbeat/reader/config.go index f8dbabe5fee..e559681aea7 100644 --- a/journalbeat/reader/config.go +++ b/journalbeat/reader/config.go @@ -20,7 +20,8 @@ package reader import ( "time" - "github.com/elastic/beats/v7/journalbeat/config" + "github.com/elastic/beats/v7/journalbeat/pkg/journalfield" + "github.com/elastic/beats/v7/journalbeat/pkg/journalread" ) // Config stores the options of a reder. @@ -29,16 +30,16 @@ type Config struct { Path string // Seek specifies the seeking stategy. // Possible values: head, tail, cursor. - Seek config.SeekMode + Seek journalread.SeekMode // CursorSeekFallback sets where to seek if registry file is not available. - CursorSeekFallback config.SeekMode + CursorSeekFallback journalread.SeekMode // MaxBackoff is the limit of the backoff time. MaxBackoff time.Duration // Backoff is the current interval to wait before // attemting to read again from the journal. Backoff time.Duration // Matches store the key value pairs to match entries. - Matches []string + Matches []journalfield.Matcher // SaveRemoteHostname defines if the original source of the entry needs to be saved. SaveRemoteHostname bool // CheckpointID is the identifier to use when persisting state. diff --git a/journalbeat/reader/fields.go b/journalbeat/reader/fields.go index d0230849cba..0b6efc3cdc8 100644 --- a/journalbeat/reader/fields.go +++ b/journalbeat/reader/fields.go @@ -18,78 +18,3 @@ //+build linux,cgo package reader - -import "github.com/coreos/go-systemd/v22/sdjournal" - -type fieldConversion struct { - names []string - isInteger bool - dropped bool -} - -var ( - journaldEventFields = map[string]fieldConversion{ - // provided by systemd journal - "COREDUMP_UNIT": fieldConversion{[]string{"journald.coredump.unit"}, false, false}, - "COREDUMP_USER_UNIT": fieldConversion{[]string{"journald.coredump.user_unit"}, false, false}, - "OBJECT_AUDIT_LOGINUID": fieldConversion{[]string{"journald.object.audit.login_uid"}, true, false}, - "OBJECT_AUDIT_SESSION": fieldConversion{[]string{"journald.object.audit.session"}, true, false}, - "OBJECT_CMDLINE": fieldConversion{[]string{"journald.object.cmd"}, false, false}, - "OBJECT_COMM": fieldConversion{[]string{"journald.object.name"}, false, false}, - "OBJECT_EXE": fieldConversion{[]string{"journald.object.executable"}, false, false}, - "OBJECT_GID": fieldConversion{[]string{"journald.object.gid"}, true, false}, - "OBJECT_PID": fieldConversion{[]string{"journald.object.pid"}, true, false}, - "OBJECT_SYSTEMD_OWNER_UID": fieldConversion{[]string{"journald.object.systemd.owner_uid"}, true, false}, - "OBJECT_SYSTEMD_SESSION": fieldConversion{[]string{"journald.object.systemd.session"}, false, false}, - "OBJECT_SYSTEMD_UNIT": fieldConversion{[]string{"journald.object.systemd.unit"}, false, false}, - "OBJECT_SYSTEMD_USER_UNIT": fieldConversion{[]string{"journald.object.systemd.user_unit"}, false, false}, - "OBJECT_UID": fieldConversion{[]string{"journald.object.uid"}, true, false}, - "_KERNEL_DEVICE": fieldConversion{[]string{"journald.kernel.device"}, false, false}, - "_KERNEL_SUBSYSTEM": fieldConversion{[]string{"journald.kernel.subsystem"}, false, false}, - "_SYSTEMD_INVOCATION_ID": fieldConversion{[]string{"systemd.invocation_id"}, false, false}, - "_SYSTEMD_USER_SLICE": fieldConversion{[]string{"systemd.user_slice"}, false, false}, - "_UDEV_DEVLINK": fieldConversion{[]string{"journald.kernel.device_symlinks"}, false, false}, // TODO aggregate multiple elements - "_UDEV_DEVNODE": fieldConversion{[]string{"journald.kernel.device_node_path"}, false, false}, - "_UDEV_SYSNAME": fieldConversion{[]string{"journald.kernel.device_name"}, false, false}, - sdjournal.SD_JOURNAL_FIELD_AUDIT_LOGINUID: fieldConversion{[]string{"process.audit.login_uid"}, true, false}, - sdjournal.SD_JOURNAL_FIELD_AUDIT_SESSION: fieldConversion{[]string{"process.audit.session"}, false, false}, - sdjournal.SD_JOURNAL_FIELD_BOOT_ID: fieldConversion{[]string{"host.boot_id"}, false, false}, - sdjournal.SD_JOURNAL_FIELD_CAP_EFFECTIVE: fieldConversion{[]string{"process.capabilites"}, false, false}, - sdjournal.SD_JOURNAL_FIELD_CMDLINE: fieldConversion{[]string{"process.cmd"}, false, false}, - sdjournal.SD_JOURNAL_FIELD_CODE_FILE: fieldConversion{[]string{"journald.code.file"}, false, false}, - sdjournal.SD_JOURNAL_FIELD_CODE_FUNC: fieldConversion{[]string{"journald.code.func"}, false, false}, - sdjournal.SD_JOURNAL_FIELD_CODE_LINE: fieldConversion{[]string{"journald.code.line"}, true, false}, - sdjournal.SD_JOURNAL_FIELD_COMM: fieldConversion{[]string{"process.name"}, false, false}, - sdjournal.SD_JOURNAL_FIELD_EXE: fieldConversion{[]string{"process.executable"}, false, false}, - sdjournal.SD_JOURNAL_FIELD_GID: fieldConversion{[]string{"process.uid"}, true, false}, - sdjournal.SD_JOURNAL_FIELD_HOSTNAME: fieldConversion{[]string{"host.hostname"}, false, false}, - sdjournal.SD_JOURNAL_FIELD_MACHINE_ID: fieldConversion{[]string{"host.id"}, false, false}, - sdjournal.SD_JOURNAL_FIELD_MESSAGE: fieldConversion{[]string{"message"}, false, false}, - sdjournal.SD_JOURNAL_FIELD_PID: fieldConversion{[]string{"process.pid"}, true, false}, - sdjournal.SD_JOURNAL_FIELD_PRIORITY: fieldConversion{[]string{"syslog.priority", "log.syslog.priority"}, true, false}, - sdjournal.SD_JOURNAL_FIELD_SYSLOG_FACILITY: fieldConversion{[]string{"syslog.facility", "log.syslog.facility.name"}, true, false}, - sdjournal.SD_JOURNAL_FIELD_SYSLOG_IDENTIFIER: fieldConversion{[]string{"syslog.identifier"}, false, false}, - sdjournal.SD_JOURNAL_FIELD_SYSLOG_PID: fieldConversion{[]string{"syslog.pid"}, true, false}, - sdjournal.SD_JOURNAL_FIELD_SYSTEMD_CGROUP: fieldConversion{[]string{"systemd.cgroup"}, false, false}, - sdjournal.SD_JOURNAL_FIELD_SYSTEMD_OWNER_UID: fieldConversion{[]string{"systemd.owner_uid"}, true, false}, - sdjournal.SD_JOURNAL_FIELD_SYSTEMD_SESSION: fieldConversion{[]string{"systemd.session"}, false, false}, - sdjournal.SD_JOURNAL_FIELD_SYSTEMD_SLICE: fieldConversion{[]string{"systemd.slice"}, false, false}, - sdjournal.SD_JOURNAL_FIELD_SYSTEMD_UNIT: fieldConversion{[]string{"systemd.unit"}, false, false}, - sdjournal.SD_JOURNAL_FIELD_SYSTEMD_USER_UNIT: fieldConversion{[]string{"systemd.user_unit"}, false, false}, - sdjournal.SD_JOURNAL_FIELD_TRANSPORT: fieldConversion{[]string{"systemd.transport"}, false, false}, - sdjournal.SD_JOURNAL_FIELD_UID: fieldConversion{[]string{"process.uid"}, true, false}, - - // docker journald fields from: https://docs.docker.com/config/containers/logging/journald/ - "CONTAINER_ID": fieldConversion{[]string{"container.id_truncated"}, false, false}, - "CONTAINER_ID_FULL": fieldConversion{[]string{"container.id"}, false, false}, - "CONTAINER_NAME": fieldConversion{[]string{"container.name"}, false, false}, - "CONTAINER_TAG": fieldConversion{[]string{"container.log.tag"}, false, false}, - "CONTAINER_PARTIAL_MESSAGE": fieldConversion{[]string{"container.partial"}, false, false}, - - // dropped fields - sdjournal.SD_JOURNAL_FIELD_MONOTONIC_TIMESTAMP: fieldConversion{nil, false, true}, // saved in the registry - sdjournal.SD_JOURNAL_FIELD_SOURCE_REALTIME_TIMESTAMP: fieldConversion{nil, false, true}, // saved in the registry - sdjournal.SD_JOURNAL_FIELD_CURSOR: fieldConversion{nil, false, true}, // saved in the registry - "_SOURCE_MONOTONIC_TIMESTAMP": fieldConversion{nil, false, true}, // received timestamp stored in @timestamp - } -) diff --git a/journalbeat/reader/journal.go b/journalbeat/reader/journal.go index 7a254f4f6d2..fb5b91c5019 100644 --- a/journalbeat/reader/journal.go +++ b/journalbeat/reader/journal.go @@ -20,28 +20,23 @@ package reader import ( - "fmt" - "io" - "os" - "strconv" - "strings" - "syscall" "time" "github.com/coreos/go-systemd/v22/sdjournal" - "github.com/pkg/errors" "github.com/elastic/beats/v7/journalbeat/checkpoint" "github.com/elastic/beats/v7/journalbeat/cmd/instance" - "github.com/elastic/beats/v7/journalbeat/config" + "github.com/elastic/beats/v7/journalbeat/pkg/journalfield" + "github.com/elastic/beats/v7/journalbeat/pkg/journalread" "github.com/elastic/beats/v7/libbeat/beat" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/common/backoff" "github.com/elastic/beats/v7/libbeat/logp" + "github.com/elastic/go-concert/ctxtool" ) // Reader reads entries from journal(s). type Reader struct { + r *journalread.Reader journal *sdjournal.Journal config Config done chan struct{} @@ -51,223 +46,83 @@ type Reader struct { // New creates a new journal reader and moves the FP to the configured position. func New(c Config, done chan struct{}, state checkpoint.JournalState, logger *logp.Logger) (*Reader, error) { - f, err := os.Stat(c.Path) - if err != nil { - return nil, errors.Wrap(err, "failed to open file") - } - - var j *sdjournal.Journal - if f.IsDir() { - j, err = sdjournal.NewJournalFromDir(c.Path) - if err != nil { - return nil, errors.Wrap(err, "failed to open journal directory") - } - } else { - j, err = sdjournal.NewJournalFromFiles(c.Path) - if err != nil { - return nil, errors.Wrap(err, "failed to open journal file") - } - } - - l := logger.With("path", c.Path) - l.Debug("New journal is opened for reading") - - return newReader(l, done, c, j, state) + return newReader(c.Path, c, done, state, logger) } // NewLocal creates a reader to read form the local journal and moves the FP // to the configured position. func NewLocal(c Config, done chan struct{}, state checkpoint.JournalState, logger *logp.Logger) (*Reader, error) { - j, err := sdjournal.NewJournal() - if err != nil { - return nil, errors.Wrap(err, "failed to open local journal") - } - - l := logger.With("path", "local") - l.Debug("New local journal is opened for reading") - - return newReader(l, done, c, j, state) + return newReader(LocalSystemJournalID, c, done, state, logger) } -func newReader(logger *logp.Logger, done chan struct{}, c Config, journal *sdjournal.Journal, state checkpoint.JournalState) (*Reader, error) { - err := setupMatches(journal, c.Matches) +func newReader(path string, c Config, done chan struct{}, state checkpoint.JournalState, logger *logp.Logger) (*Reader, error) { + logger = logger.With("path", path) + backoff := backoff.NewExpBackoff(done, c.Backoff, c.MaxBackoff) + + var journal *sdjournal.Journal + r, err := journalread.Open(logger, c.Path, backoff, func(j *sdjournal.Journal) error { + journal = j + return journalfield.ApplyMatchersOr(j, c.Matches) + }) if err != nil { return nil, err } - r := &Reader{ - journal: journal, - config: c, - done: done, - logger: logger, - backoff: backoff.NewExpBackoff(done, c.Backoff, c.MaxBackoff), + if err := r.Seek(seekBy(logger, c, state)); err != nil { + logger.Error("Continue from current position. Seek failed with: %v", err) } - r.seek(state.Cursor) + logger.Debug("New journal is opened for reading") instance.AddJournalToMonitor(c.Path, journal) - return r, nil + return &Reader{ + journal: journal, + config: c, + done: done, + logger: logger, + backoff: backoff, + }, nil } -func setupMatches(j *sdjournal.Journal, matches []string) error { - for _, m := range matches { - elems := strings.Split(m, "=") - if len(elems) != 2 { - return fmt.Errorf("invalid match format: %s", m) - } - - var p string - for journalKey, eventField := range journaldEventFields { - for _, name := range eventField.names { - if elems[0] == name { - p = journalKey + "=" + elems[1] - } - } - } - - // pass custom fields as is - if p == "" { - p = m - } - - logp.Debug("journal", "Added matcher expression: %s", p) - - err := j.AddMatch(p) - if err != nil { - return fmt.Errorf("error adding match to journal %v", err) - } - - err = j.AddDisjunction() - if err != nil { - return fmt.Errorf("error adding disjunction to journal: %v", err) +func seekBy(log *logp.Logger, c Config, state checkpoint.JournalState) (journalread.SeekMode, string) { + mode := c.Seek + if mode == journalread.SeekCursor && state.Cursor == "" { + mode = c.CursorSeekFallback + if mode != journalread.SeekHead && mode != journalread.SeekTail { + log.Error("Invalid option for cursor_seek_fallback") + mode = journalread.SeekHead } } - return nil + return mode, state.Cursor } -// seek seeks to the position determined by the coniguration and cursor state. -func (r *Reader) seek(cursor string) { - switch r.config.Seek { - case config.SeekCursor: - if cursor == "" { - switch r.config.CursorSeekFallback { - case config.SeekHead: - r.journal.SeekHead() - r.logger.Debug("Seeking method set to cursor, but no state is saved for reader. Starting to read from the beginning") - case config.SeekTail: - r.journal.SeekTail() - r.journal.Next() - r.logger.Debug("Seeking method set to cursor, but no state is saved for reader. Starting to read from the end") - default: - r.logger.Error("Invalid option for cursor_seek_fallback") - } - return - } - r.journal.SeekCursor(cursor) - _, err := r.journal.Next() - if err != nil { - r.logger.Error("Error while seeking to cursor") - } - r.logger.Debug("Seeked to position defined in cursor") - case config.SeekTail: - r.journal.SeekTail() - r.journal.Next() - r.logger.Debug("Tailing the journal file") - case config.SeekHead: - r.journal.SeekHead() - r.logger.Debug("Reading from the beginning of the journal file") - default: - r.logger.Error("Invalid seeking mode") - } +// Close closes the underlying journal reader. +func (r *Reader) Close() { + instance.StopMonitoringJournal(r.config.Path) + r.r.Close() } // Next waits until a new event shows up and returns it. // It blocks until an event is returned or an error occurs. func (r *Reader) Next() (*beat.Event, error) { - for { - select { - case <-r.done: - return nil, nil - default: - } - - c, err := r.journal.Next() - if err != nil && err != io.EOF { - return nil, err - } - - switch { - // error while reading next entry - case c < 0: - return nil, fmt.Errorf("error while reading next entry %+v", syscall.Errno(-c)) - // no new entry, so wait - case c == 0: - hasNewEntry, err := r.checkForNewEvents() - if err != nil { - return nil, err - } - if !hasNewEntry { - r.backoff.Wait() - } - continue - // new entries are available - default: - } - - entry, err := r.journal.GetEntry() - if err != nil { - return nil, err - } - event := r.toEvent(entry) - r.backoff.Reset() - - return event, nil - } -} - -func (r *Reader) checkForNewEvents() (bool, error) { - c := r.journal.Wait(100 * time.Millisecond) - switch c { - case sdjournal.SD_JOURNAL_NOP: - return false, nil - // new entries are added or the journal has changed (e.g. vacuum, rotate) - case sdjournal.SD_JOURNAL_APPEND, sdjournal.SD_JOURNAL_INVALIDATE: - return true, nil - default: + entry, err := r.r.Next(ctxtool.FromChannel(r.done)) + if err != nil { + return nil, err } - r.logger.Errorf("Unknown return code from Wait: %d\n", c) - return false, nil + event := toEvent(r.logger, r.config.CheckpointID, entry, r.config.SaveRemoteHostname) + return event, nil } // toEvent creates a beat.Event from journal entries. -func (r *Reader) toEvent(entry *sdjournal.JournalEntry) *beat.Event { - fields := common.MapStr{ - "event": common.MapStr{ - "kind": "event", - }, - } - custom := common.MapStr{} - - for entryKey, v := range entry.Fields { - if fieldConversionInfo, ok := journaldEventFields[entryKey]; !ok { - normalized := strings.ToLower(strings.TrimLeft(entryKey, "_")) - custom.Put(normalized, v) - } else if !fieldConversionInfo.dropped { - value := r.convertNamedField(fieldConversionInfo, v) - for _, name := range fieldConversionInfo.names { - fields.Put(name, value) - } - } - } - - if len(custom) != 0 { - fields.Put("journald.custom", custom) - } +func toEvent(logger *logp.Logger, id string, entry *sdjournal.JournalEntry, saveRemoteHostname bool) *beat.Event { + created := time.Now() + fields := journalfield.NewConverter(logger, nil).Convert(entry.Fields) + fields.Put("event.kind", "event") // if entry is coming from a remote journal, add_host_metadata overwrites the source hostname, so it // has to be copied to a different field - if r.config.SaveRemoteHostname { + if saveRemoteHostname { remoteHostname, err := fields.GetValue("host.hostname") if err == nil { fields.Put("log.source.address", remoteHostname) @@ -275,13 +130,13 @@ func (r *Reader) toEvent(entry *sdjournal.JournalEntry) *beat.Event { } state := checkpoint.JournalState{ - Path: r.config.CheckpointID, + Path: id, Cursor: entry.Cursor, RealtimeTimestamp: entry.RealtimeTimestamp, MonotonicTimestamp: entry.MonotonicTimestamp, } - fields.Put("event.created", time.Now()) + fields.Put("event.created", created) receivedByJournal := time.Unix(0, int64(entry.RealtimeTimestamp)*1000) event := beat.Event{ @@ -291,29 +146,3 @@ func (r *Reader) toEvent(entry *sdjournal.JournalEntry) *beat.Event { } return &event } - -func (r *Reader) convertNamedField(fc fieldConversion, value string) interface{} { - if fc.isInteger { - v, err := strconv.ParseInt(value, 10, 64) - if err != nil { - // On some versions of systemd the 'syslog.pid' can contain the username - // appended to the end of the pid. In most cases this does not occur - // but in the cases that it does, this tries to strip ',\w*' from the - // value and then perform the conversion. - s := strings.Split(value, ",") - v, err = strconv.ParseInt(s[0], 10, 64) - if err != nil { - r.logger.Debugf("Failed to convert field: %v \"%v\" to int: %v", fc.names, value, err) - return value - } - } - return v - } - return value -} - -// Close closes the underlying journal reader. -func (r *Reader) Close() { - instance.StopMonitoringJournal(r.config.Path) - r.journal.Close() -} diff --git a/journalbeat/reader/journal_test.go b/journalbeat/reader/journal_test.go deleted file mode 100644 index 4293b4b31e1..00000000000 --- a/journalbeat/reader/journal_test.go +++ /dev/null @@ -1,184 +0,0 @@ -// Licensed to Elasticsearch B.V. under one or more contributor -// license agreements. See the NOTICE file distributed with -// this work for additional information regarding copyright -// ownership. Elasticsearch B.V. licenses this file to you under -// the Apache License, Version 2.0 (the "License"); you may -// not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, -// software distributed under the License is distributed on an -// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -// KIND, either express or implied. See the License for the -// specific language governing permissions and limitations -// under the License. - -//+build linux,cgo - -package reader - -import ( - "reflect" - "testing" - - "github.com/coreos/go-systemd/v22/sdjournal" - "github.com/stretchr/testify/assert" - - "github.com/elastic/beats/v7/journalbeat/checkpoint" - "github.com/elastic/beats/v7/journalbeat/cmd/instance" - "github.com/elastic/beats/v7/libbeat/common" - "github.com/elastic/beats/v7/libbeat/logp" -) - -type ToEventTestCase struct { - entry sdjournal.JournalEntry - expectedFields common.MapStr -} - -type SetupMatchesTestCase struct { - matches []string - expectError bool -} - -func TestToEvent(t *testing.T) { - tests := []ToEventTestCase{ - // field name from fields.go - ToEventTestCase{ - entry: sdjournal.JournalEntry{ - Fields: map[string]string{ - sdjournal.SD_JOURNAL_FIELD_BOOT_ID: "123456", - }, - }, - expectedFields: common.MapStr{ - "host": common.MapStr{ - "boot_id": "123456", - }, - }, - }, - // 'syslog.pid' field without user append - ToEventTestCase{ - entry: sdjournal.JournalEntry{ - Fields: map[string]string{ - sdjournal.SD_JOURNAL_FIELD_SYSLOG_PID: "123456", - }, - }, - expectedFields: common.MapStr{ - "syslog": common.MapStr{ - "pid": int64(123456), - }, - }, - }, - // 'syslog.priority' field with junk - ToEventTestCase{ - entry: sdjournal.JournalEntry{ - Fields: map[string]string{ - sdjournal.SD_JOURNAL_FIELD_PRIORITY: "123456, ", - }, - }, - expectedFields: common.MapStr{ - "syslog": common.MapStr{ - "priority": int64(123456), - }, - "log": common.MapStr{ - "syslog": common.MapStr{ - "priority": int64(123456), - }, - }, - }, - }, - // 'syslog.pid' field with user append - ToEventTestCase{ - entry: sdjournal.JournalEntry{ - Fields: map[string]string{ - sdjournal.SD_JOURNAL_FIELD_SYSLOG_PID: "123456,root", - }, - }, - expectedFields: common.MapStr{ - "syslog": common.MapStr{ - "pid": int64(123456), - }, - }, - }, - // 'syslog.pid' field empty - ToEventTestCase{ - entry: sdjournal.JournalEntry{ - Fields: map[string]string{ - sdjournal.SD_JOURNAL_FIELD_SYSLOG_PID: "", - }, - }, - expectedFields: common.MapStr{ - "syslog": common.MapStr{ - "pid": "", - }, - }, - }, - // custom field - ToEventTestCase{ - entry: sdjournal.JournalEntry{ - Fields: map[string]string{ - "my_custom_field": "value", - }, - }, - expectedFields: common.MapStr{ - "journald": common.MapStr{ - "custom": common.MapStr{ - "my_custom_field": "value", - }, - }, - }, - }, - // dropped field - ToEventTestCase{ - entry: sdjournal.JournalEntry{ - Fields: map[string]string{ - "_SOURCE_MONOTONIC_TIMESTAMP": "value", - }, - }, - expectedFields: common.MapStr{}, - }, - } - - instance.SetupJournalMetrics() - r, err := NewLocal(Config{Path: "dummy.journal"}, nil, checkpoint.JournalState{}, logp.NewLogger("test")) - if err != nil { - t.Fatalf("error creating test journal: %v", err) - } - for _, test := range tests { - event := r.toEvent(&test.entry) - event.Fields.Delete("event") - assert.True(t, reflect.DeepEqual(event.Fields, test.expectedFields)) - } -} - -func TestSetupMatches(t *testing.T) { - tests := []SetupMatchesTestCase{ - // correct filter expression - SetupMatchesTestCase{ - matches: []string{"systemd.unit=nginx"}, - expectError: false, - }, - // custom field - SetupMatchesTestCase{ - matches: []string{"_MY_CUSTOM_FIELD=value"}, - expectError: false, - }, - // incorrect separator - SetupMatchesTestCase{ - matches: []string{"systemd.unit~nginx"}, - expectError: true, - }, - } - journal, err := sdjournal.NewJournal() - if err != nil { - t.Fatalf("error while creating test journal: %v", err) - } - - for _, test := range tests { - err = setupMatches(journal, test.matches) - if err != nil && !test.expectError { - t.Errorf("unexpected outcome of setupMatches: error: '%v', expected error: %v", err, test.expectError) - } - } -}