icinga2/components/compat/compatcomponent.cpp

686 lines
23 KiB
C++
Raw Normal View History

2012-06-27 18:43:34 +02:00
/******************************************************************************
* Icinga 2 *
* Copyright (C) 2012 Icinga Development Team (http://www.icinga.org/) *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License *
* as published by the Free Software Foundation; either version 2 *
* of the License, or (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the Free Software Foundation *
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
******************************************************************************/
2013-03-17 20:19:29 +01:00
#include "compat/compatcomponent.h"
#include "icinga/externalcommandprocessor.h"
#include "icinga/icingaapplication.h"
#include "icinga/cib.h"
#include "icinga/hostgroup.h"
#include "icinga/servicegroup.h"
2013-03-16 21:18:53 +01:00
#include "base/dynamictype.h"
#include "base/objectlock.h"
#include "base/logger_fwd.h"
#include "base/exception.h"
2013-03-17 20:19:29 +01:00
#include "base/application.h"
2013-03-16 21:18:53 +01:00
#include <boost/smart_ptr/make_shared.hpp>
#include <boost/foreach.hpp>
2013-03-18 11:02:18 +01:00
#include <boost/tuple/tuple.hpp>
2013-03-16 21:18:53 +01:00
#include <boost/exception/diagnostic_information.hpp>
#include <boost/algorithm/string/replace.hpp>
2013-03-17 20:19:29 +01:00
#include <fstream>
2012-06-27 18:43:34 +02:00
using namespace icinga;
REGISTER_TYPE(CompatComponent);
/**
* Hint: The reason why we're using "\n" rather than std::endl is because
* std::endl also _flushes_ the output stream which severely degrades
* performance (see http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt11ch25s02.html).
*/
/**
* Starts the component.
*/
void CompatComponent::Start(void)
{
m_StatusTimer = boost::make_shared<Timer>();
m_StatusTimer->SetInterval(15);
m_StatusTimer->OnTimerExpired.connect(boost::bind(&CompatComponent::StatusTimerHandler, this));
m_StatusTimer->Start();
m_StatusTimer->Reschedule(0);
#ifndef _WIN32
2013-03-15 18:21:29 +01:00
m_CommandThread = boost::thread(boost::bind(&CompatComponent::CommandPipeThread, this, GetCommandPath()));
m_CommandThread.detach();
#endif /* _WIN32 */
}
/**
2013-01-21 13:08:08 +01:00
* Retrieves the status.dat path.
*
* @returns statuspath from config, or static default
*/
String CompatComponent::GetStatusPath(void) const
{
Value statusPath = m_StatusPath;
2013-01-21 13:08:08 +01:00
if (statusPath.IsEmpty())
return Application::GetLocalStateDir() + "/cache/icinga2/status.dat";
else
2013-01-21 13:08:08 +01:00
return statusPath;
}
/**
2013-01-21 13:08:08 +01:00
* Retrieves the objects.cache path.
*
* @returns objectspath from config, or static default
*/
String CompatComponent::GetObjectsPath(void) const
{
Value objectsPath = m_ObjectsPath;
2013-01-21 13:08:08 +01:00
if (objectsPath.IsEmpty())
return Application::GetLocalStateDir() + "/cache/icinga2/objects.cache";
2013-01-21 13:08:08 +01:00
else
return objectsPath;
}
/**
* Retrieves the icinga.cmd path.
*
* @returns icinga.cmd path
*/
String CompatComponent::GetCommandPath(void) const
{
Value commandPath = m_CommandPath;
2013-01-21 13:08:08 +01:00
if (commandPath.IsEmpty())
return Application::GetLocalStateDir() + "/run/icinga2/icinga2.cmd";
2013-01-21 13:08:08 +01:00
else
return commandPath;
}
CompatComponent::CompatComponent(const Dictionary::Ptr& serializedUpdate)
: DynamicObject(serializedUpdate)
2012-06-27 18:43:34 +02:00
{
RegisterAttribute("status_path", Attribute_Config, &m_StatusPath);
RegisterAttribute("objects_path", Attribute_Config, &m_ObjectsPath);
RegisterAttribute("command_path", Attribute_Config, &m_CommandPath);
2012-06-27 18:43:34 +02:00
}
#ifndef _WIN32
2013-01-21 13:08:08 +01:00
void CompatComponent::CommandPipeThread(const String& commandPath)
{
struct stat statbuf;
bool fifo_ok = false;
2013-01-21 13:08:08 +01:00
if (lstat(commandPath.CStr(), &statbuf) >= 0) {
if (S_ISFIFO(statbuf.st_mode) && access(commandPath.CStr(), R_OK) >= 0) {
fifo_ok = true;
} else {
2013-03-11 13:45:08 +01:00
if (unlink(commandPath.CStr()) < 0) {
BOOST_THROW_EXCEPTION(posix_error()
2013-03-18 17:04:22 +01:00
<< boost::errinfo_api_function("unlink")
<< boost::errinfo_errno(errno)
<< boost::errinfo_file_name(commandPath));
2013-03-11 13:45:08 +01:00
}
}
}
2013-01-21 13:08:08 +01:00
2013-03-11 13:45:08 +01:00
if (!fifo_ok && mkfifo(commandPath.CStr(), S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP) < 0) {
BOOST_THROW_EXCEPTION(posix_error()
2013-03-18 17:04:22 +01:00
<< boost::errinfo_api_function("mkfifo")
<< boost::errinfo_errno(errno)
<< boost::errinfo_file_name(commandPath));
2013-03-11 13:45:08 +01:00
}
2013-01-21 13:08:08 +01:00
for (;;) {
int fd;
do {
fd = open(commandPath.CStr(), O_RDONLY);
} while (fd < 0 && errno == EINTR);
2013-01-21 13:08:08 +01:00
2013-03-11 13:45:08 +01:00
if (fd < 0) {
BOOST_THROW_EXCEPTION(posix_error()
2013-03-18 17:04:22 +01:00
<< boost::errinfo_api_function("open")
<< boost::errinfo_errno(errno)
<< boost::errinfo_file_name(commandPath));
2013-03-11 13:45:08 +01:00
}
2013-01-21 13:08:08 +01:00
FILE *fp = fdopen(fd, "r");
if (fp == NULL) {
2013-03-11 13:45:08 +01:00
(void) close(fd);
BOOST_THROW_EXCEPTION(posix_error()
2013-03-18 17:04:22 +01:00
<< boost::errinfo_api_function("fdopen")
<< boost::errinfo_errno(errno));
2013-01-21 13:08:08 +01:00
}
char line[2048];
while (fgets(line, sizeof(line), fp) != NULL) {
// remove trailing new-line
while (strlen(line) > 0 &&
(line[strlen(line) - 1] == '\r' || line[strlen(line) - 1] == '\n'))
line[strlen(line) - 1] = '\0';
String command = line;
2013-02-17 19:14:34 +01:00
2013-03-02 09:07:47 +01:00
try {
2013-03-16 21:18:53 +01:00
Log(LogInformation, "compat", "Executing external command: " + command);
2013-03-02 09:07:47 +01:00
ExternalCommandProcessor::Execute(command);
2013-03-16 21:18:53 +01:00
} catch (const std::exception& ex) {
std::ostringstream msgbuf;
msgbuf << "External command failed: " << boost::diagnostic_information(ex);
Log(LogWarning, "compat", msgbuf.str());
2013-03-02 09:07:47 +01:00
}
2013-01-21 13:08:08 +01:00
}
fclose(fp);
}
}
#endif /* _WIN32 */
2013-01-21 13:08:08 +01:00
2013-03-17 20:19:29 +01:00
void CompatComponent::DumpComments(std::ostream& fp, const Service::Ptr& owner, CompatObjectType type)
2013-01-29 16:29:09 +01:00
{
Service::Ptr service;
Host::Ptr host;
Dictionary::Ptr comments = owner->GetComments();
2013-01-29 16:29:09 +01:00
if (!comments)
return;
2013-03-02 09:07:47 +01:00
ObjectLock olock(comments);
2013-01-29 16:29:09 +01:00
String id;
Dictionary::Ptr comment;
2013-03-18 11:02:18 +01:00
BOOST_FOREACH(boost::tie(id, comment), comments) {
if (Service::IsCommentExpired(comment))
2013-01-30 14:28:13 +01:00
continue;
if (type == CompatTypeHost)
2013-01-29 16:29:09 +01:00
fp << "hostcomment {" << "\n";
else
fp << "servicecomment {" << "\n"
<< "\t" << "service_description=" << owner->GetShortName() << "\n";
2013-01-29 16:29:09 +01:00
fp << "\t" << "host_name=" << owner->GetHost()->GetName() << "\n"
<< "\t" << "comment_id=" << static_cast<String>(comment->Get("legacy_id")) << "\n"
2013-01-29 16:29:09 +01:00
<< "\t" << "entry_time=" << static_cast<double>(comment->Get("entry_time")) << "\n"
<< "\t" << "entry_type=" << static_cast<long>(comment->Get("entry_type")) << "\n"
<< "\t" << "persistent=" << 1 << "\n"
<< "\t" << "author=" << static_cast<String>(comment->Get("author")) << "\n"
<< "\t" << "comment_data=" << static_cast<String>(comment->Get("text")) << "\n"
<< "\t" << "expires=" << (static_cast<double>(comment->Get("expire_time")) != 0 ? 1 : 0) << "\n"
<< "\t" << "expire_time=" << static_cast<double>(comment->Get("expire_time")) << "\n"
<< "\t" << "}" << "\n"
<< "\n";
}
}
2013-03-17 20:19:29 +01:00
void CompatComponent::DumpDowntimes(std::ostream& fp, const Service::Ptr& owner, CompatObjectType type)
2013-01-29 14:19:54 +01:00
{
2013-03-02 09:07:47 +01:00
Host::Ptr host = owner->GetHost();
2013-03-01 12:07:52 +01:00
2013-03-02 09:07:47 +01:00
if (!host)
return;
2013-02-18 14:40:24 +01:00
2013-03-02 09:07:47 +01:00
Dictionary::Ptr downtimes = owner->GetDowntimes();
2013-01-29 14:19:54 +01:00
if (!downtimes)
return;
2013-03-02 09:07:47 +01:00
ObjectLock olock(downtimes);
2013-03-01 12:07:52 +01:00
2013-01-29 14:19:54 +01:00
String id;
Dictionary::Ptr downtime;
2013-03-18 11:02:18 +01:00
BOOST_FOREACH(boost::tie(id, downtime), downtimes) {
if (Service::IsDowntimeExpired(downtime))
2013-01-30 14:28:13 +01:00
continue;
if (type == CompatTypeHost)
2013-01-29 14:19:54 +01:00
fp << "hostdowntime {" << "\n";
else
fp << "servicedowntime {" << "\n"
2013-03-02 09:07:47 +01:00
<< "\t" << "service_description=" << owner->GetShortName() << "\n";
2013-01-29 14:19:54 +01:00
Dictionary::Ptr triggeredByObj = Service::GetDowntimeByID(downtime->Get("triggered_by"));
int triggeredByLegacy = 0;
if (triggeredByObj)
triggeredByLegacy = triggeredByObj->Get("legacy_id");
2013-03-02 09:07:47 +01:00
fp << "\t" << "host_name=" << host->GetName() << "\n"
<< "\t" << "downtime_id=" << static_cast<String>(downtime->Get("legacy_id")) << "\n"
2013-01-29 14:19:54 +01:00
<< "\t" << "entry_time=" << static_cast<double>(downtime->Get("entry_time")) << "\n"
<< "\t" << "start_time=" << static_cast<double>(downtime->Get("start_time")) << "\n"
<< "\t" << "end_time=" << static_cast<double>(downtime->Get("end_time")) << "\n"
<< "\t" << "triggered_by=" << triggeredByLegacy << "\n"
2013-01-29 14:19:54 +01:00
<< "\t" << "fixed=" << static_cast<long>(downtime->Get("fixed")) << "\n"
<< "\t" << "duration=" << static_cast<long>(downtime->Get("duration")) << "\n"
<< "\t" << "is_in_effect=" << (Service::IsDowntimeActive(downtime) ? 1 : 0) << "\n"
2013-01-29 14:19:54 +01:00
<< "\t" << "author=" << static_cast<String>(downtime->Get("author")) << "\n"
<< "\t" << "comment=" << static_cast<String>(downtime->Get("comment")) << "\n"
2013-01-29 14:50:51 +01:00
<< "\t" << "trigger_time=" << static_cast<double>(downtime->Get("trigger_time")) << "\n"
2013-01-29 14:19:54 +01:00
<< "\t" << "}" << "\n"
<< "\n";
}
}
2013-03-17 20:19:29 +01:00
void CompatComponent::DumpHostStatus(std::ostream& fp, const Host::Ptr& host)
2012-06-27 18:43:34 +02:00
{
2013-03-02 09:07:47 +01:00
fp << "hoststatus {" << "\n"
<< "\t" << "host_name=" << host->GetName() << "\n";
2013-02-19 23:02:08 +01:00
2013-03-02 09:07:47 +01:00
Service::Ptr hc = host->GetHostCheckService();
ObjectLock olock(hc);
2013-02-19 23:02:08 +01:00
if (hc)
DumpServiceStatusAttrs(fp, hc, CompatTypeHost);
2013-02-07 20:29:35 +01:00
fp << "\t" << "}" << "\n"
<< "\n";
2013-01-29 14:19:54 +01:00
2013-02-19 23:02:08 +01:00
if (hc) {
DumpDowntimes(fp, hc, CompatTypeHost);
DumpComments(fp, hc, CompatTypeHost);
}
2012-06-27 18:43:34 +02:00
}
2013-03-17 20:19:29 +01:00
void CompatComponent::DumpHostObject(std::ostream& fp, const Host::Ptr& host)
2012-06-27 18:43:34 +02:00
{
2013-03-02 09:07:47 +01:00
fp << "define host {" << "\n"
<< "\t" << "host_name" << "\t" << host->GetName() << "\n"
<< "\t" << "display_name" << "\t" << host->GetDisplayName() << "\n";
2013-03-16 21:18:53 +01:00
std::set<Host::Ptr> parents = host->GetParentHosts();
if (!parents.empty()) {
fp << "\t" << "parents" << "\t";
DumpNameList(fp, parents);
fp << "\n";
}
2013-03-02 09:07:47 +01:00
Service::Ptr hc = host->GetHostCheckService();
if (hc) {
ObjectLock olock(hc);
fp << "\t" << "check_interval" << "\t" << hc->GetCheckInterval() / 60.0 << "\n"
<< "\t" << "retry_interval" << "\t" << hc->GetRetryInterval() / 60.0 << "\n"
<< "\t" << "max_check_attempts" << "\t" << hc->GetMaxCheckAttempts() << "\n"
<< "\t" << "active_checks_enabled" << "\t" << (hc->GetEnableActiveChecks() ? 1 : 0) << "\n"
<< "\t" << "passive_checks_enabled" << "\t" << (hc->GetEnablePassiveChecks() ? 1 : 0) << "\n"
<< "\t" << "notifications_enabled" << "\t" << (hc->GetEnableNotifications() ? 1 : 0) << "\n"
<< "\t" << "notification_options" << "\t" << "d,u,r" << "\n"
<< "\t" << "notification_interval" << "\t" << 1 << "\n";
} else {
fp << "\t" << "check_interval" << "\t" << 60 << "\n"
<< "\t" << "retry_interval" << "\t" << 60 << "\n"
<< "\t" << "max_check_attempts" << "\t" << 1 << "\n"
<< "\t" << "active_checks_enabled" << "\t" << 0 << "\n"
<< "\t" << "passive_checks_enabled" << "\t" << 0 << "\n"
<< "\t" << "notifications_enabled" << "\t" << 0 << "\n";
2012-07-09 12:44:31 +02:00
}
fp << "\t" << "}" << "\n"
<< "\n";
2012-06-27 18:43:34 +02:00
}
2013-03-17 20:19:29 +01:00
void CompatComponent::DumpServiceStatusAttrs(std::ostream& fp, const Service::Ptr& service, CompatObjectType type)
2012-06-27 18:43:34 +02:00
{
ASSERT(service->OwnsLock());
2013-03-02 09:07:47 +01:00
String output;
String perfdata;
2013-02-24 01:10:34 +01:00
double schedule_end = -1;
2013-03-02 09:07:47 +01:00
Dictionary::Ptr cr = service->GetLastCheckResult();
2013-02-19 23:02:08 +01:00
if (cr) {
output = cr->Get("output");
boost::algorithm::replace_all(output, "\n", "\\n");
schedule_end = cr->Get("schedule_end");
perfdata = cr->Get("performance_data_raw");
boost::algorithm::replace_all(perfdata, "\n", "\\n");
2012-06-27 18:43:34 +02:00
}
2013-03-02 09:07:47 +01:00
int state = service->GetState();
2012-07-09 10:09:53 +02:00
if (state > StateUnknown)
2012-07-02 15:48:49 +02:00
state = StateUnknown;
if (type == CompatTypeHost) {
2013-02-07 20:29:35 +01:00
if (state == StateOK || state == StateWarning)
2013-03-02 09:07:47 +01:00
state = 0; /* UP */
2013-02-07 20:29:35 +01:00
else
2013-03-02 09:07:47 +01:00
state = 1; /* DOWN */
2013-02-07 20:29:35 +01:00
2013-03-02 09:07:47 +01:00
Host::Ptr host = service->GetHost();
2013-02-07 20:29:35 +01:00
2013-03-02 09:07:47 +01:00
if (!host)
return;
2013-02-19 23:02:08 +01:00
2013-03-02 09:07:47 +01:00
if (!host->IsReachable())
state = 2; /* UNREACHABLE */
2013-02-19 23:02:08 +01:00
}
2013-03-02 09:07:47 +01:00
double last_notification = 0;
BOOST_FOREACH(const Notification::Ptr& notification, service->GetNotifications()) {
if (notification->GetLastNotification() > last_notification)
last_notification = notification->GetLastNotification();
}
2013-03-02 09:07:47 +01:00
fp << "\t" << "check_interval=" << service->GetCheckInterval() / 60.0 << "\n"
<< "\t" << "retry_interval=" << service->GetRetryInterval() / 60.0 << "\n"
<< "\t" << "has_been_checked=" << (service->GetLastCheckResult() ? 1 : 0) << "\n"
<< "\t" << "should_be_scheduled=1" << "\n"
<< "\t" << "check_execution_time=" << Service::CalculateExecutionTime(cr) << "\n"
<< "\t" << "check_latency=" << Service::CalculateLatency(cr) << "\n"
<< "\t" << "current_state=" << state << "\n"
<< "\t" << "state_type=" << service->GetStateType() << "\n"
<< "\t" << "plugin_output=" << output << "\n"
<< "\t" << "performance_data=" << perfdata << "\n"
<< "\t" << "last_check=" << schedule_end << "\n"
<< "\t" << "next_check=" << service->GetNextCheck() << "\n"
<< "\t" << "current_attempt=" << service->GetCurrentCheckAttempt() << "\n"
<< "\t" << "max_attempts=" << service->GetMaxCheckAttempts() << "\n"
<< "\t" << "last_state_change=" << service->GetLastStateChange() << "\n"
<< "\t" << "last_hard_state_change=" << service->GetLastHardStateChange() << "\n"
<< "\t" << "last_update=" << time(NULL) << "\n"
<< "\t" << "notifications_enabled=" << (service->GetEnableNotifications() ? 1 : 0) << "\n"
<< "\t" << "active_checks_enabled=" << (service->GetEnableActiveChecks() ? 1 : 0) <<"\n"
<< "\t" << "passive_checks_enabled=" << (service->GetEnablePassiveChecks() ? 1 : 0) << "\n"
<< "\t" << "problem_has_been_acknowledged=" << (service->GetAcknowledgement() != AcknowledgementNone ? 1 : 0) << "\n"
<< "\t" << "acknowledgement_type=" << static_cast<int>(service->GetAcknowledgement()) << "\n"
<< "\t" << "acknowledgement_end_time=" << service->GetAcknowledgementExpiry() << "\n"
<< "\t" << "scheduled_downtime_depth=" << (service->IsInDowntime() ? 1 : 0) << "\n"
<< "\t" << "last_notification=" << last_notification << "\n";
2013-02-07 20:29:35 +01:00
}
2013-03-17 20:19:29 +01:00
void CompatComponent::DumpServiceStatus(std::ostream& fp, const Service::Ptr& service)
2013-02-07 20:29:35 +01:00
{
2013-03-02 09:07:47 +01:00
Host::Ptr host = service->GetHost();
2013-02-19 23:02:08 +01:00
2013-03-02 09:07:47 +01:00
if (!host)
return;
2013-02-18 14:40:24 +01:00
2013-02-07 20:29:35 +01:00
fp << "servicestatus {" << "\n"
2013-03-02 09:07:47 +01:00
<< "\t" << "host_name=" << host->GetName() << "\n"
<< "\t" << "service_description=" << service->GetShortName() << "\n";
2013-02-07 20:29:35 +01:00
2013-03-04 15:52:42 +01:00
{
ObjectLock olock(service);
DumpServiceStatusAttrs(fp, service, CompatTypeService);
}
2013-02-07 20:29:35 +01:00
fp << "\t" << "}" << "\n"
<< "\n";
2013-01-29 14:19:54 +01:00
DumpDowntimes(fp, service, CompatTypeService);
DumpComments(fp, service, CompatTypeService);
2012-06-27 18:43:34 +02:00
}
2013-03-17 20:19:29 +01:00
void CompatComponent::DumpServiceObject(std::ostream& fp, const Service::Ptr& service)
2012-06-27 18:43:34 +02:00
{
2013-03-02 09:07:47 +01:00
Host::Ptr host = service->GetHost();
2013-02-19 23:02:08 +01:00
2013-02-27 16:04:49 +01:00
if (!host)
return;
double notification_interval = -1;
BOOST_FOREACH(const Notification::Ptr& notification, service->GetNotifications()) {
if (notification_interval == -1 || notification->GetNotificationInterval() < notification_interval)
notification_interval = notification->GetNotificationInterval();
}
if (notification_interval == -1)
notification_interval = 60;
2013-02-19 23:02:08 +01:00
{
ObjectLock olock(service);
fp << "define service {" << "\n"
2013-03-02 09:07:47 +01:00
<< "\t" << "host_name" << "\t" << host->GetName() << "\n"
<< "\t" << "service_description" << "\t" << service->GetShortName() << "\n"
2013-02-19 23:02:08 +01:00
<< "\t" << "display_name" << "\t" << service->GetDisplayName() << "\n"
<< "\t" << "check_command" << "\t" << "check_i2" << "\n"
<< "\t" << "check_interval" << "\t" << service->GetCheckInterval() / 60.0 << "\n"
<< "\t" << "retry_interval" << "\t" << service->GetRetryInterval() / 60.0 << "\n"
<< "\t" << "max_check_attempts" << "\t" << 1 << "\n"
<< "\t" << "active_checks_enabled" << "\t" << (service->GetEnableActiveChecks() ? 1 : 0) << "\n"
<< "\t" << "passive_checks_enabled" << "\t" << (service->GetEnablePassiveChecks() ? 1 : 0) << "\n"
<< "\t" << "notifications_enabled" << "\t" << (service->GetEnableNotifications() ? 1 : 0) << "\n"
<< "\t" << "notification_options" << "\t" << "u,w,c,r" << "\n"
<< "\t" << "notification_interval" << "\t" << notification_interval / 60.0 << "\n"
2013-02-19 23:02:08 +01:00
<< "\t" << "}" << "\n"
<< "\n";
}
2013-03-02 09:07:47 +01:00
BOOST_FOREACH(const Service::Ptr& parent, service->GetParentServices()) {
Host::Ptr host = service->GetHost();
if (!host)
continue;
Host::Ptr parent_host = parent->GetHost();
if (!parent_host)
continue;
2013-02-18 14:40:24 +01:00
fp << "define servicedependency {" << "\n"
2013-03-02 09:07:47 +01:00
<< "\t" << "dependent_host_name" << "\t" << host->GetName() << "\n"
<< "\t" << "dependent_service_description" << "\t" << service->GetShortName() << "\n"
2013-03-02 09:07:47 +01:00
<< "\t" << "host_name" << "\t" << parent_host->GetName() << "\n"
<< "\t" << "service_description" << "\t" << service->GetShortName() << "\n"
<< "\t" << "execution_failure_criteria" << "\t" << "n" << "\n"
2013-02-07 20:29:35 +01:00
<< "\t" << "notification_failure_criteria" << "\t" << "w,u,c" << "\n"
<< "\t" << "}" << "\n"
<< "\n";
}
2012-06-27 18:43:34 +02:00
}
/**
* Periodically writes the status.dat and objects.cache files.
*/
void CompatComponent::StatusTimerHandler(void)
{
2013-03-16 21:18:53 +01:00
Log(LogInformation, "compat", "Writing compat status information");
2012-07-18 11:15:39 +02:00
String statuspath = GetStatusPath();
String objectspath = GetObjectsPath();
String statuspathtmp = statuspath + ".tmp"; /* XXX make this a global definition */
String objectspathtmp = objectspath + ".tmp";
2013-03-17 20:19:29 +01:00
std::ofstream statusfp;
statusfp.open(statuspathtmp.CStr(), std::ofstream::out | std::ofstream::trunc);
2012-06-27 18:43:34 +02:00
statusfp << std::fixed;
statusfp << "# Icinga status file" << "\n"
<< "# This file is auto-generated. Do not modify this file." << "\n"
<< "\n";
statusfp << "info {" << "\n"
2012-08-03 18:17:47 +02:00
<< "\t" << "created=" << Utility::GetTime() << "\n"
<< "\t" << "version=2.0" << "\n"
<< "\t" << "}" << "\n"
<< "\n";
statusfp << "programstatus {" << "\n"
2012-09-19 14:10:34 +02:00
<< "icinga_pid=" << Utility::GetPid() << "\n"
<< "\t" << "daemon_mode=1" << "\n"
2013-03-02 09:07:47 +01:00
<< "\t" << "program_start=" << IcingaApplication::GetInstance()->GetStartTime() << "\n"
<< "\t" << "active_service_checks_enabled=1" << "\n"
<< "\t" << "passive_service_checks_enabled=1" << "\n"
<< "\t" << "active_host_checks_enabled=1" << "\n"
<< "\t" << "passive_host_checks_enabled=1" << "\n"
2012-07-02 15:49:36 +02:00
<< "\t" << "check_service_freshness=0" << "\n"
<< "\t" << "check_host_freshness=0" << "\n"
2013-02-24 01:10:34 +01:00
<< "\t" << "enable_notifications=1" << "\n"
<< "\t" << "enable_flap_detection=1" << "\n"
<< "\t" << "enable_failure_prediction=0" << "\n"
<< "\t" << "active_scheduled_service_check_stats=" << CIB::GetActiveChecksStatistics(60) << "," << CIB::GetActiveChecksStatistics(5 * 60) << "," << CIB::GetActiveChecksStatistics(15 * 60) << "\n"
<< "\t" << "passive_service_check_stats=" << CIB::GetPassiveChecksStatistics(60) << "," << CIB::GetPassiveChecksStatistics(5 * 60) << "," << CIB::GetPassiveChecksStatistics(15 * 60) << "\n"
<< "\t" << "next_downtime_id=" << Service::GetNextDowntimeID() << "\n"
<< "\t" << "next_comment_id=" << Service::GetNextCommentID() << "\n"
<< "\t" << "}" << "\n"
<< "\n";
2012-06-27 23:38:50 +02:00
2013-03-17 20:19:29 +01:00
std::ofstream objectfp;
objectfp.open(objectspathtmp.CStr(), std::ofstream::out | std::ofstream::trunc);
2012-06-27 18:43:34 +02:00
objectfp << std::fixed;
objectfp << "# Icinga objects cache file" << "\n"
<< "# This file is auto-generated. Do not modify this file." << "\n"
<< "\n";
2012-06-27 18:43:34 +02:00
2013-02-18 23:44:24 +01:00
BOOST_FOREACH(const DynamicObject::Ptr& object, DynamicType::GetObjects("Host")) {
Host::Ptr host = static_pointer_cast<Host>(object);
2012-06-27 18:43:34 +02:00
2013-03-16 21:18:53 +01:00
std::ostringstream tempstatusfp;
2013-02-26 10:13:54 +01:00
tempstatusfp << std::fixed;
DumpHostStatus(tempstatusfp, host);
statusfp << tempstatusfp.str();
2013-02-26 10:13:54 +01:00
2013-03-16 21:18:53 +01:00
std::ostringstream tempobjectfp;
2013-02-26 10:13:54 +01:00
tempobjectfp << std::fixed;
DumpHostObject(tempobjectfp, host);
objectfp << tempobjectfp.str();
2012-06-27 18:43:34 +02:00
}
2013-02-18 23:44:24 +01:00
BOOST_FOREACH(const DynamicObject::Ptr& object, DynamicType::GetObjects("HostGroup")) {
2013-02-20 19:52:25 +01:00
HostGroup::Ptr hg = static_pointer_cast<HostGroup>(object);
2013-02-19 23:02:08 +01:00
2013-03-16 21:18:53 +01:00
std::ostringstream tempobjectfp;
2013-02-26 10:13:54 +01:00
tempobjectfp << std::fixed;
2013-03-02 09:07:47 +01:00
tempobjectfp << "define hostgroup {" << "\n"
<< "\t" << "hostgroup_name" << "\t" << hg->GetName() << "\n"
<< "\t" << "notes_url" << "\t" << hg->GetNotesUrl() << "\n"
<< "\t" << "action_url" << "\t" << hg->GetActionUrl() << "\n";
2013-02-18 14:40:24 +01:00
tempobjectfp << "\t" << "members" << "\t";
2013-03-02 09:07:47 +01:00
DumpNameList(tempobjectfp, hg->GetMembers());
tempobjectfp << "\n"
<< "\t" << "}" << "\n";
objectfp << tempobjectfp.str();
2012-06-30 13:39:55 +02:00
}
2013-02-18 23:44:24 +01:00
BOOST_FOREACH(const DynamicObject::Ptr& object, DynamicType::GetObjects("Service")) {
Service::Ptr service = static_pointer_cast<Service>(object);
2012-07-16 22:00:50 +02:00
2013-03-16 21:18:53 +01:00
std::ostringstream tempstatusfp;
2013-02-26 10:13:54 +01:00
tempstatusfp << std::fixed;
DumpServiceStatus(tempstatusfp, service);
statusfp << tempstatusfp.str();
2013-02-26 10:13:54 +01:00
2013-03-16 21:18:53 +01:00
std::ostringstream tempobjectfp;
2013-02-26 10:13:54 +01:00
tempobjectfp << std::fixed;
DumpServiceObject(tempobjectfp, service);
objectfp << tempobjectfp.str();
2013-02-18 23:44:24 +01:00
}
2012-06-30 13:39:55 +02:00
2013-02-18 23:44:24 +01:00
BOOST_FOREACH(const DynamicObject::Ptr& object, DynamicType::GetObjects("ServiceGroup")) {
2013-02-20 19:52:25 +01:00
ServiceGroup::Ptr sg = static_pointer_cast<ServiceGroup>(object);
2012-06-30 13:39:55 +02:00
2013-03-16 21:18:53 +01:00
std::ostringstream tempobjectfp;
2013-02-26 10:13:54 +01:00
tempobjectfp << std::fixed;
2013-03-02 09:07:47 +01:00
tempobjectfp << "define servicegroup {" << "\n"
<< "\t" << "servicegroup_name" << "\t" << sg->GetName() << "\n"
<< "\t" << "notes_url" << "\t" << sg->GetNotesUrl() << "\n"
<< "\t" << "action_url" << "\t" << sg->GetActionUrl() << "\n";
2013-02-18 14:40:24 +01:00
tempobjectfp << "\t" << "members" << "\t";
2013-02-18 14:40:24 +01:00
2013-03-16 21:18:53 +01:00
std::vector<String> sglist;
2013-03-02 09:07:47 +01:00
BOOST_FOREACH(const Service::Ptr& service, sg->GetMembers()) {
Host::Ptr host = service->GetHost();
2013-02-19 23:02:08 +01:00
2013-03-02 09:07:47 +01:00
if (!host)
continue;
2013-02-19 23:02:08 +01:00
2013-03-02 09:07:47 +01:00
sglist.push_back(host->GetName());
sglist.push_back(service->GetShortName());
2013-02-18 14:40:24 +01:00
}
2013-02-18 23:44:24 +01:00
DumpStringList(tempobjectfp, sglist);
2013-02-18 23:44:24 +01:00
tempobjectfp << "\n"
2013-02-18 23:44:24 +01:00
<< "}" << "\n";
objectfp << tempobjectfp.str();
2012-06-27 18:43:34 +02:00
}
BOOST_FOREACH(const DynamicObject::Ptr& object, DynamicType::GetObjects("User")) {
User::Ptr user = static_pointer_cast<User>(object);
2013-03-16 21:18:53 +01:00
std::ostringstream tempobjectfp;
tempobjectfp << std::fixed;
2013-03-02 09:07:47 +01:00
tempobjectfp << "define contact {" << "\n"
<< "\t" << "contact_name" << "\t" << user->GetName() << "\n"
<< "\t" << "alias" << "\t" << user->GetDisplayName() << "\n"
<< "\t" << "service_notification_options" << "\t" << "w,u,c,r,f,s" << "\n"
<< "\t" << "host_notification_options" << "\t" << "d,u,r,f,s" << "\n"
<< "\t" << "host_notifications_enabled" << "\t" << 1 << "\n"
<< "\t" << "service_notifications_enabled" << "\t" << 1 << "\n"
<< "\t" << "}" << "\n"
<< "\n";
objectfp << tempobjectfp.str();
}
BOOST_FOREACH(const DynamicObject::Ptr& object, DynamicType::GetObjects("UserGroup")) {
UserGroup::Ptr ug = static_pointer_cast<UserGroup>(object);
2013-03-16 21:18:53 +01:00
std::ostringstream tempobjectfp;
tempobjectfp << std::fixed;
2013-03-02 09:07:47 +01:00
tempobjectfp << "define contactgroup {" << "\n"
<< "\t" << "contactgroup_name" << "\t" << ug->GetName() << "\n"
<< "\t" << "alias" << "\t" << ug->GetDisplayName() << "\n";
tempobjectfp << "\t" << "members" << "\t";
2013-03-02 09:07:47 +01:00
DumpNameList(tempobjectfp, ug->GetMembers());
tempobjectfp << "\n"
<< "\t" << "}" << "\n";
objectfp << tempobjectfp.str();
}
2012-06-27 18:43:34 +02:00
statusfp.close();
objectfp.close();
#ifdef _WIN32
_unlink(statuspath.CStr());
_unlink(objectspath.CStr());
#endif /* _WIN32 */
statusfp.close();
2013-03-11 13:45:08 +01:00
if (rename(statuspathtmp.CStr(), statuspath.CStr()) < 0) {
BOOST_THROW_EXCEPTION(posix_error()
2013-03-18 17:04:22 +01:00
<< boost::errinfo_api_function("rename")
<< boost::errinfo_errno(errno)
<< boost::errinfo_file_name(statuspathtmp));
2013-03-11 13:45:08 +01:00
}
2012-06-27 18:43:34 +02:00
objectfp.close();
2013-03-11 13:45:08 +01:00
if (rename(objectspathtmp.CStr(), objectspath.CStr()) < 0) {
BOOST_THROW_EXCEPTION(posix_error()
2013-03-18 17:04:22 +01:00
<< boost::errinfo_api_function("rename")
<< boost::errinfo_errno(errno)
<< boost::errinfo_file_name(objectspathtmp));
2013-03-11 13:45:08 +01:00
}
2012-06-27 18:43:34 +02:00
}