Module: check_mk
Branch: master
Commit: fc99702ccc2e615a3eb7092ae86875c369fdcee7
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=fc99702ccc2e61…
Author: Jukka Aro <ja(a)mathias-kettner.de>
Date: Tue Mar 20 12:00:48 2018 +0100
Win_agent: use std library for file write time
std::experimental::filesystem::last_write_time does the trick easier
than WinAPI FindFirstFile and conversions from the fields of the
resulting WIN32_FIND_DATA struct.
---
agents/windows/build_version | 2 +-
agents/windows/sections/Section.cc | 15 ------------
agents/windows/sections/SectionFileinfo.cc | 24 +++++++++++--------
agents/windows/sections/SectionPS.cc | 20 +++++++++++++---
agents/windows/sections/SectionSpool.cc | 37 ++++++++++--------------------
5 files changed, 44 insertions(+), 54 deletions(-)
diff --git a/agents/windows/build_version b/agents/windows/build_version
index b4ca7ef..517d20b 100644
--- a/agents/windows/build_version
+++ b/agents/windows/build_version
@@ -1 +1 @@
-3194
+3196
diff --git a/agents/windows/sections/Section.cc b/agents/windows/sections/Section.cc
index ea2380e..60cf4be 100644
--- a/agents/windows/sections/Section.cc
+++ b/agents/windows/sections/Section.cc
@@ -27,21 +27,6 @@
#include "Environment.h"
#include "Logger.h"
-namespace section_helpers {
-
-static const double SEC_TO_UNIX_EPOCH = 11644473600.0;
-static const double WINDOWS_TICK = 10000000.0;
-
-double file_time(const FILETIME *filetime) {
- ULARGE_INTEGER uli{0};
- uli.LowPart = filetime->dwLowDateTime;
- uli.HighPart = filetime->dwHighDateTime;
-
- return (double(uli.QuadPart) / WINDOWS_TICK) - SEC_TO_UNIX_EPOCH;
-}
-
-} // namespace section_helpers
-
Section::Section(const std::string &outputName, const std::string &configName,
const Environment &env, Logger *logger,
const WinApiAdaptor &winapi)
diff --git a/agents/windows/sections/SectionFileinfo.cc
b/agents/windows/sections/SectionFileinfo.cc
index a3a51a1..170d213 100644
--- a/agents/windows/sections/SectionFileinfo.cc
+++ b/agents/windows/sections/SectionFileinfo.cc
@@ -23,11 +23,16 @@
// Boston, MA 02110-1301 USA.
#include "SectionFileinfo.h"
+#include <chrono>
#include <cstring>
+#include <experimental/filesystem>
#include <iomanip>
#include <sstream>
#include "Logger.h"
+namespace chrono = std::chrono;
+namespace fs = std::experimental::filesystem;
+
SectionFileinfo::SectionFileinfo(Configuration &config, Logger *logger,
const WinApiAdaptor &winapi)
: Section("fileinfo", "fileinfo", config.getEnvironment(),
logger, winapi)
@@ -138,17 +143,16 @@ void SectionFileinfo::outputFileinfos(std::ostream &out, const
char *path) {
bool SectionFileinfo::outputFileinfo(std::ostream &out,
const std::string filename) {
- WIN32_FIND_DATA findData;
- SearchHandle findHandle{_winapi.FindFirstFile(filename.c_str(), &findData),
- _winapi};
- if (findHandle) {
- unsigned long long size =
- (unsigned long long)findData.nFileSizeLow +
- (((unsigned long long)findData.nFileSizeHigh) << 32);
- out << filename << "|" << size << "|"
<< std::fixed
- << std::setprecision(0)
- << section_helpers::file_time(&findData.ftLastWriteTime) <<
"\n";
+ const fs::path path{filename};
+
+ try {
+ out << filename << "|" << fs::file_size(path)
<< "|";
+ const auto timeEntry = chrono::duration_cast<chrono::seconds>(
+ fs::last_write_time(path).time_since_epoch());
+ out << std::fixed << std::setprecision(0) << timeEntry.count()
<< "\n";
return true;
+ } catch (const fs::filesystem_error &e) {
+ Error(_logger) << e.what();
}
return false;
}
diff --git a/agents/windows/sections/SectionPS.cc b/agents/windows/sections/SectionPS.cc
index df0598c..46966ed 100644
--- a/agents/windows/sections/SectionPS.cc
+++ b/agents/windows/sections/SectionPS.cc
@@ -29,6 +29,21 @@
#include "PerfCounter.h"
#include "dynamic_func.h"
+namespace {
+
+const unsigned long long SEC_TO_UNIX_EPOCH = 11644473600L;
+const unsigned long WINDOWS_TICK = 10000000;
+
+inline unsigned long long sinceEpoch(const FILETIME &filetime) {
+ ULARGE_INTEGER uli{0};
+ uli.LowPart = filetime.dwLowDateTime;
+ uli.HighPart = filetime.dwHighDateTime;
+
+ return uli.QuadPart / WINDOWS_TICK - SEC_TO_UNIX_EPOCH;
+}
+
+} // namespace
+
SectionPS::SectionPS(Configuration &config, Logger *logger,
const WinApiAdaptor &winapi)
: Section("ps", "ps", config.getEnvironment(), logger, winapi)
@@ -308,9 +323,8 @@ bool SectionPS::outputNative(std::ostream &out) {
}
// Uptime
- double ft = section_helpers::file_time(&createTime);
- ULONGLONG uptime =
- static_cast<ULONGLONG>(section_helpers::current_time() - ft);
+ auto uptime = static_cast<unsigned long long>(
+ section_helpers::current_time() - sinceEpoch(createTime));
// Note: CPU utilization is determined out of usermodetime and
// kernelmodetime
diff --git a/agents/windows/sections/SectionSpool.cc
b/agents/windows/sections/SectionSpool.cc
index efff191..105aa04 100644
--- a/agents/windows/sections/SectionSpool.cc
+++ b/agents/windows/sections/SectionSpool.cc
@@ -24,12 +24,14 @@
#include "SectionSpool.h"
#include <sys/types.h>
+#include <chrono>
#include <experimental/filesystem>
#include "Environment.h"
#include "Logger.h"
#include "WinApiAdaptor.h"
#include "types.h"
+namespace chrono = std::chrono;
namespace fs = std::experimental::filesystem;
SectionSpool::SectionSpool(const Environment &env, Logger *logger,
@@ -43,37 +45,22 @@ bool SectionSpool::produceOutputInner(std::ostream &out,
// the agent output. The name of the files may begin with a number
// of digits. If this is the case then it is interpreted as a time
// in seconds: the maximum allowed age of the file. Outdated files
- // are simply being ignored.
- //
- time_t now = time(0);
-
+ // are simply ignored.
for (const auto &de : fs::directory_iterator(_env.spoolDirectory())) {
const auto &path = de.path();
const auto filename = path.filename().string();
const int max_age = isdigit(filename[0]) ? atoi(filename.c_str()) : -1;
if (max_age >= 0) {
- WIN32_FIND_DATA filedata{0};
- SearchHandle searchHandle{
- _winapi.FindFirstFileEx(path.string().c_str(),
- FindExInfoStandard, &filedata,
- FindExSearchNameMatch, NULL, 0),
- _winapi};
- if (searchHandle) {
- double mtime =
- section_helpers::file_time(&(filedata.ftLastWriteTime));
- int age = now - mtime;
- if (age > max_age) {
- Informational(_logger)
- << " " << filename
- << ": skipping outdated file: age is " <<
age
- << " sec, "
- << "max age is " << max_age << "
sec.";
- continue;
- }
- } else {
- Warning(_logger)
- << " " << filename << ": cannot
determine file age";
+ const auto age =
+ chrono::duration_cast<chrono::seconds>(
+ chrono::system_clock::now() - fs::last_write_time(path))
+ .count();
+ if (age > max_age) {
+ Informational(_logger)
+ << " " << filename << ": skipping
outdated file: age is "
+ << age << " sec, "
+ << "max age is " << max_age << "
sec.";
continue;
}
}