Skip to content

Commit

Permalink
add time line for the check_mk section
Browse files Browse the repository at this point in the history
integration tests fixed too

Change-Id: Id9c7344f1fba5ac27cfcc92dabdf4af0155c0a73
  • Loading branch information
s-kipnis committed Jan 6, 2024
1 parent eda8398 commit 3e3f7a1
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 20 deletions.
9 changes: 9 additions & 0 deletions agents/wnx/include/providers/check_mk.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#ifndef CHECK_MK_H
#define CHECK_MK_H

#include <chrono>
#include <string>
#include <string_view>

Expand Down Expand Up @@ -34,6 +35,14 @@ class CheckMk final : public Synchronous {
static std::string makeOnlyFrom();
};

std::string GetTimezoneOffset();

template <typename T>
std::string PrintIsoTime(std::chrono::time_point<T> t) {
return std::format("{:%FT%T}{}",
std::chrono::time_point_cast<std::chrono::seconds>(t),
GetTimezoneOffset());
}
} // namespace cma::provider

#endif // CHECK_MK_H
10 changes: 10 additions & 0 deletions agents/wnx/src/engine/providers/check_mk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@ using namespace std::string_literals;

namespace cma::provider {

std::string GetTimezoneOffset() {
const auto tm =
std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
const auto ret = std::put_time(std::localtime(&tm), "%z");
std::stringstream sss;
sss << ret;
return sss.str();
}

// function to provide format compatibility for monitoring site
// probably, a bit to pedantic
std::string AddressToCheckMkString(std::string_view entry) {
Expand Down Expand Up @@ -69,6 +78,7 @@ std::string MakeInfo() {
{"AgentOS", "windows"},
{"Hostname", cfg::GetHostName()},
{"Architecture", tgt::Is64bit() ? "64bit" : "32bit"},
{"Time", PrintIsoTime(std::chrono::system_clock::now())},
};
std::string out;
for (const auto &info : infos) {
Expand Down
26 changes: 24 additions & 2 deletions agents/wnx/tests/integration/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,36 @@
import telnetlib3 # type: ignore[import]
import yaml

# check_mk section, example of output
# <<<check_mk>>>
# Version: 2.3.0b1
# BuildDate: Jan 5 2024
# AgentOS: windows
# Hostname: klapp-9999
# Architecture: 64bit
# Time: 2024-01-05T14:47:46+0100
# WorkingDirectory: C:\Program Files (x86)\checkmk\service
# ConfigFile: C:\Program Files (x86)\checkmk\service\check_mk.yml
# LocalConfigFile: C:\ProgramData\checkmk\agent\check_mk.user.yml
# AgentDirectory: C:\Program Files (x86)\checkmk\service
# PluginsDirectory: C:\ProgramData\checkmk\agent\plugins
# StateDirectory: C:\ProgramData\checkmk\agent\state
# ConfigDirectory: C:\ProgramData\checkmk\agent\config
# TempDirectory: C:\ProgramData\checkmk\agent\tmp
# LogDirectory: C:\ProgramData\checkmk\agent\log
# SpoolDirectory: C:\ProgramData\checkmk\agent\spool
# LocalDirectory: C:\ProgramData\checkmk\agent\local
# OnlyFrom:


YamlDict = dict[str, dict[str, Any]]
INTEGRATION_PORT: Final = 25998
AGENT_EXE_NAME: Final = "check_mk_agent.exe"
_HOST: Final = "localhost"
USER_YAML_CONFIG: Final = "check_mk.user.yml"
SECTION_COUNT: Final = 18
ONLY_FROM_LINE: Final = 17
CTL_STATUS_LINE: Final = 19
ONLY_FROM_LINE: Final = 18
CTL_STATUS_LINE: Final = 20
PYTHON_CAB_NAME: Final = "python-3.cab"
CMK_UPDATER_PY: Final = "cmk_update_agent.py"
CMK_UPDATER_CHECKMK_PY: Final = "cmk_update_agent.checkmk.py"
Expand Down
14 changes: 7 additions & 7 deletions agents/wnx/watest/test-section_providers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,15 +111,15 @@ TEST(SectionProviders, SystemTime) {

class SectionProviderCheckMkFixture : public ::testing::Test {
public:
static constexpr size_t core_lines_ = 19;
static constexpr size_t core_lines_ = 20;
static constexpr size_t full_lines_ = core_lines_ + 3;
static constexpr std::string_view names_[core_lines_ - 1] = {
"Version", "BuildDate", "AgentOS",
"Hostname", "Architecture", "WorkingDirectory",
"ConfigFile", "LocalConfigFile", "AgentDirectory",
"PluginsDirectory", "StateDirectory", "ConfigDirectory",
"TempDirectory", "LogDirectory", "SpoolDirectory",
"LocalDirectory", "OnlyFrom"};
"Version", "BuildDate", "AgentOS",
"Hostname", "Architecture", "Time",
"WorkingDirectory", "ConfigFile", "LocalConfigFile",
"AgentDirectory", "PluginsDirectory", "StateDirectory",
"ConfigDirectory", "TempDirectory", "LogDirectory",
"SpoolDirectory", "LocalDirectory", "OnlyFrom"};

static constexpr std::pair<std::string_view, std::string_view>
only_from_cases_[] = {
Expand Down
26 changes: 15 additions & 11 deletions agents/wnx/watest/test_check_mk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,27 @@

#include "pch.h"

#include "wnx/cfg.h"
#include "providers/check_mk.h"
#include "wnx/cfg.h"

namespace cma::provider {
TEST(CheckMkHeader, Convert) {
auto local_host = AddressToCheckMkString("127.0.0.1");
EXPECT_EQ(local_host, "127.0.0.1");

auto usual_addr = AddressToCheckMkString("10.1.2.3");
EXPECT_EQ(usual_addr, "10.1.2.3");
TEST(CheckMkHeader, GetTimeZone) {
constexpr std::chrono::system_clock::time_point tp;
EXPECT_EQ(GetTimezoneOffset(), "+0100");
}

auto ipv6_addr =
AddressToCheckMkString("2001:0db8:85a3:0000:0000:8a2e:0370:7334");
EXPECT_EQ(ipv6_addr, "2001:0db8:85a3:0000:0000:8a2e:0370:7334");
TEST(CheckMkHeader, IsoTime) {
constexpr std::chrono::system_clock::time_point tp;
EXPECT_EQ(PrintIsoTime(tp), "1970-01-01T00:00:00+0100");
}

auto a3 = AddressToCheckMkString("10.1.2.3/4");
EXPECT_EQ(a3, "10.1.2.3/4");
TEST(CheckMkHeader, Convert) {
EXPECT_EQ(AddressToCheckMkString("127.0.0.1"), "127.0.0.1");
EXPECT_EQ(AddressToCheckMkString("10.1.2.3"), "10.1.2.3");
EXPECT_EQ(AddressToCheckMkString("2001:0db8:85a3:0000:0000:8a2e:0370:7334"),
"2001:0db8:85a3:0000:0000:8a2e:0370:7334");
EXPECT_EQ(AddressToCheckMkString("10.1.2.3/4"), "10.1.2.3/4");
}

} // namespace cma::provider

0 comments on commit 3e3f7a1

Please sign in to comment.