Merge pull request #9775 from Icinga/icingadb-service-crashes-on-negative-downtime-duration-or-end-before-start-9774

Icinga DB: don't write negative Downtime durations into Redis
This commit is contained in:
Julian Brost 2023-05-31 11:37:42 +02:00 committed by GitHub
commit 8a42c3bf18
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -30,6 +30,7 @@
#include "icinga/timeperiod.hpp" #include "icinga/timeperiod.hpp"
#include "icinga/pluginutility.hpp" #include "icinga/pluginutility.hpp"
#include "remote/zone.hpp" #include "remote/zone.hpp"
#include <algorithm>
#include <chrono> #include <chrono>
#include <cstdint> #include <cstdint>
#include <iterator> #include <iterator>
@ -1431,20 +1432,23 @@ bool IcingaDB::PrepareObject(const ConfigObject::Ptr& object, Dictionary::Ptr& a
attributes->Set("entry_time", TimestampToMilliseconds(downtime->GetEntryTime())); attributes->Set("entry_time", TimestampToMilliseconds(downtime->GetEntryTime()));
attributes->Set("scheduled_start_time", TimestampToMilliseconds(downtime->GetStartTime())); attributes->Set("scheduled_start_time", TimestampToMilliseconds(downtime->GetStartTime()));
attributes->Set("scheduled_end_time", TimestampToMilliseconds(downtime->GetEndTime())); attributes->Set("scheduled_end_time", TimestampToMilliseconds(downtime->GetEndTime()));
attributes->Set("scheduled_duration", TimestampToMilliseconds(downtime->GetEndTime() - downtime->GetStartTime())); attributes->Set("scheduled_duration", TimestampToMilliseconds(std::max(0.0, downtime->GetEndTime() - downtime->GetStartTime())));
attributes->Set("flexible_duration", TimestampToMilliseconds(downtime->GetDuration())); attributes->Set("flexible_duration", TimestampToMilliseconds(std::max(0.0, downtime->GetDuration())));
attributes->Set("is_flexible", !downtime->GetFixed()); attributes->Set("is_flexible", !downtime->GetFixed());
attributes->Set("is_in_effect", downtime->IsInEffect()); attributes->Set("is_in_effect", downtime->IsInEffect());
if (downtime->IsInEffect()) { if (downtime->IsInEffect()) {
attributes->Set("start_time", TimestampToMilliseconds(downtime->GetTriggerTime())); attributes->Set("start_time", TimestampToMilliseconds(downtime->GetTriggerTime()));
attributes->Set("end_time", TimestampToMilliseconds(downtime->GetFixed() ? downtime->GetEndTime() : (downtime->GetTriggerTime() + downtime->GetDuration())));
attributes->Set("end_time", TimestampToMilliseconds(
downtime->GetFixed() ? downtime->GetEndTime() : (downtime->GetTriggerTime() + std::max(0.0, downtime->GetDuration()))
));
} }
auto duration = downtime->GetDuration(); auto duration = downtime->GetDuration();
if (downtime->GetFixed()) { if (downtime->GetFixed()) {
duration = downtime->GetEndTime() - downtime->GetStartTime(); duration = downtime->GetEndTime() - downtime->GetStartTime();
} }
attributes->Set("duration", TimestampToMilliseconds(duration)); attributes->Set("duration", TimestampToMilliseconds(std::max(0.0, duration)));
Host::Ptr host; Host::Ptr host;
Service::Ptr service; Service::Ptr service;
@ -1834,7 +1838,7 @@ void IcingaDB::SendStartedDowntime(const Downtime::Ptr& downtime)
"author", Utility::ValidateUTF8(downtime->GetAuthor()), "author", Utility::ValidateUTF8(downtime->GetAuthor()),
"comment", Utility::ValidateUTF8(downtime->GetComment()), "comment", Utility::ValidateUTF8(downtime->GetComment()),
"is_flexible", Convert::ToString((unsigned short)!downtime->GetFixed()), "is_flexible", Convert::ToString((unsigned short)!downtime->GetFixed()),
"flexible_duration", Convert::ToString(TimestampToMilliseconds(downtime->GetDuration())), "flexible_duration", Convert::ToString(TimestampToMilliseconds(std::max(0.0, downtime->GetDuration()))),
"scheduled_start_time", Convert::ToString(TimestampToMilliseconds(downtime->GetStartTime())), "scheduled_start_time", Convert::ToString(TimestampToMilliseconds(downtime->GetStartTime())),
"scheduled_end_time", Convert::ToString(TimestampToMilliseconds(downtime->GetEndTime())), "scheduled_end_time", Convert::ToString(TimestampToMilliseconds(downtime->GetEndTime())),
"has_been_cancelled", Convert::ToString((unsigned short)downtime->GetWasCancelled()), "has_been_cancelled", Convert::ToString((unsigned short)downtime->GetWasCancelled()),
@ -1867,7 +1871,7 @@ void IcingaDB::SendStartedDowntime(const Downtime::Ptr& downtime)
xAdd.emplace_back("start_time"); xAdd.emplace_back("start_time");
xAdd.emplace_back(Convert::ToString(TimestampToMilliseconds(downtime->GetTriggerTime()))); xAdd.emplace_back(Convert::ToString(TimestampToMilliseconds(downtime->GetTriggerTime())));
xAdd.emplace_back("end_time"); xAdd.emplace_back("end_time");
xAdd.emplace_back(Convert::ToString(TimestampToMilliseconds(downtime->GetTriggerTime() + downtime->GetDuration()))); xAdd.emplace_back(Convert::ToString(TimestampToMilliseconds(downtime->GetTriggerTime() + std::max(0.0, downtime->GetDuration()))));
} }
auto endpoint (Endpoint::GetLocalEndpoint()); auto endpoint (Endpoint::GetLocalEndpoint());
@ -1924,7 +1928,7 @@ void IcingaDB::SendRemovedDowntime(const Downtime::Ptr& downtime)
"cancelled_by", Utility::ValidateUTF8(downtime->GetRemovedBy()), "cancelled_by", Utility::ValidateUTF8(downtime->GetRemovedBy()),
"comment", Utility::ValidateUTF8(downtime->GetComment()), "comment", Utility::ValidateUTF8(downtime->GetComment()),
"is_flexible", Convert::ToString((unsigned short)!downtime->GetFixed()), "is_flexible", Convert::ToString((unsigned short)!downtime->GetFixed()),
"flexible_duration", Convert::ToString(TimestampToMilliseconds(downtime->GetDuration())), "flexible_duration", Convert::ToString(TimestampToMilliseconds(std::max(0.0, downtime->GetDuration()))),
"scheduled_start_time", Convert::ToString(TimestampToMilliseconds(downtime->GetStartTime())), "scheduled_start_time", Convert::ToString(TimestampToMilliseconds(downtime->GetStartTime())),
"scheduled_end_time", Convert::ToString(TimestampToMilliseconds(downtime->GetEndTime())), "scheduled_end_time", Convert::ToString(TimestampToMilliseconds(downtime->GetEndTime())),
"has_been_cancelled", Convert::ToString((unsigned short)downtime->GetWasCancelled()), "has_been_cancelled", Convert::ToString((unsigned short)downtime->GetWasCancelled()),
@ -1958,7 +1962,7 @@ void IcingaDB::SendRemovedDowntime(const Downtime::Ptr& downtime)
xAdd.emplace_back("start_time"); xAdd.emplace_back("start_time");
xAdd.emplace_back(Convert::ToString(TimestampToMilliseconds(downtime->GetTriggerTime()))); xAdd.emplace_back(Convert::ToString(TimestampToMilliseconds(downtime->GetTriggerTime())));
xAdd.emplace_back("end_time"); xAdd.emplace_back("end_time");
xAdd.emplace_back(Convert::ToString(TimestampToMilliseconds(downtime->GetTriggerTime() + downtime->GetDuration()))); xAdd.emplace_back(Convert::ToString(TimestampToMilliseconds(downtime->GetTriggerTime() + std::max(0.0, downtime->GetDuration()))));
} }
auto endpoint (Endpoint::GetLocalEndpoint()); auto endpoint (Endpoint::GetLocalEndpoint());