Skip to content

Commit

Permalink
Implement support for writing the objects.json file
Browse files Browse the repository at this point in the history
refs #6702
  • Loading branch information
gunnarbeutner committed Aug 14, 2014
1 parent a6335a7 commit 6ee94bc
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 7 deletions.
1 change: 1 addition & 0 deletions doc/6-configuring-icinga-2.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 13,7 @@ LocalStateDir |**Read-only.** Contains the path of the local state directo
RunDir |**Read-only.** Contains the path of the run directory. Defaults to LocalStateDir "/run".
PkgDataDir |**Read-only.** Contains the path of the package data directory. Defaults to PrefixDir "/share/icinga2".
StatePath |**Read-write.** Contains the path of the Icinga 2 state file. Defaults to LocalStateDir "/lib/icinga2/icinga2.state".
ObjectsPath |**Read-write.** Contains the path of the Icinga 2 objects file. Defaults to LocalStateDir "/cache/icinga2/objects.json".
PidPath |**Read-write.** Contains the path of the Icinga 2 PID file. Defaults to RunDir "/icinga2/icinga2.pid".
Vars |**Read-write.** Contains a dictionary with global custom attributes. Not set by default.
NodeName |**Read-write.** Contains the cluster node name. Set to the local hostname by default.
Expand Down
7 changes: 4 additions & 3 deletions icinga-app/icinga.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 82,7 @@ static void IncludeNonLocalZone(const String& zonePath)
IncludeZoneDirRecursive(zonePath);
}

static bool LoadConfigFiles(const String& appType)
static bool LoadConfigFiles(const String& appType, const String& objectsFile = String())
{
ConfigCompilerContext::GetInstance()->Reset();

Expand Down Expand Up @@ -113,7 113,7 @@ static bool LoadConfigFiles(const String& appType)
ConfigItem::Ptr item = builder->Compile();
item->Register();

bool result = ConfigItem::ValidateItems();
bool result = ConfigItem::ValidateItems(objectsFile);

int warnings = 0, errors = 0;

Expand Down Expand Up @@ -382,6 382,7 @@ int Main(void)
}

Application::DeclareStatePath(Application::GetLocalStateDir() "/lib/icinga2/icinga2.state");
Application::DeclareObjectsPath(Application::GetLocalStateDir() "/cache/icinga2/objects.json");
Application::DeclarePidPath(Application::GetRunDir() "/icinga2/icinga2.pid");

#ifndef _WIN32
Expand Down Expand Up @@ -543,7 544,7 @@ int Main(void)
}
}

if (!LoadConfigFiles(appType))
if (!LoadConfigFiles(appType, Application::GetObjectsPath()))
return EXIT_FAILURE;

if (g_AppParams.count("validate")) {
Expand Down
26 changes: 24 additions & 2 deletions lib/base/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 478,7 @@ void Application::DisplayInfoMessage(bool skipVersion)
<< " Local state directory: " << GetLocalStateDir() << std::endl
<< " Package data directory: " << GetPkgDataDir() << std::endl
<< " State path: " << GetStatePath() << std::endl
<< " Objects path: " << GetObjectsPath() << std::endl
<< " PID path: " << GetPidPath() << std::endl
<< " Application type: " << GetApplicationType() << std::endl;
}
Expand Down Expand Up @@ -976,6 977,26 @@ void Application::DeclareStatePath(const String& path)
ScriptVariable::Set("StatePath", path, false);
}

/**
* Retrieves the path for the objects file.
*
* @returns The path.
*/
String Application::GetObjectsPath(void)
{
return ScriptVariable::Get("ObjectsPath");
}

/**
* Sets the path for the objects file.
*
* @param path The new path.
*/
void Application::DeclareObjectsPath(const String& path)
{
ScriptVariable::Set("ObjectsPath", path, false);
}

/**
* Retrieves the path for the PID file.
*
Expand Down Expand Up @@ -1023,8 1044,9 @@ void Application::MakeVariablesConstant(void)
ScriptVariable::GetByName("LocalStateDir")->SetConstant(true);
ScriptVariable::GetByName("RunDir")->SetConstant(true);
ScriptVariable::GetByName("PkgDataDir")->SetConstant(true);
ScriptVariable::GetByName("StatePath")->SetConstant(false);
ScriptVariable::GetByName("PidPath")->SetConstant(false);
ScriptVariable::GetByName("StatePath")->SetConstant(true);
ScriptVariable::GetByName("ObjectsPath")->SetConstant(true);
ScriptVariable::GetByName("PidPath")->SetConstant(true);
ScriptVariable::GetByName("ApplicationType")->SetConstant(true);
}

Expand Down
3 changes: 3 additions & 0 deletions lib/base/application.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 104,9 @@ class I2_BASE_API Application : public ObjectImpl<Application> {
static String GetStatePath(void);
static void DeclareStatePath(const String& path);

static String GetObjectsPath(void);
static void DeclareObjectsPath(const String& path);

static String GetPidPath(void);
static void DeclarePidPath(const String& path);

Expand Down
53 changes: 52 additions & 1 deletion lib/config/configitem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 30,10 @@
#include "base/debug.hpp"
#include "base/workqueue.hpp"
#include "base/exception.hpp"
#include "base/stdiostream.hpp"
#include "base/netstring.hpp"
#include <sstream>
#include <fstream>
#include <boost/foreach.hpp>

using namespace icinga;
Expand Down Expand Up @@ -264,7 267,52 @@ void ConfigItem::ValidateItem(void)
m_Validated = true;
}

bool ConfigItem::ValidateItems(void)
void ConfigItem::WriteObjectsFile(const String& filename)
{
Log(LogInformation, "ConfigItem", "Dumping config items to file '" filename "'");

String tempFilename = filename ".tmp";

std::fstream fp;
fp.open(tempFilename.CStr(), std::ios_base::out);

if (!fp)
BOOST_THROW_EXCEPTION(std::runtime_error("Could not open '" tempFilename "' file"));

StdioStream::Ptr sfp = make_shared<StdioStream>(&fp, false);

BOOST_FOREACH(const ItemMap::value_type& kv, m_Items) {
ConfigItem::Ptr item = kv.second;

Dictionary::Ptr persistentItem = make_shared<Dictionary>();

persistentItem->Set("type", item->GetType());
persistentItem->Set("name", item->GetName());
persistentItem->Set("abstract", item->IsAbstract());
persistentItem->Set("properties", item->GetProperties());

String json = JsonSerialize(persistentItem);

NetString::WriteStringToStream(sfp, json);
}

sfp->Close();

fp.close();

#ifdef _WIN32
_unlink(filename.CStr());
#endif /* _WIN32 */

if (rename(tempFilename.CStr(), filename.CStr()) < 0) {
BOOST_THROW_EXCEPTION(posix_error()
<< boost::errinfo_api_function("rename")
<< boost::errinfo_errno(errno)
<< boost::errinfo_file_name(tempFilename));
}
}

bool ConfigItem::ValidateItems(const String& objectsFile)
{
if (ConfigCompilerContext::GetInstance()->HasErrors())
return false;
Expand Down Expand Up @@ -323,6 371,9 @@ bool ConfigItem::ValidateItems(void)

upq.Join();

if (!objectsFile.IsEmpty())
ConfigItem::WriteObjectsFile(objectsFile);

ConfigItem::DiscardItems();
ConfigType::DiscardTypes();

Expand Down
4 changes: 3 additions & 1 deletion lib/config/configitem.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 64,12 @@ class I2_CONFIG_API ConfigItem : public Object {

void ValidateItem(void);

static bool ValidateItems(void);
static bool ValidateItems(const String& objectsFile = String());
static bool ActivateItems(void);
static void DiscardItems(void);

static void WriteObjectsFile(const String& filename);

private:
String m_Type; /**< The object type. */
String m_Name; /**< The name. */
Expand Down

0 comments on commit 6ee94bc

Please sign in to comment.