mirror of https://github.com/Icinga/icinga2.git
Implement the PROCESS_FILE external command.
This commit is contained in:
parent
335a0b3067
commit
54ffa0a144
|
@ -147,48 +147,10 @@ void CompatComponent::CommandPipeThread(const String& commandPath)
|
|||
|
||||
void CompatComponent::ProcessCommand(const String& command)
|
||||
{
|
||||
if (command.IsEmpty())
|
||||
return;
|
||||
|
||||
if (command[0] != '[') {
|
||||
Logger::Write(LogWarning, "compat", "Missing timestamp in command: " + command);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
size_t pos = command.FindFirstOf("]");
|
||||
|
||||
if (pos == String::NPos) {
|
||||
Logger::Write(LogWarning, "compat", "Missing timestamp in command: " + command);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
String timestamp = command.SubStr(1, pos - 1);
|
||||
String args = command.SubStr(pos + 2, String::NPos);
|
||||
|
||||
double ts = timestamp.ToDouble();
|
||||
|
||||
if (ts == 0) {
|
||||
Logger::Write(LogWarning, "compat", "Invalid timestamp in command: " + command);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
vector<String> argv = args.Split(is_any_of(";"));
|
||||
|
||||
if (argv.size() == 0) {
|
||||
Logger::Write(LogWarning, "compat", "Missing arguments in command: " + command);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
vector<String> argvExtra(argv.begin() + 1, argv.end());
|
||||
|
||||
try {
|
||||
Logger::Write(LogInformation, "compat", "Executing external command: " + command);
|
||||
|
||||
ExternalCommand::Execute(ts, argv[0], argvExtra);
|
||||
ExternalCommand::Execute(command);
|
||||
} catch (const exception& ex) {
|
||||
stringstream msgbuf;
|
||||
msgbuf << "External command failed: " << ex.what();
|
||||
|
|
|
@ -24,6 +24,36 @@ using namespace icinga;
|
|||
bool I2_EXPORT ExternalCommand::m_Initialized;
|
||||
map<String, ExternalCommand::Callback> I2_EXPORT ExternalCommand::m_Commands;
|
||||
|
||||
void ExternalCommand::Execute(const String& line)
|
||||
{
|
||||
if (line.IsEmpty())
|
||||
return;
|
||||
|
||||
if (line[0] != '[')
|
||||
throw_exception(invalid_argument("Missing timestamp in command: " + line));
|
||||
|
||||
size_t pos = line.FindFirstOf("]");
|
||||
|
||||
if (pos == String::NPos)
|
||||
throw_exception(invalid_argument("Missing timestamp in command: " + line));
|
||||
|
||||
String timestamp = line.SubStr(1, pos - 1);
|
||||
String args = line.SubStr(pos + 2, String::NPos);
|
||||
|
||||
double ts = timestamp.ToDouble();
|
||||
|
||||
if (ts == 0)
|
||||
throw_exception(invalid_argument("Invalid timestamp in command: " + line));
|
||||
|
||||
vector<String> argv = args.Split(is_any_of(";"));
|
||||
|
||||
if (argv.size() == 0)
|
||||
throw_exception(invalid_argument("Missing arguments in command: " + line));
|
||||
|
||||
vector<String> argvExtra(argv.begin() + 1, argv.end());
|
||||
Execute(ts, argv[0], argvExtra);
|
||||
}
|
||||
|
||||
void ExternalCommand::Execute(double time, const String& command, const vector<String>& arguments)
|
||||
{
|
||||
if (!m_Initialized) {
|
||||
|
@ -51,6 +81,7 @@ void ExternalCommand::Execute(double time, const String& command, const vector<S
|
|||
RegisterCommand("DISABLE_SERVICEGROUP_PASSIVE_SVC_CHECKS", &ExternalCommand::DisableServicegroupPassiveSvcChecks);
|
||||
RegisterCommand("ENABLE_HOSTGROUP_PASSIVE_SVC_CHECKS", &ExternalCommand::EnableHostgroupPassiveSvcChecks);
|
||||
RegisterCommand("DISABLE_HOSTGROUP_PASSIVE_SVC_CHECKS", &ExternalCommand::DisableHostgroupPassiveSvcChecks);
|
||||
RegisterCommand("PROCESS_FILE", &ExternalCommand::ProcessFile);
|
||||
|
||||
m_Initialized = true;
|
||||
}
|
||||
|
@ -490,3 +521,37 @@ void ExternalCommand::DisableHostgroupPassiveSvcChecks(double time, const vector
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ExternalCommand::ProcessFile(double time, const vector<String>& arguments)
|
||||
{
|
||||
if (arguments.size() < 2)
|
||||
throw_exception(invalid_argument("Expected 2 arguments."));
|
||||
|
||||
String file = arguments[0];
|
||||
int del = arguments[1].ToDouble();
|
||||
|
||||
ifstream ifp;
|
||||
ifp.exceptions(ifstream::badbit);
|
||||
|
||||
ifp.open(file, ifstream::in);
|
||||
|
||||
while(ifp.good()) {
|
||||
std::string line;
|
||||
std::getline(ifp, line);
|
||||
|
||||
try {
|
||||
Logger::Write(LogInformation, "compat", "Executing external command: " + line);
|
||||
|
||||
Execute(line);
|
||||
} catch (const exception& ex) {
|
||||
stringstream msgbuf;
|
||||
msgbuf << "External command failed: " << ex.what();
|
||||
Logger::Write(LogWarning, "icinga", msgbuf.str());
|
||||
}
|
||||
}
|
||||
|
||||
ifp.close();
|
||||
|
||||
if (del)
|
||||
(void) unlink(file.CStr());
|
||||
}
|
||||
|
|
|
@ -25,7 +25,7 @@ namespace icinga
|
|||
|
||||
class I2_ICINGA_API ExternalCommand {
|
||||
public:
|
||||
|
||||
static void Execute(const String& line);
|
||||
static void Execute(double time, const String& command, const vector<String>& arguments);
|
||||
|
||||
static void HelloWorld(double time, const vector<String>& arguments);
|
||||
|
@ -52,6 +52,7 @@ public:
|
|||
static void DisableServicegroupPassiveSvcChecks(double time, const vector<String>& arguments);
|
||||
static void EnableHostgroupPassiveSvcChecks(double time, const vector<String>& arguments);
|
||||
static void DisableHostgroupPassiveSvcChecks(double time, const vector<String>& arguments);
|
||||
static void ProcessFile(double time, const vector<String>& arguments);
|
||||
|
||||
private:
|
||||
typedef function<void (double time, const vector<String>& arguments)> Callback;
|
||||
|
|
Loading…
Reference in New Issue