Styleguide for IcingaDB

This commit is contained in:
Michael Friedrich 2019-11-02 18:01:31 +01:00
parent c3f6fa2e41
commit 9d9804d50a
6 changed files with 51 additions and 37 deletions

View File

@ -2,10 +2,19 @@
#include "icingadb/icingadb.hpp"
#include "icingadb/redisconnection.hpp"
#include "icinga/command.hpp"
#include "icinga/compatutility.hpp"
#include "base/configtype.hpp"
#include "base/configobject.hpp"
#include "base/json.hpp"
#include "base/logger.hpp"
#include "base/serializer.hpp"
#include "base/tlsutility.hpp"
#include "base/initialize.hpp"
#include "base/convert.hpp"
#include "base/array.hpp"
#include "base/exception.hpp"
#include "base/utility.hpp"
#include "icinga/command.hpp"
#include "icinga/compatutility.hpp"
#include "icinga/customvarobject.hpp"
#include "icinga/host.hpp"
#include "icinga/service.hpp"
@ -18,15 +27,6 @@
#include "icinga/timeperiod.hpp"
#include "icinga/pluginutility.hpp"
#include "remote/zone.hpp"
#include "base/json.hpp"
#include "base/logger.hpp"
#include "base/serializer.hpp"
#include "base/tlsutility.hpp"
#include "base/initialize.hpp"
#include "base/convert.hpp"
#include "base/array.hpp"
#include "base/exception.hpp"
#include "base/utility.hpp"
#include <iterator>
#include <map>
#include <memory>

View File

@ -1,11 +1,6 @@
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
#include "icingadb/icingadb.hpp"
#include "icinga/customvarobject.hpp"
#include "icinga/checkcommand.hpp"
#include "icinga/notificationcommand.hpp"
#include "icinga/eventcommand.hpp"
#include "icinga/host.hpp"
#include "base/configtype.hpp"
#include "base/object-packer.hpp"
#include "base/logger.hpp"
@ -17,11 +12,15 @@
#include "base/scriptglobal.hpp"
#include "base/convert.hpp"
#include "base/json.hpp"
#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>
#include <boost/algorithm/string.hpp>
using namespace icinga;

View File

@ -7,15 +7,12 @@
#include "base/json.hpp"
#include "icinga/checkable.hpp"
#include "icinga/host.hpp"
#include <memory>
#include <boost/algorithm/string.hpp>
#include <memory>
#include <utility>
using namespace icinga;
//TODO Make configurable and figure out a sane default
#define MAX_EVENTS_DEFAULT 5000
REGISTER_TYPE(IcingaDB);
@ -292,11 +289,13 @@ void IcingaDB::SendEvent(const Dictionary::Ptr& event)
if (type == "CheckResult") {
Checkable::Ptr checkable;
if (event->Contains("service")) {
checkable = Service::GetByNamePair(event->Get("host"), event->Get("service"));
} else {
checkable = Host::GetByName(event->Get("host"));
}
// Update State for icingaweb
m_WorkQueue.Enqueue([this, checkable]() { UpdateState(checkable); });
}
@ -315,6 +314,7 @@ void IcingaDB::SendEvent(const Dictionary::Ptr& event)
if (type == "AcknowledgementSet") {
Timestamp entry = 0;
Comment::Ptr AckComment;
for (const Comment::Ptr& c : checkable->GetComments()) {
if (c->GetEntryType() == CommentAcknowledgement) {
if (c->GetEntryTime() > entry) {
@ -324,15 +324,13 @@ void IcingaDB::SendEvent(const Dictionary::Ptr& event)
}
}
}
event->Set("comment_id", GetObjectIdentifier(AckComment));
}
}
String body = JsonEncode(event);
// Log(LogInformation, "IcingaDB")
// << "Sending event \"" << body << "\"";
m_Rcon->FireAndForgetQueries({
{ "PUBLISH", "icinga:event:all", body },
{ "PUBLISH", "icinga:event:" + event->Get("type"), body }});

View File

@ -4,14 +4,14 @@
#define ICINGADB_H
#include "icingadb/icingadb-ti.hpp"
#include "icinga/customvarobject.hpp"
#include "remote/messageorigin.hpp"
#include "icingadb/redisconnection.hpp"
#include "base/timer.hpp"
#include "base/workqueue.hpp"
#include "icingadb/redisconnection.hpp"
#include "icinga/customvarobject.hpp"
#include "icinga/checkable.hpp"
#include "icinga/service.hpp"
#include "icinga/downtime.hpp"
#include "remote/messageorigin.hpp"
#include <memory>
namespace icinga

View File

@ -22,7 +22,7 @@
using namespace icinga;
namespace asio = boost::asio;
RedisConnection::RedisConnection(const String host, const int port, const String path, const String password, const int db) :
RedisConnection::RedisConnection(const String& host, const int port, const String& path, const String& password, const int db) :
RedisConnection(IoEngine::Get().GetIoContext(), host, port, path, password, db)
{
}
@ -192,12 +192,15 @@ void RedisConnection::ReadLoop(asio::yield_context& yc)
} catch (const std::exception& ex) {
Log(LogCritical, "IcingaDB")
<< "Error during receiving the response to a query which has been fired and forgotten: " << ex.what();
continue;
} catch (...) {
Log(LogCritical, "IcingaDB")
<< "Error during receiving the response to a query which has been fired and forgotten";
continue;
}
break;
case ResponseAction::Deliver:
for (auto i (item.Amount); i; --i) {
@ -212,11 +215,13 @@ void RedisConnection::ReadLoop(asio::yield_context& yc)
throw;
} catch (...) {
promise.set_exception(std::current_exception());
continue;
}
promise.set_value(std::move(reply));
}
break;
case ResponseAction::DeliverBulk:
{
@ -233,6 +238,7 @@ void RedisConnection::ReadLoop(asio::yield_context& yc)
throw;
} catch (...) {
promise.set_exception(std::current_exception());
continue;
}
}
@ -286,11 +292,13 @@ void RedisConnection::WriteItem(boost::asio::yield_context& yc, RedisConnection:
Log msg (LogCritical, "IcingaDB", "Error during sending query");
LogQuery(item, msg);
msg << " which has been fired and forgotten: " << ex.what();
return;
} catch (...) {
Log msg (LogCritical, "IcingaDB", "Error during sending query");
LogQuery(item, msg);
msg << " which has been fired and forgotten";
return;
}
@ -318,11 +326,13 @@ void RedisConnection::WriteItem(boost::asio::yield_context& yc, RedisConnection:
Log msg (LogCritical, "IcingaDB", "Error during sending query");
LogQuery(item[i], msg);
msg << " which has been fired and forgotten: " << ex.what();
return;
} catch (...) {
Log msg (LogCritical, "IcingaDB", "Error during sending query");
LogQuery(item[i], msg);
msg << " which has been fired and forgotten";
return;
}
@ -344,6 +354,7 @@ void RedisConnection::WriteItem(boost::asio::yield_context& yc, RedisConnection:
throw;
} catch (...) {
item.second.set_exception(std::current_exception());
return;
}
@ -369,6 +380,7 @@ void RedisConnection::WriteItem(boost::asio::yield_context& yc, RedisConnection:
throw;
} catch (...) {
item.second.set_exception(std::current_exception());
return;
}

View File

@ -3,6 +3,12 @@
#ifndef REDISCONNECTION_H
#define REDISCONNECTION_H
#include "base/array.hpp"
#include "base/atomic.hpp"
#include "base/io-engine.hpp"
#include "base/object.hpp"
#include "base/string.hpp"
#include "base/value.hpp"
#include <boost/asio/spawn.hpp>
#include <boost/asio/buffer.hpp>
#include <boost/asio/buffered_stream.hpp>
@ -26,12 +32,6 @@
#include <stdexcept>
#include <utility>
#include <vector>
#include "base/array.hpp"
#include "base/atomic.hpp"
#include "base/io-engine.hpp"
#include "base/object.hpp"
#include "base/string.hpp"
#include "base/value.hpp"
namespace icinga
{
@ -50,7 +50,8 @@ namespace icinga
typedef Value Reply;
typedef std::vector<Reply> Replies;
RedisConnection(const String host, const int port, const String path, const String password = "", const int db = 0);
RedisConnection(const String& host, const int port, const String& path,
const String& password = "", const int db = 0);
void Start();
@ -221,6 +222,7 @@ RedisConnection::Reply RedisConnection::ReadOne(StreamPtr& stream, boost::asio::
m_Connected.store(false);
stream = nullptr;
}
throw;
}
}
@ -244,6 +246,7 @@ void RedisConnection::WriteOne(StreamPtr& stream, RedisConnection::Query& query,
m_Connected.store(false);
stream = nullptr;
}
throw;
}
}
@ -370,7 +373,9 @@ void RedisConnection::WriteRESP(AsyncWriteStream& stream, const Query& query, bo
for (auto& arg : query) {
asio::async_write(stream, asio::const_buffer("$", 1), yc);
WriteInt(stream, arg.GetLength(), yc);
asio::async_write(stream, asio::const_buffer("\r\n", 2), yc);
asio::async_write(stream, asio::const_buffer(arg.CStr(), arg.GetLength()), yc);
asio::async_write(stream, asio::const_buffer("\r\n", 2), yc);