mirror of https://github.com/Icinga/icinga2.git
Basic support for other scripting languages.
This commit is contained in:
parent
099821ce07
commit
7fac5b454e
|
@ -239,3 +239,11 @@ type Notification {
|
|||
},
|
||||
%attribute string "notification_command"
|
||||
}
|
||||
|
||||
type Script {
|
||||
%require "language",
|
||||
%attribute string "language",
|
||||
|
||||
%require "code",
|
||||
%attribute string "code"
|
||||
}
|
||||
|
|
|
@ -41,8 +41,14 @@ libbase_la_SOURCES = \
|
|||
qstring.h \
|
||||
ringbuffer.cpp \
|
||||
ringbuffer.h \
|
||||
script.cpp \
|
||||
script.h \
|
||||
scriptfunction.cpp \
|
||||
scriptfunction.h \
|
||||
scriptinterpreter.cpp \
|
||||
scriptinterpreter.h \
|
||||
scriptlanguage.cpp \
|
||||
scriptlanguage.h \
|
||||
scripttask.cpp \
|
||||
scripttask.h \
|
||||
socket.cpp \
|
||||
|
|
|
@ -202,6 +202,9 @@ namespace tuples = boost::tuples;
|
|||
#include "scripttask.h"
|
||||
#include "dynamicobject.h"
|
||||
#include "dynamictype.h"
|
||||
#include "script.h"
|
||||
#include "scriptinterpreter.h"
|
||||
#include "scriptlanguage.h"
|
||||
#include "logger.h"
|
||||
#include "application.h"
|
||||
#include "component.h"
|
||||
|
|
|
@ -0,0 +1,60 @@
|
|||
/******************************************************************************
|
||||
* 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. *
|
||||
******************************************************************************/
|
||||
|
||||
#include "i2-base.h"
|
||||
|
||||
using namespace icinga;
|
||||
|
||||
REGISTER_TYPE(Script, NULL);
|
||||
|
||||
/**
|
||||
* Constructor for the Script class.
|
||||
*
|
||||
* @param properties A serialized dictionary containing attributes.
|
||||
*/
|
||||
Script::Script(const Dictionary::Ptr& properties)
|
||||
: DynamicObject(properties)
|
||||
{ }
|
||||
|
||||
String Script::GetLanguage(void) const
|
||||
{
|
||||
return Get("language");
|
||||
}
|
||||
|
||||
String Script::GetCode(void) const
|
||||
{
|
||||
return Get("code");
|
||||
}
|
||||
|
||||
void Script::OnAttributeUpdate(const String& name, const Value& oldValue)
|
||||
{
|
||||
if (name == "code")
|
||||
Reload();
|
||||
}
|
||||
|
||||
void Script::Reload(void)
|
||||
{
|
||||
if (!m_Interpreter) {
|
||||
ScriptLanguage::Ptr language = ScriptLanguage::GetByName(GetLanguage());
|
||||
|
||||
m_Interpreter = language->CreateInterpreter(GetSelf());
|
||||
} else {
|
||||
m_Interpreter->Reload();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,55 @@
|
|||
/******************************************************************************
|
||||
* 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. *
|
||||
******************************************************************************/
|
||||
|
||||
#ifndef SCRIPT_H
|
||||
#define SCRIPT_H
|
||||
|
||||
namespace icinga
|
||||
{
|
||||
|
||||
class ScriptInterpreter;
|
||||
|
||||
/**
|
||||
* A script.
|
||||
*
|
||||
* @ingroup base
|
||||
*/
|
||||
class I2_BASE_API Script : public DynamicObject
|
||||
{
|
||||
public:
|
||||
typedef shared_ptr<Script> Ptr;
|
||||
typedef weak_ptr<Script> WeakPtr;
|
||||
|
||||
Script(const Dictionary::Ptr& properties);
|
||||
|
||||
String GetLanguage(void) const;
|
||||
String GetCode(void) const;
|
||||
|
||||
protected:
|
||||
virtual void OnAttributeUpdate(const String& name, const Value& oldValue);
|
||||
|
||||
private:
|
||||
shared_ptr<ScriptInterpreter> m_Interpreter;
|
||||
|
||||
void Reload(void);
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif /* SCRIPT_H */
|
|
@ -0,0 +1,57 @@
|
|||
/******************************************************************************
|
||||
* 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. *
|
||||
******************************************************************************/
|
||||
|
||||
#include "i2-base.h"
|
||||
|
||||
using namespace icinga;
|
||||
|
||||
ScriptInterpreter::ScriptInterpreter(const Script::Ptr& script)
|
||||
: m_Thread(&ScriptInterpreter::ThreadWorkerProc, this, script)
|
||||
{
|
||||
m_Thread.detach();
|
||||
}
|
||||
|
||||
void ScriptInterpreter::ThreadWorkerProc(const Script::Ptr& script)
|
||||
{
|
||||
ScriptCall call;
|
||||
|
||||
while (WaitForCall(&call))
|
||||
ProcessCall(call);
|
||||
}
|
||||
|
||||
void ScriptInterpreter::EnqueueCall(const ScriptCall& call)
|
||||
{
|
||||
boost::mutex::scoped_lock lock(m_Mutex);
|
||||
m_Calls.push_back(call);
|
||||
m_CallAvailable.notify_all();
|
||||
}
|
||||
|
||||
bool ScriptInterpreter::WaitForCall(ScriptCall *call)
|
||||
{
|
||||
boost::mutex::scoped_lock lock(m_Mutex);
|
||||
|
||||
while (m_Calls.empty())
|
||||
m_CallAvailable.wait(lock);
|
||||
|
||||
*call = m_Calls.front();
|
||||
m_Calls.pop_front();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
|
@ -0,0 +1,67 @@
|
|||
/******************************************************************************
|
||||
* 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. *
|
||||
******************************************************************************/
|
||||
|
||||
#ifndef SCRIPTINTERPRETER_H
|
||||
#define SCRIPTINTERPRETER_H
|
||||
|
||||
namespace icinga
|
||||
{
|
||||
|
||||
struct ScriptCall
|
||||
{
|
||||
String Method;
|
||||
vector<String> Arguments;
|
||||
ScriptTask::Ptr Task;
|
||||
};
|
||||
|
||||
/**
|
||||
* A script interpreter.
|
||||
*
|
||||
* @ingroup base
|
||||
*/
|
||||
class I2_BASE_API ScriptInterpreter : public Object
|
||||
{
|
||||
public:
|
||||
typedef shared_ptr<ScriptInterpreter> Ptr;
|
||||
typedef weak_ptr<ScriptInterpreter> WeakPtr;
|
||||
|
||||
virtual void Reload(void) = 0;
|
||||
|
||||
void EnqueueCall(const ScriptCall& call);
|
||||
|
||||
protected:
|
||||
ScriptInterpreter(const Script::Ptr& script);
|
||||
|
||||
virtual void ProcessCall(const ScriptCall& call) = 0;
|
||||
|
||||
bool WaitForCall(ScriptCall *call);
|
||||
|
||||
private:
|
||||
boost::mutex m_Mutex;
|
||||
deque<ScriptCall> m_Calls;
|
||||
condition_variable m_CallAvailable;
|
||||
|
||||
boost::thread m_Thread;
|
||||
|
||||
void ThreadWorkerProc(const Script::Ptr& script);
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif /* SCRIPTINTERPRETER_H */
|
|
@ -0,0 +1,50 @@
|
|||
/******************************************************************************
|
||||
* 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. *
|
||||
******************************************************************************/
|
||||
|
||||
#include "i2-base.h"
|
||||
|
||||
using namespace icinga;
|
||||
|
||||
void ScriptLanguage::Register(const String& name, const ScriptLanguage::Ptr& language)
|
||||
{
|
||||
GetLanguages()[name] = language;
|
||||
}
|
||||
|
||||
void ScriptLanguage::Unregister(const String& name)
|
||||
{
|
||||
GetLanguages().erase(name);
|
||||
}
|
||||
|
||||
ScriptLanguage::Ptr ScriptLanguage::GetByName(const String& name)
|
||||
{
|
||||
map<String, ScriptLanguage::Ptr>::iterator it;
|
||||
|
||||
it = GetLanguages().find(name);
|
||||
|
||||
if (it == GetLanguages().end())
|
||||
return ScriptLanguage::Ptr();
|
||||
|
||||
return it->second;
|
||||
}
|
||||
|
||||
map<String, ScriptLanguage::Ptr>& ScriptLanguage::GetLanguages(void)
|
||||
{
|
||||
static map<String, ScriptLanguage::Ptr> languages;
|
||||
return languages;
|
||||
}
|
|
@ -0,0 +1,52 @@
|
|||
/******************************************************************************
|
||||
* 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. *
|
||||
******************************************************************************/
|
||||
|
||||
#ifndef SCRIPTLANGUAGE_H
|
||||
#define SCRIPTLANGUAGE_H
|
||||
|
||||
namespace icinga
|
||||
{
|
||||
|
||||
/**
|
||||
* A scripting language.
|
||||
*
|
||||
* @ingroup base
|
||||
*/
|
||||
class I2_BASE_API ScriptLanguage : public Object
|
||||
{
|
||||
public:
|
||||
typedef shared_ptr<ScriptLanguage> Ptr;
|
||||
typedef weak_ptr<ScriptLanguage> WeakPtr;
|
||||
|
||||
static void Register(const String& name, const ScriptLanguage::Ptr& language);
|
||||
static void Unregister(const String& name);
|
||||
static ScriptLanguage::Ptr GetByName(const String& name);
|
||||
|
||||
virtual ScriptInterpreter::Ptr CreateInterpreter(const Script::Ptr& script) = 0;
|
||||
|
||||
protected:
|
||||
ScriptLanguage(void);
|
||||
|
||||
private:
|
||||
static map<String, ScriptLanguage::Ptr>& GetLanguages(void);
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif /* SCRIPTLANGUAGE_H */
|
Loading…
Reference in New Issue