-
Notifications
You must be signed in to change notification settings - Fork 90
/
ConsumeJournald.cpp
230 lines (203 loc) · 10.5 KB
/
ConsumeJournald.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/**
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF 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.
*/
#include "ConsumeJournald.h"
#include <algorithm>
#include "date/date.h"
#include "fmt/format.h"
#include "utils/GeneralUtils.h"
#include "utils/OptionalUtils.h"
#include "core/ProcessContext.h"
#include "core/ProcessSession.h"
#include "core/Resource.h"
namespace org::apache::nifi::minifi::extensions::systemd {
namespace chr = std::chrono;
ConsumeJournald::ConsumeJournald(std::string_view name, const utils::Identifier &id, std::unique_ptr<libwrapper::LibWrapper>&& libwrapper)
:core::Processor{name, id}, libwrapper_{std::move(libwrapper)}
{}
void ConsumeJournald::initialize() {
setSupportedProperties(Properties);
setSupportedRelationships(Relationships);
worker_ = std::make_unique<utils::FifoExecutor>();
}
void ConsumeJournald::notifyStop() {
bool running = true;
if (!running_.compare_exchange_strong(running, false, std::memory_order_acq_rel) || !journal_) return;
worker_->enqueue([this] {
journal_ = nullptr;
}).get();
worker_ = nullptr;
}
void ConsumeJournald::onSchedule(core::ProcessContext& context, core::ProcessSessionFactory&) {
gsl_Expects(!running_ && worker_);
using JournalTypeEnum = systemd::JournalType;
const auto parse_payload_format = [](const std::string& property_value) -> std::optional<systemd::PayloadFormat> {
if (property_value == PAYLOAD_FORMAT_RAW) return systemd::PayloadFormat::Raw;
if (property_value == PAYLOAD_FORMAT_SYSLOG) return systemd::PayloadFormat::Syslog;
return std::nullopt;
};
const auto parse_journal_type = [](const std::string& property_value) -> std::optional<JournalTypeEnum> {
if (property_value == JOURNAL_TYPE_USER) return JournalTypeEnum::User;
if (property_value == JOURNAL_TYPE_SYSTEM) return JournalTypeEnum::System;
if (property_value == JOURNAL_TYPE_BOTH) return JournalTypeEnum::Both;
return std::nullopt;
};
batch_size_ = context.getProperty<size_t>(BatchSize).value();
payload_format_ = (context.getProperty(PayloadFormat) | utils::andThen(parse_payload_format)
| utils::orElse([]{ throw Exception{ExceptionType::PROCESSOR_EXCEPTION, "invalid payload format"}; }))
.value();
include_timestamp_ = context.getProperty<bool>(IncludeTimestamp).value();
const auto journal_type = (context.getProperty(JournalType) | utils::andThen(parse_journal_type)
| utils::orElse([]{ throw Exception{ExceptionType::PROCESSOR_EXCEPTION, "invalid journal type"}; }))
.value();
const auto process_old_messages = context.getProperty<bool>(ProcessOldMessages).value_or(false);
timestamp_format_ = [&context] {
auto tf_prop = (context.getProperty(TimestampFormat)
| utils::orElse([]{ throw Exception{ExceptionType::PROCESSOR_EXCEPTION, "invalid timestamp format" }; }))
.value();
if (tf_prop == "ISO" || tf_prop == "ISO 8601" || tf_prop == "ISO8601") return std::string{"%FT%T%Ez"};
return tf_prop;
}();
state_manager_ = context.getStateManager();
// All journal-related API calls are thread-agnostic, meaning they need to be called from the same thread. In our environment,
// where a processor can easily be scheduled on different threads, we ensure this by executing all library calls on a dedicated
// worker thread. This is why all such operations are dispatched to a thread and immediately waited for in the initiating thread.
journal_ = worker_->enqueue([this, journal_type]{ return libwrapper_->openJournal(journal_type); }).get();
const auto seek_default = [process_old_messages](libwrapper::Journal& journal) {
return process_old_messages ? journal.seekHead() : journal.seekTail();
};
worker_->enqueue([this, &seek_default] {
const auto cursor = state_manager_->get() | utils::transform([](std::unordered_map<std::string, std::string>&& m) { return m.at(CURSOR_KEY); });
if (!cursor) {
seek_default(*journal_);
} else {
const auto ret = journal_->seekCursor(cursor->c_str());
if (ret < 0) {
const auto error_message = std::generic_category().default_error_condition(-ret).message();
logger_->log_warn("Failed to seek to cursor: {}. Seeking to tail or head (depending on Process Old Messages property) instead. cursor=\"{}\"", error_message, *cursor);
seek_default(*journal_);
}
}
}).get();
running_ = true;
}
void ConsumeJournald::onTrigger(core::ProcessContext&, core::ProcessSession& session) {
if (!running_.load(std::memory_order_acquire)) return;
auto cursor_and_messages = getCursorAndMessageBatch().get();
auto messages = std::move(cursor_and_messages.second);
if (messages.empty()) {
yield();
return;
}
for (auto& msg: messages) {
const auto flow_file = session.create();
if (payload_format_ == systemd::PayloadFormat::Syslog) session.writeBuffer(flow_file, gsl::make_span(formatSyslogMessage(msg)));
for (auto& field: msg.fields) {
if (field.name == "MESSAGE" && payload_format_ == systemd::PayloadFormat::Raw) {
session.writeBuffer(flow_file, gsl::make_span(field.value));
} else {
flow_file->setAttribute(std::move(field.name), std::move(field.value));
}
}
if (include_timestamp_) flow_file->setAttribute("timestamp", date::format(timestamp_format_, chr::floor<chr::microseconds>(msg.timestamp)));
session.transfer(flow_file, Success);
}
state_manager_->set({{"cursor", std::move(cursor_and_messages.first)}});
}
std::optional<std::span<const char>> ConsumeJournald::enumerateJournalEntry(libwrapper::Journal& journal) {
const void* data_ptr{};
size_t data_length{};
const auto status_code = journal.enumerateData(&data_ptr, &data_length);
if (status_code == 0) return std::nullopt;
if (status_code < 0) throw SystemErrorException{ "sd_journal_enumerate_data", std::generic_category().default_error_condition(-status_code) };
gsl_Ensures(data_ptr && "if sd_journal_enumerate_data was successful, then data_ptr must be set");
gsl_Ensures(data_length > 0 && "if sd_journal_enumerate_data was successful, then data_length must be greater than zero");
const char* const data_str_ptr = reinterpret_cast<const char*>(data_ptr);
return gsl::make_span(data_str_ptr, data_length);
}
std::optional<ConsumeJournald::journal_field> ConsumeJournald::getNextField(libwrapper::Journal& journal) {
return enumerateJournalEntry(journal) | utils::transform([](std::span<const char> field) {
const auto eq_pos = std::find(std::begin(field), std::end(field), '=');
gsl_Ensures(eq_pos != std::end(field) && "field string must contain an equals sign");
const auto eq_idx = gsl::narrow<size_t>(eq_pos - std::begin(field));
return journal_field{
utils::span_to<std::string>(field.subspan(0, eq_idx)),
utils::span_to<std::string>(field.subspan(eq_idx + 1))
};
});
}
std::future<std::pair<std::string, std::vector<ConsumeJournald::journal_message>>> ConsumeJournald::getCursorAndMessageBatch() {
gsl_Expects(worker_);
return worker_->enqueue([this] {
std::vector<journal_message> messages;
messages.reserve(batch_size_);
for (size_t i = 0; i < batch_size_ && journal_->next() > 0; ++i) {
journal_message message;
std::optional<journal_field> field;
while ((field = getNextField(*journal_)).has_value()) {
message.fields.push_back(std::move(*field));
}
if (include_timestamp_ || payload_format_ == systemd::PayloadFormat::Syslog) {
message.timestamp = [this] {
uint64_t journal_timestamp_usec_since_epoch{};
journal_->getRealtimeUsec(&journal_timestamp_usec_since_epoch);
return std::chrono::system_clock::time_point{std::chrono::microseconds{journal_timestamp_usec_since_epoch}};
}();
}
messages.push_back(std::move(message));
}
return std::make_pair(getCursor(), messages);
});
}
std::string ConsumeJournald::formatSyslogMessage(const journal_message& msg) const {
gsl_Expects(msg.timestamp != decltype(msg.timestamp){});
const std::string* systemd_hostname = nullptr;
const std::string* syslog_pid = nullptr;
const std::string* systemd_pid = nullptr;
const std::string* syslog_identifier = nullptr;
const std::string* message = nullptr;
for (const auto& field: msg.fields) {
if (field.name == "_HOSTNAME") systemd_hostname = &field.value;
else if (field.name == "SYSLOG_PID") syslog_pid = &field.value;
else if (field.name == "_PID") systemd_pid = &field.value;
else if (field.name == "SYSLOG_IDENTIFIER") syslog_identifier = &field.value;
else if (field.name == "MESSAGE") message = &field.value;
else if (systemd_hostname && (syslog_pid || systemd_pid) && syslog_identifier && message) break;
}
gsl_Ensures(message && "MESSAGE is guaranteed to be present");
const auto pid_string = utils::optional_from_ptr(syslog_pid)
| utils::orElse([&] { return utils::optional_from_ptr(systemd_pid); })
| utils::transform([](const std::string* const pid) { return fmt::format("[{}]", *pid); });
return fmt::format("{} {} {}{}: {}",
date::format(timestamp_format_, chr::floor<chr::microseconds>(msg.timestamp)),
(utils::optional_from_ptr(systemd_hostname) | utils::transform(utils::dereference)).value_or("unknown_host"),
(utils::optional_from_ptr(syslog_identifier) | utils::transform(utils::dereference)).value_or("unknown_process"),
pid_string.value_or(std::string{}),
*message);
}
std::string ConsumeJournald::getCursor() const {
const auto cursor = [this] {
gsl::owner<char*> cursor_out;
const auto err_code = journal_->getCursor(&cursor_out);
if (err_code < 0) throw SystemErrorException{"sd_journal_get_cursor", std::generic_category().default_error_condition(-err_code)};
gsl_Ensures(cursor_out);
return std::unique_ptr<char, utils::FreeDeleter>{cursor_out};
}();
return std::string{cursor.get()};
}
REGISTER_RESOURCE(ConsumeJournald, Processor);
} // namespace org::apache::nifi::minifi::extensions::systemd