Redis: Dump host and service state events

fixes #5163
This commit is contained in:
Michael Friedrich 2017-04-12 16:58:08 +02:00
parent b680aedc23
commit 17615ac444
3 changed files with 85 additions and 17 deletions

View File

@ -195,6 +195,74 @@ void RedisWriter::SendStatusUpdate(const ConfigObject::Ptr& object, const String
String objectName = object->GetName();
ExecuteQuery({ "HSET", "icinga:status:" + typeName, objectName, jsonBody });
/* Icinga DB part for Icinga Web 2 */
Checkable::Ptr checkable = dynamic_pointer_cast<Checkable>(object);
if (checkable) {
Dictionary::Ptr attrs = new Dictionary();
String tableName;
String objectCheckSum = CalculateCheckSumString(objectName, true); //store binary checksum here
Host::Ptr host;
Service::Ptr service;
tie(host, service) = GetHostService(checkable);
if (service) {
tableName = "servicestate";
attrs->Set("service_checksum", objectCheckSum);
attrs->Set("host_checksum", CalculateCheckSumString(host->GetName(), true));
} else {
tableName = "hoststate";
attrs->Set("host_checksum", objectCheckSum);
}
attrs->Set("last_check", checkable->GetLastCheck());
attrs->Set("next_check", checkable->GetNextCheck());
attrs->Set("severity", 0); //TODO
/*
'host_checksum' => null,
'command' => null, // JSON, array
'execution_start' => null,
'execution_end' => null,
'schedule_start' => null,
'schedule_end' => null,
'exit_status' => null,
'output' => null,
'performance_data' => null, // JSON, array
10.0.3.12:6379> keys icinga:hoststate.*
1) "icinga:hoststate.~\xf5a\x91+\x03\x97\x99\xb5(\x16 CYm\xb1\xdf\x85\xa2\xcb"
10.0.3.12:6379> get "icinga:hoststate.~\xf5a\x91+\x03\x97\x99\xb5(\x16 CYm\xb1\xdf\x85\xa2\xcb"
"{\"command\":[\"\\/usr\\/lib\\/nagios\\/plugins\\/check_ping\",\"-H\",\"127.0.0.1\",\"-c\",\"5000,100%\",\"-w\",\"3000,80%\"],\"execution_start\":1492007581.7624,\"execution_end\":1492007585.7654,\"schedule_start\":1492007581.7609,\"schedule_end\":1492007585.7655,\"exit_status\":0,\"output\":\"PING OK - Packet loss = 0%, RTA = 0.08 ms\",\"performance_data\":[\"rta=0.076000ms;3000.000000;5000.000000;0.000000\",\"pl=0%;80;100;0\"]}"
*/
CheckResult::Ptr cr = checkable->GetLastCheckResult();
if (cr) {
attrs->Set("command", JsonEncode(cr->GetCommand()));
attrs->Set("execution_start", cr->GetExecutionStart());
attrs->Set("execution_end", cr->GetExecutionEnd());
attrs->Set("schedule_start", cr->GetScheduleStart());
attrs->Set("schedule_end", cr->GetScheduleStart());
attrs->Set("exit_status", cr->GetExitStatus());
attrs->Set("output", cr->GetOutput());
attrs->Set("performance_data", JsonEncode(cr->GetPerformanceData()));
}
String jsonAttrs = JsonEncode(attrs);
String key = "icinga:" + tableName + "." + objectCheckSum;
ExecuteQuery({ "SET", key, jsonAttrs });
/* expire in check_interval * attempts + timeout + some more seconds */
double expireTime = checkable->GetCheckInterval() * checkable->GetMaxCheckAttempts() + 60;
ExecuteQuery({ "EXPIRE", key, String(expireTime) });
}
}
void RedisWriter::StateChangedHandler(const ConfigObject::Ptr& object)

View File

@ -36,12 +36,12 @@ String RedisWriter::FormatCheckSumBinary(const String& str)
return output;
}
String RedisWriter::CalculateCheckSumString(const String& str)
String RedisWriter::CalculateCheckSumString(const String& str, bool binary)
{
return SHA1(str);
return SHA1(str, binary);
}
String RedisWriter::CalculateCheckSumGroups(const Array::Ptr& groups)
String RedisWriter::CalculateCheckSumGroups(const Array::Ptr& groups, bool binary)
{
String output;
@ -51,31 +51,31 @@ String RedisWriter::CalculateCheckSumGroups(const Array::Ptr& groups)
output += SHA1(group, true); //binary checksum required here
}
return SHA1(output);
return SHA1(output, binary);
}
String RedisWriter::CalculateCheckSumProperties(const ConfigObject::Ptr& object)
String RedisWriter::CalculateCheckSumProperties(const ConfigObject::Ptr& object, bool binary)
{
//TODO: consider precision of 6 for double values; use specific config fields for hashing?
return HashValue(object);
return HashValue(object, binary);
}
String RedisWriter::CalculateCheckSumVars(const ConfigObject::Ptr& object)
String RedisWriter::CalculateCheckSumVars(const ConfigObject::Ptr& object, bool binary)
{
CustomVarObject::Ptr customVarObject = dynamic_pointer_cast<CustomVarObject>(object);
if (!customVarObject)
return HashValue(Empty);
return HashValue(Empty, binary);
Dictionary::Ptr vars = customVarObject->GetVars();
if (!vars)
return HashValue(Empty);
return HashValue(Empty, binary);
return HashValue(vars);
return HashValue(vars, binary);
}
String RedisWriter::HashValue(const Value& value)
String RedisWriter::HashValue(const Value& value, bool binary)
{
Value temp;
@ -86,7 +86,7 @@ String RedisWriter::HashValue(const Value& value)
else
temp = value;
return SHA1(JsonEncode(temp));
return SHA1(JsonEncode(temp), binary);
}
Dictionary::Ptr RedisWriter::SerializeObjectAttrs(const Object::Ptr& object, int fieldType)

View File

@ -70,12 +70,12 @@ private:
/* utilities */
static String FormatCheckSumBinary(const String& str);
static String CalculateCheckSumString(const String& str);
static String CalculateCheckSumGroups(const Array::Ptr& groups);
static String CalculateCheckSumProperties(const ConfigObject::Ptr& object);
static String CalculateCheckSumVars(const ConfigObject::Ptr& object);
static String CalculateCheckSumString(const String& str, bool binary = false);
static String CalculateCheckSumGroups(const Array::Ptr& groups, bool binary = false);
static String CalculateCheckSumProperties(const ConfigObject::Ptr& object, bool binary = false);
static String CalculateCheckSumVars(const ConfigObject::Ptr& object, bool binary = false);
static String HashValue(const Value& value);
static String HashValue(const Value& value, bool binary = false);
static Dictionary::Ptr SerializeObjectAttrs(const Object::Ptr& object, int fieldType);
static void StateChangedHandler(const ConfigObject::Ptr& object);