icinga2/lib/icingadb/icingadb-utility.cpp

210 lines
4.8 KiB
C++
Raw Normal View History

2019-11-02 14:00:06 +01:00
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
2017-09-25 14:41:43 +02:00
2019-10-29 18:36:16 +01:00
#include "icingadb/icingadb.hpp"
2018-10-29 13:48:15 +01:00
#include "base/configtype.hpp"
#include "base/object-packer.hpp"
2017-09-25 14:41:43 +02:00
#include "base/logger.hpp"
#include "base/serializer.hpp"
#include "base/tlsutility.hpp"
#include "base/initialize.hpp"
#include "base/objectlock.hpp"
#include "base/array.hpp"
#include "base/scriptglobal.hpp"
2018-09-13 15:40:27 +02:00
#include "base/convert.hpp"
2019-11-02 11:59:58 +01:00
#include "base/json.hpp"
2019-11-02 18:01:31 +01:00
#include "icinga/customvarobject.hpp"
#include "icinga/checkcommand.hpp"
#include "icinga/notificationcommand.hpp"
#include "icinga/eventcommand.hpp"
#include "icinga/host.hpp"
#include <boost/algorithm/string.hpp>
#include <map>
#include <utility>
#include <vector>
2017-09-25 14:41:43 +02:00
using namespace icinga;
String IcingaDB::FormatCheckSumBinary(const String& str)
2017-09-25 14:41:43 +02:00
{
char output[20*2+1];
for (int i = 0; i < 20; i++)
sprintf(output + 2 * i, "%02x", str[i]);
return output;
}
String IcingaDB::FormatCommandLine(const Value& commandLine)
2018-12-03 15:47:05 +01:00
{
String result;
if (commandLine.IsObjectType<Array>()) {
Array::Ptr args = commandLine;
bool first = true;
ObjectLock olock(args);
for (const Value& arg : args) {
String token = "'" + Convert::ToString(arg) + "'";
if (first)
first = false;
else
result += String(1, ' ');
result += token;
}
} else if (!commandLine.IsEmpty()) {
result = commandLine;
boost::algorithm::replace_all(result, "\'", "\\'");
2018-12-03 16:31:06 +01:00
result = "'" + result + "'";
2018-12-03 15:47:05 +01:00
}
return result;
}
String IcingaDB::GetEnvironment()
{
return ConfigType::GetObjectsByType<IcingaApplication>()[0]->GetEnvironment();
}
String IcingaDB::GetObjectIdentifier(const ConfigObject::Ptr& object)
{
Type::Ptr type = object->GetReflectionType();
if (type == CheckCommand::TypeInstance || type == NotificationCommand::TypeInstance || type == EventCommand::TypeInstance)
return HashValue((Array::Ptr)new Array({GetEnvironment(), type->GetName(), object->GetName()}));
else
return HashValue((Array::Ptr)new Array({GetEnvironment(), object->GetName()}));
}
static const std::set<String> metadataWhitelist ({"package", "source_location", "templates"});
/**
* Prepare object's custom vars for being written to Redis
*
* object.vars = {
* "disks": {
* "disk": {},
* "disk /": {
* "disk_partitions": "/"
* }
* }
* }
*
* return {
* SHA1(PackObject([
* Environment,
* "disks",
* {
* "disk": {},
* "disk /": {
* "disk_partitions": "/"
* }
* }
* ])): {
* "envId": SHA1(Environment),
* "name_checksum": SHA1("disks"),
* "name": "disks",
* "value": {
* "disk": {},
* "disk /": {
* "disk_partitions": "/"
* }
* }
* }
* }
*
* @param object Config object with custom vars
*
* @return JSON-like data structure for Redis
*/
Dictionary::Ptr IcingaDB::SerializeVars(const CustomVarObject::Ptr& object)
{
Dictionary::Ptr vars = object->GetVars();
if (!vars)
return nullptr;
Dictionary::Ptr res = new Dictionary();
auto env (GetEnvironment());
auto envChecksum (SHA1(env));
2018-07-04 16:44:44 +02:00
ObjectLock olock(vars);
for (auto& kv : vars) {
res->Set(
SHA1(PackObject((Array::Ptr)new Array({env, kv.first, kv.second}))),
(Dictionary::Ptr)new Dictionary({
2019-09-25 11:03:33 +02:00
{"environment_id", envChecksum},
{"name_checksum", SHA1(kv.first)},
{"name", kv.first},
2019-07-18 15:02:47 +02:00
{"value", JsonEncode(kv.second)},
})
);
}
return res;
}
static const std::set<String> propertiesBlacklistEmpty;
String IcingaDB::HashValue(const Value& value)
{
return HashValue(value, propertiesBlacklistEmpty);
}
String IcingaDB::HashValue(const Value& value, const std::set<String>& propertiesBlacklist, bool propertiesWhitelist)
2017-09-25 14:41:43 +02:00
{
Value temp;
bool mutabl;
2017-09-25 14:41:43 +02:00
Type::Ptr type = value.GetReflectionType();
if (ConfigObject::TypeInstance->IsAssignableFrom(type)) {
2017-09-25 14:41:43 +02:00
temp = Serialize(value, FAConfig);
mutabl = true;
} else {
2017-09-25 14:41:43 +02:00
temp = value;
mutabl = false;
}
if (propertiesBlacklist.size() && temp.IsObject()) {
Dictionary::Ptr dict = dynamic_pointer_cast<Dictionary>((Object::Ptr)temp);
if (dict) {
if (!mutabl)
dict = dict->ShallowClone();
ObjectLock olock(dict);
if (propertiesWhitelist) {
auto current = dict->Begin();
auto propertiesBlacklistEnd = propertiesBlacklist.end();
while (current != dict->End()) {
if (propertiesBlacklist.find(current->first) == propertiesBlacklistEnd) {
dict->Remove(current++);
} else {
++current;
}
}
} else {
for (auto& property : propertiesBlacklist)
dict->Remove(property);
}
if (!mutabl)
temp = dict;
}
}
2017-09-25 14:41:43 +02:00
return SHA1(PackObject(temp));
2017-09-25 14:41:43 +02:00
}
String IcingaDB::GetLowerCaseTypeNameDB(const ConfigObject::Ptr& obj)
2018-10-29 13:48:15 +01:00
{
return obj->GetReflectionType()->GetName().ToLower();
2018-10-29 13:48:15 +01:00
}
long long IcingaDB::TimestampToMilliseconds(double timestamp) {
return static_cast<long long>(timestamp * 1000);
}