Rebase against master changes, fix indent

refs #7619
This commit is contained in:
Michael Friedrich 2014-11-10 20:21:22 +01:00
parent 721483e0b2
commit 3211756656

View File

@ -40,61 +40,61 @@ void GelfWriter::Start(void)
{ {
DynamicObject::Start(); DynamicObject::Start();
m_ReconnectTimer = make_shared<Timer>(); m_ReconnectTimer = new Timer();
m_ReconnectTimer->SetInterval(10); m_ReconnectTimer->SetInterval(10);
m_ReconnectTimer->OnTimerExpired.connect(boost::bind(&GelfWriter::ReconnectTimerHandler, this)); m_ReconnectTimer->OnTimerExpired.connect(boost::bind(&GelfWriter::ReconnectTimerHandler, this));
m_ReconnectTimer->Start(); m_ReconnectTimer->Start();
m_ReconnectTimer->Reschedule(0); m_ReconnectTimer->Reschedule(0);
// Send check results // Send check results
Service::OnNewCheckResult.connect(boost::bind(&GelfWriter::CheckResultHandler, this, _1, _2)); Service::OnNewCheckResult.connect(boost::bind(&GelfWriter::CheckResultHandler, this, _1, _2));
// Send notifications // Send notifications
Service::OnNotificationSentToUser.connect(boost::bind(&GelfWriter::NotificationToUserHandler, this, _1, _2, _3, _4, _5, _6, _7, _8)); Service::OnNotificationSentToUser.connect(boost::bind(&GelfWriter::NotificationToUserHandler, this, _1, _2, _3, _4, _5, _6, _7, _8));
// Send state change // Send state change
Service::OnStateChange.connect(boost::bind(&GelfWriter::StateChangeHandler, this, _1, _2, _3)); Service::OnStateChange.connect(boost::bind(&GelfWriter::StateChangeHandler, this, _1, _2, _3));
} }
void GelfWriter::ReconnectTimerHandler(void) void GelfWriter::ReconnectTimerHandler(void)
{ {
if (m_Stream) if (m_Stream)
return; return;
TcpSocket::Ptr socket = make_shared<TcpSocket>(); TcpSocket::Ptr socket = new TcpSocket();
Log(LogNotice, "GelfWriter") Log(LogNotice, "GelfWriter")
<< "Reconnecting to GELF endpoint '" << GetHost() << "' port '" << GetPort() << "'."; << "Reconnecting to GELF endpoint '" << GetHost() << "' port '" << GetPort() << "'.";
try { try {
socket->Connect(GetHost(), GetPort()); socket->Connect(GetHost(), GetPort());
} catch (std::exception&) { } catch (std::exception&) {
Log(LogCritical, "GelfWriter") Log(LogCritical, "GelfWriter")
<< "Can't connect to GELF endpoint '" << GetHost() << "' port '" << GetPort() << "'."; << "Can't connect to GELF endpoint '" << GetHost() << "' port '" << GetPort() << "'.";
return; return;
} }
m_Stream = make_shared<NetworkStream>(socket); m_Stream = new NetworkStream(socket);
} }
void GelfWriter::CheckResultHandler(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr) void GelfWriter::CheckResultHandler(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr)
{ {
CONTEXT("GELF Processing check result for '" + checkable->GetName() + "'"); CONTEXT("GELF Processing check result for '" + checkable->GetName() + "'");
Dictionary::Ptr fields = make_shared<Dictionary>(); Dictionary::Ptr fields = new Dictionary();
Service::Ptr service = dynamic_pointer_cast<Service>(checkable); Service::Ptr service = dynamic_pointer_cast<Service>(checkable);
Host::Ptr host; Host::Ptr host;
if (service) { if (service) {
host = service->GetHost(); host = service->GetHost();
fields->Set("_service_name", service->GetShortName()); fields->Set("_service_name", service->GetShortName());
fields->Set("_service_state", Service::StateToString(service->GetState())); fields->Set("_service_state", Service::StateToString(service->GetState()));
} else { } else {
host = static_pointer_cast<Host>(checkable); host = static_pointer_cast<Host>(checkable);
} }
fields->Set("_hostname", host->GetName()); fields->Set("_hostname", host->GetName());
fields->Set("short_message", cr->GetOutput()); fields->Set("short_message", cr->GetOutput());
fields->Set("_type", "CHECK RESULT"); fields->Set("_type", "CHECK RESULT");
SendLogMessage(ComposeGelfMessage(fields, "icinga")); SendLogMessage(ComposeGelfMessage(fields, "icinga"));
} }
void GelfWriter::NotificationToUserHandler(const Notification::Ptr& notification, const Checkable::Ptr& checkable, void GelfWriter::NotificationToUserHandler(const Notification::Ptr& notification, const Checkable::Ptr& checkable,
@ -102,100 +102,100 @@ void GelfWriter::NotificationToUserHandler(const Notification::Ptr& notification
const String& author, const String& comment_text, const String& command_name) const String& author, const String& comment_text, const String& command_name)
{ {
CONTEXT("GELF Processing notification to all users '" + checkable->GetName() + "'"); CONTEXT("GELF Processing notification to all users '" + checkable->GetName() + "'");
Host::Ptr host;
Service::Ptr service;
tie(host, service) = GetHostService(checkable);
String notification_type_str = Notification::NotificationTypeToString(notification_type); Host::Ptr host;
Service::Ptr service;
tie(host, service) = GetHostService(checkable);
String author_comment = ""; String notification_type_str = Notification::NotificationTypeToString(notification_type);
if (notification_type == NotificationCustom || notification_type == NotificationAcknowledgement) {
author_comment = author + ";" + comment_text;
}
String output; String author_comment = "";
if (cr) if (notification_type == NotificationCustom || notification_type == NotificationAcknowledgement) {
output = CompatUtility::GetCheckResultOutput(cr); author_comment = author + ";" + comment_text;
}
Dictionary::Ptr fields = make_shared<Dictionary>(); String output;
if (service) { if (cr)
host = service->GetHost(); output = CompatUtility::GetCheckResultOutput(cr);
fields->Set("_type", "SERVICE NOTIFICATION");
fields->Set("_service", service->GetShortName());
fields->Set("short_message", output);
} else {
host = static_pointer_cast<Host>(checkable);
fields->Set("_type", "HOST NOTIFICATION");
fields->Set("short_message", "(" << (host->IsReachable() ? Host::StateToString(host->GetState()) : "UNREACHABLE") << ")");
}
fields->Set("_hostname", host->GetName());
fields->Set("_command", command_name);
fields->Set("_state", notification_type_str);
fields->Set("_comment", author_comment);
SendLogMessage(ComposeGelfMessage(fields, "icinga")); Dictionary::Ptr fields = new Dictionary();
if (service) {
host = service->GetHost();
fields->Set("_type", "SERVICE NOTIFICATION");
fields->Set("_service", service->GetShortName());
fields->Set("short_message", output);
} else {
host = static_pointer_cast<Host>(checkable);
fields->Set("_type", "HOST NOTIFICATION");
fields->Set("short_message", "(" << (host->IsReachable() ? Host::StateToString(host->GetState()) : "UNREACHABLE") << ")");
}
fields->Set("_hostname", host->GetName());
fields->Set("_command", command_name);
fields->Set("_state", notification_type_str);
fields->Set("_comment", author_comment);
SendLogMessage(ComposeGelfMessage(fields, "icinga"));
} }
void GelfWriter::StateChangeHandler(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr, StateType type) void GelfWriter::StateChangeHandler(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr, StateType type)
{ {
CONTEXT("GELF Processing state change '" + checkable->GetName() + "'"); CONTEXT("GELF Processing state change '" + checkable->GetName() + "'");
Host::Ptr host; Host::Ptr host;
Service::Ptr service; Service::Ptr service;
tie(host, service) = GetHostService(checkable); tie(host, service) = GetHostService(checkable);
Dictionary::Ptr fields = make_shared<Dictionary>(); Dictionary::Ptr fields = new Dictionary();
fields->Set("_state", service ? static_cast<int>(service->GetState()) : static_cast<int>(host->GetState())); fields->Set("_state", service ? static_cast<int>(service->GetState()) : static_cast<int>(host->GetState()));
fields->Set("_type", "STATE CHANGE"); fields->Set("_type", "STATE CHANGE");
fields->Set("_current_check_attempt", checkable->GetCheckAttempt()); fields->Set("_current_check_attempt", checkable->GetCheckAttempt());
fields->Set("_max_check_attempts", checkable->GetMaxCheckAttempts()); fields->Set("_max_check_attempts", checkable->GetMaxCheckAttempts());
if (service) { if (service) {
fields->Set("_last_state", service->GetLastState()); fields->Set("_last_state", service->GetLastState());
fields->Set("_last_hard_state", service->GetLastHardState()); fields->Set("_last_hard_state", service->GetLastHardState());
} else { } else {
fields->Set("_last_state", host->GetLastState()); fields->Set("_last_state", host->GetLastState());
fields->Set("_last_hard_state", host->GetLastHardState()); fields->Set("_last_hard_state", host->GetLastHardState());
} }
if (cr) { if (cr) {
fields->Set("short_message", CompatUtility::GetCheckResultOutput(cr)); fields->Set("short_message", CompatUtility::GetCheckResultOutput(cr));
fields->Set("full_message", CompatUtility::GetCheckResultLongOutput(cr)); fields->Set("full_message", CompatUtility::GetCheckResultLongOutput(cr));
fields->Set("_check_source", cr->GetCheckSource()); fields->Set("_check_source", cr->GetCheckSource());
} }
SendLogMessage(ComposeGelfMessage(fields, "icinga")); SendLogMessage(ComposeGelfMessage(fields, "icinga"));
} }
String GelfWriter::ComposeGelfMessage(const Dictionary::Ptr& fields, const String& source) String GelfWriter::ComposeGelfMessage(const Dictionary::Ptr& fields, const String& source)
{ {
fields->Set("version", "1.1"); fields->Set("version", "1.1");
fields->Set("host", source); fields->Set("host", source);
fields->Set("timestamp", Utility::GetTime()); fields->Set("timestamp", Utility::GetTime());
return JsonEncode(fields); return JsonEncode(fields);
} }
void GelfWriter::SendLogMessage(const String& gelf) void GelfWriter::SendLogMessage(const String& gelf)
{ {
std::ostringstream msgbuf; std::ostringstream msgbuf;
msgbuf << gelf; msgbuf << gelf;
msgbuf << '\0'; msgbuf << '\0';
String log = msgbuf.str(); String log = msgbuf.str();
ObjectLock olock(this); ObjectLock olock(this);
if (!m_Stream) if (!m_Stream)
return; return;
try { try {
m_Stream->Write(log.CStr(), log.GetLength()); m_Stream->Write(log.CStr(), log.GetLength());
} catch (const std::exception& ex) { } catch (const std::exception& ex) {
Log(LogCritical, "GelfWriter") Log(LogCritical, "GelfWriter")
<< "Cannot write to TCP socket on host '" << GetHost() << "' port '" << GetPort() << "'."; << "Cannot write to TCP socket on host '" << GetHost() << "' port '" << GetPort() << "'.";
m_Stream.reset(); m_Stream.reset();
} }
} }