icinga2/icinga/icingaapplication.cpp

186 lines
5.7 KiB
C++
Raw Normal View History

2012-03-31 16:29:53 +02:00
#include <cstdio>
#include <iostream>
#include "i2-icinga.h"
#ifndef _WIN32
# include "icinga-version.h"
# define ICINGA_VERSION GIT_MESSAGE
#endif /* _WIN32 */
2012-03-31 16:29:53 +02:00
using namespace icinga;
int IcingaApplication::Main(const vector<string>& args)
{
#ifdef _WIN32
cout << "Icinga component loader" << endl;
#else /* _WIN32 */
cout << "Icinga component loader (version: " << ICINGA_VERSION << ")" << endl;
#endif /* _WIN32 */
2012-04-02 13:09:33 +02:00
if (args.size() < 2) {
PrintUsage(args[0]);
return EXIT_FAILURE;
}
2012-04-24 14:02:15 +02:00
shared_ptr<SSL_CTX> sslContext = Utility::MakeSSLContext("icinga-c1.crt", "icinga-c1.key", "ca.crt");
m_EndpointManager = make_shared<EndpointManager>(sslContext);
string componentDirectory = GetExeDirectory() + "/../lib/icinga";
2012-04-02 13:09:33 +02:00
AddComponentSearchDir(componentDirectory);
ConfigCollection::Ptr componentCollection = GetConfigHive()->GetCollection("component");
2012-04-20 13:49:04 +02:00
function<int (const EventArgs&)> NewComponentHandler = bind_weak(&IcingaApplication::NewComponentHandler, shared_from_this());
componentCollection->OnObjectCreated += NewComponentHandler;
componentCollection->ForEachObject(NewComponentHandler);
componentCollection->OnObjectRemoved += bind_weak(&IcingaApplication::DeletedComponentHandler, shared_from_this());
ConfigCollection::Ptr listenerCollection = GetConfigHive()->GetCollection("rpclistener");
2012-04-20 13:49:04 +02:00
function<int (const EventArgs&)> NewRpcListenerHandler = bind_weak(&IcingaApplication::NewRpcListenerHandler, shared_from_this());
listenerCollection->OnObjectCreated += NewRpcListenerHandler;
listenerCollection->ForEachObject(NewRpcListenerHandler);
listenerCollection->OnObjectRemoved += bind_weak(&IcingaApplication::DeletedRpcListenerHandler, shared_from_this());
ConfigCollection::Ptr connectionCollection = GetConfigHive()->GetCollection("rpcconnection");
2012-04-20 13:49:04 +02:00
function<int (const EventArgs&)> NewRpcConnectionHandler = bind_weak(&IcingaApplication::NewRpcConnectionHandler, shared_from_this());
connectionCollection->OnObjectCreated += NewRpcConnectionHandler;
connectionCollection->ForEachObject(NewRpcConnectionHandler);
connectionCollection->OnObjectRemoved += bind_weak(&IcingaApplication::DeletedRpcConnectionHandler, shared_from_this());
AuthenticationComponent::Ptr authenticationComponent = make_shared<AuthenticationComponent>();
RegisterComponent(authenticationComponent);
SubscriptionComponent::Ptr subscriptionComponent = make_shared<SubscriptionComponent>();
RegisterComponent(subscriptionComponent);
2012-04-23 16:51:03 +02:00
DiscoveryComponent::Ptr discoveryComponent = make_shared<DiscoveryComponent>();
RegisterComponent(discoveryComponent);
2012-04-13 13:24:32 +02:00
ConfigObject::Ptr fileComponentConfig = make_shared<ConfigObject>("component", "configfile");
2012-04-20 13:49:04 +02:00
fileComponentConfig->SetPropertyString("configFilename", args[1]);
fileComponentConfig->SetPropertyInteger("replicate", 0);
2012-03-31 16:29:53 +02:00
GetConfigHive()->AddObject(fileComponentConfig);
ConfigCollection::Ptr collection = GetConfigHive()->GetCollection("rpclistener");
2012-03-31 16:29:53 +02:00
RunEventLoop();
2012-04-02 13:09:33 +02:00
return EXIT_SUCCESS;
}
void IcingaApplication::PrintUsage(const string& programPath)
{
cout << "Syntax: " << programPath << " <config-file>" << endl;
2012-03-31 16:29:53 +02:00
}
EndpointManager::Ptr IcingaApplication::GetEndpointManager(void)
2012-03-31 16:29:53 +02:00
{
return m_EndpointManager;
2012-03-31 16:29:53 +02:00
}
2012-04-20 13:49:04 +02:00
int IcingaApplication::NewComponentHandler(const EventArgs& ea)
{
string path;
2012-04-18 15:22:25 +02:00
ConfigObject::Ptr object = static_pointer_cast<ConfigObject>(ea.Source);
2012-04-20 13:49:04 +02:00
/* don't allow replicated config objects */
if (object->GetReplicated())
return 0;
2012-04-20 13:49:04 +02:00
if (!object->GetPropertyString("path", &path)) {
#ifdef _WIN32
path = object->GetName() + ".dll";
#else /* _WIN32 */
2012-04-13 11:32:19 +02:00
path = object->GetName() + ".la";
#endif /* _WIN32 */
}
LoadComponent(path, object);
return 0;
}
2012-04-20 13:49:04 +02:00
int IcingaApplication::DeletedComponentHandler(const EventArgs& ea)
{
2012-04-18 15:22:25 +02:00
ConfigObject::Ptr object = static_pointer_cast<ConfigObject>(ea.Source);
2012-04-18 15:22:25 +02:00
Component::Ptr component = GetComponent(object->GetName());
UnregisterComponent(component);
return 0;
}
2012-04-20 13:49:04 +02:00
int IcingaApplication::NewRpcListenerHandler(const EventArgs& ea)
{
2012-04-18 15:22:25 +02:00
ConfigObject::Ptr object = static_pointer_cast<ConfigObject>(ea.Source);
2012-04-20 13:49:04 +02:00
long portValue;
unsigned short port;
/* don't allow replicated config objects */
if (object->GetReplicated())
return 0;
2012-04-20 13:49:04 +02:00
if (!object->GetPropertyInteger("port", &portValue))
throw InvalidArgumentException("Parameter 'port' is required for 'rpclistener' objects.");
if (portValue < 0 || portValue > USHRT_MAX)
throw InvalidArgumentException("Parameter 'port' contains an invalid value.");
port = (unsigned short)portValue;
Log("Creating JSON-RPC listener on port %d", port);
GetEndpointManager()->AddListener(port);
return 0;
}
2012-04-20 13:49:04 +02:00
int IcingaApplication::DeletedRpcListenerHandler(const EventArgs& ea)
{
throw Exception("Unsupported operation.");
return 0;
}
2012-04-20 13:49:04 +02:00
int IcingaApplication::NewRpcConnectionHandler(const EventArgs& ea)
{
2012-04-18 15:22:25 +02:00
ConfigObject::Ptr object = static_pointer_cast<ConfigObject>(ea.Source);
string hostname;
2012-04-20 13:49:04 +02:00
long portValue;
unsigned short port;
/* don't allow replicated config objects */
if (object->GetReplicated())
return 0;
2012-04-20 13:49:04 +02:00
if (!object->GetPropertyString("hostname", &hostname))
throw InvalidArgumentException("Parameter 'hostname' is required for 'rpcconnection' objects.");
if (!object->GetPropertyInteger("port", &portValue))
throw InvalidArgumentException("Parameter 'port' is required for 'rpcconnection' objects.");
2012-04-20 13:49:04 +02:00
if (portValue < 0 || portValue > USHRT_MAX)
throw InvalidArgumentException("Parameter 'port' contains an invalid value.");
2012-04-20 13:49:04 +02:00
port = (unsigned short)portValue;
Log("Creating JSON-RPC connection to %s:%d", hostname.c_str(), port);
GetEndpointManager()->AddConnection(hostname, port);
return 0;
}
2012-04-20 13:49:04 +02:00
int IcingaApplication::DeletedRpcConnectionHandler(const EventArgs& ea)
{
throw Exception("Unsupported operation.");
return 0;
}