2019-11-02 14:00:06 +01:00
|
|
|
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
|
2017-09-25 14:41:43 +02:00
|
|
|
|
2019-11-02 17:24:34 +01:00
|
|
|
#ifndef ICINGADB_H
|
|
|
|
#define ICINGADB_H
|
2017-09-25 14:41:43 +02:00
|
|
|
|
2019-10-29 18:36:16 +01:00
|
|
|
#include "icingadb/icingadb-ti.hpp"
|
2019-11-02 18:01:31 +01:00
|
|
|
#include "icingadb/redisconnection.hpp"
|
2022-06-27 16:33:25 +02:00
|
|
|
#include "base/atomic.hpp"
|
2022-01-31 12:38:02 +01:00
|
|
|
#include "base/bulker.hpp"
|
2017-09-25 14:41:43 +02:00
|
|
|
#include "base/timer.hpp"
|
|
|
|
#include "base/workqueue.hpp"
|
2019-11-02 18:01:31 +01:00
|
|
|
#include "icinga/customvarobject.hpp"
|
2018-11-15 17:04:03 +01:00
|
|
|
#include "icinga/checkable.hpp"
|
2018-12-07 16:33:10 +01:00
|
|
|
#include "icinga/service.hpp"
|
2018-11-30 09:28:20 +01:00
|
|
|
#include "icinga/downtime.hpp"
|
2019-11-02 18:01:31 +01:00
|
|
|
#include "remote/messageorigin.hpp"
|
2021-07-28 14:28:21 +02:00
|
|
|
#include <atomic>
|
2022-01-31 12:38:02 +01:00
|
|
|
#include <chrono>
|
2022-03-01 15:28:03 +01:00
|
|
|
#include <future>
|
2018-10-30 12:51:55 +01:00
|
|
|
#include <memory>
|
2021-02-24 13:37:36 +01:00
|
|
|
#include <mutex>
|
|
|
|
#include <set>
|
2021-03-23 12:37:32 +01:00
|
|
|
#include <unordered_map>
|
2022-08-10 11:40:53 +02:00
|
|
|
#include <unordered_set>
|
2019-12-06 14:16:27 +01:00
|
|
|
#include <utility>
|
2017-09-25 14:41:43 +02:00
|
|
|
|
|
|
|
namespace icinga
|
|
|
|
{
|
|
|
|
|
|
|
|
/**
|
2019-10-29 18:36:16 +01:00
|
|
|
* @ingroup icingadb
|
2017-09-25 14:41:43 +02:00
|
|
|
*/
|
2019-10-29 17:32:29 +01:00
|
|
|
class IcingaDB : public ObjectImpl<IcingaDB>
|
2017-09-25 14:41:43 +02:00
|
|
|
{
|
|
|
|
public:
|
2019-10-29 17:32:29 +01:00
|
|
|
DECLARE_OBJECT(IcingaDB);
|
|
|
|
DECLARE_OBJECTNAME(IcingaDB);
|
2017-09-25 14:41:43 +02:00
|
|
|
|
2019-10-29 17:32:29 +01:00
|
|
|
IcingaDB();
|
2017-09-25 14:41:43 +02:00
|
|
|
|
2018-05-15 14:43:01 +02:00
|
|
|
static void ConfigStaticInitialize();
|
2017-09-25 14:41:43 +02:00
|
|
|
|
2021-07-22 14:34:07 +02:00
|
|
|
void Validate(int types, const ValidationUtils& utils) override;
|
2017-09-25 14:41:43 +02:00
|
|
|
virtual void Start(bool runtimeCreated) override;
|
|
|
|
virtual void Stop(bool runtimeRemoved) override;
|
|
|
|
|
2021-10-14 12:00:59 +02:00
|
|
|
String GetEnvironmentId() const override;
|
|
|
|
|
2022-06-01 11:38:17 +02:00
|
|
|
inline RedisConnection::Ptr GetConnection()
|
|
|
|
{
|
2022-06-27 16:33:25 +02:00
|
|
|
return m_RconLocked.load();
|
2022-06-01 11:38:17 +02:00
|
|
|
}
|
|
|
|
|
2022-04-19 13:19:54 +02:00
|
|
|
template<class T>
|
|
|
|
static void AddKvsToMap(const Array::Ptr& kvs, T& map)
|
|
|
|
{
|
|
|
|
Value* key = nullptr;
|
|
|
|
ObjectLock oLock (kvs);
|
|
|
|
|
|
|
|
for (auto& kv : kvs) {
|
|
|
|
if (key) {
|
|
|
|
map.emplace(std::move(*key), std::move(kv));
|
|
|
|
key = nullptr;
|
|
|
|
} else {
|
|
|
|
key = &kv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-22 14:34:07 +02:00
|
|
|
protected:
|
|
|
|
void ValidateTlsProtocolmin(const Lazy<String>& lvalue, const ValidationUtils& utils) override;
|
2021-07-26 16:14:51 +02:00
|
|
|
void ValidateConnectTimeout(const Lazy<double>& lvalue, const ValidationUtils& utils) override;
|
2021-07-22 14:34:07 +02:00
|
|
|
|
2017-09-25 14:41:43 +02:00
|
|
|
private:
|
2021-02-24 13:37:36 +01:00
|
|
|
class DumpedGlobals
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
void Reset();
|
|
|
|
bool IsNew(const String& id);
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::set<String> m_Ids;
|
|
|
|
std::mutex m_Mutex;
|
|
|
|
};
|
|
|
|
|
2022-01-12 15:16:26 +01:00
|
|
|
enum StateUpdate
|
|
|
|
{
|
|
|
|
Volatile = 1ull << 0,
|
|
|
|
RuntimeOnly = 1ull << 1,
|
|
|
|
Full = Volatile | RuntimeOnly,
|
|
|
|
};
|
|
|
|
|
2021-01-19 10:56:54 +01:00
|
|
|
void OnConnectedHandler();
|
2017-09-25 14:41:43 +02:00
|
|
|
|
2018-05-15 14:43:01 +02:00
|
|
|
void PublishStatsTimerHandler();
|
|
|
|
void PublishStats();
|
2017-09-25 14:41:43 +02:00
|
|
|
|
|
|
|
/* config & status dump */
|
2018-05-15 14:43:01 +02:00
|
|
|
void UpdateAllConfigObjects();
|
2019-06-27 15:53:55 +02:00
|
|
|
std::vector<std::vector<intrusive_ptr<ConfigObject>>> ChunkObjects(std::vector<intrusive_ptr<ConfigObject>> objects, size_t chunkSize);
|
2021-03-23 12:37:32 +01:00
|
|
|
void DeleteKeys(const RedisConnection::Ptr& conn, const std::vector<String>& keys, RedisConnection::QueryPriority priority);
|
2021-03-04 17:29:39 +01:00
|
|
|
std::vector<String> GetTypeOverwriteKeys(const String& type);
|
2021-03-17 17:36:59 +01:00
|
|
|
std::vector<String> GetTypeDumpSignalKeys(const Type::Ptr& type);
|
2019-09-18 12:04:59 +02:00
|
|
|
void InsertObjectDependencies(const ConfigObject::Ptr& object, const String typeName, std::map<String, std::vector<String>>& hMSets,
|
2021-04-22 09:09:54 +02:00
|
|
|
std::vector<Dictionary::Ptr>& runtimeUpdates, bool runtimeUpdate);
|
2022-01-12 15:16:26 +01:00
|
|
|
void UpdateState(const Checkable::Ptr& checkable, StateUpdate mode);
|
2018-10-18 14:39:59 +02:00
|
|
|
void SendConfigUpdate(const ConfigObject::Ptr& object, bool runtimeUpdate);
|
2019-09-18 12:04:59 +02:00
|
|
|
void CreateConfigUpdate(const ConfigObject::Ptr& object, const String type, std::map<String, std::vector<String>>& hMSets,
|
2021-04-22 09:09:54 +02:00
|
|
|
std::vector<Dictionary::Ptr>& runtimeUpdates, bool runtimeUpdate);
|
2017-10-12 08:51:13 +02:00
|
|
|
void SendConfigDelete(const ConfigObject::Ptr& object);
|
2021-07-29 12:13:32 +02:00
|
|
|
void SendStateChange(const ConfigObject::Ptr& object, const CheckResult::Ptr& cr, StateType type);
|
2021-04-22 09:09:54 +02:00
|
|
|
void AddObjectDataToRuntimeUpdates(std::vector<Dictionary::Ptr>& runtimeUpdates, const String& objectKey,
|
|
|
|
const String& redisKey, const Dictionary::Ptr& data);
|
2021-10-08 16:43:09 +02:00
|
|
|
void DeleteRelationship(const String& id, const String& redisKeyWithoutPrefix, bool hasChecksum = false);
|
2019-10-02 12:01:51 +02:00
|
|
|
|
|
|
|
void SendSentNotification(
|
2019-11-07 15:57:31 +01:00
|
|
|
const Notification::Ptr& notification, const Checkable::Ptr& checkable, const std::set<User::Ptr>& users,
|
2021-10-08 18:25:19 +02:00
|
|
|
NotificationType type, const CheckResult::Ptr& cr, const String& author, const String& text, double sendTime
|
2019-10-02 12:01:51 +02:00
|
|
|
);
|
|
|
|
|
2019-10-02 14:32:10 +02:00
|
|
|
void SendStartedDowntime(const Downtime::Ptr& downtime);
|
|
|
|
void SendRemovedDowntime(const Downtime::Ptr& downtime);
|
2019-10-02 15:53:52 +02:00
|
|
|
void SendAddedComment(const Comment::Ptr& comment);
|
|
|
|
void SendRemovedComment(const Comment::Ptr& comment);
|
2019-12-04 16:06:04 +01:00
|
|
|
void SendFlappingChange(const Checkable::Ptr& checkable, double changeTime, double flappingLastChange);
|
2019-11-27 13:13:58 +01:00
|
|
|
void SendNextUpdate(const Checkable::Ptr& checkable);
|
2019-12-04 15:19:03 +01:00
|
|
|
void SendAcknowledgementSet(const Checkable::Ptr& checkable, const String& author, const String& comment, AcknowledgementType type, bool persistent, double changeTime, double expiry);
|
2019-12-04 16:06:04 +01:00
|
|
|
void SendAcknowledgementCleared(const Checkable::Ptr& checkable, const String& removedBy, double changeTime, double ackLastChange);
|
2021-10-08 16:43:09 +02:00
|
|
|
void SendNotificationUsersChanged(const Notification::Ptr& notification, const Array::Ptr& oldValues, const Array::Ptr& newValues);
|
|
|
|
void SendNotificationUserGroupsChanged(const Notification::Ptr& notification, const Array::Ptr& oldValues, const Array::Ptr& newValues);
|
|
|
|
void SendTimePeriodRangesChanged(const TimePeriod::Ptr& timeperiod, const Dictionary::Ptr& oldValues, const Dictionary::Ptr& newValues);
|
|
|
|
void SendTimePeriodIncludesChanged(const TimePeriod::Ptr& timeperiod, const Array::Ptr& oldValues, const Array::Ptr& newValues);
|
|
|
|
void SendTimePeriodExcludesChanged(const TimePeriod::Ptr& timeperiod, const Array::Ptr& oldValues, const Array::Ptr& newValues);
|
|
|
|
template<class T>
|
|
|
|
void SendGroupsChanged(const ConfigObject::Ptr& command, const Array::Ptr& oldValues, const Array::Ptr& newValues);
|
|
|
|
void SendCommandEnvChanged(const ConfigObject::Ptr& command, const Dictionary::Ptr& oldValues, const Dictionary::Ptr& newValues);
|
|
|
|
void SendCommandArgumentsChanged(const ConfigObject::Ptr& command, const Dictionary::Ptr& oldValues, const Dictionary::Ptr& newValues);
|
|
|
|
void SendCustomVarsChanged(const ConfigObject::Ptr& object, const Dictionary::Ptr& oldValues, const Dictionary::Ptr& newValues);
|
2019-10-02 14:32:10 +02:00
|
|
|
|
2022-01-31 12:38:02 +01:00
|
|
|
void ForwardHistoryEntries();
|
|
|
|
|
2018-11-12 17:45:32 +01:00
|
|
|
std::vector<String> UpdateObjectAttrs(const ConfigObject::Ptr& object, int fieldType, const String& typeNameOverride);
|
2018-11-15 17:04:03 +01:00
|
|
|
Dictionary::Ptr SerializeState(const Checkable::Ptr& checkable);
|
2017-09-25 14:41:43 +02:00
|
|
|
|
2018-06-08 11:38:36 +02:00
|
|
|
/* Stats */
|
|
|
|
Dictionary::Ptr GetStats();
|
|
|
|
|
2017-09-25 14:41:43 +02:00
|
|
|
/* utilities */
|
|
|
|
static String FormatCheckSumBinary(const String& str);
|
2018-12-03 15:47:05 +01:00
|
|
|
static String FormatCommandLine(const Value& commandLine);
|
2019-10-09 14:08:03 +02:00
|
|
|
static long long TimestampToMilliseconds(double timestamp);
|
2021-04-22 09:09:54 +02:00
|
|
|
static String IcingaToStreamValue(const Value& value);
|
2021-10-08 16:43:09 +02:00
|
|
|
static std::vector<Value> GetArrayDeletedValues(const Array::Ptr& arrayOld, const Array::Ptr& arrayNew);
|
|
|
|
static std::vector<String> GetDictionaryDeletedKeys(const Dictionary::Ptr& dictOld, const Dictionary::Ptr& dictNew);
|
2017-09-25 14:41:43 +02:00
|
|
|
|
2018-08-06 10:55:24 +02:00
|
|
|
static String GetObjectIdentifier(const ConfigObject::Ptr& object);
|
Icinga DB: make icinga:history:stream:*#event_id deterministic
... i.e. UUID -> SHA1(env, eventType, x...) given that SHA1(env, x...) = type-specific ID.
Rationale: allow both masters to write the same history concurrently (while not
in split-brain), so that REPLACE INTO deduplicates the same events written twice.
* ack: SHA1(env, "ack_set"|"ack_clear", checkable.name, setTime)
* comment: SHA1(env, "comment_add"|"comment_remove", comment.name)
* downtime: SHA1(env, "downtime_start"|"downtime_end", downtime.name)
* flapping: SHA1(env, "flapping_start"|"flapping_end", checkable.name, startTime)
* notification: SHA1(env, "notification", notification.name, notificationType, sendTime)
* state: SHA1(env, "state_change", checkable.name, changeTime)
2021-10-11 17:36:40 +02:00
|
|
|
static String CalcEventID(const char* eventType, const ConfigObject::Ptr& object, double eventTime = 0, NotificationType nt = NotificationType(0));
|
2021-10-08 18:25:19 +02:00
|
|
|
static const char* GetNotificationTypeByEnum(NotificationType type);
|
2021-10-08 16:43:09 +02:00
|
|
|
static Dictionary::Ptr SerializeVars(const Dictionary::Ptr& vars);
|
2017-09-25 14:41:43 +02:00
|
|
|
|
2017-10-02 10:05:56 +02:00
|
|
|
static String HashValue(const Value& value);
|
2018-06-07 12:49:38 +02:00
|
|
|
static String HashValue(const Value& value, const std::set<String>& propertiesBlacklist, bool propertiesWhitelist = false);
|
2017-09-25 14:41:43 +02:00
|
|
|
|
2018-10-29 13:48:15 +01:00
|
|
|
static String GetLowerCaseTypeNameDB(const ConfigObject::Ptr& obj);
|
2018-11-30 17:01:42 +01:00
|
|
|
static bool PrepareObject(const ConfigObject::Ptr& object, Dictionary::Ptr& attributes, Dictionary::Ptr& checkSums);
|
2018-10-29 13:48:15 +01:00
|
|
|
|
2022-01-05 12:47:54 +01:00
|
|
|
static void ReachabilityChangeHandler(const std::set<Checkable::Ptr>& children);
|
2019-10-02 13:14:31 +02:00
|
|
|
static void StateChangeHandler(const ConfigObject::Ptr& object, const CheckResult::Ptr& cr, StateType type);
|
2017-09-25 14:41:43 +02:00
|
|
|
static void VersionChangedHandler(const ConfigObject::Ptr& object);
|
2019-10-02 14:32:10 +02:00
|
|
|
static void DowntimeStartedHandler(const Downtime::Ptr& downtime);
|
|
|
|
static void DowntimeRemovedHandler(const Downtime::Ptr& downtime);
|
2017-09-25 14:41:43 +02:00
|
|
|
|
2019-10-02 12:01:51 +02:00
|
|
|
static void NotificationSentToAllUsersHandler(
|
|
|
|
const Notification::Ptr& notification, const Checkable::Ptr& checkable, const std::set<User::Ptr>& users,
|
|
|
|
NotificationType type, const CheckResult::Ptr& cr, const String& author, const String& text
|
|
|
|
);
|
|
|
|
|
2019-10-02 15:53:52 +02:00
|
|
|
static void CommentAddedHandler(const Comment::Ptr& comment);
|
|
|
|
static void CommentRemovedHandler(const Comment::Ptr& comment);
|
2019-12-04 16:06:04 +01:00
|
|
|
static void FlappingChangeHandler(const Checkable::Ptr& checkable, double changeTime);
|
2019-11-27 13:13:58 +01:00
|
|
|
static void NewCheckResultHandler(const Checkable::Ptr& checkable);
|
2022-09-05 15:28:36 +02:00
|
|
|
static void NextCheckUpdatedHandler(const Checkable::Ptr& checkable);
|
2022-01-12 16:39:15 +01:00
|
|
|
static void HostProblemChangedHandler(const Service::Ptr& service);
|
2019-12-04 15:19:03 +01:00
|
|
|
static void AcknowledgementSetHandler(const Checkable::Ptr& checkable, const String& author, const String& comment, AcknowledgementType type, bool persistent, double changeTime, double expiry);
|
|
|
|
static void AcknowledgementClearedHandler(const Checkable::Ptr& checkable, const String& removedBy, double changeTime);
|
2021-10-08 16:43:09 +02:00
|
|
|
static void NotificationUsersChangedHandler(const Notification::Ptr& notification, const Array::Ptr& oldValues, const Array::Ptr& newValues);
|
|
|
|
static void NotificationUserGroupsChangedHandler(const Notification::Ptr& notification, const Array::Ptr& oldValues, const Array::Ptr& newValues);
|
|
|
|
static void TimePeriodRangesChangedHandler(const TimePeriod::Ptr& timeperiod, const Dictionary::Ptr& oldValues, const Dictionary::Ptr& newValues);
|
|
|
|
static void TimePeriodIncludesChangedHandler(const TimePeriod::Ptr& timeperiod, const Array::Ptr& oldValues, const Array::Ptr& newValues);
|
|
|
|
static void TimePeriodExcludesChangedHandler(const TimePeriod::Ptr& timeperiod, const Array::Ptr& oldValues, const Array::Ptr& newValues);
|
|
|
|
static void UserGroupsChangedHandler(const User::Ptr& user, const Array::Ptr&, const Array::Ptr& newValues);
|
|
|
|
static void HostGroupsChangedHandler(const Host::Ptr& host, const Array::Ptr& oldValues, const Array::Ptr& newValues);
|
|
|
|
static void ServiceGroupsChangedHandler(const Service::Ptr& service, const Array::Ptr& oldValues, const Array::Ptr& newValues);
|
|
|
|
static void CommandEnvChangedHandler(const ConfigObject::Ptr& command, const Dictionary::Ptr& oldValues, const Dictionary::Ptr& newValues);
|
|
|
|
static void CommandArgumentsChangedHandler(const ConfigObject::Ptr& command, const Dictionary::Ptr& oldValues, const Dictionary::Ptr& newValues);
|
|
|
|
static void CustomVarsChangedHandler(const ConfigObject::Ptr& object, const Dictionary::Ptr& oldValues, const Dictionary::Ptr& newValues);
|
2019-10-02 15:53:52 +02:00
|
|
|
|
2018-05-15 14:43:01 +02:00
|
|
|
void AssertOnWorkQueue();
|
2017-09-25 14:41:43 +02:00
|
|
|
|
|
|
|
void ExceptionHandler(boost::exception_ptr exp);
|
|
|
|
|
2021-03-23 12:37:32 +01:00
|
|
|
static std::vector<Type::Ptr> GetTypes();
|
|
|
|
|
2022-06-08 17:14:09 +02:00
|
|
|
static void InitEnvironmentId();
|
|
|
|
static void PersistEnvironmentId();
|
|
|
|
|
2017-09-25 14:41:43 +02:00
|
|
|
Timer::Ptr m_StatsTimer;
|
2021-02-10 17:18:09 +01:00
|
|
|
WorkQueue m_WorkQueue{0, 1, LogNotice};
|
2018-07-10 11:09:37 +02:00
|
|
|
|
2022-03-01 15:28:03 +01:00
|
|
|
std::future<void> m_HistoryThread;
|
2022-01-31 12:38:02 +01:00
|
|
|
Bulker<RedisConnection::Query> m_HistoryBulker {4096, std::chrono::milliseconds(250)};
|
|
|
|
|
2018-07-10 11:09:37 +02:00
|
|
|
String m_PrefixConfigObject;
|
|
|
|
String m_PrefixConfigCheckSum;
|
|
|
|
|
2017-09-25 14:41:43 +02:00
|
|
|
bool m_ConfigDumpInProgress;
|
2018-10-18 14:39:59 +02:00
|
|
|
bool m_ConfigDumpDone;
|
2017-09-25 14:41:43 +02:00
|
|
|
|
2018-10-18 14:39:59 +02:00
|
|
|
RedisConnection::Ptr m_Rcon;
|
2022-06-27 16:33:25 +02:00
|
|
|
// m_RconLocked containes a copy of the value in m_Rcon where all accesses are guarded by a mutex to allow safe
|
|
|
|
// concurrent access like from the icingadb check command. It's a copy to still allow fast access without additional
|
|
|
|
// syncronization to m_Rcon within the IcingaDB feature itself.
|
|
|
|
Locked<RedisConnection::Ptr> m_RconLocked;
|
2021-03-23 12:37:32 +01:00
|
|
|
std::unordered_map<ConfigType*, RedisConnection::Ptr> m_Rcons;
|
2021-07-28 14:28:21 +02:00
|
|
|
std::atomic_size_t m_PendingRcons;
|
2021-02-24 13:37:36 +01:00
|
|
|
|
|
|
|
struct {
|
|
|
|
DumpedGlobals CustomVar, ActionUrl, NotesUrl, IconImage;
|
|
|
|
} m_DumpedGlobals;
|
2021-03-19 15:31:19 +01:00
|
|
|
|
2022-06-08 17:14:09 +02:00
|
|
|
// m_EnvironmentId is shared across all IcingaDB objects (typically there is at most one, but it is perfectly fine
|
|
|
|
// to have multiple ones). It is initialized once (synchronized using m_EnvironmentIdInitMutex). After successful
|
|
|
|
// initialization, the value is read-only and can be accessed without further synchronization.
|
2021-03-19 15:31:19 +01:00
|
|
|
static String m_EnvironmentId;
|
2022-06-08 17:14:09 +02:00
|
|
|
static std::mutex m_EnvironmentIdInitMutex;
|
2022-08-10 11:40:53 +02:00
|
|
|
|
|
|
|
static std::unordered_set<Type*> m_IndexedTypes;
|
2018-10-18 14:39:59 +02:00
|
|
|
};
|
2017-09-25 14:41:43 +02:00
|
|
|
}
|
|
|
|
|
2019-11-02 17:24:34 +01:00
|
|
|
#endif /* ICINGADB_H */
|