Skip to content

Commit

Permalink
Added Processor class, grouping related stuff together
Browse files Browse the repository at this point in the history
Change-Id: I99d8f6672d88402c0c020564f1a3ed280dbe4293
  • Loading branch information
spt29 committed Nov 14, 2024
1 parent 4dca9f7 commit 08fdaa2
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 63 deletions.
38 changes: 23 additions & 15 deletions packages/livestatus/include/livestatus/TableStateHistory.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,24 @@ class TableStateHistory : public Table {
LogCache *log_cache_;
bool abort_query_;

class Processor {
public:
Processor(Query &query, const User &user, const LogPeriod &period);

[[nodiscard]] bool process(HostServiceState &hss) const;

// private:
Query *query_;
const User *user_;
const LogPeriod *period_;
};

enum class ModificationStatus { unchanged, changed };

void answerQueryInternal(Query &query, const User &user, const ICore &core,
LogEntryForwardIterator &it);

void handle_state_entry(Query &query, const User &user, const ICore &core,
void handle_state_entry(Processor &processor, const ICore &core,
const LogEntry *entry, bool only_update,
const TimePeriods &time_periods, bool is_host_entry,
state_info_t &state_info,
Expand All @@ -117,28 +129,24 @@ class TableStateHistory : public Table {
const IService *entry_service,
HostServiceKey key);

void handle_timeperiod_transition(Query &query, const User &user,
const ICore &core,
const LogPeriod &period,
void handle_timeperiod_transition(Processor &processor, const ICore &core,
const LogEntry *entry, bool only_update,
TimePeriods &time_periods,
const state_info_t &state_info);

void final_reports(Query &query, const User &user,
const state_info_t &state_info, const LogPeriod &period);

void process(Query &query, const User &user, const LogPeriod &period,
HostServiceState &hss);
void final_reports(Processor &processor, const state_info_t &state_info,
const LogPeriod &period);

void update(Query &query, const User &user, const ICore &core,
const LogPeriod &period, const LogEntry *entry,
void update(Processor &processor, const ICore &core, const LogEntry *entry,
HostServiceState &state, bool only_update,
const TimePeriods &time_periods);

ModificationStatus updateHostServiceState(
Query &query, const User &user, const ICore &core,
const LogPeriod &period, const LogEntry *entry, HostServiceState &hss,
bool only_update, const TimePeriods &time_periods);
ModificationStatus updateHostServiceState(Processor &processor,
const ICore &core,
const LogEntry *entry,
HostServiceState &hss,
bool only_update,
const TimePeriods &time_periods);
};

#endif // TableStateHistory_h
98 changes: 50 additions & 48 deletions packages/livestatus/src/TableStateHistory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ void TableStateHistory::answerQueryInternal(Query &query, const User &user,
state_info_t state_info;

auto *logger = core.loggerLivestatus();
auto period = LogPeriod::make(query);
const auto period = LogPeriod::make(query);
if (period.empty()) {
Debug(logger) << "empty query period " << period;
return;
Expand All @@ -337,6 +337,8 @@ void TableStateHistory::answerQueryInternal(Query &query, const User &user,
return;
}

Processor processor{query, user, period};

// From now on use getNextLogentry()
bool only_update = true;
bool in_nagios_initial_states = false;
Expand Down Expand Up @@ -371,7 +373,7 @@ void TableStateHistory::answerQueryInternal(Query &query, const User &user,
in_nagios_initial_states = false;
break;
case LogEntryKind::state_service_initial:
handle_state_entry(query, user, core, entry, only_update,
handle_state_entry(processor, core, entry, only_update,
time_periods, false, state_info, blacklist,
period);
break;
Expand All @@ -381,13 +383,13 @@ void TableStateHistory::answerQueryInternal(Query &query, const User &user,
case LogEntryKind::flapping_service:
set_unknown_to_unmonitored(in_nagios_initial_states,
state_info);
handle_state_entry(query, user, core, entry, only_update,
handle_state_entry(processor, core, entry, only_update,
time_periods, false, state_info, blacklist,
period);
in_nagios_initial_states = false;
break;
case LogEntryKind::state_host_initial:
handle_state_entry(query, user, core, entry, only_update,
handle_state_entry(processor, core, entry, only_update,
time_periods, true, state_info, blacklist,
period);
break;
Expand All @@ -397,15 +399,15 @@ void TableStateHistory::answerQueryInternal(Query &query, const User &user,
case LogEntryKind::flapping_host:
set_unknown_to_unmonitored(in_nagios_initial_states,
state_info);
handle_state_entry(query, user, core, entry, only_update,
handle_state_entry(processor, core, entry, only_update,
time_periods, true, state_info, blacklist,
period);
in_nagios_initial_states = false;
break;
case LogEntryKind::timeperiod_transition:
set_unknown_to_unmonitored(in_nagios_initial_states,
state_info);
handle_timeperiod_transition(query, user, core, period, entry,
handle_timeperiod_transition(processor, core, entry,
only_update, time_periods,
state_info);
in_nagios_initial_states = false;
Expand All @@ -420,12 +422,12 @@ void TableStateHistory::answerQueryInternal(Query &query, const User &user,
}

if (!abort_query_) {
final_reports(query, user, state_info, period);
final_reports(processor, state_info, period);
}
}

void TableStateHistory::handle_state_entry(
Query &query, const User &user, const ICore &core, const LogEntry *entry,
Processor &processor, const ICore &core, const LogEntry *entry,
bool only_update, const TimePeriods &time_periods, bool is_host_entry,
state_info_t &state_info, ObjectBlacklist &blacklist,
const LogPeriod &period) {
Expand Down Expand Up @@ -454,8 +456,7 @@ void TableStateHistory::handle_state_entry(
insert_new_state(entry, only_update, time_periods, state_info,
blacklist, period, entry_host, entry_service, key);
}
update(query, user, core, period, entry, *state_info[key], only_update,
time_periods);
update(processor, core, entry, *state_info[key], only_update, time_periods);
}

// static
Expand Down Expand Up @@ -542,14 +543,14 @@ void TableStateHistory::insert_new_state(
}

void TableStateHistory::handle_timeperiod_transition(
Query &query, const User &user, const ICore &core, const LogPeriod &period,
const LogEntry *entry, bool only_update, TimePeriods &time_periods,
Processor &processor, const ICore &core, const LogEntry *entry,
bool only_update, TimePeriods &time_periods,
const state_info_t &state_info) {
try {
time_periods.update(entry->options());
for (const auto &[key, hss] : state_info) {
updateHostServiceState(query, user, core, period, entry, *hss,
only_update, time_periods);
updateHostServiceState(processor, core, entry, *hss, only_update,
time_periods);
}
} catch (const std::logic_error &e) {
Warning(core.loggerLivestatus())
Expand All @@ -558,7 +559,7 @@ void TableStateHistory::handle_timeperiod_transition(
}
}

void TableStateHistory::final_reports(Query &query, const User &user,
void TableStateHistory::final_reports(Processor &processor,
const state_info_t &state_info,
const LogPeriod &period) {
for (const auto &[key, hss] : state_info) {
Expand All @@ -568,7 +569,7 @@ void TableStateHistory::final_reports(Query &query, const User &user,
// Log last known state up to nagios restart
hss->_time = hss->_last_known_time;
hss->_until = hss->_last_known_time;
process(query, user, period, *hss);
abort_query_ = processor.process(*hss);

// Set absent state
hss->_state = -1;
Expand All @@ -584,24 +585,23 @@ void TableStateHistory::final_reports(Query &query, const User &user,
hss->_time = period.until - 1s;
hss->_until = hss->_time;

process(query, user, period, *hss);
abort_query_ = processor.process(*hss);
}
}

void TableStateHistory::update(Query &query, const User &user,
const ICore &core, const LogPeriod &period,
void TableStateHistory::update(Processor &processor, const ICore &core,
const LogEntry *entry, HostServiceState &state,
bool only_update,
const TimePeriods &time_periods) {
auto state_changed = updateHostServiceState(
query, user, core, period, entry, state, only_update, time_periods);
auto state_changed = updateHostServiceState(processor, core, entry, state,
only_update, time_periods);
// Host downtime or state changes also affect its services
if (entry->kind() == LogEntryKind::alert_host ||
entry->kind() == LogEntryKind::state_host ||
entry->kind() == LogEntryKind::downtime_alert_host) {
if (state_changed == ModificationStatus::changed) {
for (auto &svc : state._services) {
updateHostServiceState(query, user, core, period, entry, *svc,
updateHostServiceState(processor, core, entry, *svc,
only_update, time_periods);
}
}
Expand All @@ -610,9 +610,8 @@ void TableStateHistory::update(Query &query, const User &user,

// NOLINTNEXTLINE(readability-function-cognitive-complexity)
TableStateHistory::ModificationStatus TableStateHistory::updateHostServiceState(
Query &query, const User &user, const ICore &core, const LogPeriod &period,
const LogEntry *entry, HostServiceState &hss, bool only_update,
const TimePeriods &time_periods) {
Processor &processor, const ICore &core, const LogEntry *entry,
HostServiceState &hss, bool only_update, const TimePeriods &time_periods) {
ModificationStatus state_changed{ModificationStatus::changed};

// Revive host / service if it was unmonitored
Expand All @@ -621,7 +620,7 @@ TableStateHistory::ModificationStatus TableStateHistory::updateHostServiceState(
hss._time = hss._last_known_time;
hss._until = hss._last_known_time;
if (!only_update) {
process(query, user, period, hss);
abort_query_ = processor.process(hss);
}

hss._may_no_longer_exist = false;
Expand Down Expand Up @@ -669,7 +668,7 @@ TableStateHistory::ModificationStatus TableStateHistory::updateHostServiceState(
if (hss._is_host) {
if (hss._state != entry->state()) {
if (!only_update) {
process(query, user, period, hss);
abort_query_ = processor.process(hss);
}
hss._state = entry->state();
hss._host_down = static_cast<int>(entry->state() > 0);
Expand All @@ -679,7 +678,7 @@ TableStateHistory::ModificationStatus TableStateHistory::updateHostServiceState(
}
} else if (hss._host_down != static_cast<int>(entry->state() > 0)) {
if (!only_update) {
process(query, user, period, hss);
abort_query_ = processor.process(hss);
}
hss._host_down = static_cast<int>(entry->state() > 0);
hss._debug_info = "SVC HOST STATE";
Expand All @@ -691,7 +690,7 @@ TableStateHistory::ModificationStatus TableStateHistory::updateHostServiceState(
case LogEntryKind::alert_service: {
if (hss._state != entry->state()) {
if (!only_update) {
process(query, user, period, hss);
abort_query_ = processor.process(hss);
}
hss._debug_info = "SVC ALERT";
hss._state = entry->state();
Expand All @@ -704,7 +703,7 @@ TableStateHistory::ModificationStatus TableStateHistory::updateHostServiceState(

if (hss._in_host_downtime != downtime_active) {
if (!only_update) {
process(query, user, period, hss);
abort_query_ = processor.process(hss);
}
hss._debug_info =
hss._is_host ? "HOST DOWNTIME" : "SVC HOST DOWNTIME";
Expand All @@ -722,7 +721,7 @@ TableStateHistory::ModificationStatus TableStateHistory::updateHostServiceState(
entry->state_type().starts_with("STARTED") ? 1 : 0;
if (hss._in_downtime != downtime_active) {
if (!only_update) {
process(query, user, period, hss);
abort_query_ = processor.process(hss);
}
hss._debug_info = "DOWNTIME SERVICE";
hss._in_downtime = downtime_active;
Expand All @@ -735,7 +734,7 @@ TableStateHistory::ModificationStatus TableStateHistory::updateHostServiceState(
entry->state_type().starts_with("STARTED") ? 1 : 0;
if (hss._is_flapping != flapping_active) {
if (!only_update) {
process(query, user, period, hss);
abort_query_ = processor.process(hss);
}
hss._debug_info = "FLAPPING ";
hss._is_flapping = flapping_active;
Expand All @@ -753,7 +752,7 @@ TableStateHistory::ModificationStatus TableStateHistory::updateHostServiceState(
tpt.name() == hss._notification_period) {
if (tpt.to() != hss._in_notification_period) {
if (!only_update) {
process(query, user, period, hss);
abort_query_ = processor.process(hss);
}
hss._debug_info = "TIMEPERIOD ";
hss._in_notification_period = tpt.to();
Expand All @@ -763,7 +762,7 @@ TableStateHistory::ModificationStatus TableStateHistory::updateHostServiceState(
if (hss._host != nullptr && tpt.name() == hss._service_period) {
if (tpt.to() != hss._in_service_period) {
if (!only_update) {
process(query, user, period, hss);
abort_query_ = processor.process(hss);
}
hss._debug_info = "TIMEPERIOD ";
hss._in_service_period = tpt.to();
Expand All @@ -790,20 +789,6 @@ TableStateHistory::ModificationStatus TableStateHistory::updateHostServiceState(
return state_changed;
}

void TableStateHistory::process(Query &query, const User &user,
const LogPeriod &period,
HostServiceState &hss) {
hss._duration = hss._until - hss._from;
hss.computePerStateDurations(period.duration());

// if (hss._duration > 0)
abort_query_ =
user.is_authorized_for_object(hss._host, hss._service, false) &&
!query.processDataset(Row{&hss});

hss._from = hss._until;
}

std::shared_ptr<Column> TableStateHistory::column(std::string colname) const {
try {
// First try to find column in the usual way
Expand All @@ -817,6 +802,23 @@ std::shared_ptr<Column> TableStateHistory::column(std::string colname) const {
}
}

TableStateHistory::Processor::Processor(Query &query, const User &user,
const LogPeriod &period)
: query_{&query}, user_{&user}, period_{&period} {}

bool TableStateHistory::Processor::process(HostServiceState &hss) const {
hss._duration = hss._until - hss._from;
hss.computePerStateDurations(period_->duration());

// if (hss._duration > 0)
auto abort_query =
user_->is_authorized_for_object(hss._host, hss._service, false) &&
!query_->processDataset(Row{&hss});

hss._from = hss._until;
return abort_query;
}

ObjectBlacklist::ObjectBlacklist(const Query &query, const User &user)
: query_{&query}
, user_{&user}
Expand Down

0 comments on commit 08fdaa2

Please sign in to comment.