Module: check_mk
Branch: master
Commit: 75b9ac47a1d21464c958bfec9b535722e15bf4dd
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=75b9ac47a1d214…
Author: Sergey Kipnis <sk(a)mathias-kettner.de>
Date: Fri Mar 22 13:30:59 2019 +0100
[CMK-1807] - self testing call "-test self"
- allows every 25 second call of the agent with results on
- diagnostic is imporved
Change-Id: Iee8241ebff6d804e492aab3c518806caeaf4ebd4
---
agents/wnx/src/engine/external_port.h | 13 +++-
agents/wnx/src/engine/providers/plugins.cpp | 4 +-
agents/wnx/src/engine/service_processor.h | 6 +-
agents/wnx/src/engine/windows_service_api.cpp | 90 +++++++++++++++++++++++++--
4 files changed, 102 insertions(+), 11 deletions(-)
diff --git a/agents/wnx/src/engine/external_port.h
b/agents/wnx/src/engine/external_port.h
index e0078c4..a8a56eb 100644
--- a/agents/wnx/src/engine/external_port.h
+++ b/agents/wnx/src/engine/external_port.h
@@ -208,9 +208,18 @@ private:
// #TODO blocking call here. This is not a good idea
x->start(Reply);
+ } catch (const std::system_error& e) {
+ if (e.code().value() == WSAECONNRESET)
+ XLOG::l.i(XLOG_FLINE + " Client closed
connection");
+ else
+ XLOG::l(
+ XLOG_FLINE +
+ " Thrown unexpected exception '{}' with
value {}",
+ e.what(), e.code().value());
} catch (const std::exception& e) {
- XLOG::l(XLOG_FLINE + " Thrown unexpected exception
{}",
- e.what());
+ XLOG::l(
+ XLOG_FLINE + " Thrown unexpected exception
'{}'",
+ e.what());
}
}
diff --git a/agents/wnx/src/engine/providers/plugins.cpp
b/agents/wnx/src/engine/providers/plugins.cpp
index 69d1d30..332c5aa 100644
--- a/agents/wnx/src/engine/providers/plugins.cpp
+++ b/agents/wnx/src/engine/providers/plugins.cpp
@@ -88,7 +88,7 @@ void PluginsProvider::gatherAllData(std::string& Out) {
// empty body empty
void PluginsProvider::updateSectionStatus() {
- XLOG::d(XLOG_FUNC + " !");
+ XLOG::d.t(XLOG_FUNC + " !");
std::string out = cma::section::MakeEmptyHeader();
gatherAllData(out);
out += cma::section::MakeEmptyHeader();
@@ -97,7 +97,7 @@ void PluginsProvider::updateSectionStatus() {
// local body empty
void LocalProvider::updateSectionStatus() {
- XLOG::d(XLOG_FUNC + " !");
+ XLOG::d.t(XLOG_FUNC + " !");
std::string out = cma::section::MakeLocalHeader();
gatherAllData(out);
out += cma::section::MakeEmptyHeader();
diff --git a/agents/wnx/src/engine/service_processor.h
b/agents/wnx/src/engine/service_processor.h
index 907f5b7..bd95240 100644
--- a/agents/wnx/src/engine/service_processor.h
+++ b/agents/wnx/src/engine/service_processor.h
@@ -556,9 +556,9 @@ private:
[this, ExeName](const AnswerId Tp, ServiceProcessor* Proc,
const std::wstring& SegmentName, int Timeout,
const std::wstring& CommandLine) {
- XLOG::l("Exec {} for {} started",
- wtools::ConvertToUTF8(ExeName),
- wtools::ConvertToUTF8(SegmentName));
+ XLOG::l.i("Exec {} for {} started",
+ wtools::ConvertToUTF8(ExeName),
+ wtools::ConvertToUTF8(SegmentName));
auto full_path = cma::cfg::FindExeFileOnPath(ExeName);
if (full_path.empty()) {
diff --git a/agents/wnx/src/engine/windows_service_api.cpp
b/agents/wnx/src/engine/windows_service_api.cpp
index c90e4c2..90d45f3 100644
--- a/agents/wnx/src/engine/windows_service_api.cpp
+++ b/agents/wnx/src/engine/windows_service_api.cpp
@@ -150,6 +150,81 @@ static void CheckForCommand(std::string& Command) {
return;
}
+// on -test self
+int TestMainServiceSelf() {
+ XLOG::setup::DuplicateOnStdio(true);
+ XLOG::setup::ColoredOutputOnStdio(true);
+ bool stop = false;
+
+ // not a best method to call thread, but this is only for VISUAL testing
+ std::thread kick_and_print([&stop]() {
+ auto port = cma::cfg::groups::global.port();
+
+ using namespace asio;
+
+ io_context ios;
+ std::string address = "127.0.0.1";
+
+ ip::tcp::endpoint endpoint(ip::make_address(address), port);
+
+ asio::ip::tcp::socket socket(ios);
+ std::error_code ec;
+
+ while (!stop) {
+ auto enc = cma::cfg::groups::global.globalEncrypt();
+ auto password = enc ? cma::cfg::groups::global.password() : "";
+ socket.connect(endpoint, ec);
+ if (ec.value() != 0) {
+ XLOG::l("Can't connect to {}:{}, waiting for 5 seconds",
+ address, port);
+
+ // methods below is not a good still we do not want
+ // to over complicate the code just for testing purposes
+ for (int i = 0; i < 5; i++) {
+ if (stop) break;
+ cma::tools::sleep(1000);
+ }
+ if (stop) break;
+ continue;
+ }
+ error_code error;
+ std::vector<char> v;
+ for (;;) {
+ char text[4096];
+ auto count = socket.read_some(asio::buffer(text), error);
+ if (error.value()) break;
+ if (count) {
+ v.insert(v.end(), text, text + count);
+ }
+ }
+ XLOG::l.i("Received {} bytes", v.size());
+ if (enc && password[0]) {
+ XLOG::l.i("Decrypting {} bytes", v.size());
+ // attempt to decode
+ cma::encrypt::Commander e(password);
+ auto size = v.size();
+ v.resize(size + 1024);
+ auto [ret, sz] = e.decode(v.data(), size, true);
+ XLOG::l.i("Decrypted {} bytes {}", ret, sz);
+ }
+ socket.close();
+ // methods below is not a good still we do not want
+ // to over complicate the code just for testing purposes
+ for (int i = 0; i < 25; i++) {
+ if (stop) break;
+ cma::tools::sleep(1000);
+ }
+ }
+ XLOG::l.i("Leaving testing thread");
+ });
+
+ ExecMainService(); // blocking call waiting for keypress
+ stop = true;
+ if (kick_and_print.joinable()) kick_and_print.join();
+
+ return 0;
+}
+
// on -test
int TestMainService(const std::wstring& What) {
using namespace std::chrono;
@@ -195,10 +270,17 @@ int TestMainService(const std::wstring& What) {
2000ms, [&command](const void* Sp) { return true; });
sp.startServiceAsLegacyTest();
sp.stopService();
+ } else if (What == L"self") {
+ TestMainServiceSelf();
+ } else {
+ XLOG::setup::DuplicateOnStdio(true);
+ XLOG::setup::ColoredOutputOnStdio(true);
+ XLOG::l(
+ "Unsupported second parameter\n\tAllowed: port, mt, legacy,
self");
}
return 0;
-}
+} // namespace srv
// on -cvt
// may be used as internal API function to convert ini to yaml
@@ -408,8 +490,9 @@ int ServiceAsService(
auto processor = new ServiceProcessor(Delay, InternalCallback);
wtools::ServiceController service_controller(processor);
auto ret = service_controller.registerAndRun(
- cma::srv::kServiceName); // we will stay here till service will
- // be stopped itself or from outside
+ cma::srv::kServiceName); // we will stay here till
+ // service will be stopped
+ // itself or from outside
return ret ? 0 : -1;
} catch (const std::exception& e) {
XLOG::l.crit("Exception hit {} in main proc", e.what());
@@ -421,5 +504,4 @@ int ServiceAsService(
}
} // namespace srv
-
}; // namespace cma